create_audio.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. /**
  13. * Code
  14. */
  15. /* INIT SECTION */
  16. $language_file = array('document');
  17. //require_once '../inc/global.inc.php';
  18. $_SESSION['whereami'] = 'document/createaudio';
  19. $this_section = SECTION_COURSES;
  20. require_once 'document.inc.php';
  21. $nameTools = get_lang('CreateAudio');
  22. api_protect_course_script();
  23. api_block_anonymous_users();
  24. if (api_get_setting('enabled_text2audio') == 'false'){
  25. api_not_allowed(true);
  26. }
  27. $document_data = DocumentManager::get_document_data_by_id($_GET['id'], api_get_course_id());
  28. if (empty($document_data)) {
  29. if (api_is_in_group()) {
  30. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  31. $document_id = DocumentManager::get_document_id(api_get_course_info(), $group_properties['directory']);
  32. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  33. }
  34. }
  35. $document_id = $document_data['id'];
  36. $dir = $document_data['path'];
  37. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  38. // Please, do not modify this dirname formatting
  39. if (strstr($dir, '..')) {
  40. $dir = '/';
  41. }
  42. if ($dir[0] == '.') {
  43. $dir = substr($dir, 1);
  44. }
  45. if ($dir[0] != '/') {
  46. $dir = '/'.$dir;
  47. }
  48. if ($dir[strlen($dir) - 1] != '/') {
  49. $dir .= '/';
  50. }
  51. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  52. if (!is_dir($filepath)) {
  53. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  54. $dir = '/';
  55. }
  56. //groups //TODO: clean
  57. if (isset ($_SESSION['_gid']) && $_SESSION['_gid'] != 0) {
  58. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  59. $interbreadcrumb[] = array ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  60. $noPHP_SELF = true;
  61. $to_group_id = $_SESSION['_gid'];
  62. $group = GroupManager :: get_group_properties($to_group_id);
  63. $path = explode('/', $dir);
  64. if ('/'.$path[1] != $group['directory']) {
  65. api_not_allowed(true);
  66. }
  67. }
  68. $interbreadcrumb[] = array(
  69. "url" => "./document.php?curdirpath=".urlencode($dir).$req_gid,
  70. "name" => get_lang('Documents')
  71. );
  72. if (!$is_allowed_in_course) {
  73. api_not_allowed(true);
  74. }
  75. if (!($is_allowed_to_edit || GroupManager::groupMemberWithUploadRights() || is_my_shared_folder(
  76. api_get_user_id(),
  77. Security::remove_XSS($dir),
  78. api_get_session_id()
  79. ))
  80. ) {
  81. api_not_allowed(true);
  82. }
  83. /* Header */
  84. Event::event_access_tool(TOOL_DOCUMENT);
  85. $display_dir = $dir;
  86. if (isset ($group)) {
  87. $display_dir = explode('/', $dir);
  88. unset ($display_dir[0]);
  89. unset ($display_dir[1]);
  90. $display_dir = implode('/', $display_dir);
  91. }
  92. // Interbreadcrumb for the current directory root path
  93. // Copied from document.php
  94. $dir_array = explode('/', $dir);
  95. $array_len = count($dir_array);
  96. $dir_acum = '';
  97. for ($i = 0; $i < $array_len; $i++) {
  98. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  99. //Max char 80
  100. $url_to_who = Text::cut($dir_array[$i], 80);
  101. if ($is_certificate_mode) {
  102. $interbreadcrumb[] = array('url' => $url_dir.'&selectcat='.Security::remove_XSS($_GET['selectcat']), 'name' => $url_to_who);
  103. } else {
  104. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  105. }
  106. $dir_acum .= $dir_array[$i].'/';
  107. }
  108. Display :: display_header($nameTools, 'Doc');
  109. echo '<div class="actions">';
  110. echo '<a href="document.php?id='.$document_id.'">'.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">'.Display::return_icon('google.png',get_lang('GoogleAudio'),'',ICON_SIZE_MEDIUM).'</a>';
  112. 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>';
  113. echo '</div>';
  114. ?>
  115. <!-- javascript and styles for textareaCounter-->
  116. <script type="text/javascript">
  117. var info;
  118. $(document).ready(function(){
  119. var options = {
  120. 'maxCharacterSize': 100,
  121. 'originalStyle': 'originalTextareaInfo',
  122. 'warningStyle' : 'warningTextareaInfo',
  123. 'warningNumber': 20,
  124. 'displayFormat' : '#input/#max'
  125. };
  126. $('#textarea_google').textareaCount(options, function(data){
  127. $('#textareaCallBack').html(data);
  128. });
  129. });
  130. </script>
  131. <style>
  132. .overview {
  133. background: #FFEC9D;
  134. padding: 10px;
  135. width: 90%;
  136. border: 1px solid #CCCCCC;
  137. }
  138. .originalTextareaInfo {
  139. font-size: 12px;
  140. color: #000000;
  141. text-align: right;
  142. }
  143. .warningTextareaInfo {
  144. color: #FF0000;
  145. font-weight:bold;
  146. text-align: right;
  147. }
  148. #showData {
  149. height: 70px;
  150. width: 200px;
  151. border: 1px solid #CCCCCC;
  152. padding: 10px;
  153. margin: 10px;
  154. }
  155. </style>
  156. <div id="textareaCallBack"></div>
  157. <?php
  158. if (Security::remove_XSS($_POST['text2voice_mode'])=='google') {
  159. downloadMP3_google($filepath, $dir);
  160. } elseif (Security::remove_XSS($_POST['text2voice_mode']) == 'pediaphon') {
  161. downloadMP3_pediaphon($filepath, $dir);
  162. }
  163. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  164. $sql_select = "SELECT * FROM $tbl_admin_languages";
  165. $result_select = Database::query($sql_select);
  166. $options = $options_pedia = array();
  167. $selected_language = null;
  168. $options_pedia['defaultmessage'] = get_lang('FirstSelectALanguage'); //need read before platform languages
  169. while ($row = Database::fetch_array($result_select)) {
  170. if (api_get_setting('platformLanguage')==$row['english_name']) {
  171. //$selected_language = $row['isocode'];//lang default is the default platform language
  172. }
  173. $options[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  174. if (in_array($row['isocode'], array('de', 'en', 'es', 'fr'))){
  175. $options_pedia[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  176. }
  177. }
  178. $icon = Display::return_icon('text2audio.png', get_lang('HelpText2Audio'),'',ICON_SIZE_MEDIUM);
  179. echo '<div class="page-header"><h2>'.$icon.get_lang('HelpText2Audio').'</h2></div>';
  180. if(Security::remove_XSS($_GET['dt2a'])=='google'){
  181. $selected_language = api_get_language_isocode();//lang default is the course language
  182. echo '<div>';
  183. $form = new FormValidator('form1', 'post', null, '', array('id' => 'form1'));
  184. $form->addElement('hidden', 'text2voice_mode', 'google');
  185. $form->addElement('hidden', 'document_id', $document_id);
  186. $form->addElement('text', 'title', get_lang('Title'));
  187. $form->addElement('select', 'lang', get_lang('Language'), $options);
  188. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google', 'class' =>'span6' ));
  189. //echo Display :: return_icon('info3.gif', get_lang('HelpGoogleAudio'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  190. $form->addElement('style_submit_button', 'submit', get_lang('SaveMP3'), 'class="save"');
  191. $defaults = array();
  192. $defaults['lang'] = $selected_language;
  193. $form->setDefaults($defaults);
  194. $form->display();
  195. echo '</div>';
  196. }
  197. if(Security::remove_XSS($_GET['dt2a'])=='pediaphon'){
  198. //lang default is a default message
  199. $selected_language = "defaultmessage";
  200. $options['defaultmessage'] =get_lang('FirstSelectALanguage');
  201. echo '<div>';
  202. $form = new FormValidator('form2', 'post', null, '', array('id' => 'form2'));
  203. $form->addElement('hidden', 'text2voice_mode','pediaphon');
  204. $form->addElement('hidden', 'document_id', $document_id);
  205. $form->addElement('text', 'title', get_lang('Title'));
  206. $form->addElement('select', 'lang', get_lang('Language'), $options_pedia, array('onclick' => 'update_voices(this.selectedIndex);'));
  207. $form->addElement('select', 'voices', get_lang('Voice'), array(get_lang('FirstSelectALanguage')), array());
  208. $speed_options = array();
  209. $speed_options['1'] = get_lang('Normal');
  210. $speed_options['0.75'] = get_lang('GoFaster');
  211. $speed_options['0.8'] = get_lang('Fast');
  212. $speed_options['1.2'] = get_lang('Slow');
  213. $speed_options['1.6'] = get_lang('SlowDown');
  214. $form->addElement('select', 'speed', get_lang('Speed'), $speed_options, array());
  215. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_pediaphon', 'class' =>'span6'));
  216. //echo Display :: return_icon('info3.gif', get_lang('HelpPediaphon'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  217. $form->addElement('style_submit_button', 'submit', get_lang('SaveMP3'), 'class="save"');
  218. $defaults = array();
  219. $defaults['lang'] = $selected_language;
  220. $form->setDefaults($defaults);
  221. $form->display();
  222. echo '</div>';
  223. ?>
  224. <!-- javascript form name form2 update voices -->
  225. <script>
  226. var langslist=document.form2.lang
  227. var voiceslist=document.form2.voices
  228. var voices = new Array();
  229. voices[0] = ["<?php echo get_lang('FirstSelectALanguage'); ?>"]
  230. // German
  231. voices[1] = ["<?php echo get_lang('Female').' (de1)'; ?>|de1", "<?php echo get_lang(
  232. 'Male'
  233. ).' (de2)'; ?>|de2", "<?php echo get_lang('Female').' (de3)'; ?>|de3", "<?php echo get_lang(
  234. 'Male'
  235. ).' (de4)'; ?>|de4", "<?php echo get_lang('Female').' (de5)'; ?>|de5", "<?php echo get_lang(
  236. 'Male'
  237. ).' (de6)'; ?>|de6", "<?php echo get_lang('Female').' (de7)'; ?>|de7", "<?php echo get_lang(
  238. 'Female'
  239. ).' (de8 HQ)'; ?>|de8"]
  240. // English
  241. voices[2] = ["<?php echo get_lang('Male').' (en1)'; ?>|en1", "<?php echo get_lang(
  242. 'Male'
  243. ).' (en2 HQ)'; ?>|en2", "<?php echo get_lang('Female').' (us1)'; ?>| us1", "<?php echo get_lang(
  244. 'Male'
  245. ).' (us2)'; ?>|us2", "<?php echo get_lang('Male').' (us3)'; ?>|us3", "<?php echo get_lang(
  246. 'Female'
  247. ).'(us4 HQ)'; ?>|us4"]
  248. //Spanish
  249. voices[3]=["<?php echo get_lang('Male').' (es5 HQ)'; ?>|es5"]
  250. //French
  251. voices[4]=["<?php echo get_lang('Female').' (fr8 HQ)'; ?>|fr8"]
  252. function update_voices(selectedvoicegroup){
  253. voiceslist.options.length=0
  254. for (i=0; i<voices[selectedvoicegroup].length; i++)
  255. voiceslist.options[voiceslist.options.length]=new Option(voices[selectedvoicegroup][i].split("|")[0], voices[selectedvoicegroup][i].split("|")[1])
  256. }
  257. </script>
  258. <?php
  259. }
  260. //end pediaphon
  261. //vozMe services
  262. //disabled for a time
  263. /*
  264. 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').'';
  265. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  266. echo '<div id="option3" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  267. echo '<form id="form3" name="form3" method="post" action="http://vozme.com/text2voice.php" target="mymp3" class="formw">';
  268. echo '<br/>';
  269. echo '<label>'.get_lang('Language').': ';
  270. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  271. $sql_select = "SELECT * FROM $tbl_admin_languages";
  272. $result_select = Database::query($sql_select);
  273. echo '<select name="lang" id="select">';
  274. while ($row = Database::fetch_array($result_select)) {
  275. if (in_array($row['isocode'], array('ca', 'en', 'es', 'hi', 'it', 'pt'))){
  276. if (api_get_setting('platformLanguage')==$row['english_name']){
  277. echo '<option value="'.$row['isocode'].'" selected="selected">'.$row['original_name'].' ('.$row['english_name'].')</option>';
  278. } else {
  279. echo '<option value="'.$row['isocode'].'">'.$row['original_name'].' ('.$row['english_name'].')</option>';
  280. }
  281. }
  282. }
  283. echo '</select>';
  284. echo '</label>';
  285. echo '<label>&nbsp;&nbsp;'.get_lang('Voice').': ';
  286. echo '<select name="gn" id="select1">';
  287. echo '<option value="ml">'.get_lang('Male').'</option>';
  288. echo '<option value="fm">'.get_lang('Female').'</option>';
  289. echo '</select>';
  290. echo '</label>';
  291. echo '<br/><br/>';
  292. echo '<div>'.get_lang('InsertText2Audio').'</div>';
  293. echo '<br/>';
  294. echo '<label>';
  295. echo '<textarea name="text" id="textarea" cols="70" rows="10"></textarea>';
  296. echo '</label>';
  297. echo '<br/><br/>';
  298. echo '<button class="save" type="submit" name="SendText2Audio">'.get_lang('BuildMP3').'</button>';
  299. echo '<br/>';
  300. echo '</form>';
  301. echo '</div>';
  302. */
  303. echo '</div>';
  304. Display :: display_footer();
  305. //Functions. TODO:all at one
  306. /**
  307. * This function save a post into a file mp3 from google services
  308. *
  309. * @param $filepath
  310. * @param $dir
  311. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  312. * @version january 2011, chamilo 1.8.8
  313. */
  314. function downloadMP3_google($filepath, $dir)
  315. {
  316. $location='create_audio.php?'.api_get_cidreq().'&id='.Security::remove_XSS($_POST['document_id']).'&dt2a=google';
  317. //security
  318. if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  319. echo '<script>window.location.href="'.$location.'"</script>';
  320. return;
  321. }
  322. global $_user;
  323. $_course = api_get_course_info();
  324. $clean_title=trim($_POST['title']);
  325. $clean_text=trim($_POST['text']);
  326. if(empty($clean_title) || empty($clean_text)){
  327. echo '<script>window.location.href="'.$location.'"</script>';
  328. return;
  329. }
  330. $clean_title=Security::remove_XSS($clean_title);
  331. $clean_title=Database::escape_string($clean_title);
  332. $clean_title=str_replace(' ','_', $clean_title);//compound file names
  333. $clean_text=Security::remove_XSS($clean_text);
  334. $clean_lang=Security::remove_XSS($_POST['lang']);
  335. $extension='mp3';
  336. $audio_filename=$clean_title.'.'.$extension;
  337. $audio_title = str_replace('_',' ',$clean_title);
  338. //prevent duplicates
  339. if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){
  340. $i = 1;
  341. while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) {
  342. $i++;
  343. }
  344. $audio_filename = $clean_title . '_' . $i . '.'.$extension;
  345. $audio_title = $clean_title . '_' . $i . '.'.$extension;
  346. $audio_title = str_replace('_',' ',$audio_title);
  347. }
  348. $documentPath = $filepath.'/'.$audio_filename;
  349. //prev for a fine unicode, borrowed from main api TODO:clean
  350. // Safe replacements for some non-letter characters (whitout blank spaces)
  351. $search = array(
  352. "\0",
  353. "\t",
  354. "\n",
  355. "\r",
  356. "\x0B",
  357. '/',
  358. "\\",
  359. '"',
  360. "'",
  361. '?',
  362. '*',
  363. '>',
  364. '<',
  365. '|',
  366. ':',
  367. '$',
  368. '(',
  369. ')',
  370. '^',
  371. '[',
  372. ']',
  373. '#',
  374. '+',
  375. '&',
  376. '%'
  377. );
  378. $replace = array(
  379. '',
  380. '_',
  381. '_',
  382. '_',
  383. '_',
  384. '-',
  385. '-',
  386. '-',
  387. '_',
  388. '-',
  389. '-',
  390. '-',
  391. '-',
  392. '-',
  393. '-',
  394. '-',
  395. '-',
  396. '-',
  397. '-',
  398. '-',
  399. '-',
  400. '-',
  401. '-',
  402. '-',
  403. '-'
  404. );
  405. $filename=$clean_text;
  406. // Encoding detection.
  407. $encoding = api_detect_encoding($filename);
  408. // Converting html-entities into encoded characters.
  409. $filename = api_html_entity_decode($filename, ENT_QUOTES, $encoding);
  410. // Transliteration to ASCII letters, they are not dangerous for filesystems.
  411. $filename = api_transliterate($filename, 'x', $encoding);
  412. // Replacing remaining dangerous non-letter characters.
  413. $clean_text = str_replace($search, $replace, $filename);
  414. $returntext2voice = file_get_contents(
  415. "http://translate.google.com/translate_tts?tl=".$clean_lang."&q=".urlencode($clean_text).""
  416. );
  417. //make a temporal file for get the file size
  418. $tmpfname = tempnam("/tmp", "CTF");
  419. $handle = fopen($tmpfname, "w");
  420. fwrite($handle, $returntext2voice);
  421. fclose($handle);
  422. // Check if there is enough space in the course to save the file
  423. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  424. unlink($tmpfname);
  425. die(get_lang('UplNotEnoughSpace'));
  426. }
  427. //erase temporal file
  428. unlink($tmpfname);
  429. //adding the file
  430. //add new file to disk
  431. file_put_contents($documentPath, $returntext2voice);
  432. //add document to database
  433. $current_session_id = api_get_session_id();
  434. $groupId=$_SESSION['_gid'];
  435. $file_size = filesize($documentPath);
  436. $relativeUrlPath=$dir;
  437. $doc_id = FileManager::add_document(
  438. $_course,
  439. $relativeUrlPath.$audio_filename,
  440. 'file',
  441. filesize($documentPath),
  442. $audio_title
  443. );
  444. api_item_property_update(
  445. $_course,
  446. TOOL_DOCUMENT,
  447. $doc_id,
  448. 'DocumentAdded',
  449. $_user['user_id'],
  450. $groupId,
  451. null,
  452. null,
  453. null,
  454. $current_session_id
  455. );
  456. Display::display_confirmation_message(get_lang('DocumentCreated'));
  457. //return to location
  458. echo '<script>window.location.href="'.$location.'"</script>';
  459. }
  460. /**
  461. * This function save a post into a file mp3 from pediaphon services
  462. *
  463. * @param $filepath
  464. * @param $dir
  465. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  466. * @version january 2011, chamilo 1.8.8
  467. */
  468. function downloadMP3_pediaphon($filepath, $dir)
  469. {
  470. $location = 'create_audio.php?'.api_get_cidreq().'&id='.Security::remove_XSS(
  471. $_POST['document_id']
  472. ).'&dt2a=pediaphon';
  473. //security
  474. if(!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  475. echo '<script>window.location.href="'.$location.'"</script>';
  476. return;
  477. }
  478. global $_user;
  479. $_course = api_get_course_info();
  480. $clean_title=trim($_POST['title']);
  481. $clean_title= Database::escape_string($clean_title);
  482. $clean_text=trim($_POST['text']);
  483. $clean_voices=Security::remove_XSS($_POST['voices']);
  484. if(empty($clean_title) || empty($clean_text) || empty($clean_voices)){
  485. echo '<script>window.location.href="'.$location.'"</script>';
  486. return;
  487. }
  488. $clean_title=Security::remove_XSS($clean_title);
  489. $clean_title=Database::escape_string($clean_title);
  490. $clean_title=str_replace(' ','_', $clean_title);//compound file names
  491. $clean_text=Security::remove_XSS($clean_text);
  492. $clean_lang=Security::remove_XSS($_POST['lang']);
  493. $clean_speed=Security::remove_XSS($_POST['speed']);
  494. $extension='mp3';
  495. $audio_filename=$clean_title.'.'.$extension;
  496. $audio_title = str_replace('_',' ',$clean_title);
  497. //prevent duplicates
  498. if (file_exists($filepath.'/'.$clean_title.'.'.$extension)){
  499. $i = 1;
  500. while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) {
  501. $i++;
  502. }
  503. $audio_filename = $clean_title . '_' . $i . '.'.$extension;
  504. $audio_title = $clean_title . '_' . $i . '.'.$extension;
  505. $audio_title = str_replace('_',' ',$audio_title);
  506. }
  507. $documentPath = $filepath.'/'.$audio_filename;
  508. //prev for a fine unicode, borrowed from main api TODO:clean
  509. // Safe replacements for some non-letter characters (whitout blank spaces)
  510. $search = array(
  511. "\0",
  512. "\t",
  513. "\n",
  514. "\r",
  515. "\x0B",
  516. '/',
  517. "\\",
  518. '"',
  519. "'",
  520. '?',
  521. '*',
  522. '>',
  523. '<',
  524. '|',
  525. ':',
  526. '$',
  527. '(',
  528. ')',
  529. '^',
  530. '[',
  531. ']',
  532. '#',
  533. '+',
  534. '&',
  535. '%'
  536. );
  537. $replace = array(
  538. '',
  539. '_',
  540. '_',
  541. '_',
  542. '_',
  543. '-',
  544. '-',
  545. '-',
  546. '_',
  547. '-',
  548. '-',
  549. '-',
  550. '-',
  551. '-',
  552. '-',
  553. '-',
  554. '-',
  555. '-',
  556. '-',
  557. '-',
  558. '-',
  559. '-',
  560. '-',
  561. '-',
  562. '-'
  563. );
  564. $filename=$clean_text;
  565. // Encoding detection.
  566. $encoding = api_detect_encoding($filename);
  567. // Converting html-entities into encoded characters.
  568. $filename = api_html_entity_decode($filename, ENT_QUOTES, $encoding);
  569. // Transliteration to ASCII letters, they are not dangerous for filesystems.
  570. $filename = api_transliterate($filename, 'x', $encoding);
  571. // Replacing remaining dangerous non-letter characters.
  572. $clean_text = str_replace($search, $replace, $filename);
  573. //adding the file
  574. if ($clean_lang=='de') {
  575. $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice.cgi';
  576. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/(.*)\.mp3\"/';
  577. } else {
  578. $url_pediaphon='http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice_'.$clean_lang.'.cgi';//en, es, fr
  579. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/'.$clean_lang.'\/(.*)\.mp3\"/';
  580. }
  581. $data="stimme=".$clean_voices."&inputtext=".$clean_text."&speed=".$clean_speed."&go=go";
  582. $opts = array(
  583. 'http' =>
  584. array(
  585. 'method' => 'POST',
  586. 'header' =>"Content-Type: application/x-www-form-urlencoded\r\n",
  587. "Content-Length: " . strlen($data) . "\r\n",
  588. 'content' => $data
  589. )
  590. );
  591. $context = stream_context_create($opts);
  592. $previous_returntext2voice = file_get_contents($url_pediaphon,false,$context);
  593. //clean file contents
  594. $search_source=preg_match($find_t2v, $previous_returntext2voice, $hits);
  595. $souce_end=substr($hits[0], 0,-1);
  596. $returntext2voice = file_get_contents($souce_end);
  597. //make a temporal file for get the file size
  598. $tmpfname = tempnam("/tmp", "CTF");
  599. $handle = fopen($tmpfname, "w");
  600. fwrite($handle, $returntext2voice);
  601. fclose($handle);
  602. // Check if there is enough space in the course to save the file
  603. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  604. unlink($tmpfname);
  605. die(get_lang('UplNotEnoughSpace'));
  606. }
  607. //erase temporal file
  608. unlink($tmpfname);
  609. //save file
  610. file_put_contents($documentPath, $returntext2voice);
  611. //add document to database
  612. $current_session_id = api_get_session_id();
  613. $groupId=$_SESSION['_gid'];
  614. $file_size = filesize($documentPath);
  615. $relativeUrlPath=$dir;
  616. $doc_id = FileManager::add_document(
  617. $_course,
  618. $relativeUrlPath.$audio_filename,
  619. 'file',
  620. filesize($documentPath),
  621. $audio_title
  622. );
  623. api_item_property_update(
  624. $_course,
  625. TOOL_DOCUMENT,
  626. $doc_id,
  627. 'DocumentAdded',
  628. $_user['user_id'],
  629. $groupId,
  630. null,
  631. null,
  632. null,
  633. $current_session_id
  634. );
  635. Display::display_confirmation_message(get_lang('DocumentCreated'));
  636. //return to location
  637. echo '<script>window.location.href="'.$location.'"</script>';
  638. }