weekly release 2.3dev
[moodle.git] / repository / filepicker.php
CommitLineData
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 29require_once('../config.php');
30require_once($CFG->libdir.'/filelib.php');
31require_once('lib.php');
b6fd5aee 32/// Wait as long as it takes for this script to finish
33set_time_limit(0);
34
71267723 35require_sesskey();
b6fd5aee 36require_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
563d0417 70$user_context = get_context_instance(CONTEXT_USER, $USER->id);
8ea068a2 71
563d0417 72$PAGE->set_context($user_context);
a14a933c
DC
73if (!$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 81if ($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
75d138a3
DC
96$moodle_maxbytes = get_max_upload_file_size();
97// to prevent maxbytes greater than moodle maxbytes setting
98if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
99 $maxbytes = $moodle_maxbytes;
100}
101
71267723 102$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
563d0417
DC
103$params['action'] = 'browse';
104$params['draftpath'] = $draftpath;
105$home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
106
107$params['savepath'] = $savepath;
bf1873c6 108$params['repo_id'] = $repo_id;
563d0417
DC
109$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
110$PAGE->set_url('/repository/filepicker.php', $params);
b6fd5aee 111
14f3c882 112switch ($action) {
ab9cdbb9 113case 'upload':
009a97ce 114 // The uploaded file has been processed in plugin construct function
8ea068a2 115 // redirect to default page
84641adc
PS
116 try {
117 $repo->upload('', $maxbytes);
118 redirect($home_url, get_string('uploadsucc','repository'));
119 } catch (moodle_exception $e) {
120 // inject target URL
121 $e->link = $PAGE->url->out();
122 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
123 throw $e;
124 }
14f3c882 125 break;
8ea068a2 126
78ff2983 127case 'search':
c33aa23b 128 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
78ff2983 129 try {
130 $search_result = $repo->search($search_text);
29199e56 131 $search_result['issearchresult'] = true;
78ff2983 132 $search_result['repo_id'] = $repo_id;
009a97ce 133
134 // TODO: need a better solution
ef3e1fbc 135 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
929d7a83
PS
136 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
137 echo $OUTPUT->render($pagingbar);
009a97ce 138
78ff2983 139 echo '<table>';
140 foreach ($search_result['list'] as $item) {
141 echo '<tr>';
142 echo '<td><img src="'.$item['thumbnail'].'" />';
143 echo '</td><td>';
144 if (!empty($item['url'])) {
71267723 145 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
78ff2983 146 } else {
147 echo $item['title'];
148 }
149 echo '</td>';
150 echo '<td>';
009a97ce 151 echo '<form method="post">';
71267723 152 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
009a97ce 153 echo '<input type="hidden" name="action" value="confirm"/>';
71267723
DC
154 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
155 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
156 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
009a97ce 157 echo '</form>';
78ff2983 158 echo '</td>';
009a97ce 159 echo '</tr>';
78ff2983 160 }
161 echo '</table>';
162 } catch (repository_exception $e) {
163 }
164 break;
8ea068a2 165
14f3c882 166case 'list':
167case 'sign':
af804e3e 168 echo $OUTPUT->header();
71267723
DC
169
170 echo $OUTPUT->container_start();
171 echo html_writer::link($url, get_string('back', 'repository'));
172 echo $OUTPUT->container_end();
173
761265ad 174 if ($repo->check_login()) {
6f2cd52a 175 $list = $repo->get_listing($req_path, $curr_page);
761265ad 176 $dynload = !empty($list['dynload'])?true:false;
177 if (!empty($list['upload'])) {
47ce714b 178 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
ab9cdbb9 179 echo '<label>'.$list['upload']['label'].': </label>';
761265ad 180 echo '<input type="file" name="repo_upload_file" /><br />';
ab9cdbb9 181 echo '<input type="hidden" name="action" value="upload" /><br />';
71267723
DC
182 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
183 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
184 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
185 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
761265ad 186 echo '</form>';
187 } else {
5e98ab96 188 if (!empty($list['path'])) {
189 foreach ($list['path'] as $p) {
bf1873c6 190 //echo '<form method="post" style="display:inline">';
71267723 191 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
bf1873c6 192 //echo '<input type="hidden" name="action" value="list"';
71267723
DC
193 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
194 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
195 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
bf1873c6
DC
196 //echo '</form>';
197
198 $pathurl = new moodle_url($url, array(
199 'p'=>$p['path'],
200 'action'=>'list',
201 'draftpath'=>$draftpath,
202 'savepath'=>$savepath
203 ));
204 echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
205 echo '<span> / </span>';
5e98ab96 206 }
761265ad 207 }
009a97ce 208 if (!empty($list['page'])) {
20ee5084
MG
209 // TODO MDL-28482: need a better solution
210 // paging_bar is not a good option because it starts page numbering from 0 and
211 // repositories number pages starting from 1.
212 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
213 if (!isset($list['perpage']) && !isset($list['total'])) {
214 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
215 }
216 if (empty($list['total'])) {
217 if ($list['pages'] == -1) {
218 $total = ($list['page'] + 2) * $list['perpage'];
219 } else {
220 $total = $list['pages'] * $list['perpage'];
221 }
222 } else {
223 $total = $list['total'];
224 }
225 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
009a97ce 226 }
761265ad 227 echo '<table>';
228 foreach ($list['list'] as $item) {
229 echo '<tr>';
230 echo '<td><img src="'.$item['thumbnail'].'" />';
231 echo '</td><td>';
232 if (!empty($item['url'])) {
71267723 233 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
761265ad 234 } else {
235 echo $item['title'];
236 }
237 echo '</td>';
238 echo '<td>';
239 if (!isset($item['children'])) {
240 echo '<form method="post">';
71267723 241 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
761265ad 242 echo '<input type="hidden" name="action" value="confirm"/>';
71267723
DC
243 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
244 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
245 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
246 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
247 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
761265ad 248 echo '</form>';
249 } else {
250 echo '<form method="post">';
71267723
DC
251 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
252 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
761265ad 253 echo '</form>';
254 }
255 echo '</td>';
009a97ce 256 echo '</tr>';
761265ad 257 }
258 echo '</table>';
259 }
260 } else {
261 echo '<form method="post">';
761265ad 262 echo '<input type="hidden" name="action" value="sign" />';
71267723 263 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
78ff2983 264 $repo->print_login();
761265ad 265 echo '</form>';
266 }
6fd42112 267 echo $OUTPUT->footer();
14f3c882 268 break;
8ea068a2 269
14f3c882 270case 'download':
9efa43f3 271 $thefile = $repo->get_file($fileurl, $filename);
563d0417
DC
272 $filesize = filesize($thefile['path']);
273 if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
274 print_error('maxbytes');
275 }
14469892 276 if (!empty($thefile)) {
6bdfef5d 277 $record = new stdClass();
563d0417 278 $record->filepath = $savepath;
e5051df3 279 $record->filename = $filename;
64f93798
PS
280 $record->component = 'user';
281 $record->filearea = 'draft';
e5051df3
DC
282 $record->itemid = $itemid;
283 $record->license = '';
284 $record->author = '';
285 $record->source = $thefile['url'];
84641adc
PS
286 try {
287 $info = repository::move_to_filepool($thefile['path'], $record);
288 redirect($home_url, get_string('downloadsucc', 'repository'));
289 } catch (moodle_exception $e) {
290 // inject target URL
291 $e->link = $PAGE->url->out();
292 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
293 throw $e;
294 }
009a97ce 295 } else {
296 print_error('cannotdownload', 'repository');
14f3c882 297 }
298
299 break;
8ea068a2 300
14f3c882 301case 'confirm':
af804e3e 302 echo $OUTPUT->header();
b6fd5aee 303 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
8ea068a2 304 echo '<img src="'.$thumbnail.'" />';
305 echo '<form method="post">';
306 echo '<table>';
307 echo ' <tr>';
3211569a
SH
308 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
309 echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
71267723 310 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
8ea068a2 311 echo ' <td><input type="hidden" name="action" value="download" /></td>';
71267723 312 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
8ea068a2 313 echo ' </tr>';
14f3c882 314 echo '</table>';
315 echo '<div>';
8ea068a2 316 // the save path
71267723
DC
317 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
318 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
319 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
14f3c882 320 echo '</div>';
321 echo '</form>';
6fd42112 322 echo $OUTPUT->footer();
14f3c882 323 break;
8ea068a2 324
563d0417 325default:
ab9cdbb9 326case 'plugins':
6f2cd52a
DC
327 $params = array();
328 $params['context'] = array($user_context, get_system_context());
329 $params['currentcontext'] = $PAGE->context;
563d0417
DC
330 $params['return_types'] = FILE_INTERNAL;
331
6f2cd52a 332 $repos = repository::get_instances($params);
af804e3e 333 echo $OUTPUT->header();
563d0417 334 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
8ea068a2 335 echo '<div>';
336 echo '<ul>';
ab9cdbb9 337 foreach($repos as $repo) {
338 $info = $repo->get_meta();
8ae8bf8a
PS
339
340 $aurl = clone($url);
563d0417 341 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
8ae8bf8a 342
e5051df3 343 echo '<li>';
563d0417 344 echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
e5051df3
DC
345 echo html_writer::link($aurl, $info->name);
346 echo '</li>';
ab9cdbb9 347 }
8ea068a2 348 echo '</ul>';
349 echo '</div>';
350 echo $OUTPUT->footer();
351 break;
af804e3e 352}