create_audio.php 22 KB

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