create_audio.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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('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. $requestId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
  25. $document_data = DocumentManager::get_document_data_by_id(
  26. $requestId,
  27. api_get_course_id()
  28. );
  29. if (empty($document_data)) {
  30. if (api_is_in_group()) {
  31. $group_properties = GroupManager::get_group_properties(
  32. $groupId
  33. );
  34. $document_id = DocumentManager::get_document_id(
  35. api_get_course_info(),
  36. $group_properties['directory']
  37. );
  38. $document_data = DocumentManager::get_document_data_by_id(
  39. $document_id,
  40. api_get_course_id()
  41. );
  42. }
  43. }
  44. $document_id = $document_data['id'];
  45. $dir = $document_data['path'];
  46. //jquery textareaCounter
  47. $htmlHeadXtra[] = '<script src="../inc/lib/javascript/textareacounter/jquery.textareaCounter.plugin.js" type="text/javascript"></script>';
  48. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  49. // Please, do not modify this dirname formatting
  50. if (strstr($dir, '..')) {
  51. $dir = '/';
  52. }
  53. if ($dir[0] == '.') {
  54. $dir = substr($dir, 1);
  55. }
  56. if ($dir[0] != '/') {
  57. $dir = '/'.$dir;
  58. }
  59. if ($dir[strlen($dir) - 1] != '/') {
  60. $dir .= '/';
  61. }
  62. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  63. if (!is_dir($filepath)) {
  64. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  65. $dir = '/';
  66. }
  67. //groups //TODO: clean
  68. if (!empty($groupId)) {
  69. $interbreadcrumb[] = [
  70. "url" => "../group/group_space.php?".api_get_cidreq(),
  71. "name" => get_lang('GroupSpace'),
  72. ];
  73. $group = GroupManager:: get_group_properties($groupId);
  74. $path = explode('/', $dir);
  75. if ('/'.$path[1] != $group['directory']) {
  76. api_not_allowed(true);
  77. }
  78. }
  79. $interbreadcrumb[] = [
  80. "url" => "./document.php?curdirpath=".urlencode($dir)."&".api_get_cidreq(),
  81. "name" => get_lang('Documents'),
  82. ];
  83. if (!api_is_allowed_in_course()) {
  84. api_not_allowed(true);
  85. }
  86. if (!($is_allowed_to_edit || $groupRights ||
  87. DocumentManager::is_my_shared_folder(
  88. api_get_user_id(),
  89. Security::remove_XSS($dir),
  90. api_get_session_id()
  91. ))
  92. ) {
  93. api_not_allowed(true);
  94. }
  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. // Copied from document.php
  104. $dir_array = explode('/', $dir);
  105. $array_len = count($dir_array);
  106. $dir_acum = '';
  107. for ($i = 0; $i < $array_len; $i++) {
  108. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  109. //Max char 80
  110. $url_to_who = cut($dir_array[$i], 80);
  111. $interbreadcrumb[] = ['url' => $url_dir, 'name' => $url_to_who];
  112. $dir_acum .= $dir_array[$i].'/';
  113. }
  114. $service = isset($_GET['service']) ? $_GET['service'] : 'google';
  115. if (isset($_POST['text2voice_mode']) && $_POST['text2voice_mode'] == 'google') {
  116. downloadAudioGoogle($filepath, $dir);
  117. }
  118. Display:: display_header($nameTools, 'Doc');
  119. echo '<div class="actions">';
  120. echo '<a href="document.php?id='.$document_id.'">';
  121. echo Display::return_icon(
  122. 'back.png',
  123. get_lang('BackTo').' '.get_lang('DocumentsOverview'),
  124. '',
  125. ICON_SIZE_MEDIUM
  126. );
  127. echo '</a>';
  128. echo '<a href="create_audio.php?'.api_get_cidreq().'&id='.$document_id.'&service=google">'.
  129. Display::return_icon('google.png', get_lang('GoogleAudio'), '', ICON_SIZE_MEDIUM).'</a>';
  130. echo '</div>';
  131. ?>
  132. <!-- javascript and styles for textareaCounter-->
  133. <script>
  134. var info;
  135. $(function() {
  136. var options = {
  137. 'maxCharacterSize': 100,
  138. 'originalStyle': 'originalTextareaInfo',
  139. 'warningStyle': 'warningTextareaInfo',
  140. 'warningNumber': 20,
  141. 'displayFormat': '#input/#max'
  142. };
  143. $('#textarea_google').textareaCount(options, function (data) {
  144. $('#textareaCallBack').html(data);
  145. });
  146. });
  147. </script>
  148. <style>
  149. .overview {
  150. background: #FFEC9D;
  151. padding: 10px;
  152. width: 90%;
  153. border: 1px solid #CCCCCC;
  154. }
  155. .originalTextareaInfo {
  156. font-size: 12px;
  157. color: #000000;
  158. text-align: right;
  159. }
  160. .warningTextareaInfo {
  161. color: #FF0000;
  162. font-weight: bold;
  163. text-align: right;
  164. }
  165. #showData {
  166. height: 70px;
  167. width: 200px;
  168. border: 1px solid #CCCCCC;
  169. padding: 10px;
  170. margin: 10px;
  171. }
  172. </style>
  173. <div id="textareaCallBack"></div>
  174. <?php
  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 = [];
  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'], ['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(), '', ['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'), ['id' => 'textarea_google']);
  195. $form->addButtonSave(get_lang('SaveMP3'));
  196. $defaults = [];
  197. $defaults['lang'] = $selected_language;
  198. $form->setDefaults($defaults);
  199. $form->display();
  200. }
  201. Display::display_footer();
  202. /**
  203. * This function save a post into a file mp3 from google services.
  204. *
  205. * @param $filepath
  206. * @param $dir
  207. *
  208. * @author Juan Carlos Raña Trabado <herodoto@telefonica.net>
  209. *
  210. * @version january 2011, chamilo 1.8.8
  211. */
  212. function downloadAudioGoogle($filepath, $dir)
  213. {
  214. $location = 'create_audio.php?'.api_get_cidreq().'&id='.intval($_POST['id']).'&service=google';
  215. //security
  216. if (!isset($_POST['lang']) && !isset($_POST['text']) &&
  217. !isset($_POST['title']) && !isset($filepath) && !isset($dir)
  218. ) {
  219. echo '<script>window.location.href="'.$location.'"</script>';
  220. return;
  221. }
  222. $_course = api_get_course_info();
  223. $_user = api_get_user_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. //prevent duplicates
  239. if (file_exists($filepath.'/'.$clean_title.'.'.$extension)) {
  240. $i = 1;
  241. while (file_exists($filepath.'/'.$clean_title.'_'.$i.'.'.$extension)) {
  242. $i++;
  243. }
  244. $audio_filename = $clean_title.'_'.$i.'.'.$extension;
  245. $audio_title = $clean_title.'_'.$i.'.'.$extension;
  246. $audio_title = str_replace('_', ' ', $audio_title);
  247. }
  248. $documentPath = $filepath.'/'.$audio_filename;
  249. $clean_text = api_replace_dangerous_char($clean_text);
  250. // adding the file
  251. // add new file to disk
  252. $proxySettings = api_get_configuration_value('proxy_settings');
  253. $key = api_get_configuration_value('translate_app_google_key');
  254. $url = "https://www.googleapis.com/language/translate/v2?key=$key&".$clean_lang."&target=$clean_lang&q=".urlencode($clean_text)."";
  255. if (empty($proxySettings)) {
  256. $content = @file_get_contents($url);
  257. } else {
  258. if (!empty($proxySettings['stream_context_create'])) {
  259. $context = stream_context_create($proxySettings['stream_context_create']);
  260. } else {
  261. $context = stream_context_create();
  262. }
  263. $content = file_get_contents($url, false, $context);
  264. }
  265. if (empty($content)) {
  266. Display::addFlash(Display::return_message(get_lang('GoogleTranslateApiReturnedEmptyAnswer'), 'error'));
  267. return;
  268. }
  269. file_put_contents(
  270. $documentPath,
  271. $content
  272. );
  273. // add document to database
  274. $current_session_id = api_get_session_id();
  275. $groupId = api_get_group_id();
  276. $groupInfo = GroupManager::get_group_properties($groupId);
  277. $relativeUrlPath = $dir;
  278. $doc_id = add_document(
  279. $_course,
  280. $relativeUrlPath.$audio_filename,
  281. 'file',
  282. filesize($documentPath),
  283. $audio_title
  284. );
  285. api_item_property_update(
  286. $_course,
  287. TOOL_DOCUMENT,
  288. $doc_id,
  289. 'DocumentAdded',
  290. $_user['user_id'],
  291. $groupInfo,
  292. null,
  293. null,
  294. null,
  295. $current_session_id
  296. );
  297. echo Display::return_message(get_lang('DocumentCreated'), 'confirm');
  298. echo '<script>window.location.href="'.$location.'"</script>';
  299. }