index.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. <?php
  2. $altcfg = $ubillingConfig->getAlter();
  3. if ($altcfg['ASKOZIA_ENABLED']) {
  4. /**
  5. * Returns number aliases
  6. *
  7. * @return array
  8. */
  9. function zb_AskoziaGetNumAliases() {
  10. $result = array();
  11. $rawAliases = zb_StorageGet('ASKOZIAPBX_NUMALIAS');
  12. if (empty($rawAliases)) {
  13. $newAliasses = serialize($result);
  14. $newAliasses = base64_encode($newAliasses);
  15. zb_StorageSet('ASKOZIAPBX_NUMALIAS', $newAliasses);
  16. } else {
  17. $readAlias = base64_decode($rawAliases);
  18. $readAlias = unserialize($readAlias);
  19. $result = $readAlias;
  20. }
  21. return ($result);
  22. }
  23. /**
  24. * Returns or setups current Askozia configuration options
  25. *
  26. * @return array
  27. */
  28. function zb_AskoziaGetConf() {
  29. $result = array();
  30. $emptyArray = array();
  31. //getting url
  32. $url = zb_StorageGet('ASKOZIAPBX_URL');
  33. if (empty($url)) {
  34. $url = 'http://sip.isp/';
  35. zb_StorageSet('ASKOZIAPBX_URL', $url);
  36. }
  37. //getting login
  38. $login = zb_StorageGet('ASKOZIAPBX_LOGIN');
  39. if (empty($login)) {
  40. $login = 'admin';
  41. zb_StorageSet('ASKOZIAPBX_LOGIN', $login);
  42. }
  43. //getting password
  44. $password = zb_StorageGet('ASKOZIAPBX_PASSWORD');
  45. if (empty($password)) {
  46. $password = 'askozia';
  47. zb_StorageSet('ASKOZIAPBX_PASSWORD', $password);
  48. }
  49. //getting caching time
  50. $cache = zb_StorageGet('ASKOZIAPBX_CACHETIME');
  51. if (empty($cache)) {
  52. $cache = '1';
  53. zb_StorageSet('ASKOZIAPBX_CACHETIME', $cache);
  54. }
  55. $result['url'] = $url;
  56. $result['login'] = $login;
  57. $result['password'] = $password;
  58. $result['cachetime'] = $cache;
  59. return ($result);
  60. }
  61. /**
  62. * Renders time duration in seconds into formatted human-readable view
  63. *
  64. * @param int $seconds
  65. *
  66. * @return string
  67. */
  68. function zb_AskoziaFormatTime($seconds) {
  69. $init = $seconds;
  70. $hours = floor($seconds / 3600);
  71. $minutes = floor(($seconds / 60) % 60);
  72. $seconds = $seconds % 60;
  73. if ($init < 3600) {
  74. //less than 1 hour
  75. if ($init < 60) {
  76. //less than minute
  77. $result = $seconds . ' ' . __('sec.');
  78. } else {
  79. //more than one minute
  80. $result = $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  81. }
  82. } else {
  83. //more than hour
  84. $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  85. }
  86. return ($result);
  87. }
  88. /**
  89. * Returns number alias
  90. *
  91. * @global array $numAliases
  92. * @param string $number
  93. * @param bool $brief
  94. *
  95. * @return string
  96. */
  97. function zb_AskoziaGetNumAlias($number, $brief = false) {
  98. global $numAliases;
  99. if (!empty($numAliases)) {
  100. if (isset($numAliases[$number])) {
  101. if ($brief) {
  102. return($numAliases[$number]);
  103. } else {
  104. return($number . ' - ' . $numAliases[$number]);
  105. }
  106. } else {
  107. return ($number);
  108. }
  109. } else {
  110. return ($number);
  111. }
  112. }
  113. /**
  114. * Checks is callerid contains prefix
  115. *
  116. * @param string $prefix
  117. * @param string $callerid
  118. *
  119. * @return bool
  120. */
  121. function zb_AskoziaCheckPrefix($prefix, $callerid) {
  122. if (substr($callerid, 0, 1) == $prefix) {
  123. return (true);
  124. } else {
  125. return (false);
  126. }
  127. }
  128. /**
  129. * Renders parsed calls data
  130. *
  131. * @global array $altcfg
  132. * @param string $data
  133. *
  134. * @return void
  135. */
  136. function zb_AskoziaParseCallHistory($data) {
  137. /**
  138. * D A
  139. * Жизнь дерьмо
  140. * F# G
  141. * Возненавидь любя
  142. * D A
  143. * Всем смертям назло
  144. * F# G
  145. * Убей себя сам
  146. */
  147. global $altcfg;
  148. $debugFlag = false;
  149. $answeredFlag = true;
  150. $prevTimeStart = '';
  151. $prevTimeEnd = '';
  152. $controlGroups = array();
  153. $controlStats = array();
  154. $providerStats = array();
  155. if (isset($altcfg['ASKOZIA_DEBUG'])) {
  156. if ($altcfg['ASKOZIA_DEBUG']) {
  157. $debugFlag = true;
  158. }
  159. }
  160. //control groups option handling
  161. if (isset($altcfg['ASKOZIA_CONTROLGROUPS'])) {
  162. if (!empty($altcfg['ASKOZIA_CONTROLGROUPS'])) {
  163. $controlGroups = explode(',', $altcfg['ASKOZIA_CONTROLGROUPS']);
  164. $controlGroups = array_flip($controlGroups);
  165. }
  166. }
  167. //working time setup
  168. $rawWorkTime = $altcfg['WORKING_HOURS'];
  169. $rawWorkTime = explode('-', $rawWorkTime);
  170. $workStartTime = $rawWorkTime[0];
  171. $workEndTime = $rawWorkTime[1];
  172. $normalData = array();
  173. $callersData = array();
  174. $data = explodeRows($data);
  175. if (!empty($data)) {
  176. foreach ($data as $eachline) {
  177. $explode = explode(';', $eachline); //in 2.2.8 delimiter changed from ," to ;
  178. if (!empty($eachline)) {
  179. $normalData[] = str_replace('"', '', $explode);
  180. }
  181. }
  182. }
  183. //custom caller options
  184. if (isset($altcfg['ASKOZIA_CUSTOM'])) {
  185. if (!empty($altcfg['ASKOZIA_CUSTOM'])) {
  186. // 0 - internal peers
  187. // 1 - external gateways
  188. // 2 - group prefix
  189. // 3 - parking
  190. $customCfg = explode(',', $altcfg['ASKOZIA_CUSTOM']);
  191. } else {
  192. $customCfg = array();
  193. }
  194. } else {
  195. $customCfg = array();
  196. }
  197. if (!empty($normalData)) {
  198. $totalTime = 0;
  199. $callsCounter = 0;
  200. $answerCounter = 0;
  201. $noAnswerCounter = 0;
  202. $WorkHoursAnswerCounter = 0;
  203. $WorkHoursNoAnswerCounter = 0;
  204. $busycount = 0;
  205. $callFlows = array();
  206. $chartData = array();
  207. $cells = wf_TableCell('#');
  208. $cells .= wf_TableCell(__('Time'));
  209. $cells .= wf_TableCell(__('From'));
  210. $cells .= wf_TableCell(__('To'));
  211. $cells .= wf_TableCell(__('Picked up'));
  212. $cells .= wf_TableCell(__('Type'));
  213. $cells .= wf_TableCell(__('Status'));
  214. $cells .= wf_TableCell(__('Talk time'));
  215. $rows = wf_TableRow($cells, 'row1');
  216. foreach ($normalData as $io => $each) {
  217. //fix parsing for askozia 2.2.8
  218. if ($each[0] != 'accountcode') {
  219. //Askozia CFE fix
  220. if (sizeof($each) > 25) {
  221. array_splice($each, 3, 1);
  222. }
  223. /*
  224. * CFLOWS FIX
  225. * ************** */
  226. //some flow started for income call
  227. if (!ispos($each['16'], 'out')) {
  228. $incomeNumber = $each[1];
  229. if ($each[2] == 'CALLFLOW-START' OR ispos($each[8], 'CALLFLOW-START')) {
  230. $callFlows[$incomeNumber . '|' . $each[11]] = 'NO ANSWER';
  231. } else {
  232. if (isset($callFlows[$incomeNumber . '|' . $each[11]])) {
  233. $callFlows[$incomeNumber . '|' . $each[11]] = $each[14];
  234. } else {
  235. foreach ($callFlows as $cflowid => $cflowdata) {
  236. if (ispos($cflowid, $incomeNumber)) {
  237. $flowtime = explode('|', $cflowid);
  238. $flowtime = $flowtime[1];
  239. $flowtime = strtotime($flowtime);
  240. $callTimeTmp = strtotime($each[11]);
  241. if (($callTimeTmp - $flowtime) <= 10) {
  242. if (ispos($each[14], 'ANS')) {
  243. $callFlows[$cflowid] = $each[14];
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. $callsCounter++;
  252. $debugData = wf_tag('pre') . print_r($each, true) . wf_tag('pre', true);
  253. $startTime = explode(' ', $each[9]);
  254. @$startDate = $startTime[0];
  255. @$startTime = $startTime[1];
  256. @$startHour = date("H:00:00", strtotime($startTime));
  257. $endTime = explode(' ', $each[11]);
  258. @$endTime = $endTime[1];
  259. $answerTime = explode(' ', $each[10]);
  260. @$answerTime = $answerTime[1];
  261. $tmpStats = __('Taken up the phone') . ': ' . $answerTime . "\n";
  262. $tmpStats .= __('End of call') . ': ' . $endTime;
  263. $sessionTimeStats = wf_tag('abbr', false, '', 'title="' . $tmpStats . '"');
  264. $sessionTimeStats .= $startTime;
  265. $sessionTimeStats .= wf_tag('abbr', true);
  266. $callDirection = '';
  267. if (ispos($each[4], 'SIP-PROVIDER')) {
  268. $providerId = explode('-', $each[4]);
  269. $providerId = $providerId[0] . $providerId[1] . $providerId[2];
  270. } else {
  271. $providerId = '';
  272. }
  273. //setting call direction icon
  274. if (ispos($each['16'], 'out')) {
  275. $toNumber = $each[2];
  276. $callDirection = wf_img('skins/calls/outgoing.png') . ' ';
  277. $directionFlag = 'out';
  278. } else {
  279. $toNumber = $each[18];
  280. $callDirection = wf_img('skins/calls/incoming.png') . ' ';
  281. $directionFlag = 'in';
  282. }
  283. //showing debug info
  284. if ($debugFlag) {
  285. $callIdData = wf_modal($callsCounter, $callsCounter, $debugData, '', '500', '600');
  286. } else {
  287. $callIdData = $callsCounter;
  288. }
  289. $cells = wf_TableCell($callIdData, '', '', 'sorttable_customkey="' . $callsCounter . '"');
  290. $cells .= wf_TableCell($callDirection . $sessionTimeStats, '', '', 'sorttable_customkey="' . strtotime($each[9]) . '"');
  291. $cells .= wf_TableCell(zb_AskoziaGetNumAlias($each[1]));
  292. $cells .= wf_TableCell(zb_AskoziaGetNumAlias($toNumber));
  293. $receiveCid = '';
  294. if (!empty($each[6])) {
  295. $tmpRcid = explode('-', $each[6]);
  296. @$receiveCid = vf($tmpRcid[0], 3);
  297. }
  298. $cells .= wf_TableCell(zb_AskoziaGetNumAlias($receiveCid));
  299. $CallType = __('Dial');
  300. if (ispos($each[3], 'internal-caller-transfer')) {
  301. $CallType = __('Call transfer');
  302. }
  303. if (ispos($each[7], 'VoiceMail')) {
  304. $CallType = __('Voice mail');
  305. }
  306. $cells .= wf_TableCell($CallType);
  307. $callStatus = $each[14];
  308. $statusIcon = '';
  309. if (ispos($each[14], 'ANSWERED') AND ( !ispos($each[7], 'VoiceMail'))) {
  310. $answeredFlag = true;
  311. $callStatus = __('Answered');
  312. $statusIcon = wf_img('skins/calls/phone_green.png');
  313. $answerCounter++;
  314. //work time controls
  315. if (zb_isTimeBetween($workStartTime, $workEndTime, $startTime)) {
  316. $WorkHoursAnswerCounter++;
  317. //control groups answered calls handling
  318. if (isset($controlGroups[$toNumber])) {
  319. if (isset($controlStats[$toNumber])) {
  320. if (isset($controlStats[$toNumber]['answered'])) {
  321. $controlStats[$toNumber]['answered'] ++;
  322. } else {
  323. $controlStats[$toNumber]['answered'] = 1;
  324. }
  325. }
  326. }
  327. }
  328. if (isset($chartData[$startDate . ' ' . $startHour]['answered'])) {
  329. $chartData[$startDate . ' ' . $startHour]['answered'] ++;
  330. } else {
  331. $chartData[$startDate . ' ' . $startHour]['answered'] = 1;
  332. }
  333. //filling provider stats for answered calls
  334. if (!empty($providerId)) {
  335. if ($directionFlag == 'in') {
  336. if (isset($providerStats[$providerId])) {
  337. $providerStats[$providerId]['answered'] ++;
  338. $providerStats[$providerId]['time'] += $each[13];
  339. } else {
  340. $providerStats[$providerId]['answered'] = 1;
  341. $providerStats[$providerId]['unanswered'] = 0;
  342. $providerStats[$providerId]['time'] = $each[13];
  343. }
  344. }
  345. }
  346. }
  347. if ((ispos($each[14], 'NO ANSWER')) OR ( ispos($each[7], 'VoiceMail'))) {
  348. $answeredFlag = false;
  349. $callStatus = __('No answer');
  350. $statusIcon = wf_img('skins/calls/phone_red.png');
  351. //only incoming calls is unanswered
  352. if ($each[16] != 'outbound') {
  353. $noAnswerCounter++;
  354. if (zb_isTimeBetween($workStartTime, $workEndTime, $startTime)) {
  355. $WorkHoursNoAnswerCounter++;
  356. if (isset($controlGroups[$toNumber])) {
  357. //control groups no answered calls count in work time
  358. if (isset($controlStats[$toNumber]['noanswer'])) {
  359. $controlStats[$toNumber]['noanswer'] ++;
  360. } else {
  361. $controlStats[$toNumber]['noanswer'] = 1;
  362. }
  363. }
  364. }
  365. //filling provider stats for not answered calls
  366. if (!empty($providerId)) {
  367. if ($directionFlag == 'in') {
  368. if (isset($providerStats[$providerId])) {
  369. $providerStats[$providerId]['unanswered'] ++;
  370. } else {
  371. $providerStats[$providerId]['answered'] = 0;
  372. $providerStats[$providerId]['unanswered'] = 1;
  373. $providerStats[$providerId]['time'] = 0;
  374. }
  375. }
  376. }
  377. }
  378. if (isset($chartData[$startDate . ' ' . $startHour]['noanswer'])) {
  379. $chartData[$startDate . ' ' . $startHour]['noanswer'] ++;
  380. } else {
  381. $chartData[$startDate . ' ' . $startHour]['noanswer'] = 1;
  382. }
  383. }
  384. if (ispos($each[14], 'BUSY')) {
  385. $callStatus = __('Busy');
  386. $statusIcon = wf_img('skins/calls/phone_yellow.png');
  387. }
  388. if (ispos($each[14], 'FAILED')) {
  389. $callStatus = __('Failed');
  390. $statusIcon = wf_img('skins/calls/phone_fail.png');
  391. }
  392. $cells .= wf_TableCell($statusIcon . ' ' . $callStatus);
  393. $speekTimeRaw = $each[13];
  394. $totalTime = $totalTime + $each[13];
  395. $speekTime = zb_AskoziaFormatTime($speekTimeRaw);
  396. //current caller stats
  397. if (isset($callersData[$each[1]])) {
  398. $callersData[$each[1]]['calls'] = $callersData[$each[1]]['calls'] + 1;
  399. $callersData[$each[1]]['time'] = $callersData[$each[1]]['time'] + $speekTimeRaw;
  400. } else {
  401. $callersData[$each[1]]['calls'] = 1;
  402. $callersData[$each[1]]['time'] = $speekTimeRaw;
  403. }
  404. if (isset($callersData[$each[2]])) {
  405. $callersData[$each[2]]['calls'] = $callersData[$each[2]]['calls'] + 1;
  406. $callersData[$each[2]]['time'] = $callersData[$each[2]]['time'] + $speekTimeRaw;
  407. } else {
  408. $callersData[$each[2]]['calls'] = 1;
  409. $callersData[$each[2]]['time'] = $speekTimeRaw;
  410. }
  411. if (!empty($receiveCid)) {
  412. if (isset($callersData[$receiveCid])) {
  413. $callersData[$receiveCid]['calls'] = $callersData[$receiveCid]['calls'] + 1;
  414. $callersData[$receiveCid]['time'] = $callersData[$receiveCid]['time'] + $speekTimeRaw;
  415. } else {
  416. $callersData[$receiveCid]['calls'] = 1;
  417. $callersData[$receiveCid]['time'] = $speekTimeRaw;
  418. }
  419. }
  420. $cells .= wf_TableCell($speekTime, '', '', 'sorttable_customkey="' . $each[13] . '"');
  421. //default rowclass
  422. $rowClass = 'row3';
  423. //non answered calls coloring
  424. if ($answeredFlag == false) {
  425. $rowClass = 'ukvbankstadup';
  426. }
  427. //time range processing
  428. $curTimeStart = date("H:i:s", strtotime($each[9]));
  429. $curTimeEnd = date("H:i:s", strtotime($each[11]));
  430. if ((empty($prevTimeStart)) AND ( empty($prevTimeEnd))) {
  431. $prevTimeStart = $curTimeStart;
  432. $prevTimeEnd = $curTimeEnd;
  433. } else {
  434. if ($answeredFlag == false) {
  435. if (zb_isTimeBetween($prevTimeStart, $prevTimeEnd, $curTimeStart, true)) {
  436. $rowClass = 'undone';
  437. if (zb_isTimeBetween($workStartTime, $workEndTime, $startTime)) {
  438. $busycount++;
  439. }
  440. }
  441. }
  442. $prevTimeStart = $curTimeStart;
  443. if (strtotime($curTimeEnd) > strtotime($prevTimeEnd)) {
  444. $prevTimeEnd = $curTimeEnd;
  445. }
  446. }
  447. //control groups stats
  448. if (isset($controlGroups[$toNumber])) {
  449. if (isset($controlStats[$toNumber])) {
  450. if (isset($controlStats[$toNumber]['time'])) {
  451. $controlStats[$toNumber]['time'] += $speekTimeRaw;
  452. } else {
  453. $controlStats[$toNumber]['time'] = $speekTime;
  454. }
  455. }
  456. }
  457. $rows .= wf_TableRow($cells, $rowClass);
  458. }
  459. }
  460. if (!empty($controlStats)) {
  461. $ccells = wf_TableCell(__('Phone'));
  462. $ccells .= wf_TableCell(__('Total calls'));
  463. $ccells .= wf_TableCell(__('Time'));
  464. $ccells .= wf_TableCell(__('Answered'));
  465. $crows = wf_TableRow($ccells, 'row1');
  466. foreach ($controlStats as $io => $each) {
  467. $ccells = wf_TableCell(zb_AskoziaGetNumAlias($io));
  468. $ccells .= wf_TableCell(@$each['answered'] + $each['noanswer']);
  469. $ccells .= wf_TableCell(zb_AskoziaFormatTime($each['time']));
  470. $ccells .= @wf_TableCell($each['answered'] . ' (' . zb_PercentValue(($each['answered'] + $each['noanswer']), $each['answered']) . '%)');
  471. $crows .= wf_TableRow($ccells, 'row3');
  472. }
  473. }
  474. if (!empty($callersData)) {
  475. if (!empty($customCfg)) {
  476. $gcells = wf_TableCell(__('Phone'));
  477. $gcells .= wf_TableCell(__('Total calls'));
  478. $gcells .= wf_TableCell(__('Time'));
  479. $grows = wf_TableRow($gcells, 'row1');
  480. }
  481. foreach ($callersData as $cix => $eachcdat) {
  482. if (!empty($customCfg)) {
  483. if ((zb_AskoziaCheckPrefix($customCfg[0], $cix)) AND ( strlen($cix) < 4)) {
  484. $gcells = wf_TableCell(zb_AskoziaGetNumAlias($cix));
  485. $gcells .= wf_TableCell($eachcdat['calls']);
  486. $gcells .= wf_TableCell(zb_AskoziaFormatTime($eachcdat['time']), '', '', 'sorttable_customkey="' . $eachcdat['time'] . '"');
  487. $grows .= wf_TableRow($gcells, 'row3');
  488. }
  489. }
  490. }
  491. }
  492. //total time stats
  493. $result = '';
  494. if (!empty($chartData)) {
  495. if (sizeof($chartData) >= 2) {
  496. $gdata = __('Date') . ',' . __('Total') . ',' . __('Answered') . ',' . __('No answer') . "\n";
  497. foreach ($chartData as $io => $each) {
  498. @$gdata .= $io . ',' . ($each['answered'] + $each['noanswer']) . ',' . $each['answered'] . ',' . $each['noanswer'] . "\n";
  499. }
  500. $result .= wf_tag('div', false, '', '');
  501. $result .= wf_tag('h2') . __('Stats') . wf_tag('h2', true) . wf_tag('br');
  502. $result .= wf_Graph($gdata, '800', '200', false);
  503. $result .= wf_tag('div', true);
  504. $result .= wf_delimiter();
  505. }
  506. }
  507. if (!empty($callFlows)) {
  508. $noAnswerCounter = 0;
  509. $answerCounter = 0;
  510. $WorkHoursNoAnswerCounter = 0;
  511. $WorkHoursAnswerCounter = 0;
  512. $callsCounter = sizeof($callFlows);
  513. foreach ($callFlows as $cflowid => $cflowdata) {
  514. $flowTime = explode('|', $cflowid);
  515. $flowTime = explode(' ', $flowTime[1]);
  516. $flowTime = $flowTime[1];
  517. if ($cflowdata == 'NO ANSWER') {
  518. $noAnswerCounter++;
  519. if (zb_isTimeBetween($workStartTime, $workEndTime, $flowTime)) {
  520. $WorkHoursNoAnswerCounter++;
  521. }
  522. } else {
  523. $answerCounter++;
  524. if (zb_isTimeBetween($workStartTime, $workEndTime, $flowTime)) {
  525. $WorkHoursAnswerCounter++;
  526. }
  527. }
  528. }
  529. }
  530. $result .= __('Time spent on calls') . ': ' . zb_AskoziaFormatTime($totalTime) . wf_tag('br');
  531. $result .= __('Total') . ': ' . __('Answered') . ' / ' . __('No answer') . ': ' . $answerCounter . ' / ' . $noAnswerCounter . ' (' . zb_PercentValue($answerCounter + $noAnswerCounter, $answerCounter) . '%)' . wf_tag('br');
  532. $result .= wf_tag('b') . __('Working hours') . ': ' . __('Answered') . ' / ' . __('No answer') . ': ' . $WorkHoursAnswerCounter . ' / ' . $WorkHoursNoAnswerCounter . ' (' . zb_PercentValue($WorkHoursAnswerCounter + $WorkHoursNoAnswerCounter, $WorkHoursAnswerCounter) . '%)' . wf_tag('b', true) . wf_tag('br');
  533. $result .= __('Not working hours') . ': ' . __('Answered') . ' / ' . __('No answer') . ': ' . ($answerCounter - $WorkHoursAnswerCounter) . ' / ' . ($noAnswerCounter - $WorkHoursNoAnswerCounter) . ' (' . zb_PercentValue(($answerCounter - $WorkHoursAnswerCounter) + ($noAnswerCounter - $WorkHoursNoAnswerCounter), ($answerCounter - $WorkHoursAnswerCounter)) . '%)' . wf_tag('br');
  534. $result .= __('Missing calls because of overlap with the previous by time') . ' (' . __('Working hours') . '): ' . $busycount . wf_tag('br');
  535. $result .= __('Total calls') . ': ' . $callsCounter;
  536. //rendering provider stats
  537. if (!empty($providerStats)) {
  538. $cellsp = wf_TableCell(__('SIP trunk'));
  539. $cellsp .= wf_TableCell(__('Answered'));
  540. $cellsp .= wf_TableCell(__('No answer'));
  541. $cellsp .= wf_TableCell(__('Total calls'));
  542. $cellsp .= wf_TableCell(__('Talk time'));
  543. $rowsp = wf_TableRow($cellsp, 'row1');
  544. foreach ($providerStats as $ioz => $eachz) {
  545. $cellsp = wf_TableCell(zb_AskoziaGetNumAlias($ioz, true));
  546. $cellsp .= wf_TableCell($eachz['answered']);
  547. $cellsp .= wf_TableCell($eachz['unanswered']);
  548. $cellsp .= wf_TableCell($eachz['unanswered'] + $eachz['answered']);
  549. $cellsp .= wf_TableCell(zb_AskoziaFormatTime($eachz['time']));
  550. $rowsp .= wf_TableRow($cellsp, 'row3');
  551. }
  552. $result .= wf_delimiter();
  553. $result .= wf_TableBody($rowsp, '100%', 0, 'sortable');
  554. $result .= wf_delimiter();
  555. }
  556. if (!empty($controlStats)) {
  557. $result .= wf_tag('h3') . __('Contol groups stats') . wf_tag('h3', true);
  558. @$result .= wf_TableBody($crows, '100%', '0', 'sortable') . wf_delimiter();
  559. }
  560. if (!empty($customCfg)) {
  561. @$result .= wf_TableBody($grows, '100%', '0', 'sortable') . wf_delimiter();
  562. }
  563. $result .= wf_TableBody($rows, '100%', '0', 'sortable');
  564. show_window('', $result);
  565. }
  566. }
  567. /**
  568. * Fetches calls history from Askozia URL
  569. *
  570. * @global string $askoziaUrl
  571. * @global string $askoziaLogin
  572. * @global string $askoziaPassword
  573. * @global int $askoziaCacheTime
  574. * @param string $from
  575. * @param string $to
  576. *
  577. * @return void
  578. */
  579. function zb_AskoziaGetCallHistory($from, $to) {
  580. global $askoziaUrl, $askoziaLogin, $askoziaPassword, $askoziaCacheTime;
  581. $cachePath = 'exports/';
  582. $fields = array(
  583. 'extension_number' => 'all',
  584. 'cdr_filter' => 'incomingoutgoing',
  585. 'period_from' => $from,
  586. 'period_to' => $to,
  587. 'date_format' => 'Y-m-d',
  588. 'time_format' => 'H:i:s',
  589. 'page_format' => 'A4',
  590. 'SubmitCSVCDR' => 'Download CSV'
  591. );
  592. //caching
  593. $cacheUpdate = true;
  594. $cacheName = serialize($fields);
  595. $cacheName = md5($cacheName);
  596. $cacheName = $cachePath . $cacheName . '.askozia';
  597. $cachetime = time() - ($askoziaCacheTime * 60);
  598. if (file_exists($cacheName)) {
  599. if ((filemtime($cacheName) > $cachetime)) {
  600. $rawResult = file_get_contents($cacheName);
  601. $cacheUpdate = false;
  602. } else {
  603. $cacheUpdate = true;
  604. }
  605. } else {
  606. $cacheUpdate = true;
  607. }
  608. if ($cacheUpdate) {
  609. $ch = curl_init();
  610. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  611. curl_setopt($ch, CURLOPT_URL, $askoziaUrl . '/status_cdr.php');
  612. curl_setopt($ch, CURLOPT_USERPWD, $askoziaLogin . ":" . $askoziaPassword);
  613. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  614. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  615. $rawResult = curl_exec($ch);
  616. curl_close($ch);
  617. file_put_contents($cacheName, $rawResult);
  618. }
  619. if (!empty($rawResult)) {
  620. zb_AskoziaParseCallHistory($rawResult);
  621. } else {
  622. show_error(__('Empty reply received'));
  623. }
  624. }
  625. /**
  626. * Renders date selection form
  627. *
  628. * @return string
  629. */
  630. function web_AskoziaDateForm() {
  631. $inputs = wf_Link("?module=askozia&config=true", wf_img('skins/settings.png', __('Settings'))) . ' ';
  632. $inputs .= wf_DatePickerPreset('datefrom', curdate()) . ' ' . __('From');
  633. $inputs .= wf_DatePickerPreset('dateto', curdate()) . ' ' . __('To');
  634. $inputs .= wf_Submit(__('Show'));
  635. $result = wf_Form("", "POST", $inputs, 'glamour');
  636. return ($result);
  637. }
  638. /**
  639. * Renders askozia configuration form
  640. *
  641. * @global string $askoziaUrl
  642. * @global string $askoziaLogin
  643. * @global string $askoziaPassword
  644. * @global int $askoziaCacheTime
  645. *
  646. * @return string
  647. */
  648. function web_AskoziaConfigForm() {
  649. global $askoziaUrl, $askoziaLogin, $askoziaPassword, $askoziaCacheTime;
  650. $result = wf_BackLink('?module=askozia') . wf_delimiter();
  651. $inputs = wf_TextInput('newurl', __('AskoziaPBX URL'), $askoziaUrl, true);
  652. $inputs .= wf_TextInput('newlogin', __('Administrator login'), $askoziaLogin, true);
  653. $inputs .= wf_TextInput('newpassword', __('Password'), $askoziaPassword, true);
  654. $inputs .= wf_TextInput('newcachetime', __('Cache time'), $askoziaCacheTime, true);
  655. $inputs .= wf_Submit(__('Save'));
  656. $result .= wf_Form("", "POST", $inputs, 'glamour');
  657. return ($result);
  658. }
  659. /**
  660. * Renders askozia aliases assigning form
  661. *
  662. * @global array $numAliases
  663. *
  664. * @return string
  665. */
  666. function web_AskoziaAliasesForm() {
  667. global $numAliases;
  668. $createinputs = wf_TextInput('newaliasnum', __('Phone'), '', true);
  669. $createinputs .= wf_TextInput('newaliasname', __('Alias'), '', true);
  670. $createinputs .= wf_Submit(__('Create'));
  671. $createform = wf_Form('', 'POST', $createinputs, 'glamour');
  672. $result = $createform;
  673. if (!empty($numAliases)) {
  674. $delArr = array();
  675. foreach ($numAliases as $num => $eachname) {
  676. $delArr[$num] = $num . ' - ' . $eachname;
  677. }
  678. $delinputs = wf_Selector('deletealias', $delArr, __('Delete alias'), '', false);
  679. $delinputs .= wf_Submit(__('Delete'));
  680. $delform = wf_Form('', 'POST', $delinputs, 'glamour');
  681. $result .= $delform;
  682. }
  683. return ($result);
  684. }
  685. /**
  686. * Parses askozia system status data
  687. *
  688. * @param string $rawData
  689. *
  690. * @return string
  691. */
  692. function zb_AskoziaParseStatus($rawData) {
  693. $exploded = explodeRows($rawData);
  694. $data = array(
  695. 'phones' => 0,
  696. 'curcalls' => 0,
  697. 'totalcalls' => 0,
  698. 'ram' => 0,
  699. 'disk' => 0,
  700. 'uptime' => 0
  701. );
  702. if (!empty($exploded)) {
  703. foreach ($exploded as $each) {
  704. //detecting stats
  705. if (ispos($each, ';')) {
  706. $parse = explode(';', $each);
  707. //current calls
  708. $data['curcalls'] = $parse[1];
  709. //total calls
  710. $data['totalcalls'] = $parse[2];
  711. //registered phones
  712. $data['phones'] = $parse[4];
  713. //uptime in days or minutes
  714. $data['uptime'] = $parse[5];
  715. $data['uptime'] = str_replace('min', __('minutes'), $data['uptime']);
  716. $data['uptime'] = str_replace('hours', __('hours'), $data['uptime']);
  717. $data['uptime'] = str_replace('hour', __('hour'), $data['uptime']);
  718. $data['uptime'] = str_replace('days', __('days'), $data['uptime']);
  719. $data['uptime'] = str_replace('day', __('day'), $data['uptime']);
  720. //system memory
  721. $data['ram'] = $parse[6];
  722. //external storage
  723. $data['disk'] = $parse[7];
  724. }
  725. }
  726. }
  727. $cells = wf_TableCell(__('Phones'));
  728. $cells .= wf_TableCell(__('Current calls'));
  729. $cells .= wf_TableCell(__('Calls processed'));
  730. $cells .= wf_TableCell(__('Uptime'));
  731. $cells .= wf_TableCell(__('Memory usage'));
  732. $cells .= wf_TableCell(__('External storage'));
  733. $rows = wf_TableRow($cells, 'row2');
  734. $cells = wf_TableCell($data['phones']);
  735. $cells .= wf_TableCell($data['curcalls']);
  736. $cells .= wf_TableCell($data['totalcalls']);
  737. $cells .= wf_TableCell($data['uptime']);
  738. $cells .= wf_TableCell(web_bar($data['ram'], '100') . ' ' . $data['ram'] . '%');
  739. $cells .= wf_TableCell(web_bar($data['disk'], '100') . ' ' . $data['disk'] . '%');
  740. $rows .= wf_TableRow($cells, 'row3');
  741. $result = wf_TableBody($rows, '100%', 0, '');
  742. return ($result);
  743. }
  744. /**
  745. * Fetches askozia system status from URL
  746. *
  747. * @global string $askoziaUrl
  748. * @global string $askoziaLogin
  749. * @global string $askoziaPassword
  750. * @global int $askoziaCacheTime
  751. *
  752. * @return void
  753. */
  754. function zb_AskoziaGetCurrentStatus() {
  755. global $askoziaUrl, $askoziaLogin, $askoziaPassword, $askoziaCacheTime;
  756. //caching
  757. $cachePath = 'exports/';
  758. $cacheUpdate = true;
  759. $cacheName = 'currentStatus';
  760. $cacheName = $cachePath . $cacheName . '.askozia';
  761. $cachetime = time() - ($askoziaCacheTime * 60);
  762. if (file_exists($cacheName)) {
  763. if ((filemtime($cacheName) > $cachetime)) {
  764. $rawResult = file_get_contents($cacheName);
  765. $cacheUpdate = false;
  766. } else {
  767. $cacheUpdate = true;
  768. }
  769. } else {
  770. $cacheUpdate = true;
  771. }
  772. if ($cacheUpdate) {
  773. $ch = curl_init();
  774. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  775. curl_setopt($ch, CURLOPT_URL, $askoziaUrl . '/external_get_info.php?data=main');
  776. curl_setopt($ch, CURLOPT_USERPWD, $askoziaLogin . ":" . $askoziaPassword);
  777. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  778. $rawResult = curl_exec($ch);
  779. curl_close($ch);
  780. file_put_contents($cacheName, $rawResult);
  781. }
  782. if (!empty($rawResult)) {
  783. show_window(__('Current PBX status'), zb_AskoziaParseStatus($rawResult));
  784. } else {
  785. show_error(__('Empty reply received'));
  786. }
  787. }
  788. /**
  789. * Renders numlog stats if it exists
  790. *
  791. * @return void
  792. */
  793. function zb_AskoziaRenderNumLog() {
  794. global $ubillingConfig;
  795. $billCfg = $ubillingConfig->getBilling();
  796. $logPath = PBXNum::LOG_PATH;
  797. $catPath = $billCfg['CAT'];
  798. $grepPath = $billCfg['GREP'];
  799. $replyOffset = 5;
  800. $numberOffset = 2;
  801. $loginOffset = 7;
  802. $replyCount = 0;
  803. $replyStats = array();
  804. $replyNames = array(
  805. 0 => __('Not found'),
  806. 1 => __('Active'),
  807. 2 => __('Debt'),
  808. 3 => __('Frozen')
  809. );
  810. $result = '';
  811. if (file_exists($logPath)) {
  812. if (!wf_CheckPost(array('numyear', 'nummonth'))) {
  813. $curYear = curyear();
  814. $curMonth = date("m");
  815. } else {
  816. $curYear = vf($_POST['numyear'], 3);
  817. $curMonth = vf($_POST['nummonth'], 3);
  818. }
  819. $parseDate = $curYear . '-' . $curMonth;
  820. $dateInputs = wf_YearSelectorPreset('numyear', __('Year'), false, $curYear) . ' ';
  821. $dateInputs .= wf_MonthSelector('nummonth', __('Month'), $curMonth, false) . ' ';
  822. $dateInputs .= wf_Submit(__('Show'));
  823. $result .= wf_Form('', 'POST', $dateInputs, 'glamour');
  824. $rawLog = shell_exec($catPath . ' ' . $logPath . ' | ' . $grepPath . ' ' . $parseDate . '-');
  825. if (!empty($rawLog)) {
  826. $rawLog = explodeRows($rawLog);
  827. if (!empty($rawLog)) {
  828. foreach ($rawLog as $io => $each) {
  829. if (!empty($each)) {
  830. $line = explode(' ', $each);
  831. $callReply = $line[$replyOffset];
  832. if (isset($replyStats[$callReply])) {
  833. $replyStats[$callReply] ++;
  834. } else {
  835. $replyStats[$callReply] = 1;
  836. }
  837. $replyCount++;
  838. }
  839. }
  840. if (!empty($replyStats)) {
  841. $cells = wf_TableCell(__('Calls'));
  842. $cells .= wf_TableCell(__('Count'));
  843. $rows = wf_TableRow($cells, 'row1');
  844. foreach ($replyStats as $replyCode => $callsCount) {
  845. $cells = wf_TableCell($replyNames[$replyCode]);
  846. $cells .= wf_TableCell($callsCount);
  847. $rows .= wf_TableRow($cells, 'row3');
  848. }
  849. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  850. $result .= __('Total') . ': ' . $replyCount;
  851. }
  852. }
  853. }
  854. if (filesize($logPath) > 10) {
  855. show_window(__('Stats') . ' ' . __('on') . ' ' . $curYear . '-' . $curMonth, $result);
  856. }
  857. }
  858. }
  859. if (cfr('ASKOZIA')) {
  860. //loading askozia config
  861. $askoziaConf = zb_AskoziaGetConf();
  862. $numAliases = zb_AskoziaGetNumAliases();
  863. $askoziaUrl = $askoziaConf['url'];
  864. $askoziaLogin = $askoziaConf['login'];
  865. $askoziaPassword = $askoziaConf['password'];
  866. $askoziaCacheTime = $askoziaConf['cachetime'];
  867. //showing configuration form
  868. if (wf_CheckGet(array('config'))) {
  869. //changing settings
  870. if (wf_CheckPost(array('newurl', 'newlogin', 'newpassword'))) {
  871. zb_StorageSet('ASKOZIAPBX_URL', $_POST['newurl']);
  872. zb_StorageSet('ASKOZIAPBX_LOGIN', $_POST['newlogin']);
  873. zb_StorageSet('ASKOZIAPBX_PASSWORD', $_POST['newpassword']);
  874. zb_StorageSet('ASKOZIAPBX_CACHETIME', $_POST['newcachetime']);
  875. log_register("ASKOZIAPBX settings changed");
  876. rcms_redirect("?module=askozia&config=true");
  877. }
  878. //aliases creation
  879. if (wf_CheckPost(array('newaliasnum', 'newaliasname'))) {
  880. $newStoreAliases = $numAliases;
  881. $newAliasNum = mysql_real_escape_string($_POST['newaliasnum']);
  882. $newAliasName = mysql_real_escape_string($_POST['newaliasname']);
  883. $newStoreAliases[$newAliasNum] = $newAliasName;
  884. $newStoreAliases = serialize($newStoreAliases);
  885. $newStoreAliases = base64_encode($newStoreAliases);
  886. zb_StorageSet('ASKOZIAPBX_NUMALIAS', $newStoreAliases);
  887. log_register("ASKOZIAPBX ALIAS ADD `" . $newAliasNum . "` NAME `" . $newAliasName . "`");
  888. rcms_redirect("?module=askozia&config=true");
  889. }
  890. //alias deletion
  891. if (wf_CheckPost(array('deletealias'))) {
  892. $newStoreAliases = $numAliases;
  893. $deleteAliasNum = mysql_real_escape_string($_POST['deletealias']);
  894. if (isset($newStoreAliases[$deleteAliasNum])) {
  895. unset($newStoreAliases[$deleteAliasNum]);
  896. $newStoreAliases = serialize($newStoreAliases);
  897. $newStoreAliases = base64_encode($newStoreAliases);
  898. zb_StorageSet('ASKOZIAPBX_NUMALIAS', $newStoreAliases);
  899. log_register("ASKOZIAPBX ALIAS DELETE `" . $deleteAliasNum . "`");
  900. rcms_redirect("?module=askozia&config=true");
  901. }
  902. }
  903. show_window(__('Settings'), web_AskoziaConfigForm());
  904. show_window(__('Phone book'), web_AskoziaAliasesForm());
  905. } else {
  906. //showing call history form
  907. show_window(__('Calls history'), web_AskoziaDateForm());
  908. }
  909. if (wf_CheckPost(array('datefrom', 'dateto'))) {
  910. zb_AskoziaGetCallHistory($_POST['datefrom'], $_POST['dateto']);
  911. } else {
  912. if (!wf_CheckGet(array('config'))) {
  913. zb_AskoziaGetCurrentStatus();
  914. zb_AskoziaRenderNumLog();
  915. }
  916. }
  917. } else {
  918. show_error(__('Permission denied'));
  919. }
  920. } else {
  921. show_error(__('AskoziaPBX integration now disabled'));
  922. }