Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[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');
ea62bb3e 32$PAGE->set_context(get_system_context());
64f93798
PS
33require_login();
34if (isguestuser()) {
35 print_error('noguest');
36}
37require_sesskey();
38
39$action = required_param('action', PARAM_ALPHA);
40$draftid = required_param('itemid', PARAM_INT);
eacae660 41$filepath = optional_param('filepath', '/', PARAM_PATH);
64f93798
PS
42
43$user_context = get_context_instance(CONTEXT_USER, $USER->id);
44
bce08d9a
PS
45echo $OUTPUT->header(); // send headers
46
64f93798
PS
47//
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!!
49//
50
51switch ($action) {
52 case 'dir':
6bdfef5d 53 $data = new stdClass();
64f93798
PS
54 file_get_drafarea_folders($draftid, $filepath, $data);
55 echo json_encode($data);
56 die;
57
58 case 'list':
59 $filepath = optional_param('filepath', '/', PARAM_PATH);
60
61 $data = file_get_drafarea_files($draftid, $filepath);
62 echo json_encode($data);
63 die;
64
65 case 'mkdir':
66 $filepath = required_param('filepath', PARAM_PATH);
67 $newdirname = required_param('newdirname', PARAM_FILE);
68
69 $fs = get_file_storage();
70 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
6bdfef5d 71 $return = new stdClass();
64f93798
PS
72 $return->filepath = $filepath;
73 echo json_encode($return);
74 die;
75
76 case 'delete':
77 $filename = required_param('filename', PARAM_FILE);
78 $filepath = required_param('filepath', PARAM_PATH);
79
80 $fs = get_file_storage();
81 $filepath = file_correct_filepath($filepath);
6bdfef5d 82 $return = new stdClass();
64f93798
PS
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();
5ad337c8
DC
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();
89 }
90 $stored_file->delete();
64f93798
PS
91 $return->filepath = $parent_path;
92 echo json_encode($return);
93 } else {
5ad337c8
DC
94 if($result = $stored_file->delete()) {
95 $return->filepath = $parent_path;
96 echo json_encode($return);
97 } else {
98 echo json_encode(false);
99 }
64f93798
PS
100 }
101 } else {
102 echo json_encode(false);
103 }
104 die;
105
106 case 'setmainfile':
107 $filename = required_param('filename', PARAM_FILE);
108 $filepath = required_param('filepath', PARAM_PATH);
109
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;
117
118 case 'rename':
119 $filename = required_param('filename', PARAM_FILE);
120 $filepath = required_param('filepath', PARAM_PATH);
121 $newfilename = required_param('newfilename', PARAM_FILE);
122
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)) {
a27ab6fd 128 $return = new stdClass();
64f93798
PS
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);
135 }
136 die;
137
138 case 'renamedir':
54da2ec7 139 case 'movedir':
64f93798 140
54da2ec7
PS
141 $filepath = required_param('filepath', PARAM_PATH);
142 $fs = get_file_storage();
64f93798 143
54da2ec7
PS
144 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
145 echo json_encode(false);
146 die;
147 }
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);
157 }
64f93798 158
54da2ec7
PS
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;
164 }
165
166 $xfilepath = preg_quote($filepath, '|');
167
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;
173 }
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;
178 }
179 foreach ($moved as $file) {
180 // delete all old
181 $file->delete();
182 }
183
a27ab6fd 184 $return = new stdClass();
54da2ec7
PS
185 if ($action === 'renamedir') {
186 $return->filepath = $parent;
187 } else {
188 $return->filepath = $newfilepath;
189 }
190 echo json_encode($return);
64f93798
PS
191 die;
192
193 case 'movefile':
194 $filename = required_param('filename', PARAM_FILE);
195 $filepath = required_param('filepath', PARAM_PATH);
196 $newfilepath = required_param('newfilepath', PARAM_PATH);
197
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)) {
a27ab6fd 203 $return = new stdClass();
64f93798
PS
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);
210 }
211 die;
212
213 case 'zip':
214 $filepath = required_param('filepath', PARAM_PATH);
215
216 $zipper = get_file_packer('application/zip');
217 $fs = get_file_storage();
218
219 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
220
221 $parent_path = $file->get_parent_directory()->get_filepath();
222
223 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
6bdfef5d 224 $return = new stdClass();
64f93798
PS
225 $return->filepath = $parent_path;
226 echo json_encode($return);
227 } else {
228 echo json_encode(false);
229 }
230 die;
231
232 case 'downloaddir':
233 $filepath = required_param('filepath', PARAM_PATH);
234
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;
241 }
242
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';
250 }
251
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)) {
6bdfef5d 255 $return = new stdClass();
50a8bd6c 256 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
64f93798
PS
257 $return->filepath = $parent_path;
258 echo json_encode($return);
259 } else {
260 echo json_encode(false);
261 }
262 die;
263
264 case 'unzip':
265 $filename = required_param('filename', PARAM_FILE);
266 $filepath = required_param('filepath', PARAM_PATH);
267
268 $zipper = get_file_packer('application/zip');
269
270 $fs = get_file_storage();
271
272 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
273
274 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
6bdfef5d 275 $return = new stdClass();
64f93798
PS
276 $return->filepath = $filepath;
277 echo json_encode($return);
278 } else {
279 echo json_encode(false);
280 }
281 die;
282
283 default:
284 // no/unknown action?
285 echo json_encode(false);
286 die;
287}