MDL-31901: FileManager uses more YUI3 windows and more renderers
[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 78 $return->filepath = $filepath;
e709ddd2
MG
79 $return->tree = new stdClass();
80 file_get_drafarea_folders($draftid, '/', $return->tree);
64f93798
PS
81 echo json_encode($return);
82 die;
83
84 case 'delete':
85 $filename = required_param('filename', PARAM_FILE);
86 $filepath = required_param('filepath', PARAM_PATH);
87
88 $fs = get_file_storage();
89 $filepath = file_correct_filepath($filepath);
6bdfef5d 90 $return = new stdClass();
64f93798
PS
91 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
92 $parent_path = $stored_file->get_parent_directory()->get_filepath();
5ad337c8
DC
93 if ($stored_file->is_directory()) {
94 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $filepath, true);
95 foreach ($files as $file) {
96 $file->delete();
97 }
98 $stored_file->delete();
64f93798
PS
99 $return->filepath = $parent_path;
100 echo json_encode($return);
101 } else {
5ad337c8
DC
102 if($result = $stored_file->delete()) {
103 $return->filepath = $parent_path;
104 echo json_encode($return);
105 } else {
106 echo json_encode(false);
107 }
64f93798
PS
108 }
109 } else {
110 echo json_encode(false);
111 }
112 die;
113
114 case 'setmainfile':
115 $filename = required_param('filename', PARAM_FILE);
116 $filepath = required_param('filepath', PARAM_PATH);
117
118 $filepath = file_correct_filepath($filepath);
119 // reset sort order
120 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
121 // set main file
122 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
123 echo json_encode($return);
124 die;
125
126 case 'rename':
127 $filename = required_param('filename', PARAM_FILE);
128 $filepath = required_param('filepath', PARAM_PATH);
129 $newfilename = required_param('newfilename', PARAM_FILE);
130
131 $fs = get_file_storage();
132 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) {
133 //bad luck, we can not rename!
134 echo json_encode(false);
135 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
a27ab6fd 136 $return = new stdClass();
64f93798
PS
137 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
138 $file->delete();
139 $return->filepath = $newfile->get_filepath();
140 echo json_encode($return);
141 } else {
142 echo json_encode(false);
143 }
144 die;
145
146 case 'renamedir':
54da2ec7 147 case 'movedir':
64f93798 148
54da2ec7
PS
149 $filepath = required_param('filepath', PARAM_PATH);
150 $fs = get_file_storage();
64f93798 151
54da2ec7
PS
152 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
153 echo json_encode(false);
154 die;
155 }
156 if ($action === 'renamedir') {
157 $newdirname = required_param('newdirname', PARAM_FILE);
158 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH);
159 $newfilepath = $parent . $newdirname . '/';
160 } else {
161 $newfilepath = required_param('newfilepath', PARAM_PATH);
162 $parts = explode('/', trim($dir->get_filepath(), '/'));
163 $dirname = end($parts);
164 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH);
165 }
64f93798 166
54da2ec7
PS
167 //we must update directory and all children too
168 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
169 //bad luck, we can not rename if something already exists there
170 echo json_encode(false);
171 die;
172 }
173
174 $xfilepath = preg_quote($filepath, '|');
175
176 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
177 $moved = array();
178 foreach ($files as $file) {
179 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
180 continue;
181 }
182 // move one by one
183 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
184 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
185 $moved[] = $file;
186 }
187 foreach ($moved as $file) {
188 // delete all old
189 $file->delete();
190 }
191
a27ab6fd 192 $return = new stdClass();
54da2ec7
PS
193 if ($action === 'renamedir') {
194 $return->filepath = $parent;
195 } else {
196 $return->filepath = $newfilepath;
197 }
e709ddd2
MG
198 $return->tree = new stdClass();
199 file_get_drafarea_folders($draftid, '/', $return->tree);
54da2ec7 200 echo json_encode($return);
64f93798
PS
201 die;
202
203 case 'movefile':
204 $filename = required_param('filename', PARAM_FILE);
205 $filepath = required_param('filepath', PARAM_PATH);
206 $newfilepath = required_param('newfilepath', PARAM_PATH);
207
208 $fs = get_file_storage();
209 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) {
210 //bad luck, we can not rename!
211 echo json_encode(false);
212 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
a27ab6fd 213 $return = new stdClass();
64f93798
PS
214 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
215 $file->delete();
216 $return->filepath = $newfile->get_filepath();
217 echo json_encode($return);
218 } else {
219 echo json_encode(false);
220 }
221 die;
222
223 case 'zip':
224 $filepath = required_param('filepath', PARAM_PATH);
225
226 $zipper = get_file_packer('application/zip');
227 $fs = get_file_storage();
228
229 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
230
231 $parent_path = $file->get_parent_directory()->get_filepath();
232
233 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
6bdfef5d 234 $return = new stdClass();
64f93798
PS
235 $return->filepath = $parent_path;
236 echo json_encode($return);
237 } else {
238 echo json_encode(false);
239 }
240 die;
241
242 case 'downloaddir':
243 $filepath = required_param('filepath', PARAM_PATH);
244
245 $zipper = get_file_packer('application/zip');
246 $fs = get_file_storage();
247 $area = file_get_draft_area_info($draftid);
248 if ($area['filecount'] == 0) {
249 echo json_encode(false);
250 die;
251 }
252
253 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
254 if ($filepath === '/') {
255 $parent_path = '/';
256 $filename = get_string('files').'.zip';
257 } else {
258 $parent_path = $stored_file->get_parent_directory()->get_filepath();
259 $filename = trim($filepath, '/').'.zip';
260 }
261
262 // archive compressed file to an unused draft area
263 $newdraftitemid = file_get_unused_draft_itemid();
264 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
6bdfef5d 265 $return = new stdClass();
50a8bd6c 266 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
64f93798
PS
267 $return->filepath = $parent_path;
268 echo json_encode($return);
269 } else {
270 echo json_encode(false);
271 }
272 die;
273
274 case 'unzip':
275 $filename = required_param('filename', PARAM_FILE);
276 $filepath = required_param('filepath', PARAM_PATH);
277
278 $zipper = get_file_packer('application/zip');
279
280 $fs = get_file_storage();
281
282 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
283
284 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
6bdfef5d 285 $return = new stdClass();
64f93798 286 $return->filepath = $filepath;
e709ddd2
MG
287 $return->tree = new stdClass();
288 file_get_drafarea_folders($draftid, '/', $return->tree);
64f93798
PS
289 echo json_encode($return);
290 } else {
291 echo json_encode(false);
292 }
293 die;
294
295 default:
296 // no/unknown action?
297 echo json_encode(false);
298 die;
299}