forked from FreePBX/cdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.inc.php
214 lines (186 loc) · 6.5 KB
/
functions.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Portions Copyright (C) 2011 Igor Okunev
// Portions Copyright (C) 2011 Mikael Carlsson
// Copyright 2013 Schmooze Com Inc.
//
class cdr_conf {
private static $obj;
var $_cel_general = array();
// FreePBX magic ::create() call
public static function create() {
if (!isset(self::$obj))
self::$obj = new cdr_conf();
return self::$obj;
}
function __construct() {
self::$obj = $this;
}
// return an array of filenames to write
function get_filename() {
global $chan_dahdi;
$files = array(
'cel_general_additional.conf',
);
return $files;
}
function generateConf($file) {
global $version;
global $amp_conf;
switch ($file) {
case 'cel_general_additional.conf':
return $this->generate_cel_general_additional($version);
break;
}
}
function generate_cel_general_additional($ast_version) {
$output = '';
if (!empty($this->_cel_general)) {
foreach ($this->_cel_general as $values) {
$output .= $values['key']."=".$values['value']."\n";
}
}
return $output;
}
function addCelGeneral($key, $value) {
$this->_cel_general[] = array('key' => $key, 'value' => $value);
}
}
function cdr_get_config($engine) {
global $core_conf, $cdr_conf, $amp_conf;
switch($engine) {
case "asterisk":
if (isset($cdr_conf) && is_a($cdr_conf, "cdr_conf")) {
$cdr_conf->addCelGeneral('enable', 'yes');
$cdr_conf->addCelGeneral('apps', 'all');
$cdr_conf->addCelGeneral('events', 'all');
$cdr_conf->addCelGeneral('dateformat', '%F %T');
}
/*
*/
if (isset($core_conf) && is_a($core_conf, "core_conf")) {
$section = 'asteriskcdrdb';
$core_conf->addResOdbc($section, array('enabled' => 'yes'));
$core_conf->addResOdbc($section, array('dsn' => 'MySQL-asteriskcdrdb'));
$core_conf->addResOdbc($section, array('pooling' => 'no'));
$core_conf->addResOdbc($section, array('limit' => '1'));
$core_conf->addResOdbc($section, array('pre-connect' => 'yes'));
$core_conf->addResOdbc($section, array('username' => $amp_conf['AMPDBUSER']));
$core_conf->addResOdbc($section, array('password' => $amp_conf['AMPDBPASS']));
}
break;
}
}
// NOTE: This function should probably be in a FreePBX library
// php function empty() treats 0 as empty, that is why I need the function below
// to be able to search for any number starting with 0
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
/* Asterisk RegExp parser */
function cdr_asteriskregexp2sqllike( $source_data, $user_num ) {
$number = $user_num;
if ( strlen($number) < 1 ) {
$number = $_POST[$source_data];
}
if ( '__' == substr($number,0,2) ) {
$number = substr($number,1);
} elseif ( '_' == substr($number,0,1) ) {
$number_chars = preg_split('//', substr($number,1), -1, PREG_SPLIT_NO_EMPTY);
$number = '';
foreach ($number_chars as $chr) {
if ( $chr == 'X' ) {
$number .= '[0-9]';
} elseif ( $chr == 'Z' ) {
$number .= '[1-9]';
} elseif ( $chr == 'N' ) {
$number .= '[2-9]';
} elseif ( $chr == '.' ) {
$number .= '.+';
} elseif ( $chr == '!' ) {
$_POST[ $source_data .'_neg' ] = 'true';
} else {
$number .= $chr;
}
}
$_POST[ $source_data .'_mod' ] = 'asterisk-regexp';
}
return $number;
}
function cdr_get_cel($uid, $cel_table = 'asteriskcdrdb.cel') {
global $dbcdr;
// common query components
//
$sql_base = "SELECT * FROM $cel_table WHERE ";
$sql_order = " ORDER BY eventtime, id";
// get first set of CEL records
//
$sql_start = $sql_base . "uniqueid = '$uid' OR linkedid = '$uid'" . $sql_order;
$pass = $dbcdr->getAll($sql_start,DB_FETCHMODE_ASSOC);
if(DB::IsError($pass)) {
die_freepbx($pass->getDebugInfo() . "SQL - <br /> $sql_start" );
}
$last_criteria = array();
$next =array();
$done = false;
// continue querying all records based on the uniqueid and linkedid fields associated
// with the first set we queried until we have found all of them. This usually results
// in one or two more queries prior to the last one being identical indicating we have
// found all the records
//
while (!$done) {
unset($next);
foreach ($pass as $set) {
$next[] = $set['uniqueid'];
$next[] = $set['linkedid'];
}
$next = array_unique($next);
sort($next);
// if our criteria is now the same then we have found everything
//
if ($next == $last_criteria) {
$done = true;
continue;
}
unset($pass);
$set = "('" . implode($next,"','") . "')";
$sql_next = $sql_base . "uniqueid IN $set OR linkedid IN $set" . $sql_order;
$last_criteria = $next;
$next = array();
$pass = $dbcdr->getAll($sql_next,DB_FETCHMODE_ASSOC);
if(DB::IsError($pass)) {
die_freepbx($pass->getDebugInfo() . "SQL - <br /> $sql_next" );
}
}
return $pass;
}
function cdr_export_csv($csvdata) {
// Searching for more than 10,000 records take more than 30 seconds.
// php default timeout is 30 seconds, hard code it to 3000 seconds for now (which is WAY overkill).
// TODO: make this value a setting in Advanced Settings
set_time_limit(3000);
$fname = "cdr__" . (string) time() . $_SERVER["SERVER_NAME"] . ".csv";
$csv_header ="calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield";
$mimetype = "application/octet-stream";
// Start sending headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Content-Type: " . $mimetype);
header("Content-Disposition: attachment; filename=\"" . $fname . "\";" );
// Send data
$out = fopen('php://output', 'w');
fputcsv($out, explode(",",$csv_header));
foreach ($csvdata as $csv) {
$csv_line = array();
foreach(explode(",",$csv_header) as $k => $item) {
$csv_line[$k] = $csv[$item];
}
fputcsv($out, $csv_line);
}
fclose($out);
die();
}