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