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 $info = file_get_draft_area_info($draftid);
63 $data->filecount
= $info['filecount'];
64 $data->filesize
= $info['filesize'];
65 echo json_encode($data);
69 $filepath = required_param('filepath', PARAM_PATH
);
70 $newdirname = required_param('newdirname', PARAM_FILE
);
72 $fs = get_file_storage();
73 $fs->create_directory($user_context->id
, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
74 $return = new stdClass();
75 $return->filepath
= $filepath;
76 echo json_encode($return);
80 $filename = required_param('filename', PARAM_FILE
);
81 $filepath = required_param('filepath', PARAM_PATH
);
83 $fs = get_file_storage();
84 $filepath = file_correct_filepath($filepath);
85 $return = new stdClass();
86 if ($stored_file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
87 $parent_path = $stored_file->get_parent_directory()->get_filepath();
88 if ($stored_file->is_directory()) {
89 $files = $fs->get_directory_files($user_context->id
, 'user', 'draft', $draftid, $filepath, true);
90 foreach ($files as $file) {
93 $stored_file->delete();
94 $return->filepath
= $parent_path;
95 echo json_encode($return);
97 if($result = $stored_file->delete()) {
98 $return->filepath
= $parent_path;
99 echo json_encode($return);
101 echo json_encode(false);
105 echo json_encode(false);
110 $filename = required_param('filename', PARAM_FILE
);
111 $filepath = required_param('filepath', PARAM_PATH
);
113 $filepath = file_correct_filepath($filepath);
115 file_reset_sortorder($user_context->id
, 'user', 'draft', $draftid);
117 $return = file_set_sortorder($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename, 1);
118 echo json_encode($return);
122 $filename = required_param('filename', PARAM_FILE
);
123 $filepath = required_param('filepath', PARAM_PATH
);
124 $newfilename = required_param('newfilename', PARAM_FILE
);
126 $fs = get_file_storage();
127 if ($fs->file_exists($user_context->id
, 'user', 'draft', $draftid, $filepath, $newfilename)) {
128 //bad luck, we can not rename!
129 echo json_encode(false);
130 } else if ($file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
131 $return = new stdClass();
132 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
134 $return->filepath
= $newfile->get_filepath();
135 echo json_encode($return);
137 echo json_encode(false);
144 $filepath = required_param('filepath', PARAM_PATH
);
145 $fs = get_file_storage();
147 if (!$dir = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.')) {
148 echo json_encode(false);
151 if ($action === 'renamedir') {
152 $newdirname = required_param('newdirname', PARAM_FILE
);
153 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH
);
154 $newfilepath = $parent . $newdirname . '/';
156 $newfilepath = required_param('newfilepath', PARAM_PATH
);
157 $parts = explode('/', trim($dir->get_filepath(), '/'));
158 $dirname = end($parts);
159 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH
);
162 //we must update directory and all children too
163 if ($fs->get_directory_files($user_context->id
, 'user', 'draft', $draftid, $newfilepath, true)) {
164 //bad luck, we can not rename if something already exists there
165 echo json_encode(false);
169 $xfilepath = preg_quote($filepath, '|');
171 $files = $fs->get_area_files($user_context->id
, 'user', 'draft', $draftid);
173 foreach ($files as $file) {
174 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
178 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
179 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
182 foreach ($moved as $file) {
187 $return = new stdClass();
188 if ($action === 'renamedir') {
189 $return->filepath
= $parent;
191 $return->filepath
= $newfilepath;
193 echo json_encode($return);
197 $filename = required_param('filename', PARAM_FILE
);
198 $filepath = required_param('filepath', PARAM_PATH
);
199 $newfilepath = required_param('newfilepath', PARAM_PATH
);
201 $fs = get_file_storage();
202 if ($fs->file_exists($user_context->id
, 'user', 'draft', $draftid, $newfilepath, $filename)) {
203 //bad luck, we can not rename!
204 echo json_encode(false);
205 } else if ($file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename)) {
206 $return = new stdClass();
207 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
209 $return->filepath
= $newfile->get_filepath();
210 echo json_encode($return);
212 echo json_encode(false);
217 $filepath = required_param('filepath', PARAM_PATH
);
219 $zipper = get_file_packer('application/zip');
220 $fs = get_file_storage();
222 $file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.');
224 $parent_path = $file->get_parent_directory()->get_filepath();
226 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id
, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id
)) {
227 $return = new stdClass();
228 $return->filepath
= $parent_path;
229 echo json_encode($return);
231 echo json_encode(false);
236 $filepath = required_param('filepath', PARAM_PATH
);
238 $zipper = get_file_packer('application/zip');
239 $fs = get_file_storage();
240 $area = file_get_draft_area_info($draftid);
241 if ($area['filecount'] == 0) {
242 echo json_encode(false);
246 $stored_file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, '.');
247 if ($filepath === '/') {
249 $filename = get_string('files').'.zip';
251 $parent_path = $stored_file->get_parent_directory()->get_filepath();
252 $filename = trim($filepath, '/').'.zip';
255 // archive compressed file to an unused draft area
256 $newdraftitemid = file_get_unused_draft_itemid();
257 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id
, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id
)) {
258 $return = new stdClass();
259 $return->fileurl
= moodle_url
::make_draftfile_url($newdraftitemid, '/', $filename)->out();
260 $return->filepath
= $parent_path;
261 echo json_encode($return);
263 echo json_encode(false);
268 $filename = required_param('filename', PARAM_FILE
);
269 $filepath = required_param('filepath', PARAM_PATH
);
271 $zipper = get_file_packer('application/zip');
273 $fs = get_file_storage();
275 $file = $fs->get_file($user_context->id
, 'user', 'draft', $draftid, $filepath, $filename);
277 if ($newfile = $file->extract_to_storage($zipper, $user_context->id
, 'user', 'draft', $draftid, $filepath, $USER->id
)) {
278 $return = new stdClass();
279 $return->filepath
= $filepath;
280 echo json_encode($return);
282 echo json_encode(false);
287 // no/unknown action?
288 echo json_encode(false);