document.ajax.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for the document upload
  5. */
  6. $action = $_REQUEST['a'];
  7. switch($action) {
  8. case 'upload_file':
  9. api_protect_course_script(true);
  10. //User access same as upload.php
  11. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  12. // This needs cleaning!
  13. if (api_get_group_id()) {
  14. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id())) { // Only courseadmin or group members allowed
  15. } else {
  16. exit;
  17. }
  18. } elseif ($is_allowed_to_edit || is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id())) {
  19. } else { // No course admin and no group member...
  20. exit;
  21. }
  22. if (!empty($_FILES)) {
  23. $file = $_FILES['file'];
  24. $result = DocumentManager::upload_document($_FILES, $_POST['curdirpath'], $file['name'], null, 0, 'overwrite', false, false);
  25. $json = array();
  26. $json['name'] = Display::url(api_htmlentities($file['name']), api_htmlentities($result['url']), array('target'=>'_blank'));
  27. $json['type'] = api_htmlentities($file['type']);
  28. $json['size'] = Text::format_file_size($file['size']);
  29. if (!empty($result) && is_array($result)) {
  30. $json['result'] = Display::return_icon('accept.png', get_lang('Uploaded'));
  31. } else {
  32. $json['result'] = Display::return_icon('exclamation.png', get_lang('Error'));
  33. }
  34. echo json_encode($json);
  35. }
  36. break;
  37. case 'document_preview':
  38. $course_info = api_get_course_info_by_id($_REQUEST['course_id']);
  39. if (!empty($course_info) && is_array($course_info)) {
  40. echo DocumentManager::get_document_preview($course_info, false, '_blank', $_REQUEST['session_id']);
  41. }
  42. break;
  43. }
  44. exit;