upload.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Accept uploading files by web service token to the user draft file area.
  18. *
  19. * POST params:
  20. * token => the web service user token (needed for authentication)
  21. * filepath => file path (where files will be stored)
  22. * [_FILES] => for example you can send the files with <input type=file>,
  23. * or with curl magic: 'file_1' => '@/path/to/file', or ...
  24. * itemid => The draftid - this can be used to add a list of files
  25. * to a draft area in separate requests. If it is 0, a new draftid will be generated.
  26. *
  27. * @package core_webservice
  28. * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. /**
  32. * AJAX_SCRIPT - exception will be converted into JSON
  33. */
  34. define('AJAX_SCRIPT', true);
  35. /**
  36. * NO_MOODLE_COOKIES - we don't want any cookie
  37. */
  38. define('NO_MOODLE_COOKIES', true);
  39. require_once(__DIR__ . '/../config.php');
  40. require_once($CFG->dirroot . '/webservice/lib.php');
  41. $filepath = optional_param('filepath', '/', PARAM_PATH);
  42. $itemid = optional_param('itemid', 0, PARAM_INT);
  43. echo $OUTPUT->header();
  44. // authenticate the user
  45. $token = required_param('token', PARAM_ALPHANUM);
  46. $webservicelib = new webservice();
  47. $authenticationinfo = $webservicelib->authenticate_user($token);
  48. $fileuploaddisabled = empty($authenticationinfo['service']->uploadfiles);
  49. if ($fileuploaddisabled) {
  50. throw new webservice_access_exception('Web service file upload must be enabled in external service settings');
  51. }
  52. $context = context_user::instance($USER->id);
  53. $fs = get_file_storage();
  54. $totalsize = 0;
  55. $files = array();
  56. foreach ($_FILES as $fieldname=>$uploaded_file) {
  57. // check upload errors
  58. if (!empty($_FILES[$fieldname]['error'])) {
  59. switch ($_FILES[$fieldname]['error']) {
  60. case UPLOAD_ERR_INI_SIZE:
  61. throw new moodle_exception('upload_error_ini_size', 'repository_upload');
  62. break;
  63. case UPLOAD_ERR_FORM_SIZE:
  64. throw new moodle_exception('upload_error_form_size', 'repository_upload');
  65. break;
  66. case UPLOAD_ERR_PARTIAL:
  67. throw new moodle_exception('upload_error_partial', 'repository_upload');
  68. break;
  69. case UPLOAD_ERR_NO_FILE:
  70. throw new moodle_exception('upload_error_no_file', 'repository_upload');
  71. break;
  72. case UPLOAD_ERR_NO_TMP_DIR:
  73. throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
  74. break;
  75. case UPLOAD_ERR_CANT_WRITE:
  76. throw new moodle_exception('upload_error_cant_write', 'repository_upload');
  77. break;
  78. case UPLOAD_ERR_EXTENSION:
  79. throw new moodle_exception('upload_error_extension', 'repository_upload');
  80. break;
  81. default:
  82. throw new moodle_exception('nofile');
  83. }
  84. }
  85. // Scan for viruses.
  86. \core\antivirus\manager::scan_file($_FILES[$fieldname]['tmp_name'], $_FILES[$fieldname]['name'], true);
  87. $file = new stdClass();
  88. $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
  89. // check system maxbytes setting
  90. if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
  91. // oversize file will be ignored, error added to array to notify
  92. // web service client
  93. $file->errortype = 'fileoversized';
  94. $file->error = get_string('maxbytes', 'error');
  95. } else {
  96. $file->filepath = $_FILES[$fieldname]['tmp_name'];
  97. // calculate total size of upload
  98. $totalsize += $_FILES[$fieldname]['size'];
  99. }
  100. $files[] = $file;
  101. }
  102. $fs = get_file_storage();
  103. if ($itemid <= 0) {
  104. $itemid = file_get_unused_draft_itemid();
  105. }
  106. // Get any existing file size limits.
  107. $maxupload = get_user_max_upload_file_size($context, $CFG->maxbytes);
  108. // Check the size of this upload.
  109. if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload) {
  110. throw new file_exception('userquotalimit');
  111. }
  112. $results = array();
  113. foreach ($files as $file) {
  114. if (!empty($file->error)) {
  115. // including error and filename
  116. $results[] = $file;
  117. continue;
  118. }
  119. $file_record = new stdClass;
  120. $file_record->component = 'user';
  121. $file_record->contextid = $context->id;
  122. $file_record->userid = $USER->id;
  123. $file_record->filearea = 'draft';
  124. $file_record->filename = $file->filename;
  125. $file_record->filepath = $filepath;
  126. $file_record->itemid = $itemid;
  127. $file_record->license = $CFG->sitedefaultlicense;
  128. $file_record->author = fullname($authenticationinfo['user']);
  129. $file_record->source = serialize((object)array('source' => $file->filename));
  130. //Check if the file already exist
  131. $existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
  132. $file_record->itemid, $file_record->filepath, $file_record->filename);
  133. if ($existingfile) {
  134. $file->errortype = 'filenameexist';
  135. $file->error = get_string('filenameexist', 'webservice', $file->filename);
  136. $results[] = $file;
  137. } else {
  138. $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
  139. $results[] = $file_record;
  140. }
  141. }
  142. echo json_encode($results);