dropbox.ajax.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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('YouMustSelectAtLeastOneDestinee')];
  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;