MDL-39177 make sure we don't remove existing directories when cleaning up unzip
[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 43
21c08c63 44$user_context = context_user::instance($USER->id);
64f93798 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);
c092681b 128 $updatedata = array();
f7c588d2
MG
129 $updatedata['filename'] = optional_param('newfilename', $filename, PARAM_FILE);
130 $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $filepath, PARAM_PATH);
131 if (($v = optional_param('newlicense', false, PARAM_TEXT)) !== false) {
132 $updatedata['license'] = $v;
c092681b 133 }
f7c588d2
MG
134 if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
135 $updatedata['author'] = $v;
136 }
137 try {
138 repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
139 } catch (moodle_exception $e) {
140 die(json_encode((object)array('error' => $e->getMessage())));
c092681b 141 }
c092681b
MG
142 die(json_encode((object)array('filepath' => $newfilepath)));
143
c092681b
MG
144 case 'updatedir':
145 $filepath = required_param('filepath', PARAM_PATH);
c092681b
MG
146 $newdirname = required_param('newdirname', PARAM_FILE);
147 $parent = required_param('newfilepath', PARAM_PATH);
148 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
f7c588d2
MG
149 try {
150 repository::update_draftfile($draftid, $filepath, '.', array('filepath' => $newfilepath));
151 } catch (moodle_exception $e) {
152 die(json_encode((object)array('error' => $e->getMessage())));
c092681b 153 }
f7c588d2 154 die(json_encode((object)array('filepath' => $parent)));
c092681b 155
64f93798
PS
156 case 'zip':
157 $filepath = required_param('filepath', PARAM_PATH);
158
159 $zipper = get_file_packer('application/zip');
160 $fs = get_file_storage();
161
162 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
163
164 $parent_path = $file->get_parent_directory()->get_filepath();
638d72cd 165
697ade28
VD
166 $filepath = explode('/', trim($file->get_filepath(), '/'));
167 $filepath = array_pop($filepath);
c24cd394 168 $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
64f93798 169
c24cd394 170 if ($newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
6bdfef5d 171 $return = new stdClass();
64f93798
PS
172 $return->filepath = $parent_path;
173 echo json_encode($return);
174 } else {
175 echo json_encode(false);
176 }
177 die;
178
179 case 'downloaddir':
180 $filepath = required_param('filepath', PARAM_PATH);
181
182 $zipper = get_file_packer('application/zip');
183 $fs = get_file_storage();
638d72cd
FM
184 $area = file_get_draft_area_info($draftid, $filepath);
185 if ($area['filecount'] == 0 && $area['foldercount'] == 0) {
64f93798
PS
186 echo json_encode(false);
187 die;
188 }
189
190 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
191 if ($filepath === '/') {
64f93798
PS
192 $filename = get_string('files').'.zip';
193 } else {
638d72cd
FM
194 $filename = explode('/', trim($filepath, '/'));
195 $filename = array_pop($filename) . '.zip';
64f93798
PS
196 }
197
198 // archive compressed file to an unused draft area
199 $newdraftitemid = file_get_unused_draft_itemid();
697ade28 200 if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
6bdfef5d 201 $return = new stdClass();
50a8bd6c 202 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
638d72cd 203 $return->filepath = $filepath;
64f93798
PS
204 echo json_encode($return);
205 } else {
206 echo json_encode(false);
207 }
208 die;
209
210 case 'unzip':
211 $filename = required_param('filename', PARAM_FILE);
212 $filepath = required_param('filepath', PARAM_PATH);
213
214 $zipper = get_file_packer('application/zip');
215
216 $fs = get_file_storage();
217
218 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
219
77c21e6d 220 // Find unused name for directory to extract the archive.
da4b1ee4 221 $temppath = $fs->get_unused_dirname($user_context->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
bfa8ff63
MG
222 $donotremovedirs = array();
223 $doremovedirs = array($temppath);
77c21e6d
MG
224 // Extract archive and move all files from $temppath to $filepath
225 if ($file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $temppath, $USER->id) !== false) {
226 $extractedfiles = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $temppath, true);
227 $xtemppath = preg_quote($temppath, '|');
228 foreach ($extractedfiles as $file) {
229 $realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
230 if (!$file->is_directory()) {
231 // Set the source to the extracted file to indicate that it came from archive.
232 $file->set_source(serialize((object)array('source' => $filepath)));
233 }
234 if (!$fs->file_exists($user_context->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
235 // File or directory did not exist, just move it.
236 $file->rename($realpath, $file->get_filename());
237 } else if (!$file->is_directory()) {
bfa8ff63 238 // File already existed, overwrite it
77c21e6d 239 repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
bfa8ff63
MG
240 } else {
241 // Directory already existed, remove temporary dir but make sure we don't remove the existing dir
242 $doremovedirs[] = $file->get_filepath();
243 $donotremovedirs[] = $realpath;
77c21e6d
MG
244 }
245 }
6bdfef5d 246 $return = new stdClass();
64f93798 247 $return->filepath = $filepath;
64f93798 248 } else {
77c21e6d 249 $return = false;
64f93798 250 }
77c21e6d 251 // Remove remaining temporary directories.
bfa8ff63
MG
252 foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
253 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
254 $file->delete();
255 }
77c21e6d
MG
256 }
257 die(json_encode($return));
64f93798 258
9a62f779
MG
259 case 'getoriginal':
260 $filename = required_param('filename', PARAM_FILE);
261 $filepath = required_param('filepath', PARAM_PATH);
262
263 $fs = get_file_storage();
264 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
265 if (!$file) {
266 echo json_encode(false);
267 } else {
268 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
269 echo json_encode((object)$return);
270 }
271 die;
272
9dbdf31f
MG
273 case 'getreferences':
274 $filename = required_param('filename', PARAM_FILE);
275 $filepath = required_param('filepath', PARAM_PATH);
276
277 $fs = get_file_storage();
278 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
279 if (!$file) {
280 echo json_encode(false);
281 } else {
282 $source = unserialize($file->get_source());
283 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
284 $browser = get_file_browser();
285 if (isset($source->original)) {
286 $reffiles = $fs->search_references($source->original);
287 foreach ($reffiles as $reffile) {
d197ea43 288 $refcontext = context::instance_by_id($reffile->get_contextid());
9dbdf31f
MG
289 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
290 if (empty($fileinfo)) {
291 $return['references'][] = get_string('undisclosedreference', 'repository');
292 } else {
293 $return['references'][] = $fileinfo->get_readable_fullname();
294 }
295 }
296 }
297 echo json_encode((object)$return);
298 }
299 die;
300
64f93798
PS
301 default:
302 // no/unknown action?
303 echo json_encode(false);
304 die;
305}