MDL-30459 when one filename already exist we bypass the execution and continue. We...
[moodle.git] / webservice / upload.php
CommitLineData
42b2809f
DC
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/**
f25aee32 18 * Accept uploading files by web service token
bba1338a 19 *
f25aee32
JM
20 * POST params:
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 ...
bba1338a 25 *
42b2809f
DC
26 * @package moodlecore
27 * @subpackage files
28 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31
32define('AJAX_SCRIPT', true);
33define('NO_MOODLE_COOKIES', true);
34require_once(dirname(dirname(__FILE__)) . '/config.php');
07cc3d11 35require_once($CFG->dirroot . '/webservice/lib.php');
42b2809f
DC
36$filepath = optional_param('filepath', '/', PARAM_PATH);
37
38echo $OUTPUT->header();
39
07cc3d11
JM
40//authenticate the user
41$token = required_param('token', PARAM_ALPHANUM);
42$webservicelib = new webservice();
43$authenticationinfo = $webservicelib->authenticate_user($token);
42b2809f 44
07cc3d11 45//check the user can manage his own files (can upload)
42b2809f
DC
46$context = get_context_instance(CONTEXT_USER, $USER->id);
47require_capability('moodle/user:manageownfiles', $context);
48
49$fs = get_file_storage();
50
7515e1ba
DC
51$totalsize = 0;
52$files = array();
53foreach ($_FILES as $fieldname=>$uploaded_file) {
42b2809f
DC
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');
59 break;
60 case UPLOAD_ERR_FORM_SIZE:
61 throw new moodle_exception('upload_error_form_size', 'repository_upload');
62 break;
63 case UPLOAD_ERR_PARTIAL:
64 throw new moodle_exception('upload_error_partial', 'repository_upload');
65 break;
66 case UPLOAD_ERR_NO_FILE:
67 throw new moodle_exception('upload_error_no_file', 'repository_upload');
68 break;
69 case UPLOAD_ERR_NO_TMP_DIR:
70 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
71 break;
72 case UPLOAD_ERR_CANT_WRITE:
73 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
74 break;
75 case UPLOAD_ERR_EXTENSION:
76 throw new moodle_exception('upload_error_extension', 'repository_upload');
77 break;
78 default:
79 throw new moodle_exception('nofile');
80 }
81 }
7515e1ba
DC
82 $file = new stdClass();
83 $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
84 // check system maxbytes setting
85 if (($_FILES[$fieldname]['size'] > $CFG->maxbytes)) {
86 // oversize file will be ignored, error added to array to notify
87 // web service client
88 $file->error = get_string('maxbytes', 'error');
89 } else {
90 $file->filepath = $_FILES[$fieldname]['tmp_name'];
91 // calculate total size of upload
92 $totalsize += $_FILES[$fieldname]['size'];
93 }
94 $files[] = $file;
95}
96
97$fs = get_file_storage();
98
99$usedspace = 0;
100$privatefiles = $fs->get_area_files($context->id, 'user', 'private', false, 'id', false);
101foreach ($privatefiles as $file) {
102 $usedspace += $file->get_filesize();
103}
104
105if ($totalsize > ($CFG->userquota - $usedspace)) {
106 throw new file_exception('userquotalimit');
107}
108
109$results = array();
110foreach ($files as $file) {
111 if (!empty($file->error)) {
112 // including error and filename
113 $results[] = $file;
114 continue;
115 }
42b2809f
DC
116 $file_record = new stdClass;
117 $file_record->component = 'user';
118 $file_record->contextid = $context->id;
119 $file_record->userid = $USER->id;
120 $file_record->filearea = 'private';
7515e1ba 121 $file_record->filename = $file->filename;
42b2809f
DC
122 $file_record->filepath = $filepath;
123 $file_record->itemid = 0;
124 $file_record->license = $CFG->sitedefaultlicense;
07cc3d11 125 $file_record->author = fullname($authenticationinfo['user']);;
42b2809f 126 $file_record->source = '';
bba1338a 127
f25aee32 128 //Check if the file already exist
bba1338a 129 $existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
f25aee32
JM
130 $file_record->itemid, $file_record->filepath, $file_record->filename);
131 if ($existingfile) {
132 //if allow automatic rename (avoid)
264764e2
JM
133 $fileerror = new stdClass();
134 $fileerror->filename = $file->filename;
135 $fileerror->errortype = 'filenameexist';
136 $fileerror->errormsg = get_string('filenameexist', 'webservice', $file->filename);
137 $results[] = $fileerror;
138 } else {
139 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
140 $results[] = $file_record;
f25aee32 141 }
42b2809f 142}
7515e1ba 143echo json_encode($results);