create_audio.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file allows creating audio files from a text.
  5. *
  6. * @package chamilo.document
  7. *
  8. * @author Juan Carlos Raña Trabado
  9. * @since 8/January/2011
  10. * TODO:clean all file
  11. */
  12. require_once '../inc/global.inc.php';
  13. $_SESSION['whereami'] = 'document/createaudio';
  14. $this_section = SECTION_COURSES;
  15. $nameTools = get_lang('CreateAudio');
  16. api_protect_course_script();
  17. api_block_anonymous_users();
  18. $groupId = api_get_group_id();
  19. if (api_get_setting('enabled_text2audio') == 'false') {
  20. api_not_allowed(true);
  21. }
  22. $document_data = DocumentManager::get_document_data_by_id(
  23. $_REQUEST['id'],
  24. api_get_course_id()
  25. );
  26. if (empty($document_data)) {
  27. if (api_is_in_group()) {
  28. $group_properties = GroupManager::get_group_properties(
  29. $groupId
  30. );
  31. $document_id = DocumentManager::get_document_id(
  32. api_get_course_info(),
  33. $group_properties['directory']
  34. );
  35. $document_data = DocumentManager::get_document_data_by_id(
  36. $document_id,
  37. api_get_course_id()
  38. );
  39. }
  40. }
  41. $document_id = $document_data['id'];
  42. $dir = $document_data['path'];
  43. //jquery textareaCounter
  44. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/textareacounter/jquery.textareaCounter.plugin.js" type="text/javascript"></script>';
  45. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  46. // Please, do not modify this dirname formatting
  47. if (strstr($dir, '..')) {
  48. $dir = '/';
  49. }
  50. if ($dir[0] == '.') {
  51. $dir = substr($dir, 1);
  52. }
  53. if ($dir[0] != '/') {
  54. $dir = '/'.$dir;
  55. }
  56. if ($dir[strlen($dir) - 1] != '/') {
  57. $dir .= '/';
  58. }
  59. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  60. if (!is_dir($filepath)) {
  61. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  62. $dir = '/';
  63. }
  64. //groups //TODO: clean
  65. if (!empty($groupId)) {
  66. $interbreadcrumb[] = array("url" => "../group/group_space.php?".api_get_cidreq(), "name" => get_lang('GroupSpace'));
  67. $group = GroupManager :: get_group_properties($groupId);
  68. $path = explode('/', $dir);
  69. if ('/'.$path[1] != $group['directory']) {
  70. api_not_allowed(true);
  71. }
  72. }
  73. $interbreadcrumb[] = array ("url" => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(), "name" => get_lang('Documents'));
  74. if (!$is_allowed_in_course) {
  75. api_not_allowed(true);
  76. }
  77. if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'] ||
  78. DocumentManager::is_my_shared_folder(
  79. api_get_user_id(),
  80. Security::remove_XSS($dir),
  81. api_get_session_id()
  82. ))
  83. ) {
  84. api_not_allowed(true);
  85. }
  86. /* Header */
  87. Event::event_access_tool(TOOL_DOCUMENT);
  88. $display_dir = $dir;
  89. if (isset ($group)) {
  90. $display_dir = explode('/', $dir);
  91. unset ($display_dir[0]);
  92. unset ($display_dir[1]);
  93. $display_dir = implode('/', $display_dir);
  94. }
  95. // Interbreadcrumb for the current directory root path
  96. // Copied from document.php
  97. $dir_array = explode('/', $dir);
  98. $array_len = count($dir_array);
  99. $dir_acum = '';
  100. for ($i = 0; $i < $array_len; $i++) {
  101. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  102. //Max char 80
  103. $url_to_who = cut($dir_array[$i],80);
  104. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  105. $dir_acum .= $dir_array[$i].'/';
  106. }
  107. Display :: display_header($nameTools, 'Doc');
  108. echo '<div class="actions">';
  109. echo '<a href="document.php?id='.$document_id.'">'.
  110. Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  111. echo '<a href="create_audio.php?'.api_get_cidreq().'&amp;id='.$document_id.'&amp;dt2a=google">'.
  112. Display::return_icon('google.png',get_lang('GoogleAudio'),'',ICON_SIZE_MEDIUM).'</a>';
  113. echo '<a href="create_audio.php?'.api_get_cidreq().'&amp;id='.$document_id.'&amp;dt2a=pediaphon">'.
  114. Display::return_icon('pediaphon.png', get_lang('Pediaphon'),'',ICON_SIZE_MEDIUM).'</a>';
  115. echo '</div>';
  116. ?>
  117. <!-- javascript and styles for textareaCounter-->
  118. <script type="text/javascript">
  119. var info;
  120. $(document).ready(function(){
  121. var options = {
  122. 'maxCharacterSize': 100,
  123. 'originalStyle': 'originalTextareaInfo',
  124. 'warningStyle' : 'warningTextareaInfo',
  125. 'warningNumber': 20,
  126. 'displayFormat' : '#input/#max'
  127. };
  128. $('#textarea_google').textareaCount(options, function(data){
  129. $('#textareaCallBack').html(data);
  130. });
  131. });
  132. </script>
  133. <style>
  134. .overview {
  135. background: #FFEC9D;
  136. padding: 10px;
  137. width: 90%;
  138. border: 1px solid #CCCCCC;
  139. }
  140. .originalTextareaInfo {
  141. font-size: 12px;
  142. color: #000000;
  143. text-align: right;
  144. }
  145. .warningTextareaInfo {
  146. color: #FF0000;
  147. font-weight:bold;
  148. text-align: right;
  149. }
  150. #showData {
  151. height: 70px;
  152. width: 200px;
  153. border: 1px solid #CCCCCC;
  154. padding: 10px;
  155. margin: 10px;
  156. }
  157. </style>
  158. <div id="textareaCallBack"></div>
  159. <?php
  160. if (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'google') {
  161. downloadMP3_google($filepath, $dir);
  162. } elseif (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'pediaphon') {
  163. downloadMP3_pediaphon($filepath, $dir);
  164. }
  165. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  166. $sql_select = "SELECT * FROM $tbl_admin_languages";
  167. $result_select = Database::query($sql_select);
  168. $options = $options_pedia = array();
  169. $selected_language = null;
  170. while ($row = Database::fetch_array($result_select)) {
  171. $options[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  172. if (in_array($row['isocode'], array('de', 'en', 'es', 'fr'))){
  173. $options_pedia[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  174. }
  175. }
  176. $icon = Display::return_icon('text2audio.png', get_lang('HelpText2Audio'),'',ICON_SIZE_MEDIUM);
  177. echo '<div class="page-header"><h2>'.$icon.get_lang('HelpText2Audio').'</h2></div>';
  178. if (Security::remove_XSS($_GET['dt2a']) == 'google') {
  179. $selected_language = api_get_language_isocode();//lang default is the course language
  180. echo '<div>';
  181. $form = new FormValidator('form1', 'post', null, '', array('id' => 'form1'));
  182. $form->addElement('hidden', 'text2voice_mode', 'google');
  183. $form->addElement('hidden', 'id', $document_id);
  184. $form->addElement('text', 'title', get_lang('Title'));
  185. $form->addElement('select', 'lang', get_lang('Language'), $options);
  186. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google', 'class' =>'span6' ));
  187. //echo Display :: return_icon('info3.gif', get_lang('HelpGoogleAudio'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  188. $form->addButtonSave(get_lang('SaveMP3'));
  189. $defaults = array();
  190. $defaults['lang'] = $selected_language;
  191. $form->setDefaults($defaults);
  192. $form->display();
  193. echo '</div>';
  194. }
  195. if (Security::remove_XSS($_GET['dt2a']) == 'pediaphon') {
  196. //lang default is a default message
  197. $selected_language = "defaultmessage";
  198. $options_pedia['defaultmessage'] =get_lang('FirstSelectALanguage');
  199. $options['defaultmessage'] =get_lang('FirstSelectALanguage');
  200. echo '<div>';
  201. $form = new FormValidator('form2', 'post', null, '', array('id' => 'form2'));
  202. $form->addElement('hidden', 'text2voice_mode','pediaphon');
  203. $form->addElement('hidden', 'id', $document_id);
  204. $form->addElement('text', 'title', get_lang('Title'));
  205. $form->addElement('select', 'lang', get_lang('Language'), $options_pedia, array('onclick' => 'update_voices(this.selectedIndex);'));
  206. $form->addElement('select', 'voices', get_lang('Voice'), array(get_lang('FirstSelectALanguage')), array());
  207. $speed_options = array();
  208. $speed_options['1'] = get_lang('Normal');
  209. $speed_options['0.75'] = get_lang('GoFaster');
  210. $speed_options['0.8'] = get_lang('Fast');
  211. $speed_options['1.2'] = get_lang('Slow');
  212. $speed_options['1.6'] = get_lang('SlowDown');
  213. $form->addElement('select', 'speed', get_lang('Speed'), $speed_options, array());
  214. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_pediaphon', 'class' =>'span6'));
  215. //echo Display :: return_icon('info3.gif', get_lang('HelpPediaphon'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  216. $form->addButtonSave(get_lang('SaveMP3'));
  217. $defaults = array();
  218. $defaults['lang'] = $selected_language;
  219. $form->setDefaults($defaults);
  220. $form->display();
  221. echo '</div>';
  222. ?>
  223. <!-- javascript form name form2 update voices -->
  224. <script type="text/javascript">
  225. var langslist=document.form2.lang
  226. var voiceslist=document.form2.voices
  227. var voices=new Array()
  228. <!--German -->
  229. voices[0]=["<?php echo get_lang('Female').' (de1)'; ?>|de1", "<?php echo get_lang('Male').' (de2)'; ?>|de2", "<?php echo get_lang('Female').' (de3)'; ?>|de3", "<?php echo get_lang('Male').' (de4)'; ?>|de4", "<?php echo get_lang('Female').' (de5)'; ?>|de5", "<?php echo get_lang('Male').' (de6)'; ?>|de6", "<?php echo get_lang('Female').' (de7)'; ?>|de7", "<?php echo get_lang('Female').' (de8 HQ)'; ?>|de8"]
  230. <!--English -->
  231. voices[1]=["<?php echo get_lang('Male').' (en1)'; ?>|en1", "<?php echo get_lang('Male').' (en2 HQ)'; ?>|en2", "<?php echo get_lang('Female').' (us1)'; ?>| us1", "<?php echo get_lang('Male').' (us2)'; ?>|us2", "<?php echo get_lang('Male').' (us3)'; ?>|us3", "<?php echo get_lang('Female').'(us4 HQ)'; ?>|us4"]
  232. <!--Spanish -->
  233. voices[2]=["<?php echo get_lang('Male').' (es5 HQ)'; ?>|es5"]
  234. <!--French -->
  235. voices[3]=["<?php echo get_lang('Female').' (fr8 HQ)'; ?>|fr8"]
  236. function update_voices(selectedvoicegroup){
  237. voiceslist.options.length=0
  238. for (i=0; i<voices[selectedvoicegroup].length; i++)
  239. voiceslist.options[voiceslist.options.length]=new Option(voices[selectedvoicegroup][i].split("|")[0], voices[selectedvoicegroup][i].split("|")[1])
  240. }
  241. </script>
  242. <?php
  243. }//end pediaphon
  244. //vozMe services
  245. //disabled for a time
  246. /*
  247. echo '<div>&nbsp;</div><input type="checkbox" value="1" name="checktext2voice" onclick="javascript: if(this.checked){document.getElementById(\'option3\').style.display=\'block\';}else{document.getElementById(\'option3\').style.display=\'none\';}"/>&nbsp;<img src="../img/file_sound.gif" title="'.get_lang('HelpvozMe').'" alt="'.get_lang('vozMe').'"/>&nbsp;'.get_lang('vozMe').'';
  248. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  249. echo '<div id="option3" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  250. echo '<form id="form3" name="form3" method="post" action="http://vozme.com/text2voice.php" target="mymp3" class="formw">';
  251. echo '<br/>';
  252. echo '<label>'.get_lang('Language').': ';
  253. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  254. $sql_select = "SELECT * FROM $tbl_admin_languages";
  255. $result_select = Database::query($sql_select);
  256. echo '<select name="lang" id="select">';
  257. while ($row = Database::fetch_array($result_select)) {
  258. if (in_array($row['isocode'], array('ca', 'en', 'es', 'hi', 'it', 'pt'))) {
  259. if (api_get_setting('platformLanguage')==$row['english_name']){
  260. echo '<option value="'.$row['isocode'].'" selected="selected">'.$row['original_name'].' ('.$row['english_name'].')</option>';
  261. } else {
  262. echo '<option value="'.$row['isocode'].'">'.$row['original_name'].' ('.$row['english_name'].')</option>';
  263. }
  264. }
  265. }
  266. echo '</select>';
  267. echo '</label>';
  268. echo '<label>&nbsp;&nbsp;'.get_lang('Voice').': ';
  269. echo '<select name="gn" id="select1">';
  270. echo '<option value="ml">'.get_lang('Male').'</option>';
  271. echo '<option value="fm">'.get_lang('Female').'</option>';
  272. echo '</select>';
  273. echo '</label>';
  274. echo '<br/><br/>';
  275. echo '<div>'.get_lang('InsertText2Audio').'</div>';
  276. echo '<br/>';
  277. echo '<label>';
  278. echo '<textarea name="text" id="textarea" cols="70" rows="10"></textarea>';
  279. echo '</label>';
  280. echo '<br/><br/>';
  281. echo '<button class="save" type="submit" name="SendText2Audio">'.get_lang('BuildMP3').'</button>';
  282. echo '<br/>';
  283. echo '</form>';
  284. echo '</div>';
  285. */
  286. echo '</div>';
  287. Display :: display_footer();
  288. //Functions. TODO:all at one
  289. /**
  290. * This function save a post into a file mp3 from google services
  291. *
  292. * @param $filepath
  293. * @param $dir
  294. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  295. * @version january 2011, chamilo 1.8.8
  296. */
  297. function downloadMP3_google($filepath, $dir)
  298. {
  299. $location='create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&dt2a=google';
  300. //security
  301. if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  302. echo '<script>window.location.href="'.$location.'"</script>';
  303. return;
  304. }
  305. $_course = api_get_course_info();
  306. $_user = api_get_user_info();
  307. $clean_title=trim($_POST['title']);
  308. $clean_text=trim($_POST['text']);
  309. if(empty($clean_title) || empty($clean_text)){
  310. echo '<script>window.location.href="'.$location.'"</script>';
  311. return;
  312. }
  313. $clean_title = Security::remove_XSS($clean_title);
  314. $clean_title = Database::escape_string($clean_title);
  315. $clean_title = str_replace(' ', '_', $clean_title);//compound file names
  316. $clean_text = Security::remove_XSS($clean_text);
  317. $clean_lang = Security::remove_XSS($_POST['lang']);
  318. $extension='mp3';
  319. $audio_filename=$clean_title.'.'.$extension;
  320. $audio_title = str_replace('_',' ',$clean_title);
  321. //prevent duplicates
  322. if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){
  323. $i = 1;
  324. while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) $i++;
  325. $audio_filename = $clean_title . '_' . $i . '.'.$extension;
  326. $audio_title = $clean_title . '_' . $i . '.'.$extension;
  327. $audio_title = str_replace('_',' ',$audio_title);
  328. }
  329. $documentPath = $filepath.'/'.$audio_filename;
  330. /*
  331. //prev for a fine unicode, borrowed from main api TODO:clean
  332. // Safe replacements for some non-letter characters (whitout blank spaces)
  333. $search = array("\0", "\t", "\n", "\r", "\x0B", '/', "\\", '"', "'", '?', '*', '>', '<', '|', ':', '$', '(', ')', '^', '[', ']', '#', '+', '&', '%');
  334. $replace = array('', '_', '_', '_', '_', '-', '-', '-', '_', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-');
  335. $filename=$clean_text;
  336. // Encoding detection.
  337. $encoding = api_detect_encoding($filename);
  338. // Converting html-entities into encoded characters.
  339. $filename = api_html_entity_decode($filename, ENT_QUOTES, $encoding);
  340. // Transliteration to ASCII letters, they are not dangerous for filesystems.
  341. $filename = api_transliterate($filename, 'x', $encoding);
  342. // Replacing remaining dangerous non-letter characters.
  343. $clean_text = str_replace($search, $replace, $filename);*/
  344. $clean_text = api_replace_dangerous_char($clean_text);
  345. // adding the file
  346. // add new file to disk
  347. $proxySettings = api_get_configuration_value('proxy_settings');
  348. $url = "http://translate.google.com/translate_tts?tl=".$clean_lang."&q=".urlencode($clean_text)."";
  349. if (empty($proxySettings)) {
  350. $content = file_get_contents($url);
  351. } else {
  352. $context = stream_context_create($proxySettings);
  353. $content = file_get_contents($url, false, $context);
  354. }
  355. file_put_contents(
  356. $documentPath,
  357. $content
  358. );
  359. // add document to database
  360. $current_session_id = api_get_session_id();
  361. $groupId = api_get_group_id();
  362. $relativeUrlPath=$dir;
  363. $doc_id = add_document(
  364. $_course,
  365. $relativeUrlPath.$audio_filename,
  366. 'file',
  367. filesize($documentPath),
  368. $audio_title
  369. );
  370. api_item_property_update(
  371. $_course,
  372. TOOL_DOCUMENT,
  373. $doc_id,
  374. 'DocumentAdded',
  375. $_user['user_id'],
  376. $groupId,
  377. null,
  378. null,
  379. null,
  380. $current_session_id
  381. );
  382. Display::display_confirmation_message(get_lang('DocumentCreated'));
  383. //return to location
  384. echo '<script>window.location.href="'.$location.'"</script>';
  385. }
  386. /**
  387. * This function save a post into a file mp3 from pediaphon services
  388. *
  389. * @param $filepath
  390. * @param $dir
  391. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  392. * @version january 2011, chamilo 1.8.8
  393. */
  394. function downloadMP3_pediaphon($filepath, $dir)
  395. {
  396. $location='create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&dt2a=pediaphon';
  397. //security
  398. if(!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  399. echo '<script>window.location.href="'.$location.'"</script>';
  400. return;
  401. }
  402. $_course = api_get_course_info();
  403. $_user = api_get_user_info();
  404. $clean_title=trim($_POST['title']);
  405. $clean_title= Database::escape_string($clean_title);
  406. $clean_text=trim($_POST['text']);
  407. $clean_voices=Security::remove_XSS($_POST['voices']);
  408. if(empty($clean_title) || empty($clean_text) || empty($clean_voices)){
  409. echo '<script>window.location.href="'.$location.'"</script>';
  410. return;
  411. }
  412. $clean_title = Security::remove_XSS($clean_title);
  413. $clean_title = Database::escape_string($clean_title);
  414. $clean_title = str_replace(' ', '_', $clean_title);//compound file names
  415. $clean_text = Security::remove_XSS($clean_text);
  416. $clean_lang = Security::remove_XSS($_POST['lang']);
  417. $clean_speed = Security::remove_XSS($_POST['speed']);
  418. $extension='mp3';
  419. $audio_filename=$clean_title.'.'.$extension;
  420. $audio_title = str_replace('_',' ',$clean_title);
  421. //prevent duplicates
  422. if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){
  423. $i = 1;
  424. while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) $i++;
  425. $audio_filename = $clean_title . '_' . $i . '.'.$extension;
  426. $audio_title = $clean_title . '_' . $i . '.'.$extension;
  427. $audio_title = str_replace('_',' ',$audio_title);
  428. }
  429. $documentPath = $filepath.'/'.$audio_filename;
  430. /*//prev for a fine unicode, borrowed from main api TODO:clean
  431. // Safe replacements for some non-letter characters (whitout blank spaces)
  432. $search = array("\0", "\t", "\n", "\r", "\x0B", '/', "\\", '"', "'", '?', '*', '>', '<', '|', ':', '$', '(', ')', '^', '[', ']', '#', '+', '&', '%');
  433. $replace = array('', '_', '_', '_', '_', '-', '-', '-', '_', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-');
  434. $filename=$clean_text;
  435. // Encoding detection.
  436. $encoding = api_detect_encoding($filename);
  437. // Converting html-entities into encoded characters.
  438. $filename = api_html_entity_decode($filename, ENT_QUOTES, $encoding);
  439. // Transliteration to ASCII letters, they are not dangerous for filesystems.
  440. $filename = api_transliterate($filename, 'x', $encoding);
  441. // Replacing remaining dangerous non-letter characters.
  442. $clean_text = str_replace($search, $replace, $filename);*/
  443. $clean_text = api_replace_dangerous_char($clean_text);
  444. //adding the file
  445. if ($clean_lang=='de') {
  446. $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice.cgi';
  447. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/(.*)\.mp3\"/';
  448. } else {
  449. $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice_'.$clean_lang.'.cgi';//en, es, fr
  450. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/'.$clean_lang.'\/(.*)\.mp3\"/';
  451. }
  452. $data="stimme=".$clean_voices."&inputtext=".$clean_text."&speed=".$clean_speed."&go=speak";
  453. $opts = array('http' =>
  454. array(
  455. 'method' => 'POST',
  456. 'header' =>"Content-Type: application/x-www-form-urlencoded\r\n",
  457. "Content-Length: " . strlen($data) . "\r\n",
  458. 'content' => $data
  459. )
  460. );
  461. $context = stream_context_create($opts);
  462. // Download the whole HTML page
  463. $previous_returntext2voice = file_get_contents($url_pediaphon,false,$context);
  464. //extract the audio file path
  465. $search_source = preg_match($find_t2v, $previous_returntext2voice, $hits);
  466. $souce_end = substr($hits[0], 0, -1);
  467. //download file
  468. $returntext2voice = file_get_contents($souce_end);
  469. //save file
  470. $f = @file_put_contents($documentPath, $returntext2voice);
  471. if ($f === false && !empty($php_errormsg)) {
  472. error_log($php_errormsg);
  473. }
  474. //add document to database
  475. $current_session_id = api_get_session_id();
  476. $groupId = api_get_group_id();
  477. $relativeUrlPath=$dir;
  478. $doc_id = add_document($_course, $relativeUrlPath.$audio_filename, 'file', filesize($documentPath), $audio_title);
  479. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  480. Display::display_confirmation_message(get_lang('DocumentCreated'));
  481. //return to location
  482. echo '<script>window.location.href="'.$location.'"</script>';
  483. }