MDL-27764 restore - always restore as much info as possible (1.9 approach)
[moodle.git] / repository / filepicker.php
blobb7b1a65f3f3c23ea7afe37b0f3e33196c8c81801
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 $moodle_maxbytes = get_max_upload_file_size();
97 // to prevent maxbytes greater than moodle maxbytes setting
98 if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
99 $maxbytes = $moodle_maxbytes;
102 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
103 $params['action'] = 'browse';
104 $params['draftpath'] = $draftpath;
105 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
107 $params['savepath'] = $savepath;
108 $params['repo_id'] = $repo_id;
109 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
110 $PAGE->set_url('/repository/filepicker.php', $params);
112 switch ($action) {
113 case 'upload':
114 // The uploaded file has been processed in plugin construct function
115 // redirect to default page
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;
125 break;
127 case 'search':
128 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
129 try {
130 $search_result = $repo->search($search_text);
131 $search_result['issearchresult'] = true;
132 $search_result['repo_id'] = $repo_id;
134 // TODO: need a better solution
135 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
136 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
137 echo $OUTPUT->render($pagingbar);
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'])) {
145 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
146 } else {
147 echo $item['title'];
149 echo '</td>';
150 echo '<td>';
151 echo '<form method="post">';
152 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
153 echo '<input type="hidden" name="action" value="confirm"/>';
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')).'" />';
157 echo '</form>';
158 echo '</td>';
159 echo '</tr>';
161 echo '</table>';
162 } catch (repository_exception $e) {
164 break;
166 case 'list':
167 case 'sign':
168 echo $OUTPUT->header();
170 echo $OUTPUT->container_start();
171 echo html_writer::link($url, get_string('back', 'repository'));
172 echo $OUTPUT->container_end();
174 if ($repo->check_login()) {
175 $list = $repo->get_listing($req_path, $curr_page);
176 $dynload = !empty($list['dynload'])?true:false;
177 if (!empty($list['upload'])) {
178 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
179 echo '<label>'.$list['upload']['label'].': </label>';
180 echo '<input type="file" name="repo_upload_file" /><br />';
181 echo '<input type="hidden" name="action" value="upload" /><br />';
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')).'" />';
186 echo '</form>';
187 } else {
188 if (!empty($list['path'])) {
189 foreach ($list['path'] as $p) {
190 //echo '<form method="post" style="display:inline">';
191 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
192 //echo '<input type="hidden" name="action" value="list"';
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']).'" />';
196 //echo '</form>';
198 $pathurl = new moodle_url($url, array(
199 'p'=>$p['path'],
200 'action'=>'list',
201 'draftpath'=>$draftpath,
202 'savepath'=>$savepath
204 echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
205 echo '<span> / </span>';
208 if (!empty($list['page'])) {
209 // TODO: need a better solution
210 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid");
211 echo $OUTPUT->paging_bar($list['total'], $list['page'] - 1, $list['perpage'], $pagingurl);
213 echo '<table>';
214 foreach ($list['list'] as $item) {
215 echo '<tr>';
216 echo '<td><img src="'.$item['thumbnail'].'" />';
217 echo '</td><td>';
218 if (!empty($item['url'])) {
219 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
220 } else {
221 echo $item['title'];
223 echo '</td>';
224 echo '<td>';
225 if (!isset($item['children'])) {
226 echo '<form method="post">';
227 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
228 echo '<input type="hidden" name="action" value="confirm"/>';
229 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
230 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
231 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
232 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
233 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
234 echo '</form>';
235 } else {
236 echo '<form method="post">';
237 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
238 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
239 echo '</form>';
241 echo '</td>';
242 echo '</tr>';
244 echo '</table>';
246 } else {
247 echo '<form method="post">';
248 echo '<input type="hidden" name="action" value="sign" />';
249 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
250 $repo->print_login();
251 echo '</form>';
253 echo $OUTPUT->footer();
254 break;
256 case 'download':
257 $thefile = $repo->get_file($fileurl, $filename);
258 $filesize = filesize($thefile['path']);
259 if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
260 print_error('maxbytes');
262 if (!empty($thefile)) {
263 $record = new stdClass();
264 $record->filepath = $savepath;
265 $record->filename = $filename;
266 $record->component = 'user';
267 $record->filearea = 'draft';
268 $record->itemid = $itemid;
269 $record->license = '';
270 $record->author = '';
271 $record->source = $thefile['url'];
272 try {
273 $info = repository::move_to_filepool($thefile['path'], $record);
274 redirect($home_url, get_string('downloadsucc', 'repository'));
275 } catch (moodle_exception $e) {
276 // inject target URL
277 $e->link = $PAGE->url->out();
278 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
279 throw $e;
281 } else {
282 print_error('cannotdownload', 'repository');
285 break;
287 case 'confirm':
288 echo $OUTPUT->header();
289 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
290 echo '<img src="'.$thumbnail.'" />';
291 echo '<form method="post">';
292 echo '<table>';
293 echo ' <tr>';
294 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
295 echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
296 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
297 echo ' <td><input type="hidden" name="action" value="download" /></td>';
298 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
299 echo ' </tr>';
300 echo '</table>';
301 echo '<div>';
302 // the save path
303 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
304 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
305 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
306 echo '</div>';
307 echo '</form>';
308 echo $OUTPUT->footer();
309 break;
311 default:
312 case 'plugins':
313 $params = array();
314 $params['context'] = array($user_context, get_system_context());
315 $params['currentcontext'] = $PAGE->context;
316 $params['return_types'] = FILE_INTERNAL;
318 $repos = repository::get_instances($params);
319 echo $OUTPUT->header();
320 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
321 echo '<div>';
322 echo '<ul>';
323 foreach($repos as $repo) {
324 $info = $repo->get_meta();
326 $aurl = clone($url);
327 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
329 echo '<li>';
330 echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
331 echo html_writer::link($aurl, $info->name);
332 echo '</li>';
334 echo '</ul>';
335 echo '</div>';
336 echo $OUTPUT->footer();
337 break;