forked from FreePBX/cdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCdr.class.php
More file actions
265 lines (249 loc) · 9.54 KB
/
Cdr.class.php
File metadata and controls
265 lines (249 loc) · 9.54 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
// vim: set ai ts=4 sw=4 ft=php:
class Cdr implements BMO {
//supported playback formats
public $supportedFormats = array(
"oga" => "ogg",
"wav" => "wav"
);
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new Exception("Not given a FreePBX Object");
}
$amp_conf = FreePBX::$conf;
$this->FreePBX = $freepbx;
$this->db = $freepbx->Database;
$config = $this->FreePBX->Config;
$db_name = $config->get('CDRDBNAME');
$db_host = $config->get('CDRDBHOST');
$db_port = $config->get('CDRDBPORT');
$db_user = $config->get('CDRDBUSER');
$db_pass = $config->get('CDRDBPASS');
$dbt = $config->get('CDRDBTYPE');
$db_hash = array('mysql' => 'mysql', 'postgres' => 'pgsql');
$dbt = !empty($dbt) ? $dbt : 'mysql';
$db_type = $db_hash[$dbt];
$db_name = !empty($db_name)?$db_name:"asteriskcdrdb";
$db_host = !empty($db_host)?$db_host:"localhost";
$db_port = empty($db_port) ? '' : ':' . $db_port;
$db_user = empty($db_user) ? $amp_conf['AMPDBUSER'] : $db_user;
$db_pass = empty($db_pass) ? $amp_conf['AMPDBPASS'] : $db_pass;
try {
$this->cdrdb = new Database($db_type.':host='.$db_host.$db_port.';dbname='.$db_name,$db_user,$db_pass);
} catch(\Exception $e) {
die('Unable to connect to CDR Database using string:'.$db_type.':host='.$db_host.$db_port.';dbname='.$db_name.','.$db_user.','.$db_pass);
}
}
public function doConfigPageInit($page) {
}
public function install() {
}
public function uninstall() {
}
public function backup(){
}
public function restore($backup){
}
public function genConfig() {
}
public function getRecordByIDExtension($rid,$ext, $generateMedia = false) {
$sql = "SELECT * FROM cdr WHERE uniqueid = :uid AND (src = :ext OR dst = :ext OR src = :vmext OR dst = :vmext OR cnum = :ext OR cnum = :vmext OR dstchannel LIKE :dstchannel)";
$sth = $this->cdrdb->prepare($sql);
try {
$sth->execute(array("uid" => str_replace("_",".",$rid), "ext" => $ext, "vmext" => "vmu".$ext, ':dstchannel' => '%/'.$ext.'-%'));
$recording = $sth->fetch(PDO::FETCH_ASSOC);
} catch(\Exception $e) {
return false;
}
if(!empty($recording['recordingfile'])) {
$spool = $this->FreePBX->Config->get('ASTSPOOLDIR');
$mixmondir = $this->FreePBX->Config->get('MIXMON_DIR');
$rec_parts = explode('-',$recording['recordingfile']);
$fyear = substr($rec_parts[3],0,4);
$fmonth = substr($rec_parts[3],4,2);
$fday = substr($rec_parts[3],6,2);
$monitor_base = $mixmondir ? $mixmondir : $spool . '/monitor';
$file = "$monitor_base/$fyear/$fmonth/$fday/" . $recording['recordingfile'];
if($this->queryAudio($file)) {
if($generateMedia) {
$this->generateAdditionalMediaFormats($file, false);
}
$sha = sha1_file($file);
$filename = pathinfo($file,PATHINFO_FILENAME);
$basename = dirname($file);
foreach($this->supportedFormats as $format => $extension) {
$mf = $basename."/".$filename."_".$sha.".".$extension;
if($this->queryAudio($mf)) {
$recording['recordings']['format'][$format] = array(
"filename" => basename($mf),
"path" => dirname($mf),
"length" => filesize($mf)
);
}
}
$recording['recordings']['format'][$format] = array(
'path' => dirname($file),
'filename' => basename($file),
'length' => filesize($file)
);
}
}
return $recording;
}
public function readRecordingBinaryByRecordingIDExtension($msgid,$ext,$format,$start=0,$buffer=8192) {
$record = $this->getRecordByIDExtension($msgid,$ext);
$fpath = $record['recordings']['format'][$format]['path']."/".$record['recordings']['format'][$format]['filename'];
if(!empty($record) && !empty($record['recordings']['format'][$format]) && $this->queryAudio($fpath)) {
$end = $record['recordings']['format'][$format]['length'] - 1;
$fp = fopen($fpath, "rb");
fseek($fp, $start);
if(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
$contents = fread($fp, $buffer);
fclose($fp);
return $contents;
}
fclose($fp);
}
return false;
}
public function getCalls($extension,$page=1,$orderby='date',$order='desc',$search='',$limit=100) {
$start = ($limit * ($page - 1));
$end = $limit;
switch($orderby) {
case 'description':
$orderby = 'clid';
break;
case 'duration':
$orderby = 'duration';
break;
case 'date':
default:
$orderby = 'timestamp';
break;
}
$order = ($order == 'desc') ? 'desc' : 'asc';
if(!empty($search)) {
$sql = "SELECT *, UNIX_TIMESTAMP(calldate) As timestamp FROM cdr WHERE (dstchannel LIKE :dstchannel OR src = :extension OR dst = :extension OR src = :extensionv OR dst = :extensionv OR cnum = :extension OR cnum = :extensionv) AND (clid LIKE :search OR src LIKE :search OR dst LIKE :search) ORDER by $orderby $order LIMIT $start,$end";
$sth = $this->cdrdb->prepare($sql);
$sth->execute(array(':dstchannel' => '%/'.$extension.'-%', ':extension' => $extension, ':search' => '%'.$search.'%', ':extensionv' => 'vmu'.$extension));
} else {
$sql = "SELECT *, UNIX_TIMESTAMP(calldate) As timestamp FROM cdr WHERE (dstchannel LIKE :dstchannel OR src = :extension OR dst = :extension OR src = :extensionv OR dst = :extensionv OR cnum = :extension OR cnum = :extensionv) ORDER by $orderby $order LIMIT $start,$end";
$sth = $this->cdrdb->prepare($sql);
$sth->execute(array(':dstchannel' => '%/'.$extension.'-%', ':extension' => $extension, ':extensionv' => 'vmu'.$extension));
}
$calls = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($calls as &$call) {
if(empty($call['dst']) && preg_match('/\/(.*)\-/',$call['dstchannel'],$matches)) {
$call['dst'] = $matches[1];
}
if($call['duration'] > 59) {
$min = floor($call['duration'] / 60);
if($min > 59) {
$call['niceDuration'] = sprintf(_('%s hour, %s min, %s sec'),gmdate("H", $call['duration']), gmdate("i", $call['duration']), gmdate("s", $call['duration']));
} else {
$call['niceDuration'] = sprintf(_('%s min, %s sec'),gmdate("i", $call['duration']), gmdate("s", $call['duration']));
}
} else {
$call['niceDuration'] = sprintf(_('%s sec'),$call['duration']);
}
$call['niceUniqueid'] = str_replace(".","_",$call['uniqueid']);
$call['recordingformat'] = !empty($call['recordingfile']) ? strtolower(pathinfo($call['recordingfile'],PATHINFO_EXTENSION)) : '';
if(!empty($call['recordingfile'])) {
$spool = $this->FreePBX->Config->get('ASTSPOOLDIR');
$mixmondir = $this->FreePBX->Config->get('MIXMON_DIR');
$rec_parts = explode('-',$call['recordingfile']);
$fyear = substr($rec_parts[3],0,4);
$fmonth = substr($rec_parts[3],4,2);
$fday = substr($rec_parts[3],6,2);
$monitor_base = $mixmondir ? $mixmondir : $spool . '/monitor';
$file = "$monitor_base/$fyear/$fmonth/$fday/" . $call['recordingfile'];
if($this->queryAudio($file)) {
$this->generateAdditionalMediaFormats($file);
$sha = sha1_file($file);
$filename = pathinfo($file,PATHINFO_FILENAME);
$basename = dirname($file);
foreach($this->supportedFormats as $format => $extension) {
$mf = $basename."/".$filename."_".$sha.".".$extension;
if($this->queryAudio($mf)) {
$call['format'][$format] = array(
"filename" => basename($mf),
"path" => dirname($mf),
"length" => filesize($mf)
);
}
}
} else {
$call['format'] = array();
$call['recordingfile'] = "";
$call['recordingformat'] = "";
}
}
}
return $calls;
}
private function generateAdditionalMediaFormats($file,$background = true) {
$b = ($background) ? '&' : ''; //this is so very important
$path = dirname($file);
$filename = pathinfo($file,PATHINFO_FILENAME);
if(!$this->queryAudio($file)) {
return false;
}
$sha1 = sha1_file($file);
foreach($this->supportedFormats as $format) {
switch($format) {
case "ogg":
if(!file_exists($path . "/" . $filename . "_".$sha1.".ogg")) {
exec("sox $file " . $path . "/" . $filename . "_".$sha1.".ogg > /dev/null 2>&1 ".$b);
}
break;
}
}
return true;
}
/**
* Query the audio file and make sure it's actually audio
* @param string $file The full file path to check
*/
public function queryAudio($file) {
if(!file_exists($file) || !is_readable($file)) {
return false;
}
$last = exec('sox '.$file.' -n stat 2>&1',$output,$ret);
if(preg_match('/not sound/',$last)) {
return false;
}
$data = array();
foreach($output as $o) {
$parts = explode(":",$o);
$key = preg_replace("/\W/","",$parts[0]);
$data[$key] = trim($parts[1]);
}
return $data;
}
/**
* Get the Number of Pages by limit for extension
* @param {int} $extension The Extension to lookup
* @param {int} $limit=100 The limit of results per page
*/
public function getPages($extension,$search='',$limit=100) {
if(!empty($search)) {
$sql = "SELECT count(*) as count FROM cdr WHERE (src = :extension OR dst = :extension OR src = :extensionv OR dst = :extensionv OR cnum = :extension) AND (clid LIKE :search OR src LIKE :search OR dst LIKE :search)";
$sth = $this->cdrdb->prepare($sql);
$sth->execute(array(':extension' => $extension, ':search' => '%'.$search.'%',':extensionv' => 'vmu'.$extension));
} else {
$sql = "SELECT count(*) as count FROM cdr WHERE (src = :extension OR dst = :extension OR src = :extensionv OR dst = :extensionv OR cnum = :extension)";
$sth = $this->cdrdb->prepare($sql);
$sth->execute(array(':extension' => $extension,':extensionv' => 'vmu'.$extension));
}
$res = $sth->fetch(PDO::FETCH_ASSOC);
$total = $res['count'];
if(!empty($total)) {
return ceil($total/$limit);
} else {
return false;
}
}
}