Commit | Line | Data |
---|---|---|
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 | |
14469892 DC |
29 | require_once(dirname(dirname(__FILE__)).'/config.php'); |
30 | require_once(dirname(dirname(__FILE__)).'/lib/filelib.php'); | |
31 | require_once(dirname(__FILE__).'/lib.php'); | |
99eaca9d | 32 | |
9d4ef80f | 33 | require_login(); |
cf493e89 | 34 | |
d0f8585d | 35 | /// Parameters |
9d4ef80f | 36 | $action = optional_param('action', '', PARAM_ALPHA); |
3e123368 | 37 | $repo_id = optional_param('repo_id', 0, PARAM_INT); // Pepository ID |
3e123368 DC |
38 | $client_id = optional_param('client_id', '', PARAM_RAW); // Client ID |
39 | $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID | |
40 | $env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform | |
1dce6261 | 41 | $license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT); |
3e123368 DC |
42 | $author = optional_param('author', '', PARAM_TEXT); // File author |
43 | $source = optional_param('source', '', PARAM_RAW); // File to download | |
44 | $itemid = optional_param('itemid', 0, PARAM_INT); // Itemid | |
45 | $page = optional_param('page', '', PARAM_RAW); // Page | |
46 | $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes | |
47 | $req_path = optional_param('p', '', PARAM_RAW); // Path | |
3e123368 DC |
48 | $saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name |
49 | $saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path | |
9d4ef80f DC |
50 | $search_text = optional_param('s', '', PARAM_CLEANHTML); |
51 | $linkexternal = optional_param('linkexternal', '', PARAM_ALPHA); | |
455860ce | 52 | |
577aab9b | 53 | /// Headers to make it not cacheable |
9d4ef80f DC |
54 | header('Cache-Control: no-cache, must-revalidate'); |
55 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | |
56 | ||
57 | $err = new stdclass; | |
58 | $err->client_id = $client_id; | |
0eb58cf4 | 59 | |
6127179b DC |
60 | if (!confirm_sesskey()) { |
61 | $err->error = get_string('invalidsesskey'); | |
62 | die(json_encode($err)); | |
63 | } | |
64 | ||
d0f8585d | 65 | /// Check permissions |
9d4ef80f DC |
66 | if (! (isloggedin() && repository::check_context($contextid)) ) { |
67 | $err->e = get_string('nopermissiontoaccess', 'repository'); | |
68 | die(json_encode($err)); | |
69 | } | |
0eb58cf4 | 70 | |
3e123368 DC |
71 | $moodle_maxbytes = get_max_upload_file_size(); |
72 | // to prevent maxbytes greater than moodle maxbytes setting | |
73 | if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) { | |
74 | $maxbytes = $moodle_maxbytes; | |
75 | } | |
76 | ||
577aab9b | 77 | /// Wait as long as it takes for this script to finish |
9d4ef80f | 78 | set_time_limit(0); |
577aab9b | 79 | |
9d4ef80f DC |
80 | // Early actions which need to be done before repository instaces initialised |
81 | switch ($action) { | |
82 | // global search | |
83 | case 'gsearch': | |
84 | $params = array(); | |
85 | $params['context'] = array(get_context_instance_by_id($contextid), get_system_context()); | |
86 | $params['currentcontext'] = get_context_instance_by_id($contextid); | |
87 | $repos = repository::get_instances($params); | |
88 | $list = array(); | |
89 | foreach($repos as $repo){ | |
90 | if ($repo->global_search()) { | |
91 | try { | |
92 | $ret = $repo->search($search_text); | |
93 | array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below | |
94 | $tmp = array_merge($list, $ret['list']); | |
95 | $list = $tmp; | |
96 | } catch (repository_exception $e) { | |
97 | $err->e = $e->getMessage(); | |
98 | die(json_encode($err)); | |
d0f8585d | 99 | } |
100 | } | |
9d4ef80f DC |
101 | } |
102 | $listing = array('list'=>$list); | |
103 | $listing['gsearch'] = true; | |
104 | $listing['client_id'] = $client_id; | |
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 DC |
120 | if (!$repository = $DB->get_record_sql($sql, array($repo_id))) { |
121 | $err->e = get_string('invalidrepositoryid', 'repository'); | |
122 | die(json_encode($err)); | |
123 | } else { | |
124 | $type = $repository->type; | |
125 | } | |
126 | ||
127 | if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) { | |
128 | require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php'); | |
129 | $classname = 'repository_' . $type; | |
130 | try { | |
131 | $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type, 'client_id'=>$client_id)); | |
132 | } catch (repository_exception $e){ | |
133 | $err->e = $e->getMessage(); | |
bf1fccf0 | 134 | die(json_encode($err)); |
5bce5972 | 135 | } |
a06878d3 | 136 | } else { |
9d4ef80f DC |
137 | $err->e = get_string('invalidplugin', 'repository', $type); |
138 | die(json_encode($err)); | |
a06878d3 | 139 | } |
9d4ef80f | 140 | |
d0f8585d | 141 | /// These actions all occur on the currently active repository instance |
9d4ef80f DC |
142 | switch ($action) { |
143 | case 'sign': | |
144 | case 'signin': | |
145 | case 'list': | |
146 | if ($repo->check_login()) { | |
d0f8585d | 147 | try { |
9d4ef80f | 148 | $listing = $repo->get_listing($req_path, $page); |
e189ec00 | 149 | $listing['client_id'] = $client_id; |
150 | $listing['repo_id'] = $repo_id; | |
151 | echo json_encode($listing); | |
d0f8585d | 152 | } catch (repository_exception $e) { |
d0f8585d | 153 | $err->e = $e->getMessage(); |
154 | die(json_encode($err)); | |
155 | } | |
156 | break; | |
9d4ef80f DC |
157 | } else { |
158 | $action = 'login'; | |
159 | } | |
160 | case 'login': | |
161 | try { | |
162 | $listing = $repo->print_login(); | |
163 | $listing['client_id'] = $client_id; | |
164 | $listing['repo_id'] = $repo_id; | |
165 | echo json_encode($listing); | |
166 | } catch (repository_exception $e){ | |
167 | $err->e = $e->getMessage(); | |
168 | die(json_encode($err)); | |
169 | } | |
170 | break; | |
171 | case 'logout': | |
172 | $logout = $repo->logout(); | |
173 | $logout['client_id'] = $client_id; | |
174 | $logout['repo_id'] = $repo_id; | |
175 | echo json_encode($logout); | |
176 | break; | |
177 | case 'searchform': | |
178 | $search_form['form'] = $repo->print_search($client_id); | |
179 | $search_form['client_id'] = $client_id; | |
180 | echo json_encode($search_form); | |
181 | break; | |
182 | case 'search': | |
183 | try { | |
184 | $search_result = $repo->search($search_text, (int)$page); | |
185 | $search_result['client_id'] = $client_id; | |
186 | $search_result['repo_id'] = $repo_id; | |
187 | $search_result['search_result'] = true; | |
188 | echo json_encode($search_result); | |
189 | } catch (repository_exception $e) { | |
190 | $err->e = $e->getMessage(); | |
191 | die(json_encode($err)); | |
192 | } | |
193 | break; | |
194 | case 'download': | |
195 | try { | |
ea1780ad DC |
196 | // We have two special repoisitory type need to deal with |
197 | // local and recent plugins don't added new files to moodle, just add new records to database | |
198 | // so we don't check user quota and maxbytes here | |
6bf197b3 | 199 | if (in_array($repo->options['type'], array('local', 'recent', 'user'))) { |
acb70a9b | 200 | try { |
64f93798 | 201 | $fileinfo = $repo->copy_to_area($source, 'draft', $itemid, $saveas_path, $saveas_filename); |
acb70a9b DC |
202 | } catch (Exception $e) { |
203 | throw $e; | |
204 | } | |
9d4ef80f DC |
205 | $info = array(); |
206 | $info['client_id'] = $client_id; | |
207 | $info['file'] = $fileinfo['title']; | |
208 | $info['id'] = $itemid; | |
209 | $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title']; | |
210 | $filesize = $fileinfo['filesize']; | |
acb70a9b | 211 | if (($maxbytes!==-1) && ($filesize>$maxbytes)) { |
dd64051e | 212 | throw new file_exception('maxbytes'); |
41076c58 | 213 | } |
acb70a9b DC |
214 | echo json_encode($info); |
215 | die; // ends here!! | |
9d4ef80f | 216 | } else { |
acb70a9b DC |
217 | $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks'); |
218 | if (!empty($allowexternallink)) { | |
219 | $allowexternallink = true; | |
220 | } else { | |
221 | $allowexternallink = false; | |
99d52655 | 222 | } |
acb70a9b DC |
223 | // allow external links in url element all the time |
224 | $allowexternallink = ($allowexternallink || ($env == 'url')); | |
99d52655 | 225 | |
ea1780ad | 226 | // Use link of the files |
acb70a9b DC |
227 | if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() || FILE_EXTERNAL)) { |
228 | // use external link | |
229 | try { | |
230 | $link = $repo->get_link($source); | |
231 | } catch (repository_exception $e){ | |
ea1780ad | 232 | throw $e; |
acb70a9b DC |
233 | } |
234 | $info = array(); | |
235 | $info['filename'] = $saveas_filename; | |
236 | $info['type'] = 'link'; | |
237 | $info['url'] = $link; | |
238 | echo json_encode($info); | |
239 | die; | |
240 | } else { | |
ea1780ad | 241 | // Download file to moodle |
acb70a9b DC |
242 | $file = $repo->get_file($source, $saveas_filename); |
243 | if ($file['path'] === false) { | |
244 | $err->e = get_string('cannotdownload', 'repository'); | |
245 | die(json_encode($err)); | |
246 | } | |
ea1780ad DC |
247 | |
248 | // check if exceed maxbytes | |
acb70a9b | 249 | if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) { |
dd64051e | 250 | throw new file_exception('maxbytes'); |
acb70a9b | 251 | } |
14469892 | 252 | |
ea1780ad DC |
253 | // check if exceed user quota |
254 | $userquota = file_get_user_used_space(); | |
255 | if (filesize($file['path'])+$userquota>=(int)$CFG->userquota) { | |
256 | throw new file_exception('userquotalimit'); | |
257 | } | |
258 | ||
acb70a9b DC |
259 | $record = new stdclass; |
260 | $record->filepath = $saveas_path; | |
261 | $record->filename = $saveas_filename; | |
64f93798 PS |
262 | $record->component = 'user'; |
263 | $record->filearea = 'draft'; | |
acb70a9b | 264 | $record->itemid = $itemid; |
14469892 | 265 | |
acb70a9b DC |
266 | if (!empty($file['license'])) { |
267 | $record->license = $file['license']; | |
268 | } else { | |
269 | $record->license = $license; | |
270 | } | |
271 | if (!empty($file['author'])) { | |
272 | $record->author = $file['author']; | |
273 | } else { | |
274 | $record->author = $author; | |
275 | } | |
276 | $record->source = !empty($file['url']) ? $file['url'] : ''; | |
1dce6261 | 277 | |
acb70a9b DC |
278 | $info = repository::move_to_filepool($file['path'], $record); |
279 | if (empty($info)) { | |
280 | $info['e'] = get_string('error', 'moodle'); | |
281 | } | |
282 | echo json_encode($info); | |
283 | die; | |
284 | } | |
9d4ef80f | 285 | } |
9d4ef80f DC |
286 | } catch (Exception $e) { |
287 | $err->e = $e->getMessage(); | |
288 | die(json_encode($err)); | |
289 | } | |
290 | break; | |
291 | case 'upload': | |
292 | try { | |
1dce6261 | 293 | $result = $repo->upload(); |
9d4ef80f DC |
294 | $result['client_id'] = $client_id; |
295 | echo json_encode($result); | |
296 | } catch (Exception $e){ | |
297 | $err->e = $e->getMessage(); | |
298 | $err->client_id = $client_id; | |
299 | die(json_encode($err)); | |
300 | } | |
301 | break; | |
302 | } | |
d0f8585d | 303 | |
304 | /** | |
305 | * Small function to walk an array to attach repository ID | |
9d4ef80f DC |
306 | * @param array $value |
307 | * @param string $key | |
308 | * @param int $id | |
d0f8585d | 309 | */ |
310 | function repository_attach_id(&$value, $key, $id){ | |
311 | $value['repo_id'] = $id; | |
0eb58cf4 | 312 | } |