MDL-32639 files: Zipping a folder does not overwrite existing files
[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)) {
b53fad1e 157 $file->set_author($updatedata['author']);
6dd299be
MG
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
c092681b
MG
168 case 'updatedir':
169 $filepath = required_param('filepath', PARAM_PATH);
170 $fs = get_file_storage();
171 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
172 die(json_encode((object)array('error' => get_string('foldernotfound', 'repository'))));
173 }
174 $parts = explode('/', trim($dir->get_filepath(), '/'));
175 $dirname = end($parts);
176 $newdirname = required_param('newdirname', PARAM_FILE);
177 $parent = required_param('newfilepath', PARAM_PATH);
178 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
55e86805
MG
179 if ($newfilepath == $filepath) {
180 // no action required
181 die(json_encode((object)array('filepath' => $parent)));
182 }
c092681b
MG
183 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
184 //bad luck, we can not rename if something already exists there
185 die(json_encode((object)array('error' => get_string('folderexists', 'repository'))));
186 }
187 $xfilepath = preg_quote($filepath, '|');
55e86805 188 if (preg_match("|^$xfilepath|", $parent)) {
c092681b
MG
189 // we can not move folder to it's own subfolder
190 die(json_encode((object)array('error' => get_string('folderrecurse', 'repository'))));
191 }
192
55e86805 193 //we must update directory and all children too
c092681b 194 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
c092681b
MG
195 foreach ($files as $file) {
196 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
197 continue;
198 }
199 // move one by one
200 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
55e86805 201 if ($dirname !== $newdirname && $file->get_filepath() === $filepath && $file->get_filename() === '.') {
c092681b 202 // this is the main directory we move/rename AND it has actually been renamed
55e86805 203 $file->set_timemodified(time());
c092681b 204 }
55e86805 205 $file->rename($path, $file->get_filename());
c092681b
MG
206 }
207
208 $return = new stdClass();
209 $return->filepath = $parent;
210 echo json_encode($return);
211 die;
212
64f93798
PS
213 case 'zip':
214 $filepath = required_param('filepath', PARAM_PATH);
215
216 $zipper = get_file_packer('application/zip');
217 $fs = get_file_storage();
218
219 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
220
221 $parent_path = $file->get_parent_directory()->get_filepath();
aa4d9734 222
4dcae0d8
VD
223 $filepath = explode('/', trim($file->get_filepath(), '/'));
224 $filepath = array_pop($filepath);
03f8841d 225 $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
64f93798 226
03f8841d 227 if ($newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
6bdfef5d 228 $return = new stdClass();
64f93798
PS
229 $return->filepath = $parent_path;
230 echo json_encode($return);
231 } else {
232 echo json_encode(false);
233 }
234 die;
235
236 case 'downloaddir':
237 $filepath = required_param('filepath', PARAM_PATH);
238
239 $zipper = get_file_packer('application/zip');
240 $fs = get_file_storage();
241 $area = file_get_draft_area_info($draftid);
242 if ($area['filecount'] == 0) {
243 echo json_encode(false);
244 die;
245 }
246
247 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
248 if ($filepath === '/') {
64f93798
PS
249 $filename = get_string('files').'.zip';
250 } else {
aa4d9734
FM
251 $filename = explode('/', trim($filepath, '/'));
252 $filename = array_pop($filename) . '.zip';
64f93798
PS
253 }
254
255 // archive compressed file to an unused draft area
256 $newdraftitemid = file_get_unused_draft_itemid();
4dcae0d8 257 if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
6bdfef5d 258 $return = new stdClass();
50a8bd6c 259 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
aa4d9734 260 $return->filepath = $filepath;
64f93798
PS
261 echo json_encode($return);
262 } else {
263 echo json_encode(false);
264 }
265 die;
266
267 case 'unzip':
268 $filename = required_param('filename', PARAM_FILE);
269 $filepath = required_param('filepath', PARAM_PATH);
270
271 $zipper = get_file_packer('application/zip');
272
273 $fs = get_file_storage();
274
275 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
276
277 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
6bdfef5d 278 $return = new stdClass();
64f93798
PS
279 $return->filepath = $filepath;
280 echo json_encode($return);
281 } else {
282 echo json_encode(false);
283 }
284 die;
285
9a62f779
MG
286 case 'getoriginal':
287 $filename = required_param('filename', PARAM_FILE);
288 $filepath = required_param('filepath', PARAM_PATH);
289
290 $fs = get_file_storage();
291 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
292 if (!$file) {
293 echo json_encode(false);
294 } else {
295 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
296 echo json_encode((object)$return);
297 }
298 die;
299
9dbdf31f
MG
300 case 'getreferences':
301 $filename = required_param('filename', PARAM_FILE);
302 $filepath = required_param('filepath', PARAM_PATH);
303
304 $fs = get_file_storage();
305 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
306 if (!$file) {
307 echo json_encode(false);
308 } else {
309 $source = unserialize($file->get_source());
310 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
311 $browser = get_file_browser();
312 if (isset($source->original)) {
313 $reffiles = $fs->search_references($source->original);
314 foreach ($reffiles as $reffile) {
315 $refcontext = get_context_instance_by_id($reffile->get_contextid());
316 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
317 if (empty($fileinfo)) {
318 $return['references'][] = get_string('undisclosedreference', 'repository');
319 } else {
320 $return['references'][] = $fileinfo->get_readable_fullname();
321 }
322 }
323 }
324 echo json_encode((object)$return);
325 }
326 die;
327
64f93798
PS
328 default:
329 // no/unknown action?
330 echo json_encode(false);
331 die;
332}