3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Draft file ajax file manager
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
27 define('AJAX_SCRIPT', true);
29 require('../config.php');
30 require_once($CFG->libdir
.'/filelib.php');
31 require_once($CFG->libdir
.'/adminlib.php');
32 $PAGE->set_context(get_system_context());
35 print_error('noguest');
39 $action = required_param('action', PARAM_ALPHA
);
40 $draftid = required_param('itemid', PARAM_INT
);
41 $filepath = optional_param('filepath', '/', PARAM_PATH
);
43 $user_context = get_context_instance(CONTEXT_USER
, $USER->id
);
45 echo $OUTPUT->header(); // send headers
48 //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!!
53 $data = new stdClass();
54 file_get_drafarea_folders($draftid, $filepath, $data);
55 echo json_encode($data);
59 $filepath = optional_param('filepath', '/', PARAM_PATH
);
61 $data = file_get_drafarea_files($draftid, $filepath);
62 echo json_encode($data);
66 $filepath = required_param('filepath', PARAM_PATH
);
67 $newdirname = required_param('newdirname', PARAM_FILE
);
69 $fs = get_file_storage();
70 $fs->create_directory($user_context->id
, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
71 $return = new stdClass();
72 $return->filepath
= $filepath;
73 echo json_encode($return);
77 $filename = required_param('filename', PARAM_FILE
);
78 $filepath = required_param('filepath', PARAM_PATH
);
80 $fs = get_file_storage();
81 $filepath = file_correct_filepath($filepath);
82 $return = new stdClass();
83 if ($stored_file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
84 $parent_path = $stored_file->get_parent_directory()->get_filepath();
85 if ($stored_file->is_directory()) {
86 $files = $fs->get_directory_files($user_context->id
, 'user', 'draft', $draftid, $filepath, true);
87 foreach ($files as $file) {
90 $stored_file->delete();
91 $return->filepath
= $parent_path;
92 echo json_encode($return);
94 if($result = $stored_file->delete()) {
95 $return->filepath
= $parent_path;
96 echo json_encode($return);
98 echo json_encode(false);
102 echo json_encode(false);
107 $filename = required_param('filename', PARAM_FILE
);
108 $filepath = required_param('filepath', PARAM_PATH
);
110 $filepath = file_correct_filepath($filepath);
112 file_reset_sortorder($user_context->id
, 'user', 'draft', $draftid);
114 $return = file_set_sortorder($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename, 1);
115 echo json_encode($return);
119 $filename = required_param('filename', PARAM_FILE
);
120 $filepath = required_param('filepath', PARAM_PATH
);
121 $newfilename = required_param('newfilename', PARAM_FILE
);
123 $fs = get_file_storage();
124 if ($fs->file_exists($user_context->id
, 'user', 'draft', $draftid, $filepath, $newfilename)) {
125 //bad luck, we can not rename!
126 echo json_encode(false);
127 } else if ($file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
128 $return = new stdClass();
129 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
131 $return->filepath
= $newfile->get_filepath();
132 echo json_encode($return);
134 echo json_encode(false);
141 $filepath = required_param('filepath', PARAM_PATH
);
142 $fs = get_file_storage();
144 if (!$dir = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.')) {
145 echo json_encode(false);
148 if ($action === 'renamedir') {
149 $newdirname = required_param('newdirname', PARAM_FILE
);
150 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH
);
151 $newfilepath = $parent . $newdirname . '/';
153 $newfilepath = required_param('newfilepath', PARAM_PATH
);
154 $parts = explode('/', trim($dir->get_filepath(), '/'));
155 $dirname = end($parts);
156 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH
);
159 //we must update directory and all children too
160 if ($fs->get_directory_files($user_context->id
, 'user', 'draft', $draftid, $newfilepath, true)) {
161 //bad luck, we can not rename if something already exists there
162 echo json_encode(false);
166 $xfilepath = preg_quote($filepath, '|');
168 $files = $fs->get_area_files($user_context->id
, 'user', 'draft', $draftid);
170 foreach ($files as $file) {
171 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
175 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
176 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
179 foreach ($moved as $file) {
184 $return = new stdClass();
185 if ($action === 'renamedir') {
186 $return->filepath
= $parent;
188 $return->filepath
= $newfilepath;
190 echo json_encode($return);
194 $filename = required_param('filename', PARAM_FILE
);
195 $filepath = required_param('filepath', PARAM_PATH
);
196 $newfilepath = required_param('newfilepath', PARAM_PATH
);
198 $fs = get_file_storage();
199 if ($fs->file_exists($user_context->id
, 'user', 'draft', $draftid, $newfilepath, $filename)) {
200 //bad luck, we can not rename!
201 echo json_encode(false);
202 } else if ($file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
203 $return = new stdClass();
204 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
206 $return->filepath
= $newfile->get_filepath();
207 echo json_encode($return);
209 echo json_encode(false);
214 $filepath = required_param('filepath', PARAM_PATH
);
216 $zipper = get_file_packer('application/zip');
217 $fs = get_file_storage();
219 $file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.');
221 $parent_path = $file->get_parent_directory()->get_filepath();
223 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id
, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id
)) {
224 $return = new stdClass();
225 $return->filepath
= $parent_path;
226 echo json_encode($return);
228 echo json_encode(false);
233 $filepath = required_param('filepath', PARAM_PATH
);
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);
243 $stored_file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.');
244 if ($filepath === '/') {
246 $filename = get_string('files').'.zip';
248 $parent_path = $stored_file->get_parent_directory()->get_filepath();
249 $filename = trim($filepath, '/').'.zip';
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
)) {
255 $return = new stdClass();
256 $return->fileurl
= moodle_url
::make_draftfile_url($newdraftitemid, '/', $filename)->out();
257 $return->filepath
= $parent_path;
258 echo json_encode($return);
260 echo json_encode(false);
265 $filename = required_param('filename', PARAM_FILE
);
266 $filepath = required_param('filepath', PARAM_PATH
);
268 $zipper = get_file_packer('application/zip');
270 $fs = get_file_storage();
272 $file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename);
274 if ($newfile = $file->extract_to_storage($zipper, $user_context->id
, 'user', 'draft', $draftid, $filepath, $USER->id
)) {
275 $return = new stdClass();
276 $return->filepath
= $filepath;
277 echo json_encode($return);
279 echo json_encode(false);
284 // no/unknown action?
285 echo json_encode(false);