MDL-55994 blog: Resolve warning messages in RSS feed generation
[moodle.git] / webservice / upload.php
blob1550ee5ffd898f5eb85a77d3b70f00728d4f6957
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.
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/>.
18 /**
19 * Accept uploading files by web service token to the user draft file area.
21 * POST params:
22 * token => the web service user token (needed for authentication)
23 * filepath => file path (where files will be stored)
24 * [_FILES] => for example you can send the files with <input type=file>,
25 * or with curl magic: 'file_1' => '@/path/to/file', or ...
26 * itemid => The draftid - this can be used to add a list of files
27 * to a draft area in separate requests. If it is 0, a new draftid will be generated.
29 * @package core_webservice
30 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 /**
35 * AJAX_SCRIPT - exception will be converted into JSON
37 define('AJAX_SCRIPT', true);
39 /**
40 * NO_MOODLE_COOKIES - we don't want any cookie
42 define('NO_MOODLE_COOKIES', true);
44 require_once(__DIR__ . '/../config.php');
45 require_once($CFG->dirroot . '/webservice/lib.php');
46 $filepath = optional_param('filepath', '/', PARAM_PATH);
47 $itemid = optional_param('itemid', 0, PARAM_INT);
49 echo $OUTPUT->header();
51 // authenticate the user
52 $token = required_param('token', PARAM_ALPHANUM);
53 $webservicelib = new webservice();
54 $authenticationinfo = $webservicelib->authenticate_user($token);
55 $fileuploaddisabled = empty($authenticationinfo['service']->uploadfiles);
56 if ($fileuploaddisabled) {
57 throw new webservice_access_exception('Web service file upload must be enabled in external service settings');
60 $context = context_user::instance($USER->id);
62 $fs = get_file_storage();
64 $totalsize = 0;
65 $files = array();
66 foreach ($_FILES as $fieldname=>$uploaded_file) {
67 // check upload errors
68 if (!empty($_FILES[$fieldname]['error'])) {
69 switch ($_FILES[$fieldname]['error']) {
70 case UPLOAD_ERR_INI_SIZE:
71 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
72 break;
73 case UPLOAD_ERR_FORM_SIZE:
74 throw new moodle_exception('upload_error_form_size', 'repository_upload');
75 break;
76 case UPLOAD_ERR_PARTIAL:
77 throw new moodle_exception('upload_error_partial', 'repository_upload');
78 break;
79 case UPLOAD_ERR_NO_FILE:
80 throw new moodle_exception('upload_error_no_file', 'repository_upload');
81 break;
82 case UPLOAD_ERR_NO_TMP_DIR:
83 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
84 break;
85 case UPLOAD_ERR_CANT_WRITE:
86 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
87 break;
88 case UPLOAD_ERR_EXTENSION:
89 throw new moodle_exception('upload_error_extension', 'repository_upload');
90 break;
91 default:
92 throw new moodle_exception('nofile');
96 // Scan for viruses.
97 \core\antivirus\manager::scan_file($_FILES[$fieldname]['tmp_name'], $_FILES[$fieldname]['name'], true);
99 $file = new stdClass();
100 $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
101 // check system maxbytes setting
102 if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
103 // oversize file will be ignored, error added to array to notify
104 // web service client
105 $file->errortype = 'fileoversized';
106 $file->error = get_string('maxbytes', 'error');
107 } else {
108 $file->filepath = $_FILES[$fieldname]['tmp_name'];
109 // calculate total size of upload
110 $totalsize += $_FILES[$fieldname]['size'];
112 $files[] = $file;
115 $fs = get_file_storage();
117 if ($itemid <= 0) {
118 $itemid = file_get_unused_draft_itemid();
121 // Get any existing file size limits.
122 $maxupload = get_user_max_upload_file_size($context, $CFG->maxbytes);
124 // Check the size of this upload.
125 if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload) {
126 throw new file_exception('userquotalimit');
129 $results = array();
130 foreach ($files as $file) {
131 if (!empty($file->error)) {
132 // including error and filename
133 $results[] = $file;
134 continue;
136 $file_record = new stdClass;
137 $file_record->component = 'user';
138 $file_record->contextid = $context->id;
139 $file_record->userid = $USER->id;
140 $file_record->filearea = 'draft';
141 $file_record->filename = $file->filename;
142 $file_record->filepath = $filepath;
143 $file_record->itemid = $itemid;
144 $file_record->license = $CFG->sitedefaultlicense;
145 $file_record->author = fullname($authenticationinfo['user']);
146 $file_record->source = serialize((object)array('source' => $file->filename));
148 //Check if the file already exist
149 $existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
150 $file_record->itemid, $file_record->filepath, $file_record->filename);
151 if ($existingfile) {
152 $file->errortype = 'filenameexist';
153 $file->error = get_string('filenameexist', 'webservice', $file->filename);
154 $results[] = $file;
155 } else {
156 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
157 $results[] = $file_record;
160 echo json_encode($results);