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'),
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_FILE
, 'file name'),
56 'modified' => new external_value(PARAM_INT
, 'timestamp to return files changed after this time.', VALUE_DEFAULT
, null)
62 * Return moodle files listing
64 * @param int $contextid context id
65 * @param int $component component
66 * @param int $filearea file area
67 * @param int $itemid item id
68 * @param string $filepath file path
69 * @param string $filename file name
70 * @param int $modified timestamp to return files changed after this time.
74 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null) {
75 global $CFG, $USER, $OUTPUT;
76 $fileinfo = self
::validate_parameters(self
::get_files_parameters(), array(
77 'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea,
78 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'modified'=>$modified));
80 $browser = get_file_browser();
82 if (empty($fileinfo['contextid'])) {
83 $context = context_system
::instance();
85 $context = context
::instance_by_id($fileinfo['contextid']);
87 if (empty($fileinfo['component'])) {
88 $fileinfo['component'] = null;
90 if (empty($fileinfo['filearea'])) {
91 $fileinfo['filearea'] = null;
93 if (empty($fileinfo['itemid'])) {
94 $fileinfo['itemid'] = null;
96 if (empty($fileinfo['filename'])) {
97 $fileinfo['filename'] = null;
99 if (empty($fileinfo['filepath'])) {
100 $fileinfo['filepath'] = null;
104 $return['parents'] = array();
105 $return['files'] = array();
107 if ($file = $browser->get_file_info(
108 $context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'],
109 $fileinfo['filepath'], $fileinfo['filename'])) {
110 $level = $file->get_parent();
112 $params = $level->get_params();
113 $params['filename'] = $level->get_visible_name();
114 array_unshift($return['parents'], $params);
115 $level = $level->get_parent();
117 $children = $file->get_children();
118 foreach ($children as $child) {
120 $params = $child->get_params();
121 $timemodified = $child->get_timemodified();
123 if ($child->is_directory()) {
124 if ((is_null($modified)) or ($modified < $timemodified)) {
126 'contextid' => $params['contextid'],
127 'component' => $params['component'],
128 'filearea' => $params['filearea'],
129 'itemid' => $params['itemid'],
130 'filepath' => $params['filepath'],
131 'filename' => $child->get_visible_name(),
134 'timemodified' => $timemodified
139 if ((is_null($modified)) or ($modified < $timemodified)) {
141 'contextid' => $params['contextid'],
142 'component' => $params['component'],
143 'filearea' => $params['filearea'],
144 'itemid' => $params['itemid'],
145 'filepath' => $params['filepath'],
146 'filename' => $child->get_visible_name(),
147 'url' => $child->get_url(),
149 'timemodified' => $timemodified
156 $return['files'] = $list;
161 * Returns description of get_files returns
163 * @return external_single_structure
166 public static function get_files_returns() {
167 return new external_single_structure(
169 'parents' => new external_multiple_structure(
170 new external_single_structure(
172 'contextid' => new external_value(PARAM_INT
, ''),
173 'component' => new external_value(PARAM_COMPONENT
, ''),
174 'filearea' => new external_value(PARAM_AREA
, ''),
175 'itemid' => new external_value(PARAM_INT
, ''),
176 'filepath' => new external_value(PARAM_TEXT
, ''),
177 'filename' => new external_value(PARAM_TEXT
, ''),
181 'files' => new external_multiple_structure(
182 new external_single_structure(
184 'contextid' => new external_value(PARAM_INT
, ''),
185 'component' => new external_value(PARAM_COMPONENT
, ''),
186 'filearea' => new external_value(PARAM_AREA
, ''),
187 'itemid' => new external_value(PARAM_INT
, ''),
188 'filepath' => new external_value(PARAM_TEXT
, ''),
189 'filename' => new external_value(PARAM_FILE
, ''),
190 'isdir' => new external_value(PARAM_BOOL
, ''),
191 'url' => new external_value(PARAM_TEXT
, ''),
192 'timemodified' => new external_value(PARAM_INT
, ''),
201 * Returns description of upload parameters
203 * @return external_function_parameters
206 public static function upload_parameters() {
207 return new external_function_parameters(
209 'contextid' => new external_value(PARAM_INT
, 'context id', VALUE_DEFAULT
, null),
210 'component' => new external_value(PARAM_COMPONENT
, 'component'),
211 'filearea' => new external_value(PARAM_AREA
, 'file area'),
212 'itemid' => new external_value(PARAM_INT
, 'associated id'),
213 'filepath' => new external_value(PARAM_PATH
, 'file path'),
214 'filename' => new external_value(PARAM_FILE
, 'file name'),
215 'filecontent' => new external_value(PARAM_TEXT
, 'file content'),
216 'contextlevel' => new external_value(PARAM_ALPHA
, 'The context level to put the file in,
217 (block, course, coursecat, system, user, module)', VALUE_DEFAULT
, null),
218 'instanceid' => new external_value(PARAM_INT
, 'The Instance id of item associated
219 with the context level', VALUE_DEFAULT
, null)
225 * Uploading a file to moodle
227 * @param int $contextid context id
228 * @param string $component component
229 * @param string $filearea file area
230 * @param int $itemid item id
231 * @param string $filepath file path
232 * @param string $filename file name
233 * @param string $filecontent file content
234 * @param string $contextlevel Context level (block, course, coursecat, system, user or module)
235 * @param int $instanceid Instance id of the item associated with the context level
239 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent, $contextlevel, $instanceid) {
242 $fileinfo = self
::validate_parameters(self
::upload_parameters(), array(
243 'contextid' => $contextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid,
244 'filepath' => $filepath, 'filename' => $filename, 'filecontent' => $filecontent, 'contextlevel' => $contextlevel,
245 'instanceid' => $instanceid));
247 if (!isset($fileinfo['filecontent'])) {
248 throw new moodle_exception('nofile');
251 $dir = make_temp_directory('wsupload');
253 if (empty($fileinfo['filename'])) {
254 $filename = uniqid('wsupload', true).'_'.time().'.tmp';
256 $filename = $fileinfo['filename'];
259 if (file_exists($dir.$filename)) {
260 $savedfilepath = $dir.uniqid('m').$filename;
262 $savedfilepath = $dir.$filename;
265 file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent']));
266 unset($fileinfo['filecontent']);
268 if (!empty($fileinfo['filepath'])) {
269 $filepath = $fileinfo['filepath'];
274 if (isset($fileinfo['itemid'])) {
275 // TODO MDL-31116 in user private area, itemid is always 0.
278 throw new coding_exception('itemid cannot be empty');
281 // We need to preserve backword compatibility. Context id is no more a required.
282 if (empty($fileinfo['contextid'])) {
283 unset($fileinfo['contextid']);
286 // Get and validate context.
287 $context = self
::get_context_from_params($fileinfo);
288 self
::validate_context($context);
290 if (!($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'private')) {
291 throw new coding_exception('File can be uploaded to user private area only');
293 // TODO MDL-31116 hard-coded to use user_private area.
295 $filearea = 'private';
298 $browser = get_file_browser();
300 // Check existing file.
301 if ($file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
302 throw new moodle_exception('fileexist');
305 // Move file to filepool.
306 if ($dir = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, '.')) {
307 $info = $dir->create_file_from_pathname($filename, $savedfilepath);
308 $params = $info->get_params();
309 unlink($savedfilepath);
311 'contextid'=>$params['contextid'],
312 'component'=>$params['component'],
313 'filearea'=>$params['filearea'],
314 'itemid'=>$params['itemid'],
315 'filepath'=>$params['filepath'],
316 'filename'=>$params['filename'],
317 'url'=>$info->get_url()
320 throw new moodle_exception('nofile');
325 * Returns description of upload returns
327 * @return external_single_structure
330 public static function upload_returns() {
331 return new external_single_structure(
333 'contextid' => new external_value(PARAM_INT
, ''),
334 'component' => new external_value(PARAM_COMPONENT
, ''),
335 'filearea' => new external_value(PARAM_AREA
, ''),
336 'itemid' => new external_value(PARAM_INT
, ''),
337 'filepath' => new external_value(PARAM_TEXT
, ''),
338 'filename' => new external_value(PARAM_FILE
, ''),
339 'url' => new external_value(PARAM_TEXT
, ''),
346 * Deprecated files external functions
348 * @package core_files
349 * @copyright 2010 Dongsheng Cai
350 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
352 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
353 * @see core_files_external
355 class moodle_file_external
extends external_api
{
358 * Returns description of get_files parameters
360 * @return external_function_parameters
362 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
363 * @see core_files_external::get_files_parameters()
365 public static function get_files_parameters() {
366 return core_files_external
::get_files_parameters();
370 * Return moodle files listing
372 * @param int $contextid
373 * @param int $component
374 * @param int $filearea
376 * @param string $filepath
377 * @param string $filename
380 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
381 * @see core_files_external::get_files()
383 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename) {
384 return core_files_external
::get_files($contextid, $component, $filearea, $itemid, $filepath, $filename);
388 * Returns description of get_files returns
390 * @return external_single_structure
392 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
393 * @see core_files_external::get_files_returns()
395 public static function get_files_returns() {
396 return core_files_external
::get_files_returns();
400 * Returns description of upload parameters
402 * @return external_function_parameters
404 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
405 * @see core_files_external::upload_parameters()
407 public static function upload_parameters() {
408 return core_files_external
::upload_parameters();
412 * Uploading a file to moodle
414 * @param int $contextid
415 * @param string $component
416 * @param string $filearea
418 * @param string $filepath
419 * @param string $filename
420 * @param string $filecontent
423 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
424 * @see core_files_external::upload()
426 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent) {
427 return core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
431 * Returns description of upload returns
433 * @return external_single_structure
435 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
436 * @see core_files_external::upload_returns()
438 public static function upload_returns() {
439 return core_files_external
::upload_returns();