MDL-23065, fix non js filepicker
[moodle.git] / repository / draftfiles_manager.php
CommitLineData
563d0417
DC
1<?php
2
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
19
20//
21// NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!!
22//
23
24
25/**
26 * This file is used to manage draft files in non-javascript browsers
27 *
28 * @since 2.0
29 * @package moodlecore
30 * @subpackage repository
31 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34
35require_once('../config.php');
36require_once($CFG->libdir.'/filelib.php');
37require_once('lib.php');
38
39require_login();
40
41// disable blocks in this page
42$PAGE->set_pagelayout('embedded');
43
44// general parameters
45$action = optional_param('action', '', PARAM_ALPHA);
46$itemid = optional_param('itemid', '', PARAM_INT);
47
48// parameters for repository
49$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
50$courseid = optional_param('course', SITEID, PARAM_INT); // course ID
51$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
52$filename = optional_param('filename', '', PARAM_FILE);
53$targetpath = optional_param('targetpath', '', PARAM_PATH);
54$maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
55$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
56$subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
57
58// draft area
59$newdirname = optional_param('newdirname', '', PARAM_FILE);
60$newfilename = optional_param('newfilename', '', PARAM_FILE);
61// path in draft area
62$draftpath = optional_param('draftpath', '/', PARAM_PATH);
63
64// user context
65$user_context = get_context_instance(CONTEXT_USER, $USER->id);
66
67
68$PAGE->set_context($user_context);
69
70$fs = get_file_storage();
71
72$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs);
73$PAGE->set_url('/repository/draftfiles_manager.php', $params);
74$filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
75
76$params['action'] = 'browse';
77$home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
78
79switch ($action) {
80
81 // delete draft files
82case 'deletedraft':
83 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
84 if ($file->is_directory()) {
85 $pathname = $draftpath;
86 if ($file->get_parent_directory()) {
87 $draftpath = $file->get_parent_directory()->get_filepath();
88 } else {
89 $draftpath = '/';
90 }
91
92 // delete files in folder
93 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
94 foreach ($files as $storedfile) {
95 $storedfile->delete();
96 }
97 $file->delete();
98 } else {
99 $file->delete();
100 }
101 $home_url->param('draftpath', $draftpath);
102 $home_url->param('action', 'browse');
103 redirect($home_url);
104 }
105 break;
106
107case 'renameform':
108 echo $OUTPUT->header();
109 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
110 $home_url->param('draftpath', $draftpath);
111 $home_url->param('action', 'rename');
112 echo ' <form method="post" action="'.$home_url->out().'">';
113 echo ' <input name="newfilename" type="text" value="'.$filename.'" />';
114 echo ' <input name="filename" type="hidden" value="'.$filename.'" />';
115 echo ' <input name="draftpath" type="hidden" value="'.$draftpath.'" />';
116 echo ' <input type="submit" value="'.get_string('rename', 'moodle').'" />';
117 echo ' </form>';
118 echo $OUTPUT->footer();
119 break;
120
121case 'rename':
122
123 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) {
124 print_error('fileexists');
125 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
126 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
127 $file->delete();
128 }
129
130 $home_url->param('action', 'browse');
131 $home_url->param('draftpath', $draftpath);
132 redirect($home_url);
133 break;
134
135case 'downloaddir':
136 $zipper = new zip_packer();
137
138 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
139 if ($file->get_parent_directory()) {
140 $parent_path = $file->get_parent_directory()->get_filepath();
141 $filename = trim($draftpath, '/').'.zip';
142 } else {
143 $parent_path = '/';
144 $filename = get_string('files').'.zip';
145 }
146
147 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) {
148 $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out();
149 header('Location: ' . $fileurl );
150 } else {
151 print_error('cannotdownloaddir', 'repository');
152 }
153 break;
154
155case 'zip':
156 $zipper = new zip_packer();
157
158 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
159 if (!$file->get_parent_directory()) {
160 $parent_path = '/';
161 $filename = get_string('files').'.zip';
162 } else {
163 $parent_path = $file->get_parent_directory()->get_filepath();
164 $filepath = explode('/', trim($file->get_filepath(), '/'));
165 $filename = array_pop($filepath).'.zip';
166 }
167
168 $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
169
170 $home_url->param('action', 'browse');
171 $home_url->param('draftpath', $parent_path);
172 redirect($home_url, get_string('ziped', 'repository'));
173 break;
174
175case 'unzip':
176 $zipper = new zip_packer();
177 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
178
179 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
180 $str = get_string('unzipped', 'repository');
181 } else {
182 $str = get_string('cannotunzip', 'error');
183 }
184 $home_url->param('action', 'browse');
185 $home_url->param('draftpath', $draftpath);
186 redirect($home_url, $str);
187 break;
188
189case 'movefile':
190 if (!empty($targetpath)) {
191 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) {
192 print_error('cannotmovefile');
193 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
194 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file);
195 $file->delete();
196 } else {
197 var_dump('cannot find file');
198 die;
199 }
200 $home_url->param('action', 'browse');
201 $home_url->param('draftpath', $targetpath);
202 redirect($home_url);
203 }
204 echo $OUTPUT->header();
205 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
206 $data = new stdclass;
207 $home_url->param('action', 'movefile');
208 $home_url->param('draftpath', $draftpath);
209 $home_url->param('filename', $filename);
210 file_get_drafarea_folders($itemid, '/', $data);
211 print_draft_area_tree($data, true, $home_url);
212 echo $OUTPUT->footer();
213 break;
214
215case 'mkdirform':
216 echo $OUTPUT->header();
217 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
218 $home_url->param('draftpath', $draftpath);
219 $home_url->param('action', 'mkdir');
220 echo ' <form method="post" action="'.$home_url->out().'">';
221 echo ' <input name="newdirname" type="text" />';
222 echo ' <input name="draftpath" type="hidden" value="'.$draftpath.'" />';
223 echo ' <input type="submit" value="'.get_string('makeafolder', 'moodle').'" />';
224 echo ' </form>';
225 echo $OUTPUT->footer();
226 break;
227
228case 'mkdir':
229
230 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
231 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
232 $home_url->param('action', 'browse');
233 if (!empty($newdirname)) {
234 $home_url->param('draftpath', $newfolderpath);
235 $str = get_string('createfoldersuccess', 'repository');
236 } else {
237 $home_url->param('draftpath', $draftpath);
238 $str = get_string('createfolderfail', 'repository');
239 }
240 redirect($home_url, $str);
241 break;
242
243case 'browse':
244default:
245 $files = file_get_drafarea_files($itemid, $draftpath);
246 $info = file_get_draft_area_info($itemid);
247 $filecount = $info['filecount'];
248
249 echo $OUTPUT->header();
250 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
251 echo '<div class="fm-breadcrumb">';
252 $home_url->param('action', 'browse');
253 $home_url->param('draftpath', '/');
254 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
255 $trail = '';
256 if ($draftpath !== '/') {
257 $path = '/' . trim($draftpath, '/') . '/';
258 $parts = explode('/', $path);
259 foreach ($parts as $part) {
260 if (!empty($part)) {
261 $trail .= ('/'.$part.'/');
262 $data->path[] = array('name'=>$part, 'path'=>$trail);
263 $home_url->param('draftpath', $trail);
264 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
265 }
266 }
267 }
268 echo '</div>';
269 }
270
271 $filepicker_url->param('draftpath', $draftpath);
272 $filepicker_url->param('savepath', $draftpath);
273 $filepicker_url->param('action', 'plugins');
274 echo '<div class="filemanager-toolbar">';
275 if ($env == 'filepicker') {
276 $maxfiles = 1;
277 }
278 if ($filecount < $maxfiles && $maxfiles != -1) {
279 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
280 }
281 if ($env == 'filemanager') {
282 if (!empty($subdirs)) {
283 $home_url->param('action', 'mkdirform');
284 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
285 }
286 $home_url->param('action', 'downloaddir');
287 echo ' <a href="'.$home_url->out().'" target="_blank">'.get_string('downloadfolder', 'repository').'</a>';
288 }
289 echo '</div>';
290
291 if (!empty($files->list)) {
292 echo '<ul>';
293 foreach ($files->list as $file) {
294 if ($file->type != 'folder') {
295 $drafturl = $file->url;
296 // a file
297 $fileicon = $CFG->wwwroot.'/pix/'.(file_extension_icon($file->filename));
298 $type = str_replace('.gif', '', mimeinfo('icon', $file->filename));
299 echo '<li>';
300 echo '<img src="'.$fileicon. '" class="iconsmall" />';
301 echo ' <a href="'.$drafturl.'">'.$file->filename.'</a> ';
302
303 $home_url->param('filename', $file->filename);
304 $home_url->param('draftpath', $file->filepath);
305
306 $home_url->param('action', 'deletedraft');
307 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
308
309 $home_url->param('action', 'movefile');
310 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
311
312 $home_url->param('action', 'renameform');
313 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
314
315 if ($type == 'zip') {
316 $home_url->param('action', 'unzip');
317 $home_url->param('draftpath', $file->filepath);
318 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
319 }
320
321 echo '</li>';
322 } else {
323 // a folder
324 echo '<li>';
325 echo '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="iconsmall" />';
326
327 $home_url->param('action', 'browse');
328 $home_url->param('draftpath', $file->filepath);
329 $foldername = trim(array_pop(explode('/', trim($file->filepath, '/'))), '/');
330 echo ' <a href="'.$home_url->out().'">'.$foldername.'</a>';
331
332 $home_url->param('draftpath', $file->filepath);
333 $home_url->param('filename', $file->filename);
334 $home_url->param('action', 'deletedraft');
335 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
336
337 $home_url->param('action', 'zip');
338 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
339 echo '</li>';
340 }
341 }
342 echo '</ul>';
343 } else {
344 echo get_string('nofilesavailable', 'repository');
345 }
346 echo $OUTPUT->footer();
347 break;
348}
349
350function print_draft_area_tree($tree, $root, $url) {
351 echo '<ul>';
352 if ($root) {
353 $url->param('targetpath', '/');
354 if ($url->param('draftpath') == '/') {
355 echo '<li>'.get_string('files').'</li>';
356 } else {
357 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
358 }
359 echo '<ul>';
360 if (isset($tree->children)) {
361 $tree = $tree->children;
362 }
363 }
364
365 if (!empty($tree)) {
366 foreach ($tree as $node) {
367 echo '<li>';
368 $url->param('targetpath', $node->filepath);
369 if ($url->param('draftpath') != $node->filepath) {
370 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
371 } else {
372 echo $node->fullname;
373 }
374 echo '</li>';
375 if (!empty($node->children)) {
376 print_draft_area_tree($node->children, false, $url);
377 }
378 }
379 }
380 if ($root) {
381 echo '</ul>';
382 }
383 echo '</ul>';
384}