MDL-31901 Styled File Manager Icon view, Table view and Tree view
[moodle.git] / repository / draftfiles_ajax.php
CommitLineData
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
27define('AJAX_SCRIPT', true);
28
29require('../config.php');
30require_once($CFG->libdir.'/filelib.php');
31require_once($CFG->libdir.'/adminlib.php');
e709ddd2 32require_once($CFG->dirroot.'/repository/lib.php');
ea62bb3e 33$PAGE->set_context(get_system_context());
64f93798
PS
34require_login();
35if (isguestuser()) {
36 print_error('noguest');
37}
38require_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
46echo $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
52switch ($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)) {
c092681b
MG
146 if (array_key_exists('filename', $updatedata) || array_key_exists('filepath', $updatedata)) {
147 // check that target file name does not exist
148 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $newfilename)) {
149 die(json_encode((object)array('error' => get_string('fileexists', 'repository'))));
150 }
6dd299be
MG
151 $file->rename($newfilepath, $newfilename);
152 }
153 if (array_key_exists('license', $updatedata)) {
154 $file->set_license($updatedata['license']);
155 }
156 if (array_key_exists('author', $updatedata)) {
157 $file->set_license($updatedata['author']);
158 }
159 $changes = array_diff(array_keys($updatedata), array('filepath'));
160 if (!empty($changes)) {
161 // any change except for the moving to another folder alters 'Date modified' of the file
162 $file->set_timemodified(time());
c092681b
MG
163 }
164 }
165
166 die(json_encode((object)array('filepath' => $newfilepath)));
167
64f93798 168 case 'rename':
c092681b 169 // TODO deprecate this, use 'updatefile' instead
64f93798
PS
170 $filename = required_param('filename', PARAM_FILE);
171 $filepath = required_param('filepath', PARAM_PATH);
172 $newfilename = required_param('newfilename', PARAM_FILE);
173
174 $fs = get_file_storage();
175 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) {
176 //bad luck, we can not rename!
177 echo json_encode(false);
178 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
a27ab6fd 179 $return = new stdClass();
64f93798
PS
180 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
181 $file->delete();
182 $return->filepath = $newfile->get_filepath();
183 echo json_encode($return);
184 } else {
185 echo json_encode(false);
186 }
187 die;
188
c092681b
MG
189 case 'updatedir':
190 $filepath = required_param('filepath', PARAM_PATH);
191 $fs = get_file_storage();
192 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
193 die(json_encode((object)array('error' => get_string('foldernotfound', 'repository'))));
194 }
195 $parts = explode('/', trim($dir->get_filepath(), '/'));
196 $dirname = end($parts);
197 $newdirname = required_param('newdirname', PARAM_FILE);
198 $parent = required_param('newfilepath', PARAM_PATH);
199 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
200 //we must update directory and all children too
201 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
202 //bad luck, we can not rename if something already exists there
203 die(json_encode((object)array('error' => get_string('folderexists', 'repository'))));
204 }
205 $xfilepath = preg_quote($filepath, '|');
206 if ($newfilepath !== $filepath && preg_match("|^$xfilepath|", $parent)) {
207 // we can not move folder to it's own subfolder
208 die(json_encode((object)array('error' => get_string('folderrecurse', 'repository'))));
209 }
210
211 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
212 $moved = array();
213 foreach ($files as $file) {
214 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
215 continue;
216 }
217 // move one by one
218 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
219 $updatedata = array('filepath' => $path, 'timemodified' => $file->get_timemodified());
220 if ($dirname !== $newdirname && $file->get_filepath() === $filepath) {
221 // this is the main directory we move/rename AND it has actually been renamed
222 $updatedata['timemodified'] = time();
223 }
224 $fs->create_file_from_storedfile($updatedata, $file);
225 $moved[] = $file;
226 }
227 foreach ($moved as $file) {
228 // delete all old
229 $file->delete();
230 }
231
232 $return = new stdClass();
233 $return->filepath = $parent;
234 echo json_encode($return);
235 die;
236
64f93798 237 case 'renamedir':
54da2ec7 238 case 'movedir':
c092681b 239 // TODO deprecate this, use 'renamemovedir' instead
64f93798 240
54da2ec7
PS
241 $filepath = required_param('filepath', PARAM_PATH);
242 $fs = get_file_storage();
64f93798 243
54da2ec7
PS
244 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
245 echo json_encode(false);
246 die;
247 }
248 if ($action === 'renamedir') {
249 $newdirname = required_param('newdirname', PARAM_FILE);
250 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH);
251 $newfilepath = $parent . $newdirname . '/';
252 } else {
253 $newfilepath = required_param('newfilepath', PARAM_PATH);
254 $parts = explode('/', trim($dir->get_filepath(), '/'));
255 $dirname = end($parts);
256 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH);
257 }
64f93798 258
54da2ec7
PS
259 //we must update directory and all children too
260 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
261 //bad luck, we can not rename if something already exists there
262 echo json_encode(false);
263 die;
264 }
265
266 $xfilepath = preg_quote($filepath, '|');
267
268 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
269 $moved = array();
270 foreach ($files as $file) {
271 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
272 continue;
273 }
274 // move one by one
275 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
276 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
277 $moved[] = $file;
278 }
279 foreach ($moved as $file) {
280 // delete all old
281 $file->delete();
282 }
283
a27ab6fd 284 $return = new stdClass();
54da2ec7
PS
285 if ($action === 'renamedir') {
286 $return->filepath = $parent;
287 } else {
288 $return->filepath = $newfilepath;
289 }
290 echo json_encode($return);
64f93798
PS
291 die;
292
293 case 'movefile':
c092681b 294 // TODO deprecate this, use 'updatefile' instead
64f93798
PS
295 $filename = required_param('filename', PARAM_FILE);
296 $filepath = required_param('filepath', PARAM_PATH);
297 $newfilepath = required_param('newfilepath', PARAM_PATH);
298
299 $fs = get_file_storage();
300 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) {
301 //bad luck, we can not rename!
302 echo json_encode(false);
303 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
a27ab6fd 304 $return = new stdClass();
64f93798
PS
305 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
306 $file->delete();
307 $return->filepath = $newfile->get_filepath();
308 echo json_encode($return);
309 } else {
310 echo json_encode(false);
311 }
312 die;
313
314 case 'zip':
315 $filepath = required_param('filepath', PARAM_PATH);
316
317 $zipper = get_file_packer('application/zip');
318 $fs = get_file_storage();
319
320 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
321
322 $parent_path = $file->get_parent_directory()->get_filepath();
323
324 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
6bdfef5d 325 $return = new stdClass();
64f93798
PS
326 $return->filepath = $parent_path;
327 echo json_encode($return);
328 } else {
329 echo json_encode(false);
330 }
331 die;
332
333 case 'downloaddir':
334 $filepath = required_param('filepath', PARAM_PATH);
335
336 $zipper = get_file_packer('application/zip');
337 $fs = get_file_storage();
338 $area = file_get_draft_area_info($draftid);
339 if ($area['filecount'] == 0) {
340 echo json_encode(false);
341 die;
342 }
343
344 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
345 if ($filepath === '/') {
346 $parent_path = '/';
347 $filename = get_string('files').'.zip';
348 } else {
349 $parent_path = $stored_file->get_parent_directory()->get_filepath();
350 $filename = trim($filepath, '/').'.zip';
351 }
352
353 // archive compressed file to an unused draft area
354 $newdraftitemid = file_get_unused_draft_itemid();
355 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
6bdfef5d 356 $return = new stdClass();
50a8bd6c 357 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
64f93798
PS
358 $return->filepath = $parent_path;
359 echo json_encode($return);
360 } else {
361 echo json_encode(false);
362 }
363 die;
364
365 case 'unzip':
366 $filename = required_param('filename', PARAM_FILE);
367 $filepath = required_param('filepath', PARAM_PATH);
368
369 $zipper = get_file_packer('application/zip');
370
371 $fs = get_file_storage();
372
373 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
374
375 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
6bdfef5d 376 $return = new stdClass();
64f93798
PS
377 $return->filepath = $filepath;
378 echo json_encode($return);
379 } else {
380 echo json_encode(false);
381 }
382 die;
383
9a62f779
MG
384 case 'getoriginal':
385 $filename = required_param('filename', PARAM_FILE);
386 $filepath = required_param('filepath', PARAM_PATH);
387
388 $fs = get_file_storage();
389 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
390 if (!$file) {
391 echo json_encode(false);
392 } else {
393 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
394 echo json_encode((object)$return);
395 }
396 die;
397
64f93798
PS
398 default:
399 // no/unknown action?
400 echo json_encode(false);
401 die;
402}