create_audio.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. *
  11. * @since 8/January/2011
  12. * TODO:clean all file
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. $nameTools = get_lang('Create audio');
  17. api_protect_course_script();
  18. api_block_anonymous_users();
  19. $courseInfo = api_get_course_info();
  20. $groupRights = Session::read('group_member_with_upload_rights');
  21. $groupId = api_get_group_id();
  22. if (api_get_setting('enabled_text2audio') === 'false') {
  23. api_not_allowed(true);
  24. }
  25. $requestId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
  26. $document_data = DocumentManager::get_document_data_by_id(
  27. $requestId,
  28. api_get_course_id()
  29. );
  30. if (empty($document_data)) {
  31. if (api_is_in_group()) {
  32. $group_properties = GroupManager::get_group_properties(
  33. $groupId
  34. );
  35. $document_id = DocumentManager::get_document_id(
  36. api_get_course_info(),
  37. $group_properties['directory']
  38. );
  39. $document_data = DocumentManager::get_document_data_by_id(
  40. $document_id,
  41. api_get_course_id()
  42. );
  43. }
  44. }
  45. $document_id = $document_data['id'];
  46. $dir = $document_data['path'];
  47. //jquery textareaCounter
  48. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/textareacounter/jquery.textareaCounter.plugin.js" type="text/javascript"></script>';
  49. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  50. // Please, do not modify this dirname formatting
  51. if (strstr($dir, '..')) {
  52. $dir = '/';
  53. }
  54. if ($dir[0] == '.') {
  55. $dir = substr($dir, 1);
  56. }
  57. if ($dir[0] != '/') {
  58. $dir = '/'.$dir;
  59. }
  60. if ($dir[strlen($dir) - 1] != '/') {
  61. $dir .= '/';
  62. }
  63. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  64. if (!is_dir($filepath)) {
  65. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  66. $dir = '/';
  67. }
  68. //groups //TODO: clean
  69. if (!empty($groupId)) {
  70. $interbreadcrumb[] = [
  71. "url" => "../group/group_space.php?".api_get_cidreq(),
  72. "name" => get_lang('Group area'),
  73. ];
  74. $group = GroupManager:: get_group_properties($groupId);
  75. $path = explode('/', $dir);
  76. if ('/'.$path[1] != $group['directory']) {
  77. api_not_allowed(true);
  78. }
  79. }
  80. $interbreadcrumb[] = [
  81. 'url' => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(),
  82. 'name' => get_lang('Documents'),
  83. ];
  84. if (!api_is_allowed_in_course()) {
  85. api_not_allowed(true);
  86. }
  87. if (!($is_allowed_to_edit || $groupRights ||
  88. DocumentManager::is_my_shared_folder(
  89. api_get_user_id(),
  90. Security::remove_XSS($dir),
  91. api_get_session_id()
  92. ))
  93. ) {
  94. api_not_allowed(true);
  95. }
  96. Event::event_access_tool(TOOL_DOCUMENT);
  97. $display_dir = $dir;
  98. if (isset($group)) {
  99. $display_dir = explode('/', $dir);
  100. unset($display_dir[0]);
  101. unset($display_dir[1]);
  102. $display_dir = implode('/', $display_dir);
  103. }
  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[] = ['url' => $url_dir, 'name' => $url_to_who];
  113. $dir_acum .= $dir_array[$i].'/';
  114. }
  115. $service = isset($_GET['service']) ? $_GET['service'] : 'google';
  116. if (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'google') {
  117. downloadAudioGoogle($dir);
  118. }
  119. Display:: display_header($nameTools, 'Doc');
  120. echo '<div class="actions">';
  121. echo '<a href="document.php?id='.$document_id.'">';
  122. echo Display::return_icon(
  123. 'back.png',
  124. get_lang('Back to').' '.get_lang('Documents overview'),
  125. '',
  126. ICON_SIZE_MEDIUM
  127. );
  128. echo '</a>';
  129. echo '<a href="create_audio.php?'.api_get_cidreq().'&id='.$document_id.'&service=google">'.
  130. Display::return_icon('google.png', get_lang('Use Google audio services'), '', ICON_SIZE_MEDIUM).'</a>';
  131. echo '</div>';
  132. ?>
  133. <!-- javascript and styles for textareaCounter-->
  134. <script>
  135. var info;
  136. $(function() {
  137. var options = {
  138. 'maxCharacterSize': 100,
  139. 'originalStyle': 'originalTextareaInfo',
  140. 'warningStyle': 'warningTextareaInfo',
  141. 'warningNumber': 20,
  142. 'displayFormat': '#input/#max'
  143. };
  144. $('#textarea_google').textareaCount(options, function (data) {
  145. $('#textareaCallBack').html(data);
  146. });
  147. });
  148. </script>
  149. <style>
  150. .overview {
  151. background: #FFEC9D;
  152. padding: 10px;
  153. width: 90%;
  154. border: 1px solid #CCCCCC;
  155. }
  156. .originalTextareaInfo {
  157. font-size: 12px;
  158. color: #000000;
  159. text-align: right;
  160. }
  161. .warningTextareaInfo {
  162. color: #FF0000;
  163. font-weight: bold;
  164. text-align: right;
  165. }
  166. #showData {
  167. height: 70px;
  168. width: 200px;
  169. border: 1px solid #CCCCCC;
  170. padding: 10px;
  171. margin: 10px;
  172. }
  173. </style>
  174. <div id="textareaCallBack"></div>
  175. <?php
  176. $tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  177. $sql_select = "SELECT * FROM $tbl_admin_languages";
  178. $result_select = Database::query($sql_select);
  179. $options = $options_pedia = [];
  180. $selected_language = null;
  181. while ($row = Database::fetch_array($result_select)) {
  182. $options[$row['isocode']] = $row['original_name'].' ('.$row['english_name'].')';
  183. if (in_array($row['isocode'], ['de', 'en', 'es', 'fr'])) {
  184. $options_pedia[$row['isocode']] = $row['original_name'].' ('.$row['english_name'].')';
  185. }
  186. }
  187. if ($service == 'google') {
  188. $selected_language = api_get_language_isocode(); //lang default is the course language
  189. $form = new FormValidator('form1', 'post', api_get_self().'?'.api_get_cidreq(), '', ['id' => 'form1']);
  190. $form->addHeader(get_lang('Convert your text to speech'));
  191. $form->addElement('hidden', 'text2voice_mode', 'google');
  192. $form->addElement('hidden', 'id', $document_id);
  193. $form->addElement('text', 'title', get_lang('Title'));
  194. $form->addElement('select', 'lang', get_lang('Language'), $options);
  195. $form->addElement('textarea', 'text', get_lang('Enter the text you want to convert to an audio file'), ['id' => 'textarea_google']);
  196. $form->addButtonSave(get_lang('Save mp3'));
  197. $defaults = [];
  198. $defaults['lang'] = $selected_language;
  199. $form->setDefaults($defaults);
  200. $form->display();
  201. }
  202. Display::display_footer();
  203. /**
  204. * This function save a post into a file mp3 from google services.
  205. *
  206. * @param $filepath
  207. * @param $dir
  208. *
  209. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  210. *
  211. * @version january 2011, chamilo 1.8.8
  212. */
  213. function downloadAudioGoogle($dir)
  214. {
  215. $location = 'create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&service=google';
  216. //security
  217. if (!isset($_POST['lang']) && !isset($_POST['text']) &&
  218. !isset($_POST['title']) && !isset($dir)
  219. ) {
  220. echo '<script>window.location.href="'.$location.'"</script>';
  221. return;
  222. }
  223. $_course = api_get_course_info();
  224. $clean_title = trim($_POST['title']);
  225. $clean_text = trim($_POST['text']);
  226. if (empty($clean_title) || empty($clean_text)) {
  227. echo '<script>window.location.href="'.$location.'"</script>';
  228. return;
  229. }
  230. $clean_title = Security::remove_XSS($clean_title);
  231. $clean_title = Database::escape_string($clean_title);
  232. $clean_title = str_replace(' ', '_', $clean_title); //compound file names
  233. $clean_text = Security::remove_XSS($clean_text);
  234. $clean_lang = Security::remove_XSS($_POST['lang']);
  235. $extension = 'mp3';
  236. $audio_filename = $clean_title.'.'.$extension;
  237. $audio_title = str_replace('_', ' ', $clean_title);
  238. $clean_text = api_replace_dangerous_char($clean_text);
  239. // adding the file
  240. // add new file to disk
  241. $proxySettings = api_get_configuration_value('proxy_settings');
  242. $key = api_get_configuration_value('translate_app_google_key');
  243. $url = "https://www.googleapis.com/language/translate/v2?key=$key&".$clean_lang."&target=$clean_lang&q=".urlencode($clean_text)."";
  244. if (empty($proxySettings)) {
  245. $content = @file_get_contents($url);
  246. } else {
  247. if (!empty($proxySettings['stream_context_create'])) {
  248. $context = stream_context_create($proxySettings['stream_context_create']);
  249. } else {
  250. $context = stream_context_create();
  251. }
  252. $content = file_get_contents($url, false, $context);
  253. }
  254. if (empty($content)) {
  255. Display::addFlash(Display::return_message(get_lang('The Google Translate API used by this feature returned and empty answer. Please check or ask your translate_app_google_key setting is set correctly, or kindly ask an administrator to do so.'), 'error'));
  256. return;
  257. }
  258. // add document to database
  259. $relativeUrlPath = $dir;
  260. DocumentManager::addDocument(
  261. $_course,
  262. $relativeUrlPath.$audio_filename,
  263. 'file',
  264. '',
  265. $audio_title,
  266. null,
  267. 0,
  268. null,
  269. 0,
  270. 0,
  271. 0,
  272. true,
  273. $content
  274. );
  275. echo Display::return_message(get_lang('Documented created'), 'confirm');
  276. echo '<script>window.location.href="'.$location.'"</script>';
  277. }