document.ajax.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id())) { // Only courseadmin or group members allowed
  16. } else {
  17. exit;
  18. }
  19. } elseif ($is_allowed_to_edit || is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id())) {
  20. } else { // No course admin and no group member...
  21. exit;
  22. }
  23. if (!empty($_FILES)) {
  24. $file = $_FILES['file'];
  25. $result = DocumentManager::upload_document($_FILES, $_POST['curdirpath'], $file['name'], null, 0, 'overwrite', false, false);
  26. $json = array();
  27. $json['name'] = Display::url(api_htmlentities($file['name']), api_htmlentities($result['url']), array('target'=>'_blank'));
  28. $json['type'] = api_htmlentities($file['type']);
  29. $json['size'] = Text::format_file_size($file['size']);
  30. if (!empty($result) && is_array($result)) {
  31. $json['result'] = Display::return_icon('accept.png', get_lang('Uploaded'));
  32. } else {
  33. $json['result'] = Display::return_icon('exclamation.png', get_lang('Error'));
  34. }
  35. echo json_encode($json);
  36. }
  37. break;
  38. case 'document_preview':
  39. $course_info = api_get_course_info_by_id($_REQUEST['course_id']);
  40. if (!empty($course_info) && is_array($course_info)) {
  41. echo DocumentManager::get_document_preview($course_info, false, '_blank', $_REQUEST['session_id']);
  42. }
  43. break;
  44. }
  45. exit;