Merge branch 'MDL-35765-27' of git://github.com/merrill-oakland/moodle into MOODLE_27...
[moodle.git] / repository / filepicker.php
blob4ce0236020aa862d7ed0acc851553dcebbc21984
1 <?php
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.
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/>.
19 /**
20 * This file is used to browse repositories in non-javascript mode
22 * @since Moodle 2.0
23 * @package core
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
29 require_once('../config.php');
30 require_once($CFG->libdir.'/filelib.php');
31 require_once('lib.php');
32 /// Wait as long as it takes for this script to finish
33 core_php_time_limit::raise();
35 require_sesskey();
36 require_login();
38 // disable blocks in this page
39 $PAGE->set_pagelayout('embedded');
41 // general parameters
42 $action = optional_param('action', '', PARAM_ALPHA);
43 $client_id = optional_param('client_id', '', PARAM_RAW); // client ID
44 $itemid = optional_param('itemid', '', PARAM_INT);
46 // parameters for repository
47 $callback = optional_param('callback', '', PARAM_CLEANHTML);
48 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
49 $courseid = optional_param('course', SITEID, PARAM_INT); // course ID
50 $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
51 $filename = optional_param('filename', '', PARAM_FILE);
52 $fileurl = optional_param('fileurl', '', PARAM_RAW);
53 $thumbnail = optional_param('thumbnail', '', PARAM_RAW);
54 $targetpath = optional_param('targetpath', '', PARAM_PATH);
55 $repo_id = optional_param('repo_id', 0, PARAM_INT); // repository ID
56 $req_path = optional_param('p', '', PARAM_RAW); // the path in repository
57 $curr_page = optional_param('page', '', PARAM_RAW); // What page in repository?
58 $search_text = optional_param('s', '', PARAM_CLEANHTML);
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
62 $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes.
63 $accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW);
65 // the path to save files
66 $savepath = optional_param('savepath', '/', PARAM_PATH);
67 // path in draft area
68 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
71 // user context
72 $user_context = context_user::instance($USER->id);
74 $PAGE->set_context($user_context);
75 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
76 print_error('invalidcourseid');
78 $PAGE->set_course($course);
80 if ($repo_id) {
81 // Get repository instance information
82 $repooptions = array(
83 'ajax' => false,
84 'mimetypes' => $accepted_types
86 $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
88 // Check permissions
89 $repo->check_capability();
92 $context = context::instance_by_id($contextid);
94 // Make sure maxbytes passed is within site filesize limits.
95 $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes, $maxbytes);
97 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
98 $params['action'] = 'browse';
99 $params['draftpath'] = $draftpath;
100 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
102 $params['savepath'] = $savepath;
103 $params['repo_id'] = $repo_id;
104 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
105 $PAGE->set_url('/repository/filepicker.php', $params);
107 switch ($action) {
108 case 'upload':
109 // The uploaded file has been processed in plugin construct function
110 // redirect to default page
111 try {
112 $repo->upload('', $maxbytes);
113 redirect($home_url, get_string('uploadsucc','repository'));
114 } catch (moodle_exception $e) {
115 // inject target URL
116 $e->link = $PAGE->url->out();
117 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
118 throw $e;
120 break;
122 case 'search':
123 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
124 try {
125 $search_result = $repo->search($search_text);
126 $search_result['issearchresult'] = true;
127 $search_result['repo_id'] = $repo_id;
129 // TODO: need a better solution
130 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
131 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
132 echo $OUTPUT->render($pagingbar);
134 echo '<table>';
135 foreach ($search_result['list'] as $item) {
136 echo '<tr>';
137 echo '<td>';
138 $style = '';
139 if (isset($item['thumbnail_height'])) {
140 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
142 if (isset($item['thumbnail_width'])) {
143 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
145 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'alt' => '', 'style' => $style));
146 echo '</td><td>';
147 if (!empty($item['url'])) {
148 echo html_writer::link($item['url'], s($item['title']), array('target'=>'_blank'));
149 } else {
150 echo s($item['title']);
152 echo '</td>';
153 echo '<td>';
154 echo '<form method="post">';
155 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
156 echo '<input type="hidden" name="action" value="confirm"/>';
157 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
158 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
159 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
160 echo '</form>';
161 echo '</td>';
162 echo '</tr>';
164 echo '</table>';
165 } catch (repository_exception $e) {
167 break;
169 case 'list':
170 case 'sign':
171 echo $OUTPUT->header();
173 echo $OUTPUT->container_start();
174 echo html_writer::link($url, get_string('back', 'repository'));
175 echo $OUTPUT->container_end();
177 if ($repo->check_login()) {
178 $list = $repo->get_listing($req_path, $curr_page);
179 $dynload = !empty($list['dynload'])?true:false;
180 if (!empty($list['upload'])) {
181 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
182 echo '<label>'.$list['upload']['label'].': </label>';
183 echo '<input type="file" name="repo_upload_file" /><br />';
184 echo '<input type="hidden" name="action" value="upload" /><br />';
185 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
186 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
187 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
188 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
189 echo '</form>';
190 } else {
191 if (!empty($list['path'])) {
192 foreach ($list['path'] as $p) {
193 //echo '<form method="post" style="display:inline">';
194 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
195 //echo '<input type="hidden" name="action" value="list"';
196 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
197 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
198 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
199 //echo '</form>';
201 $pathurl = new moodle_url($url, array(
202 'p'=>$p['path'],
203 'action'=>'list',
204 'draftpath'=>$draftpath,
205 'savepath'=>$savepath
207 echo '<strong>' . html_writer::link($pathurl, s($p['name'])) . '</strong>';
208 echo '<span> / </span>';
211 if (!empty($list['page'])) {
212 // TODO MDL-28482: need a better solution
213 // paging_bar is not a good option because it starts page numbering from 0 and
214 // repositories number pages starting from 1.
215 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
216 if (!isset($list['perpage']) && !isset($list['total'])) {
217 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
219 if (empty($list['total'])) {
220 if ($list['pages'] == -1) {
221 $total = ($list['page'] + 2) * $list['perpage'];
222 } else {
223 $total = $list['pages'] * $list['perpage'];
225 } else {
226 $total = $list['total'];
228 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
230 echo '<table>';
231 foreach ($list['list'] as $item) {
232 echo '<tr>';
233 echo '<td>';
234 $style = '';
235 if (isset($item['thumbnail_height'])) {
236 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
238 if (isset($item['thumbnail_width'])) {
239 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
241 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style));
242 echo '</td><td>';
243 if (!empty($item['url'])) {
244 echo html_writer::link($item['url'], s($item['title']), array('target'=>'_blank'));
245 } else {
246 echo s($item['title']);
248 echo '</td>';
249 echo '<td>';
250 if (!isset($item['children'])) {
251 echo '<form method="post">';
252 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
253 echo '<input type="hidden" name="action" value="confirm"/>';
254 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
255 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
256 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
257 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
258 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
259 echo '</form>';
260 } else {
261 echo '<form method="post">';
262 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
263 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
264 echo '</form>';
266 echo '</td>';
267 echo '</tr>';
269 echo '</table>';
271 } else {
272 echo '<form method="post">';
273 echo '<input type="hidden" name="action" value="sign" />';
274 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
275 $repo->print_login();
276 echo '</form>';
278 echo $OUTPUT->footer();
279 break;
281 case 'download':
282 // Check that user has permission to access this file
283 if (!$repo->file_is_accessible($fileurl)) {
284 print_error('storedfilecannotread');
286 $record = new stdClass();
287 $reference = $repo->get_file_reference($fileurl);
289 $sourcefield = $repo->get_file_source_info($fileurl);
290 $record->source = repository::build_source_field($sourcefield);
292 // If file is already a reference, set $fileurl = file source, $repo = file repository
293 // note that in this case user may not have permission to access the source file directly
294 // so no file_browser/file_info can be used below
295 if ($repo->has_moodle_files()) {
296 $file = repository::get_moodle_file($reference);
297 if ($file && $file->is_external_file()) {
298 $sourcefield = $file->get_source(); // remember the original source
299 $record->source = $repo::build_source_field($sourcefield);
300 $reference = $file->get_reference();
301 $repo_id = $file->get_repository_id();
302 $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
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 = '';
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;
321 if ($repo->has_moodle_files()) {
322 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes);
323 redirect($home_url, get_string('downloadsucc', 'repository'));
324 } else {
325 $thefile = $repo->get_file($reference, $filename);
326 if (!empty($thefile['path'])) {
327 $filesize = filesize($thefile['path']);
328 if ($maxbytes != -1 && $filesize>$maxbytes) {
329 unlink($thefile['path']);
330 print_error('maxbytes');
332 // Ensure the file will not make the area exceed its size limit.
333 if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, $filesize)) {
334 unlink($thefile['path']);
335 print_error('maxareabytes');
337 try {
338 $info = repository::move_to_filepool($thefile['path'], $record);
339 redirect($home_url, get_string('downloadsucc', 'repository'));
340 } catch (moodle_exception $e) {
341 // inject target URL
342 $e->link = $PAGE->url->out();
343 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
344 throw $e;
346 } else {
347 print_error('cannotdownload', 'repository');
351 break;
353 case 'confirm':
354 echo $OUTPUT->header();
355 echo '<div><a href="'.s($PAGE->url->out(false)).'">'.get_string('back', 'repository').'</a></div>';
356 echo '<img src="'.$thumbnail.'" />';
357 echo '<form method="post">';
358 echo '<table>';
359 echo ' <tr>';
360 echo ' <td>'. html_writer::label(get_string('filename', 'repository'), 'filename'). '</td>';
361 echo ' <td><input type="text" id="filename" name="filename" value="'.s($filename).'" /></td>';
362 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
363 echo ' <td><input type="hidden" name="action" value="download" /></td>';
364 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
365 echo ' </tr>';
366 echo '</table>';
367 echo '<div>';
368 // the save path
369 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
370 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
371 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
372 echo '</div>';
373 echo '</form>';
374 echo $OUTPUT->footer();
375 break;
377 default:
378 case 'plugins':
379 $params = array();
380 $params['context'] = array($user_context, context_system::instance());
381 $params['currentcontext'] = $PAGE->context;
382 $params['return_types'] = FILE_INTERNAL;
384 $repos = repository::get_instances($params);
385 echo $OUTPUT->header();
386 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
387 echo '<div>';
388 echo '<ul>';
389 foreach($repos as $repo) {
390 $info = $repo->get_meta();
392 $aurl = clone($url);
393 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
395 echo '<li>';
396 echo html_writer::empty_tag('img', array('src'=>$info->icon, 'alt'=>$info->name, 'class'=>'icon icon-pre'));
397 echo html_writer::link($aurl, s($info->name));
398 echo '</li>';
400 echo '</ul>';
401 echo '</div>';
402 echo $OUTPUT->footer();
403 break;