create_audio.php 20 KB

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