Merge branch 'MDL-81413-main' of https://github.com/lameze/moodle
[moodle.git] / repository / draftfiles_ajax.php
blobe1c7a979a3649e81164056a07ae1a0dc4ddd81ba
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 throw new \moodle_exception('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 $usercontext = 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($usercontext->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);
85 $selectedfile = (object)[
86 'filename' => $filename,
87 'filepath' => $filepath
89 $return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
91 if ($return) {
92 $response = new stdClass();
93 $response->filepath = array_keys($return)[0];
94 echo json_encode($response);
95 die;
98 echo json_encode(false);
99 die;
101 case 'deleteselected':
102 $selected = required_param('selected', PARAM_RAW);
103 $return = [];
104 $selectedfiles = json_decode($selected);
105 $return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
106 echo (json_encode($return ? array_keys($return) : 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($usercontext->id, 'user', 'draft', $draftid);
116 // set main file
117 $return = file_set_sortorder($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
118 echo json_encode($return);
119 die;
121 case 'updatefile':
122 // Allows to Rename file, move it to another directory, change it's license and author information in one request
123 $filename = required_param('filename', PARAM_FILE);
124 $filepath = required_param('filepath', PARAM_PATH);
125 $updatedata = array();
126 $updatedata['filename'] = optional_param('newfilename', $filename, PARAM_FILE);
127 $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $filepath, PARAM_PATH);
128 if (($v = optional_param('newlicense', false, PARAM_TEXT)) !== false) {
129 $updatedata['license'] = $v;
131 if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
132 $updatedata['author'] = $v;
134 try {
135 repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
136 } catch (moodle_exception $e) {
137 die(json_encode((object)array('error' => $e->getMessage())));
139 die(json_encode((object)array('filepath' => $newfilepath)));
141 case 'updatedir':
142 $filepath = required_param('filepath', PARAM_PATH);
143 $newdirname = required_param('newdirname', PARAM_FILE);
144 $parent = required_param('newfilepath', PARAM_PATH);
145 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
146 try {
147 repository::update_draftfile($draftid, $filepath, '.', array('filepath' => $newfilepath));
148 } catch (moodle_exception $e) {
149 die(json_encode((object)array('error' => $e->getMessage())));
151 die(json_encode((object)array('filepath' => $parent)));
153 case 'zip':
154 $filepath = required_param('filepath', PARAM_PATH);
156 $zipper = get_file_packer('application/zip');
157 $fs = get_file_storage();
159 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.');
161 $parent_path = $file->get_parent_directory()->get_filepath();
163 $filepath = explode('/', trim($file->get_filepath(), '/'));
164 $filepath = array_pop($filepath);
165 $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
167 if ($newfile = $zipper->archive_to_storage([$filepath => $file], $usercontext->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
168 $return = new stdClass();
169 $return->filepath = $parent_path;
170 echo json_encode($return);
171 } else {
172 echo json_encode(false);
174 die;
175 case 'downloadselected':
176 $selected = required_param('selected', PARAM_RAW);
177 $selectedfiles = json_decode($selected);
178 if (!count($selectedfiles)) {
179 $filepath = required_param('filepath', PARAM_PATH);
180 $selectedfiles = [(object)[
181 'filename' => '',
182 'filepath' => $filepath
185 $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
186 echo (json_encode($return));
187 die;
189 case 'downloaddir':
190 $filepath = required_param('filepath', PARAM_PATH);
192 $selectedfile = (object)[
193 'filename' => '',
194 'filepath' => $filepath
196 $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
197 echo json_encode($return);
198 die;
200 case 'unzip':
201 $filename = required_param('filename', PARAM_FILE);
202 $filepath = required_param('filepath', PARAM_PATH);
203 $areamaxbytes = required_param('areamaxbytes', PARAM_INT);
205 $return = new stdClass();
206 $zipper = get_file_packer('application/zip');
207 $fs = get_file_storage();
208 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
209 // Get the total size of the content in the archive.
210 $filecontentsize = $file->get_total_content_size($zipper);
212 // Return an error if the returned size of the content is NULL.
213 // This means the utility class was unable to read the content of the archive.
214 if (is_null($filecontentsize)) {
215 $return->error = get_string('cannotunzipcontentunreadable', 'repository');
216 die(json_encode($return));
219 // Check whether the maximum size allowed in this draft area will be exceeded with unzipping the file.
220 // If the maximum size allowed is exceeded, return an error before attempting to unzip.
221 if (file_is_draft_area_limit_reached($draftid, $areamaxbytes, $filecontentsize)) {
222 $return->error = get_string('cannotunzipquotaexceeded', 'repository');
223 die(json_encode($return));
226 // Find unused name for directory to extract the archive.
227 $temppath = $fs->get_unused_dirname($usercontext->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
228 $donotremovedirs = array();
229 $doremovedirs = array($temppath);
231 // Extract archive and move all files from $temppath to $filepath
232 if (($processed = $file->extract_to_storage($zipper, $usercontext->id, 'user', 'draft', $draftid, $temppath, $USER->id))
233 !== false) {
235 // Find all failures within the processed files, and return an error if any are found.
236 $failed = array_filter($processed, static function($result): bool {
237 return $result !== true;
239 if (count($failed) > 0) {
240 if ($failed[0] == "cannotunzipquotaexceeded") {
241 $return->error = get_string($failed[0], 'repository');
242 } else {
243 $return->error = get_string('cannotunzipextractfileerror', 'repository');
245 die(json_encode($return));
248 $extractedfiles = $fs->get_directory_files($usercontext->id, 'user', 'draft', $draftid, $temppath, true);
249 $xtemppath = preg_quote($temppath, '|');
250 foreach ($extractedfiles as $file) {
251 $realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
252 if (!$file->is_directory()) {
253 // Set the source to the extracted file to indicate that it came from archive.
254 $file->set_source(serialize((object)array('source' => $filepath)));
256 if (!$fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
257 // File or directory did not exist, just move it.
258 $file->rename($realpath, $file->get_filename());
259 } else if (!$file->is_directory()) {
260 // File already existed, overwrite it
261 repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
262 } else {
263 // Directory already existed, remove temporary dir but make sure we don't remove the existing dir
264 $doremovedirs[] = $file->get_filepath();
265 $donotremovedirs[] = $realpath;
268 $return->filepath = $filepath;
269 } else {
270 $return = false;
272 // Remove remaining temporary directories.
273 foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
274 if ($file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.')) {
275 $file->delete();
278 die(json_encode($return));
280 case 'getoriginal':
281 $filename = required_param('filename', PARAM_FILE);
282 $filepath = required_param('filepath', PARAM_PATH);
284 $fs = get_file_storage();
285 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
286 if (!$file) {
287 echo json_encode(false);
288 } else {
289 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
290 echo json_encode((object)$return);
292 die;
294 case 'getreferences':
295 $filename = required_param('filename', PARAM_FILE);
296 $filepath = required_param('filepath', PARAM_PATH);
298 $fs = get_file_storage();
299 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
300 if (!$file) {
301 echo json_encode(false);
302 } else {
303 $source = unserialize($file->get_source());
304 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
305 $browser = get_file_browser();
306 if (isset($source->original)) {
307 $reffiles = $fs->search_references($source->original);
308 foreach ($reffiles as $reffile) {
309 $refcontext = context::instance_by_id($reffile->get_contextid());
310 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
311 if (empty($fileinfo)) {
312 $return['references'][] = get_string('undisclosedreference', 'repository');
313 } else {
314 $return['references'][] = $fileinfo->get_readable_fullname();
318 echo json_encode((object)$return);
320 die;
322 default:
323 // no/unknown action?
324 echo json_encode(false);
325 die;