Portions created by the Initial Developer are Copyright (C) 2008-2020 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane James Rose */ //includes include "root.php"; require_once "resources/require.php"; require_once "resources/check_auth.php"; //check permissions if (permission_exists('music_on_hold_view')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //get the music_on_hold array $sql = "select * from v_music_on_hold "; $sql .= "where true "; if ($_GET['show'] != "all" || !permission_exists('music_on_hold_all')) { $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; } if (permission_exists('music_on_hold_domain')) { $sql .= "or domain_uuid is null "; } $sql .= "order by domain_uuid desc, music_on_hold_name asc, music_on_hold_rate asc"; $database = new database; $streams = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); //get the http post data if (is_array($_POST['moh'])) { $action = $_POST['action']; $moh = $_POST['moh']; } //process the http post data by action if ($action != '' && is_array($moh) && @sizeof($moh) != 0) { switch ($action) { case 'delete': if (permission_exists('music_on_hold_delete')) { $obj = new switch_music_on_hold; $obj->delete($moh); } break; } header('Location: music_on_hold.php'); exit; } //download music on hold file if ($_GET['action'] == "download" && is_uuid($_GET['id']) && is_array($streams) && @sizeof($streams) != 0) { //get the uuid $stream_uuid = $_GET['id']; //get the record foreach($streams as $row) { if ($stream_uuid == $row['music_on_hold_uuid']) { $stream_domain_uuid = $row['domain_uuid']; $stream_name = $row['music_on_hold_name']; $stream_path = $row['music_on_hold_path']; break; } } //replace the sounds_dir variable in the path $stream_path = str_replace('$${sounds_dir}', $_SESSION['switch']['sounds']['dir'], $stream_path); $stream_path = str_replace('..', '', $stream_path); //get the file and sanitize it $stream_file = basename($_GET['file']); $search = array('..', '/', ':'); $stream_file = str_replace($search, '', $stream_file); //join the path and file name $stream_full_path = path_join($stream_path, $stream_file); //download the file if (file_exists($stream_full_path)) { //content-range if (isset($_SERVER['HTTP_RANGE']) && $_GET['t'] != "bin") { range_download($stream_full_path); } $fd = fopen($stream_full_path, "rb"); if ($_GET['t'] == "bin") { header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); } else { $stream_file_ext = pathinfo($stream_file, PATHINFO_EXTENSION); switch ($stream_file_ext) { case "wav" : header("Content-Type: audio/x-wav"); break; case "mp3" : header("Content-Type: audio/mpeg"); break; case "ogg" : header("Content-Type: audio/ogg"); break; } } header('Content-Disposition: attachment; filename="'.$stream_file.'"'); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past if ($_GET['t'] == "bin") { header("Content-Length: ".filesize($stream_full_path)); } ob_clean(); fpassthru($fd); } exit; } //upload music on hold file if ($_POST['action'] == 'upload' && is_array($_FILES) && is_uploaded_file($_FILES['file']['tmp_name']) ) { //validate the token $token = new token; if (!$token->validate($_SERVER['PHP_SELF'])) { message::add($text['message-invalid_token'],'negative'); header('Location: music_on_hold.php'); exit; } //determine name if ($_POST['name_new'] != '') { //set the action $action = 'add'; //get the stream_name $stream_name = $_POST['name_new']; //get the rate $stream_rate = is_numeric($_POST['rate']) ? $_POST['rate'] : ''; } else { //get the stream uuid $stream_uuid = $_POST['name']; //find the matching stream if (is_array($streams) && @sizeof($streams) != 0) { foreach ($streams as $row) { if ($stream_uuid == $row['music_on_hold_uuid']) { //set the action $action = 'update'; //set the variables $stream_domain_uuid = $row['domain_uuid']; $stream_name = $row['music_on_hold_name']; $stream_path = $row['music_on_hold_path']; $stream_rate = $row['music_on_hold_rate']; $stream_shuffle = $row['music_on_hold_shuffle']; $stream_channels = $row['music_on_hold_channels']; $stream_internal = $row['music_on_hold_interval']; $stream_timer_name = $row['music_on_hold_timer_name']; $stream_chime_list = $row['music_on_hold_chime_list']; $stream_chime_freq = $row['music_on_hold_chime_freq']; $stream_chime_max = $row['music_on_hold_chime_max']; $stream_rate = $row['music_on_hold_rate']; //end the loop break; } } } } //get remaining values $stream_file_name_temp = $_FILES['file']['tmp_name']; $stream_file_name = $_FILES['file']['name']; $stream_file_ext = strtolower(pathinfo($stream_file_name, PATHINFO_EXTENSION)); //check file type $valid_file_type = ($stream_file_ext == 'wav' || $stream_file_ext == 'mp3' || $stream_file_ext == 'ogg') ? true : false; //proceed for valid file type if ($stream_file_ext == 'wav' || $stream_file_ext == 'mp3' || $stream_file_ext == 'ogg') { //strip slashes, replace spaces $slashes = ["/","\\"]; $stream_file_name = str_replace($slashes, '', $stream_file_name); $stream_file_name = str_replace(' ', '-', $stream_file_name); if ($action == "add") { $stream_name = str_replace($slashes, '', $stream_name); $stream_name = str_replace(' ', '_', $stream_name); } //detect auto rate if ($stream_rate == '') { $path_rate = '48000'; $stream_rate_auto = true; } else { $path_rate = $stream_rate; $stream_rate_auto = false; } //define default path if ($action == "add") { $stream_path = path_join($_SESSION['switch']['sounds']['dir'], 'music', $_SESSION['domain_name'], $stream_name, $path_rate); $stream_path = str_replace('.loc', '._loc', $stream_path); // 14.03.22 freeswitch bug } //find whether the path already exists $stream_new_name = true; if (is_array($streams) && @sizeof($streams) != 0) { foreach ($streams as $row) { $alternate_path = str_replace('$${sounds_dir}', $_SESSION['switch']['sounds']['dir'], $row['music_on_hold_path']); if ($stream_path == $row['music_on_hold_path'] || $stream_path == $alternate_path) { $stream_new_name = false; break; } } } //set the variables $stream_path = str_replace('$${sounds_dir}', $_SESSION['switch']['sounds']['dir'], $stream_path); //add new path if ($stream_new_name) { $stream_uuid = uuid(); $array['music_on_hold'][0]['music_on_hold_uuid'] = $stream_uuid; $array['music_on_hold'][0]['domain_uuid'] = $domain_uuid; $array['music_on_hold'][0]['music_on_hold_name'] = $stream_name; $array['music_on_hold'][0]['music_on_hold_path'] = $stream_path; $array['music_on_hold'][0]['music_on_hold_rate'] = strlen($stream_rate) != 0 ? $stream_rate : null; $array['music_on_hold'][0]['music_on_hold_shuffle'] = 'false'; $array['music_on_hold'][0]['music_on_hold_channels'] = 1; $array['music_on_hold'][0]['music_on_hold_interval'] = 20; $array['music_on_hold'][0]['music_on_hold_timer_name'] = 'soft'; $array['music_on_hold'][0]['music_on_hold_chime_list'] = null; $array['music_on_hold'][0]['music_on_hold_chime_freq'] = null; $array['music_on_hold'][0]['music_on_hold_chime_max'] = null; $p = new permissions; $p->add('music_on_hold_add', 'temp'); $database = new database; $database->app_name = 'music_on_hold'; $database->app_uuid = '1dafe0f8-c08a-289b-0312-15baf4f20f81'; $database->save($array); unset($array); $p->delete('music_on_hold_add', 'temp'); } //check target folder, move uploaded file if (!is_dir($stream_path)) { mkdir($stream_path, 0770, true); // 14.03.22 freeswitch bug - shouldn't be needed with freeswitch 1.10.8 if (preg_match('|^(/usr/share/freeswitch/sounds/music/(.*?\._loc.*?))/|', $stream_path, $m)) { $fs_bug_target = $m[2]; $fs_bug_link = str_replace('._loc', '.loc', $m[1]); symlink($fs_bug_target, $fs_bug_link); } } if (is_dir($stream_path)) { if (copy($stream_file_name_temp, $stream_path.'/'.$stream_file_name)) { @unlink($stream_file_name_temp); } } //set message message::add($text['message-upload_completed']); //clear the cache $cache = new cache; $cache->delete("configuration:local_stream.conf"); //require_once "app/music_on_hold/resources/classes/switch_music_on_hold.php"; $music = new switch_music_on_hold; $music->reload(); } //set message for unsupported file type else { message::add($text['message-unsupported_file_type']); } //redirect header("Location: music_on_hold.php"); exit; } //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //include the header $document['title'] = $text['title-music_on_hold']; require_once "resources/header.php"; //script echo ""; //show the content echo "
\n"; echo "
".$text['title-music_on_hold']."
\n"; echo "
\n"; if (permission_exists('music_on_hold_add')) { $modify_add_action = !is_array($streams) || @sizeof($streams) == 0 ? "name_mode('new'); $('#btn_select').hide();" : null; //hide categories select box when none exist echo "
\n"; echo "\n"; echo "\n"; echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','onclick'=>"$(this).fadeOut(250, function(){ ".$modify_add_action." $('span#form_upload').fadeIn(250); });"]); echo "\n"; echo "
"; } if (permission_exists('music_on_hold_all')) { if ($_GET['show'] == 'all') { echo " "; } else { echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?type=&show=all'.($search != '' ? "&search=".urlencode($search) : null)]); } } if (permission_exists('music_on_hold_delete') && $streams) { echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none;','onclick'=>"modal_open('modal-delete','btn_delete');"]); } echo "
\n"; echo "
\n"; echo "
\n"; if (permission_exists('music_on_hold_delete') && $streams) { echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]); } echo $text['title_description-music_on_hold']."\n"; echo "

