Merge branch 'install_22_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_22...
[moodle.git] / repository / draftfiles_ajax.php
blob1b9903640c39e422a37391ad245f41178c1ce955
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 $info = file_get_draft_area_info($draftid);
63 $data->filecount = $info['filecount'];
64 $data->filesize = $info['filesize'];
65 echo json_encode($data);
66 die;
68 case 'mkdir':
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);
77 die;
79 case 'delete':
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) {
91 $file->delete();
93 $stored_file->delete();
94 $return->filepath = $parent_path;
95 echo json_encode($return);
96 } else {
97 if($result = $stored_file->delete()) {
98 $return->filepath = $parent_path;
99 echo json_encode($return);
100 } else {
101 echo json_encode(false);
104 } else {
105 echo json_encode(false);
107 die;
109 case 'setmainfile':
110 $filename = required_param('filename', PARAM_FILE);
111 $filepath = required_param('filepath', PARAM_PATH);
113 $filepath = file_correct_filepath($filepath);
114 // reset sort order
115 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
116 // set main file
117 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
118 echo json_encode($return);
119 die;
121 case 'rename':
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);
133 $file->delete();
134 $return->filepath = $newfile->get_filepath();
135 echo json_encode($return);
136 } else {
137 echo json_encode(false);
139 die;
141 case 'renamedir':
142 case 'movedir':
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);
149 die;
151 if ($action === 'renamedir') {
152 $newdirname = required_param('newdirname', PARAM_FILE);
153 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH);
154 $newfilepath = $parent . $newdirname . '/';
155 } else {
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);
166 die;
169 $xfilepath = preg_quote($filepath, '|');
171 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
172 $moved = array();
173 foreach ($files as $file) {
174 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
175 continue;
177 // move one by one
178 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
179 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
180 $moved[] = $file;
182 foreach ($moved as $file) {
183 // delete all old
184 $file->delete();
187 $return = new stdClass();
188 if ($action === 'renamedir') {
189 $return->filepath = $parent;
190 } else {
191 $return->filepath = $newfilepath;
193 echo json_encode($return);
194 die;
196 case 'movefile':
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);
208 $file->delete();
209 $return->filepath = $newfile->get_filepath();
210 echo json_encode($return);
211 } else {
212 echo json_encode(false);
214 die;
216 case 'zip':
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);
230 } else {
231 echo json_encode(false);
233 die;
235 case 'downloaddir':
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);
243 die;
246 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
247 if ($filepath === '/') {
248 $parent_path = '/';
249 $filename = get_string('files').'.zip';
250 } else {
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);
262 } else {
263 echo json_encode(false);
265 die;
267 case 'unzip':
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);
281 } else {
282 echo json_encode(false);
284 die;
286 default:
287 // no/unknown action?
288 echo json_encode(false);
289 die;