root/modules/branches/2.9/recordings/functions.inc.php

Revision 10609, 13.9 kB (checked in by p_lindheimer, 3 years ago)

have destinations provided for these featurecodes re #4653 (and making paging codes enabled by default)

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
  • Property svn:eol-type set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 // Source and Destination Dirctories for recording
4 global $recordings_astsnd_path; // PHP5 needs extra convincing of a global
5 global $amp_conf;
6 $recordings_save_path = $amp_conf['ASTSPOOLDIR']."/tmp/";
7 $recordings_astsnd_path = isset($asterisk_conf['astvarlibdir'])?$asterisk_conf['astvarlibdir']:'/var/lib/asterisk';
8 $recordings_astsnd_path .= "/sounds/";
9
10 function recordings_get_config($engine) {
11   global $ext;  // is this the best way to pass this?
12   global $recordings_save_path;
13   global $version;
14
15   $ast_ge_14 = version_compare($version, '1.4', 'ge');
16  
17   $modulename = "recordings";
18   $appcontext = "app-recordings";
19  
20   switch($engine) {
21     case "asterisk":
22       // FeatureCodes for save / check
23       $fcc = new featurecode($modulename, 'record_save');
24       $fc_save = $fcc->getCodeActive();
25       unset($fcc);
26
27       $fcc = new featurecode($modulename, 'record_check');
28       $fc_check = $fcc->getCodeActive();
29       unset($fcc);
30
31       if ($fc_save != '' || $fc_check != '') {
32         $ext->addInclude('from-internal-additional', 'app-recordings'); // Add the include from from-internal
33         
34         if ($fc_save != '') {
35           $ext->add($appcontext, $fc_save, '', new ext_macro('user-callerid'));
36           $ext->add($appcontext, $fc_save, '', new ext_wait('2'));
37           $ext->add($appcontext, $fc_save, '', new ext_macro('systemrecording', 'dorecord'));
38         }
39
40         if ($fc_check != '') {
41           $ext->add($appcontext, $fc_check, '', new ext_macro('user-callerid'));
42           $ext->add($appcontext, $fc_check, '', new ext_wait('2'));
43           $ext->add($appcontext, $fc_check, '', new ext_macro('systemrecording', 'docheck'));
44         }
45       }
46
47       // Now generate the Feature Codes to edit recordings
48       //
49       $recordings = recordings_list();
50       foreach ($recordings as $item) {
51
52         // Get the feature code, and do a sanity check if it is not suppose to be active and delete it
53         //
54         if ($item['fcode'] != 0) {
55           $fcc = new featurecode($modulename, 'edit-recording-'.$item['id']);
56           $fcode = $fcc->getCodeActive();
57           unset($fcc);
58         } else {
59           $fcc = new featurecode('recordings', 'edit-recording-'.$item['id']);
60           $fcc->delete();
61           unset($fcc); 
62           continue; // loop back to foreach
63         }
64
65         if ($fcode != '') {
66           // Do a sanity check, there should be no compound files
67           //
68           if (strpos($item['filename'], '&') === false && trim($item['filename']) != '') {
69             $fcode_pass = (trim($item['fcode_pass']) != '') ? ','.$item['fcode_pass'] : '';
70             $ext->add($appcontext, $fcode, '', new ext_macro('user-callerid'));
71             $ext->add($appcontext, $fcode, '', new ext_wait('2'));
72             $ext->add($appcontext, $fcode, '', new ext_macro('systemrecording', 'docheck,'.$item['filename'].$fcode_pass));
73             //$ext->add($appcontext, $fcode, '', new ext_macro('hangup'));
74           }
75         }
76       }
77
78       // moved from modules/core to modules/recordings
79       // since it really belongs here and not there
80       // also provides direct access to $recordings_save_path
81       // which removes a hard-coded value in the macro
82
83       $context = 'macro-systemrecording';
84       
85       $ext->add($context, 's', '', new ext_setvar('RECFILE','${IF($["${ARG2}" = ""]?'.$recordings_save_path.'${AMPUSER}-ivrrecording:${ARG2})}'));
86       $ext->add($context, 's', '', new ext_execif('$["${ARG3}" != ""]','Authenticate','${ARG3}'));
87       $ext->add($context, 's', '', new ext_goto(1, '${ARG1}'));
88       
89       $exten = 'dorecord';
90       
91       // Delete all versions of the current sound file (does not consider languages though
92       // otherwise you might have some versions that are not re-recorded
93       //
94       $ext->add($context, $exten, '', new ext_system('rm ${ASTVARLIBDIR}/sounds/${RECFILE}.*'));
95       $ext->add($context, $exten, '', new ext_record('${RECFILE}:wav'));
96       $ext->add($context, $exten, '', new ext_wait(1));
97       $ext->add($context, $exten, '', new ext_goto(1, 'confmenu'));
98
99       $exten = 'docheck';
100       
101       $ext->add($context, $exten, '', new ext_playback('beep'));
102       if ($ast_ge_14) {
103         $ext->add($context, $exten, 'dc_start', new ext_background('${RECFILE},m,${CHANNEL(language)},macro-systemrecording'));
104       } else {
105         $ext->add($context, $exten, 'dc_start', new ext_background('${RECFILE},m,${LANGUAGE},macro-systemrecording'));
106       }
107       $ext->add($context, $exten, '', new ext_wait(1));
108       $ext->add($context, $exten, '', new ext_goto(1, 'confmenu'));
109
110       $exten = 'confmenu';
111       if ($ast_ge_14) {
112         $ext->add($context, $exten, '', new ext_background('to-listen-to-it&press-1&to-rerecord-it&press-star&astcc-followed-by-pound,m,${CHANNEL(language)},macro-systemrecording'));
113       } else {
114         $ext->add($context, $exten, '', new ext_background('to-listen-to-it&press-1&to-rerecord-it&press-star&astcc-followed-by-pound,m,${LANGUAGE},macro-systemrecording'));
115       }
116       $ext->add($context, $exten, '', new ext_read('RECRESULT', '', 1, '', '', 4));
117       $ext->add($context, $exten, '', new ext_gotoif('$["x${RECRESULT}"="x*"]', 'dorecord,1'));
118       $ext->add($context, $exten, '', new ext_gotoif('$["x${RECRESULT}"="x1"]', 'docheck,2'));
119       $ext->add($context, $exten, '', new ext_goto(1));
120       
121       $ext->add($context, '1', '', new ext_goto('dc_start', 'docheck'));
122       $ext->add($context, '*', '', new ext_goto(1, 'dorecord'));
123       
124       $ext->add($context, 't', '', new ext_playback('goodbye'));
125       $ext->add($context, 't', '', new ext_hangup());
126       
127       $ext->add($context, 'i', '', new ext_playback('pm-invalid-option'));
128       $ext->add($context, 'i', '', new ext_goto(1, 'confmenu'));
129
130       $ext->add($context, 'h', '', new ext_hangup());
131
132     break;
133   }
134 }
135
136 function recordings_get_or_create_id($fn, $module) {
137   $id = recordings_get_id($fn);
138   if ($id != null) {
139     return $id;
140   } else {
141     // Create the id, name it the file name or if multi-part ...
142     //
143     $dname = explode('&',$displayname);
144     $displayname = 'auto-created: ';
145     $displayname .= count($dname) == 1 ? $fn : $dname[0]."&...";
146     $description = sprintf(_("Missing Sound file auto-created from migration of %s module"),$module);
147     recordings_add($displayname, $fn, $description='');
148
149     // get the id we just created
150     //
151     $id = recordings_get_id($fn);
152
153     // Notify of issue
154     //
155     $nt =& notifications::create($db);
156     $text = sprintf(_("Non-Existent Recording in module %s"),$module);
157     $extext = sprintf(_("The %s referenced a recording file listed below that does not exists. An entry has been generated, named %s, with the referenced file(s) but you should confirm that it really works and the real files exist. The file(s) referenced: %s "),$module, $displayname, $fn);
158     $nt->add_error('recordings', 'NEWREC-'.$id, $text, $extext, '', true, true);
159     unset($nt);
160
161     // return the id just created
162     return $id;
163   }
164 }
165
166 function recordings_get_id($fn) {
167   global $db;
168  
169   $sql = "SELECT id FROM recordings WHERE filename='$fn'";
170         $results = $db->getRow($sql, DB_FETCHMODE_ASSOC);
171   if (isset($results['id'])) {
172     return $results['id'];
173   } else {
174     return null;
175   }
176 }
177
178 function recordings_get_file($id) {
179   $res = recordings_get($id);
180   return $res['filename'];
181 }
182  
183
184 function recordings_list($compound=true) {
185
186   static $initialized=false;
187   static $full_list;
188   static $filter_list=array();
189
190   if ($initialized) {
191     return ($compound ? $full_list : $filter_list);
192   }
193   $initialized=true;
194
195   $sql = "SELECT * FROM recordings where displayname <> '__invalid' ORDER BY displayname";
196   $full_list = sql($sql,'getAll',DB_FETCHMODE_ASSOC);
197
198   // Make array backward compatible, put first 4 columns as numeric
199   $count = 0;
200   foreach($full_list as $item) {
201     $full_list[$count][0] = $item['id'];
202     $full_list[$count][1] = $item['displayname'];
203     $full_list[$count][2] = $item['filename'];
204     $full_list[$count][3] = $item['description'];
205     if (strstr($item['filename'],'&') === false) {
206       $filter_list[] = $full_list[$count];
207     }
208     $count++;
209   }
210   return ($compound ? $full_list : $filter_list);
211 }
212
213 function recordings_get($id) {
214   global $db;
215         $sql = "SELECT * FROM recordings where id='$id'";
216         $results = $db->getRow($sql, DB_FETCHMODE_ASSOC);
217         if(DB::IsError($results)) {
218                 $results = null;
219         }
220   return $results;
221 }
222
223 function recordings_add($displayname, $filename, $description='') {
224   global $db;
225   global $recordings_astsnd_path;
226
227   // Check to make sure we can actually read the file if it has an extension (if it doesn't,
228   // it was put here by system recordings, so we know it's there.
229   if (preg_match("/\.(au|g723|g723sf|g726-\d\d|g729|gsm|h263|ilbc|mp3|ogg|pcm|[au]law|[au]l|mu|sln|raw|vox|WAV|wav|wav49)$/", $filename)) {
230     if (!is_readable($recordings_astsnd_path.$filename)) {
231       print "<p>Unable to add ".$recordings_astsnd_path.$filename." - Can not read file!</p>";
232       return false;
233     }
234     $fname = preg_replace("/\.(au|g723|g723sf|g726-\d\d|g729|gsm|h263|ilbc|mp3|ogg|pcm|[au]law|[au]l|mu|sln|raw|vox|WAV|wav|wav49)$/", "", $filename);
235
236   } else {
237     $fname = $filename;
238   }
239   $description = ($description != '') ? $db->escapeSimple($description) : _("No long description available");
240   $displayname = $db->escapeSimple($displayname);
241   sql("INSERT INTO recordings (displayname, filename, description) VALUES ( '$displayname', '$fname', '$description')");
242
243   return true;
244  
245 }
246
247 function recordings_update($id, $rname, $descr, $_REQUEST, $fcode=0, $fcode_pass='') {
248   global $db;
249
250   // Update the descriptive fields
251   $fcode_pass = preg_replace("/[^0-9*]/" ,"", trim($fcode_pass));
252   $results = sql("UPDATE recordings SET displayname = '".$db->escapeSimple($rname)."', description = '".$db->escapeSimple($descr)."', fcode='$fcode', fcode_pass='".$fcode_pass."' WHERE id = '$id'");
253  
254   // Build the file list from _REQUEST
255         $astsnd = isset($asterisk_conf['astvarlibdir'])?$asterisk_conf['astvarlibdir']:'/var/lib/asterisk';
256         $astsnd .= "/sounds/";
257   $recordings = Array();
258
259   // Set the file names from the submitted page, sysrec[N]
260   // We don't set if feature code was selected, we use what was already there
261   // because the fields will have been disabled and won't be accessible in the
262   // $_REQUEST array anyhow
263   //
264   if ($fcode != 1) {
265     // delete the feature code if it existed
266     //
267     $fcc = new featurecode('recordings', 'edit-recording-'.$id);
268     $fcc->delete();
269     unset($fcc); 
270     foreach ($_REQUEST as $key => $val) {
271       $res = strpos($key, 'sysrec');
272       if ($res !== false) {
273         // strip out any relative paths, since this is coming from a URL
274         str_replace('..','',$val);
275
276         $recordings[substr($key,6)]=$val;
277       }
278     }
279
280     // Stick the filename in the database
281     recordings_set_file($id, implode('&', $recordings));
282   } else {
283     // Add the feature code if it is needed
284     //
285     $fcc = new featurecode('recordings', 'edit-recording-'.$id);
286     $fcc->setDescription("Edit Recording: $rname");
287     $fcc->setDefault('*29'.$id);
288     $fcc->setProvideDest();
289     $fcc->update();
290     unset($fcc); 
291   }
292
293   // In _REQUEST there are also various actions (possibly)
294   // up[N] - Move file id N up one place
295   // down[N] - Move fid N down one place
296   // del[N] - Delete fid N
297  
298   foreach ($_REQUEST as $key => $val) {
299     if (strpos($key,"_") == 0) {
300             $up = strpos($key, "up");
301
302       $down = strpos($key, "down");
303       $del = strpos($key, "del");
304     }
305     if ( $up !== false ) {
306       $up = substr($key, 2);
307       recordings_move_file_up($id, $up);
308     }
309     if ($del !== false ) {
310       $del = substr($key,3);
311       recordings_delete_file($id, $del);
312     }
313     if ($down !== false ) {
314       $down = substr($key,4);
315       recordings_move_file_down($id, $down);
316     }
317   }
318 }
319
320 function recordings_move_file_up($id, $src) {
321   $files = recordings_get_file($id);
322   if ($src === 0 || $src < 0) { return false; } // Should never happen, up shouldn't appear whten fid=0
323   $tmparr = explode('&', $files);
324   $tmp = $tmparr[$src-1];
325   $tmparr[$src-1] = $tmparr[$src];
326   $tmparr[$src] = $tmp;
327   recordings_set_file($id, implode('&', $tmparr));
328 }
329 function recordings_move_file_down($id, $src) {
330   $files = recordings_get_file($id);
331   $tmparr = explode('&', $files);
332   $tmp = $tmparr[$src+1];
333   $tmparr[$src+1] = $tmparr[$src];
334   $tmparr[$src] = $tmp;
335   recordings_set_file($id, implode('&', $tmparr));
336 }
337 function recordings_delete_file($id, $src) {
338   $files = recordings_get_file($id);
339   $tmparr = explode('&', $files);
340   $tmp = Array();
341   $counter = 0;
342   foreach ($tmparr as $file) {
343     if ($counter != $src) { $tmp[] = $file; }
344     $counter++;
345   }
346   recordings_set_file($id, implode('&', $tmp));
347 }
348  
349
350 function recordings_del($id) {
351   $results = sql("DELETE FROM recordings WHERE id = \"$id\"");
352
353   // delete the feature code if it existed
354   $fcc = new featurecode('recordings', 'edit-recording-'.$id);
355   $fcc->delete();
356   unset($fcc); 
357 }
358
359 function recordings_set_file($id, $filename) {
360   global $db;
361   // Strip off any dangling &'s on the end:
362   $filename = rtrim($filename, '&');
363   $results = sql("UPDATE recordings SET filename = '".$db->escapeSimple($filename)."' WHERE id = '$id'");
364 }
365
366
367
368 function recordings_readdir($snddir) {
369   $files = recordings_getdir($snddir);
370   $ptr = 0;
371   foreach ($files as $fnam) {
372     $files[$ptr] = substr($fnam, strlen($snddir)+1);
373     $ptr++;
374   }
375   // Strip off every possible file extension
376   $flist = preg_replace("/\.(au|g723|g723sf|g726-\d\d|g729|gsm|h263|ilbc|mp3|ogg|pcm|[au]law|[au]l|mu|sln|raw|vox|WAV|wav|wav49)$/", "", $files);
377   sort($flist);
378   return array_unique($flist);
379 }
380  
381 function recordings_getdir($snddir) {
382   $dir = opendir($snddir);
383   $files = Array();
384   while ($fn = readdir($dir)) {
385     if ($fn == '.' || $fn == '..') { continue; }
386     if (is_dir($snddir.'/'.$fn)) {
387       $files = array_merge(recordings_getdir($snddir.'/'.$fn), $files);
388       continue;
389     }
390     $files[] = $snddir.'/'.$fn;
391   }
392   return $files;
393 }
394
395 function recordings_list_usage($id) {
396   global $active_modules;
397   $full_usage_arr = array();
398
399   foreach(array_keys($active_modules) as $mod) {
400     $function = $mod."_recordings_usage";
401     if (function_exists($function)) {
402       if (isset($_COOKIE['lang']) && is_dir("./modules/$mod/i18n/".$_COOKIE['lang'])) {
403         $prev_domain = textdomain(NULL);
404         bindtextdomain($mod,"./modules/$mod/i18n");
405         bind_textdomain_codeset($mod, 'utf8');
406         textdomain($mod);
407         $recordings_usage = $function($id);
408         textdomain($prev_domain);
409       } else {
410         $recordings_usage = $function($id);
411       }
412       if (!empty($recordings_usage)) {
413         $full_usage_arr = array_merge($full_usage_arr, $recordings_usage);
414       }
415     }
416   }
417   return $full_usage_arr;
418 }
419
420 ?>
Note: See TracBrowser for help on using the browser.