MDL-32508 Trivial whitespace cleanup
[moodle.git] / webservice / upload.php
blob5e2f6cc0f2f2b0724c50964e3f957ea216baddb4
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
21 * POST params:
22 * token => the web service user token (needed for authentication)
23 * filepath => the private file aera 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 ...
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
32 /**
33 * AJAX_SCRIPT - exception will be converted into JSON
35 define('AJAX_SCRIPT', true);
37 /**
38 * NO_MOODLE_COOKIES - we don't want any cookie
40 define('NO_MOODLE_COOKIES', true);
42 require_once(dirname(dirname(__FILE__)) . '/config.php');
43 require_once($CFG->dirroot . '/webservice/lib.php');
44 $filepath = optional_param('filepath', '/', PARAM_PATH);
46 echo $OUTPUT->header();
48 // authenticate the user
49 $token = required_param('token', PARAM_ALPHANUM);
50 $webservicelib = new webservice();
51 $authenticationinfo = $webservicelib->authenticate_user($token);
53 // check the user can manage his own files (can upload)
54 $context = get_context_instance(CONTEXT_USER, $USER->id);
55 require_capability('moodle/user:manageownfiles', $context);
57 $fs = get_file_storage();
59 $totalsize = 0;
60 $files = array();
61 foreach ($_FILES as $fieldname=>$uploaded_file) {
62 // check upload errors
63 if (!empty($_FILES[$fieldname]['error'])) {
64 switch ($_FILES[$fieldname]['error']) {
65 case UPLOAD_ERR_INI_SIZE:
66 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
67 break;
68 case UPLOAD_ERR_FORM_SIZE:
69 throw new moodle_exception('upload_error_form_size', 'repository_upload');
70 break;
71 case UPLOAD_ERR_PARTIAL:
72 throw new moodle_exception('upload_error_partial', 'repository_upload');
73 break;
74 case UPLOAD_ERR_NO_FILE:
75 throw new moodle_exception('upload_error_no_file', 'repository_upload');
76 break;
77 case UPLOAD_ERR_NO_TMP_DIR:
78 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
79 break;
80 case UPLOAD_ERR_CANT_WRITE:
81 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
82 break;
83 case UPLOAD_ERR_EXTENSION:
84 throw new moodle_exception('upload_error_extension', 'repository_upload');
85 break;
86 default:
87 throw new moodle_exception('nofile');
90 $file = new stdClass();
91 $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
92 // check system maxbytes setting
93 if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
94 // oversize file will be ignored, error added to array to notify
95 // web service client
96 $file->errortype = 'fileoversized';
97 $file->error = get_string('maxbytes', 'error');
98 } else {
99 $file->filepath = $_FILES[$fieldname]['tmp_name'];
100 // calculate total size of upload
101 $totalsize += $_FILES[$fieldname]['size'];
103 $files[] = $file;
106 $fs = get_file_storage();
108 $usedspace = 0;
109 $privatefiles = $fs->get_area_files($context->id, 'user', 'private', false, 'id', false);
110 foreach ($privatefiles as $file) {
111 $usedspace += $file->get_filesize();
114 if ($totalsize > ($CFG->userquota - $usedspace)) {
115 throw new file_exception('userquotalimit');
118 $results = array();
119 foreach ($files as $file) {
120 if (!empty($file->error)) {
121 // including error and filename
122 $results[] = $file;
123 continue;
125 $file_record = new stdClass;
126 $file_record->component = 'user';
127 $file_record->contextid = $context->id;
128 $file_record->userid = $USER->id;
129 $file_record->filearea = 'private';
130 $file_record->filename = $file->filename;
131 $file_record->filepath = $filepath;
132 $file_record->itemid = 0;
133 $file_record->license = $CFG->sitedefaultlicense;
134 $file_record->author = fullname($authenticationinfo['user']);;
135 $file_record->source = '';
137 //Check if the file already exist
138 $existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
139 $file_record->itemid, $file_record->filepath, $file_record->filename);
140 if ($existingfile) {
141 $file->errortype = 'filenameexist';
142 $file->error = get_string('filenameexist', 'webservice', $file->filename);
143 $results[] = $file;
144 } else {
145 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
146 $results[] = $file_record;
149 echo json_encode($results);