MDL-38878 behat: Also update composer dependencies after site install/reinstall
[moodle.git] / repository / draftfiles_manager.php
blobe398b5340d3148729a7336cb468ca9ca38f4fc2d
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/>.
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!!
25 /**
26 * This file is used to manage draft files in non-javascript browsers
28 * @since 2.0
29 * @package core
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
35 require_once('../config.php');
36 require_once($CFG->libdir.'/filelib.php');
37 require_once('lib.php');
39 require_sesskey();
40 require_login();
42 // disable blocks in this page
43 $PAGE->set_pagelayout('embedded');
45 // general parameters
46 $action = optional_param('action', '', PARAM_ALPHA);
47 $itemid = optional_param('itemid', '', PARAM_INT);
49 // parameters for repository
50 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
51 $courseid = optional_param('course', SITEID, PARAM_INT); // course ID
52 $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
53 $filename = optional_param('filename', '', PARAM_FILE);
54 $targetpath = optional_param('targetpath', '', PARAM_PATH);
55 $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
56 $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
57 $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
58 $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes.
60 // draft area
61 $newdirname = optional_param('newdirname', '', PARAM_FILE);
62 $newfilename = optional_param('newfilename', '', PARAM_FILE);
63 // path in draft area
64 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
66 // user context
67 $user_context = context_user::instance($USER->id);
70 $PAGE->set_context($user_context);
72 $fs = get_file_storage();
74 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
75 $PAGE->set_url('/repository/draftfiles_manager.php', $params);
76 $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
78 $params['action'] = 'browse';
79 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
81 switch ($action) {
83 // delete draft files
84 case 'deletedraft':
85 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
86 if ($file->is_directory()) {
87 $pathname = $draftpath;
88 if ($file->get_parent_directory()) {
89 $draftpath = $file->get_parent_directory()->get_filepath();
90 } else {
91 $draftpath = '/';
94 // delete files in folder
95 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
96 foreach ($files as $storedfile) {
97 $storedfile->delete();
99 $file->delete();
100 } else {
101 $file->delete();
103 $home_url->param('draftpath', $draftpath);
104 $home_url->param('action', 'browse');
105 redirect($home_url);
107 break;
109 case 'renameform':
110 echo $OUTPUT->header();
111 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
112 $home_url->param('draftpath', $draftpath);
113 $home_url->param('action', 'rename');
114 echo ' <form method="post" action="'.$home_url->out().'">';
115 echo html_writer::label(get_string('enternewname', 'repository'), 'newfilename', array('class' => 'accesshide'));
116 echo ' <input id="newfilename" name="newfilename" type="text" value="'.s($filename).'" />';
117 echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
118 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
119 echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
120 echo ' </form>';
121 echo $OUTPUT->footer();
122 break;
124 case 'rename':
126 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) {
127 print_error('fileexists');
128 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
129 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
130 $file->delete();
133 $home_url->param('action', 'browse');
134 $home_url->param('draftpath', $draftpath);
135 redirect($home_url);
136 break;
138 case 'downloaddir':
139 $zipper = new zip_packer();
141 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
142 if ($draftpath === '/') {
143 $filename = get_string('files').'.zip';
144 } else {
145 $filename = explode('/', trim($draftpath, '/'));
146 $filename = array_pop($filename) . '.zip';
149 $newdraftitemid = file_get_unused_draft_itemid();
150 if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
151 $fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
152 header('Location: ' . $fileurl);
153 } else {
154 print_error('cannotdownloaddir', 'repository');
156 break;
158 case 'zip':
159 $zipper = new zip_packer();
161 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
162 if (!$file->get_parent_directory()) {
163 $parent_path = '/';
164 $filepath = '/';
165 $filename = get_string('files').'.zip';
166 } else {
167 $parent_path = $file->get_parent_directory()->get_filepath();
168 $filepath = explode('/', trim($file->get_filepath(), '/'));
169 $filepath = array_pop($filepath);
170 $filename = $filepath.'.zip';
173 $filename = repository::get_unused_filename($itemid, $parent_path, $filename);
174 $newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
176 $home_url->param('action', 'browse');
177 $home_url->param('draftpath', $parent_path);
178 redirect($home_url, get_string('ziped', 'repository'));
179 break;
181 case 'unzip':
182 $zipper = new zip_packer();
183 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
185 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
186 $str = get_string('unzipped', 'repository');
187 } else {
188 $str = get_string('cannotunzip', 'error');
190 $home_url->param('action', 'browse');
191 $home_url->param('draftpath', $draftpath);
192 redirect($home_url, $str);
193 break;
195 case 'movefile':
196 if (!empty($targetpath)) {
197 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) {
198 print_error('cannotmovefile');
199 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
200 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file);
201 $file->delete();
202 } else {
203 var_dump('cannot find file');
204 die;
206 $home_url->param('action', 'browse');
207 $home_url->param('draftpath', $targetpath);
208 redirect($home_url);
210 echo $OUTPUT->header();
212 echo $OUTPUT->container_start();
213 echo html_writer::link($home_url, get_string('back', 'repository'));
214 echo $OUTPUT->container_end();
216 $data = new stdClass();
217 $home_url->param('action', 'movefile');
218 $home_url->param('draftpath', $draftpath);
219 $home_url->param('filename', $filename);
220 file_get_drafarea_folders($itemid, '/', $data);
221 print_draft_area_tree($data, true, $home_url);
222 echo $OUTPUT->footer();
223 break;
225 case 'mkdirform':
226 echo $OUTPUT->header();
228 echo $OUTPUT->container_start();
229 echo html_writer::link($home_url, get_string('back', 'repository'));
230 echo $OUTPUT->container_end();
232 $home_url->param('draftpath', $draftpath);
233 $home_url->param('action', 'mkdir');
234 echo ' <form method="post" action="'.$home_url->out().'">';
235 echo html_writer::label(get_string('entername', 'repository'), 'newdirname', array('class' => 'accesshide'));
236 echo ' <input name="newdirname" id="newdirname" type="text" />';
237 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
238 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
239 echo ' </form>';
240 echo $OUTPUT->footer();
241 break;
243 case 'mkdir':
245 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
246 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
247 $home_url->param('action', 'browse');
248 if (!empty($newdirname)) {
249 $home_url->param('draftpath', $newfolderpath);
250 $str = get_string('createfoldersuccess', 'repository');
251 } else {
252 $home_url->param('draftpath', $draftpath);
253 $str = get_string('createfolderfail', 'repository');
255 redirect($home_url, $str);
256 break;
258 case 'browse':
259 default:
260 $files = file_get_drafarea_files($itemid, $draftpath);
261 $info = file_get_draft_area_info($itemid);
262 $filecount = $info['filecount'];
264 echo $OUTPUT->header();
265 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
266 echo '<div class="fm-breadcrumb">';
267 $home_url->param('action', 'browse');
268 $home_url->param('draftpath', '/');
269 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
270 $trail = '';
271 if ($draftpath !== '/') {
272 $path = '/' . trim($draftpath, '/') . '/';
273 $parts = explode('/', $path);
274 foreach ($parts as $part) {
275 if ($part != '') {
276 $trail .= ('/'.$part.'/');
277 $data->path[] = array('name'=>$part, 'path'=>$trail);
278 $home_url->param('draftpath', $trail);
279 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
283 echo '</div>';
286 $filepicker_url->param('draftpath', $draftpath);
287 $filepicker_url->param('savepath', $draftpath);
288 $filepicker_url->param('action', 'plugins');
289 echo '<div class="filemanager-toolbar">';
290 if ($env == 'filepicker') {
291 $maxfiles = 1;
293 if ($filecount < $maxfiles || $maxfiles == -1) {
294 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
296 if ($env == 'filemanager') {
297 if (!empty($subdirs)) {
298 $home_url->param('action', 'mkdirform');
299 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
301 if (!empty($files->list)) {
302 $home_url->param('action', 'downloaddir');
303 echo ' ' . html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
306 echo '</div>';
308 if (!empty($files->list)) {
309 echo '<ul>';
310 foreach ($files->list as $file) {
311 if ($file->type != 'folder') {
312 $drafturl = $file->url;
313 // a file
314 echo '<li>';
315 echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
316 echo html_writer::link($drafturl, $file->filename);
318 $home_url->param('filename', $file->filename);
319 $home_url->param('draftpath', $file->filepath);
321 $home_url->param('action', 'deletedraft');
322 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
324 $home_url->param('action', 'movefile');
325 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
327 $home_url->param('action', 'renameform');
328 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
330 if (file_extension_in_typegroup($file->filename, 'archive', true)) {
331 $home_url->param('action', 'unzip');
332 $home_url->param('draftpath', $file->filepath);
333 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
336 echo '</li>';
337 } else {
338 // a folder
339 echo '<li>';
340 echo '<img src="'.$OUTPUT->pix_url(file_folder_icon()) . '" class="iconsmall" />';
342 $home_url->param('action', 'browse');
343 $home_url->param('draftpath', $file->filepath);
344 $filepathchunks = explode('/', trim($file->filepath, '/'));
345 $foldername = trim(array_pop($filepathchunks), '/');
346 echo html_writer::link($home_url, $foldername);
348 $home_url->param('draftpath', $file->filepath);
349 $home_url->param('filename', $file->filename);
350 $home_url->param('action', 'deletedraft');
351 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
353 $home_url->param('action', 'zip');
354 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
355 echo '</li>';
358 echo '</ul>';
359 } else {
360 echo get_string('nofilesavailable', 'repository');
362 echo $OUTPUT->footer();
363 break;
366 function print_draft_area_tree($tree, $root, $url) {
367 echo '<ul>';
368 if ($root) {
369 $url->param('targetpath', '/');
370 if ($url->param('draftpath') == '/') {
371 echo '<li>'.get_string('files').'</li>';
372 } else {
373 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
375 echo '<ul>';
376 if (isset($tree->children)) {
377 $tree = $tree->children;
381 if (!empty($tree)) {
382 foreach ($tree as $node) {
383 echo '<li>';
384 $url->param('targetpath', $node->filepath);
385 if ($url->param('draftpath') != $node->filepath) {
386 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
387 } else {
388 echo $node->fullname;
390 echo '</li>';
391 if (!empty($node->children)) {
392 print_draft_area_tree($node->children, false, $url);
396 if ($root) {
397 echo '</ul>';
399 echo '</ul>';