From 61bd88ce9bae592abfbdd10215e593d301bc0df6 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Wed, 18 May 2016 11:21:08 -0500 Subject: [PATCH] Minor - Indent code --- main/document/create_audio.php | 651 +++++++++++++++++---------------- 1 file changed, 329 insertions(+), 322 deletions(-) diff --git a/main/document/create_audio.php b/main/document/create_audio.php index c35a8afe20..57f2bc3336 100755 --- a/main/document/create_audio.php +++ b/main/document/create_audio.php @@ -2,14 +2,14 @@ /* For licensing terms, see /license.txt */ /** - * This file allows creating audio files from a text. + * This file allows creating audio files from a text. * - * @package chamilo.document + * @package chamilo.document * * @author Juan Carlos RaƱa Trabado * @since 8/January/2011 * TODO:clean all file -*/ + */ require_once '../inc/global.inc.php'; $_SESSION['whereami'] = 'document/createaudio'; @@ -23,26 +23,26 @@ api_block_anonymous_users(); $groupId = api_get_group_id(); if (api_get_setting('enabled_text2audio') == 'false') { - api_not_allowed(true); + api_not_allowed(true); } $document_data = DocumentManager::get_document_data_by_id( - $_REQUEST['id'], - api_get_course_id() + $_REQUEST['id'], + api_get_course_id() ); if (empty($document_data)) { if (api_is_in_group()) { - $group_properties = GroupManager::get_group_properties( - $groupId - ); - $document_id = DocumentManager::get_document_id( - api_get_course_info(), - $group_properties['directory'] - ); - $document_data = DocumentManager::get_document_data_by_id( - $document_id, - api_get_course_id() - ); + $group_properties = GroupManager::get_group_properties( + $groupId + ); + $document_id = DocumentManager::get_document_id( + api_get_course_info(), + $group_properties['directory'] + ); + $document_data = DocumentManager::get_document_data_by_id( + $document_id, + api_get_course_id() + ); } } $document_id = $document_data['id']; @@ -55,59 +55,59 @@ $is_allowed_to_edit = api_is_allowed_to_edit(null, true); // Please, do not modify this dirname formatting if (strstr($dir, '..')) { - $dir = '/'; + $dir = '/'; } if ($dir[0] == '.') { - $dir = substr($dir, 1); + $dir = substr($dir, 1); } if ($dir[0] != '/') { - $dir = '/'.$dir; + $dir = '/' . $dir; } if ($dir[strlen($dir) - 1] != '/') { - $dir .= '/'; + $dir .= '/'; } -$filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir; +$filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir; if (!is_dir($filepath)) { - $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/'; - $dir = '/'; + $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/'; + $dir = '/'; } //groups //TODO: clean if (!empty($groupId)) { - $interbreadcrumb[] = array( - "url" => "../group/group_space.php?".api_get_cidreq(), - "name" => get_lang('GroupSpace') - ); - $group = GroupManager :: get_group_properties($groupId); - $path = explode('/', $dir); - if ('/'.$path[1] != $group['directory']) { - api_not_allowed(true); - } + $interbreadcrumb[] = array( + "url" => "../group/group_space.php?" . api_get_cidreq(), + "name" => get_lang('GroupSpace') + ); + $group = GroupManager:: get_group_properties($groupId); + $path = explode('/', $dir); + if ('/' . $path[1] != $group['directory']) { + api_not_allowed(true); + } } -$interbreadcrumb[] = array ( - "url" => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(), +$interbreadcrumb[] = array( + "url" => "./document.php?curdirpath=" . urlencode($dir) . "&" . api_get_cidreq(), "name" => get_lang('Documents') ); if (!$is_allowed_in_course) { - api_not_allowed(true); + api_not_allowed(true); } if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'] || - DocumentManager::is_my_shared_folder( - api_get_user_id(), - Security::remove_XSS($dir), - api_get_session_id() - )) + DocumentManager::is_my_shared_folder( + api_get_user_id(), + Security::remove_XSS($dir), + api_get_session_id() + )) ) { - api_not_allowed(true); + api_not_allowed(true); } @@ -116,10 +116,10 @@ Event::event_access_tool(TOOL_DOCUMENT); $display_dir = $dir; if (isset ($group)) { - $display_dir = explode('/', $dir); - unset ($display_dir[0]); - unset ($display_dir[1]); - $display_dir = implode('/', $display_dir); + $display_dir = explode('/', $dir); + unset ($display_dir[0]); + unset ($display_dir[1]); + $display_dir = implode('/', $display_dir); } // Interbreadcrumb for the current directory root path @@ -129,80 +129,81 @@ $array_len = count($dir_array); $dir_acum = ''; for ($i = 0; $i < $array_len; $i++) { - $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i]; - //Max char 80 - $url_to_who = cut($dir_array[$i],80); - $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who); - $dir_acum .= $dir_array[$i].'/'; + $url_dir = 'document.php?&curdirpath=' . $dir_acum . $dir_array[$i]; + //Max char 80 + $url_to_who = cut($dir_array[$i], 80); + $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who); + $dir_acum .= $dir_array[$i] . '/'; } -Display :: display_header($nameTools, 'Doc'); +Display:: display_header($nameTools, 'Doc'); echo '
'; -echo ''. - Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).''; -echo ''. - Display::return_icon('google.png',get_lang('GoogleAudio'),'',ICON_SIZE_MEDIUM).''; -echo ''. - Display::return_icon('pediaphon.png', get_lang('Pediaphon'),'',ICON_SIZE_MEDIUM).''; +echo '' . + Display::return_icon('back.png', get_lang('BackTo') . ' ' . get_lang('DocumentsOverview'), '', + ICON_SIZE_MEDIUM) . ''; +echo '' . + Display::return_icon('google.png', get_lang('GoogleAudio'), '', ICON_SIZE_MEDIUM) . ''; +echo '' . + Display::return_icon('pediaphon.png', get_lang('Pediaphon'), '', ICON_SIZE_MEDIUM) . ''; echo '
'; ?> - - - -
+ + +
'form1')); + $form = new FormValidator('form1', 'post', null, '', array('id' => 'form1')); $form->addHeader(get_lang('HelpText2Audio')); - $form->addElement('hidden', 'text2voice_mode', 'google'); - $form->addElement('hidden', 'id', $document_id); - $form->addElement('text', 'title', get_lang('Title')); - $form->addElement('select', 'lang', get_lang('Language'), $options); - $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google')); + $form->addElement('hidden', 'text2voice_mode', 'google'); + $form->addElement('hidden', 'id', $document_id); + $form->addElement('text', 'title', get_lang('Title')); + $form->addElement('select', 'lang', get_lang('Language'), $options); + $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google')); $form->addButtonSave(get_lang('SaveMP3')); - $defaults = array(); - $defaults['lang'] = $selected_language; - $form->setDefaults($defaults); - $form->display(); + $defaults = array(); + $defaults['lang'] = $selected_language; + $form->setDefaults($defaults); + $form->display(); } if (Security::remove_XSS($_GET['dt2a']) == 'pediaphon') { - //lang default is a default message - $selected_language = "defaultmessage"; - $options_pedia['defaultmessage'] =get_lang('FirstSelectALanguage'); - $options['defaultmessage'] =get_lang('FirstSelectALanguage'); + //lang default is a default message + $selected_language = "defaultmessage"; + $options_pedia['defaultmessage'] = get_lang('FirstSelectALanguage'); + $options['defaultmessage'] = get_lang('FirstSelectALanguage'); - $form = new FormValidator('form2', 'post', null, '', array('id' => 'form2')); + $form = new FormValidator('form2', 'post', null, '', array('id' => 'form2')); $form->addHeader(get_lang('HelpText2Audio')); - $form->addElement('hidden', 'text2voice_mode','pediaphon'); - $form->addElement('hidden', 'id', $document_id); - $form->addElement('text', 'title', get_lang('Title')); - $form->addSelect('lang', get_lang('Language'), $options_pedia, array('class' => 'lang')); + $form->addElement('hidden', 'text2voice_mode', 'pediaphon'); + $form->addElement('hidden', 'id', $document_id); + $form->addElement('text', 'title', get_lang('Title')); + $form->addSelect('lang', get_lang('Language'), $options_pedia, array('class' => 'lang')); $form->addSelect('voices', get_lang('Voice'), array(get_lang('FirstSelectALanguage')), array('id' => 'voices')); $speed_options = array(); $speed_options['1'] = get_lang('Normal'); @@ -254,53 +255,53 @@ if (Security::remove_XSS($_GET['dt2a']) == 'pediaphon') { $speed_options['1.2'] = get_lang('Slow'); $speed_options['1.6'] = get_lang('SlowDown'); - $form->addElement('select', 'speed', get_lang('Speed'), $speed_options, array()); - $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_pediaphon')); - //echo Display :: return_icon('info3.gif', get_lang('HelpPediaphon'), array('align' => 'absmiddle', 'hspace' => '3px'), false); - $form->addButtonSave(get_lang('SaveMP3')); - $defaults = array(); - $defaults['lang'] = $selected_language; - $form->setDefaults($defaults); - $form->display(); - ?> - - - - + window.location.href="'.$location.'"'; - return; - } - - $_course = api_get_course_info(); - $_user = api_get_user_info(); - - $clean_title=trim($_POST['title']); - $clean_text=trim($_POST['text']); - if(empty($clean_title) || empty($clean_text)){ - echo ''; - return; - } - $clean_title = Security::remove_XSS($clean_title); - $clean_title = Database::escape_string($clean_title); - $clean_title = str_replace(' ', '_', $clean_title);//compound file names - - $clean_text = Security::remove_XSS($clean_text); - $clean_lang = Security::remove_XSS($_POST['lang']); - - $extension='mp3'; - $audio_filename=$clean_title.'.'.$extension; - $audio_title = str_replace('_',' ',$clean_title); - - //prevent duplicates - if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){ - $i = 1; - while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) $i++; - $audio_filename = $clean_title . '_' . $i . '.'.$extension; - $audio_title = $clean_title . '_' . $i . '.'.$extension; - $audio_title = str_replace('_',' ',$audio_title); - } - - $documentPath = $filepath.'/'.$audio_filename; - - $clean_text = api_replace_dangerous_char($clean_text); - - // adding the file - // add new file to disk - - $proxySettings = api_get_configuration_value('proxy_settings'); + $location = 'create_audio.php?' . api_get_cidreq() . '&id=' . intval($_POST['id']) . '&dt2a=google'; + + //security + if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) { + echo ''; + return; + } + + $_course = api_get_course_info(); + $_user = api_get_user_info(); + + $clean_title = trim($_POST['title']); + $clean_text = trim($_POST['text']); + if (empty($clean_title) || empty($clean_text)) { + echo ''; + return; + } + $clean_title = Security::remove_XSS($clean_title); + $clean_title = Database::escape_string($clean_title); + $clean_title = str_replace(' ', '_', $clean_title);//compound file names + + $clean_text = Security::remove_XSS($clean_text); + $clean_lang = Security::remove_XSS($_POST['lang']); + + $extension = 'mp3'; + $audio_filename = $clean_title . '.' . $extension; + $audio_title = str_replace('_', ' ', $clean_title); + + //prevent duplicates + if (file_exists($filepath . '/' . $clean_title . '.' . $extension)) { + $i = 1; + while (file_exists($filepath . '/' . $clean_title . '_' . $i . '.' . $extension)) { + $i++; + } + $audio_filename = $clean_title . '_' . $i . '.' . $extension; + $audio_title = $clean_title . '_' . $i . '.' . $extension; + $audio_title = str_replace('_', ' ', $audio_title); + } + + $documentPath = $filepath . '/' . $audio_filename; + + $clean_text = api_replace_dangerous_char($clean_text); + + // adding the file + // add new file to disk + + $proxySettings = api_get_configuration_value('proxy_settings'); $key = api_get_configuration_value('translate_app_google_key'); - $url = "https://www.googleapis.com/language/translate/v2?key=$key&".$clean_lang."&target=$clean_lang&q=".urlencode($clean_text).""; + $url = "https://www.googleapis.com/language/translate/v2?key=$key&" . $clean_lang . "&target=$clean_lang&q=" . urlencode($clean_text) . ""; - if (empty($proxySettings)) { - $content = file_get_contents($url); - } else { - $context = stream_context_create($proxySettings); - $content = file_get_contents($url, false, $context); - } + if (empty($proxySettings)) { + $content = file_get_contents($url); + } else { + $context = stream_context_create($proxySettings); + $content = file_get_contents($url, false, $context); + } file_put_contents( $documentPath, $content ); - // add document to database - $current_session_id = api_get_session_id(); - $groupId = api_get_group_id(); - $relativeUrlPath=$dir; - $doc_id = add_document( - $_course, - $relativeUrlPath.$audio_filename, - 'file', - filesize($documentPath), - $audio_title - ); - api_item_property_update( - $_course, - TOOL_DOCUMENT, - $doc_id, - 'DocumentAdded', - $_user['user_id'], - $groupId, - null, - null, - null, - $current_session_id - ); - Display::display_confirmation_message(get_lang('DocumentCreated')); - //return to location - echo ''; + // add document to database + $current_session_id = api_get_session_id(); + $groupId = api_get_group_id(); + $relativeUrlPath = $dir; + $doc_id = add_document( + $_course, + $relativeUrlPath . $audio_filename, + 'file', + filesize($documentPath), + $audio_title + ); + api_item_property_update( + $_course, + TOOL_DOCUMENT, + $doc_id, + 'DocumentAdded', + $_user['user_id'], + $groupId, + null, + null, + null, + $current_session_id + ); + Display::display_confirmation_message(get_lang('DocumentCreated')); + //return to location + echo ''; } /** @@ -410,12 +413,12 @@ function downloadMP3_google($filepath, $dir) */ function downloadMP3_pediaphon($filepath, $dir) { - $location='create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&dt2a=pediaphon'; - //security - if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) { - echo ''; - return; - } + $location = 'create_audio.php?' . api_get_cidreq() . '&id=' . intval($_POST['id']) . '&dt2a=pediaphon'; + //security + if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) { + echo ''; + return; + } $_course = api_get_course_info(); $_user = api_get_user_info(); $clean_title = trim($_POST['title']); @@ -423,74 +426,78 @@ function downloadMP3_pediaphon($filepath, $dir) $clean_text = trim($_POST['text']); $clean_voices = Security::remove_XSS($_POST['voices']); if (empty($clean_title) || empty($clean_text) || empty($clean_voices)) { - echo ''; + echo ''; return; } - $clean_title = Security::remove_XSS($clean_title); - $clean_title = Database::escape_string($clean_title); - $clean_title = str_replace(' ', '_', $clean_title);//compound file names - $clean_text = Security::remove_XSS($clean_text); - $clean_lang = Security::remove_XSS($_POST['lang']); - $clean_speed = Security::remove_XSS($_POST['speed']); - - $extension='mp3'; - $audio_filename=$clean_title.'.'.$extension; - $audio_title = str_replace('_',' ',$clean_title); - - //prevent duplicates - if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){ - $i = 1; - while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) $i++; - $audio_filename = $clean_title . '_' . $i . '.'.$extension; - $audio_title = $clean_title . '_' . $i . '.'.$extension; - $audio_title = str_replace('_',' ',$audio_title); - } - - $documentPath = $filepath.'/'.$audio_filename; - $clean_text = api_replace_dangerous_char($clean_text); - - //adding the file - - if ($clean_lang == 'de') { - $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice.cgi'; - $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/(.*)\.mp3\"/'; - } else { - $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice_'.$clean_lang.'.cgi';//en, es, fr - $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/'.$clean_lang.'\/(.*)\.mp3\"/'; - } - - $data="stimme=".$clean_voices."&inputtext=".$clean_text."&speed=".$clean_speed."&go=speak"; - $opts = array('http' => - array( - 'method' => 'POST', - 'header' =>"Content-Type: application/x-www-form-urlencoded\r\n", - "Content-Length: " . strlen($data) . "\r\n", - 'content' => $data - ) - ); - - $context = stream_context_create($opts); - // Download the whole HTML page - $previous_returntext2voice = file_get_contents($url_pediaphon,false,$context); - - //extract the audio file path - $search_source = preg_match($find_t2v, $previous_returntext2voice, $hits); - $souce_end = substr($hits[0], 0, -1); - //download file - $returntext2voice = file_get_contents($souce_end); - //save file - $f = @file_put_contents($documentPath, $returntext2voice); - if ($f === false && !empty($php_errormsg)) { - error_log($php_errormsg); - } - //add document to database - $current_session_id = api_get_session_id(); - $groupId = api_get_group_id(); - $relativeUrlPath=$dir; - $doc_id = add_document($_course, $relativeUrlPath.$audio_filename, 'file', filesize($documentPath), $audio_title); - api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id); + $clean_title = Security::remove_XSS($clean_title); + $clean_title = Database::escape_string($clean_title); + $clean_title = str_replace(' ', '_', $clean_title);//compound file names + $clean_text = Security::remove_XSS($clean_text); + $clean_lang = Security::remove_XSS($_POST['lang']); + $clean_speed = Security::remove_XSS($_POST['speed']); + + $extension = 'mp3'; + $audio_filename = $clean_title . '.' . $extension; + $audio_title = str_replace('_', ' ', $clean_title); + + //prevent duplicates + if (file_exists($filepath . '/' . $clean_title . '.' . $extension)) { + $i = 1; + while (file_exists($filepath . '/' . $clean_title . '_' . $i . '.' . $extension)) { + $i++; + } + $audio_filename = $clean_title . '_' . $i . '.' . $extension; + $audio_title = $clean_title . '_' . $i . '.' . $extension; + $audio_title = str_replace('_', ' ', $audio_title); + } + + $documentPath = $filepath . '/' . $audio_filename; + $clean_text = api_replace_dangerous_char($clean_text); + + //adding the file + + if ($clean_lang == 'de') { + $url_pediaphon = 'http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice.cgi'; + $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/(.*)\.mp3\"/'; + } else { + $url_pediaphon = 'http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice_' . $clean_lang . '.cgi';//en, es, fr + $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/' . $clean_lang . '\/(.*)\.mp3\"/'; + } + + $data = "stimme=" . $clean_voices . "&inputtext=" . $clean_text . "&speed=" . $clean_speed . "&go=speak"; + $opts = array( + 'http' => + array( + 'method' => 'POST', + 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", + "Content-Length: " . strlen($data) . "\r\n", + 'content' => $data + ) + ); + + $context = stream_context_create($opts); + // Download the whole HTML page + $previous_returntext2voice = file_get_contents($url_pediaphon, false, $context); + + //extract the audio file path + $search_source = preg_match($find_t2v, $previous_returntext2voice, $hits); + $souce_end = substr($hits[0], 0, -1); + //download file + $returntext2voice = file_get_contents($souce_end); + //save file + $f = @file_put_contents($documentPath, $returntext2voice); + if ($f === false && !empty($php_errormsg)) { + error_log($php_errormsg); + } + //add document to database + $current_session_id = api_get_session_id(); + $groupId = api_get_group_id(); + $relativeUrlPath = $dir; + $doc_id = add_document($_course, $relativeUrlPath . $audio_filename, 'file', filesize($documentPath), $audio_title); + api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, + null, $current_session_id); Display::display_confirmation_message(get_lang('DocumentCreated')); - //return to location - echo ''; + //return to location + echo ''; }