\n"; echo "
\n"; echo "\n"; //show the array of data if (is_array($streams) && @sizeof($streams) != 0) { $previous_name = ''; //loop through the array $x = 0; foreach ($streams as $row) { //set the variables $music_on_hold_name = $row['music_on_hold_name']; $music_on_hold_rate = $row['music_on_hold_rate']; //add the name (category) if ($previous_name != $music_on_hold_name) { echo "".escape($music_on_hold_name)."".(!is_uuid($row['domain_uuid']) ? '   ('.$text['label-global'].')' : null)."
\n"; } //determine if rate was set to auto or not $auto_rate = strlen($music_on_hold_rate) == 0 ? true : false; //determine icons to show $stream_icons = array(); $i = 0; if (permission_exists('music_on_hold_path')) { $stream_icons[$i]['icon'] = 'fa-folder-open'; $stream_icons[$i]['title'] = $row['music_on_hold_name']; $i++; } if ($row['music_on_hold_shuffle'] == 'true') { $stream_icons[$i]['icon'] = 'fa-random'; $stream_icons[$i]['title'] = $text['label-shuffle']; $i++; } if ($row['music_on_hold_chime_list'] != '') { $stream_icons[$i]['icon'] = 'fa-bell'; $stream_icons[$i]['title'] = $text['label-chime_list'].': '.$row['music_on_hold_chime_list']; $i++; } if ($row['music_on_hold_channels'] == '2') { $stream_icons[$i]['icon'] = 'fa-headphones'; $stream_icons[$i]['title'] = $text['label-stereo']; $stream_icons[$i]['margin'] = 6; $i++; } if (is_array($stream_icons) && sizeof($stream_icons) > 0) { foreach ($stream_icons as $stream_icon) { $icons .= ""; } } //set the rate label $stream_rate = $auto_rate ? $text['option-default'] : ($music_on_hold_rate/1000).' kHz'; if (permission_exists('music_on_hold_edit')) { $stream_details = "".$stream_rate.' '.$icons; } else { $stream_details = $stream_rate.' '.$icons; } //get the music on hold path and files $stream_path = str_replace("\$\${sounds_dir}",$_SESSION['switch']['sounds']['dir'], $row['music_on_hold_path']); if (file_exists($stream_path)) { $stream_files = array_merge(glob($stream_path.'/*.wav'), glob($stream_path.'/*.mp3'), glob($stream_path.'/*.ogg')); } //start the table echo "\n"; echo " \n"; if (permission_exists('music_on_hold_delete')) { echo " \n"; } if ($_GET['show'] == "all" && permission_exists('music_on_hold_all')) { echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, $param, "class='shrink'"); } echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " "; unset($stream_icons, $icons); //list the stream files if (is_array($stream_files) && @sizeof($stream_files) != 0) { foreach ($stream_files as $stream_file_path) { $row_uuid = uuid(); $stream_file = pathinfo($stream_file_path, PATHINFO_BASENAME); $stream_file_size = byte_convert(filesize($stream_file_path)); $stream_file_date = date("M d, Y H:i:s", filemtime($stream_file_path)); $stream_file_ext = pathinfo($stream_file, PATHINFO_EXTENSION); switch ($stream_file_ext) { case "wav" : $stream_file_type = "audio/wav"; break; case "mp3" : $stream_file_type = "audio/mpeg"; break; case "ogg" : $stream_file_type = "audio/ogg"; break; } //playback progress bar echo "\n"; echo "\n"; // dummy row to maintain alternating background color $list_row_link = "javascript:recording_play('".$row_uuid."');"; echo "\n"; if (permission_exists('music_on_hold_delete')) { echo " \n"; } if ($_GET['show'] == "all" && permission_exists('music_on_hold_all')) { if (strlen($_SESSION['domains'][$row['domain_uuid']]['domain_name']) > 0) { $domain = $_SESSION['domains'][$row['domain_uuid']]['domain_name']; } else { $domain = $text['label-global']; } echo " \n"; } echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "\n"; $x++; } } echo "
\n"; echo " \n"; echo " \n"; echo " ".$stream_details."".$text['label-tools']."".$text['label-file-size']."".$text['label-uploaded']."
\n"; echo " \n"; echo " \n"; echo " ".escape($domain)."".escape($stream_file)."".escape($stream_file_size)."".escape($stream_file_date)."
\n"; echo "
\n"; //set the previous music_on_hold_name $previous_name = $music_on_hold_name; } unset($streams, $row); } echo "\n"; echo "
\n"; //include the footer require_once "resources/footer.php"; //define the download function (helps safari play audio sources) function range_download($file) { $fp = @fopen($file, 'rb'); $size = filesize($file); // File size $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte // Now that we've gotten so far without errors we send the accept range header /* At the moment we only support single ranges. * Multiple ranges requires some more work to ensure it works correctly * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 * * Multirange support annouces itself with: * header('Accept-Ranges: bytes'); * * Multirange content must be sent with multipart/byteranges mediatype, * (mediatype = mimetype) * as well as a boundry header to indicate the various chunks of data. */ header("Accept-Ranges: 0-$length"); // header('Accept-Ranges: bytes'); // multipart/byteranges // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 if (isset($_SERVER['HTTP_RANGE'])) { $c_start = $start; $c_end = $end; // Extract the range string list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); // Make sure the client hasn't sent us a multibyte range if (strpos($range, ',') !== false) { // (?) Shoud this be issued here, or should the first // range be used? Or should the header be ignored and // we output the whole content? header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); // (?) Echo some info to the client? exit; } // If the range starts with an '-' we start from the beginning // If not, we forward the file pointer // And make sure to get the end byte if spesified if ($range0 == '-') { // The n-number of the last bytes is requested $c_start = $size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; } /* Check the range and make sure it's treated according to the specs. * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ // End bytes can not be larger than $end. $c_end = ($c_end > $end) ? $end : $c_end; // Validate the requested range and return an error if it's not correct. if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); // (?) Echo some info to the client? exit; } $start = $c_start; $end = $c_end; $length = $end - $start + 1; // Calculate new content length fseek($fp, $start); header('HTTP/1.1 206 Partial Content'); } // Notify the client the byte range we'll be outputting header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); // Start buffered download $buffer = 1024 * 8; while(!feof($fp) && ($p = ftell($fp)) <= $end) { if ($p + $buffer > $end) { // In case we're only outputtin a chunk, make sure we don't // read past the length $buffer = $end - $p + 1; } set_time_limit(0); // Reset time limit for big files echo fread($fp, $buffer); flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit. } fclose($fp); } ?>