* Rolf Bensch, rolf(at)bensch-net(dot)de * * Creation: 2016-08-25 jed * Last Update: $Id: capi2text-ajax.php 43029 2016-08-26 17:15:28Z jed $ * * 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(); $ret['Debug']= $msn_sort_order; $selMsnOpt = ""; foreach ($arr_msn as $key => $val) { $xKey = str_replace("*","_", $key); $selMsnOpt .= ""; } $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 = "
MSN
Ersetzung
TF
HF
"; foreach ($arr_msn as $key => $val) { $trClass = ".msn$key"; $fcolor = color_name_to_hex($val['fcolor']); $bcolor = color_name_to_hex($val['bcolor']); $msnCSS .= "$trClass, .ui-selectmenu-menu $trClass\t {color: $fcolor; background-color:$bcolor}\n"; $style = "style='color:$fcolor; background-image: linear-gradient(to bottom, $bcolor, transparent )'"; $tr .= "
{$val['msn']}
{$val['desc']}
"; } $ret['params']['country'] = $config_country; $ret['params']['from'] = $config_from; $ret['params']['msnsort'] = $config_msnsort; $ret['params']['theme'] = $config_theme; $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)) { // 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 (! $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
"; $ret['debug'] = $arr_phone_calls[1]['to'] . " - " . $arr_phone_calls[1]['to_replaced']; 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 = "$thead2 $tbody2
"; $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"; $flags = (isset($_POST['flags'])) ? ($_POST['flags'] == "true") : (! $no_flags); $fromlink = (isset($_POST['fromlink'])) ? ($_POST['fromlink'] == "true") : (! $no_from_link); 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 (! $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--; $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 ... $equal = str_replace("*","_",$filter_msn) === str_replace('*','_',$phone_call['to']); if ($filter_msn == "0" || $equal) { // 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) ? "" : "edit entry"; // toggle background color $trclass = "call"; // default backgroud-color $trclass .= " msn" . str_replace("*","_",$phone_call['to']); // set color for MSN if defined in msn-replace $trclass .= (isset($EditButton)) ? " editable" : ""; $flag = ($flags) ? "" : $phone_call['country_abbr'] ; $callFrom = $phone_call['from']; $callFrom = ($fromlink) ? "$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)); 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% )
"; } ?>