MDL-68758 core_repository: Allow ability to download files on all views.
[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');
e709ddd2 32require_once($CFG->dirroot.'/repository/lib.php');
0601e0ee 33$PAGE->set_context(context_system::instance());
64f93798
PS
34require_login();
35if (isguestuser()) {
36 print_error('noguest');
37}
38require_sesskey();
39
40$action = required_param('action', PARAM_ALPHA);
41$draftid = required_param('itemid', PARAM_INT);
eacae660 42$filepath = optional_param('filepath', '/', PARAM_PATH);
64f93798 43
2f039236 44$usercontext = context_user::instance($USER->id);
64f93798 45
bce08d9a
PS
46echo $OUTPUT->header(); // send headers
47
64f93798
PS
48//
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!!
50//
51
52switch ($action) {
53 case 'dir':
6bdfef5d 54 $data = new stdClass();
64f93798
PS
55 file_get_drafarea_folders($draftid, $filepath, $data);
56 echo json_encode($data);
57 die;
58
59 case 'list':
60 $filepath = optional_param('filepath', '/', PARAM_PATH);
61
e709ddd2 62 $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath));
910e1ecd
DS
63 $info = file_get_draft_area_info($draftid);
64 $data->filecount = $info['filecount'];
65 $data->filesize = $info['filesize'];
e709ddd2
MG
66 $data->tree = new stdClass();
67 file_get_drafarea_folders($draftid, '/', $data->tree);
64f93798
PS
68 echo json_encode($data);
69 die;
70
71 case 'mkdir':
72 $filepath = required_param('filepath', PARAM_PATH);
73 $newdirname = required_param('newdirname', PARAM_FILE);
74
75 $fs = get_file_storage();
2f039236 76 $fs->create_directory($usercontext->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
6bdfef5d 77 $return = new stdClass();
64f93798
PS
78 $return->filepath = $filepath;
79 echo json_encode($return);
80 die;
81
82 case 'delete':
83 $filename = required_param('filename', PARAM_FILE);
84 $filepath = required_param('filepath', PARAM_PATH);
2f039236
P
85 $selectedfile = (object)[
86 'filename' => $filename,
87 'filepath' => $filepath
88 ];
89 $return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
90
91 if ($return) {
92 $response = new stdClass();
93 $response->filepath = array_keys($return)[0];
94 echo json_encode($response);
95 die;
64f93798 96 }
2f039236
P
97
98 echo json_encode(false);
99 die;
100
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));
64f93798
PS
107 die;
108
109 case 'setmainfile':
110 $filename = required_param('filename', PARAM_FILE);
111 $filepath = required_param('filepath', PARAM_PATH);
112
113 $filepath = file_correct_filepath($filepath);
114 // reset sort order
2f039236 115 file_reset_sortorder($usercontext->id, 'user', 'draft', $draftid);
64f93798 116 // set main file
2f039236 117 $return = file_set_sortorder($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
64f93798
PS
118 echo json_encode($return);
119 die;
120
c092681b
MG
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);
c092681b 125 $updatedata = array();
f7c588d2
MG
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;
c092681b 130 }
f7c588d2
MG
131 if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
132 $updatedata['author'] = $v;
133 }
134 try {
135 repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
136 } catch (moodle_exception $e) {
137 die(json_encode((object)array('error' => $e->getMessage())));
c092681b 138 }
c092681b
MG
139 die(json_encode((object)array('filepath' => $newfilepath)));
140
c092681b
MG
141 case 'updatedir':
142 $filepath = required_param('filepath', PARAM_PATH);
c092681b
MG
143 $newdirname = required_param('newdirname', PARAM_FILE);
144 $parent = required_param('newfilepath', PARAM_PATH);
145 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
f7c588d2
MG
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())));
c092681b 150 }
f7c588d2 151 die(json_encode((object)array('filepath' => $parent)));
c092681b 152
64f93798
PS
153 case 'zip':
154 $filepath = required_param('filepath', PARAM_PATH);
155
156 $zipper = get_file_packer('application/zip');
157 $fs = get_file_storage();
158
2f039236 159 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.');
64f93798
PS
160
161 $parent_path = $file->get_parent_directory()->get_filepath();
638d72cd 162
697ade28
VD
163 $filepath = explode('/', trim($file->get_filepath(), '/'));
164 $filepath = array_pop($filepath);
c24cd394 165 $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
64f93798 166
2f039236 167 if ($newfile = $zipper->archive_to_storage([$filepath => $file], $usercontext->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
6bdfef5d 168 $return = new stdClass();
64f93798
PS
169 $return->filepath = $parent_path;
170 echo json_encode($return);
171 } else {
172 echo json_encode(false);
173 }
174 die;
a216d86c
P
175 case 'downloadselected':
176 $selected = required_param('selected', PARAM_RAW);
177 $selectedfiles = json_decode($selected);
31788c8c
PD
178 if (!count($selectedfiles)) {
179 $filepath = required_param('filepath', PARAM_PATH);
180 $selectedfiles = [(object)[
181 'filename' => '',
182 'filepath' => $filepath
183 ]];
184 }
a216d86c
P
185 $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
186 echo (json_encode($return));
187 die;
64f93798
PS
188
189 case 'downloaddir':
190 $filepath = required_param('filepath', PARAM_PATH);
191
a216d86c
P
192 $selectedfile = (object)[
193 'filename' => '',
194 'filepath' => $filepath
195 ];
196 $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
197 echo json_encode($return);
64f93798
PS
198 die;
199
200 case 'unzip':
201 $filename = required_param('filename', PARAM_FILE);
202 $filepath = required_param('filepath', PARAM_PATH);
203
204 $zipper = get_file_packer('application/zip');
205
206 $fs = get_file_storage();
207
2f039236 208 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
64f93798 209
77c21e6d 210 // Find unused name for directory to extract the archive.
2f039236 211 $temppath = $fs->get_unused_dirname($usercontext->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
bfa8ff63
MG
212 $donotremovedirs = array();
213 $doremovedirs = array($temppath);
77c21e6d 214 // Extract archive and move all files from $temppath to $filepath
2f039236
P
215 if ($file->extract_to_storage($zipper, $usercontext->id, 'user', 'draft', $draftid, $temppath, $USER->id) !== false) {
216 $extractedfiles = $fs->get_directory_files($usercontext->id, 'user', 'draft', $draftid, $temppath, true);
77c21e6d
MG
217 $xtemppath = preg_quote($temppath, '|');
218 foreach ($extractedfiles as $file) {
219 $realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
220 if (!$file->is_directory()) {
221 // Set the source to the extracted file to indicate that it came from archive.
222 $file->set_source(serialize((object)array('source' => $filepath)));
223 }
2f039236 224 if (!$fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
77c21e6d
MG
225 // File or directory did not exist, just move it.
226 $file->rename($realpath, $file->get_filename());
227 } else if (!$file->is_directory()) {
bfa8ff63 228 // File already existed, overwrite it
77c21e6d 229 repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
bfa8ff63
MG
230 } else {
231 // Directory already existed, remove temporary dir but make sure we don't remove the existing dir
232 $doremovedirs[] = $file->get_filepath();
233 $donotremovedirs[] = $realpath;
77c21e6d
MG
234 }
235 }
6bdfef5d 236 $return = new stdClass();
64f93798 237 $return->filepath = $filepath;
64f93798 238 } else {
77c21e6d 239 $return = false;
64f93798 240 }
77c21e6d 241 // Remove remaining temporary directories.
bfa8ff63 242 foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
2f039236 243 if ($file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.')) {
bfa8ff63
MG
244 $file->delete();
245 }
77c21e6d
MG
246 }
247 die(json_encode($return));
64f93798 248
9a62f779
MG
249 case 'getoriginal':
250 $filename = required_param('filename', PARAM_FILE);
251 $filepath = required_param('filepath', PARAM_PATH);
252
253 $fs = get_file_storage();
2f039236 254 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
9a62f779
MG
255 if (!$file) {
256 echo json_encode(false);
257 } else {
258 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
259 echo json_encode((object)$return);
260 }
261 die;
262
9dbdf31f
MG
263 case 'getreferences':
264 $filename = required_param('filename', PARAM_FILE);
265 $filepath = required_param('filepath', PARAM_PATH);
266
267 $fs = get_file_storage();
2f039236 268 $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
9dbdf31f
MG
269 if (!$file) {
270 echo json_encode(false);
271 } else {
272 $source = unserialize($file->get_source());
273 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
274 $browser = get_file_browser();
275 if (isset($source->original)) {
276 $reffiles = $fs->search_references($source->original);
277 foreach ($reffiles as $reffile) {
d197ea43 278 $refcontext = context::instance_by_id($reffile->get_contextid());
9dbdf31f
MG
279 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
280 if (empty($fileinfo)) {
281 $return['references'][] = get_string('undisclosedreference', 'repository');
282 } else {
283 $return['references'][] = $fileinfo->get_readable_fullname();
284 }
285 }
286 }
287 echo json_encode((object)$return);
288 }
289 die;
290
64f93798
PS
291 default:
292 // no/unknown action?
293 echo json_encode(false);
294 die;
295}