MDL-29954 repository: remove dead code
[moodle.git] / repository / repository_ajax.php
CommitLineData
aa6c1ced 1<?php
6f2cd52a
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * The Web service script that is called from the filepicker front end
20 *
21 * @since 2.0
67233725
DC
22 * @package repository
23 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
5d354ded 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6f2cd52a 25 */
455860ce 26
e35194be
DC
27define('AJAX_SCRIPT', true);
28
14469892
DC
29require_once(dirname(dirname(__FILE__)).'/config.php');
30require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
31require_once(dirname(__FILE__).'/lib.php');
99eaca9d 32
6bdfef5d 33$err = new stdClass();
dd0566eb 34
67233725 35// Parameters
9d4ef80f 36$action = optional_param('action', '', PARAM_ALPHA);
4277133e 37$repo_id = optional_param('repo_id', 0, PARAM_INT); // Repository ID
3e123368
DC
38$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID
39$env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform
1dce6261 40$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
3e123368
DC
41$author = optional_param('author', '', PARAM_TEXT); // File author
42$source = optional_param('source', '', PARAM_RAW); // File to download
43$itemid = optional_param('itemid', 0, PARAM_INT); // Itemid
44$page = optional_param('page', '', PARAM_RAW); // Page
45$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes
46$req_path = optional_param('p', '', PARAM_RAW); // Path
18bd7573 47$accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW);
3e123368 48$saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name
68acd115 49$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area max bytes.
3e123368 50$saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path
9d4ef80f
DC
51$search_text = optional_param('s', '', PARAM_CLEANHTML);
52$linkexternal = optional_param('linkexternal', '', PARAM_ALPHA);
f8e74b20 53$usefilereference = optional_param('usefilereference', false, PARAM_BOOL);
455860ce 54
a7db0a26
DC
55list($context, $course, $cm) = get_context_info_array($contextid);
56require_login($course, false, $cm);
57$PAGE->set_context($context);
58
59echo $OUTPUT->header(); // send headers
cd455965 60
67233725 61// If uploaded file is larger than post_max_size (php.ini) setting, $_POST content will be empty.
cd455965
DC
62if (empty($_POST) && !empty($action)) {
63 $err->error = get_string('errorpostmaxsize', 'repository');
64 die(json_encode($err));
65}
66
6127179b 67if (!confirm_sesskey()) {
a5159b86 68 $err->error = get_string('invalidsesskey', 'error');
6127179b
DC
69 die(json_encode($err));
70}
71
67233725 72// Get repository instance information
6dc82385
MG
73$repooptions = array(
74 'ajax' => true,
75 'mimetypes' => $accepted_types
76);
77$repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
be85f7ab 78
67233725 79// Check permissions
6dc82385 80$repo->check_capability();
960f4379 81
7030756a
RT
82$coursemaxbytes = 0;
83if (!empty($course)) {
84 $coursemaxbytes = $course->maxbytes;
85}
960f4379
RT
86// Make sure maxbytes passed is within site filesize limits.
87$maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursemaxbytes, $maxbytes);
3e123368 88
67233725 89// Wait as long as it takes for this script to finish
9d4ef80f 90set_time_limit(0);
577aab9b 91
67233725 92// These actions all occur on the currently active repository instance
9d4ef80f
DC
93switch ($action) {
94 case 'sign':
95 case 'signin':
96 case 'list':
97 if ($repo->check_login()) {
5bdf63cc 98 $listing = repository::prepare_listing($repo->get_listing($req_path, $page));
e35194be
DC
99 $listing['repo_id'] = $repo_id;
100 echo json_encode($listing);
d0f8585d 101 break;
9d4ef80f
DC
102 } else {
103 $action = 'login';
104 }
105 case 'login':
e35194be
DC
106 $listing = $repo->print_login();
107 $listing['repo_id'] = $repo_id;
108 echo json_encode($listing);
9d4ef80f
DC
109 break;
110 case 'logout':
111 $logout = $repo->logout();
9d4ef80f
DC
112 $logout['repo_id'] = $repo_id;
113 echo json_encode($logout);
114 break;
115 case 'searchform':
b5e7b638 116 $search_form['repo_id'] = $repo_id;
e35194be 117 $search_form['form'] = $repo->print_search();
b5e7b638 118 $search_form['allowcaching'] = true;
9d4ef80f
DC
119 echo json_encode($search_form);
120 break;
121 case 'search':
5bdf63cc 122 $search_result = repository::prepare_listing($repo->search($search_text, (int)$page));
e35194be 123 $search_result['repo_id'] = $repo_id;
29199e56 124 $search_result['issearchresult'] = true;
e35194be 125 echo json_encode($search_result);
9d4ef80f
DC
126 break;
127 case 'download':
e35194be
DC
128 // validate mimetype
129 $mimetypes = array();
2385e6de 130 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
e35194be
DC
131 $mimetypes = '*';
132 } else {
133 foreach ($accepted_types as $type) {
16f61c70 134 $mimetypes[] = mimeinfo('type', $type);
e35194be
DC
135 }
136 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
559276b1 137 throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $saveas_filename)));
e35194be
DC
138 }
139 }
140
4277133e 141 // We have two special repository type need to deal with
e35194be
DC
142 // local and recent plugins don't added new files to moodle, just add new records to database
143 // so we don't check user quota and maxbytes here
93beda04
DC
144 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
145 if (!empty($allowexternallink)) {
146 $allowexternallink = true;
147 } else {
148 $allowexternallink = false;
149 }
150 // allow external links in url element all the time
151 $allowexternallink = ($allowexternallink || ($env == 'url'));
152
75dd40b2
MG
153 $reference = $repo->get_file_reference($source);
154
93beda04
DC
155 // Use link of the files
156 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
157 // use external link
75dd40b2 158 $link = $repo->get_link($reference);
e35194be 159 $info = array();
794cc7e1 160 $info['file'] = $saveas_filename;
93beda04
DC
161 $info['type'] = 'link';
162 $info['url'] = $link;
e35194be 163 echo json_encode($info);
93beda04 164 die;
e35194be 165 } else {
67233725 166 $fs = get_file_storage();
14469892 167
67233725 168 // Prepare file record.
6bdfef5d 169 $record = new stdClass();
93beda04
DC
170 $record->filepath = $saveas_path;
171 $record->filename = $saveas_filename;
172 $record->component = 'user';
173 $record->filearea = 'draft';
2dfa16ef
MG
174 $record->itemid = $itemid;
175 $record->license = $license;
176 $record->author = $author;
1dce6261 177
67233725
DC
178 if ($record->filepath !== '/') {
179 $record->filepath = trim($record->filepath, '/');
180 $record->filepath = '/'.$record->filepath.'/';
181 }
21c08c63 182 $usercontext = context_user::instance($USER->id);
67233725
DC
183 $now = time();
184 $record->contextid = $usercontext->id;
2dfa16ef 185 $record->timecreated = $now;
67233725 186 $record->timemodified = $now;
2dfa16ef 187 $record->userid = $USER->id;
adbb904b 188 $record->sortorder = 0;
67233725 189
1bd01747
MG
190 // Check that user has permission to access this file
191 if (!$repo->file_is_accessible($source)) {
192 throw new file_exception('storedfilecannotread');
193 }
194
59cb7598
MG
195 // {@link repository::build_source_field()}
196 $sourcefield = $repo->get_file_source_info($source);
197 $record->source = $repo::build_source_field($sourcefield);
198
c0e8f211 199 // If file is already a reference, set $source = file source, $repo = file repository
1bd01747
MG
200 // note that in this case user may not have permission to access the source file directly
201 // so no file_browser/file_info can be used below
c0e8f211
MG
202 if ($repo->has_moodle_files()) {
203 $file = repository::get_moodle_file($source);
204 if ($file && $file->is_external_file()) {
59cb7598
MG
205 $sourcefield = $file->get_source(); // remember the original source
206 $record->source = $repo::build_source_field($sourcefield);
437f5dc4
MG
207 $record->contenthash = $file->get_contenthash();
208 $record->filesize = $file->get_filesize();
59cb7598 209 $reference = $file->get_reference();
c0e8f211
MG
210 $repo_id = $file->get_repository_id();
211 $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions);
212 }
213 }
214
f8e74b20 215 if ($usefilereference) {
437f5dc4
MG
216 if ($repo->has_moodle_files()) {
217 $sourcefile = repository::get_moodle_file($reference);
218 $record->contenthash = $sourcefile->get_contenthash();
219 $record->filesize = $sourcefile->get_filesize();
220 }
67233725
DC
221 // Check if file exists.
222 if (repository::draftfile_exists($itemid, $saveas_path, $saveas_filename)) {
223 // File name being used, rename it.
224 $unused_filename = repository::get_unused_filename($itemid, $saveas_path, $saveas_filename);
225 $record->filename = $unused_filename;
226 // Create a file copy using unused filename.
227 $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
228
229 $event = array();
230 $event['event'] = 'fileexists';
231 $event['newfile'] = new stdClass;
232 $event['newfile']->filepath = $saveas_path;
233 $event['newfile']->filename = $unused_filename;
234 $event['newfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $unused_filename)->out();
235
236 $event['existingfile'] = new stdClass;
237 $event['existingfile']->filepath = $saveas_path;
238 $event['existingfile']->filename = $saveas_filename;
0e35ba6f 239 $event['existingfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();
9945ec5e 240 } else {
d6453211 241
9945ec5e
MG
242 $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
243 $event = array(
244 'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
245 'id'=>$storedfile->get_itemid(),
246 'file'=>$storedfile->get_filename(),
247 'icon' => $OUTPUT->pix_url(file_file_icon($storedfile, 32))->out(),
248 );
249 }
67233725
DC
250 // Repository plugin callback
251 // You can cache reository file in this callback
252 // or complete other tasks.
253 $repo->cache_file_by_reference($reference, $storedfile);
9945ec5e 254 echo json_encode($event);
67233725 255 die;
2dfa16ef
MG
256 } else if ($repo->has_moodle_files()) {
257 // Some repository plugins (local, user, coursefiles, recent) are hosting moodle
258 // internal files, we cannot use get_file method, so we use copy_to_area method
259
260 // If the moodle file is an alias we copy this alias, otherwise we copy the file
261 // {@link repository::copy_to_area()}.
68acd115 262 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes);
2dfa16ef
MG
263
264 echo json_encode($fileinfo);
265 die;
67233725
DC
266 } else {
267 // Download file to moodle.
59cb7598 268 $downloadedfile = $repo->get_file($reference, $saveas_filename);
1bd01747 269 if (empty($downloadedfile['path'])) {
67233725
DC
270 $err->error = get_string('cannotdownload', 'repository');
271 die(json_encode($err));
272 }
273
274 // Check if exceed maxbytes.
1bd01747 275 if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) {
67233725
DC
276 throw new file_exception('maxbytes');
277 }
278
39bb9b1f
FM
279 // Check if we exceed the max bytes of the area.
280 if (file_is_draft_area_limit_reached($itemid, $areamaxbytes, filesize($downloadedfile['path']))) {
281 throw new file_exception('maxareabytes');
282 }
283
67233725
DC
284 $info = repository::move_to_filepool($downloadedfile['path'], $record);
285 if (empty($info)) {
286 $info['e'] = get_string('error', 'moodle');
287 }
9d4ef80f 288 }
93beda04
DC
289 echo json_encode($info);
290 die;
9d4ef80f
DC
291 }
292 break;
293 case 'upload':
23bfe0a4
PS
294 $result = $repo->upload($saveas_filename, $maxbytes);
295 echo json_encode($result);
9d4ef80f 296 break;
d0f8585d 297
f392caba
DC
298 case 'overwrite':
299 // existing file
300 $filepath = required_param('existingfilepath', PARAM_PATH);
301 $filename = required_param('existingfilename', PARAM_FILE);
302 // user added file which needs to replace the existing file
303 $newfilepath = required_param('newfilepath', PARAM_PATH);
304 $newfilename = required_param('newfilename', PARAM_FILE);
305
794cc7e1
JP
306 $info = repository::overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename);
307 echo json_encode($info);
f392caba
DC
308 break;
309
310 case 'deletetmpfile':
311 // delete tmp file
312 $newfilepath = required_param('newfilepath', PARAM_PATH);
313 $newfilename = required_param('newfilename', PARAM_FILE);
314 echo json_encode(repository::delete_tempfile_from_draft($itemid, $newfilepath, $newfilename));
315
316 break;
0eb58cf4 317}