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 * Accpet uploading files by web service token
21 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('AJAX_SCRIPT', true);
26 define('NO_MOODLE_COOKIES', true);
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 $token = required_param('token', PARAM_ALPHANUM);
29 $filepath = optional_param('filepath', '/', PARAM_PATH);
31 echo $OUTPUT->header();
33 // web service must be enabled to use this script
34 if (!$CFG->enablewebservices) {
35 throw new moodle_exception('enablewsdescription', 'webservice');
37 // Obtain token record
38 if (!$token = $DB->get_record('external_tokens', array('token'=>$token))) {
39 throw new webservice_access_exception(get_string('invalidtoken', 'webservice'));
42 // Validate token date
43 if ($token->validuntil and $token->validuntil < time()) {
44 add_to_log(SITEID, 'webservice', get_string('tokenauthlog', 'webservice'), '' , get_string('invalidtimedtoken', 'webservice'), 0);
45 $DB->delete_records('external_tokens', array('token'=>$token->token));
46 throw new webservice_access_exception(get_string('invalidtimedtoken', 'webservice'));
49 //assumes that if sid is set then there must be a valid associated session no matter the token type
51 $session = session_get_instance();
52 if (!$session->session_exists($token->sid)) {
53 $DB->delete_records('external_tokens', array('sid'=>$token->sid));
54 throw new webservice_access_exception(get_string('invalidtokensession', 'webservice'));
59 if ($token->iprestriction and !address_in_subnet(getremoteaddr(), $token->iprestriction)) {
60 add_to_log(SITEID, 'webservice', get_string('tokenauthlog', 'webservice'), '' , get_string('failedtolog', 'webservice').": ".getremoteaddr(), 0);
61 throw new webservice_access_exception(get_string('invalidiptoken', 'webservice'));
64 $user = $DB->get_record('user', array('id'=>$token->userid, 'deleted'=>0), '*', MUST_EXIST);
67 $DB->set_field('external_tokens', 'lastaccess', time(), array('id'=>$token->id));
69 session_set_user($user);
70 $context = get_context_instance(CONTEXT_USER, $USER->id);
71 require_capability('moodle/user:manageownfiles', $context);
73 $fs = get_file_storage();
77 foreach ($_FILES as $fieldname=>$uploaded_file) {
78 // check upload errors
79 if (!empty($_FILES[$fieldname]['error'])) {
80 switch ($_FILES[$fieldname]['error']) {
81 case UPLOAD_ERR_INI_SIZE:
82 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
84 case UPLOAD_ERR_FORM_SIZE:
85 throw new moodle_exception('upload_error_form_size', 'repository_upload');
87 case UPLOAD_ERR_PARTIAL:
88 throw new moodle_exception('upload_error_partial', 'repository_upload');
90 case UPLOAD_ERR_NO_FILE:
91 throw new moodle_exception('upload_error_no_file', 'repository_upload');
93 case UPLOAD_ERR_NO_TMP_DIR:
94 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
96 case UPLOAD_ERR_CANT_WRITE:
97 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
99 case UPLOAD_ERR_EXTENSION:
100 throw new moodle_exception('upload_error_extension', 'repository_upload');
103 throw new moodle_exception('nofile');
106 $file = new stdClass();
107 $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
108 // check system maxbytes setting
109 if (($_FILES[$fieldname]['size'] > $CFG->maxbytes)) {
110 // oversize file will be ignored, error added to array to notify
111 // web service client
112 $file->error = get_string('maxbytes', 'error');
114 $file->filepath = $_FILES[$fieldname]['tmp_name'];
115 // calculate total size of upload
116 $totalsize += $_FILES[$fieldname]['size'];
121 $fs = get_file_storage();
124 $privatefiles = $fs->get_area_files($context->id, 'user', 'private', false, 'id', false);
125 foreach ($privatefiles as $file) {
126 $usedspace += $file->get_filesize();
129 if ($totalsize > ($CFG->userquota - $usedspace)) {
130 throw new file_exception('userquotalimit');
134 foreach ($files as $file) {
135 if (!empty($file->error)) {
136 // including error and filename
140 $file_record = new stdClass;
141 $file_record->component = 'user';
142 $file_record->contextid = $context->id;
143 $file_record->userid = $USER->id;
144 $file_record->filearea = 'private';
145 $file_record->filename = $file->filename;
146 $file_record->filepath = $filepath;
147 $file_record->itemid = 0;
148 $file_record->license = $CFG->sitedefaultlicense;
149 $file_record->author = fullname($user);;
150 $file_record->source = '';
151 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
152 $results[] = $file_record;
154 echo json_encode($results);