<?php
/***************************************************************************
* InfoFrame (image generator for digital picture frames)
* Copyright (C) 2009 Tobias Kolb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
***************************************************************************/
//***************************************************************************************************
// Initialization
//***************************************************************************************************
// set error handling to only report errors (no warnings, infos...)
//error_reporting( E_ERROR );
function datumDeutsch($datumsstring){
$englisch = array("Morgen", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Motag");
$deutsch = array("Morgen", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So", "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "Montag");
return str_replace($englisch, $deutsch, $datumsstring);
}
// read Temperature File
/*
$TEMPhandle = fopen ("cache/akttemp.txt", "r");
$TEMPbuffer = fgets($TEMPhandle, 100);
$TEMPbuffer1 = fgets($TEMPhandle, 100);
fclose ($TEMPhandle);
// print temperature
//Sensor0
$pos = strpos($TEMPbuffer, 'C:') + 3;
$posa = strpos($TEMPbuffer, ' ', $pos);
$TempAkt = substr($TEMPbuffer, $pos, $posa - $pos);
//Sensor1
$pos1 = strpos($TEMPbuffer1, 'C:') + 3;
$pos1a = strpos($TEMPbuffer, ' ', $pos1);
$TempAkt1 = substr($TEMPbuffer1, $pos1, $pos1a - $pos1);
*/
// load configuration
$config = parse_ini_file("config.ini", true);
$sysconfig = $config['System'];
// include path for zend framework
if ($sysconfig['zendfw_path'] != "") {
set_include_path(get_include_path() . PATH_SEPARATOR . $sysconfig['zendfw_path']);
}
if ($sysconfig['google_api_path'] != "") {
set_include_path(get_include_path() . PATH_SEPARATOR . $sysconfig['google_api_path']);
}
// set locale for date/time formatting
$loc = setlocale(LC_ALL, 'de_DE.UTF8', 'de_DE', 'de', 'ge');
// includes
require_once 'library/tools.php';
require_once 'library/dbconn.php';
require_once 'library/iplugin.php';
// set the width and height of the new image in pixels
$image_width = $sysconfig['image_width'];
$image_height = $sysconfig['image_height'];
// create simple black image
$im = ImageCreateTrueColor($image_width, $image_height);
$backgroundcol = ImageColorAllocate($im, 0, 0, 0);
ImageFillToBorder($im, 0, 0, $backgroundcol, $backgroundcol);
// copy (resized) background image on background
$bgimagefile = 'resources/background.jpg';
$bg = @ImageCreateFromJpeg ($bgimagefile); /* Versuch, Datei zu öffnen */
if ($bg) {
imagecopyresampled($im, $bg, 0, 0, 0, 0, $image_width, $image_height, imagesx($bg), imagesy($bg));
}
// open database connection
$dbconn = DbConnection::connect($sysconfig['db_host'], $sysconfig['db_name'], $sysconfig['db_user'], $sysconfig['db_password']);
if (!$dbconn)
die('keine Datenbankverbindung möglich: ' . mysql_error());
// create cache directory if it doesn't exist already
if (!file_exists('cache/'))
mkdir('cache');
// load and init plugins (calls, weather, calendar, mails...)
// every ini [group] except "System" is used as class name for a plugin and instantiated
$plugins = array();
foreach ($config as $key=>$elem) {
if ($key != "System") {
// include class file
require_once 'plugins/' . $key . '.php';
// create instance
$plugins[$key] = new $key($dbconn, $config[$key]);
}
}
// process call action if exist
if(($_GET['action'] == 'call') && $plugins['CallsPlugin'])
{
$plugins['CallsPlugin']->processCallEvent($_GET['event'], $_GET['src_name'], $_GET['src_address'], $_GET['src_numb'], $_GET['dst_name'], $_GET['dst_address'], $_GET['dst_numb'], $_GET['duration']);
}
//***************************************************************************************************
// Check if plugin data is outdated and should be updated
//***************************************************************************************************
// do update of plugins data if last update is older than config:updatetime minutes
$updateInterval = $sysconfig['updatetime'];
if (($updateInterval == null) || ($updateInterval == ""))
$updateInterval = 5;
$updatePlugins = false;
$query = "SELECT value FROM if_system where name = 'last_update'";
$result = mysql_query($query, $dbconn);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$diff_seconds = (time() - $row['value']);
if ($diff_seconds >= (5*60))
{
$updatePlugins = true;
// update last_update date
$query = "UPDATE if_system SET value = '".time()."' WHERE name = 'last_update'";
mysql_query($query, $dbconn) or die('Error, insert query failed: '.mysql_error());
}
} else {
$updatePlugins = true;
// insert last_update date
$query = "INSERT INTO if_system (name, value) VALUES ('last_update', '".time()."')";
mysql_query($query, $dbconn) or die('Error, insert query failed: '.mysql_error());
}
mysql_free_result($result);
// debug: never do updates
//$updatePlugins = false;
//***************************************************************************************************
// Output
//***************************************************************************************************
// styles
$style = array();
$style['textcolor'] = ImageColorAllocate ($im, 255, 255, 255);
$style['font'] = 'resources/calibri.ttf';
$style['fontb'] = 'resources/calibrib.ttf';
// current vertical offset for main info area. Must be increased by each plugin writing to this area,
// so that following plugins knows correct y position to start output from
$currentYOffset = 160;
// Do output of all plugins (calls, weather, calendar, mails...) with one exception:
// If the phone is currently ringing, skip displaying other plugins to show the name/number
// of the caller as fast and huge as possible and don't waste time/space for weather, mails etc...
if (($plugins['CallsPlugin']) && ($plugins['CallsPlugin']->isPhoneRinging())) {
// only print calls
$plugins['CallsPlugin']->doOutput($im, $style, $updatePlugins, $currentYOffset);
} else {
// print all plugins in the order as they are in config
foreach ($plugins as $plugin) {
$plugin->doOutput($im, $style, $updatePlugins, $currentYOffset);
}
$opt1 = array(
'width' => 150,
'align' => ALIGN_RIGHT
);
/*imagettftextboxopt($im, 20, 0, $image_width-162, 113, $style['textcolor'], $style['font'], ' '.str_pad(number_format($TempAkt,1),3,'0',STR_PAD_LEFT).'°C', $opt1);
$Baum = ImageCreateFromPNG ( 'resources/icons/tree.png' );
ImageCopy($im, $Baum, $image_width-108, 111, 0, 0, imagesx($Baum), imagesy($Baum));
ImageDestroy($Baum);
imagettftextboxopt($im, 20, 0, $image_width-285, 113, $style['textcolor'], $style['font'], ' '.str_pad(number_format($TempAkt1,1),3,'0',STR_PAD_LEFT).'°C', $opt1);
$Haus = ImageCreateFromPNG ( 'resources/icons/house.png' );
ImageCopy($im, $Haus, $image_width-235, 112, 0, 0, imagesx($Haus), imagesy($Haus));
ImageDestroy($Haus);
*/
// check again for active incomming call (maybe we just get one in the meantime while updating/printing all other plugins)
// if so don't output any image to prevent overwriting the (incoming call) image already sent to another request
if (($plugins['CallsPlugin']) && ($plugins['CallsPlugin']->isPhoneRinging()))
die();
}
// print date & time
$opt = array(
'width' => 450,
'align' => ALIGN_LEFT
);
imagettftextboxopt($im, 72, 0, 20, 25, $style['textcolor'], $style['font'], strftime("%H:%M"), $opt);
imagettftextboxopt($im, 20, 0, 20, 100, $style['textcolor'], $style['font'], datumDeutsch(strftime("%A, %d. %B %Y (KW%V)")), $opt);
imagesetthickness($im, 2);
imageline($im, 15, 140, $image_width-20, 140, $style['textcolor']);
ImageJpeg ($im, 'cache/info.jpg', 90);
// resize image
//$resized_image = imagecreatetruecolor(480, 324);
//imagecopyresampled($resized_image, $im, 0, 0, 0, 0, imagesx($resized_image), imagesy($resized_image), imagesx($im), imagesy($im));
//$im = $resized_image;
// set the HTTP header type to jpeg
header("Content-type: image/jpeg");
// send the new PNG image to the browser
ImageJpeg ($im);
// destroy the reference pointer to the image in memory to free up resources
ImageDestroy($im);
// close database connection
DbConnection::disconnect();
?>