MDL-57328 core: Remove xdebug extension, so test run faster
[moodle.git] / repository / draftfiles_ajax.php
blobdbd96675fa536ed5bc64e169d9bb8dcacde10781
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 require_once($CFG->dirroot.'/repository/lib.php');
33 $PAGE->set_context(context_system::instance());
34 require_login();
35 if (isguestuser()) {
36 print_error('noguest');
38 require_sesskey();
40 $action = required_param('action', PARAM_ALPHA);
41 $draftid = required_param('itemid', PARAM_INT);
42 $filepath = optional_param('filepath', '/', PARAM_PATH);
44 $user_context = context_user::instance($USER->id);
46 echo $OUTPUT->header(); // send headers
49 //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!!
52 switch ($action) {
53 case 'dir':
54 $data = new stdClass();
55 file_get_drafarea_folders($draftid, $filepath, $data);
56 echo json_encode($data);
57 die;
59 case 'list':
60 $filepath = optional_param('filepath', '/', PARAM_PATH);
62 $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath));
63 $info = file_get_draft_area_info($draftid);
64 $data->filecount = $info['filecount'];
65 $data->filesize = $info['filesize'];
66 $data->tree = new stdClass();
67 file_get_drafarea_folders($draftid, '/', $data->tree);
68 echo json_encode($data);
69 die;
71 case 'mkdir':
72 $filepath = required_param('filepath', PARAM_PATH);
73 $newdirname = required_param('newdirname', PARAM_FILE);
75 $fs = get_file_storage();
76 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
77 $return = new stdClass();
78 $return->filepath = $filepath;
79 echo json_encode($return);
80 die;
82 case 'delete':
83 $filename = required_param('filename', PARAM_FILE);
84 $filepath = required_param('filepath', PARAM_PATH);
86 $fs = get_file_storage();
87 $filepath = file_correct_filepath($filepath);
88 $return = new stdClass();
89 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
90 $parent_path = $stored_file->get_parent_directory()->get_filepath();
91 if ($stored_file->is_directory()) {
92 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $filepath, true);
93 foreach ($files as $file) {
94 $file->delete();
96 $stored_file->delete();
97 $return->filepath = $parent_path;
98 echo json_encode($return);
99 } else {
100 if($result = $stored_file->delete()) {
101 $return->filepath = $parent_path;
102 echo json_encode($return);
103 } else {
104 echo json_encode(false);
107 } else {
108 echo json_encode(false);
110 die;
112 case 'setmainfile':
113 $filename = required_param('filename', PARAM_FILE);
114 $filepath = required_param('filepath', PARAM_PATH);
116 $filepath = file_correct_filepath($filepath);
117 // reset sort order
118 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
119 // set main file
120 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
121 echo json_encode($return);
122 die;
124 case 'updatefile':
125 // Allows to Rename file, move it to another directory, change it's license and author information in one request
126 $filename = required_param('filename', PARAM_FILE);
127 $filepath = required_param('filepath', PARAM_PATH);
128 $updatedata = array();
129 $updatedata['filename'] = optional_param('newfilename', $filename, PARAM_FILE);
130 $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $filepath, PARAM_PATH);
131 if (($v = optional_param('newlicense', false, PARAM_TEXT)) !== false) {
132 $updatedata['license'] = $v;
134 if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
135 $updatedata['author'] = $v;
137 try {
138 repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
139 } catch (moodle_exception $e) {
140 die(json_encode((object)array('error' => $e->getMessage())));
142 die(json_encode((object)array('filepath' => $newfilepath)));
144 case 'updatedir':
145 $filepath = required_param('filepath', PARAM_PATH);
146 $newdirname = required_param('newdirname', PARAM_FILE);
147 $parent = required_param('newfilepath', PARAM_PATH);
148 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
149 try {
150 repository::update_draftfile($draftid, $filepath, '.', array('filepath' => $newfilepath));
151 } catch (moodle_exception $e) {
152 die(json_encode((object)array('error' => $e->getMessage())));
154 die(json_encode((object)array('filepath' => $parent)));
156 case 'zip':
157 $filepath = required_param('filepath', PARAM_PATH);
159 $zipper = get_file_packer('application/zip');
160 $fs = get_file_storage();
162 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
164 $parent_path = $file->get_parent_directory()->get_filepath();
166 $filepath = explode('/', trim($file->get_filepath(), '/'));
167 $filepath = array_pop($filepath);
168 $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
170 if ($newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
171 $return = new stdClass();
172 $return->filepath = $parent_path;
173 echo json_encode($return);
174 } else {
175 echo json_encode(false);
177 die;
179 case 'downloaddir':
180 $filepath = required_param('filepath', PARAM_PATH);
182 $zipper = get_file_packer('application/zip');
183 $fs = get_file_storage();
184 $area = file_get_draft_area_info($draftid, $filepath);
185 if ($area['filecount'] == 0 && $area['foldercount'] == 0) {
186 echo json_encode(false);
187 die;
190 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
191 if ($filepath === '/') {
192 $filename = get_string('files').'.zip';
193 } else {
194 $filename = explode('/', trim($filepath, '/'));
195 $filename = array_pop($filename) . '.zip';
198 // archive compressed file to an unused draft area
199 $newdraftitemid = file_get_unused_draft_itemid();
200 if ($newfile = $zipper->archive_to_storage(array('/' => $stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
201 $return = new stdClass();
202 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
203 $return->filepath = $filepath;
204 echo json_encode($return);
205 } else {
206 echo json_encode(false);
208 die;
210 case 'unzip':
211 $filename = required_param('filename', PARAM_FILE);
212 $filepath = required_param('filepath', PARAM_PATH);
214 $zipper = get_file_packer('application/zip');
216 $fs = get_file_storage();
218 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
220 // Find unused name for directory to extract the archive.
221 $temppath = $fs->get_unused_dirname($user_context->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
222 $donotremovedirs = array();
223 $doremovedirs = array($temppath);
224 // Extract archive and move all files from $temppath to $filepath
225 if ($file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $temppath, $USER->id) !== false) {
226 $extractedfiles = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $temppath, true);
227 $xtemppath = preg_quote($temppath, '|');
228 foreach ($extractedfiles as $file) {
229 $realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
230 if (!$file->is_directory()) {
231 // Set the source to the extracted file to indicate that it came from archive.
232 $file->set_source(serialize((object)array('source' => $filepath)));
234 if (!$fs->file_exists($user_context->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
235 // File or directory did not exist, just move it.
236 $file->rename($realpath, $file->get_filename());
237 } else if (!$file->is_directory()) {
238 // File already existed, overwrite it
239 repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
240 } else {
241 // Directory already existed, remove temporary dir but make sure we don't remove the existing dir
242 $doremovedirs[] = $file->get_filepath();
243 $donotremovedirs[] = $realpath;
246 $return = new stdClass();
247 $return->filepath = $filepath;
248 } else {
249 $return = false;
251 // Remove remaining temporary directories.
252 foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
253 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
254 $file->delete();
257 die(json_encode($return));
259 case 'getoriginal':
260 $filename = required_param('filename', PARAM_FILE);
261 $filepath = required_param('filepath', PARAM_PATH);
263 $fs = get_file_storage();
264 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
265 if (!$file) {
266 echo json_encode(false);
267 } else {
268 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
269 echo json_encode((object)$return);
271 die;
273 case 'getreferences':
274 $filename = required_param('filename', PARAM_FILE);
275 $filepath = required_param('filepath', PARAM_PATH);
277 $fs = get_file_storage();
278 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
279 if (!$file) {
280 echo json_encode(false);
281 } else {
282 $source = unserialize($file->get_source());
283 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
284 $browser = get_file_browser();
285 if (isset($source->original)) {
286 $reffiles = $fs->search_references($source->original);
287 foreach ($reffiles as $reffile) {
288 $refcontext = context::instance_by_id($reffile->get_contextid());
289 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
290 if (empty($fileinfo)) {
291 $return['references'][] = get_string('undisclosedreference', 'repository');
292 } else {
293 $return['references'][] = $fileinfo->get_readable_fullname();
297 echo json_encode((object)$return);
299 die;
301 default:
302 // no/unknown action?
303 echo json_encode(false);
304 die;