MDL-33098 filemanager now stores the new author field correctly
[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();
222
223 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
6bdfef5d 224 $return = new stdClass();
64f93798
PS
225 $return->filepath = $parent_path;
226 echo json_encode($return);
227 } else {
228 echo json_encode(false);
229 }
230 die;
231
232 case 'downloaddir':
233 $filepath = required_param('filepath', PARAM_PATH);
234
235 $zipper = get_file_packer('application/zip');
236 $fs = get_file_storage();
237 $area = file_get_draft_area_info($draftid);
238 if ($area['filecount'] == 0) {
239 echo json_encode(false);
240 die;
241 }
242
243 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
244 if ($filepath === '/') {
245 $parent_path = '/';
246 $filename = get_string('files').'.zip';
247 } else {
248 $parent_path = $stored_file->get_parent_directory()->get_filepath();
249 $filename = trim($filepath, '/').'.zip';
250 }
251
252 // archive compressed file to an unused draft area
253 $newdraftitemid = file_get_unused_draft_itemid();
254 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
6bdfef5d 255 $return = new stdClass();
50a8bd6c 256 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
64f93798
PS
257 $return->filepath = $parent_path;
258 echo json_encode($return);
259 } else {
260 echo json_encode(false);
261 }
262 die;
263
264 case 'unzip':
265 $filename = required_param('filename', PARAM_FILE);
266 $filepath = required_param('filepath', PARAM_PATH);
267
268 $zipper = get_file_packer('application/zip');
269
270 $fs = get_file_storage();
271
272 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
273
274 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
6bdfef5d 275 $return = new stdClass();
64f93798
PS
276 $return->filepath = $filepath;
277 echo json_encode($return);
278 } else {
279 echo json_encode(false);
280 }
281 die;
282
9a62f779
MG
283 case 'getoriginal':
284 $filename = required_param('filename', PARAM_FILE);
285 $filepath = required_param('filepath', PARAM_PATH);
286
287 $fs = get_file_storage();
288 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
289 if (!$file) {
290 echo json_encode(false);
291 } else {
292 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
293 echo json_encode((object)$return);
294 }
295 die;
296
9dbdf31f
MG
297 case 'getreferences':
298 $filename = required_param('filename', PARAM_FILE);
299 $filepath = required_param('filepath', PARAM_PATH);
300
301 $fs = get_file_storage();
302 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
303 if (!$file) {
304 echo json_encode(false);
305 } else {
306 $source = unserialize($file->get_source());
307 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
308 $browser = get_file_browser();
309 if (isset($source->original)) {
310 $reffiles = $fs->search_references($source->original);
311 foreach ($reffiles as $reffile) {
312 $refcontext = get_context_instance_by_id($reffile->get_contextid());
313 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
314 if (empty($fileinfo)) {
315 $return['references'][] = get_string('undisclosedreference', 'repository');
316 } else {
317 $return['references'][] = $fileinfo->get_readable_fullname();
318 }
319 }
320 }
321 echo json_encode((object)$return);
322 }
323 die;
324
64f93798
PS
325 default:
326 // no/unknown action?
327 echo json_encode(false);
328 die;
329}