* Rolf Bensch, rolf(at)bensch-net(dot)de
*
* Creation: 2016-08-25 jed
* Last Update: $Id$
*
* 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 2 of the License, or
* (at your option) any later version.
**************************************************************************
*/
header('content-type: text/html; charset=utf-8;');
error_reporting(E_ALL); // if "require" fails
/* capit2text AJaX-Routinen */
require('capi2text-default-params.inc');
require("../capi2text-params.inc");
require('capi2text-functions.php');
setlocale (LC_ALL, 'de_DE');
$ret = array();
if ($debug) {
error_reporting(E_ALL);
} else {
// disable error handling in production mode
error_reporting(0);
// suppress php Warning|Notice|Error in output
ob_start();
}
try {
if (! isset($_POST['do'])) throw new Exception("AJaX: missing parameter");
$do = $_POST["do"];
// create phonelist tbody
if ($do == "getTbody" ) {
$lastUpdate = (isset($_POST['lastUpdate'])) ? $_POST['lastUpdate'] : "0";
$filter_msn = (isset($_POST['filter_msn'])) ? $_POST['filter_msn'] : "0";
$filter_days = (isset($_POST['filter_days'])) ? $_POST['filter_days'] : "0";
if (! $call_file) throw new Exception("getTbody: 'call_file' not defined");
if (! file_exists($call_file)) throw new Exception("getTbody: '$call_file' not found");
$tbody = '';
$callFileTS = filemtime($call_file);
$needUpdate = ($lastUpdate < $callFileTS);
if ($needUpdate) {
$ret['needUpdate'] = $callFileTS;
$ret['filter'] = "Filter: Days: $filter_days; MSN: $filter_msn";
// get MSN list for select-box (filter)
$arr_msn = getMsnList();
// read call logfile
//
// 'date' -> date stamp, e.g. '2012-03-13'
// 'time' -> time stamp, e.g. '14:15:59'
// 'from' -> incoming phone number, e.g. '0221334455'
// 'to' -> called MSN (extension), e.g. '31'
// 'name' -> name of caller, e.g. 'Hans Mueller'
// 'address' -> address of caller, e.g. 'Hauptstr. 111, 41472 Neuss, DE'
// 'to_replaced' -> called MSN replacement string, e.g. '31 - fax machine'
// 'flag_file' -> path and name of country flag, e.g. './png/country-49.png'
// 'country_abbr' -> abbreviation of the country, e.g. 'DE'
// 'country_name' -> name of the country, e.g. 'Deutschland'
// 'fcolor' -> individual foreground color for this call, based on the MSN, e.g. '#ffee00'
// 'bcolor' -> individual background color for this call, based on the MSN, e.g. '#ffee00'
//
$arr_phone_calls = array(array('date' => '', 'time' => '', 'from' => '', 'to' => '', 'name' => '',
'address' => '', 'to_replaced' => '', 'flag_file' => '', 'country_abbr' => '',
'country_name' => '', 'fcolor' => '', 'bcolor' => ''));
$cf = fopen($call_file, 'r');
if (! $cf) throw new Exception("Cannot open file '$call_file'!");
$i = 0;
while (!feof($cf)) {
// write calls to array
list($arr_phone_calls[$i]['date'], $arr_phone_calls[$i]['time'], $arr_phone_calls[$i]['from'],
$arr_phone_calls[$i]['to'], $arr_phone_calls[$i]['name'], $arr_phone_calls[$i]['address'],
$arr_phone_calls[$i]['to_replaced'], $arr_phone_calls[$i]['flag_file'], $arr_phone_calls[$i]['fcolor'],
$arr_phone_calls[$i]['bcolor']) = array_pad(explode("\t", rtrim(fgets($cf, 500))), 10, '');
// suppress loginfo, like '[Ws]' etc.
if ($no_loginfo && preg_match("/ \[[CLW]s*\]/", $arr_phone_calls[$i]['name'])) {
$arr_phone_calls[$i]['name'] = preg_replace("/ \[[CLW]s*\]/", "", $arr_phone_calls[$i]['name']);
}
// replace MSN by description
$msn_search = $arr_phone_calls[$i]['to'];
$arr_phone_calls[$i]['to_replaced'] = $arr_msn[$msn_search]['desc'];
$arr_phone_calls[$i]['fcolor'] = $arr_msn[$msn_search]['fcolor'];
$arr_phone_calls[$i]['bcolor'] = $arr_msn[$msn_search]['bcolor'];
// add flag file name
$arr_phone_calls[$i]['flag_file'] = find_flag($arr_phone_calls[$i]['from']);
if ($no_flags) {
// add country abbreviation
$arr_phone_calls[$i]['country_abbr'] = find_country_abbr($arr_phone_calls[$i]['from']);
}
// add country name
$arr_phone_calls[$i]['country_name'] = find_country_name($arr_phone_calls[$i]['from']);
$i++;
}
$i--;
fclose($cf);
// output data in reverse order
$arr_phone_calls=array_reverse($arr_phone_calls,true);
$line_count = 0;
$prevDate = strtotime("1970-01-02");
foreach($arr_phone_calls as $phone_call) {
if ( $phone_call['date'] != "" ) {
// not empty, go on ...
if ($filter_msn == 0 || $filter_msn == $phone_call['to']) {
// valid selection, go on ...
if ( $prevDate <> strtotime($phone_call['date'])) {
$prevDate = strtotime($phone_call['date']);
// calculate the number of days
$callDate = strtotime($phone_call['date']);
$dateDiff = floor((strtotime("now") - $callDate)/3600/24);
if ($filter_days > 0 && $dateDiff > $filter_days ) break;
// print date header once per day
$dummyDatum = date("d.m.Y",$callDate);
$tbody .= "
$dummyDatum |
\n";
$line_count = 0;
}
$NoButton = ($phone_call['from'] == $call_unknown); // no caller-number
$EditButton = ($NoButton) ? "" : "
";
// toggle backup color
$trclass = "call"; // default backgroud-color
$trclass .= " msn" . $phone_call['to']; // set color for MSN if defined in msn-replace
$trclass .= ($EditButton != '') ? " editable" : "";
$flag = ($no_flags) ? $phone_call['country_abbr'] : "
";
$callFrom = $phone_call['from'];
$callFromNbr = $phone_call['from'];
$callFrom = ($no_from_link) ? $callFrom : "$callFrom";
$calltime = $phone_call['time'];
$callReplace = $phone_call['to_replaced'];
$callTo = $phone_call['to'];
$callName = $phone_call['name'];
$callAddr = $phone_call['address'];
$callName = (mb_detect_encoding($callName,'UTF-8', true)) ? $callName : utf8_encode($callName) ;
$callAddr = (mb_detect_encoding($callAddr,'UTF-8', true)) ? $callAddr : utf8_encode($callAddr) ;;
$callAddr = ($callAddr == '') ? $callName : "$callName, $callAddr";
$country_name = $phone_call['country_name'];
// print call data
$tbody .= "
$calltime |
$flag |
$callFrom |
$callReplace |
$callAddr |
$EditButton |
";
$line_count++;
}
}
} // end of foreach
$tbody = ($tbody == "") ? "$text_nbr_not_found |
" : $tbody;
} // need update
$ret['success'] = remove_tabs_and_crs($tbody);
} // end of getTbody()
if ($do == "save") {
$_POST = str_replace(',', ' ',$_POST); // replace commata by whitespace
$_POST = array_map('trim', $_POST); // trim strings
$nbr = preg_replace('/[^0-9]/', '', $_POST['nbr']);
$name = $_POST['name'];
$street = $_POST['street'];
$zip = $_POST['zip'];
$city = $_POST['city'];
$country = $_POST['country'];
$addr = "$street, $zip $city, $country";
// validation of entered name and phone number
if ($name == '') throw new Exception($data_name_missing);
if ($nbr == '') throw new Exception($data_number_missing);
// ### append to phonelist ###
// not an empty entry - make sure that number is in international format
$nbr = preg_replace('/^0([0-9][0-9]+)/', "00$default_ccode$1", $nbr);
if (! is_writable($phonelist_file)) throw new Exception("File $phonelist_file not writeable.");
// read phonelist content from file
$pf_content = file_get_contents($phonelist_file);
$pf_content = explode("\n",$pf_content);
// replace matching entries
$pf_content = preg_replace("/^$nbr.*/", "$nbr\t$name\t$addr", $pf_content);
// throw new Exception(print_r($pf_content));
// write phonelist content to file
$pf_content = implode("\n",$pf_content);
$bak_file = "/tmp/" . basename($phonelist_file) . ".edit";
file_put_contents($bak_file, $pf_content);
if (! file_exists($bak_file)) throw new Exception("'$bak_file' not created.\n\nCannot update $phonelist_file");
if (filesize($bak_file) > 5) {
$phonelistPath = dirname($phonelist_file);
clearstatcache();
$phonelistPerms = substr(sprintf('%o',fileperms($phonelistPath)), -4);// & 0777;
if (!copy($bak_file, $phonelist_file)) throw new Exception("Cannot overwrite $phonelist_file. ($phonelistPath = $phonelistPerms)");
}
unlink($bak_file);
// ### update call_file ###
if (! is_writable($call_file)) throw new Exception("File $call_file not writeable");
// read content of call_file
$cf = fopen($call_file, 'r');
if (! $cf) throw new Exception("Cannot open file '$call_file'!");
$i = 0;
while (!feof($cf)) {
list($arr_phone_calls[$i]['date'], $arr_phone_calls[$i]['time'], $arr_phone_calls[$i]['from'],
$arr_phone_calls[$i]['to'], $arr_phone_calls[$i]['name'], $arr_phone_calls[$i]['address'],
$arr_phone_calls[$i]['flag_file']) = array_pad(explode("\t", rtrim(fgets($cf, 500))), 7, '');
if ($arr_phone_calls[$i]['from'] == $_POST['nbr']) {
// update record
$arr_phone_calls[$i]['name'] = $name;
$arr_phone_calls[$i]['address'] = $addr;
}
$i++;
}
fclose($cf);
// rewrite call_file
$bak_file = "/tmp/" . basename($call_file) . ".edit";
$cf = fopen($bak_file, 'w');
if (! $cf) throw new Exception("Cannot open file '$bak_file'!");
foreach ($arr_phone_calls as $phone_call) {
if (trim($phone_call['date']) <> '' ) {
// not an empty row - write it
$line = rtrim($phone_call['date'] . "\t" . $phone_call['time'] . "\t" . $phone_call['from'] .
"\t" . $phone_call['to'] . "\t" . $phone_call['name'] . "\t" . $phone_call['address']) . "\n";
$done = fwrite($cf, $line);
if (! $done) throw new Exception("Cannot write to file '$bak_file'");
}
}
fclose($cf);
if (! file_exists($bak_file)) throw new Exception("'$bak_file' not created.\n\nCannot update $phonelist_file");
if (filesize($bak_file) < 10) throw new Exception("File $bak_file without content.");
if (! copy($bak_file, $call_file))throw new Exception("Cannot overwrite $call_file.");
unlink($bak_file);
$ret['success'] = "Data saved successful";
}
if (! isset($ret['success'])) throw new Exception("Missing function: '$do'");
} catch (Exception $e) {
$ret['error'] = $e->getMessage();
}
ob_end_clean();
die(json_encode($ret));
?>