dropbox.ajax.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for the document upload.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. require_once api_get_path(SYS_CODE_PATH).'dropbox/dropbox_functions.inc.php';
  8. $action = $_REQUEST['a'];
  9. switch ($action) {
  10. case 'upload_file':
  11. api_protect_course_script(true);
  12. // User access same as upload.php
  13. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  14. $recipients = isset($_POST['recipients']) ? $_POST['recipients'] : '';
  15. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  16. if (empty($recipients) && empty($id)) {
  17. $resultList[] = ['error' => get_lang('You must select at least one destinee')];
  18. echo json_encode(['files' => $resultList]);
  19. exit;
  20. }
  21. $work = null;
  22. if (!empty($id)) {
  23. $work = new Dropbox_SentWork($id);
  24. if (empty($work)) {
  25. $resultList[] = ['error' => get_lang('Error')];
  26. echo json_encode(['files' => $resultList]);
  27. exit;
  28. }
  29. }
  30. if (!empty($_FILES)) {
  31. $files = $_FILES['files'];
  32. $fileList = [];
  33. foreach ($files as $name => $array) {
  34. $counter = 0;
  35. foreach ($array as $data) {
  36. $fileList[$counter][$name] = $data;
  37. $counter++;
  38. }
  39. }
  40. $resultList = [];
  41. foreach ($fileList as $file) {
  42. $globalFile = [];
  43. $globalFile['files'] = $file;
  44. /** @var Dropbox_SentWork $result */
  45. $result = store_add_dropbox($file, $work);
  46. $json = [];
  47. if (!empty($result)) {
  48. $json['name'] = Display::url(
  49. api_htmlentities($result->title),
  50. api_htmlentities(api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq()),
  51. ['target' => '_blank']
  52. );
  53. $json['url'] = api_get_path(WEB_CODE_PATH).'dropbox/index.php?'.api_get_cidreq();
  54. $json['size'] = format_file_size($result->filesize);
  55. $json['type'] = api_htmlentities($file['type']);
  56. $json['result'] = Display::return_icon(
  57. 'accept.png',
  58. get_lang('Uploaded.')
  59. );
  60. } else {
  61. $json['result'] = Display::return_icon(
  62. 'exclamation.png',
  63. get_lang('Error')
  64. );
  65. }
  66. $resultList[] = $json;
  67. }
  68. echo json_encode(['files' => $resultList]);
  69. }
  70. exit;
  71. break;
  72. }
  73. exit;