document.ajax.php 4.3 KB

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