create_audio.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This file allows creating audio files from a text.
  6. *
  7. * @package chamilo.document
  8. *
  9. * @author Juan Carlos Raña Trabado
  10. * @since 8/January/2011
  11. * TODO:clean all file
  12. */
  13. //require_once '../inc/global.inc.php';
  14. Session::write('whereami', 'document/createaudio');
  15. $this_section = SECTION_COURSES;
  16. $nameTools = get_lang('CreateAudio');
  17. api_protect_course_script();
  18. api_block_anonymous_users();
  19. $groupRights = Session::read('group_member_with_upload_rights');
  20. $groupId = api_get_group_id();
  21. if (api_get_setting('enabled_text2audio') === 'false') {
  22. api_not_allowed(true);
  23. }
  24. $document_data = DocumentManager::get_document_data_by_id(
  25. $_REQUEST['id'],
  26. api_get_course_id()
  27. );
  28. if (empty($document_data)) {
  29. if (api_is_in_group()) {
  30. $group_properties = GroupManager::get_group_properties(
  31. $groupId
  32. );
  33. $document_id = DocumentManager::get_document_id(
  34. api_get_course_info(),
  35. $group_properties['directory']
  36. );
  37. $document_data = DocumentManager::get_document_data_by_id(
  38. $document_id,
  39. api_get_course_id()
  40. );
  41. }
  42. }
  43. $document_id = $document_data['id'];
  44. $dir = $document_data['path'];
  45. //jquery textareaCounter
  46. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/textareacounter/jquery.textareaCounter.plugin.js" type="text/javascript"></script>';
  47. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  48. // Please, do not modify this dirname formatting
  49. if (strstr($dir, '..')) {
  50. $dir = '/';
  51. }
  52. if ($dir[0] == '.') {
  53. $dir = substr($dir, 1);
  54. }
  55. if ($dir[0] != '/') {
  56. $dir = '/' . $dir;
  57. }
  58. if ($dir[strlen($dir) - 1] != '/') {
  59. $dir .= '/';
  60. }
  61. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  62. if (!is_dir($filepath)) {
  63. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  64. $dir = '/';
  65. }
  66. //groups //TODO: clean
  67. if (!empty($groupId)) {
  68. $interbreadcrumb[] = array(
  69. "url" => "../group/group_space.php?" . api_get_cidreq(),
  70. "name" => get_lang('GroupSpace')
  71. );
  72. $group = GroupManager:: get_group_properties($groupId);
  73. $path = explode('/', $dir);
  74. if ('/' . $path[1] != $group['directory']) {
  75. api_not_allowed(true);
  76. }
  77. }
  78. $interbreadcrumb[] = array(
  79. "url" => "./document.php?curdirpath=" . urlencode($dir) . "&" . api_get_cidreq(),
  80. "name" => get_lang('Documents')
  81. );
  82. if (!$is_allowed_in_course) {
  83. api_not_allowed(true);
  84. }
  85. if (!($is_allowed_to_edit || $groupRights ||
  86. DocumentManager::is_my_shared_folder(
  87. api_get_user_id(),
  88. Security::remove_XSS($dir),
  89. api_get_session_id()
  90. ))
  91. ) {
  92. api_not_allowed(true);
  93. }
  94. /* Header */
  95. Event::event_access_tool(TOOL_DOCUMENT);
  96. $display_dir = $dir;
  97. if (isset ($group)) {
  98. $display_dir = explode('/', $dir);
  99. unset ($display_dir[0]);
  100. unset ($display_dir[1]);
  101. $display_dir = implode('/', $display_dir);
  102. }
  103. // Interbreadcrumb for the current directory root path
  104. // Copied from document.php
  105. $dir_array = explode('/', $dir);
  106. $array_len = count($dir_array);
  107. $dir_acum = '';
  108. for ($i = 0; $i < $array_len; $i++) {
  109. $url_dir = 'document.php?&curdirpath=' . $dir_acum . $dir_array[$i];
  110. //Max char 80
  111. $url_to_who = cut($dir_array[$i], 80);
  112. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  113. $dir_acum .= $dir_array[$i] . '/';
  114. }
  115. $service = isset($_GET['service']) ? $_GET['service'] : 'pediaphon';
  116. Display:: display_header($nameTools, 'Doc');
  117. echo '<div class="actions">';
  118. echo '<a href="document.php?id=' . $document_id . '">' .
  119. Display::return_icon('back.png', get_lang('BackTo') . ' ' . get_lang('DocumentsOverview'), '',
  120. ICON_SIZE_MEDIUM) . '</a>';
  121. echo '<a href="create_audio.php?' . api_get_cidreq() . '&id=' . $document_id . '&service=pediaphon">' .
  122. Display::return_icon('pediaphon.png', get_lang('Pediaphon'), '', ICON_SIZE_MEDIUM) . '</a>';
  123. echo '<a href="create_audio.php?' . api_get_cidreq() . '&id=' . $document_id . '&service=google">' .
  124. Display::return_icon('google.png', get_lang('GoogleAudio'), '', ICON_SIZE_MEDIUM) . '</a>';
  125. echo '</div>';
  126. ?>
  127. <!-- javascript and styles for textareaCounter-->
  128. <script>
  129. var info;
  130. $(document).ready(function () {
  131. var options = {
  132. 'maxCharacterSize': 100,
  133. 'originalStyle': 'originalTextareaInfo',
  134. 'warningStyle': 'warningTextareaInfo',
  135. 'warningNumber': 20,
  136. 'displayFormat': '#input/#max'
  137. };
  138. $('#textarea_google').textareaCount(options, function(data){
  139. $('#textareaCallBack').html(data);
  140. });
  141. });
  142. </script>
  143. <style>
  144. .overview {
  145. background: #FFEC9D;
  146. padding: 10px;
  147. width: 90%;
  148. border: 1px solid #CCCCCC;
  149. }
  150. .originalTextareaInfo {
  151. font-size: 12px;
  152. color: #000000;
  153. text-align: right;
  154. }
  155. .warningTextareaInfo {
  156. color: #FF0000;
  157. font-weight: bold;
  158. text-align: right;
  159. }
  160. #showData {
  161. height: 70px;
  162. width: 200px;
  163. border: 1px solid #CCCCCC;
  164. padding: 10px;
  165. margin: 10px;
  166. }
  167. </style>
  168. <div id="textareaCallBack"></div>
  169. <?php
  170. if (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'google') {
  171. downloadMP3_google($filepath, $dir);
  172. } elseif (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'pediaphon') {
  173. downloadMP3_pediaphon($filepath, $dir);
  174. }
  175. $tbl_admin_languages = Database:: get_main_table(TABLE_MAIN_LANGUAGE);
  176. $sql_select = "SELECT * FROM $tbl_admin_languages";
  177. $result_select = Database::query($sql_select);
  178. $options = $options_pedia = array();
  179. $selected_language = null;
  180. while ($row = Database::fetch_array($result_select)) {
  181. $options[$row['isocode']] = $row['original_name'] . ' (' . $row['english_name'] . ')';
  182. if (in_array($row['isocode'], array('de', 'en', 'es', 'fr'))) {
  183. $options_pedia[$row['isocode']] = $row['original_name'] . ' (' . $row['english_name'] . ')';
  184. }
  185. }
  186. if ($service == 'google') {
  187. $selected_language = api_get_language_isocode();//lang default is the course language
  188. $form = new FormValidator('form1', 'post', api_get_self().'?'.api_get_cidreq(), '', array('id' => 'form1'));
  189. $form->addHeader(get_lang('HelpText2Audio'));
  190. $form->addElement('hidden', 'text2voice_mode', 'google');
  191. $form->addElement('hidden', 'id', $document_id);
  192. $form->addElement('text', 'title', get_lang('Title'));
  193. $form->addElement('select', 'lang', get_lang('Language'), $options);
  194. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_google'));
  195. //echo Display :: return_icon('info3.gif', get_lang('HelpGoogleAudio'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  196. $form->addButtonSave(get_lang('SaveMP3'));
  197. $defaults = array();
  198. $defaults['lang'] = $selected_language;
  199. $form->setDefaults($defaults);
  200. $form->display();
  201. }
  202. if ($service == 'pediaphon') {
  203. //lang default is a default message
  204. $selected_language = "defaultmessage";
  205. $options_pedia['defaultmessage'] = get_lang('FirstSelectALanguage');
  206. $options['defaultmessage'] = get_lang('FirstSelectALanguage');
  207. $form = new FormValidator('form2', 'post', api_get_self().'?'.api_get_cidreq(), '', array('id' => 'form2'));
  208. $form->addHeader(get_lang('HelpText2Audio'));
  209. $form->addElement('hidden', 'text2voice_mode', 'pediaphon');
  210. $form->addElement('hidden', 'id', $document_id);
  211. $form->addElement('text', 'title', get_lang('Title'));
  212. $form->addSelect('lang', get_lang('Language'), $options_pedia, array('class' => 'lang'));
  213. $form->addSelect('voices', get_lang('Voice'), array(get_lang('FirstSelectALanguage')), array('id' => 'voices'));
  214. $speed_options = array();
  215. $speed_options['1'] = get_lang('Normal');
  216. $speed_options['0.75'] = get_lang('GoFaster');
  217. $speed_options['0.8'] = get_lang('Fast');
  218. $speed_options['1.2'] = get_lang('Slow');
  219. $speed_options['1.6'] = get_lang('SlowDown');
  220. $form->addElement('select', 'speed', get_lang('Speed'), $speed_options, array());
  221. $form->addElement('textarea', 'text', get_lang('InsertText2Audio'), array('id' => 'textarea_pediaphon'));
  222. //echo Display :: return_icon('info3.gif', get_lang('HelpPediaphon'), array('align' => 'absmiddle', 'hspace' => '3px'), false);
  223. $form->addButtonSave(get_lang('SaveMP3'));
  224. $defaults = array();
  225. $defaults['lang'] = $selected_language;
  226. $form->setDefaults($defaults);
  227. $form->display();
  228. ?>
  229. <!-- javascript form name form2 update voices -->
  230. <script>
  231. var langslist = document.form2.lang
  232. var voiceslist = document.form2.voices
  233. var voices = new Array()
  234. <!--German -->
  235. voices['de'] = ["<?php echo get_lang('Female') . ' (de1)'; ?>|de1", "<?php echo get_lang('Male') . ' (de2)'; ?>|de2", "<?php echo get_lang('Female') . ' (de3)'; ?>|de3", "<?php echo get_lang('Male') . ' (de4)'; ?>|de4", "<?php echo get_lang('Female') . ' (de5)'; ?>|de5", "<?php echo get_lang('Male') . ' (de6)'; ?>|de6", "<?php echo get_lang('Female') . ' (de7)'; ?>|de7", "<?php echo get_lang('Female') . ' (de8 HQ)'; ?>|de8"]
  236. <!--English -->
  237. voices['en'] = ["<?php echo get_lang('Male') . ' (en1)'; ?>|en1", "<?php echo get_lang('Male') . ' (en2 HQ)'; ?>|en2", "<?php echo get_lang('Female') . ' (us1)'; ?>|us1", "<?php echo get_lang('Male') . ' (us2)'; ?>|us2", "<?php echo get_lang('Male') . ' (us3)'; ?>|us3", "<?php echo get_lang('Female') . '(us4 HQ)'; ?>|us4"]
  238. <!--Spanish -->
  239. voices['es'] = ["<?php echo get_lang('Male') . ' (es5 HQ)'; ?>|es5"]
  240. <!--French -->
  241. voices['fr'] = ["<?php echo get_lang('Female') . ' (fr8 HQ)'; ?>|fr8"]
  242. $(document).ready(function () {
  243. $('.lang').on('change', function () {
  244. var selectedvoicegroup = this.value;
  245. $('#voices').empty();
  246. for (i = 0; i < voices[selectedvoicegroup].length; i++) {
  247. var value = voices[selectedvoicegroup][i].split("|")[1];
  248. var text = voices[selectedvoicegroup][i].split("|")[0];
  249. var newOption = $('<option value="' + value + '">' + text + '</option>');
  250. $('#voices').append(newOption);
  251. $('#voices').selectpicker('refresh');
  252. }
  253. });
  254. });
  255. </script>
  256. <?php
  257. }//end pediaphon
  258. Display:: display_footer();
  259. //Functions. TODO:all at one
  260. /**
  261. * This function save a post into a file mp3 from google services
  262. *
  263. * @param $filepath
  264. * @param $dir
  265. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  266. * @version january 2011, chamilo 1.8.8
  267. */
  268. function downloadMP3_google($filepath, $dir)
  269. {
  270. $location = 'create_audio.php?' . api_get_cidreq() . '&id=' . intval($_POST['id']) . '&service=google';
  271. //security
  272. if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  273. echo '<script>window.location.href="' . $location . '"</script>';
  274. return;
  275. }
  276. $_course = api_get_course_info();
  277. $_user = api_get_user_info();
  278. $clean_title = trim($_POST['title']);
  279. $clean_text = trim($_POST['text']);
  280. if (empty($clean_title) || empty($clean_text)) {
  281. echo '<script>window.location.href="' . $location . '"</script>';
  282. return;
  283. }
  284. $clean_title = Security::remove_XSS($clean_title);
  285. $clean_title = Database::escape_string($clean_title);
  286. $clean_title = str_replace(' ', '_', $clean_title);//compound file names
  287. $clean_text = Security::remove_XSS($clean_text);
  288. $clean_lang = Security::remove_XSS($_POST['lang']);
  289. $extension = 'mp3';
  290. $audio_filename = $clean_title . '.' . $extension;
  291. $audio_title = str_replace('_', ' ', $clean_title);
  292. //prevent duplicates
  293. if (file_exists($filepath . '/' . $clean_title . '.' . $extension)) {
  294. $i = 1;
  295. while (file_exists($filepath . '/' . $clean_title . '_' . $i . '.' . $extension)) {
  296. $i++;
  297. }
  298. $audio_filename = $clean_title . '_' . $i . '.' . $extension;
  299. $audio_title = $clean_title . '_' . $i . '.' . $extension;
  300. $audio_title = str_replace('_', ' ', $audio_title);
  301. }
  302. $documentPath = $filepath . '/' . $audio_filename;
  303. //prev for a fine unicode, borrowed from main api TODO:clean
  304. // Safe replacements for some non-letter characters (whitout blank spaces)
  305. $clean_text = api_replace_dangerous_char($clean_text);
  306. // adding the file
  307. // add new file to disk
  308. $proxySettings = api_get_configuration_value('proxy_settings');
  309. $key = api_get_configuration_value('translate_app_google_key');
  310. $url = "https://www.googleapis.com/language/translate/v2?key=$key&" . $clean_lang . "&target=$clean_lang&q=" . urlencode($clean_text) . "";
  311. if (empty($proxySettings)) {
  312. $content = file_get_contents($url);
  313. } else {
  314. $context = stream_context_create($proxySettings);
  315. $content = file_get_contents($url, false, $context);
  316. }
  317. file_put_contents(
  318. $documentPath,
  319. $content
  320. );
  321. // add document to database
  322. $current_session_id = api_get_session_id();
  323. $groupId = api_get_group_id();
  324. $relativeUrlPath = $dir;
  325. $doc_id = add_document(
  326. $_course,
  327. $relativeUrlPath . $audio_filename,
  328. 'file',
  329. filesize($documentPath),
  330. $audio_title
  331. );
  332. api_item_property_update(
  333. $_course,
  334. TOOL_DOCUMENT,
  335. $doc_id,
  336. 'DocumentAdded',
  337. $_user['user_id'],
  338. $groupId,
  339. null,
  340. null,
  341. null,
  342. $current_session_id
  343. );
  344. Display::display_confirmation_message(get_lang('DocumentCreated'));
  345. //return to location
  346. echo '<script>window.location.href="' . $location . '"</script>';
  347. }
  348. /**
  349. * This function save a post into a file mp3 from pediaphon services
  350. *
  351. * @param $filepath
  352. * @param $dir
  353. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  354. * @version january 2011, chamilo 1.8.8
  355. */
  356. function downloadMP3_pediaphon($filepath, $dir)
  357. {
  358. $location = 'create_audio.php?' . api_get_cidreq() . '&id=' . intval($_POST['id']) . '&service=pediaphon';
  359. //security
  360. if (!isset($_POST['lang']) && !isset($_POST['text']) && !isset($_POST['title']) && !isset($filepath) && !isset($dir)) {
  361. echo '<script>window.location.href="' . $location . '"</script>';
  362. return;
  363. }
  364. $_course = api_get_course_info();
  365. $_user = api_get_user_info();
  366. $clean_title = trim($_POST['title']);
  367. $clean_title = Database::escape_string($clean_title);
  368. $clean_text = trim($_POST['text']);
  369. $clean_voices = Security::remove_XSS($_POST['voices']);
  370. if (empty($clean_title) || empty($clean_text) || empty($clean_voices)) {
  371. echo '<script>window.location.href="' . $location . '"</script>';
  372. return;
  373. }
  374. $clean_title = Security::remove_XSS($clean_title);
  375. $clean_title = Database::escape_string($clean_title);
  376. $clean_title = str_replace(' ', '_', $clean_title);//compound file names
  377. $clean_text = Security::remove_XSS($clean_text);
  378. $clean_lang = Security::remove_XSS($_POST['lang']);
  379. $clean_speed = Security::remove_XSS($_POST['speed']);
  380. $extension = 'mp3';
  381. $audio_filename = $clean_title . '.' . $extension;
  382. $audio_title = str_replace('_', ' ', $clean_title);
  383. //prevent duplicates
  384. if (file_exists($filepath . '/' . $clean_title . '.' . $extension)) {
  385. $i = 1;
  386. while (file_exists($filepath . '/' . $clean_title . '_' . $i . '.' . $extension)) {
  387. $i++;
  388. }
  389. $audio_filename = $clean_title . '_' . $i . '.' . $extension;
  390. $audio_title = $clean_title . '_' . $i . '.' . $extension;
  391. $audio_title = str_replace('_', ' ', $audio_title);
  392. }
  393. $documentPath = $filepath . '/' . $audio_filename;
  394. $clean_text = api_replace_dangerous_char($clean_text);
  395. //adding the file
  396. if ($clean_lang == 'de') {
  397. $url_pediaphon = 'http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice.cgi';
  398. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/(.*)\.mp3\"/';
  399. } else {
  400. $url_pediaphon = 'http://www.pediaphon.org/~bischoff/radiopedia/sprich_multivoice_' . $clean_lang . '.cgi';//en, es, fr
  401. $find_t2v = '/http\:\/\/www\.pediaphon\.org\/\~bischoff\/radiopedia\/mp3\/' . $clean_lang . '\/(.*)\.mp3\"/';
  402. }
  403. $data = "stimme=" . $clean_voices . "&inputtext=" . $clean_text . "&speed=" . $clean_speed . "&go=speak";
  404. $opts = array(
  405. 'http' =>
  406. array(
  407. 'method' => 'POST',
  408. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  409. "Content-Length: " . strlen($data) . "\r\n",
  410. 'content' => $data
  411. )
  412. );
  413. // Download the whole HTML page
  414. $context = stream_context_create($opts);
  415. // Download the whole HTML page
  416. $previous_returntext2voice = file_get_contents($url_pediaphon, false, $context);
  417. //extract the audio file path
  418. $search_source = preg_match($find_t2v, $previous_returntext2voice, $hits);
  419. $souce_end = substr($hits[0], 0, -1);
  420. //download file
  421. $returntext2voice = file_get_contents($souce_end);
  422. //save file
  423. $f = @file_put_contents($documentPath, $returntext2voice);
  424. if ($f === false && !empty($php_errormsg)) {
  425. error_log($php_errormsg);
  426. }
  427. //add document to database
  428. $current_session_id = api_get_session_id();
  429. $groupId = api_get_group_id();
  430. $relativeUrlPath = $dir;
  431. $doc_id = add_document(
  432. $_course,
  433. $relativeUrlPath.$audio_filename,
  434. 'file',
  435. filesize($documentPath),
  436. $audio_title
  437. );
  438. api_item_property_update(
  439. $_course,
  440. TOOL_DOCUMENT,
  441. $doc_id,
  442. 'DocumentAdded',
  443. $_user['user_id'],
  444. $groupId,
  445. null,
  446. null,
  447. null,
  448. $current_session_id
  449. );
  450. Display::display_confirmation_message(get_lang('DocumentCreated'));
  451. //return to location
  452. echo '<script>window.location.href="' . $location . '"</script>';
  453. }