2 // This file is part of Moodle - http://moodle.org/
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.
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 * Accept uploading files by web service token
21 * token => the web service user token (needed for authentication)
22 * filepath => the private file aera path (where files will be stored)
23 * [_FILES] => for example you can send the files with <input type=file>,
24 * or with curl magic: 'file_1' => '@/path/to/file', or ...
28 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 define('AJAX_SCRIPT', true);
33 define('NO_MOODLE_COOKIES', true);
34 require_once(dirname(dirname(__FILE__
)) . '/config.php');
35 require_once($CFG->dirroot
. '/webservice/lib.php');
36 $filepath = optional_param('filepath', '/', PARAM_PATH
);
38 echo $OUTPUT->header();
40 //authenticate the user
41 $token = required_param('token', PARAM_ALPHANUM
);
42 $webservicelib = new webservice();
43 $authenticationinfo = $webservicelib->authenticate_user($token);
45 //check the user can manage his own files (can upload)
46 $context = get_context_instance(CONTEXT_USER
, $USER->id
);
47 require_capability('moodle/user:manageownfiles', $context);
49 $fs = get_file_storage();
53 foreach ($_FILES as $fieldname=>$uploaded_file) {
54 // check upload errors
55 if (!empty($_FILES[$fieldname]['error'])) {
56 switch ($_FILES[$fieldname]['error']) {
57 case UPLOAD_ERR_INI_SIZE
:
58 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
60 case UPLOAD_ERR_FORM_SIZE
:
61 throw new moodle_exception('upload_error_form_size', 'repository_upload');
63 case UPLOAD_ERR_PARTIAL
:
64 throw new moodle_exception('upload_error_partial', 'repository_upload');
66 case UPLOAD_ERR_NO_FILE
:
67 throw new moodle_exception('upload_error_no_file', 'repository_upload');
69 case UPLOAD_ERR_NO_TMP_DIR
:
70 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
72 case UPLOAD_ERR_CANT_WRITE
:
73 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
75 case UPLOAD_ERR_EXTENSION
:
76 throw new moodle_exception('upload_error_extension', 'repository_upload');
79 throw new moodle_exception('nofile');
82 $file = new stdClass();
83 $file->filename
= clean_param($_FILES[$fieldname]['name'], PARAM_FILE
);
84 // check system maxbytes setting
85 if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes
))) {
86 // oversize file will be ignored, error added to array to notify
88 $file->errortype
= 'fileoversized';
89 $file->error
= get_string('maxbytes', 'error');
91 $file->filepath
= $_FILES[$fieldname]['tmp_name'];
92 // calculate total size of upload
93 $totalsize +
= $_FILES[$fieldname]['size'];
98 $fs = get_file_storage();
101 $privatefiles = $fs->get_area_files($context->id
, 'user', 'private', false, 'id', false);
102 foreach ($privatefiles as $file) {
103 $usedspace +
= $file->get_filesize();
106 if ($totalsize > ($CFG->userquota
- $usedspace)) {
107 throw new file_exception('userquotalimit');
111 foreach ($files as $file) {
112 if (!empty($file->error
)) {
113 // including error and filename
117 $file_record = new stdClass
;
118 $file_record->component
= 'user';
119 $file_record->contextid
= $context->id
;
120 $file_record->userid
= $USER->id
;
121 $file_record->filearea
= 'private';
122 $file_record->filename
= $file->filename
;
123 $file_record->filepath
= $filepath;
124 $file_record->itemid
= 0;
125 $file_record->license
= $CFG->sitedefaultlicense
;
126 $file_record->author
= fullname($authenticationinfo['user']);;
127 $file_record->source
= '';
129 //Check if the file already exist
130 $existingfile = $fs->file_exists($file_record->contextid
, $file_record->component
, $file_record->filearea
,
131 $file_record->itemid
, $file_record->filepath
, $file_record->filename
);
133 $file->errortype
= 'filenameexist';
134 $file->error
= get_string('filenameexist', 'webservice', $file->filename
);
137 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath
);
138 $results[] = $file_record;
141 echo json_encode($results);