document.ajax.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Only course admin or group members allowed
  16. if ($is_allowed_to_edit || GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id())) {
  17. } else {
  18. exit;
  19. }
  20. } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $_POST['curdirpath'], api_get_session_id())) {
  21. } else {
  22. // No course admin and no group member...
  23. exit;
  24. }
  25. $fileExistsOption = api_get_setting('document_if_file_exists_option');
  26. $defaultFileExistsOption = 'rename';
  27. if (!empty($fileExistsOption)) {
  28. $defaultFileExistsOption = $fileExistsOption;
  29. }
  30. //$ifExists = isset($_POST['if_exists']) ? $_POST['if_exists'] : $defaultFileExistsOption;
  31. if (!empty($_FILES)) {
  32. $file = $_FILES['file'];
  33. $result = DocumentManager::upload_document(
  34. $_FILES,
  35. $_POST['curdirpath'],
  36. $file['name'],
  37. '', // comment
  38. 0,
  39. $defaultFileExistsOption,
  40. false,
  41. false
  42. );
  43. $json = array();
  44. $json['name'] = Display::url(
  45. api_htmlentities($result['title']),
  46. api_htmlentities($result['url']),
  47. array('target'=>'_blank')
  48. );
  49. $json['type'] = api_htmlentities($file['type']);
  50. $json['size'] = format_file_size($file['size']);
  51. if (!empty($result) && is_array($result)) {
  52. $json['result'] = Display::return_icon('accept.png', get_lang('Uploaded'));
  53. } else {
  54. $json['result'] = Display::return_icon('exclamation.png', get_lang('Error'));
  55. }
  56. echo json_encode($json);
  57. }
  58. break;
  59. case 'document_preview':
  60. $course_info = api_get_course_info_by_id($_REQUEST['course_id']);
  61. if (!empty($course_info) && is_array($course_info)) {
  62. echo DocumentManager::get_document_preview(
  63. $course_info,
  64. false,
  65. '_blank',
  66. $_REQUEST['session_id']
  67. );
  68. }
  69. break;
  70. }
  71. exit;