weekly release 2.2.4+
[moodle.git] / repository / filepicker.php
blobfa759c56a810be2196b8f8deb98f5a39e3bb5eeb
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 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 set_time_limit(0);
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
63 // the path to save files
64 $savepath = optional_param('savepath', '/', PARAM_PATH);
65 // path in draft area
66 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
69 // user context
70 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
72 $PAGE->set_context($user_context);
73 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
74 print_error('invalidcourseid');
76 $PAGE->set_course($course);
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';
81 if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
82 $type = $repository->type;
83 if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
84 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
85 $classname = 'repository_' . $type;
86 try {
87 $repo = new $classname($repo_id, $contextid, array('ajax'=>false, 'name'=>$repository->name, 'type'=>$type));
88 } catch (repository_exception $e){
89 print_error('pluginerror', 'repository');
91 } else {
92 print_error('invalidplugin', 'repository');
96 // Make sure maxbytes passed is within site filesize limits.
97 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $coursemaxbytes, $maxbytes);
99 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
100 $params['action'] = 'browse';
101 $params['draftpath'] = $draftpath;
102 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
104 $params['savepath'] = $savepath;
105 $params['repo_id'] = $repo_id;
106 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
107 $PAGE->set_url('/repository/filepicker.php', $params);
109 switch ($action) {
110 case 'upload':
111 // The uploaded file has been processed in plugin construct function
112 // redirect to default page
113 try {
114 $repo->upload('', $maxbytes);
115 redirect($home_url, get_string('uploadsucc','repository'));
116 } catch (moodle_exception $e) {
117 // inject target URL
118 $e->link = $PAGE->url->out();
119 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
120 throw $e;
122 break;
124 case 'search':
125 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
126 try {
127 $search_result = $repo->search($search_text);
128 $search_result['issearchresult'] = true;
129 $search_result['repo_id'] = $repo_id;
131 // TODO: need a better solution
132 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
133 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
134 echo $OUTPUT->render($pagingbar);
136 echo '<table>';
137 foreach ($search_result['list'] as $item) {
138 echo '<tr>';
139 echo '<td><img src="'.$item['thumbnail'].'" />';
140 echo '</td><td>';
141 if (!empty($item['url'])) {
142 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
143 } else {
144 echo $item['title'];
146 echo '</td>';
147 echo '<td>';
148 echo '<form method="post">';
149 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
150 echo '<input type="hidden" name="action" value="confirm"/>';
151 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
152 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
153 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
154 echo '</form>';
155 echo '</td>';
156 echo '</tr>';
158 echo '</table>';
159 } catch (repository_exception $e) {
161 break;
163 case 'list':
164 case 'sign':
165 echo $OUTPUT->header();
167 echo $OUTPUT->container_start();
168 echo html_writer::link($url, get_string('back', 'repository'));
169 echo $OUTPUT->container_end();
171 if ($repo->check_login()) {
172 $list = $repo->get_listing($req_path, $curr_page);
173 $dynload = !empty($list['dynload'])?true:false;
174 if (!empty($list['upload'])) {
175 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
176 echo '<label>'.$list['upload']['label'].': </label>';
177 echo '<input type="file" name="repo_upload_file" /><br />';
178 echo '<input type="hidden" name="action" value="upload" /><br />';
179 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
180 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
181 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
182 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
183 echo '</form>';
184 } else {
185 if (!empty($list['path'])) {
186 foreach ($list['path'] as $p) {
187 //echo '<form method="post" style="display:inline">';
188 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
189 //echo '<input type="hidden" name="action" value="list"';
190 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
191 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
192 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
193 //echo '</form>';
195 $pathurl = new moodle_url($url, array(
196 'p'=>$p['path'],
197 'action'=>'list',
198 'draftpath'=>$draftpath,
199 'savepath'=>$savepath
201 echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
202 echo '<span> / </span>';
205 if (!empty($list['page'])) {
206 // TODO MDL-28482: need a better solution
207 // paging_bar is not a good option because it starts page numbering from 0 and
208 // repositories number pages starting from 1.
209 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
210 if (!isset($list['perpage']) && !isset($list['total'])) {
211 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
213 if (empty($list['total'])) {
214 if ($list['pages'] == -1) {
215 $total = ($list['page'] + 2) * $list['perpage'];
216 } else {
217 $total = $list['pages'] * $list['perpage'];
219 } else {
220 $total = $list['total'];
222 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
224 echo '<table>';
225 foreach ($list['list'] as $item) {
226 echo '<tr>';
227 echo '<td><img src="'.$item['thumbnail'].'" />';
228 echo '</td><td>';
229 if (!empty($item['url'])) {
230 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
231 } else {
232 echo $item['title'];
234 echo '</td>';
235 echo '<td>';
236 if (!isset($item['children'])) {
237 echo '<form method="post">';
238 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
239 echo '<input type="hidden" name="action" value="confirm"/>';
240 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
241 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
242 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
243 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
244 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
245 echo '</form>';
246 } else {
247 echo '<form method="post">';
248 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
249 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
250 echo '</form>';
252 echo '</td>';
253 echo '</tr>';
255 echo '</table>';
257 } else {
258 echo '<form method="post">';
259 echo '<input type="hidden" name="action" value="sign" />';
260 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
261 $repo->print_login();
262 echo '</form>';
264 echo $OUTPUT->footer();
265 break;
267 case 'download':
268 $thefile = $repo->get_file($fileurl, $filename);
269 $filesize = filesize($thefile['path']);
270 if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
271 print_error('maxbytes');
273 if (!empty($thefile)) {
274 $record = new stdClass();
275 $record->filepath = $savepath;
276 $record->filename = $filename;
277 $record->component = 'user';
278 $record->filearea = 'draft';
279 $record->itemid = $itemid;
280 $record->license = '';
281 $record->author = '';
282 $record->source = $thefile['url'];
283 try {
284 $info = repository::move_to_filepool($thefile['path'], $record);
285 redirect($home_url, get_string('downloadsucc', 'repository'));
286 } catch (moodle_exception $e) {
287 // inject target URL
288 $e->link = $PAGE->url->out();
289 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
290 throw $e;
292 } else {
293 print_error('cannotdownload', 'repository');
296 break;
298 case 'confirm':
299 echo $OUTPUT->header();
300 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
301 echo '<img src="'.$thumbnail.'" />';
302 echo '<form method="post">';
303 echo '<table>';
304 echo ' <tr>';
305 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
306 echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
307 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
308 echo ' <td><input type="hidden" name="action" value="download" /></td>';
309 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
310 echo ' </tr>';
311 echo '</table>';
312 echo '<div>';
313 // the save path
314 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
315 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
316 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
317 echo '</div>';
318 echo '</form>';
319 echo $OUTPUT->footer();
320 break;
322 default:
323 case 'plugins':
324 $params = array();
325 $params['context'] = array($user_context, get_system_context());
326 $params['currentcontext'] = $PAGE->context;
327 $params['return_types'] = FILE_INTERNAL;
329 $repos = repository::get_instances($params);
330 echo $OUTPUT->header();
331 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
332 echo '<div>';
333 echo '<ul>';
334 foreach($repos as $repo) {
335 $info = $repo->get_meta();
337 $aurl = clone($url);
338 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
340 echo '<li>';
341 echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
342 echo html_writer::link($aurl, $info->name);
343 echo '</li>';
345 echo '</ul>';
346 echo '</div>';
347 echo $OUTPUT->footer();
348 break;