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

Revision 9083, 20.4 kB (checked in by p_lindheimer, 3 years ago)

Auto Check-in of any outstanding patches

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