create_audio.php 18 KB

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