create_audio.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 ("url" => "../group/group_space.php?gidReq=".$_SESSION['_gid'], "name" => get_lang('GroupSpace'));
  62. $noPHP_SELF = true;
  63. $to_group_id = $_SESSION['_gid'];
  64. $group = GroupManager :: get_group_properties($to_group_id);
  65. $path = explode('/', $dir);
  66. if ('/'.$path[1] != $group['directory']) {
  67. api_not_allowed(true);
  68. }
  69. }
  70. $interbreadcrumb[] = array(
  71. "url" => "./document.php?curdirpath=".urlencode($dir).$req_gid,
  72. "name" => get_lang('Documents')
  73. );
  74. if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'] || is_my_shared_folder(
  75. api_get_user_id(),
  76. Security::remove_XSS($dir),
  77. api_get_session_id()
  78. ))
  79. ) {
  80. api_not_allowed(true);
  81. }
  82. /* Header */
  83. event_access_tool(TOOL_DOCUMENT);
  84. $display_dir = $dir;
  85. if (isset ($group)) {
  86. $display_dir = explode('/', $dir);
  87. unset ($display_dir[0]);
  88. unset ($display_dir[1]);
  89. $display_dir = implode('/', $display_dir);
  90. }
  91. // Interbreadcrumb for the current directory root path
  92. // Copied from document.php
  93. $dir_array = explode('/', $dir);
  94. $array_len = count($dir_array);
  95. $dir_acum = '';
  96. for ($i = 0; $i < $array_len; $i++) {
  97. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  98. //Max char 80
  99. $url_to_who = Text::cut($dir_array[$i], 80);
  100. if ($is_certificate_mode) {
  101. $interbreadcrumb[] = array('url' => $url_dir.'&selectcat='.Security::remove_XSS($_GET['selectcat']), 'name' => $url_to_who);
  102. } else {
  103. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  104. }
  105. $dir_acum .= $dir_array[$i].'/';
  106. }
  107. Display :: display_header($nameTools, 'Doc');
  108. echo '<div class="actions">';
  109. echo '<a href="document.php?id='.$document_id.'">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  110. 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>';
  111. 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>';
  112. echo '</div>';
  113. ?>
  114. <!-- javascript and styles for textareaCounter-->
  115. <script type="text/javascript">
  116. var info;
  117. $(document).ready(function(){
  118. var options = {
  119. 'maxCharacterSize': 100,
  120. 'originalStyle': 'originalTextareaInfo',
  121. 'warningStyle' : 'warningTextareaInfo',
  122. 'warningNumber': 20,
  123. 'displayFormat' : '#input/#max'
  124. };
  125. $('#textarea_google').textareaCount(options, function(data){
  126. $('#textareaCallBack').html(data);
  127. });
  128. });
  129. </script>
  130. <style>
  131. .overview {
  132. background: #FFEC9D;
  133. padding: 10px;
  134. width: 90%;
  135. border: 1px solid #CCCCCC;
  136. }
  137. .originalTextareaInfo {
  138. font-size: 12px;
  139. color: #000000;
  140. text-align: right;
  141. }
  142. .warningTextareaInfo {
  143. color: #FF0000;
  144. font-weight:bold;
  145. text-align: right;
  146. }
  147. #showData {
  148. height: 70px;
  149. width: 200px;
  150. border: 1px solid #CCCCCC;
  151. padding: 10px;
  152. margin: 10px;
  153. }
  154. </style>
  155. <div id="textareaCallBack"></div>
  156. <?php
  157. if(Security::remove_XSS($_POST['text2voice_mode'])=='google'){
  158. downloadMP3_google($filepath, $dir);
  159. } elseif (Security::remove_XSS($_POST['text2voice_mode']) == 'pediaphon') {
  160. downloadMP3_pediaphon($filepath, $dir);
  161. }
  162. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  163. $sql_select = "SELECT * FROM $tbl_admin_languages";
  164. $result_select = Database::query($sql_select);
  165. $options = $options_pedia = array();
  166. $selected_language = null;
  167. $options_pedia['defaultmessage'] = get_lang('FirstSelectALanguage'); //need read before platform languages
  168. while ($row = Database::fetch_array($result_select)) {
  169. if (api_get_setting('platformLanguage')==$row['english_name']) {
  170. //$selected_language = $row['isocode'];//lang default is the default platform language
  171. }
  172. $options[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  173. if (in_array($row['isocode'], array('de', 'en', 'es', 'fr'))){
  174. $options_pedia[$row['isocode']] =$row['original_name'].' ('.$row['english_name'].')';
  175. }
  176. }
  177. $icon = Display::return_icon('text2audio.png', get_lang('HelpText2Audio'),'',ICON_SIZE_MEDIUM);
  178. echo '<div class="page-header"><h2>'.$icon.get_lang('HelpText2Audio').'</h2></div>';
  179. if(Security::remove_XSS($_GET['dt2a'])=='google'){
  180. $selected_language = api_get_language_isocode();//lang default is the course language
  181. echo '<div>';
  182. $form = new FormValidator('form1', 'post', null, '', array('id' => 'form1'));
  183. $form->addElement('hidden', 'text2voice_mode', 'google');
  184. $form->addElement('hidden', 'document_id', $document_id);
  185. $form->addElement('text', 'title', get_lang('Title'));
  186. $form->addElement('select', 'lang', get_lang('Language'), $options);
  187. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google', 'class' =>'span6' ));
  188. //echo Display :: return_icon('info3.gif', get_lang('HelpGoogleAudio'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  189. $form->addElement('style_submit_button', 'submit', get_lang('SaveMP3'), 'class="save"');
  190. $defaults = array();
  191. $defaults['lang'] = $selected_language;
  192. $form->setDefaults($defaults);
  193. $form->display();
  194. echo '</div>';
  195. }
  196. if(Security::remove_XSS($_GET['dt2a'])=='pediaphon'){
  197. //lang default is a default message
  198. $selected_language = "defaultmessage";
  199. $options['defaultmessage'] =get_lang('FirstSelectALanguage');
  200. echo '<div>';
  201. $form = new FormValidator('form2', 'post', null, '', array('id' => 'form2'));
  202. $form->addElement('hidden', 'text2voice_mode','pediaphon');
  203. $form->addElement('hidden', 'document_id', $document_id);
  204. $form->addElement('text', 'title', get_lang('Title'));
  205. $form->addElement('select', 'lang', get_lang('Language'), $options_pedia, array('onclick' => 'update_voices(this.selectedIndex);'));
  206. $form->addElement('select', 'voices', get_lang('Voice'), array(get_lang('FirstSelectALanguage')), array());
  207. $speed_options = array();
  208. $speed_options['1'] = get_lang('Normal');
  209. $speed_options['0.75'] = get_lang('GoFaster');
  210. $speed_options['0.8'] = get_lang('Fast');
  211. $speed_options['1.2'] = get_lang('Slow');
  212. $speed_options['1.6'] = get_lang('SlowDown');
  213. $form->addElement('select', 'speed', get_lang('Speed'), $speed_options, array());
  214. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_pediaphon', 'class' =>'span6'));
  215. //echo Display :: return_icon('info3.gif', get_lang('HelpPediaphon'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  216. $form->addElement('style_submit_button', 'submit', get_lang('SaveMP3'), 'class="save"');
  217. $defaults = array();
  218. $defaults['lang'] = $selected_language;
  219. $form->setDefaults($defaults);
  220. $form->display();
  221. echo '</div>';
  222. ?>
  223. <!-- javascript form name form2 update voices -->
  224. <script>
  225. var langslist=document.form2.lang
  226. var voiceslist=document.form2.voices
  227. var voices = new Array();
  228. voices[0] = ["<?php echo get_lang('FirstSelectALanguage'); ?>"]
  229. // German
  230. voices[1] = ["<?php echo get_lang('Female').' (de1)'; ?>|de1", "<?php echo get_lang(
  231. 'Male'
  232. ).' (de2)'; ?>|de2", "<?php echo get_lang('Female').' (de3)'; ?>|de3", "<?php echo get_lang(
  233. 'Male'
  234. ).' (de4)'; ?>|de4", "<?php echo get_lang('Female').' (de5)'; ?>|de5", "<?php echo get_lang(
  235. 'Male'
  236. ).' (de6)'; ?>|de6", "<?php echo get_lang('Female').' (de7)'; ?>|de7", "<?php echo get_lang(
  237. 'Female'
  238. ).' (de8 HQ)'; ?>|de8"]
  239. // English
  240. voices[2] = ["<?php echo get_lang('Male').' (en1)'; ?>|en1", "<?php echo get_lang(
  241. 'Male'
  242. ).' (en2 HQ)'; ?>|en2", "<?php echo get_lang('Female').' (us1)'; ?>| us1", "<?php echo get_lang(
  243. 'Male'
  244. ).' (us2)'; ?>|us2", "<?php echo get_lang('Male').' (us3)'; ?>|us3", "<?php echo get_lang(
  245. 'Female'
  246. ).'(us4 HQ)'; ?>|us4"]
  247. //Spanish
  248. voices[3]=["<?php echo get_lang('Male').' (es5 HQ)'; ?>|es5"]
  249. //French
  250. voices[4]=["<?php echo get_lang('Female').' (fr8 HQ)'; ?>|fr8"]
  251. function update_voices(selectedvoicegroup){
  252. voiceslist.options.length=0
  253. for (i=0; i<voices[selectedvoicegroup].length; i++)
  254. voiceslist.options[voiceslist.options.length]=new Option(voices[selectedvoicegroup][i].split("|")[0], voices[selectedvoicegroup][i].split("|")[1])
  255. }
  256. </script>
  257. <?php
  258. }
  259. //end pediaphon
  260. //vozMe services
  261. //disabled for a time
  262. /*
  263. 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').'';
  264. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  265. echo '<div id="option3" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  266. echo '<form id="form3" name="form3" method="post" action="http://vozme.com/text2voice.php" target="mymp3" class="formw">';
  267. echo '<br/>';
  268. echo '<label>'.get_lang('Language').': ';
  269. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  270. $sql_select = "SELECT * FROM $tbl_admin_languages";
  271. $result_select = Database::query($sql_select);
  272. echo '<select name="lang" id="select">';
  273. while ($row = Database::fetch_array($result_select)) {
  274. if (in_array($row['isocode'], array('ca', 'en', 'es', 'hi', 'it', 'pt'))){
  275. if (api_get_setting('platformLanguage')==$row['english_name']){
  276. echo '<option value="'.$row['isocode'].'" selected="selected">'.$row['original_name'].' ('.$row['english_name'].')</option>';
  277. }
  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. }