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(dirname(dirname(__FILE__
)).'/config.php');
30 require_once(dirname(dirname(__FILE__
)).'/lib/filelib.php');
31 require_once(dirname(__FILE__
).'/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
);
55 list($context, $course, $cm) = get_context_info_array($contextid);
56 require_login($course, false, $cm, false, true);
57 $PAGE->set_context($context);
59 echo $OUTPUT->header(); // send headers
61 // If uploaded file is larger than post_max_size (php.ini) setting, $_POST content will be empty.
62 if (empty($_POST) && !empty($action)) {
63 $err->error
= get_string('errorpostmaxsize', 'repository');
64 die(json_encode($err));
67 if (!confirm_sesskey()) {
68 $err->error
= get_string('invalidsesskey', 'error');
69 die(json_encode($err));
72 // Get repository instance information
75 'mimetypes' => $accepted_types
77 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
80 $repo->check_capability();
83 if (!empty($course)) {
84 $coursemaxbytes = $course->maxbytes
;
86 // Make sure maxbytes passed is within site filesize limits.
87 $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes
, $coursemaxbytes, $maxbytes);
89 // Wait as long as it takes for this script to finish
92 // These actions all occur on the currently active repository instance
97 if ($repo->check_login()) {
98 $listing = repository
::prepare_listing($repo->get_listing($req_path, $page));
99 $listing['repo_id'] = $repo_id;
100 echo json_encode($listing);
106 $listing = $repo->print_login();
107 $listing['repo_id'] = $repo_id;
108 echo json_encode($listing);
111 $logout = $repo->logout();
112 $logout['repo_id'] = $repo_id;
113 echo json_encode($logout);
116 $search_form['repo_id'] = $repo_id;
117 $search_form['form'] = $repo->print_search();
118 $search_form['allowcaching'] = true;
119 echo json_encode($search_form);
122 $search_result = repository
::prepare_listing($repo->search($search_text, (int)$page));
123 $search_result['repo_id'] = $repo_id;
124 $search_result['issearchresult'] = true;
125 echo json_encode($search_result);
129 $mimetypes = array();
130 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
133 foreach ($accepted_types as $type) {
134 $mimetypes[] = mimeinfo('type', $type);
136 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
137 throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $saveas_filename)));
141 // We have two special repository type need to deal with
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
144 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
145 if (!empty($allowexternallink)) {
146 $allowexternallink = true;
148 $allowexternallink = false;
150 // allow external links in url element all the time
151 $allowexternallink = ($allowexternallink ||
($env == 'url'));
153 $reference = $repo->get_file_reference($source);
155 // Use link of the files
156 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL
)) {
158 $link = $repo->get_link($reference);
160 $info['file'] = $saveas_filename;
161 $info['type'] = 'link';
162 $info['url'] = $link;
163 echo json_encode($info);
166 $fs = get_file_storage();
168 // Prepare file record.
169 $record = new stdClass();
170 $record->filepath
= $saveas_path;
171 $record->filename
= $saveas_filename;
172 $record->component
= 'user';
173 $record->filearea
= 'draft';
174 $record->itemid
= $itemid;
175 $record->license
= $license;
176 $record->author
= $author;
178 if ($record->filepath
!== '/') {
179 $record->filepath
= trim($record->filepath
, '/');
180 $record->filepath
= '/'.$record->filepath
.'/';
182 $usercontext = context_user
::instance($USER->id
);
184 $record->contextid
= $usercontext->id
;
185 $record->timecreated
= $now;
186 $record->timemodified
= $now;
187 $record->userid
= $USER->id
;
188 $record->sortorder
= 0;
190 // Check that user has permission to access this file
191 if (!$repo->file_is_accessible($source)) {
192 throw new file_exception('storedfilecannotread');
195 // {@link repository::build_source_field()}
196 $sourcefield = $repo->get_file_source_info($source);
197 $record->source
= $repo::build_source_field($sourcefield);
199 // If file is already a reference, set $source = file source, $repo = file repository
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
202 if ($repo->has_moodle_files()) {
203 $file = repository
::get_moodle_file($source);
204 if ($file && $file->is_external_file()) {
205 $sourcefield = $file->get_source(); // remember the original source
206 $record->source
= $repo::build_source_field($sourcefield);
207 $record->contenthash
= $file->get_contenthash();
208 $record->filesize
= $file->get_filesize();
209 $reference = $file->get_reference();
210 $repo_id = $file->get_repository_id();
211 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
215 if ($usefilereference) {
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();
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);
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();
236 $event['existingfile'] = new stdClass
;
237 $event['existingfile']->filepath
= $saveas_path;
238 $event['existingfile']->filename
= $saveas_filename;
239 $event['existingfile']->url
= moodle_url
::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();
242 $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
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(),
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);
254 echo json_encode($event);
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
260 // If the moodle file is an alias we copy this alias, otherwise we copy the file
261 // {@link repository::copy_to_area()}.
262 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes);
264 echo json_encode($fileinfo);
267 // Download file to moodle.
268 $downloadedfile = $repo->get_file($reference, $saveas_filename);
269 if (empty($downloadedfile['path'])) {
270 $err->error
= get_string('cannotdownload', 'repository');
271 die(json_encode($err));
274 // Check if exceed maxbytes.
275 if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) {
276 throw new file_exception('maxbytes');
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');
284 $info = repository
::move_to_filepool($downloadedfile['path'], $record);
286 $info['e'] = get_string('error', 'moodle');
289 echo json_encode($info);
294 $result = $repo->upload($saveas_filename, $maxbytes);
295 echo json_encode($result);
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
);
306 $info = repository
::overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename);
307 echo json_encode($info);
310 case 'deletetmpfile':
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));