<?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 );
// 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']);
}
// 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['textcolorgelb'] = ImageColorAllocate ($im, 254, 251, 0);
$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);
}
// 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, 24, 0, 20, 100, $style['textcolor'], $style['font'], strftime("%A, %d. %B %Y"), $opt);
// kw anzeige anfang
$kw_aktuell = (int)date('W');
imagettftextboxopt($im, 16, 0, 350 , 5, $style['textcolor'], $style['font'], "Kalenderwoche ".$kw_aktuell, $opt);
// kw anzeige ende
imagesetthickness($im, 2);
imageline($im, 15, 140, $image_width-20, 140, $style['textcolor']);
// 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();
?>