MDL-22647 - Fixed undefined $strintro. Change $strintro to $strentries
[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');
32
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
45//
46//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!!
47//
48
49switch ($action) {
50 case 'dir':
51 $data = new stdclass;
52 file_get_drafarea_folders($draftid, $filepath, $data);
53 echo json_encode($data);
54 die;
55
56 case 'list':
57 $filepath = optional_param('filepath', '/', PARAM_PATH);
58
59 $data = file_get_drafarea_files($draftid, $filepath);
60 echo json_encode($data);
61 die;
62
63 case 'mkdir':
64 $filepath = required_param('filepath', PARAM_PATH);
65 $newdirname = required_param('newdirname', PARAM_FILE);
66
67 $fs = get_file_storage();
68 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
69 $return = new stdclass;
70 $return->filepath = $filepath;
71 echo json_encode($return);
72 die;
73
74 case 'delete':
75 $filename = required_param('filename', PARAM_FILE);
76 $filepath = required_param('filepath', PARAM_PATH);
77
78 $fs = get_file_storage();
79 $filepath = file_correct_filepath($filepath);
80 $return = new stdclass;
81 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
82 $parent_path = $stored_file->get_parent_directory()->get_filepath();
83 if($result = $stored_file->delete()) {
84 $return->filepath = $parent_path;
85 echo json_encode($return);
86 } else {
87 echo json_encode(false);
88 }
89 } else {
90 echo json_encode(false);
91 }
92 die;
93
94 case 'setmainfile':
95 $filename = required_param('filename', PARAM_FILE);
96 $filepath = required_param('filepath', PARAM_PATH);
97
98 $filepath = file_correct_filepath($filepath);
99 // reset sort order
100 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
101 // set main file
102 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
103 echo json_encode($return);
104 die;
105
106 case 'rename':
107 $filename = required_param('filename', PARAM_FILE);
108 $filepath = required_param('filepath', PARAM_PATH);
109 $newfilename = required_param('newfilename', PARAM_FILE);
110
111 $fs = get_file_storage();
112 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) {
113 //bad luck, we can not rename!
114 echo json_encode(false);
115 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
116 $return = new object();
117 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
118 $file->delete();
119 $return->filepath = $newfile->get_filepath();
120 echo json_encode($return);
121 } else {
122 echo json_encode(false);
123 }
124 die;
125
126 case 'renamedir':
54da2ec7 127 case 'movedir':
64f93798 128
54da2ec7
PS
129 $filepath = required_param('filepath', PARAM_PATH);
130 $fs = get_file_storage();
64f93798 131
54da2ec7
PS
132 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
133 echo json_encode(false);
134 die;
135 }
136 if ($action === 'renamedir') {
137 $newdirname = required_param('newdirname', PARAM_FILE);
138 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH);
139 $newfilepath = $parent . $newdirname . '/';
140 } else {
141 $newfilepath = required_param('newfilepath', PARAM_PATH);
142 $parts = explode('/', trim($dir->get_filepath(), '/'));
143 $dirname = end($parts);
144 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH);
145 }
64f93798 146
54da2ec7
PS
147 //we must update directory and all children too
148 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
149 //bad luck, we can not rename if something already exists there
150 echo json_encode(false);
151 die;
152 }
153
154 $xfilepath = preg_quote($filepath, '|');
155
156 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
157 $moved = array();
158 foreach ($files as $file) {
159 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
160 continue;
161 }
162 // move one by one
163 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
164 $fs->create_file_from_storedfile(array('filepath'=>$path), $file);
165 $moved[] = $file;
166 }
167 foreach ($moved as $file) {
168 // delete all old
169 $file->delete();
170 }
171
172 $return = new object();
173 if ($action === 'renamedir') {
174 $return->filepath = $parent;
175 } else {
176 $return->filepath = $newfilepath;
177 }
178 echo json_encode($return);
64f93798
PS
179 die;
180
181 case 'movefile':
182 $filename = required_param('filename', PARAM_FILE);
183 $filepath = required_param('filepath', PARAM_PATH);
184 $newfilepath = required_param('newfilepath', PARAM_PATH);
185
186 $fs = get_file_storage();
187 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) {
188 //bad luck, we can not rename!
189 echo json_encode(false);
190 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
191 $return = new object();
192 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
193 $file->delete();
194 $return->filepath = $newfile->get_filepath();
195 echo json_encode($return);
196 } else {
197 echo json_encode(false);
198 }
199 die;
200
201 case 'zip':
202 $filepath = required_param('filepath', PARAM_PATH);
203
204 $zipper = get_file_packer('application/zip');
205 $fs = get_file_storage();
206
207 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
208
209 $parent_path = $file->get_parent_directory()->get_filepath();
210
211 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
212 $return = new stdclass;
213 $return->filepath = $parent_path;
214 echo json_encode($return);
215 } else {
216 echo json_encode(false);
217 }
218 die;
219
220 case 'downloaddir':
221 $filepath = required_param('filepath', PARAM_PATH);
222
223 $zipper = get_file_packer('application/zip');
224 $fs = get_file_storage();
225 $area = file_get_draft_area_info($draftid);
226 if ($area['filecount'] == 0) {
227 echo json_encode(false);
228 die;
229 }
230
231 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
232 if ($filepath === '/') {
233 $parent_path = '/';
234 $filename = get_string('files').'.zip';
235 } else {
236 $parent_path = $stored_file->get_parent_directory()->get_filepath();
237 $filename = trim($filepath, '/').'.zip';
238 }
239
240 // archive compressed file to an unused draft area
241 $newdraftitemid = file_get_unused_draft_itemid();
242 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
243 $return = new stdclass;
50a8bd6c 244 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
64f93798
PS
245 $return->filepath = $parent_path;
246 echo json_encode($return);
247 } else {
248 echo json_encode(false);
249 }
250 die;
251
252 case 'unzip':
253 $filename = required_param('filename', PARAM_FILE);
254 $filepath = required_param('filepath', PARAM_PATH);
255
256 $zipper = get_file_packer('application/zip');
257
258 $fs = get_file_storage();
259
260 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
261
262 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
263 $return = new stdclass;
264 $return->filepath = $filepath;
265 echo json_encode($return);
266 } else {
267 echo json_encode(false);
268 }
269 die;
270
271 default:
272 // no/unknown action?
273 echo json_encode(false);
274 die;
275}