root/modules/branches/2.11/cdr/functions.inc.php

Revision 14260, 11.2 kB (checked in by p_lindheimer, 1 year ago)

adds CEL_ENABLED setting to remove CEL options if not being used re #5782

Line 
1 <?php
2 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
3 //This file is part of FreePBX.
4 //
5 //    FreePBX is free software: you can redistribute it and/or modify
6 //    it under the terms of the GNU General Public License as published by
7 //    the Free Software Foundation, either version 2 of the License, or
8 //    (at your option) any later version.
9 //
10 //    FreePBX is distributed in the hope that it will be useful,
11 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //    GNU General Public License for more details.
14 //
15 //    You should have received a copy of the GNU General Public License
16 //    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
17 //
18 //    cdr module for FreePBX 2.7+
19 //    Copyright (C) 2010, 2011 Anthony Joseph Messina
20 //    Portions Copyright (C) 2011 Igor Okunev
21 //    Portions Copyright (C) 2011 Mikael Carlsson
22 //    Portions Copyright (C) 2006 Seth Sargent, Steven Ward
23
24 // NOTE: This function should probably be in a FreePBX library
25 // php function empty() treats 0 as empty, that is why I need the function below
26 // to be able to search for any number starting with 0
27 function is_blank($value) {
28     return empty($value) && !is_numeric($value);
29 }
30
31 /* CDR Table Display Functions */
32 function cdr_formatCallDate($calldate) {
33     echo "<td>".$calldate."</td>";
34 }
35
36 function cdr_formatUniqueID($uniqueid) {
37     global $amp_conf;
38
39     $system = explode('-', $uniqueid, 2);
40     if ($amp_conf['CEL_ENABLED']) {
41         $href=$_SERVER['SCRIPT_NAME']."?display=cdr&action=cel_show&uid=" . urlencode($uniqueid);
42         echo '<td title="' . _("UniqueID") . ": " . $uniqueid . '">' .
43             '<a href="' . $href . '" >' . $system[0] . '</a></td>';
44     } else {
45         echo '<td title="' . _("UniqueID") . ": " . $uniqueid . '">' . $system[0] . '</td>';
46     }
47 }
48
49 function cdr_formatChannel($channel) {
50     $chan_type = explode('/', $channel, 2);
51     echo '<td title="' . _("Channel") . ": " . $channel . '">' . $chan_type[0] . "</td>";
52 }
53
54 function cdr_formatSrc($src, $clid) {
55     if (empty($src)) {
56         echo "<td class=\"record_col\">UNKNOWN</td>";
57     } else {
58         $clid = htmlspecialchars($clid);
59         echo '<td title="' . _("CallerID") . ": " . $clid . '">' . $src . "</td>";
60     }
61 }
62
63 function cdr_formatDID($did) {
64     $did = htmlspecialchars($did);
65     echo '<td title="' . _("DID") . ": " . $did . '">' . $did . "</td>";
66 }
67
68 function cdr_formatANI($ani) {
69     $ani = htmlspecialchars($ani);
70     echo '<td title="' . _("ANI") . ": " . $ani . '">' . $ani . "</td>";
71 }
72
73 function cdr_formatApp($app, $lastdata) {
74     $app = htmlspecialchars($app);
75     $lastdata = htmlspecialchars($lastdata);
76     echo '<td title="' _("Application") . ": " . $app . "(" . $lastdata . ")" . '">'
77     . $app . "</td>";
78 }
79
80 function cdr_formatDst($dst, $dcontext) {
81     echo '<td title="' . _("Destination Context") . ": " . $dcontext . '">'
82         . $dst . "</td>";
83 }
84
85 function cdr_formatDisposition($disposition, $amaflags) {
86     switch ($amaflags) {
87         case 0:
88             $amaflags = 'DOCUMENTATION';
89             break;
90         case 1:
91             $amaflags = 'IGNORE';
92             break;
93         case 2:
94             $amaflags = 'BILLING';
95             break;
96         case 3:
97         default:
98             $amaflags = 'DEFAULT';
99     }
100     echo '<td title="' . _("AMA Flag") . ": " . $amaflags . '">'
101         . $disposition . "</td>";
102 }
103
104 function cdr_formatDuration($duration, $billsec) {
105     $duration = sprintf('%02d', intval($duration/60)).':'.sprintf('%02d', intval($duration%60));
106     $billduration = sprintf('%02d', intval($billsec/60)).':'.sprintf('%02d', intval($billsec%60));
107     echo '<td title="' . _("Billing Duration") . ": " . $billduration . '">'
108         . $duration . "</td>";
109 }
110
111 function cdr_formatUserField($userfield) {
112     $userfield = htmlspecialchars($userfield);
113     echo "<td>".$userfield."</td>";
114 }
115
116 function cdr_formatAccountCode($accountcode) {
117     $accountcode = htmlspecialchars($accountcode);
118     echo "<td>".$accountcode."</td>";
119 }
120
121 function cdr_formatRecordingFile($recordingfile, $basename, $id) {
122
123     global $REC_CRYPT_PASSWORD;
124
125     if ($recordingfile) {
126         $crypt = new Crypt();
127         // Encrypt the complete file
128         $audio = urlencode($crypt->encrypt($recordingfile, $REC_CRYPT_PASSWORD));
129         $recurl=$_SERVER['SCRIPT_NAME']."?display=cdr&action=cdr_play&recordingpath=$audio";
130         $playbackRow = $id +1;
131         //
132         echo "<td title=\"$basename\"><a href=\"#\" onClick=\"javascript:cdr_play($playbackRow,'$recurl'); return false;\"><img src=\"assets/cdr/images/cdr_sound.png\" alt=\"Call recording\" /></a></td>";
133     } else {
134         echo "<td></td>";
135     }
136 }
137
138 function cdr_formatCNAM($cnam) {
139     $cnam = htmlspecialchars($cnam);
140     echo '<td title="' . _("Caller ID Name") . ": " . $cnam . '">' . $cnam . "</td>";
141 }
142
143 function cdr_formatCNUM($cnum) {
144     $cnum = htmlspecialchars($cnum);
145     echo '<td title="' . _("Caller ID Number") . ": " . $cnum . '">' . $cnum . "</td>";
146 }
147
148 function cdr_formatExten($exten) {
149     $exten = htmlspecialchars($exten);
150     echo '<td title="' . _("Dialplan exten") . ": " . $exten . '">' . $exten . "</td>";
151 }
152
153 function cdr_formatContext($context) {
154     $context = htmlspecialchars($context);
155     echo '<td title="' . _("Dialplan context") . ": " . $context . '">' . $context . "</td>";
156 }
157
158 function cdr_formatAMAFlags($amaflags) {
159     switch ($amaflags) {
160         case 0:
161             $amaflags = 'DOCUMENTATION';
162             break;
163         case 1:
164             $amaflags = 'IGNORE';
165             break;
166         case 2:
167             $amaflags = 'BILLING';
168             break;
169         case 3:
170         default:
171             $amaflags = 'DEFAULT';
172     }
173     echo '<td title="' . _("AMA Flag") . ": " . $amaflags . '">'
174         . $amaflags . "</td>";
175 }
176
177 // CEL Specific Formating:
178 //
179
180 function cdr_cel_formatEventType($eventtype) {
181     $eventtype = htmlspecialchars($eventtype);
182     echo "<td>".$eventtype."</td>";
183 }
184
185 function cdr_cel_formatUserDefType($userdeftype) {
186     $userdeftype = htmlspecialchars($userdeftype);
187     echo '<td title="' _("UserDefType") . ": " . $userdeftype . '">'
188     . $userdeftype . "</td>";
189 }
190
191 function cdr_cel_formatEventExtra($eventextra) {
192     $eventextra = htmlspecialchars($eventextra);
193     echo '<td title="' _("Event Extra") . ": " . $eventextra . '">'
194     . $eventextra . "</td>";
195 }
196
197 function cdr_cel_formatChannelName($channel) {
198     $chan_type = explode('/', $channel, 2);
199     $type = htmlspecialchars($chan_type[0]);
200     $channel = htmlspecialchars($channel);
201     echo '<td title="' . _("Channel") . ": " . $channel . '">' . $channel . "</td>";
202 }
203
204 /* Asterisk RegExp parser */
205 function cdr_asteriskregexp2sqllike( $source_data, $user_num ) {
206         $number = $user_num;
207         if ( strlen($number) < 1 ) {
208                 $number = $_POST[$source_data];
209         }
210         if ( '__' == substr($number,0,2) ) {
211                 $number = substr($number,1);
212         } elseif ( '_' == substr($number,0,1) ) {
213                 $number_chars = preg_split('//', substr($number,1), -1, PREG_SPLIT_NO_EMPTY);
214                 $number = '';
215                 foreach ($number_chars as $chr) {
216                         if ( $chr == 'X' ) {
217                                 $number .= '[0-9]';
218                         } elseif ( $chr == 'Z' ) {
219                                 $number .= '[1-9]';
220                         } elseif ( $chr == 'N' ) {
221                                 $number .= '[2-9]';
222                         } elseif ( $chr == '.' ) {
223                                 $number .= '.+';
224                         } elseif ( $chr == '!' ) {
225                                 $_POST[ $source_data .'_neg' ] = 'true';
226                         } else {
227                                 $number .= $chr;
228                         }
229                 }
230                 $_POST[ $source_data .'_mod' ] = 'asterisk-regexp';
231         }
232         return $number;
233 }
234
235 function cdr_get_cel($uid, $cel_table = 'asteriskcdrdb.cel') {
236     global $dbcdr;
237
238     // common query components
239     //
240     $sql_base = "SELECT * FROM $cel_table WHERE ";
241     $sql_order = " ORDER BY eventtime, id";
242
243
244     // get first set of CEL records
245     //
246     $sql_start = $sql_base . "uniqueid = '$uid' OR linkedid = '$uid'" . $sql_order;
247     $pass = $dbcdr->getAll($sql_start,DB_FETCHMODE_ASSOC);
248     if(DB::IsError($pass)) {
249         die_freepbx($pass->getDebugInfo() . "SQL - <br /> $sql_start" );
250     }
251
252     $last_criteria = array();
253     $next =array();
254     $done = false;
255
256     // continue querying all records based on the uniqueid and linkedid fields associated
257     // with the first set we queried until we have found all of them. This usually results
258     // in one or two more queries prior to the last one being identical indicating we have
259     // found all the records
260     //
261     while (!$done) {
262         unset($next);
263         foreach ($pass as $set) {
264             $next[] = $set['uniqueid'];
265             $next[] = $set['linkedid'];
266         }
267         $next = array_unique($next);
268         sort($next);
269
270         // if our criteria is now the same then we have found everything
271         //
272         if ($next == $last_criteria) {
273             $done = true;
274             continue;
275         }
276         unset($pass);
277
278         $set = "('" . implode($next,"','") . "')";
279         $sql_next = $sql_base . "uniqueid IN $set OR linkedid IN $set" . $sql_order;
280         $last_criteria = $next;
281         $next = array();
282         $pass = $dbcdr->getAll($sql_next,DB_FETCHMODE_ASSOC);
283         if(DB::IsError($pass)) {
284             die_freepbx($pass->getDebugInfo() . "SQL - <br /> $sql_next" );
285         }
286     }
287     return $pass;
288 }
289
290 function cdr_download($data, $name) {
291     $filesize = strlen($data);
292     $mimetype = "application/octet-stream";
293     
294     // Make sure there's not anything else left
295     cdr_ob_clean_all();
296     // Start sending headers
297     header("Pragma: public"); // required
298     header("Expires: 0");
299     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
300     header("Cache-Control: private",false); // required for certain browsers
301     header("Content-Transfer-Encoding: binary");
302     header("Content-Type: " . $mimetype);
303     header("Content-Length: " . $filesize);
304     header("Content-Disposition: attachment; filename=\"" . $name . "\";" );
305     // Send data
306     echo $data;
307     die();
308 }
309
310 function cdr_export_csv($csvdata) {
311     // Searching for more than 10,000 records take more than 30 seconds.
312     // php default timeout is 30 seconds, hard code it to 3000 seconds for now (which is WAY overkill).
313     // TODO: make this value a setting in Advanced Settings
314     set_time_limit(3000);
315     $fname        = "cdr__" .  (string) time() . $_SERVER["SERVER_NAME"] . ".csv";
316     $csv_header ="calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield\n";
317     $data         = $csv_header;
318     
319     foreach ($csvdata as $csv) {
320         $csv_line[0]     = $csv['calldate'];
321         $csv_line[1]     = $csv['clid'];
322         $csv_line[2]     = $csv['src'];
323         $csv_line[3]     = $csv['dst'];
324         $csv_line[4]     = $csv['dcontext'];
325         $csv_line[5]    = $csv['channel'];
326         $csv_line[6]     = $csv['dstchannel'];
327         $csv_line[7]     = $csv['lastapp'];
328         $csv_line[8]    = $csv['lastdata'];
329         $csv_line[9]    = $csv['duration'];
330         $csv_line[10]    = $csv['billsec'];
331         $csv_line[11]    = $csv['disposition'];
332         $csv_line[12]    = $csv['amaflags'];
333         $csv_line[13]    = $csv['accountcode'];
334         $csv_line[14]    = $csv['uniqueid'];
335         $csv_line[15]    = $csv['userfield'];
336
337         for ($i = 0; $i < count($csv_line); $i++) {
338             /* If the string contains a comma, enclose it in double-quotes. */
339             if (strpos($csv_line[$i], ",") !== FALSE) {
340                 $csv_line[$i] = "\"" . $csv_line[$i] . "\"";
341             }
342             if ($i != count($csv_line) - 1) {
343                 $data .= $csv_line[$i] . ",";
344             } else {
345                 $data .= $csv_line[$i];
346             }
347         }
348         $data .= "\n";
349         unset($csv_line);
350     }
351     cdr_download($data, $fname);
352     return;
353 }
354
355 function cdr_ob_clean_all () {
356     $ob_active = ob_get_length () !== false;
357     while($ob_active) {
358         ob_end_clean();
359         $ob_active = ob_get_length () !== false;
360     }
361     return true;
362 }
363
364 ?>
365
Note: See TracBrowser for help on using the browser.