record_document.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // Add security from Chamilo
  4. api_protect_course_script();
  5. api_block_anonymous_users();
  6. # Save the audio to a URL-accessible directory for playback.
  7. parse_str($_SERVER['QUERY_STRING'], $params);
  8. if (isset($params['waminame']) && isset($params['wamidir']) && isset($params['wamiuserid'])) {
  9. $waminame = $params['waminame'];
  10. $wamidir = $params['wamidir'];
  11. $wamiuserid = $params['wamiuserid'];
  12. } else {
  13. api_not_allowed();
  14. die();
  15. }
  16. if ($wamiuserid != api_get_user_id() || api_get_user_id() == 0 || $wamiuserid == 0) {
  17. api_not_allowed();
  18. die();
  19. }
  20. //clean
  21. $waminame = Security::remove_XSS($waminame);
  22. $waminame = Database::escape_string($waminame);
  23. $waminame = addslashes(trim($waminame));
  24. $waminame = api_replace_dangerous_char($waminame, 'strict');
  25. $waminame = FileManager::disable_dangerous_file($waminame);
  26. $wamidir = Security::remove_XSS($wamidir);
  27. $content = file_get_contents('php://input');
  28. //security extension
  29. $ext = explode('.', $waminame);
  30. $ext = strtolower($ext[sizeof($ext) - 1]);
  31. if ($ext != 'wav') {
  32. die();
  33. }
  34. //Do not use here check Fileinfo method because return: text/plain
  35. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  36. $saveDir = $dirBaseDocuments.$wamidir;
  37. $current_session_id = api_get_session_id();
  38. $groupId = $_SESSION['_gid'];
  39. //avoid duplicates
  40. $waminame_to_save = $waminame;
  41. $title_to_save = str_replace('_', ' ', $waminame);
  42. $waminame_noex = basename($waminame, ".wav");
  43. if (file_exists($saveDir.'/'.$waminame_noex.'.'.$ext)) {
  44. $i = 1;
  45. while (file_exists($saveDir.'/'.$waminame_noex.'_'.$i.'.'.$ext)) {
  46. $i++;
  47. }
  48. $waminame_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  49. $title_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  50. $title_to_save = str_replace('_', ' ', $title_to_save);
  51. }
  52. $documentPath = $saveDir.'/'.$waminame_to_save;
  53. //make a temporal file for get the file size
  54. $tmpfname = tempnam("/tmp", "CTF");
  55. $handle = fopen($tmpfname, "w");
  56. fwrite($handle, $content);
  57. fclose($handle);
  58. // Check if there is enough space in the course to save the file
  59. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  60. unlink($tmpfname);
  61. die(get_lang('UplNotEnoughSpace'));
  62. }
  63. //erase temporal file
  64. unlink($tmpfname);
  65. // Add to disk
  66. $fh = fopen($documentPath, 'w') or die("can't open file");
  67. fwrite($fh, $content);
  68. fclose($fh);
  69. error_log($documentPath);
  70. $fileInfo = pathinfo($documentPath);
  71. $courseInfo = api_get_course_info();
  72. $file = array(
  73. 'file' => array(
  74. 'name' => $fileInfo['basename'],
  75. 'tmp_name' => $documentPath,
  76. 'size' => filesize($documentPath),
  77. 'from_file' => true
  78. )
  79. );
  80. $output = true;
  81. $documentData = DocumentManager::upload_document($file, $wamidir, null, null, 0, 'overwrite', false, $output);
  82. if (!empty($documentData)) {
  83. $newDocId = $documentData['id'];
  84. $newMp3DocumentId = DocumentManager::addAndConvertWavToMp3($documentData, $courseInfo, api_get_user_id());
  85. if ($newMp3DocumentId) {
  86. $newDocId = $newMp3DocumentId;
  87. }
  88. if (isset($_REQUEST['lp_item_id']) && !empty($_REQUEST['lp_item_id'])) {
  89. $lpItemId = $_REQUEST['lp_item_id'];
  90. /** @var learnpath $lp */
  91. $lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
  92. if (!empty($lp)) {
  93. $lp->set_modified_on();
  94. $lpItem = new learnpathItem($lpItemId);
  95. $lpItem->add_audio_from_documents($newDocId);
  96. }
  97. }
  98. }