<?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/
***************************************************************************/
define("INCOMING_CALL", 1);
define("MISSED_CALL", 2);
define("ACTIVE_CALL", 3);
class CallsPlugin implements IPlugin
{
private $dbconn = NULL;
private $config = NULL;
public function __construct($dbconn, $config) {
$this->dbconn = $dbconn;
$this->config = $config;
}
public function processCallEvent($event, $src_name, $src_address, $src_numb, $dst_name, $dst_address, $dst_numb, $duration) {
if ($event == 'in:request') {
// show incomming call
// clear previous incomming calls from database
$this->clearCalls(INCOMING_CALL);
// write new incomming call to database
$this->addCall(INCOMING_CALL, $src_name, $src_address, $src_numb, 0);
} elseif ($event == 'in:cancel') {
// clear previous incomming calls from database
$this->clearCalls(INCOMING_CALL);
// add missed call
$this->addCall(MISSED_CALL, $src_name, $src_address, $src_numb, 0);
} elseif ($event == 'out:request') {
// clear missed calls (user seems to be back, since he took up phone and tried to call somebody
$this->clearCalls(MISSED_CALL);
// clear also incomming calls (to enable user to reset orphaned calls)
$this->clearCalls(INCOMING_CALL);
} elseif ($event == 'in:connect') {
// clear previous incomming calls from database
$this->clearCalls(INCOMING_CALL);
// show active connection
$this->addCall(ACTIVE_CALL, $src_name, $src_address, $src_numb, 0);
} elseif ($event == 'out:connect') {
// show active connection
$this->addCall(ACTIVE_CALL, $dst_name, $dst_address, $dst_numb, 0);
} elseif (($event == 'in:disconnect') || ($event == 'out:disconnect')) {
// clear active connection
$this->clearCalls(ACTIVE_CALL);
// if duration was less than 10 seconds add call to missed calls (call gets only connected
// because of answering machine, but then caller hung up without leaving a message)
$treshold = $this->config['missedcall_duration_treshold'];
if (($treshold == null) || ($treshold == ""))
$treshold = -1;
if (($event == 'in:disconnect') && ($duration <= 10)) {
$this->addCall(MISSED_CALL, $src_name, $src_address, $src_numb, $duration);
}
}
}
public function isPhoneRinging() {
// delete old orphaned incoming calls (older than 2 minutes)
$query = "DELETE FROM if_calls WHERE (type = 1) AND (timestamp < DATE_SUB(NOW(), INTERVAL 2 MINUTE))";
mysql_query($query, $this->dbconn) or die('Error, delete query failed');
// now read current incomming calls
$query = "SELECT * FROM if_calls where type = 1";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0)
return true;
else
return false;
}
public function doUpdate() {
}
// returns
public function doOutput($image, $style, $updateData, &$yoffset) {
// print incomming call
$query = "SELECT name, address FROM if_calls where type = 1";
$result = mysql_query($query, $this->dbconn);
$incomingCallActive = false;
if (mysql_num_rows($result) > 0) {
$incomingCallActive = true;
$row = mysql_fetch_assoc($result);
// format output to display caller name in biggest font size possible
$imageWidth = imagesx($image);
$opt = array(
'width' => $imageWidth,
'align' => ALIGN_CENTER
);
$text = $row['name'];
$size = getBiggestFontsizeForText($style['font'], $imageWidth-20, $text);
$height = imagettftextboxopt($image, $size, 0, 0, 400-$size, $style['textcolor'], $style['font'], $text, $opt);
// draw call icon
$wicon = ImageCreateFromPNG ('resources/icons/phone.png');
ImageCopy($image, $wicon, ($imageWidth/2)-(imagesx($wicon)/2), 400-$size-imagesy($wicon), 0, 0, imagesx($wicon), imagesy($wicon));
//ImageCopy($image, $wicon, $imageWidth-imagesx($wicon)-20, 10, 0, 0, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
imagettftextboxopt($image, 32, 0, 0, 420-$size+$height, $style['textcolor'], $style['font'], $row['address'], $opt);
}
mysql_free_result($result);
if ($incomingCallActive)
return;
// print missed calls
$query = "SELECT * FROM `if_calls` WHERE (type = 2) ORDER BY timestamp DESC";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
$opt_header = array(
'width' => imagesx($image)-300,
'line_height' => 18,
'align' => ALIGN_LEFT
);
$opt_calls = array(
'width' => imagesx($image)-300,
'line_height' => 14,
'align' => ALIGN_LEFT
);
// print header
$icon = ImageCreateFromPNG ( 'resources/icons/call.png' );
ImageCopy($image, $icon, 20, $yoffset-2, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
imagettftextboxopt($image, 18, 0, 50, $yoffset, $style['textcolor'], $style['font'], "Verpasste Anrufe", $opt_header);
$yoffset += 22;
// print calls list
$counter = 0;
while ($row = mysql_fetch_assoc($result)) {
// if max count of displayed entries is reached OR end of screen is reached and more than one items left
// -> cut off and show hint "x more items..."
$counter++;
$rest = (mysql_num_rows($result) - $counter) + 1;
$entrylimit = $this->config['max_displayed_missed_calls'];
if ( (($entrylimit) && ($counter > $entrylimit)) || (($yoffset >= (imagesy($image) - 36)) && ($rest > 1)) ) {
// show only n calls. If more calls are available show hint "x more calls..."
if ($rest > 1)
$text = "... $rest weitere Anrufe";
else
$text = "... $rest weiterer Anruf";
imagettftextboxopt($image, 14, 0, 24, $yoffset, $style['textcolor'], $style['fontb'], $text, $opt_calls);
$yoffset += 22;
break;
}
$time = strftime("%d.%m.%Y, %H:%M", strtotime($row['timestamp']));
$missedcalls = $time." - ".$row['name'];
if ($row['duration'] > 0)
$missedcalls = $missedcalls." (".$row['duration']."s)";
imagettftextboxopt($image, 14, 0, 50, $yoffset, $style['textcolor'], $style['font'], $missedcalls, $opt_calls);
$yoffset += 16;
}
$yoffset += 20;
}
mysql_free_result($result);
// print active call
$query = "SELECT name, address FROM if_calls where type = 3";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$opt = array(
'width' => 400,
'align' => ALIGN_CENTER
);
imagettftextboxopt($image, 18, 0, 30+((imagesx($image)-400)/2), 20, $style['textcolor'], $style['font'], "Aktives Gespräch mit:\n".$row['name'], $opt);
}
mysql_free_result($result);
}
private function addCall($type, $name, $address, $number, $duration) {
if ($name == "") $name = $number;
$query = "INSERT INTO if_calls (id, type, timestamp, name, address, duration) VALUES (NULL, $type, CURRENT_TIMESTAMP, '$name', '$address', $duration)";
mysql_query($query, $this->dbconn) or die('Error, insert query failed: '.mysql_error());
}
private function clearCalls($type) {
$query = "Delete from if_calls where type = $type";
mysql_query($query, $this->dbconn) or die('Error, delete query failed');
}
}