MDL-54948 core_upgrade: fix check_unoconv_version logic
[moodle.git] / repository / filepicker.php
blobdccec4270e5e6e7889719027fa1e1b9639b43ca2
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 $usespost = true;
82 if ($repo_id) {
83 // Get repository instance information
84 $repooptions = array(
85 'ajax' => false,
86 'mimetypes' => $accepted_types
88 $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
90 // Check permissions
91 $repo->check_capability();
93 $usespost = $repo->uses_post_requests();
96 $context = context::instance_by_id($contextid);
98 // Make sure maxbytes passed is within site filesize limits.
99 $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes, $maxbytes, null, $usespost);
101 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
102 $params['action'] = 'browse';
103 $params['draftpath'] = $draftpath;
104 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
106 $params['savepath'] = $savepath;
107 $params['repo_id'] = $repo_id;
108 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
109 $PAGE->set_url('/repository/filepicker.php', $params);
111 switch ($action) {
112 case 'upload':
113 // The uploaded file has been processed in plugin construct function
114 // redirect to default page
115 try {
116 $repo->upload('', $maxbytes);
117 redirect($home_url, get_string('uploadsucc','repository'));
118 } catch (moodle_exception $e) {
119 // inject target URL
120 $e->link = $PAGE->url->out();
121 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
122 throw $e;
124 break;
126 case 'search':
127 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
128 try {
129 $search_result = $repo->search($search_text);
130 $search_result['issearchresult'] = true;
131 $search_result['repo_id'] = $repo_id;
133 // TODO: need a better solution
134 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
135 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
136 echo $OUTPUT->render($pagingbar);
138 echo '<table>';
139 foreach ($search_result['list'] as $item) {
140 echo '<tr>';
141 echo '<td>';
142 $style = '';
143 if (isset($item['thumbnail_height'])) {
144 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
146 if (isset($item['thumbnail_width'])) {
147 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
149 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'alt' => '', 'style' => $style));
150 echo '</td><td>';
151 if (!empty($item['url'])) {
152 echo html_writer::link($item['url'], s($item['title']), array('target'=>'_blank'));
153 } else {
154 echo s($item['title']);
156 echo '</td>';
157 echo '<td>';
158 echo '<form method="post">';
159 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
160 echo '<input type="hidden" name="action" value="confirm"/>';
161 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
162 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
163 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
164 echo '</form>';
165 echo '</td>';
166 echo '</tr>';
168 echo '</table>';
169 } catch (repository_exception $e) {
171 break;
173 case 'list':
174 case 'sign':
175 echo $OUTPUT->header();
177 echo $OUTPUT->container_start();
178 echo html_writer::link($url, get_string('back', 'repository'));
179 echo $OUTPUT->container_end();
181 if ($repo->check_login()) {
182 $list = $repo->get_listing($req_path, $curr_page);
183 $dynload = !empty($list['dynload'])?true:false;
184 if (!empty($list['upload'])) {
185 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
186 echo '<label>'.$list['upload']['label'].': </label>';
187 echo '<input type="file" name="repo_upload_file" /><br />';
188 echo '<input type="hidden" name="action" value="upload" /><br />';
189 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
190 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
191 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
192 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
193 echo '</form>';
194 } else {
195 if (!empty($list['path'])) {
196 foreach ($list['path'] as $p) {
197 //echo '<form method="post" style="display:inline">';
198 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
199 //echo '<input type="hidden" name="action" value="list"';
200 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
201 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
202 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
203 //echo '</form>';
205 $pathurl = new moodle_url($url, array(
206 'p'=>$p['path'],
207 'action'=>'list',
208 'draftpath'=>$draftpath,
209 'savepath'=>$savepath
211 echo '<strong>' . html_writer::link($pathurl, s($p['name'])) . '</strong>';
212 echo '<span> / </span>';
215 if (!empty($list['page'])) {
216 // TODO MDL-28482: need a better solution
217 // paging_bar is not a good option because it starts page numbering from 0 and
218 // repositories number pages starting from 1.
219 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
220 if (!isset($list['perpage']) && !isset($list['total'])) {
221 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
223 if (empty($list['total'])) {
224 if ($list['pages'] == -1) {
225 $total = ($list['page'] + 2) * $list['perpage'];
226 } else {
227 $total = $list['pages'] * $list['perpage'];
229 } else {
230 $total = $list['total'];
232 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
234 echo '<table>';
235 foreach ($list['list'] as $item) {
236 echo '<tr>';
237 echo '<td>';
238 $style = '';
239 if (isset($item['thumbnail_height'])) {
240 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
242 if (isset($item['thumbnail_width'])) {
243 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
245 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style));
246 echo '</td><td>';
247 if (!empty($item['url'])) {
248 echo html_writer::link($item['url'], s($item['title']), array('target'=>'_blank'));
249 } else {
250 echo s($item['title']);
252 echo '</td>';
253 echo '<td>';
254 if (!isset($item['children'])) {
255 echo '<form method="post">';
256 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
257 echo '<input type="hidden" name="action" value="confirm"/>';
258 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
259 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
260 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
261 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
262 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
263 echo '</form>';
264 } else {
265 echo '<form method="post">';
266 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
267 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
268 echo '</form>';
270 echo '</td>';
271 echo '</tr>';
273 echo '</table>';
275 } else {
276 echo '<form method="post">';
277 echo '<input type="hidden" name="action" value="sign" />';
278 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
279 // HACK to prevent browsers from automatically inserting the user's password into the wrong fields.
280 echo prevent_form_autofill_password();
281 $repo->print_login();
282 echo '</form>';
284 echo $OUTPUT->footer();
285 break;
287 case 'download':
288 // Check that user has permission to access this file
289 if (!$repo->file_is_accessible($fileurl)) {
290 print_error('storedfilecannotread');
292 $record = new stdClass();
293 $reference = $repo->get_file_reference($fileurl);
295 $sourcefield = $repo->get_file_source_info($fileurl);
296 $record->source = repository::build_source_field($sourcefield);
298 // If file is already a reference, set $fileurl = file source, $repo = file repository
299 // note that in this case user may not have permission to access the source file directly
300 // so no file_browser/file_info can be used below
301 if ($repo->has_moodle_files()) {
302 $file = repository::get_moodle_file($reference);
303 if ($file && $file->is_external_file()) {
304 $sourcefield = $file->get_source(); // remember the original source
305 $record->source = $repo::build_source_field($sourcefield);
306 $reference = $file->get_reference();
307 $repo_id = $file->get_repository_id();
308 $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
312 $record->filepath = $savepath;
313 $record->filename = $filename;
314 $record->component = 'user';
315 $record->filearea = 'draft';
316 $record->itemid = $itemid;
317 $record->license = '';
318 $record->author = '';
320 $now = time();
321 $record->timecreated = $now;
322 $record->timemodified = $now;
323 $record->userid = $USER->id;
324 $record->contextid = $user_context->id;
325 $record->sortorder = 0;
327 if ($repo->has_moodle_files()) {
328 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes);
329 redirect($home_url, get_string('downloadsucc', 'repository'));
330 } else {
331 $thefile = $repo->get_file($reference, $filename);
332 if (!empty($thefile['path'])) {
333 $filesize = filesize($thefile['path']);
334 if ($maxbytes != -1 && $filesize>$maxbytes) {
335 unlink($thefile['path']);
336 print_error('maxbytes');
338 // Ensure the file will not make the area exceed its size limit.
339 if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, $filesize)) {
340 unlink($thefile['path']);
341 print_error('maxareabytes');
343 try {
344 $info = repository::move_to_filepool($thefile['path'], $record);
345 redirect($home_url, get_string('downloadsucc', 'repository'));
346 } catch (moodle_exception $e) {
347 // inject target URL
348 $e->link = $PAGE->url->out();
349 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
350 throw $e;
352 } else {
353 print_error('cannotdownload', 'repository');
357 break;
359 case 'confirm':
360 echo $OUTPUT->header();
361 echo '<div><a href="'.s($PAGE->url->out(false)).'">'.get_string('back', 'repository').'</a></div>';
362 echo '<img src="'.$thumbnail.'" />';
363 echo '<form method="post">';
364 echo '<table>';
365 echo ' <tr>';
366 echo ' <td>'. html_writer::label(get_string('filename', 'repository'), 'filename'). '</td>';
367 echo ' <td><input type="text" id="filename" name="filename" value="'.s($filename).'" /></td>';
368 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
369 echo ' <td><input type="hidden" name="action" value="download" /></td>';
370 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
371 echo ' </tr>';
372 echo '</table>';
373 echo '<div>';
374 // the save path
375 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
376 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
377 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
378 echo '</div>';
379 echo '</form>';
380 echo $OUTPUT->footer();
381 break;
383 default:
384 case 'plugins':
385 $params = array();
386 $params['context'] = array($user_context, context_system::instance());
387 $params['currentcontext'] = $PAGE->context;
388 $params['return_types'] = FILE_INTERNAL;
390 $repos = repository::get_instances($params);
391 echo $OUTPUT->header();
392 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
393 echo '<div>';
394 echo '<ul>';
395 foreach($repos as $repo) {
396 $info = $repo->get_meta();
398 $aurl = clone($url);
399 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
401 echo '<li>';
402 echo html_writer::empty_tag('img', array('src'=>$info->icon, 'alt'=>$info->name, 'class'=>'icon icon-pre'));
403 echo html_writer::link($aurl, s($info->name));
404 echo '</li>';
406 echo '</ul>';
407 echo '</div>';
408 echo $OUTPUT->footer();
409 break;