root/modules/branches/2.5/music/page.music.php

Revision 7175, 19.7 kB (checked in by ethans, 5 years ago)

Changes sox to use vol nomenclature for adjusting volume of output file, rather than -v which was deprecated. The vol switch will work on
older and newer versions of sox.

Closes #3346

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php /* $Id$ */
2 //Copyright (C) 2004 Coalescent Systems Inc. (info@coalescentsystems.ca)
3 //
4 //This program is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU General Public License
6 //as published by the Free Software Foundation; either version 2
7 //of the License, or (at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //GNU General Public License for more details.
13 ?>
14
15 <?php
16 $action = isset($_REQUEST['action'])?$_REQUEST['action']:'';
17 $randon = isset($_REQUEST['randon'])?$_REQUEST['randon']:'';
18 $randoff = isset($_REQUEST['randoff'])?$_REQUEST['randoff']:'';
19 $category = strtr(isset($_REQUEST['category'])?$_REQUEST['category']:''," ./\"\'\`", "------");
20 if ($category == null) $category = 'default';
21 $display='music';
22
23 global $amp_conf;
24
25 if ($category == "default") {
26     $path_to_dir = $amp_conf['ASTVARLIBDIR']."/mohmp3"; //path to directory u want to read.
27 } else {
28     $path_to_dir = $amp_conf['ASTVARLIBDIR']."/mohmp3/$category"; //path to directory u want to read.
29 }
30
31
32 if (strlen($randon)) {
33     touch($path_to_dir."/.random");
34     createmusicconf();
35     needreload();
36 }
37 if (strlen($randoff)) {
38     unlink($path_to_dir."/.random");
39     createmusicconf();
40     needreload();
41 }
42 switch ($action) {
43     case "addednewstream":
44     case "editednewstream":
45         $stream = isset($_REQUEST['stream'])?$_REQUEST['stream']:'';
46         $format = isset($_REQUEST['format'])?trim($_REQUEST['format']):'';
47         if ($format != "") {
48             $stream .= "\nformat=$format";
49         }
50         makestreamcatergory($path_to_dir,$stream);
51         createmusicconf();
52         needreload();
53         redirect_standard();
54     case "addednew":
55         makemusiccategory($path_to_dir);
56         createmusicconf();
57         needreload();
58         redirect_standard();
59     break;
60     case "addedfile":
61         createmusicconf();
62         needreload();
63 //        redirect_standard();
64     break;
65     case "delete":
66         //$fh = fopen("/tmp/music.log","a");
67         //fwrite($fh,print_r($_REQUEST,true));
68         music_rmdirr("$path_to_dir");
69         $path_to_dir = $amp_conf['ASTVARLIBDIR']."/mohmp3"; //path to directory u want to read.
70         $category='default';
71         createmusicconf();
72         needreload();
73         redirect_standard();
74     break;
75 }
76
77
78 ?>
79 </div>
80 <div class="rnav"><ul>
81     <li><a href="config.php?display=<?php echo urlencode($display)?>&action=add"><?php echo _("Add Music Category")?></a></li>
82     <li><a href="config.php?display=<?php echo urlencode($display)?>&action=addstream"><?php echo _("Add Streaming Category")?></a></li>
83
84 <?php
85 //get existing trunk info
86 $tresults = music_list($amp_conf['ASTVARLIBDIR']."/mohmp3");
87 if (isset($tresults)) {
88     foreach ($tresults as $tresult) {
89         if ($tresult != "none") {
90             echo "<li><a id=\"".($category==$tresult ? 'current':'')."\" href=\"config.php?display=".urlencode($display)."&category=".urlencode($tresult)."&action=edit\">{$tresult}</a></li>";
91         }
92     }
93 }
94 ?>
95 </ul></div>
96
97
98 <?php
99 function createmusicconf() {
100     global $amp_conf;
101
102     $File_Write="";
103     $tresults = music_list($amp_conf['ASTVARLIBDIR']."/mohmp3");
104     if (isset($tresults)) {
105         foreach ($tresults as $tresult)  {
106             // hack - but his is all a hack until redone, in functions, etc.
107             // this puts a none category to allow no music to be chosen
108             //
109             if ($tresult == "none") {
110                 $dir = "/dev/null";
111                 $File_Write.="[{$tresult}]\nmode=files\ndirectory={$dir}\n";
112                 continue;
113             }
114             if ($tresult != "default" ) {
115                 $dir = $amp_conf['ASTVARLIBDIR']."/mohmp3/{$tresult}/";
116             } else {
117                 $dir = $amp_conf['ASTVARLIBDIR']."/mohmp3/";
118             }
119             if (file_exists("{$dir}.custom")) {
120                 $application = file_get_contents("{$dir}.custom");
121                 $File_Write.="[{$tresult}]\nmode=custom\napplication=$application\n";
122             } else if (file_exists("{$dir}.random")) {
123                 $File_Write.="[{$tresult}]\nmode=files\ndirectory={$dir}\nrandom=yes\n";
124             } else {
125                 $File_Write.="[{$tresult}]\nmode=files\ndirectory={$dir}\n";
126             }
127         }
128     }
129
130
131     $handle = fopen($amp_conf['ASTETCDIR']."/musiconhold_additional.conf", "w");
132
133     if (fwrite($handle, $File_Write) === FALSE) {
134         echo _("Cannot write to file")." ($tmpfname)";
135         exit;
136     }
137
138     fclose($handle);
139 }
140
141 function makestreamcatergory($path_to_dir,$stream) {
142     if (!is_dir($path_to_dir)) {
143         makemusiccategory($path_to_dir);
144     }
145     $fh=fopen("$path_to_dir/.custom","w");
146     fwrite($fh,$stream);
147     fclose($fh);
148 }
149
150 function makemusiccategory($path_to_dir) {
151     mkdir("$path_to_dir", 0755);
152 }
153  
154 function build_list() {
155     global $path_to_dir;
156     $pattern = '';
157     $handle=opendir($path_to_dir) ;
158     $extensions = array('mp3','wav'); // list of extensions to match
159     
160     //generate the pattern to look for.
161     $pattern = '/(\.'.implode('|\.',$extensions).')$/i';
162     
163     //store file names that match pattern in an array
164     $i = 0;
165     while (($file = readdir($handle))!==false) {
166         if ($file != "." && $file != "..") {
167             if(preg_match($pattern,$file)) {
168                 $file_array[$i] = $file; //pattern is matched store it in file_array.
169                 $i++;       
170             }
171         }
172     }
173     closedir($handle);
174     
175     return (isset($file_array))?$file_array:null//return the size of the array
176 }
177
178 function draw_list($file_array, $path_to_dir, $category) {
179     global $display;
180     //list existing mp3s and provide delete buttons
181     if ($file_array) {
182         foreach ($file_array as $thisfile) {
183             print "<div style=\"text-align:right;width:550px;border: 1px solid;padding:2px;\">";
184             print "<b style=\"float:left;margin-left:5px;\" >".$thisfile."</b>";
185
186             $delURL = $_SERVER['SCRIPT_NAME']."?display=".(isset($display)?$display:'')."&del=".urlencode($thisfile)."&category=".$category;
187             $tlabel = _("Delete");
188             $label = '<span><img width="16" height="16" border="0" title="'.$tlabel.'" alt="'.$tlabel.'" src="images/core_delete.png"/>&nbsp;</span>';
189             echo "<a style=\"margin-right:5px;\" href=".$delURL.">".$label."</a></div><br />";
190         }
191     }
192 }
193
194 function process_mohfile($mohfile,$onlywav=false,$volume=false) {
195     global $path_to_dir;
196     global $amp_conf;
197
198     $output = 0;
199     $returncode = 0;
200     $origmohfile=$path_to_dir."/orig_".$mohfile;
201     if ($amp_conf['AMPMPG123']) {
202         if($onlywav) {
203             $newname = substr($mohfile,0,strrpos($mohfile,"."));
204
205             // If we are dealing with an MP3, we need to decode it to a wav file
206             if (strpos($origmohfile,'.mp3') !== false)  {
207                 $mpg123cmd = "mpg123 -w \"".substr($origmohfile,0,strrpos($origmohfile,".")).".wav\" \"".$origmohfile."\" 2>&1 ";
208                 exec($mpg123cmd, $output, $returncode);
209             }
210             $newmohfile = $path_to_dir."/wav_".$newname.".wav";
211             //asdf
212             $soxcmd = "sox \"".substr($origmohfile,0,strrpos($origmohfile,".")).".wav\"";
213             $soxcmd .= " -r 8000 -c 1 \"".$newmohfile."\"";
214             if($volume){
215                 $soxcmd .= " vol ".$volume;
216             }
217             $soxresample = " resample -ql ";
218             exec($soxcmd.$soxresample."2>&1", $output, $returncode);
219             if ($returncode != 0) {
220                 // try it again without the resample in case the input sample rate was the same
221                 //
222                 exec("rm -rf \"".$newmohfile."\"");
223                 exec($soxcmd."2>&1", $output, $returncode);
224             }
225         }
226     } else { // AMPMPG123
227         $newname = strtr($mohfile,"&", "_");
228         if(strstr($newname,".mp3")) {
229             $onlywav = false;
230         }
231
232         if(!$onlywav) {
233             $newmohfile=$path_to_dir."/". ((strpos($newname,'.mp3') === false) ? $newname.".mp3" : $newname);
234             $lamecmd="lame --cbr -m m -t -F \"".$origmohfile."\" \"".$newmohfile."\" 2>&1 ";
235             if (strpos($newmohfile,'.mp3') !== false) {
236                 exec($lamecmd, $output, $returncode);
237             }
238         } else {
239             $newmohfile = $path_to_dir."/wav_".$newname;
240             $soxcmd = "sox \"".$origmohfile."\" -r 8000 -c 1 \"".$newmohfile."\" ";
241             $soxresample = "resample -ql ";
242             exec($soxcmd.$soxresample."2>&1", $output, $returncode);                                                     
243             if ($returncode != 0) {                                                                                       
244                 // try it again without the resample in case the input sample rate was the same                             
245                 //                                                                                                         
246                 exec("rm -rf \"".$newmohfile."\"");                                                                         
247                 exec($soxcmd."2>&1", $output, $returncode);                                                                 
248             }
249         }
250     } // AMPMPG123
251
252     if ($returncode != 0) {
253         return join("<br>\n", $output);
254     }
255     $rmcmd="rm -f \"". $origmohfile."\"";
256     exec($rmcmd);
257     if ($amp_conf['AMPMPG123']) {
258         // If this started as an mp3, we converted it to a wav and then transcoded it from there,
259         // so we have two "original" files to delete
260         //
261         if (strpos($origmohfile,'.mp3') !== false)  {
262             $rmcmd="rm -f \"". substr($origmohfile,0,strrpos($origmohfile,".")).".wav\"";
263             exec($rmcmd);
264         }
265     } // AMPMPG123
266     return null;
267 }
268
269 ?>
270
271 <div class="content">
272 <h2><?php echo _("On Hold Music")?></h2>
273
274 <?php
275 if ($action == 'add') {
276     ?>
277     <form name="addcategory" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return addcategory_onsubmit();">
278     <input type="hidden" name="display" value="<?php echo $display?>">
279     <input type="hidden" name="action" value="addednew">
280     <table>
281     <tr><td colspan="2"><h5><?php echo _("Add Music Category")?><hr></h5></td></tr>
282     <tr>
283         <td><a href="#" class="info"><?php echo _("Category Name:")?><span><?php echo _("Allows you to Set up Different Categories for music on hold.  This is useful if you would like to specify different Hold Music or Commercials for various ACD Queues.")?> </span></a></td>
284         <td><input type="text" name="category" value=""></td>
285     </tr>
286     <tr>
287         <td colspan="2"><br><h6><input name="Submit" type="submit" value='<?php echo _("Submit Changes")?>' ></h6></td>       
288     </tr>
289     </table>
290 <script language="javascript">
291 <!--
292
293 var theForm = document.addcategory;
294 theForm.category.focus();
295
296 function addcategory_onsubmit() {
297     var msgInvalidCategoryName = "<?php echo _('Please enter a valid Category Name'); ?>";
298     var msgReservedCategoryName = "<?php echo _('Categories: \"none\" and \"default\" are reserved names. Please enter a different name'); ?>";
299
300     defaultEmptyOK = false;
301     if (!isAlphanumeric(theForm.category.value))
302         return warnInvalid(theForm.category, msgInvalidCategoryName);
303     if (theForm.category.value == "default" || theForm.category.value == "none")
304         return warnInvalid(theForm.category, msgReservedCategoryName);
305    
306     return true;
307 }
308
309 //-->
310 </script>
311
312     </form>
313     <br><br><br><br><br>
314
315 <?php
316     } else if ($action == 'addstream') {
317     ?>
318     <form name="addstream" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return addstream_onsubmit();">
319     <input type="hidden" name="display" value="<?php echo $display?>">
320     <input type="hidden" name="action" value="addednewstream">
321     <table>
322     <tr><td colspan="2"><h5><?php echo _("Add Streaming Category")?><hr></h5></td></tr>
323     <tr>
324         <td><a href="#" class="info"><?php echo _("Category Name:")?><span><?php echo _("Allows you to Set up Different Categories for music on hold.  This is useful if you would like to specify different Hold Music or Commercials for various ACD Queues.")?> </span></a></td>
325         <td><input type="text" name="category" value=""></td>
326     </tr>
327     <tr>
328         <td><a href="#" class="info"><?php echo _("Application:")?><span><?php echo _("This is the \"application=\" line used to provide the streaming details to Asterisk. See information on musiconhold.conf configuration for different audio and internet streaming source options.")?> </span></a></td>
329         <td><input type="text" name="stream" size="80" value=""></td>
330     </tr>
331     <tr>
332     <tr>
333         <td><a href="#" class="info"><?php echo _("Optional Format:")?><span><?php echo _("Optional value for \"format=\" line used to provide the format to Asterisk. This should be a format understood by Asterisk such as ulaw, and is specific to the streaming application you are using. See information on musiconhold.conf configuration for different audio and internet streaming source options.")?> </span></a></td>
334         <td><input type="text" name="format" size="6" value=""></td>
335     </tr>
336     <tr>
337         <td colspan="2"><br><h6><input name="Submit" type="submit" value='<?php echo _("Submit Changes")?>' ></h6></td>       
338     </tr>
339     </table>
340 <script language="javascript">
341 <!--
342
343 var theForm = document.addstream;
344 theForm.category.focus();
345
346 function addstream_onsubmit() {
347     var msgInvalidCategoryName = "<?php echo _('Please enter a valid Category Name'); ?>";
348     var msgInvalidStreamName = "<?php echo _('Please enter a streaming application command and arguments'); ?>";
349     var msgReservedCategoryName = "<?php echo _('Categories: \"none\" and \"default\" are reserved names. Please enter a different name'); ?>";
350
351     defaultEmptyOK = false;
352     if (!isAlphanumeric(theForm.category.value))
353         return warnInvalid(theForm.category, msgInvalidCategoryName);
354     if (theForm.category.value == "default" || theForm.category.value == "none")
355         return warnInvalid(theForm.category, msgReservedCategoryName);
356     if (isEmpty(theForm.stream.value))
357         return warnInvalid(theForm.stream, msgInvalidStreamName);
358    
359     return true;
360 }
361
362 //-->
363 </script>
364
365     </form>
366     <br><br><br><br><br>
367
368 <?php
369 } else {
370 ?>
371
372     <h5><?php echo _("Category:")?> <?php echo $category=="default"?_("default"):$category;?></h5>
373 <?php 
374     if (file_exists("{$path_to_dir}/.custom")) {
375         $application = file_get_contents("{$path_to_dir}/.custom");
376         $application = explode("\n",$application);
377         if (isset($application[1])) {
378             $format = explode('=',$application[1],2);
379             $format = $format[1];
380         } else {
381             $format = "";
382         }
383     } else {
384         $application = false;
385     }
386     if ($category!="default") {
387         $delURL = $_SERVER['PHP_SELF'].'?display='.urlencode($display).'&action=delete&category='.urlencode($category);
388         $tlabel = sprintf(($application === false)?_("Delete Music Category %s"):_("Delete Streaming Category"),$category);
389         $label = '<span><img width="16" height="16" border="0" title="'.$tlabel.'" alt="" src="images/core_delete.png"/>&nbsp;'.$tlabel.'</span>';
390 ?>
391         <p><a href="<?php echo $delURL ?>"><?php echo $label; ?></a></p>
392 <?php 
393     }
394     if ($application !== false) {
395     ?>
396         <form name="editstream" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return editstream_onsubmit();">
397         <input type="hidden" name="display" value="<?php echo $display?>">
398         <input type="hidden" name="action" value="editednewstream">
399         <table>
400         <tr><td colspan="2"><h5><?php echo _("Edit Streaming Category").": $category"?><hr></h5></td></tr>
401         <tr>
402             <td><a href="#" class="info"><?php echo _("Application:")?><span><?php echo _("This is the \"application=\" line used to provide the streaming details to Asterisk. See information on musiconhold.conf configuration for different audio and internet streaming source options.")?> </span></a></td>
403             <td><input type="text" name="stream" size="80" value="<?php echo $application[0]?>"></td>
404         </tr>
405         <tr>
406             <td><a href="#" class="info"><?php echo _("Optional Format:")?><span><?php echo _("Optional value for \"format=\" line used to provide the format to Asterisk. This should be a format understood by Asterisk such as ulaw, and is specific to the streaming application you are using. See information on musiconhold.conf configuration for different audio and internet streaming source options.")?> </span></a></td>
407             <td><input type="text" name="format" size="6" value="<?php echo $format?>"></td>
408         </tr>
409         <tr>
410             <td colspan="2"><br><h6><input name="Submit" type="submit" value='<?php echo _("Submit Changes")?>' ></h6></td>       
411         </tr>
412         </table>
413 <script language="javascript">
414 <!--
415
416 var theForm = document.editstream;
417 theForm.stream.focus();
418
419 function editstream_onsubmit() {
420     var msgInvalidStreamName = "<?php echo _('Please enter a streaming application command and arguments'); ?>";
421
422     defaultEmptyOK = false;
423     if (isEmpty(theForm.stream.value))
424         return warnInvalid(theForm.stream, msgInvalidStreamName);
425    
426     return true;
427 }
428 //-->
429 </script>
430
431         </form>
432         <br><br><br><br><br>
433
434 <?php
435     } else { // normal moh dir
436 ?>
437
438     <form enctype="multipart/form-data" name="upload" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"/>
439         <?php echo _("Upload a .wav or .mp3 file:")?><br>
440         <input type="hidden" name="display" value="<?php echo $display?>">
441         <input type="hidden" name="category" value="<?php echo "$category" ?>">
442         <input type="hidden" name="action" value="addedfile">
443         <input type="file" name="mohfile" tabindex="<?php echo ++$tabindex;?>"/>
444         <input type="button" value="<?php echo _("Upload")?>" onclick="document.upload.submit(upload);alert('<?php echo addslashes(_("Please wait until the page loads. Your file is being processed."))?>');" tabindex="<?php echo ++$tabindex;?>"/>
445         <br />
446 <?php
447     if ($amp_conf['AMPMPG123']) {
448 ?>
449         <select name="volume" tabindex="<?php echo ++$tabindex;?>">
450             <option value="1.50">Volume 150%</option>
451             <option value="1.25">Volume 125%</option>
452             <option value="" selected>Volume 100%</option>
453             <option value=".75">Volume 75%</option>
454             <option value=".5">Volume 50%</option>
455             <option value=".25">Volume 25%</option>
456             <option value=".1">Volume 10%</option>
457         </select>
458         <a href="#" class="info"><?php echo "&nbsp;"._("Volume Adjustment")?><span> <?php echo _("The volume adjustment is a linear value. Since loudness is logarithmic, the linear level will be less of an adjustment. You should test out the installed music to assure it is at the correct volume. This feature will convert MP3 files to WAV files. If you do not have mpg123 installed, you can set the parameter: <strong>AMPMPG123=false</strong> in your amportal.conf file") ?></span></a>
459 <?php
460     } else { // AMPMPG123
461 ?>
462         <input type="checkbox" name="onlywav" checked="checked"><small><?php echo _("Do not encode wav to mp3"); ?></small>
463 <?php
464     } // AMPMPG123
465 ?>
466     </form>
467     <br />
468     <form name="randomon" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
469     <?php
470         if (file_exists("{$path_to_dir}/.random")) {
471             ?> <input type="submit" name="randoff" value="<?php echo _("Disable Random Play");?>"> <?php
472         } else {
473             ?> <input type="submit" name="randon" value="<?php echo _("Enable Random Play");?>"> <?php
474         }
475     ?>
476     </form>
477     <br />
478     <?php
479
480     // Check to see if the upload failed for some reason
481     if (isset($_FILES['mohfile']['name']) && !is_uploaded_file($_FILES['mohfile']['tmp_name'])) {
482         if (strlen($_FILES['mohfile']['name']) == 0) {
483             echo "<h5> PHP "._("Error Processing")."! "._("No file provided")." "._("Please select a file to upload")."</h5>";
484         } else {
485             echo "<h5> PHP "._("Error Processing")." ".$_FILES['mohfile']['name']."! "._("Check")." upload_max_filesize "._("in")." /etc/php.ini</h5>";
486         }
487     }
488     if (isset($_FILES['mohfile']['tmp_name']) && is_uploaded_file($_FILES['mohfile']['tmp_name'])) {
489         //echo $_FILES['mohfile']['name']." uploaded OK";
490         move_uploaded_file($_FILES['mohfile']['tmp_name'], $path_to_dir."/orig_".$_FILES['mohfile']['name']);
491
492         if ($amp_conf['AMPMPG123']) {
493             $process_err = process_mohfile($_FILES['mohfile']['name'],true,$_REQUEST['volume']);
494         } else {
495             $process_err = process_mohfile($_FILES['mohfile']['name'],($_REQUEST['onlywav'] != ''));
496         }
497
498         if (isset($process_err)) {
499             echo "<h5>"._("Error Processing").": \"$process_err\" for ".$_FILES['mohfile']['name']."!</h5>\n";
500             echo "<h5>"._("This is not a fatal error, your Music on Hold may still work.")."</h5>\n";
501         } else {
502             echo "<h5>"._("Completed processing")." ".$_FILES['mohfile']['name']."!</h5>";
503         }
504         needreload();
505     }
506
507     //build the array of files
508     $file_array = build_list();
509     $numf = count($file_array);
510     } // normal moh dir
511
512
513     if (isset($_REQUEST['del'])) {
514         $del = $_REQUEST['del'];
515         if (strpos($del, "\"") || strpos($del, "\'") || strpos($del, "\;")) {
516             print "You're trying to use an invalid character. Please don't.\n";
517             exit;
518         }
519         if (($numf == 1) && ($category == "default") ){
520             echo "<h5>"._("You must have at least one file for On Hold Music.  Please upload one before deleting this one.")."</h5>";
521         } else {
522             if (@unlink($path_to_dir."/".$del)) {
523                 echo "<h5>"._("Deleted")." ".$del."!</h5>";
524             } else {
525                 echo "<h5>".sprintf(_("Error Deleting %s"),$del)."!</h5>";
526             }
527             //kill_mpg123();
528             needreload();
529         }
530     }
531     if ($application === false) {
532         $file_array = build_list();
533         draw_list($file_array, $path_to_dir, $category);
534     }
535     ?>
536     <br><br><br><br><br><br>
537 <?php
538 }
539 ?>
540
541
Note: See TracBrowser for help on using the browser.