"MDL-25634 Filepicker upload should handle files which exceed max_post_size directive...
[moodle.git] / repository / repository_ajax.php
CommitLineData
aa6c1ced 1<?php
5bce5972 2
6f2cd52a
DC
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * The Web service script that is called from the filepicker front end
21 *
22 * @since 2.0
5d354ded 23 * @package core
6f2cd52a 24 * @subpackage repository
5d354ded
PS
25 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6f2cd52a 27 */
455860ce 28
e35194be
DC
29define('AJAX_SCRIPT', true);
30
14469892
DC
31require_once(dirname(dirname(__FILE__)).'/config.php');
32require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
33require_once(dirname(__FILE__).'/lib.php');
99eaca9d 34
6bdfef5d 35$err = new stdClass();
dd0566eb 36
d0f8585d 37/// Parameters
9d4ef80f 38$action = optional_param('action', '', PARAM_ALPHA);
4277133e 39$repo_id = optional_param('repo_id', 0, PARAM_INT); // Repository ID
3e123368
DC
40$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID
41$env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform
1dce6261 42$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
3e123368
DC
43$author = optional_param('author', '', PARAM_TEXT); // File author
44$source = optional_param('source', '', PARAM_RAW); // File to download
45$itemid = optional_param('itemid', 0, PARAM_INT); // Itemid
46$page = optional_param('page', '', PARAM_RAW); // Page
47$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes
48$req_path = optional_param('p', '', PARAM_RAW); // Path
e35194be 49$accepted_types = optional_param('accepted_types', '*', PARAM_RAW);
3e123368
DC
50$saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name
51$saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path
9d4ef80f
DC
52$search_text = optional_param('s', '', PARAM_CLEANHTML);
53$linkexternal = optional_param('linkexternal', '', PARAM_ALPHA);
455860ce 54
cd455965
DC
55@header('Content-type: text/plain');
56
57// if uploaded file is larger than post_max_size (php.ini) setting, $_POST content will lost
58if (empty($_POST) && !empty($action)) {
59 $err->error = get_string('errorpostmaxsize', 'repository');
60 die(json_encode($err));
61}
62
196c0451
PS
63list($context, $course, $cm) = get_context_info_array($contextid);
64require_login($course, false, $cm);
65$PAGE->set_context($context);
66
bce08d9a 67echo $OUTPUT->header(); // send headers
9d4ef80f 68
6127179b
DC
69if (!confirm_sesskey()) {
70 $err->error = get_string('invalidsesskey');
71 die(json_encode($err));
72}
73
be85f7ab
DC
74/// Get repository instance information
75$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id=? AND i.typeid=r.id';
76
77if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
78 $err->error = get_string('invalidrepositoryid', 'repository');
79 die(json_encode($err));
80} else {
81 $type = $repository->type;
82}
83
84/// Check permissions
85repository::check_capability($contextid, $repository);
86
3e123368
DC
87$moodle_maxbytes = get_max_upload_file_size();
88// to prevent maxbytes greater than moodle maxbytes setting
89if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
90 $maxbytes = $moodle_maxbytes;
91}
92
577aab9b 93/// Wait as long as it takes for this script to finish
9d4ef80f 94set_time_limit(0);
577aab9b 95
4277133e 96// Early actions which need to be done before repository instances initialised
9d4ef80f
DC
97switch ($action) {
98 // global search
99 case 'gsearch':
100 $params = array();
101 $params['context'] = array(get_context_instance_by_id($contextid), get_system_context());
102 $params['currentcontext'] = get_context_instance_by_id($contextid);
103 $repos = repository::get_instances($params);
104 $list = array();
105 foreach($repos as $repo){
106 if ($repo->global_search()) {
e35194be
DC
107 $ret = $repo->search($search_text);
108 array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below
109 $tmp = array_merge($list, $ret['list']);
110 $list = $tmp;
d0f8585d 111 }
9d4ef80f
DC
112 }
113 $listing = array('list'=>$list);
114 $listing['gsearch'] = true;
9d4ef80f
DC
115 die(json_encode($listing));
116 break;
d0f8585d 117
9d4ef80f
DC
118 // remove the cache files & logout
119 case 'ccache':
120 $cache = new curl_cache;
121 $cache->refresh();
122 $action = 'list';
123 break;
124}
d0f8585d 125
e35194be
DC
126if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
127 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
9d4ef80f 128 $classname = 'repository_' . $type;
e35194be 129 $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type));
a06878d3 130} else {
e35194be 131 $err->error = get_string('invalidplugin', 'repository', $type);
9d4ef80f 132 die(json_encode($err));
a06878d3 133}
9d4ef80f 134
d0f8585d 135/// These actions all occur on the currently active repository instance
9d4ef80f
DC
136switch ($action) {
137 case 'sign':
138 case 'signin':
139 case 'list':
140 if ($repo->check_login()) {
e35194be
DC
141 $listing = $repo->get_listing($req_path, $page);
142 $listing['repo_id'] = $repo_id;
143 echo json_encode($listing);
d0f8585d 144 break;
9d4ef80f
DC
145 } else {
146 $action = 'login';
147 }
148 case 'login':
e35194be
DC
149 $listing = $repo->print_login();
150 $listing['repo_id'] = $repo_id;
151 echo json_encode($listing);
9d4ef80f
DC
152 break;
153 case 'logout':
154 $logout = $repo->logout();
9d4ef80f
DC
155 $logout['repo_id'] = $repo_id;
156 echo json_encode($logout);
157 break;
158 case 'searchform':
e35194be 159 $search_form['form'] = $repo->print_search();
9d4ef80f
DC
160 echo json_encode($search_form);
161 break;
162 case 'search':
e35194be
DC
163 $search_result = $repo->search($search_text, (int)$page);
164 $search_result['repo_id'] = $repo_id;
29199e56 165 $search_result['issearchresult'] = true;
e35194be 166 echo json_encode($search_result);
9d4ef80f
DC
167 break;
168 case 'download':
e35194be
DC
169 // validate mimetype
170 $mimetypes = array();
2385e6de 171 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
e35194be
DC
172 $mimetypes = '*';
173 } else {
174 foreach ($accepted_types as $type) {
16f61c70 175 $mimetypes[] = mimeinfo('type', $type);
e35194be
DC
176 }
177 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
033aad78 178 throw new moodle_exception('invalidfiletype', 'repository', '', get_string(mimeinfo('type', $saveas_filename), 'mimetypes'));
e35194be
DC
179 }
180 }
181
4277133e 182 // We have two special repository type need to deal with
e35194be
DC
183 // local and recent plugins don't added new files to moodle, just add new records to database
184 // so we don't check user quota and maxbytes here
93beda04
DC
185 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
186 if (!empty($allowexternallink)) {
187 $allowexternallink = true;
188 } else {
189 $allowexternallink = false;
190 }
191 // allow external links in url element all the time
192 $allowexternallink = ($allowexternallink || ($env == 'url'));
193
194 // Use link of the files
195 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
196 // use external link
197 $link = $repo->get_link($source);
e35194be 198 $info = array();
93beda04
DC
199 $info['filename'] = $saveas_filename;
200 $info['type'] = 'link';
201 $info['url'] = $link;
e35194be 202 echo json_encode($info);
93beda04 203 die;
e35194be 204 } else {
fab3c8d6 205 if (in_array($repo->options['type'], array('local', 'recent', 'user', 'coursefiles'))) { //TODO: this hardcoding is a really ugly hack (skodak)
93beda04 206 $fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
9d4ef80f 207 $info = array();
93beda04
DC
208 $info['file'] = $fileinfo['title'];
209 $info['id'] = $itemid;
210 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
211 $filesize = $fileinfo['filesize'];
212 if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
213 throw new file_exception('maxbytes');
214 }
acb70a9b 215 echo json_encode($info);
e35194be 216 die;
93beda04
DC
217 }
218 // Download file to moodle
219 $file = $repo->get_file($source, $saveas_filename);
220 if ($file['path'] === false) {
221 $err->error = get_string('cannotdownload', 'repository');
222 die(json_encode($err));
223 }
ea1780ad 224
93beda04
DC
225 // check if exceed maxbytes
226 if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
227 throw new file_exception('maxbytes');
228 }
14469892 229
6bdfef5d 230 $record = new stdClass();
93beda04
DC
231 $record->filepath = $saveas_path;
232 $record->filename = $saveas_filename;
233 $record->component = 'user';
234 $record->filearea = 'draft';
235 $record->itemid = $itemid;
14469892 236
93beda04
DC
237 if (!empty($file['license'])) {
238 $record->license = $file['license'];
239 } else {
240 $record->license = $license;
241 }
242 if (!empty($file['author'])) {
243 $record->author = $file['author'];
244 } else {
245 $record->author = $author;
246 }
247 $record->source = !empty($file['url']) ? $file['url'] : '';
1dce6261 248
93beda04
DC
249 $info = repository::move_to_filepool($file['path'], $record);
250 if (empty($info)) {
251 $info['e'] = get_string('error', 'moodle');
9d4ef80f 252 }
93beda04
DC
253 echo json_encode($info);
254 die;
9d4ef80f
DC
255 }
256 break;
257 case 'upload':
ac1870e3
DC
258 // handle exception here instead moodle default exception handler
259 // see MDL-23407
260 try {
261 // TODO: add file scanning MDL-19380 into each plugin
b8a6131b 262 $result = $repo->upload($saveas_filename, $maxbytes);
ac1870e3
DC
263 echo json_encode($result);
264 } catch (Exception $e) {
265 $err->error = $e->getMessage();
266 echo json_encode($err);
267 die;
268 }
9d4ef80f
DC
269 break;
270}
d0f8585d 271
272/**
273 * Small function to walk an array to attach repository ID
9d4ef80f
DC
274 * @param array $value
275 * @param string $key
276 * @param int $id
d0f8585d 277 */
278function repository_attach_id(&$value, $key, $id){
279 $value['repo_id'] = $id;
0eb58cf4 280}