document.ajax.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = get_total_folder_size($path, $isAllowedToEdit);
  14. echo format_file_size($size);
  15. break;
  16. case 'get_document_quota':
  17. // Getting the course quota
  18. $course_quota = DocumentManager::get_course_quota();
  19. // Calculating the total space
  20. $already_consumed_space_course = DocumentManager::documents_total_space(
  21. api_get_course_int_id()
  22. );
  23. // Displaying the quota
  24. echo DocumentManager::display_simple_quota(
  25. $course_quota,
  26. $already_consumed_space_course
  27. );
  28. break;
  29. case 'upload_file':
  30. api_protect_course_script(true);
  31. // User access same as upload.php
  32. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  33. // This needs cleaning!
  34. if (api_get_group_id()) {
  35. $groupInfo = GroupManager::get_group_properties(api_get_group_id());
  36. // Only course admin or group members allowed
  37. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), $groupInfo)) {
  38. } else {
  39. exit;
  40. }
  41. } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id())) {
  42. // ??
  43. } else {
  44. // No course admin and no group member...
  45. exit;
  46. }
  47. $directoryParentId = isset($_POST['directory_parent_id']) ? $_POST['directory_parent_id'] : 0;
  48. $currentDirectory = '';
  49. if (empty($directoryParentId)) {
  50. $currentDirectory = isset($_REQUEST['curdirpath']) ? $_REQUEST['curdirpath'] : '';
  51. } else {
  52. $documentData = DocumentManager::get_document_data_by_id($directoryParentId, api_get_course_id());
  53. if ($documentData) {
  54. $currentDirectory = $documentData['path'];
  55. }
  56. }
  57. $ifExists = isset($_POST['if_exists']) ? $_POST['if_exists'] : '';
  58. $unzip = isset($_POST['unzip']) ? 1 : 0;
  59. if (empty($ifExists)) {
  60. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  61. $defaultFileExistsOption = 'rename';
  62. if (!empty($fileExistsOption)) {
  63. $defaultFileExistsOption = $fileExistsOption;
  64. }
  65. } else {
  66. $defaultFileExistsOption = $ifExists;
  67. }
  68. if (!empty($_FILES)) {
  69. $files = $_FILES['files'];
  70. $fileList = [];
  71. foreach ($files as $name => $array) {
  72. $counter = 0;
  73. foreach ($array as $data) {
  74. $fileList[$counter][$name] = $data;
  75. $counter++;
  76. }
  77. }
  78. $resultList = [];
  79. foreach ($fileList as $file) {
  80. $globalFile = [];
  81. $globalFile['files'] = $file;
  82. $result = DocumentManager::upload_document(
  83. $globalFile,
  84. $currentDirectory,
  85. $file['name'],
  86. '', // comment
  87. $unzip,
  88. $defaultFileExistsOption,
  89. false,
  90. false,
  91. 'files'
  92. );
  93. $json = array();
  94. if (!empty($result) && is_array($result)) {
  95. $json['name'] = Display::url(
  96. api_htmlentities($result['title']),
  97. api_htmlentities($result['url']),
  98. array('target'=>'_blank')
  99. );
  100. $json['url'] = $result['url'];
  101. $json['size'] = format_file_size($file['size']);
  102. $json['type'] = api_htmlentities($file['type']);
  103. $json['result'] = Display::return_icon(
  104. 'accept.png',
  105. get_lang('Uploaded')
  106. );
  107. } else {
  108. $json['url'] = '';
  109. $json['error'] = get_lang('Error');
  110. }
  111. $resultList[] = $json;
  112. }
  113. echo json_encode(['files' => $resultList]);
  114. }
  115. exit;
  116. break;
  117. case 'document_preview':
  118. $course_info = api_get_course_info_by_id($_REQUEST['course_id']);
  119. if (!empty($course_info) && is_array($course_info)) {
  120. echo DocumentManager::get_document_preview(
  121. $course_info,
  122. false,
  123. '_blank',
  124. $_REQUEST['session_id']
  125. );
  126. }
  127. break;
  128. case 'document_destination':
  129. //obtained the bootstrap-select selected value via ajax
  130. $dirValue = isset($_POST['dirValue']) ? $_POST['dirValue'] : null;
  131. echo Security::remove_XSS($dirValue);
  132. break;
  133. }
  134. exit;