create_audio.php 23 KB

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