2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * The Web service script that is called from the filepicker front end
23 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('AJAX_SCRIPT', true);
29 require_once(__DIR__
. '/../config.php');
30 require_once(__DIR__
. '/../lib/filelib.php');
31 require_once(__DIR__
.'/lib.php');
33 $err = new stdClass();
36 $action = optional_param('action', '', PARAM_ALPHA
);
37 $repo_id = optional_param('repo_id', 0, PARAM_INT
); // Repository ID
38 $contextid = optional_param('ctx_id', SYSCONTEXTID
, PARAM_INT
); // Context ID
39 $env = optional_param('env', 'filepicker', PARAM_ALPHA
); // Opened in editor or moodleform
40 $license = optional_param('license', $CFG->sitedefaultlicense
, PARAM_TEXT
);
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
47 $accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW
);
48 $saveas_filename = optional_param('title', '', PARAM_FILE
); // save as file name
49 $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED
, PARAM_INT
); // Area max bytes.
50 $saveas_path = optional_param('savepath', '/', PARAM_PATH
); // save as file path
51 $search_text = optional_param('s', '', PARAM_CLEANHTML
);
52 $linkexternal = optional_param('linkexternal', '', PARAM_ALPHA
);
53 $usefilereference = optional_param('usefilereference', false, PARAM_BOOL
);
54 $usecontrolledlink = optional_param('usecontrolledlink', false, PARAM_BOOL
);
56 list($context, $course, $cm) = get_context_info_array($contextid);
57 require_login($course, false, $cm, false, true);
58 $PAGE->set_context($context);
60 echo $OUTPUT->header(); // send headers
62 // If uploaded file is larger than post_max_size (php.ini) setting, $_POST content will be empty.
63 if (empty($_POST) && !empty($action)) {
64 $err->error
= get_string('errorpostmaxsize', 'repository');
65 die(json_encode($err));
68 if (!confirm_sesskey()) {
69 $err->error
= get_string('invalidsesskey', 'error');
70 die(json_encode($err));
73 // Get repository instance information
76 'mimetypes' => $accepted_types
79 ajax_capture_output();
80 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
83 $repo->check_capability();
86 if (!empty($course)) {
87 $coursemaxbytes = $course->maxbytes
;
89 // Make sure maxbytes passed is within site filesize limits.
90 $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes
, $coursemaxbytes, $maxbytes);
92 // Wait as long as it takes for this script to finish
93 core_php_time_limit
::raise();
95 // These actions all occur on the currently active repository instance
100 if ($repo->check_login()) {
101 $listing = repository
::prepare_listing($repo->get_listing($req_path, $page));
102 $listing['repo_id'] = $repo_id;
103 ajax_check_captured_output();
104 echo json_encode($listing);
110 $listing = $repo->print_login();
111 $listing['repo_id'] = $repo_id;
112 ajax_check_captured_output();
113 echo json_encode($listing);
116 $logout = $repo->logout();
117 $logout['repo_id'] = $repo_id;
118 ajax_check_captured_output();
119 echo json_encode($logout);
122 $search_form['repo_id'] = $repo_id;
123 $search_form['form'] = $repo->print_search();
124 $search_form['allowcaching'] = true;
125 ajax_check_captured_output();
126 echo json_encode($search_form);
129 $search_result = repository
::prepare_listing($repo->search($search_text, (int)$page));
130 $search_result['repo_id'] = $repo_id;
131 $search_result['issearchresult'] = true;
132 ajax_check_captured_output();
133 echo json_encode($search_result);
137 $mimetypes = array();
138 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
141 foreach ($accepted_types as $type) {
142 $mimetypes[] = mimeinfo('type', $type);
144 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
145 throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $saveas_filename)));
149 // We have two special repository type need to deal with
150 // local and recent plugins don't added new files to moodle, just add new records to database
151 // so we don't check user quota and maxbytes here
152 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
153 if (!empty($allowexternallink)) {
154 $allowexternallink = true;
156 $allowexternallink = false;
158 // allow external links in url element all the time
159 $allowexternallink = ($allowexternallink ||
($env == 'url'));
161 $reference = $repo->get_file_reference($source);
163 // Use link of the files
164 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL
)) {
166 $link = $repo->get_link($reference);
168 $info['file'] = $saveas_filename;
169 $info['type'] = 'link';
170 $info['url'] = $link;
171 ajax_check_captured_output();
172 echo json_encode($info);
175 $fs = get_file_storage();
177 // Prepare file record.
178 $record = new stdClass();
179 $record->filepath
= $saveas_path;
180 $record->filename
= $saveas_filename;
181 $record->component
= 'user';
182 $record->filearea
= 'draft';
183 $record->itemid
= $itemid;
184 $record->license
= $license;
185 $record->author
= $author;
187 if ($record->filepath
!== '/') {
188 $record->filepath
= trim($record->filepath
, '/');
189 $record->filepath
= '/'.$record->filepath
.'/';
191 $usercontext = context_user
::instance($USER->id
);
193 $record->contextid
= $usercontext->id
;
194 $record->timecreated
= $now;
195 $record->timemodified
= $now;
196 $record->userid
= $USER->id
;
197 $record->sortorder
= 0;
199 // Check that user has permission to access this file
200 if (!$repo->file_is_accessible($source)) {
201 throw new file_exception('storedfilecannotread');
204 // {@link repository::build_source_field()}
205 $sourcefield = $repo->get_file_source_info($source);
206 $record->source
= $repo::build_source_field($sourcefield);
208 // If file is already a reference, set $source = file source, $repo = file repository
209 // note that in this case user may not have permission to access the source file directly
210 // so no file_browser/file_info can be used below
211 if ($repo->has_moodle_files()) {
212 $file = repository
::get_moodle_file($reference);
213 if ($file && $file->is_external_file()) {
214 $sourcefield = $file->get_source(); // remember the original source
215 $record->source
= $repo::build_source_field($sourcefield);
216 $record->contenthash
= $file->get_contenthash();
217 $record->filesize
= $file->get_filesize();
218 $reference = $file->get_reference();
219 $repo_id = $file->get_repository_id();
220 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
224 if ($usefilereference ||
$usecontrolledlink) {
225 if ($repo->has_moodle_files()) {
226 $sourcefile = repository
::get_moodle_file($reference);
227 $record->contenthash
= $sourcefile->get_contenthash();
228 $record->filesize
= $sourcefile->get_filesize();
231 // Check if file exists.
232 if (repository
::draftfile_exists($itemid, $saveas_path, $saveas_filename)) {
233 // File name being used, rename it.
234 $unused_filename = repository
::get_unused_filename($itemid, $saveas_path, $saveas_filename);
235 $record->filename
= $unused_filename;
236 // Create a file copy using unused filename.
237 $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
240 $event['event'] = 'fileexists';
241 $event['newfile'] = new stdClass
;
242 $event['newfile']->filepath
= $saveas_path;
243 $event['newfile']->filename
= $unused_filename;
244 $event['newfile']->url
= moodle_url
::make_draftfile_url($itemid, $saveas_path, $unused_filename)->out();
246 $event['existingfile'] = new stdClass
;
247 $event['existingfile']->filepath
= $saveas_path;
248 $event['existingfile']->filename
= $saveas_filename;
249 $event['existingfile']->url
= moodle_url
::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();
252 $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
254 'url'=>moodle_url
::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
255 'id'=>$storedfile->get_itemid(),
256 'file'=>$storedfile->get_filename(),
257 'icon' => $OUTPUT->image_url(file_file_icon($storedfile, 32))->out(),
260 // Repository plugin callback
261 // You can cache reository file in this callback
262 // or complete other tasks.
263 $repo->cache_file_by_reference($reference, $storedfile);
264 ajax_check_captured_output();
265 echo json_encode($event);
267 } else if ($repo->has_moodle_files()) {
268 // Some repository plugins (local, user, coursefiles, recent) are hosting moodle
269 // internal files, we cannot use get_file method, so we use copy_to_area method
271 // If the moodle file is an alias we copy this alias, otherwise we copy the file
272 // {@link repository::copy_to_area()}.
273 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes);
275 ajax_check_captured_output();
276 echo json_encode($fileinfo);
279 // Download file to moodle.
280 $downloadedfile = $repo->get_file($reference, $saveas_filename);
282 if (!empty($downloadedfile['newfilename'])) {
283 $record->filename
= $downloadedfile['newfilename'];
285 if (empty($downloadedfile['path'])) {
286 $err->error
= get_string('cannotdownload', 'repository');
287 die(json_encode($err));
290 // Check if exceed maxbytes.
291 if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) {
292 $maxbytesdisplay = display_size($maxbytes);
293 throw new file_exception('maxbytesfile', (object) array('file' => $record->filename
,
294 'size' => $maxbytesdisplay));
297 // Check if we exceed the max bytes of the area.
298 if (file_is_draft_area_limit_reached($itemid, $areamaxbytes, filesize($downloadedfile['path']))) {
299 throw new file_exception('maxareabytes');
302 $info = repository
::move_to_filepool($downloadedfile['path'], $record);
304 $info['e'] = get_string('error', 'moodle');
307 ajax_check_captured_output();
308 echo json_encode($info);
313 $result = $repo->upload($saveas_filename, $maxbytes);
314 ajax_check_captured_output();
315 echo json_encode($result);
320 $filepath = required_param('existingfilepath', PARAM_PATH
);
321 $filename = required_param('existingfilename', PARAM_FILE
);
322 // user added file which needs to replace the existing file
323 $newfilepath = required_param('newfilepath', PARAM_PATH
);
324 $newfilename = required_param('newfilename', PARAM_FILE
);
326 $info = repository
::overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename);
327 ajax_check_captured_output();
328 echo json_encode($info);
331 case 'deletetmpfile':
333 $newfilepath = required_param('newfilepath', PARAM_PATH
);
334 $newfilename = required_param('newfilename', PARAM_FILE
);
335 ajax_check_captured_output();
336 echo json_encode(repository
::delete_tempfile_from_draft($itemid, $newfilepath, $newfilename));