record_audio_wami.ajax.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. require_once __DIR__.'/../global.inc.php';
  5. // Add security from Chamilo
  6. api_block_anonymous_users();
  7. $_course = api_get_course_info();
  8. // Save the audio to a URL-accessible directory for playback.
  9. parse_str($_SERVER['QUERY_STRING'], $params);
  10. if (isset($params['waminame']) && isset($params['wamidir']) && isset($params['wamiuserid'])) {
  11. $waminame = $params['waminame'];
  12. $wamidir = $params['wamidir'];
  13. $wamiuserid = $params['wamiuserid'];
  14. } else {
  15. api_not_allowed();
  16. die();
  17. }
  18. if (empty($wamiuserid)) {
  19. api_not_allowed();
  20. die();
  21. }
  22. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'document'; // can be document or message
  23. if ($type === 'document') {
  24. api_protect_course_script();
  25. }
  26. // Clean
  27. $waminame = Security::remove_XSS($waminame);
  28. $waminame = Database::escape_string($waminame);
  29. $waminame = api_replace_dangerous_char($waminame);
  30. $waminame = disable_dangerous_file($waminame);
  31. $wamidir = Security::remove_XSS($wamidir);
  32. $content = file_get_contents('php://input');
  33. if (empty($content)) {
  34. exit;
  35. }
  36. $ext = explode('.', $waminame);
  37. $ext = strtolower($ext[sizeof($ext) - 1]);
  38. if ($ext != 'wav') {
  39. die();
  40. }
  41. switch ($type) {
  42. case 'document':
  43. // Do not use here check Fileinfo method because return: text/plain
  44. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  45. $saveDir = $dirBaseDocuments.$wamidir;
  46. if (!is_dir($saveDir)) {
  47. DocumentManager::createDefaultAudioFolder($_course);
  48. }
  49. // Avoid duplicates
  50. $waminame_to_save = $waminame;
  51. $documentPath = $saveDir.'/'.$waminame_to_save;
  52. // Add to disk
  53. $fh = fopen($documentPath, 'w') or die("can't open file");
  54. fwrite($fh, $content);
  55. fclose($fh);
  56. $fileInfo = pathinfo($documentPath);
  57. $courseInfo = api_get_course_info();
  58. $file = [
  59. 'file' => [
  60. 'name' => $fileInfo['basename'],
  61. 'tmp_name' => $documentPath,
  62. 'size' => filesize($documentPath),
  63. 'type' => 'audio/wav',
  64. 'from_file' => true,
  65. ],
  66. ];
  67. $output = true;
  68. ob_start();
  69. // Strangely the file path changes with a double extension
  70. copy($documentPath, $documentPath.'.wav');
  71. $documentData = DocumentManager::upload_document(
  72. $file,
  73. $wamidir,
  74. $fileInfo['basename'],
  75. 'wav',
  76. 0,
  77. 'overwrite',
  78. false,
  79. $output
  80. );
  81. $contents = ob_get_contents();
  82. if (!empty($documentData)) {
  83. $newDocId = $documentData['id'];
  84. $documentData['comment'] = 'mp3';
  85. $newMp3DocumentId = DocumentManager::addAndConvertWavToMp3(
  86. $documentData,
  87. $courseInfo,
  88. api_get_session_id(),
  89. api_get_user_id(),
  90. 'overwrite',
  91. true
  92. );
  93. if ($newMp3DocumentId) {
  94. $newDocId = $newMp3DocumentId;
  95. }
  96. if (isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id'])) {
  97. $lpItemId = $_REQUEST['lp_item_id'];
  98. /** @var learnpath $lp */
  99. $lp = Session::read('oLP');
  100. if (!empty($lp)) {
  101. $lp->set_modified_on();
  102. $lpItem = new learnpathItem($lpItemId);
  103. $lpItem->add_audio_from_documents($newDocId);
  104. echo Display::return_message(get_lang('Update successful'), 'info');
  105. }
  106. }
  107. // Strangely the file path changes with a double extension
  108. // Remove file with one extension
  109. unlink($documentPath);
  110. } else {
  111. echo $contents;
  112. }
  113. break;
  114. case 'message':
  115. $tempFile = api_get_path(SYS_ARCHIVE_PATH).$waminame;
  116. file_put_contents($tempFile, $content);
  117. Session::write('current_audio_id', $waminame);
  118. $file = [
  119. 'name' => basename($tempFile),
  120. 'tmp_name' => $tempFile,
  121. 'size' => filesize($tempFile),
  122. 'type' => 'audio/wav',
  123. 'move_file' => true,
  124. ];
  125. api_upload_file('audio_message', $file, api_get_user_id());
  126. break;
  127. }