record_document.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. require_once '../../../inc/global.inc.php';
  3. //Add security from Chamilo
  4. api_protect_course_script();
  5. api_block_anonymous_users();
  6. //
  7. # Save the audio to a URL-accessible directory for playback.
  8. parse_str($_SERVER['QUERY_STRING'], $params);
  9. if (isset($params['waminame']) && isset($params['wamidir']) && isset($params['wamiuserid'])) {
  10. $waminame = $params['waminame'];
  11. $wamidir = $params['wamidir'];
  12. $wamiuserid = $params['wamiuserid'];
  13. } else {
  14. api_not_allowed();
  15. die();
  16. }
  17. if ($wamiuserid != api_get_user_id() || api_get_user_id() == 0 || $wamiuserid == 0) {
  18. api_not_allowed();
  19. die();
  20. }
  21. //clean
  22. $waminame = Security::remove_XSS($waminame);
  23. $waminame = Database::escape_string($waminame);
  24. $waminame = addslashes(trim($waminame));
  25. $waminame = api_replace_dangerous_char($waminame, 'strict');
  26. $waminame = FileManager::disable_dangerous_file($waminame);
  27. $wamidir = Security::remove_XSS($wamidir);
  28. $content = file_get_contents('php://input');
  29. //security extension
  30. $ext = explode('.', $waminame);
  31. $ext = strtolower($ext[sizeof($ext) - 1]);
  32. if ($ext != 'wav') {
  33. die();
  34. }
  35. //Do not use here check Fileinfo method because return: text/plain
  36. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  37. $saveDir = $dirBaseDocuments.$wamidir;
  38. $current_session_id = api_get_session_id();
  39. $groupId = $_SESSION['_gid'];
  40. //avoid duplicates
  41. $waminame_to_save = $waminame;
  42. $title_to_save = str_replace('_', ' ', $waminame);
  43. $waminame_noex = basename($waminame, ".wav");
  44. if (file_exists($saveDir.'/'.$waminame_noex.'.'.$ext)) {
  45. $i = 1;
  46. while (file_exists($saveDir.'/'.$waminame_noex.'_'.$i.'.'.$ext)) {
  47. $i++;
  48. }
  49. $waminame_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  50. $title_to_save = $waminame_noex.'_'.$i.'.'.$ext;
  51. $title_to_save = str_replace('_', ' ', $title_to_save);
  52. }
  53. $documentPath = $saveDir.'/'.$waminame_to_save;
  54. //make a temporal file for get the file size
  55. $tmpfname = tempnam("/tmp", "CTF");
  56. $handle = fopen($tmpfname, "w");
  57. fwrite($handle, $content);
  58. fclose($handle);
  59. // Check if there is enough space in the course to save the file
  60. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  61. unlink($tmpfname);
  62. die(get_lang('UplNotEnoughSpace'));
  63. }
  64. //erase temporal file
  65. unlink($tmpfname);
  66. //add to disk
  67. $fh = fopen($documentPath, 'w') or die("can't open file");
  68. fwrite($fh, $content);
  69. fclose($fh);
  70. //add document to database
  71. $doc_id = FileManager::add_document(
  72. $_course,
  73. $wamidir.'/'.$waminame_to_save,
  74. 'file',
  75. filesize($documentPath),
  76. $title_to_save
  77. );
  78. api_item_property_update(
  79. $_course,
  80. TOOL_DOCUMENT,
  81. $doc_id,
  82. 'DocumentAdded',
  83. $_user['user_id'],
  84. $groupId,
  85. null,
  86. null,
  87. null,
  88. $current_session_id
  89. );
  90. ?>