document.ajax.php 5.8 KB

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