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

Revision 6903, 11.1 kB (checked in by p_lindheimer, 5 years ago)

set proper translation domain for callbacks that creates recording usage list of links

  • 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 $recordings_save_path = "/tmp/";
6 $recordings_astsnd_path = isset($asterisk_conf['astvarlibdir'])?$asterisk_conf['astvarlibdir']:'/var/lib/asterisk';
7 $recordings_astsnd_path .= "/sounds/";
8
9 function recordings_get_config($engine) {
10   global $ext;  // is this the best way to pass this?
11   global $recordings_save_path;
12  
13   $modulename = "recordings";
14   $appcontext = "app-recordings";
15  
16   switch($engine) {
17     case "asterisk":
18       // FeatureCodes for save / check
19       $fcc = new featurecode($modulename, 'record_save');
20       $fc_save = $fcc->getCodeActive();
21       unset($fcc);
22
23       $fcc = new featurecode($modulename, 'record_check');
24       $fc_check = $fcc->getCodeActive();
25       unset($fcc);
26
27       if ($fc_save != '' || $fc_check != '') {
28         $ext->addInclude('from-internal-additional', 'app-recordings'); // Add the include from from-internal
29         
30         if ($fc_save != '') {
31           $ext->add($appcontext, $fc_save, '', new ext_macro('user-callerid'));
32           $ext->add($appcontext, $fc_save, '', new ext_wait('2'));
33           $ext->add($appcontext, $fc_save, '', new ext_macro('systemrecording', 'dorecord'));
34         }
35
36         if ($fc_check != '') {
37           $ext->add($appcontext, $fc_check, '', new ext_macro('user-callerid'));
38           $ext->add($appcontext, $fc_check, '', new ext_wait('2'));
39           $ext->add($appcontext, $fc_check, '', new ext_macro('systemrecording', 'docheck'));
40         }
41       }
42
43       // Now generate the Feature Codes to edit recordings
44       //
45       $recordings = recordings_list();
46       foreach ($recordings as $item) {
47
48         // Get the feature code, and do a sanity check if it is not suppose to be active and delete it
49         //
50         if ($item['fcode'] != 0) {
51           $fcc = new featurecode($modulename, 'edit-recording-'.$item['id']);
52           $fcode = $fcc->getCodeActive();
53           unset($fcc);
54         } else {
55           $fcc = new featurecode('recordings', 'edit-recording-'.$item['id']);
56           $fcc->delete();
57           unset($fcc); 
58           continue; // loop back to foreach
59         }
60
61         if ($fcode != '') {
62           // Do a sanity check, there should be no compound files
63           //
64           if (strpos($item['filename'], '&') === false && trim($item['filename']) != '') {
65             $fcode_pass = (trim($item['fcode_pass']) != '') ? ','.$item['fcode_pass'] : '';
66             $ext->add($appcontext, $fcode, '', new ext_macro('user-callerid'));
67             $ext->add($appcontext, $fcode, '', new ext_wait('2'));
68             $ext->add($appcontext, $fcode, '', new ext_macro('systemrecording', 'docheck,'.$item['filename'].$fcode_pass));
69             //$ext->add($appcontext, $fcode, '', new ext_macro('hangup'));
70           }
71         }
72       }
73     break;
74   }
75 }
76
77 function recordings_get_or_create_id($fn, $module) {
78   $id = recordings_get_id($fn);
79   if ($id != null) {
80     return $id;
81   } else {
82     // Create the id, name it the file name or if multi-part ...
83     //
84     $dname = explode('&',$displayname);
85     $displayname = 'auto-created: ';
86     $displayname .= count($dname) == 1 ? $fn : $dname[0]."&...";
87     $description = sprintf(_("Missing Sound file auto-created from migration of %s module"),$module);
88     recordings_add($displayname, $fn, $description='');
89
90     // get the id we just created
91     //
92     $id = recordings_get_id($fn);
93
94     // Notify of issue
95     //
96     $nt =& notifications::create($db);
97     $text = sprintf(_("Non-Existent Recording in module %s"),$module);
98     $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);
99     $nt->add_error('recordings', 'NEWREC-'.$id, $text, $extext, '', true, true);
100     unset($nt);
101
102     // return the id just created
103     return $id;
104   }
105 }
106
107 function recordings_get_id($fn) {
108   global $db;
109  
110   $sql = "SELECT id FROM recordings WHERE filename='$fn'";
111         $results = $db->getRow($sql, DB_FETCHMODE_ASSOC);
112   if (isset($results['id'])) {
113     return $results['id'];
114   } else {
115     return null;
116   }
117 }
118
119 function recordings_get_file($id) {
120   $res = recordings_get($id);
121   return $res['filename'];
122 }
123  
124
125 function recordings_list($compound=true) {
126   global $db;
127
128   // I'm not clued on how 'Department's' work. There obviously should be
129   // somee checking in here for it.
130
131   $sql = "SELECT * FROM recordings where displayname <> '__invalid' ORDER BY displayname";
132   $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
133   if(DB::IsError($results)) {
134     return array();
135   }
136   // Make array backward compatible, put first 4 columns as numeric
137   $count = 0;
138   foreach($results as $item) {
139     if (!$compound && strstr($item['filename'],'&') !== false) {
140       unset($results[$count]);
141     } else {
142       $results[$count][0] = $item['id'];
143       $results[$count][1] = $item['displayname'];
144       $results[$count][2] = $item['filename'];
145       $results[$count][3] = $item['description'];
146     }
147     $count++;
148   }
149   return $results;
150 }
151
152 function recordings_get($id) {
153   global $db;
154         $sql = "SELECT * FROM recordings where id='$id'";
155         $results = $db->getRow($sql, DB_FETCHMODE_ASSOC);
156         if(DB::IsError($results)) {
157                 $results = null;
158         }
159   return $results;
160 }
161
162 function recordings_add($displayname, $filename, $description='') {
163   global $db;
164   global $recordings_astsnd_path;
165
166   // Check to make sure we can actually read the file if it has an extension (if it doesn't,
167   // it was put here by system recordings, so we know it's there.
168   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)) {
169     if (!is_readable($recordings_astsnd_path.$filename)) {
170       print "<p>Unable to add ".$recordings_astsnd_path.$filename." - Can not read file!</p>";
171       return false;
172     }
173     $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);
174
175   } else {
176     $fname = $filename;
177   }
178   $description = ($description != '') ? $db->escapeSimple($description) : _("No long description available");
179   $displayname = $db->escapeSimple($displayname);
180   sql("INSERT INTO recordings (displayname, filename, description) VALUES ( '$displayname', '$fname', '$description')");
181
182   return true;
183  
184 }
185
186 function recordings_update($id, $rname, $descr, $_REQUEST, $fcode=0, $fcode_pass='') {
187   global $db;
188
189   // Update the descriptive fields
190   $fcode_pass = preg_replace("/[^0-9*]/" ,"", trim($fcode_pass));
191   $results = sql("UPDATE recordings SET displayname = '".$db->escapeSimple($rname)."', description = '".$db->escapeSimple($descr)."', fcode='$fcode', fcode_pass='".$fcode_pass."' WHERE id = '$id'");
192  
193   // Build the file list from _REQUEST
194         $astsnd = isset($asterisk_conf['astvarlibdir'])?$asterisk_conf['astvarlibdir']:'/var/lib/asterisk';
195         $astsnd .= "/sounds/";
196   $recordings = Array();
197
198   // Set the file names from the submitted page, sysrec[N]
199   // We don't set if feature code was selected, we use what was already there
200   // because the fields will have been disabled and won't be accessible in the
201   // $_REQUEST array anyhow
202   //
203   if ($fcode != 1) {
204     // delete the feature code if it existed
205     //
206     $fcc = new featurecode('recordings', 'edit-recording-'.$id);
207     $fcc->delete();
208     unset($fcc); 
209     foreach ($_REQUEST as $key => $val) {
210       $res = strpos($key, 'sysrec');
211       if ($res !== false) {
212         // strip out any relative paths, since this is coming from a URL
213         str_replace('..','',$val);
214
215         $recordings[substr($key,6)]=$val;
216       }
217     }
218
219     // Stick the filename in the database
220     recordings_set_file($id, implode('&', $recordings));
221   } else {
222     // Add the feature code if it is needed
223     //
224     $fcc = new featurecode('recordings', 'edit-recording-'.$id);
225     $fcc->setDescription("Edit Recording: $rname");
226     $fcc->setDefault('*29'.$id);
227     $fcc->update();
228     unset($fcc); 
229   }
230
231   // In _REQUEST there are also various actions (possibly)
232   // up[N] - Move file id N up one place
233   // down[N] - Move fid N down one place
234   // del[N] - Delete fid N
235  
236   foreach ($_REQUEST as $key => $val) {
237     if (strpos($key,"_") == 0) {
238             $up = strpos($key, "up");
239
240       $down = strpos($key, "down");
241       $del = strpos($key, "del");
242     }
243     if ( $up !== false ) {
244       $up = substr($key, 2);
245       recordings_move_file_up($id, $up);
246     }
247     if ($del !== false ) {
248       $del = substr($key,3);
249       recordings_delete_file($id, $del);
250     }
251     if ($down !== false ) {
252       $down = substr($key,4);
253       recordings_move_file_down($id, $down);
254     }
255   }
256 }
257
258 function recordings_move_file_up($id, $src) {
259   $files = recordings_get_file($id);
260   if ($src === 0 || $src < 0) { return false; } // Should never happen, up shouldn't appear whten fid=0
261   $tmparr = explode('&', $files);
262   $tmp = $tmparr[$src-1];
263   $tmparr[$src-1] = $tmparr[$src];
264   $tmparr[$src] = $tmp;
265   recordings_set_file($id, implode('&', $tmparr));
266 }
267 function recordings_move_file_down($id, $src) {
268   $files = recordings_get_file($id);
269   $tmparr = explode('&', $files);
270   $tmp = $tmparr[$src+1];
271   $tmparr[$src+1] = $tmparr[$src];
272   $tmparr[$src] = $tmp;
273   recordings_set_file($id, implode('&', $tmparr));
274 }
275 function recordings_delete_file($id, $src) {
276   $files = recordings_get_file($id);
277   $tmparr = explode('&', $files);
278   $tmp = Array();
279   $counter = 0;
280   foreach ($tmparr as $file) {
281     if ($counter != $src) { $tmp[] = $file; }
282     $counter++;
283   }
284   recordings_set_file($id, implode('&', $tmp));
285 }
286  
287
288 function recordings_del($id) {
289   $results = sql("DELETE FROM recordings WHERE id = \"$id\"");
290
291   // delete the feature code if it existed
292   $fcc = new featurecode('recordings', 'edit-recording-'.$id);
293   $fcc->delete();
294   unset($fcc); 
295 }
296
297 function recordings_set_file($id, $filename) {
298   global $db;
299   // Strip off any dangling &'s on the end:
300   $filename = rtrim($filename, '&');
301   $results = sql("UPDATE recordings SET filename = '".$db->escapeSimple($filename)."' WHERE id = '$id'");
302 }
303
304
305
306 function recordings_readdir($snddir) {
307   $files = recordings_getdir($snddir);
308   $ptr = 0;
309   foreach ($files as $fnam) {
310     $files[$ptr] = substr($fnam, strlen($snddir)+1);
311     $ptr++;
312   }
313   // Strip off every possible file extension
314   $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);
315   sort($flist);
316   return array_unique($flist);
317 }
318  
319 function recordings_getdir($snddir) {
320   $dir = opendir($snddir);
321   $files = Array();
322   while ($fn = readdir($dir)) {
323     if ($fn == '.' || $fn == '..') { continue; }
324     if (is_dir($snddir.'/'.$fn)) {
325       $files = array_merge(recordings_getdir($snddir.'/'.$fn), $files);
326       continue;
327     }
328     $files[] = $snddir.'/'.$fn;
329   }
330   return $files;
331 }
332
333 function recordings_list_usage($id) {
334   global $active_modules;
335   $full_usage_arr = array();
336
337   foreach(array_keys($active_modules) as $mod) {
338     $function = $mod."_recordings_usage";
339     if (function_exists($function)) {
340       if (isset($_COOKIE['lang']) && is_dir("./modules/$mod/i18n/".$_COOKIE['lang'])) {
341         $prev_domain = textdomain(NULL);
342         bindtextdomain($mod,"./modules/$mod/i18n");
343         bind_textdomain_codeset($mod, 'utf8');
344         textdomain($mod);
345         $recordings_usage = $function($id);
346         textdomain($prev_domain);
347       } else {
348         $recordings_usage = $function($id);
349       }
350       if (!empty($recordings_usage)) {
351         $full_usage_arr = array_merge($full_usage_arr, $recordings_usage);
352       }
353     }
354   }
355   return $full_usage_arr;
356 }
357
358 ?>
Note: See TracBrowser for help on using the browser.