record_audio_wami.ajax.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_protect_course_script();
  7. api_block_anonymous_users();
  8. $_course = api_get_course_info();
  9. # Save the audio to a URL-accessible directory for playback.
  10. parse_str($_SERVER['QUERY_STRING'], $params);
  11. if (isset($params['waminame']) && isset($params['wamidir']) && isset($params['wamiuserid'])) {
  12. $waminame = $params['waminame'];
  13. $wamidir = $params['wamidir'];
  14. $wamiuserid = $params['wamiuserid'];
  15. } else {
  16. api_not_allowed();
  17. die();
  18. }
  19. if ($wamiuserid != api_get_user_id() || api_get_user_id() == 0 || $wamiuserid == 0) {
  20. api_not_allowed();
  21. die();
  22. }
  23. // Clean
  24. $waminame = Security::remove_XSS($waminame);
  25. $waminame = Database::escape_string($waminame);
  26. $waminame = api_replace_dangerous_char($waminame);
  27. $waminame = disable_dangerous_file($waminame);
  28. $wamidir = Security::remove_XSS($wamidir);
  29. $content = file_get_contents('php://input');
  30. if (empty($content)) {
  31. exit;
  32. }
  33. $ext = explode('.', $waminame);
  34. $ext = strtolower($ext[sizeof($ext) - 1]);
  35. if ($ext != 'wav') {
  36. die();
  37. }
  38. // Do not use here check Fileinfo method because return: text/plain
  39. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  40. $saveDir = $dirBaseDocuments.$wamidir;
  41. if (!is_dir($saveDir)) {
  42. DocumentManager::createDefaultAudioFolder($_course);
  43. }
  44. //avoid duplicates
  45. $waminame_to_save = $waminame;
  46. $waminame_noex = basename($waminame, ".wav");
  47. if (file_exists($saveDir.'/'.$waminame_noex.'.'.$ext)) {
  48. $i = 1;
  49. while (file_exists($saveDir.'/'.$waminame_noex.'_'.$i.'.'.$ext)) {
  50. $i++;
  51. }
  52. $waminame_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  53. }
  54. $documentPath = $saveDir.'/'.$waminame_to_save;
  55. // Add to disk
  56. $fh = fopen($documentPath, 'w') or die("can't open file");
  57. fwrite($fh, $content);
  58. fclose($fh);
  59. $fileInfo = pathinfo($documentPath);
  60. $courseInfo = api_get_course_info();
  61. $file = array(
  62. 'file' => array(
  63. 'name' => $fileInfo['basename'],
  64. 'tmp_name' => $documentPath,
  65. 'size' => filesize($documentPath),
  66. 'type' => 'audio/wav',
  67. 'from_file' => true
  68. )
  69. );
  70. $output = true;
  71. ob_start();
  72. // Strangely the file path changes with a double extension
  73. copy($documentPath, $documentPath.'.wav');
  74. $documentData = DocumentManager::upload_document(
  75. $file,
  76. $wamidir,
  77. $fileInfo['basename'],
  78. 'wav',
  79. 0,
  80. 'overwrite',
  81. false,
  82. $output
  83. );
  84. $contents = ob_get_contents();
  85. if (!empty($documentData)) {
  86. $newDocId = $documentData['id'];
  87. $documentData['comment'] = 'mp3';
  88. $newMp3DocumentId = DocumentManager::addAndConvertWavToMp3(
  89. $documentData,
  90. $courseInfo,
  91. api_get_session_id(),
  92. api_get_user_id(),
  93. 'overwrite',
  94. true
  95. );
  96. if ($newMp3DocumentId) {
  97. $newDocId = $newMp3DocumentId;
  98. }
  99. if (isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id'])) {
  100. $lpItemId = $_REQUEST['lp_item_id'];
  101. /** @var learnpath $lp */
  102. $lp = Session::read('oLP');
  103. if (!empty($lp)) {
  104. $lp->set_modified_on();
  105. $lpItem = new learnpathItem($lpItemId);
  106. $lpItem->add_audio_from_documents($newDocId);
  107. echo Display::return_message(get_lang('Updated'), 'info');
  108. }
  109. }
  110. // Strangely the file path changes with a double extension
  111. // Remove file with one extension
  112. unlink($documentPath);
  113. } else {
  114. echo $contents;
  115. }