Commit | Line | Data |
---|---|---|
761265ad | 1 | <?php |
6f2cd52a | 2 | |
761265ad | 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 | ||
6f2cd52a DC |
19 | /** |
20 | * This file is used to browse repositories in non-javascript mode | |
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 DC |
27 | */ |
28 | ||
761265ad | 29 | require_once('../config.php'); |
30 | require_once($CFG->libdir.'/filelib.php'); | |
31 | require_once('lib.php'); | |
b6fd5aee | 32 | /// Wait as long as it takes for this script to finish |
33 | set_time_limit(0); | |
34 | ||
71267723 | 35 | require_sesskey(); |
b6fd5aee | 36 | require_login(); |
761265ad | 37 | |
8ea068a2 | 38 | // disable blocks in this page |
78946b9b | 39 | $PAGE->set_pagelayout('embedded'); |
8ea068a2 | 40 | |
41 | // general parameters | |
42 | $action = optional_param('action', '', PARAM_ALPHA); | |
d10c92f3 | 43 | $client_id = optional_param('client_id', '', PARAM_RAW); // client ID |
761265ad | 44 | $itemid = optional_param('itemid', '', PARAM_INT); |
8ea068a2 | 45 | |
46 | // parameters for repository | |
761265ad | 47 | $callback = optional_param('callback', '', PARAM_CLEANHTML); |
ad137107 | 48 | $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID |
a14a933c | 49 | $courseid = optional_param('course', SITEID, PARAM_INT); // course ID |
8ea068a2 | 50 | $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor |
51 | $filename = optional_param('filename', '', PARAM_FILE); | |
e5051df3 | 52 | $fileurl = optional_param('fileurl', '', PARAM_RAW); |
8ea068a2 | 53 | $thumbnail = optional_param('thumbnail', '', PARAM_RAW); |
af804e3e | 54 | $targetpath = optional_param('targetpath', '', PARAM_PATH); |
8ea068a2 | 55 | $repo_id = optional_param('repo_id', 0, PARAM_INT); // repository ID |
56 | $req_path = optional_param('p', '', PARAM_RAW); // the path in repository | |
6f2cd52a | 57 | $curr_page = optional_param('page', '', PARAM_RAW); // What page in repository? |
761265ad | 58 | $search_text = optional_param('s', '', PARAM_CLEANHTML); |
563d0417 DC |
59 | $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles |
60 | $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes | |
61 | $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes | |
761265ad | 62 | |
563d0417 DC |
63 | // the path to save files |
64 | $savepath = optional_param('savepath', '/', PARAM_PATH); | |
8ea068a2 | 65 | // path in draft area |
563d0417 | 66 | $draftpath = optional_param('draftpath', '/', PARAM_PATH); |
8ea068a2 | 67 | |
68 | ||
69 | // user context | |
4239150a | 70 | $user_context = context_user::instance($USER->id); |
8ea068a2 | 71 | |
563d0417 | 72 | $PAGE->set_context($user_context); |
a14a933c DC |
73 | if (!$course = $DB->get_record('course', array('id'=>$courseid))) { |
74 | print_error('invalidcourseid'); | |
75 | } | |
76 | $PAGE->set_course($course); | |
c33aa23b | 77 | |
761265ad | 78 | // init repository plugin |
79 | $sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '. | |
80 | 'WHERE i.id=? AND i.typeid=r.id'; | |
009a97ce | 81 | if ($repository = $DB->get_record_sql($sql, array($repo_id))) { |
761265ad | 82 | $type = $repository->type; |
e35194be DC |
83 | if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) { |
84 | require_once($CFG->dirroot.'/repository/'.$type.'/lib.php'); | |
009a97ce | 85 | $classname = 'repository_' . $type; |
86 | try { | |
563d0417 | 87 | $repo = new $classname($repo_id, $contextid, array('ajax'=>false, 'name'=>$repository->name, 'type'=>$type)); |
009a97ce | 88 | } catch (repository_exception $e){ |
89 | print_error('pluginerror', 'repository'); | |
90 | } | |
91 | } else { | |
92 | print_error('invalidplugin', 'repository'); | |
761265ad | 93 | } |
761265ad | 94 | } |
8ea068a2 | 95 | |
8ac95c27 AD |
96 | $context = context::instance_by_id($contextid); |
97 | $moodle_maxbytes = get_user_max_upload_file_size($context); | |
75d138a3 DC |
98 | // to prevent maxbytes greater than moodle maxbytes setting |
99 | if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) { | |
100 | $maxbytes = $moodle_maxbytes; | |
101 | } | |
102 | ||
71267723 | 103 | $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); |
563d0417 DC |
104 | $params['action'] = 'browse'; |
105 | $params['draftpath'] = $draftpath; | |
106 | $home_url = new moodle_url('/repository/draftfiles_manager.php', $params); | |
107 | ||
108 | $params['savepath'] = $savepath; | |
bf1873c6 | 109 | $params['repo_id'] = $repo_id; |
563d0417 DC |
110 | $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params); |
111 | $PAGE->set_url('/repository/filepicker.php', $params); | |
b6fd5aee | 112 | |
14f3c882 | 113 | switch ($action) { |
ab9cdbb9 | 114 | case 'upload': |
009a97ce | 115 | // The uploaded file has been processed in plugin construct function |
8ea068a2 | 116 | // redirect to default page |
84641adc PS |
117 | try { |
118 | $repo->upload('', $maxbytes); | |
119 | redirect($home_url, get_string('uploadsucc','repository')); | |
120 | } catch (moodle_exception $e) { | |
121 | // inject target URL | |
122 | $e->link = $PAGE->url->out(); | |
123 | echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it | |
124 | throw $e; | |
125 | } | |
14f3c882 | 126 | break; |
8ea068a2 | 127 | |
78ff2983 | 128 | case 'search': |
c33aa23b | 129 | echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>"; |
78ff2983 | 130 | try { |
131 | $search_result = $repo->search($search_text); | |
29199e56 | 132 | $search_result['issearchresult'] = true; |
78ff2983 | 133 | $search_result['repo_id'] = $repo_id; |
009a97ce | 134 | |
135 | // TODO: need a better solution | |
ef3e1fbc | 136 | $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id)); |
929d7a83 PS |
137 | $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p'); |
138 | echo $OUTPUT->render($pagingbar); | |
009a97ce | 139 | |
78ff2983 | 140 | echo '<table>'; |
141 | foreach ($search_result['list'] as $item) { | |
142 | echo '<tr>'; | |
8685679a MG |
143 | echo '<td>'; |
144 | $style = ''; | |
145 | if (isset($item['thumbnail_height'])) { | |
146 | $style .= 'max-height:'.$item['thumbnail_height'].'px;'; | |
147 | } | |
148 | if (isset($item['thumbnail_width'])) { | |
149 | $style .= 'max-width:'.$item['thumbnail_width'].'px;'; | |
150 | } | |
151 | echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style)); | |
78ff2983 | 152 | echo '</td><td>'; |
153 | if (!empty($item['url'])) { | |
71267723 | 154 | echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank')); |
78ff2983 | 155 | } else { |
156 | echo $item['title']; | |
157 | } | |
158 | echo '</td>'; | |
159 | echo '<td>'; | |
009a97ce | 160 | echo '<form method="post">'; |
71267723 | 161 | echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>'; |
009a97ce | 162 | echo '<input type="hidden" name="action" value="confirm"/>'; |
71267723 DC |
163 | echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>'; |
164 | echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>'; | |
165 | echo '<input type="submit" value="'.s(get_string('select','repository')).'" />'; | |
009a97ce | 166 | echo '</form>'; |
78ff2983 | 167 | echo '</td>'; |
009a97ce | 168 | echo '</tr>'; |
78ff2983 | 169 | } |
170 | echo '</table>'; | |
171 | } catch (repository_exception $e) { | |
172 | } | |
173 | break; | |
8ea068a2 | 174 | |
14f3c882 | 175 | case 'list': |
176 | case 'sign': | |
af804e3e | 177 | echo $OUTPUT->header(); |
71267723 DC |
178 | |
179 | echo $OUTPUT->container_start(); | |
180 | echo html_writer::link($url, get_string('back', 'repository')); | |
181 | echo $OUTPUT->container_end(); | |
182 | ||
761265ad | 183 | if ($repo->check_login()) { |
6f2cd52a | 184 | $list = $repo->get_listing($req_path, $curr_page); |
761265ad | 185 | $dynload = !empty($list['dynload'])?true:false; |
186 | if (!empty($list['upload'])) { | |
47ce714b | 187 | echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">'; |
ab9cdbb9 | 188 | echo '<label>'.$list['upload']['label'].': </label>'; |
761265ad | 189 | echo '<input type="file" name="repo_upload_file" /><br />'; |
ab9cdbb9 | 190 | echo '<input type="hidden" name="action" value="upload" /><br />'; |
71267723 DC |
191 | echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />'; |
192 | echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />'; | |
193 | echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />'; | |
194 | echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />'; | |
761265ad | 195 | echo '</form>'; |
196 | } else { | |
5e98ab96 | 197 | if (!empty($list['path'])) { |
198 | foreach ($list['path'] as $p) { | |
bf1873c6 | 199 | //echo '<form method="post" style="display:inline">'; |
71267723 | 200 | //echo '<input type="hidden" name="p" value="'.s($p['path']).'"'; |
bf1873c6 | 201 | //echo '<input type="hidden" name="action" value="list"'; |
71267723 DC |
202 | //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />'; |
203 | //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />'; | |
204 | //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />'; | |
bf1873c6 DC |
205 | //echo '</form>'; |
206 | ||
207 | $pathurl = new moodle_url($url, array( | |
208 | 'p'=>$p['path'], | |
209 | 'action'=>'list', | |
210 | 'draftpath'=>$draftpath, | |
211 | 'savepath'=>$savepath | |
212 | )); | |
213 | echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>'; | |
214 | echo '<span> / </span>'; | |
5e98ab96 | 215 | } |
761265ad | 216 | } |
009a97ce | 217 | if (!empty($list['page'])) { |
20ee5084 MG |
218 | // TODO MDL-28482: need a better solution |
219 | // paging_bar is not a good option because it starts page numbering from 0 and | |
220 | // repositories number pages starting from 1. | |
221 | $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey()); | |
222 | if (!isset($list['perpage']) && !isset($list['total'])) { | |
223 | $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same | |
224 | } | |
225 | if (empty($list['total'])) { | |
226 | if ($list['pages'] == -1) { | |
227 | $total = ($list['page'] + 2) * $list['perpage']; | |
228 | } else { | |
229 | $total = $list['pages'] * $list['perpage']; | |
230 | } | |
231 | } else { | |
232 | $total = $list['total']; | |
233 | } | |
234 | echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl); | |
009a97ce | 235 | } |
761265ad | 236 | echo '<table>'; |
237 | foreach ($list['list'] as $item) { | |
238 | echo '<tr>'; | |
8685679a MG |
239 | echo '<td>'; |
240 | $style = ''; | |
241 | if (isset($item['thumbnail_height'])) { | |
242 | $style .= 'max-height:'.$item['thumbnail_height'].'px;'; | |
243 | } | |
244 | if (isset($item['thumbnail_width'])) { | |
245 | $style .= 'max-width:'.$item['thumbnail_width'].'px;'; | |
246 | } | |
247 | echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style)); | |
761265ad | 248 | echo '</td><td>'; |
249 | if (!empty($item['url'])) { | |
71267723 | 250 | echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank')); |
761265ad | 251 | } else { |
252 | echo $item['title']; | |
253 | } | |
254 | echo '</td>'; | |
255 | echo '<td>'; | |
256 | if (!isset($item['children'])) { | |
257 | echo '<form method="post">'; | |
71267723 | 258 | echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>'; |
761265ad | 259 | echo '<input type="hidden" name="action" value="confirm"/>'; |
71267723 DC |
260 | echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />'; |
261 | echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />'; | |
262 | echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>'; | |
263 | echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>'; | |
264 | echo '<input type="submit" value="'.s(get_string('select','repository')).'" />'; | |
761265ad | 265 | echo '</form>'; |
266 | } else { | |
267 | echo '<form method="post">'; | |
71267723 DC |
268 | echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>'; |
269 | echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />'; | |
761265ad | 270 | echo '</form>'; |
271 | } | |
272 | echo '</td>'; | |
009a97ce | 273 | echo '</tr>'; |
761265ad | 274 | } |
275 | echo '</table>'; | |
276 | } | |
277 | } else { | |
278 | echo '<form method="post">'; | |
761265ad | 279 | echo '<input type="hidden" name="action" value="sign" />'; |
71267723 | 280 | echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />'; |
78ff2983 | 281 | $repo->print_login(); |
761265ad | 282 | echo '</form>'; |
283 | } | |
6fd42112 | 284 | echo $OUTPUT->footer(); |
14f3c882 | 285 | break; |
8ea068a2 | 286 | |
14f3c882 | 287 | case 'download': |
7e897e67 MG |
288 | // Check that user has permission to access this file |
289 | if (!$repo->file_is_accessible($fileurl)) { | |
290 | print_error('storedfilecannotread'); | |
563d0417 | 291 | } |
4239150a | 292 | |
7e897e67 MG |
293 | // If file is already a reference, set $fileurl = file source, $repo = file repository |
294 | // note that in this case user may not have permission to access the source file directly | |
295 | // so no file_browser/file_info can be used below | |
296 | if ($repo->has_moodle_files()) { | |
297 | $file = repository::get_moodle_file($fileurl); | |
298 | if ($file && $file->is_external_file()) { | |
299 | $fileurl = $file->get_reference(); | |
300 | $repo_id = $file->get_repository_id(); | |
301 | $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions); | |
84641adc | 302 | } |
7e897e67 MG |
303 | } |
304 | ||
305 | $record = new stdClass(); | |
306 | $record->filepath = $savepath; | |
307 | $record->filename = $filename; | |
308 | $record->component = 'user'; | |
309 | $record->filearea = 'draft'; | |
310 | $record->itemid = $itemid; | |
311 | $record->license = ''; | |
312 | $record->author = ''; | |
313 | ||
314 | $now = time(); | |
315 | $record->timecreated = $now; | |
316 | $record->timemodified = $now; | |
317 | $record->userid = $USER->id; | |
318 | $record->contextid = $user_context->id; | |
319 | $record->sortorder = 0; | |
320 | ||
321 | $sourcefield = $repo->get_file_source_info($fileurl); | |
322 | $record->source = repository::build_source_field($sourcefield); | |
323 | ||
324 | if ($repo->has_moodle_files()) { | |
325 | $fileinfo = $repo->copy_to_area($fileurl, $record, $maxbytes); | |
326 | redirect($home_url, get_string('downloadsucc', 'repository')); | |
009a97ce | 327 | } else { |
7e897e67 MG |
328 | $thefile = $repo->get_file($fileurl, $filename); |
329 | if (!empty($thefile['path'])) { | |
330 | $filesize = filesize($thefile['path']); | |
331 | if ($maxbytes != -1 && $filesize>$maxbytes) { | |
332 | unlink($thefile['path']); | |
333 | print_error('maxbytes'); | |
334 | } | |
335 | try { | |
336 | $info = repository::move_to_filepool($thefile['path'], $record); | |
337 | redirect($home_url, get_string('downloadsucc', 'repository')); | |
338 | } catch (moodle_exception $e) { | |
339 | // inject target URL | |
340 | $e->link = $PAGE->url->out(); | |
341 | echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it | |
342 | throw $e; | |
343 | } | |
344 | } else { | |
345 | print_error('cannotdownload', 'repository'); | |
346 | } | |
14f3c882 | 347 | } |
348 | ||
349 | break; | |
8ea068a2 | 350 | |
14f3c882 | 351 | case 'confirm': |
af804e3e | 352 | echo $OUTPUT->header(); |
1e98e05f | 353 | echo '<div><a href="'.s($PAGE->url->out(false)).'">'.get_string('back', 'repository').'</a></div>'; |
8ea068a2 | 354 | echo '<img src="'.$thumbnail.'" />'; |
355 | echo '<form method="post">'; | |
356 | echo '<table>'; | |
357 | echo ' <tr>'; | |
3211569a SH |
358 | echo ' <td><label>'.get_string('filename', 'repository').'</label></td>'; |
359 | echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>'; | |
71267723 | 360 | echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>'; |
8ea068a2 | 361 | echo ' <td><input type="hidden" name="action" value="download" /></td>'; |
71267723 | 362 | echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>'; |
8ea068a2 | 363 | echo ' </tr>'; |
14f3c882 | 364 | echo '</table>'; |
365 | echo '<div>'; | |
8ea068a2 | 366 | // the save path |
71267723 DC |
367 | echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />'; |
368 | echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />'; | |
369 | echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />'; | |
14f3c882 | 370 | echo '</div>'; |
371 | echo '</form>'; | |
6fd42112 | 372 | echo $OUTPUT->footer(); |
14f3c882 | 373 | break; |
8ea068a2 | 374 | |
563d0417 | 375 | default: |
ab9cdbb9 | 376 | case 'plugins': |
6f2cd52a DC |
377 | $params = array(); |
378 | $params['context'] = array($user_context, get_system_context()); | |
379 | $params['currentcontext'] = $PAGE->context; | |
563d0417 DC |
380 | $params['return_types'] = FILE_INTERNAL; |
381 | ||
6f2cd52a | 382 | $repos = repository::get_instances($params); |
af804e3e | 383 | echo $OUTPUT->header(); |
563d0417 | 384 | echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository')); |
8ea068a2 | 385 | echo '<div>'; |
386 | echo '<ul>'; | |
ab9cdbb9 | 387 | foreach($repos as $repo) { |
388 | $info = $repo->get_meta(); | |
8ae8bf8a PS |
389 | |
390 | $aurl = clone($url); | |
563d0417 | 391 | $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath)); |
8ae8bf8a | 392 | |
e5051df3 | 393 | echo '<li>'; |
563d0417 | 394 | echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> '; |
e5051df3 DC |
395 | echo html_writer::link($aurl, $info->name); |
396 | echo '</li>'; | |
ab9cdbb9 | 397 | } |
8ea068a2 | 398 | echo '</ul>'; |
399 | echo '</div>'; | |
400 | echo $OUTPUT->footer(); | |
401 | break; | |
af804e3e | 402 | } |