course/lib.php: MDL-23401 Fix regression introduced in MDL-23255 that prevents course...
[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
23 * @package moodlecore
24 * @subpackage repository
25 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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
dd0566eb
DC
35$err = new stdclass;
36
37if (!isloggedin()) {
38 $err->error = get_string('loggedinnot');
39 die(json_encode($err));
40}
41
9d4ef80f 42require_login();
cf493e89 43
d0f8585d 44/// Parameters
9d4ef80f 45$action = optional_param('action', '', PARAM_ALPHA);
3e123368 46$repo_id = optional_param('repo_id', 0, PARAM_INT); // Pepository ID
3e123368
DC
47$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID
48$env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform
1dce6261 49$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
3e123368
DC
50$author = optional_param('author', '', PARAM_TEXT); // File author
51$source = optional_param('source', '', PARAM_RAW); // File to download
52$itemid = optional_param('itemid', 0, PARAM_INT); // Itemid
53$page = optional_param('page', '', PARAM_RAW); // Page
54$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes
55$req_path = optional_param('p', '', PARAM_RAW); // Path
e35194be 56$accepted_types = optional_param('accepted_types', '*', PARAM_RAW);
3e123368
DC
57$saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name
58$saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path
9d4ef80f
DC
59$search_text = optional_param('s', '', PARAM_CLEANHTML);
60$linkexternal = optional_param('linkexternal', '', PARAM_ALPHA);
455860ce 61
577aab9b 62/// Headers to make it not cacheable
9d4ef80f
DC
63header('Cache-Control: no-cache, must-revalidate');
64header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
65
6127179b
DC
66if (!confirm_sesskey()) {
67 $err->error = get_string('invalidsesskey');
68 die(json_encode($err));
69}
70
d0f8585d 71/// Check permissions
9d4ef80f 72if (! (isloggedin() && repository::check_context($contextid)) ) {
e35194be 73 $err->error = get_string('nopermissiontoaccess', 'repository');
9d4ef80f
DC
74 die(json_encode($err));
75}
0eb58cf4 76
3e123368
DC
77$moodle_maxbytes = get_max_upload_file_size();
78// to prevent maxbytes greater than moodle maxbytes setting
79if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
80 $maxbytes = $moodle_maxbytes;
81}
82
577aab9b 83/// Wait as long as it takes for this script to finish
9d4ef80f 84set_time_limit(0);
577aab9b 85
9d4ef80f
DC
86// Early actions which need to be done before repository instaces initialised
87switch ($action) {
88 // global search
89 case 'gsearch':
90 $params = array();
91 $params['context'] = array(get_context_instance_by_id($contextid), get_system_context());
92 $params['currentcontext'] = get_context_instance_by_id($contextid);
93 $repos = repository::get_instances($params);
94 $list = array();
95 foreach($repos as $repo){
96 if ($repo->global_search()) {
e35194be
DC
97 $ret = $repo->search($search_text);
98 array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below
99 $tmp = array_merge($list, $ret['list']);
100 $list = $tmp;
d0f8585d 101 }
9d4ef80f
DC
102 }
103 $listing = array('list'=>$list);
104 $listing['gsearch'] = true;
9d4ef80f
DC
105 die(json_encode($listing));
106 break;
d0f8585d 107
9d4ef80f
DC
108 // remove the cache files & logout
109 case 'ccache':
110 $cache = new curl_cache;
111 $cache->refresh();
112 $action = 'list';
113 break;
114}
d0f8585d 115
116/// Get repository instance information
9d4ef80f
DC
117$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
118 'WHERE i.id=? AND i.typeid=r.id';
d0f8585d 119
9d4ef80f 120if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
e35194be 121 $err->error = get_string('invalidrepositoryid', 'repository');
9d4ef80f
DC
122 die(json_encode($err));
123} else {
124 $type = $repository->type;
125}
126
e35194be
DC
127if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
128 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
9d4ef80f 129 $classname = 'repository_' . $type;
e35194be 130 $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type));
a06878d3 131} else {
e35194be 132 $err->error = get_string('invalidplugin', 'repository', $type);
9d4ef80f 133 die(json_encode($err));
a06878d3 134}
9d4ef80f 135
d0f8585d 136/// These actions all occur on the currently active repository instance
9d4ef80f
DC
137switch ($action) {
138 case 'sign':
139 case 'signin':
140 case 'list':
141 if ($repo->check_login()) {
e35194be
DC
142 $listing = $repo->get_listing($req_path, $page);
143 $listing['repo_id'] = $repo_id;
144 echo json_encode($listing);
d0f8585d 145 break;
9d4ef80f
DC
146 } else {
147 $action = 'login';
148 }
149 case 'login':
e35194be
DC
150 $listing = $repo->print_login();
151 $listing['repo_id'] = $repo_id;
152 echo json_encode($listing);
9d4ef80f
DC
153 break;
154 case 'logout':
155 $logout = $repo->logout();
9d4ef80f
DC
156 $logout['repo_id'] = $repo_id;
157 echo json_encode($logout);
158 break;
159 case 'searchform':
e35194be 160 $search_form['form'] = $repo->print_search();
9d4ef80f
DC
161 echo json_encode($search_form);
162 break;
163 case 'search':
e35194be
DC
164 $search_result = $repo->search($search_text, (int)$page);
165 $search_result['repo_id'] = $repo_id;
166 $search_result['search_result'] = true;
167 echo json_encode($search_result);
9d4ef80f
DC
168 break;
169 case 'download':
e35194be
DC
170 // validate mimetype
171 $mimetypes = array();
2385e6de 172 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
e35194be
DC
173 $mimetypes = '*';
174 } else {
175 foreach ($accepted_types as $type) {
16f61c70 176 $mimetypes[] = mimeinfo('type', $type);
e35194be
DC
177 }
178 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
033aad78 179 throw new moodle_exception('invalidfiletype', 'repository', '', get_string(mimeinfo('type', $saveas_filename), 'mimetypes'));
e35194be
DC
180 }
181 }
182
183 // We have two special repoisitory type need to deal with
184 // local and recent plugins don't added new files to moodle, just add new records to database
185 // so we don't check user quota and maxbytes here
186 if (in_array($repo->options['type'], array('local', 'recent', 'user'))) {
187 $fileinfo = $repo->copy_to_area($source, 'draft', $itemid, $saveas_path, $saveas_filename);
188 $info = array();
189 $info['file'] = $fileinfo['title'];
190 $info['id'] = $itemid;
16f61c70 191 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
e35194be
DC
192 $filesize = $fileinfo['filesize'];
193 if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
194 throw new file_exception('maxbytes');
195 }
196 echo json_encode($info);
197 die; // ends here!!
198 } else {
199 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
200 if (!empty($allowexternallink)) {
201 $allowexternallink = true;
202 } else {
203 $allowexternallink = false;
204 }
205 // allow external links in url element all the time
206 $allowexternallink = ($allowexternallink || ($env == 'url'));
207
208 // Use link of the files
209 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
210 // use external link
211 $link = $repo->get_link($source);
9d4ef80f 212 $info = array();
e35194be
DC
213 $info['filename'] = $saveas_filename;
214 $info['type'] = 'link';
215 $info['url'] = $link;
acb70a9b 216 echo json_encode($info);
e35194be 217 die;
9d4ef80f 218 } else {
e35194be
DC
219 // Download file to moodle
220 $file = $repo->get_file($source, $saveas_filename);
221 if ($file['path'] === false) {
222 $err->error = get_string('cannotdownload', 'repository');
223 die(json_encode($err));
99d52655 224 }
ea1780ad 225
e35194be
DC
226 // check if exceed maxbytes
227 if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
228 throw new file_exception('maxbytes');
229 }
14469892 230
e35194be
DC
231 // check if exceed user quota
232 $userquota = file_get_user_used_space();
233 if (filesize($file['path'])+$userquota>=(int)$CFG->userquota) {
234 throw new file_exception('userquotalimit');
235 }
ea1780ad 236
e35194be
DC
237 $record = new stdclass;
238 $record->filepath = $saveas_path;
239 $record->filename = $saveas_filename;
240 $record->component = 'user';
241 $record->filearea = 'draft';
242 $record->itemid = $itemid;
14469892 243
e35194be
DC
244 if (!empty($file['license'])) {
245 $record->license = $file['license'];
246 } else {
247 $record->license = $license;
248 }
249 if (!empty($file['author'])) {
250 $record->author = $file['author'];
251 } else {
252 $record->author = $author;
253 }
254 $record->source = !empty($file['url']) ? $file['url'] : '';
1dce6261 255
e35194be
DC
256 $info = repository::move_to_filepool($file['path'], $record);
257 if (empty($info)) {
258 $info['e'] = get_string('error', 'moodle');
acb70a9b 259 }
e35194be
DC
260 echo json_encode($info);
261 die;
9d4ef80f 262 }
9d4ef80f
DC
263 }
264 break;
265 case 'upload':
f728d49b 266 // TODO: add file scanning MDL-19380 into each plugin
e35194be
DC
267 $result = $repo->upload();
268 echo json_encode($result);
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}