create_audio.php 23 KB

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