Merge branch 'w04_MDL-31173_m23_restore' of git://github.com/skodak/moodle
[moodle.git] / repository / draftfiles_ajax.php
blob0f910ddc9af2a860c078608ff2ccdfef328f324c
1 <?php
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.
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/>.
18 /**
19 * Draft file ajax file manager
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
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());
33 require_login();
34 if (isguestuser()) {
35 print_error('noguest');
37 require_sesskey();
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!!
51 switch ($action) {
52 case 'dir':
53 $data = new stdClass();
54 file_get_drafarea_folders($draftid, $filepath, $data);
55 echo json_encode($data);
56 die;
58 case 'list':
59 $filepath = optional_param('filepath', '/', PARAM_PATH);
61 $data = file_get_drafarea_files($draftid, $filepath);
62 echo json_encode($data);
63 die;
65 case 'mkdir':
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);
74 die;
76 case 'delete':
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) {
88 $file->delete();
90 $stored_file->delete();
91 $return->filepath = $parent_path;
92 echo json_encode($return);
93 } else {
94 if($result = $stored_file->delete()) {
95 $return->filepath = $parent_path;
96 echo json_encode($return);
97 } else {
98 echo json_encode(false);
101 } else {
102 echo json_encode(false);
104 die;
106 case 'setmainfile':
107 $filename = required_param('filename', PARAM_FILE);
108 $filepath = required_param('filepath', PARAM_PATH);
110 $filepath = file_correct_filepath($filepath);
111 // reset sort order
112 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
113 // set main file
114 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
115 echo json_encode($return);
116 die;
118 case 'rename':
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);
130 $file->delete();
131 $return->filepath = $newfile->get_filepath();
132 echo json_encode($return);
133 } else {
134 echo json_encode(false);
136 die;
138 case 'renamedir':
139 case 'movedir':
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);
146 die;
148 if ($action === 'renamedir') {
149 $newdirname = required_param('newdirname', PARAM_FILE);
150 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH);
151 $newfilepath = $parent . $newdirname . '/';
152 } else {
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);
163 die;
166 $xfilepath = preg_quote($filepath, '|');
168 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
169 $moved = array();
170 foreach ($files as $file) {
171 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
172 continue;
174 // move one by one
175 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
176 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
177 $moved[] = $file;
179 foreach ($moved as $file) {
180 // delete all old
181 $file->delete();
184 $return = new stdClass();
185 if ($action === 'renamedir') {
186 $return->filepath = $parent;
187 } else {
188 $return->filepath = $newfilepath;
190 echo json_encode($return);
191 die;
193 case 'movefile':
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);
205 $file->delete();
206 $return->filepath = $newfile->get_filepath();
207 echo json_encode($return);
208 } else {
209 echo json_encode(false);
211 die;
213 case 'zip':
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);
227 } else {
228 echo json_encode(false);
230 die;
232 case 'downloaddir':
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);
240 die;
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';
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);
259 } else {
260 echo json_encode(false);
262 die;
264 case 'unzip':
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);
278 } else {
279 echo json_encode(false);
281 die;
283 default:
284 // no/unknown action?
285 echo json_encode(false);
286 die;