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/>.
23 * @copyright 2010 Dongsheng Cai
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
28 require_once("$CFG->libdir/filelib.php");
31 * Files external functions
35 * @copyright 2011 Jerome Mouneyrac
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_files_external
extends external_api
{
42 * Returns description of get_files parameters
44 * @return external_function_parameters
47 public static function get_files_parameters() {
48 return new external_function_parameters(
50 'contextid' => new external_value(PARAM_INT
, 'context id Set to -1 to use contextlevel and instanceid.'),
51 'component' => new external_value(PARAM_TEXT
, 'component'),
52 'filearea' => new external_value(PARAM_TEXT
, 'file area'),
53 'itemid' => new external_value(PARAM_INT
, 'associated id'),
54 'filepath' => new external_value(PARAM_PATH
, 'file path'),
55 'filename' => new external_value(PARAM_TEXT
, 'file name'),
56 'modified' => new external_value(PARAM_INT
, 'timestamp to return files changed after this time.', VALUE_DEFAULT
, null),
57 'contextlevel' => new external_value(PARAM_ALPHA
, 'The context level for the file location.', VALUE_DEFAULT
, null),
58 'instanceid' => new external_value(PARAM_INT
, 'The instance id for where the file is located.', VALUE_DEFAULT
, null)
65 * Return moodle files listing
67 * @param int $contextid context id
68 * @param int $component component
69 * @param int $filearea file area
70 * @param int $itemid item id
71 * @param string $filepath file path
72 * @param string $filename file name
73 * @param int $modified timestamp to return files changed after this time.
74 * @param string $contextlevel The context level for the file location.
75 * @param int $instanceid The instance id for where the file is located.
79 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null,
80 $contextlevel = null, $instanceid = null) {
83 'contextid' => $contextid,
84 'component' => $component,
85 'filearea' => $filearea,
87 'filepath' => $filepath,
88 'filename' => $filename,
89 'modified' => $modified,
90 'contextlevel' => $contextlevel,
91 'instanceid' => $instanceid);
92 $fileinfo = self
::validate_parameters(self
::get_files_parameters(), $parameters);
94 $browser = get_file_browser();
96 // We need to preserve backwards compatibility. Zero will use the system context and minus one will
97 // use the addtional parameters to determine the context.
98 // TODO MDL-40489 get_context_from_params should handle this logic.
99 if ($fileinfo['contextid'] == 0) {
100 $context = context_system
::instance();
102 if ($fileinfo['contextid'] == -1) {
103 $fileinfo['contextid'] = null;
105 $context = self
::get_context_from_params($fileinfo);
107 self
::validate_context($context);
109 if (empty($fileinfo['component'])) {
110 $fileinfo['component'] = null;
112 if (empty($fileinfo['filearea'])) {
113 $fileinfo['filearea'] = null;
115 if (empty($fileinfo['filename'])) {
116 $fileinfo['filename'] = null;
118 if (empty($fileinfo['filepath'])) {
119 $fileinfo['filepath'] = null;
123 $return['parents'] = array();
124 $return['files'] = array();
127 if ($file = $browser->get_file_info(
128 $context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'],
129 $fileinfo['filepath'], $fileinfo['filename'])) {
130 $level = $file->get_parent();
132 $params = $level->get_params();
133 $params['filename'] = $level->get_visible_name();
134 array_unshift($return['parents'], $params);
135 $level = $level->get_parent();
137 $children = $file->get_children();
138 foreach ($children as $child) {
140 $params = $child->get_params();
141 $timemodified = $child->get_timemodified();
143 if ($child->is_directory()) {
144 if ((is_null($modified)) or ($modified < $timemodified)) {
146 'contextid' => $params['contextid'],
147 'component' => $params['component'],
148 'filearea' => $params['filearea'],
149 'itemid' => $params['itemid'],
150 'filepath' => $params['filepath'],
151 'filename' => $child->get_visible_name(),
154 'timemodified' => $timemodified
159 if ((is_null($modified)) or ($modified < $timemodified)) {
161 'contextid' => $params['contextid'],
162 'component' => $params['component'],
163 'filearea' => $params['filearea'],
164 'itemid' => $params['itemid'],
165 'filepath' => $params['filepath'],
166 'filename' => $child->get_visible_name(),
167 'url' => $child->get_url(),
169 'timemodified' => $timemodified
176 $return['files'] = $list;
181 * Returns description of get_files returns
183 * @return external_single_structure
186 public static function get_files_returns() {
187 return new external_single_structure(
189 'parents' => new external_multiple_structure(
190 new external_single_structure(
192 'contextid' => new external_value(PARAM_INT
, ''),
193 'component' => new external_value(PARAM_COMPONENT
, ''),
194 'filearea' => new external_value(PARAM_AREA
, ''),
195 'itemid' => new external_value(PARAM_INT
, ''),
196 'filepath' => new external_value(PARAM_TEXT
, ''),
197 'filename' => new external_value(PARAM_TEXT
, ''),
201 'files' => new external_multiple_structure(
202 new external_single_structure(
204 'contextid' => new external_value(PARAM_INT
, ''),
205 'component' => new external_value(PARAM_COMPONENT
, ''),
206 'filearea' => new external_value(PARAM_AREA
, ''),
207 'itemid' => new external_value(PARAM_INT
, ''),
208 'filepath' => new external_value(PARAM_TEXT
, ''),
209 'filename' => new external_value(PARAM_TEXT
, ''),
210 'isdir' => new external_value(PARAM_BOOL
, ''),
211 'url' => new external_value(PARAM_TEXT
, ''),
212 'timemodified' => new external_value(PARAM_INT
, ''),
221 * Returns description of upload parameters
223 * @return external_function_parameters
226 public static function upload_parameters() {
227 return new external_function_parameters(
229 'contextid' => new external_value(PARAM_INT
, 'context id', VALUE_DEFAULT
, null),
230 'component' => new external_value(PARAM_COMPONENT
, 'component'),
231 'filearea' => new external_value(PARAM_AREA
, 'file area'),
232 'itemid' => new external_value(PARAM_INT
, 'associated id'),
233 'filepath' => new external_value(PARAM_PATH
, 'file path'),
234 'filename' => new external_value(PARAM_FILE
, 'file name'),
235 'filecontent' => new external_value(PARAM_TEXT
, 'file content'),
236 'contextlevel' => new external_value(PARAM_ALPHA
, 'The context level to put the file in,
237 (block, course, coursecat, system, user, module)', VALUE_DEFAULT
, null),
238 'instanceid' => new external_value(PARAM_INT
, 'The Instance id of item associated
239 with the context level', VALUE_DEFAULT
, null)
245 * Uploading a file to moodle
247 * @param int $contextid context id
248 * @param string $component component
249 * @param string $filearea file area
250 * @param int $itemid item id
251 * @param string $filepath file path
252 * @param string $filename file name
253 * @param string $filecontent file content
254 * @param string $contextlevel Context level (block, course, coursecat, system, user or module)
255 * @param int $instanceid Instance id of the item associated with the context level
259 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid) {
262 $fileinfo = self
::validate_parameters(self
::upload_parameters(), array(
263 'contextid' => $contextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid,
264 'filepath' => $filepath, 'filename' => $filename, 'filecontent' => $filecontent, 'contextlevel' => $contextlevel,
265 'instanceid' => $instanceid));
267 if (!isset($fileinfo['filecontent'])) {
268 throw new moodle_exception('nofile');
271 $dir = make_temp_directory('wsupload');
273 if (empty($fileinfo['filename'])) {
274 $filename = uniqid('wsupload', true).'_'.time().'.tmp';
276 $filename = $fileinfo['filename'];
279 if (file_exists($dir.$filename)) {
280 $savedfilepath = $dir.uniqid('m').$filename;
282 $savedfilepath = $dir.$filename;
285 file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent']));
286 @chmod
($savedfilepath, $CFG->filepermissions
);
287 unset($fileinfo['filecontent']);
289 if (!empty($fileinfo['filepath'])) {
290 $filepath = $fileinfo['filepath'];
295 // Only allow uploads to draft or private areas (private is deprecated but still supported)
296 if (!($fileinfo['component'] == 'user' and in_array($fileinfo['filearea'], array('private', 'draft')))) {
297 throw new coding_exception('File can be uploaded to user private or draft areas only');
300 $filearea = $fileinfo['filearea'];
304 if (isset($fileinfo['itemid'])) {
305 $itemid = $fileinfo['itemid'];
307 if ($filearea == 'draft' && $itemid <= 0) {
308 // Generate a draft area for the files.
309 $itemid = file_get_unused_draft_itemid();
310 } else if ($filearea == 'private') {
311 // TODO MDL-31116 in user private area, itemid is always 0.
315 // We need to preserve backword compatibility. Context id is no more a required.
316 if (empty($fileinfo['contextid'])) {
317 unset($fileinfo['contextid']);
320 // Get and validate context.
321 $context = self
::get_context_from_params($fileinfo);
322 self
::validate_context($context);
323 if (($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'private')) {
324 debugging('Uploading directly to user private files area is deprecated. Upload to a draft area and then move the files with core_user::add_user_private_files');
327 $browser = get_file_browser();
329 // Check existing file.
330 if ($file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
331 throw new moodle_exception('fileexist');
334 // Move file to filepool.
335 if ($dir = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, '.')) {
336 $info = $dir->create_file_from_pathname($filename, $savedfilepath);
337 $params = $info->get_params();
338 unlink($savedfilepath);
340 'contextid'=>$params['contextid'],
341 'component'=>$params['component'],
342 'filearea'=>$params['filearea'],
343 'itemid'=>$params['itemid'],
344 'filepath'=>$params['filepath'],
345 'filename'=>$params['filename'],
346 'url'=>$info->get_url()
349 throw new moodle_exception('nofile');
354 * Returns description of upload returns
356 * @return external_single_structure
359 public static function upload_returns() {
360 return new external_single_structure(
362 'contextid' => new external_value(PARAM_INT
, ''),
363 'component' => new external_value(PARAM_COMPONENT
, ''),
364 'filearea' => new external_value(PARAM_AREA
, ''),
365 'itemid' => new external_value(PARAM_INT
, ''),
366 'filepath' => new external_value(PARAM_TEXT
, ''),
367 'filename' => new external_value(PARAM_FILE
, ''),
368 'url' => new external_value(PARAM_TEXT
, ''),
375 * Deprecated files external functions
377 * @package core_files
378 * @copyright 2010 Dongsheng Cai
379 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
381 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
382 * @see core_files_external
384 class moodle_file_external
extends external_api
{
387 * Returns description of get_files parameters
389 * @return external_function_parameters
391 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
392 * @see core_files_external::get_files_parameters()
394 public static function get_files_parameters() {
395 return core_files_external
::get_files_parameters();
399 * Return moodle files listing
401 * @param int $contextid
402 * @param int $component
403 * @param int $filearea
405 * @param string $filepath
406 * @param string $filename
409 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
410 * @see core_files_external::get_files()
412 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename) {
413 return core_files_external
::get_files($contextid, $component, $filearea, $itemid, $filepath, $filename);
417 * Returns description of get_files returns
419 * @return external_single_structure
421 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
422 * @see core_files_external::get_files_returns()
424 public static function get_files_returns() {
425 return core_files_external
::get_files_returns();
429 * Returns description of upload parameters
431 * @return external_function_parameters
433 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
434 * @see core_files_external::upload_parameters()
436 public static function upload_parameters() {
437 return core_files_external
::upload_parameters();
441 * Uploading a file to moodle
443 * @param int $contextid
444 * @param string $component
445 * @param string $filearea
447 * @param string $filepath
448 * @param string $filename
449 * @param string $filecontent
452 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
453 * @see core_files_external::upload()
455 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent) {
456 return core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
460 * Returns description of upload returns
462 * @return external_single_structure
464 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
465 * @see core_files_external::upload_returns()
467 public static function upload_returns() {
468 return core_files_external
::upload_returns();