document.ajax.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for the document upload.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. $action = $_REQUEST['a'];
  8. switch ($action) {
  9. case 'get_dir_size':
  10. api_protect_course_script(true);
  11. $path = isset($_GET['path']) ? $_GET['path'] : '';
  12. $isAllowedToEdit = api_is_allowed_to_edit();
  13. $size = DocumentManager::getTotalFolderSize($path, $isAllowedToEdit);
  14. echo format_file_size($size);
  15. break;
  16. case 'get_document_quota':
  17. // Getting the course quota
  18. $courseQuota = DocumentManager::get_course_quota();
  19. // Calculating the total space
  20. $total = DocumentManager::documents_total_space(api_get_course_int_id());
  21. // Displaying the quota
  22. echo DocumentManager::displaySimpleQuota($courseQuota, $total);
  23. break;
  24. case 'upload_file':
  25. api_protect_course_script(true);
  26. // User access same as upload.php
  27. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  28. $sessionId = api_get_session_id();
  29. if (!$is_allowed_to_edit && $sessionId && $_REQUEST['curdirpath'] == "/basic-course-documents__{$sessionId}__0") {
  30. $session = SessionManager::fetch($sessionId);
  31. if (!empty($session) && $session['session_admin_id'] == api_get_user_id()) {
  32. $is_allowed_to_edit = true;
  33. }
  34. }
  35. // This needs cleaning!
  36. if (api_get_group_id()) {
  37. $groupInfo = GroupManager::get_group_properties(api_get_group_id());
  38. // Only course admin or group members allowed
  39. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $groupInfo)) {
  40. if (!GroupManager::allowUploadEditDocument(api_get_user_id(), api_get_course_int_id(), $groupInfo)) {
  41. exit;
  42. }
  43. } else {
  44. exit;
  45. }
  46. } elseif ($is_allowed_to_edit ||
  47. DocumentManager::is_my_shared_folder(api_get_user_id(), $_REQUEST['curdirpath'], api_get_session_id())
  48. ) {
  49. // ??
  50. } else {
  51. // No course admin and no group member...
  52. exit;
  53. }
  54. $directoryParentId = isset($_POST['directory_parent_id']) ? $_POST['directory_parent_id'] : 0;
  55. $currentDirectory = '';
  56. if (empty($directoryParentId)) {
  57. $currentDirectory = isset($_REQUEST['curdirpath']) ? $_REQUEST['curdirpath'] : '';
  58. } else {
  59. $documentData = DocumentManager::get_document_data_by_id($directoryParentId, api_get_course_id());
  60. if ($documentData) {
  61. $currentDirectory = $documentData['path'];
  62. }
  63. }
  64. $ifExists = isset($_POST['if_exists']) ? $_POST['if_exists'] : '';
  65. $unzip = isset($_POST['unzip']) ? 1 : 0;
  66. if (empty($ifExists)) {
  67. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  68. $defaultFileExistsOption = 'rename';
  69. if (!empty($fileExistsOption)) {
  70. $defaultFileExistsOption = $fileExistsOption;
  71. }
  72. } else {
  73. $defaultFileExistsOption = $ifExists;
  74. }
  75. if (!empty($_FILES)) {
  76. $files = $_FILES['files'];
  77. $fileList = [];
  78. foreach ($files as $name => $array) {
  79. $counter = 0;
  80. foreach ($array as $data) {
  81. $fileList[$counter][$name] = $data;
  82. $counter++;
  83. }
  84. }
  85. $resultList = [];
  86. foreach ($fileList as $file) {
  87. $globalFile = [];
  88. $globalFile['files'] = $file;
  89. $result = DocumentManager::upload_document(
  90. $globalFile,
  91. $currentDirectory,
  92. '',
  93. '', // comment
  94. $unzip,
  95. $defaultFileExistsOption,
  96. false,
  97. false,
  98. 'files'
  99. );
  100. $json = [];
  101. if (!empty($result) && is_array($result)) {
  102. $json['name'] = api_htmlentities($result['title']);
  103. $json['link'] = Display::url(
  104. api_htmlentities($result['title']),
  105. api_htmlentities($result['url']),
  106. ['target' => '_blank']
  107. );
  108. $json['url'] = $result['url'];
  109. $json['size'] = format_file_size($file['size']);
  110. $json['type'] = api_htmlentities($file['type']);
  111. $json['result'] = Display::return_icon(
  112. 'accept.png',
  113. get_lang('Uploaded')
  114. );
  115. } else {
  116. $json['name'] = isset($file['name']) ? $file['name'] : get_lang('Unknown');
  117. $json['url'] = '';
  118. $json['error'] = get_lang('Error');
  119. }
  120. $resultList[] = $json;
  121. }
  122. echo json_encode(['files' => $resultList]);
  123. }
  124. exit;
  125. break;
  126. case 'document_preview':
  127. $courseInfo = api_get_course_info_by_id($_REQUEST['course_id']);
  128. if (!empty($courseInfo) && is_array($courseInfo)) {
  129. echo DocumentManager::get_document_preview(
  130. $courseInfo,
  131. false,
  132. '_blank',
  133. $_REQUEST['session_id']
  134. );
  135. }
  136. break;
  137. case 'document_destination':
  138. //obtained the bootstrap-select selected value via ajax
  139. $dirValue = isset($_POST['dirValue']) ? $_POST['dirValue'] : null;
  140. echo Security::remove_XSS($dirValue);
  141. break;
  142. }
  143. exit;