* 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 sel_msn-Option-List
if ($do == "getSelMsnOptions" ) {
global $selection_msn_all, $msn_sort_order;
$msn_sort_order = $_POST["sort"];
$arr_msn = getMsnList();
$selMsnOpt = " $selection_msn_all ";
foreach ($arr_msn as $key => $val) {
$xKey = str_replace("*","_", $key);
$selMsnOpt .= " {$val['desc']} ";
}
$ret['success']=$selMsnOpt;
die(json_encode($ret));
}
// create config-dialog
if ($do == "getDlgKonfigMSN" ) {
global $msn_sort_order;
$msn_sort_order = (isset($_POST["sort"])) ? $_POST["sort"] : $msn_sort_order;
$arr_msn = getMsnList();
$tr = "
";
foreach ($arr_msn as $msn => $val) {
$xMSN = str_replace("*","_", $msn);
$fcolor = color_name_to_hex($val['fcolor']); // color out of msn-replace.txt
$bcolor = color_name_to_hex($val['bcolor']);
$style = "style='color:$fcolor; background-image: linear-gradient(to bottom, $bcolor, transparent 70%)'";
$msn = $val['msn'];
$tr .= "
{$val['msn']}
{$val['desc']}
";
}
$ret['params']['reset'] = $config_reset;
$ret['params']['ok'] = $config_ok;
$ret['success']=$tr;
die(json_encode($ret));
}
// create phonelist tbody
if ($do == "getStatistik" ) {
global $msn_sort_order;
$msn_sort_order = $_POST['msnsort'];
$arr_msn = getMsnList();
$cf = fopen($call_file, 'r');
if (! $cf) throw new Exception("Cannot open file '$call_file'!");
$i = 0;
while (!feof($cf)) {
$line = rtrim(fgets($cf, 500));
// 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'], $arr_phone_calls[$i]['direction'], $arr_phone_calls[$i]['duration']) = array_pad(explode("\t",$line ), 12, '');
// 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 (! $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);
$maxcount = $i;
$firstDate = strtotime($arr_phone_calls[0]['date']);
$lastDate = strtotime($arr_phone_calls[$i-1]['date']);
$diff = floor(($lastDate -$firstDate)/3600/24) +1 ;
$average = number_format($maxcount / $diff,0);
$firstDate = date("d.m.Y",$firstDate);
$lastDate = date("d.m.Y",$lastDate);
$Tab1 = "
Zeitraum: $firstDate - $lastDate
$maxcount Anrufe in $diff Tagen
Ø $average Anrufe pro Tag
";
foreach ($arr_msn as $nbr=>$msn_replace) {
$z = 0;
$calls = 0;
$calls_yesterday = 0;
$calls_lastweek = 0;
$calls_wo_today = 0;
$calls_per_day = 0;
foreach ( $arr_phone_calls as $zeile ) {
if ($zeile['to']==$nbr) {
// overall sum
$calls++;
// sum of calls without todays
if ( $zeile['date']0 && $diff>0) ? number_format($calls_wo_today/$diff,2) : 0;
$numbers[$nbr] = array('days'=>$diff, 'calls'=>$calls, 'avg'=>$average, 'yesterday'=>$calls_yesterday, 'lastweek'=>$calls_lastweek) ;
}
$thead2 = "
Teilnehmer-Anschluss
Gestern
Letzte Woche (KW ".date('W',time()-(60*60*24*7)).")
Tagesschnitt
Gesamter Zeitraum (Anteil)
";
$tbody2 = "";
foreach ($arr_msn as $nbr=>$msn_replace) {
$bar = bar($maxcount,$numbers[$nbr]['calls']);
$tbody2 .= "
{$arr_msn[$nbr]['desc']}
{$numbers[$nbr]['yesterday']}
{$numbers[$nbr]['lastweek']}
{$numbers[$nbr]['avg']}
{$numbers[$nbr]['calls']}
$bar
";
$i++;
}
$Tab2 = "";
$ret['params']['preview'] = $statistic_preview;
$ret['params']['ok'] = $statistic_ok;
$ret['success'] = "$Tab1 $Tab2";
die(json_encode($ret));
}
// 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";
$filter_inout = (isset($_POST['filter_inout'])) ? $_POST['filter_inout'] : "INOUT";
$flags = (isset($_POST['flags'])) ? ($_POST['flags'] == "true") : (! $no_flags);
$fromlink = (isset($_POST['fromlink'])) ? ($_POST['fromlink'] == "true") : (! $no_from_link);
$calldirection = (isset($_POST['calldirection'])) ? ($_POST['calldirection'] == "true") : (! $no_call_direction);
$callduration = (isset($_POST['callduration'])) ? ($_POST['callduration'] == "true") : (! $no_call_duration);
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; INOUT: $filter_inout";
// 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'
// 'calldirection'-> IN/OUT
// 'callduration' -> [m:ss]
// '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'
$cf = fopen($call_file, 'r');
if (! $cf) throw new Exception("Cannot open file '$call_file'!");
$i = 0;
while (!feof($cf)) {
// write calls to array
$linedata = explode("\t",rtrim(fgets($cf, 500)));
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]['direction'], $arr_phone_calls[$i]['duration'],
$arr_phone_calls[$i]['to_replaced'], $arr_phone_calls[$i]['flag_file'], $arr_phone_calls[$i]['fcolor'],
$arr_phone_calls[$i]['bcolor']) = array_pad($linedata, 12, '');
$arr_phone_calls[$i]['direction'] = (strlen($arr_phone_calls[$i]['direction']) == 0) ? "IN" : $arr_phone_calls[$i]['direction'];
$arr_phone_calls[$i]['cntFields'] = count($linedata);
// 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
if ( $arr_phone_calls[$i]['direction'] == "IN") {
$msn_search = $arr_phone_calls[$i]['to'];
$arr_phone_calls[$i]['to_replaced'] = $arr_msn[$msn_search]['desc'];
$arr_phone_calls[$i]['from_replaced'] = $arr_phone_calls[$i]['from'];
} else {
$msn_search = $arr_phone_calls[$i]['from'];
$arr_phone_calls[$i]['from_replaced'] = $arr_msn[$msn_search]['desc'];
$arr_phone_calls[$i]['to_replaced'] = $arr_phone_calls[$i]['to'];
}
$arr_phone_calls[$i]['fcolor'] = $arr_msn[$msn_search]['fcolor'];
$arr_phone_calls[$i]['bcolor'] = $arr_msn[$msn_search]['bcolor'];
// add flag file name
$check_number = ( $arr_phone_calls[$i]['direction'] == "IN") ? $arr_phone_calls[$i]['from'] : $arr_phone_calls[$i]['to'] ;
$arr_phone_calls[$i]['flag_file'] = find_flag($check_number);
if (! $flags) {
// add country abbreviation
$arr_phone_calls[$i]['country_abbr'] = find_country_abbr($check_number);
}
// add country name
$arr_phone_calls[$i]['country_name'] = find_country_name($check_number);
$i++;
}
$i--;
$ret['maxcount'] = $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 ...
$searchMSN = str_replace("*","_",$filter_msn);
$searchTO = str_replace('*','_',$phone_call['to']);
$searchFROM = str_replace('*','_',$phone_call['from']);
$equal = ($searchMSN === $searchTO || $searchMSN === $searchFROM );
if ($filter_msn == "0" || $equal) {
if (strpos($filter_inout, $phone_call['direction']) !== false ) {
// 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 .= " \n";
$line_count = 0;
}
$NoButton = ($phone_call['from'] == $call_unknown); // no caller-number
$EditButton = ($NoButton) ? "" : " ";
// toggle background color
$trclass = "call"; // default backgroud-color
if ($phone_call['direction'] == "IN") { // set color for MSN if defined in msn-replace
$trclass .= " msn" . str_replace("*","_",$phone_call['to']);
} else {
$trclass .= " msn" . str_replace("*","_",$phone_call['from']);
}
$trclass .= ($EditButton == "") ? "" : " editable";
$flag = ($flags) ? " " : $phone_call['country_abbr'] ;
$callFrom = $phone_call['from'];
$callFromReplace = $phone_call['from_replaced'];
$calltime = $phone_call['time'];
$callToReplace = $phone_call['to_replaced'];
$callTo = $phone_call['to'];
$callName = $phone_call['name'];
$callAddr = $phone_call['address'];
$cntFields = $phone_call['cntFields'];
$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
$data_duration = ""; $tdCall_duration = ""; $data_inout="";
if ($callduration) $tdCall_duration = "{$phone_call['duration']} ";
if ($cntFields == 8) {
$data_duration = "data-duration=\"{$phone_call['duration']}\"";
$data_inout = "data-inout=\"{$phone_call['direction']}\"";
}
$td3class = ($calldirection) ? "call_{$phone_call['direction']}" : '';
$td3class = ($phone_call['duration'] == '0:00') ? $td3class.'_FAILED' : $td3class;
$td3class = "class='$td3class'";
$thisnumber = ($phone_call['direction'] == "IN") ? $callFrom : $callTo ;
$tbody .= "
$calltime
$flag
$callFromReplace
$callToReplace
$callAddr
$tdCall_duration
$EditButton
";
$line_count++;
}
}
}
} // end of foreach
$tbody = ($tbody == "") ? "$text_nbr_not_found " : $tbody;
} // need update
// if ($callduration) $ret['call_duration']=$call_duration;
$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";
$direction = (isset($_POST['direction'])) ? $_POST['direction'] : false;
$duration = (isset($_POST['duration'])) ? $_POST['duration'] : false;
// 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)) {
$linedata = explode("\t", rtrim(fgets($cf, 500)));
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]['direction'], $arr_phone_calls[$i]['duration']) = array_pad($linedata, 8, '');
$arr_phone_calls[$i]['cntFields'] = count($linedata);
$testnbr = ($arr_phone_calls[$i]['direction'] == "OUT") ? $arr_phone_calls[$i]['to'] :$arr_phone_calls[$i]['from'];
if ($testnbr == $_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']) ;
if ($phone_call['cntFields'] == 8) {
$line = $line . "\t" . $phone_call['direction'] . "\t" . $phone_call['duration'];
}
$done = fwrite($cf, $line . "\n");
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));
function bar($sum=0, $val=0) {
$v = ($sum>0 && $val>0) ? number_format(100/$sum*$val,2) : 0;
$ratio = ($sum>0 && $val>0) ? floor(0.8*$v) : 0;
$b = "
";
return "$b( $v% )
";
}
?>