Commit | Line | Data |
---|---|---|
64f93798 PS |
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 | * Draft file ajax file manager | |
20 | * | |
21 | * @package core | |
22 | * @subpackage repository | |
23 | * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com> | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
27 | define('AJAX_SCRIPT', true); | |
28 | ||
29 | require('../config.php'); | |
30 | require_once($CFG->libdir.'/filelib.php'); | |
31 | require_once($CFG->libdir.'/adminlib.php'); | |
e709ddd2 | 32 | require_once($CFG->dirroot.'/repository/lib.php'); |
ea62bb3e | 33 | $PAGE->set_context(get_system_context()); |
64f93798 PS |
34 | require_login(); |
35 | if (isguestuser()) { | |
36 | print_error('noguest'); | |
37 | } | |
38 | require_sesskey(); | |
39 | ||
40 | $action = required_param('action', PARAM_ALPHA); | |
41 | $draftid = required_param('itemid', PARAM_INT); | |
eacae660 | 42 | $filepath = optional_param('filepath', '/', PARAM_PATH); |
64f93798 PS |
43 | |
44 | $user_context = get_context_instance(CONTEXT_USER, $USER->id); | |
45 | ||
bce08d9a PS |
46 | echo $OUTPUT->header(); // send headers |
47 | ||
64f93798 PS |
48 | // |
49 | //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!! | |
50 | // | |
51 | ||
52 | switch ($action) { | |
53 | case 'dir': | |
6bdfef5d | 54 | $data = new stdClass(); |
64f93798 PS |
55 | file_get_drafarea_folders($draftid, $filepath, $data); |
56 | echo json_encode($data); | |
57 | die; | |
58 | ||
59 | case 'list': | |
60 | $filepath = optional_param('filepath', '/', PARAM_PATH); | |
61 | ||
e709ddd2 | 62 | $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath)); |
910e1ecd DS |
63 | $info = file_get_draft_area_info($draftid); |
64 | $data->filecount = $info['filecount']; | |
65 | $data->filesize = $info['filesize']; | |
e709ddd2 MG |
66 | $data->tree = new stdClass(); |
67 | file_get_drafarea_folders($draftid, '/', $data->tree); | |
64f93798 PS |
68 | echo json_encode($data); |
69 | die; | |
70 | ||
71 | case 'mkdir': | |
72 | $filepath = required_param('filepath', PARAM_PATH); | |
73 | $newdirname = required_param('newdirname', PARAM_FILE); | |
74 | ||
75 | $fs = get_file_storage(); | |
76 | $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname)); | |
6bdfef5d | 77 | $return = new stdClass(); |
64f93798 PS |
78 | $return->filepath = $filepath; |
79 | echo json_encode($return); | |
80 | die; | |
81 | ||
82 | case 'delete': | |
83 | $filename = required_param('filename', PARAM_FILE); | |
84 | $filepath = required_param('filepath', PARAM_PATH); | |
85 | ||
86 | $fs = get_file_storage(); | |
87 | $filepath = file_correct_filepath($filepath); | |
6bdfef5d | 88 | $return = new stdClass(); |
64f93798 PS |
89 | if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { |
90 | $parent_path = $stored_file->get_parent_directory()->get_filepath(); | |
5ad337c8 DC |
91 | if ($stored_file->is_directory()) { |
92 | $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $filepath, true); | |
93 | foreach ($files as $file) { | |
94 | $file->delete(); | |
95 | } | |
96 | $stored_file->delete(); | |
64f93798 PS |
97 | $return->filepath = $parent_path; |
98 | echo json_encode($return); | |
99 | } else { | |
5ad337c8 DC |
100 | if($result = $stored_file->delete()) { |
101 | $return->filepath = $parent_path; | |
102 | echo json_encode($return); | |
103 | } else { | |
104 | echo json_encode(false); | |
105 | } | |
64f93798 PS |
106 | } |
107 | } else { | |
108 | echo json_encode(false); | |
109 | } | |
110 | die; | |
111 | ||
112 | case 'setmainfile': | |
113 | $filename = required_param('filename', PARAM_FILE); | |
114 | $filepath = required_param('filepath', PARAM_PATH); | |
115 | ||
116 | $filepath = file_correct_filepath($filepath); | |
117 | // reset sort order | |
118 | file_reset_sortorder($user_context->id, 'user', 'draft', $draftid); | |
119 | // set main file | |
120 | $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1); | |
121 | echo json_encode($return); | |
122 | die; | |
123 | ||
c092681b MG |
124 | case 'updatefile': |
125 | // Allows to Rename file, move it to another directory, change it's license and author information in one request | |
126 | $filename = required_param('filename', PARAM_FILE); | |
127 | $filepath = required_param('filepath', PARAM_PATH); | |
128 | ||
129 | $fs = get_file_storage(); | |
130 | if (!($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename))) { | |
131 | die(json_encode((object)array('error' => get_string('filenotfound', 'error')))); | |
132 | } | |
133 | ||
134 | $updatedata = array(); | |
135 | $updatedata['filename'] = $newfilename = optional_param('newfilename', $file->get_filename(), PARAM_FILE); | |
136 | $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $file->get_filepath(), PARAM_PATH); | |
137 | $updatedata['license'] = optional_param('newlicense', $file->get_license(), PARAM_TEXT); | |
138 | $updatedata['author'] = optional_param('newauthor', $file->get_author(), PARAM_TEXT); | |
139 | foreach ($updatedata as $key => $value) { | |
140 | if (''.$value === ''.$file->{'get_'.$key}()) { | |
141 | unset($updatedata[$key]); | |
142 | } | |
143 | } | |
144 | ||
145 | if (!empty($updatedata)) { | |
146 | $updatedata['timemodified'] = $file->get_timemodified(); | |
147 | $changes = array_diff(array_keys($updatedata), array('filepath')); | |
148 | if (!empty($changes)) { | |
149 | // any change except for the moving to another folder alters 'Date modified' of the file | |
150 | $updatedata['timemodified'] = time(); | |
151 | } | |
152 | if (array_key_exists('filename', $updatedata) || array_key_exists('filepath', $updatedata)) { | |
153 | // check that target file name does not exist | |
154 | if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $newfilename)) { | |
155 | die(json_encode((object)array('error' => get_string('fileexists', 'repository')))); | |
156 | } | |
157 | try { | |
158 | $newfile = $fs->create_file_from_storedfile($updatedata, $file); | |
159 | } catch (Exception $e) { | |
160 | die(json_encode((object)array('error' => $e->getMessage()))); | |
161 | } | |
162 | $file->delete(); | |
163 | } else { | |
164 | $file->update((object)$updatedata); | |
165 | } | |
166 | } | |
167 | ||
168 | die(json_encode((object)array('filepath' => $newfilepath))); | |
169 | ||
64f93798 | 170 | case 'rename': |
c092681b | 171 | // TODO deprecate this, use 'updatefile' instead |
64f93798 PS |
172 | $filename = required_param('filename', PARAM_FILE); |
173 | $filepath = required_param('filepath', PARAM_PATH); | |
174 | $newfilename = required_param('newfilename', PARAM_FILE); | |
175 | ||
176 | $fs = get_file_storage(); | |
177 | if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) { | |
178 | //bad luck, we can not rename! | |
179 | echo json_encode(false); | |
180 | } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { | |
a27ab6fd | 181 | $return = new stdClass(); |
64f93798 PS |
182 | $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file); |
183 | $file->delete(); | |
184 | $return->filepath = $newfile->get_filepath(); | |
185 | echo json_encode($return); | |
186 | } else { | |
187 | echo json_encode(false); | |
188 | } | |
189 | die; | |
190 | ||
c092681b MG |
191 | case 'updatedir': |
192 | $filepath = required_param('filepath', PARAM_PATH); | |
193 | $fs = get_file_storage(); | |
194 | if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) { | |
195 | die(json_encode((object)array('error' => get_string('foldernotfound', 'repository')))); | |
196 | } | |
197 | $parts = explode('/', trim($dir->get_filepath(), '/')); | |
198 | $dirname = end($parts); | |
199 | $newdirname = required_param('newdirname', PARAM_FILE); | |
200 | $parent = required_param('newfilepath', PARAM_PATH); | |
201 | $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH); | |
202 | //we must update directory and all children too | |
203 | if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) { | |
204 | //bad luck, we can not rename if something already exists there | |
205 | die(json_encode((object)array('error' => get_string('folderexists', 'repository')))); | |
206 | } | |
207 | $xfilepath = preg_quote($filepath, '|'); | |
208 | if ($newfilepath !== $filepath && preg_match("|^$xfilepath|", $parent)) { | |
209 | // we can not move folder to it's own subfolder | |
210 | die(json_encode((object)array('error' => get_string('folderrecurse', 'repository')))); | |
211 | } | |
212 | ||
213 | $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid); | |
214 | $moved = array(); | |
215 | foreach ($files as $file) { | |
216 | if (!preg_match("|^$xfilepath|", $file->get_filepath())) { | |
217 | continue; | |
218 | } | |
219 | // move one by one | |
220 | $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath()); | |
221 | $updatedata = array('filepath' => $path, 'timemodified' => $file->get_timemodified()); | |
222 | if ($dirname !== $newdirname && $file->get_filepath() === $filepath) { | |
223 | // this is the main directory we move/rename AND it has actually been renamed | |
224 | $updatedata['timemodified'] = time(); | |
225 | } | |
226 | $fs->create_file_from_storedfile($updatedata, $file); | |
227 | $moved[] = $file; | |
228 | } | |
229 | foreach ($moved as $file) { | |
230 | // delete all old | |
231 | $file->delete(); | |
232 | } | |
233 | ||
234 | $return = new stdClass(); | |
235 | $return->filepath = $parent; | |
236 | echo json_encode($return); | |
237 | die; | |
238 | ||
64f93798 | 239 | case 'renamedir': |
54da2ec7 | 240 | case 'movedir': |
c092681b | 241 | // TODO deprecate this, use 'renamemovedir' instead |
64f93798 | 242 | |
54da2ec7 PS |
243 | $filepath = required_param('filepath', PARAM_PATH); |
244 | $fs = get_file_storage(); | |
64f93798 | 245 | |
54da2ec7 PS |
246 | if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) { |
247 | echo json_encode(false); | |
248 | die; | |
249 | } | |
250 | if ($action === 'renamedir') { | |
251 | $newdirname = required_param('newdirname', PARAM_FILE); | |
252 | $parent = clean_param(dirname($filepath) . '/', PARAM_PATH); | |
253 | $newfilepath = $parent . $newdirname . '/'; | |
254 | } else { | |
255 | $newfilepath = required_param('newfilepath', PARAM_PATH); | |
256 | $parts = explode('/', trim($dir->get_filepath(), '/')); | |
257 | $dirname = end($parts); | |
258 | $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH); | |
259 | } | |
64f93798 | 260 | |
54da2ec7 PS |
261 | //we must update directory and all children too |
262 | if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) { | |
263 | //bad luck, we can not rename if something already exists there | |
264 | echo json_encode(false); | |
265 | die; | |
266 | } | |
267 | ||
268 | $xfilepath = preg_quote($filepath, '|'); | |
269 | ||
270 | $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid); | |
271 | $moved = array(); | |
272 | foreach ($files as $file) { | |
273 | if (!preg_match("|^$xfilepath|", $file->get_filepath())) { | |
274 | continue; | |
275 | } | |
276 | // move one by one | |
277 | $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath()); | |
278 | $fs->create_file_from_storedfile(array('filepath'=>$path), $file); | |
279 | $moved[] = $file; | |
280 | } | |
281 | foreach ($moved as $file) { | |
282 | // delete all old | |
283 | $file->delete(); | |
284 | } | |
285 | ||
a27ab6fd | 286 | $return = new stdClass(); |
54da2ec7 PS |
287 | if ($action === 'renamedir') { |
288 | $return->filepath = $parent; | |
289 | } else { | |
290 | $return->filepath = $newfilepath; | |
291 | } | |
292 | echo json_encode($return); | |
64f93798 PS |
293 | die; |
294 | ||
295 | case 'movefile': | |
c092681b | 296 | // TODO deprecate this, use 'updatefile' instead |
64f93798 PS |
297 | $filename = required_param('filename', PARAM_FILE); |
298 | $filepath = required_param('filepath', PARAM_PATH); | |
299 | $newfilepath = required_param('newfilepath', PARAM_PATH); | |
300 | ||
301 | $fs = get_file_storage(); | |
302 | if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) { | |
303 | //bad luck, we can not rename! | |
304 | echo json_encode(false); | |
305 | } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { | |
a27ab6fd | 306 | $return = new stdClass(); |
64f93798 PS |
307 | $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file); |
308 | $file->delete(); | |
309 | $return->filepath = $newfile->get_filepath(); | |
310 | echo json_encode($return); | |
311 | } else { | |
312 | echo json_encode(false); | |
313 | } | |
314 | die; | |
315 | ||
316 | case 'zip': | |
317 | $filepath = required_param('filepath', PARAM_PATH); | |
318 | ||
319 | $zipper = get_file_packer('application/zip'); | |
320 | $fs = get_file_storage(); | |
321 | ||
322 | $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); | |
323 | ||
324 | $parent_path = $file->get_parent_directory()->get_filepath(); | |
325 | ||
326 | if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) { | |
6bdfef5d | 327 | $return = new stdClass(); |
64f93798 PS |
328 | $return->filepath = $parent_path; |
329 | echo json_encode($return); | |
330 | } else { | |
331 | echo json_encode(false); | |
332 | } | |
333 | die; | |
334 | ||
335 | case 'downloaddir': | |
336 | $filepath = required_param('filepath', PARAM_PATH); | |
337 | ||
338 | $zipper = get_file_packer('application/zip'); | |
339 | $fs = get_file_storage(); | |
340 | $area = file_get_draft_area_info($draftid); | |
341 | if ($area['filecount'] == 0) { | |
342 | echo json_encode(false); | |
343 | die; | |
344 | } | |
345 | ||
346 | $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); | |
347 | if ($filepath === '/') { | |
348 | $parent_path = '/'; | |
349 | $filename = get_string('files').'.zip'; | |
350 | } else { | |
351 | $parent_path = $stored_file->get_parent_directory()->get_filepath(); | |
352 | $filename = trim($filepath, '/').'.zip'; | |
353 | } | |
354 | ||
355 | // archive compressed file to an unused draft area | |
356 | $newdraftitemid = file_get_unused_draft_itemid(); | |
357 | if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { | |
6bdfef5d | 358 | $return = new stdClass(); |
50a8bd6c | 359 | $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out(); |
64f93798 PS |
360 | $return->filepath = $parent_path; |
361 | echo json_encode($return); | |
362 | } else { | |
363 | echo json_encode(false); | |
364 | } | |
365 | die; | |
366 | ||
367 | case 'unzip': | |
368 | $filename = required_param('filename', PARAM_FILE); | |
369 | $filepath = required_param('filepath', PARAM_PATH); | |
370 | ||
371 | $zipper = get_file_packer('application/zip'); | |
372 | ||
373 | $fs = get_file_storage(); | |
374 | ||
375 | $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename); | |
376 | ||
377 | if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) { | |
6bdfef5d | 378 | $return = new stdClass(); |
64f93798 PS |
379 | $return->filepath = $filepath; |
380 | echo json_encode($return); | |
381 | } else { | |
382 | echo json_encode(false); | |
383 | } | |
384 | die; | |
385 | ||
386 | default: | |
387 | // no/unknown action? | |
388 | echo json_encode(false); | |
389 | die; | |
390 | } |