2009-09-14 23:52:08 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-01-18 10:52:25 +08:00
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
/**
|
|
|
|
* Support for external API
|
|
|
|
*
|
2012-01-18 10:52:25 +08:00
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
2009-09-14 23:52:08 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2010-07-25 13:35:05 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2009-10-26 19:10:20 +00:00
|
|
|
|
2009-10-20 22:30:18 +00:00
|
|
|
/**
|
2010-01-13 10:10:27 +00:00
|
|
|
* Returns detailed function information
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-20 22:30:18 +00:00
|
|
|
* @param string|object $function name of external function or record from external_function
|
|
|
|
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
|
|
|
|
* MUST_EXIST means throw exception if no record or multiple records found
|
2012-01-18 10:52:25 +08:00
|
|
|
* @return stdClass description or false if not found or exception thrown
|
|
|
|
* @since Moodle 2.0
|
2009-10-20 22:30:18 +00:00
|
|
|
*/
|
|
|
|
function external_function_info($function, $strictness=MUST_EXIST) {
|
|
|
|
global $DB, $CFG;
|
|
|
|
|
|
|
|
if (!is_object($function)) {
|
|
|
|
if (!$function = $DB->get_record('external_functions', array('name'=>$function), '*', $strictness)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 10:53:39 +08:00
|
|
|
// First try class autoloading.
|
|
|
|
if (!class_exists($function->classname)) {
|
|
|
|
// Fallback to explicit include of externallib.php.
|
|
|
|
$function->classpath = empty($function->classpath) ? core_component::get_component_directory($function->component).'/externallib.php' : $CFG->dirroot.'/'.$function->classpath;
|
|
|
|
if (!file_exists($function->classpath)) {
|
2015-10-14 12:03:38 +08:00
|
|
|
throw new coding_exception('Cannot find file with external function implementation: ' . $function->classname);
|
2014-03-15 10:53:39 +08:00
|
|
|
}
|
|
|
|
require_once($function->classpath);
|
|
|
|
if (!class_exists($function->classname)) {
|
|
|
|
throw new coding_exception('Cannot find external class');
|
|
|
|
}
|
2009-10-20 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-22 14:56:50 +08:00
|
|
|
$function->ajax_method = $function->methodname.'_is_allowed_from_ajax';
|
2009-10-20 22:30:18 +00:00
|
|
|
$function->parameters_method = $function->methodname.'_parameters';
|
|
|
|
$function->returns_method = $function->methodname.'_returns';
|
2015-01-23 10:24:13 +08:00
|
|
|
$function->deprecated_method = $function->methodname.'_is_deprecated';
|
2009-10-20 22:30:18 +00:00
|
|
|
|
|
|
|
// make sure the implementaion class is ok
|
|
|
|
if (!method_exists($function->classname, $function->methodname)) {
|
2010-04-22 01:51:24 +00:00
|
|
|
throw new coding_exception('Missing implementation method of '.$function->classname.'::'.$function->methodname);
|
2009-10-20 22:30:18 +00:00
|
|
|
}
|
|
|
|
if (!method_exists($function->classname, $function->parameters_method)) {
|
|
|
|
throw new coding_exception('Missing parameters description');
|
|
|
|
}
|
|
|
|
if (!method_exists($function->classname, $function->returns_method)) {
|
|
|
|
throw new coding_exception('Missing returned values description');
|
|
|
|
}
|
2015-01-23 10:24:13 +08:00
|
|
|
if (method_exists($function->classname, $function->deprecated_method)) {
|
|
|
|
if (call_user_func(array($function->classname, $function->deprecated_method)) === true) {
|
|
|
|
$function->deprecated = true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-22 14:56:50 +08:00
|
|
|
$function->allowed_from_ajax = false;
|
2009-10-20 22:30:18 +00:00
|
|
|
|
|
|
|
// fetch the parameters description
|
|
|
|
$function->parameters_desc = call_user_func(array($function->classname, $function->parameters_method));
|
|
|
|
if (!($function->parameters_desc instanceof external_function_parameters)) {
|
|
|
|
throw new coding_exception('Invalid parameters description');
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the return values description
|
|
|
|
$function->returns_desc = call_user_func(array($function->classname, $function->returns_method));
|
|
|
|
// null means void result or result is ignored
|
|
|
|
if (!is_null($function->returns_desc) and !($function->returns_desc instanceof external_description)) {
|
|
|
|
throw new coding_exception('Invalid return description');
|
|
|
|
}
|
|
|
|
|
|
|
|
//now get the function description
|
2012-01-18 10:52:25 +08:00
|
|
|
//TODO MDL-31115 use localised lang pack descriptions, it would be nice to have
|
2010-05-22 19:06:34 +00:00
|
|
|
// easy to understand descriptions in admin UI,
|
2009-10-20 22:30:18 +00:00
|
|
|
// on the other hand this is still a bit in a flux and we need to find some new naming
|
|
|
|
// conventions for these descriptions in lang packs
|
|
|
|
$function->description = null;
|
2013-07-16 22:42:37 +02:00
|
|
|
$servicesfile = core_component::get_component_directory($function->component).'/db/services.php';
|
2009-10-20 22:30:18 +00:00
|
|
|
if (file_exists($servicesfile)) {
|
|
|
|
$functions = null;
|
|
|
|
include($servicesfile);
|
|
|
|
if (isset($functions[$function->name]['description'])) {
|
|
|
|
$function->description = $functions[$function->name]['description'];
|
|
|
|
}
|
2011-07-06 15:42:48 +01:00
|
|
|
if (isset($functions[$function->name]['testclientpath'])) {
|
|
|
|
$function->testclientpath = $functions[$function->name]['testclientpath'];
|
|
|
|
}
|
2015-07-13 12:21:33 +08:00
|
|
|
if (isset($functions[$function->name]['type'])) {
|
|
|
|
$function->type = $functions[$function->name]['type'];
|
|
|
|
}
|
2015-07-16 14:14:33 +08:00
|
|
|
if (isset($functions[$function->name]['ajax'])) {
|
|
|
|
$function->allowed_from_ajax = $functions[$function->name]['ajax'];
|
2015-09-14 15:09:38 +08:00
|
|
|
} else if (method_exists($function->classname, $function->ajax_method)) {
|
|
|
|
if (call_user_func(array($function->classname, $function->ajax_method)) === true) {
|
|
|
|
debugging('External function ' . $function->ajax_method . '() function is deprecated.' .
|
|
|
|
'Set ajax=>true in db/service.php instead.', DEBUG_DEVELOPER);
|
|
|
|
$function->allowed_from_ajax = true;
|
|
|
|
}
|
2015-07-16 14:14:33 +08:00
|
|
|
}
|
2015-07-13 12:21:33 +08:00
|
|
|
if (isset($functions[$function->name]['loginrequired'])) {
|
|
|
|
$function->loginrequired = $functions[$function->name]['loginrequired'];
|
|
|
|
} else {
|
|
|
|
$function->loginrequired = true;
|
|
|
|
}
|
2009-10-20 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $function;
|
|
|
|
}
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
/**
|
2012-01-18 10:52:25 +08:00
|
|
|
* Exception indicating user is not allowed to use external function in the current context.
|
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-09-14 23:52:08 +00:00
|
|
|
*/
|
|
|
|
class restricted_context_exception extends moodle_exception {
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @since Moodle 2.0
|
2009-09-14 23:52:08 +00:00
|
|
|
*/
|
|
|
|
function __construct() {
|
|
|
|
parent::__construct('restrictedcontextexception', 'error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for external api methods.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-09-14 23:52:08 +00:00
|
|
|
*/
|
|
|
|
class external_api {
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var stdClass context where the function calls will be restricted */
|
2009-09-14 23:52:08 +00:00
|
|
|
private static $contextrestriction;
|
|
|
|
|
2009-09-16 18:06:31 +00:00
|
|
|
/**
|
2010-05-22 19:06:34 +00:00
|
|
|
* Set context restriction for all following subsequent function calls.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @param stdClass $context the context restriction
|
|
|
|
* @since Moodle 2.0
|
2009-09-16 18:06:31 +00:00
|
|
|
*/
|
2009-10-07 21:44:54 +00:00
|
|
|
public static function set_context_restriction($context) {
|
2009-09-14 23:52:08 +00:00
|
|
|
self::$contextrestriction = $context;
|
|
|
|
}
|
|
|
|
|
2009-10-07 21:44:54 +00:00
|
|
|
/**
|
|
|
|
* This method has to be called before every operation
|
|
|
|
* that takes a longer time to finish!
|
|
|
|
*
|
|
|
|
* @param int $seconds max expected time the next operation needs
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-10-07 21:44:54 +00:00
|
|
|
*/
|
|
|
|
public static function set_timeout($seconds=360) {
|
|
|
|
$seconds = ($seconds < 300) ? 300 : $seconds;
|
2013-10-15 13:22:19 +01:00
|
|
|
core_php_time_limit::raise($seconds);
|
2009-10-07 21:44:54 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 18:06:31 +00:00
|
|
|
/**
|
2009-10-02 12:13:56 +00:00
|
|
|
* Validates submitted function parameters, if anything is incorrect
|
2009-09-16 18:06:31 +00:00
|
|
|
* invalid_parameter_exception is thrown.
|
2009-10-06 19:49:07 +00:00
|
|
|
* This is a simple recursive method which is intended to be called from
|
|
|
|
* each implementation method of external API.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-02 12:13:56 +00:00
|
|
|
* @param external_description $description description of parameters
|
|
|
|
* @param mixed $params the actual parameters
|
|
|
|
* @return mixed params with added defaults for optional items, invalid_parameters_exception thrown if any problem found
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-09-16 18:06:31 +00:00
|
|
|
*/
|
2009-10-02 12:13:56 +00:00
|
|
|
public static function validate_parameters(external_description $description, $params) {
|
2009-10-07 21:49:03 +00:00
|
|
|
if ($description instanceof external_value) {
|
2009-10-02 12:13:56 +00:00
|
|
|
if (is_array($params) or is_object($params)) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_parameter_exception('Scalar type expected, array or object received.');
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
2009-10-26 22:39:08 +00:00
|
|
|
|
|
|
|
if ($description->type == PARAM_BOOL) {
|
|
|
|
// special case for PARAM_BOOL - we want true/false instead of the usual 1/0 - we can not be too strict here ;-)
|
|
|
|
if (is_bool($params) or $params === 0 or $params === 1 or $params === '0' or $params === '1') {
|
|
|
|
return (bool)$params;
|
|
|
|
}
|
|
|
|
}
|
2011-10-07 16:06:19 +08:00
|
|
|
$debuginfo = 'Invalid external api parameter: the value is "' . $params .
|
|
|
|
'", the server was expecting "' . $description->type . '" type';
|
|
|
|
return validate_param($params, $description->type, $description->allownull, $debuginfo);
|
2010-03-31 07:41:31 +00:00
|
|
|
|
2009-10-02 12:13:56 +00:00
|
|
|
} else if ($description instanceof external_single_structure) {
|
|
|
|
if (!is_array($params)) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_parameter_exception('Only arrays accepted. The bad value is: \''
|
|
|
|
. print_r($params, true) . '\'');
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach ($description->keys as $key=>$subdesc) {
|
|
|
|
if (!array_key_exists($key, $params)) {
|
2010-02-05 02:58:24 +00:00
|
|
|
if ($subdesc->required == VALUE_REQUIRED) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_parameter_exception('Missing required key in single structure: '. $key);
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
2010-07-07 08:15:26 +00:00
|
|
|
if ($subdesc->required == VALUE_DEFAULT) {
|
|
|
|
try {
|
|
|
|
$result[$key] = self::validate_parameters($subdesc, $subdesc->default);
|
|
|
|
} catch (invalid_parameter_exception $e) {
|
2011-10-07 16:06:19 +08:00
|
|
|
//we are only interested by exceptions returned by validate_param() and validate_parameters()
|
|
|
|
//(in order to build the path to the faulty attribut)
|
|
|
|
throw new invalid_parameter_exception($key." => ".$e->getMessage() . ': ' .$e->debuginfo);
|
2010-02-05 02:58:24 +00:00
|
|
|
}
|
2010-07-07 08:15:26 +00:00
|
|
|
}
|
2009-10-02 12:13:56 +00:00
|
|
|
} else {
|
2010-02-05 04:10:36 +00:00
|
|
|
try {
|
|
|
|
$result[$key] = self::validate_parameters($subdesc, $params[$key]);
|
|
|
|
} catch (invalid_parameter_exception $e) {
|
2011-10-07 16:06:19 +08:00
|
|
|
//we are only interested by exceptions returned by validate_param() and validate_parameters()
|
|
|
|
//(in order to build the path to the faulty attribut)
|
|
|
|
throw new invalid_parameter_exception($key." => ".$e->getMessage() . ': ' .$e->debuginfo);
|
2010-02-05 04:10:36 +00:00
|
|
|
}
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
|
|
|
unset($params[$key]);
|
|
|
|
}
|
|
|
|
if (!empty($params)) {
|
2011-10-17 13:18:30 +13:00
|
|
|
throw new invalid_parameter_exception('Unexpected keys (' . implode(', ', array_keys($params)) . ') detected in parameter array.');
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2009-09-16 18:06:31 +00:00
|
|
|
|
2009-10-02 12:13:56 +00:00
|
|
|
} else if ($description instanceof external_multiple_structure) {
|
|
|
|
if (!is_array($params)) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_parameter_exception('Only arrays accepted. The bad value is: \''
|
|
|
|
. print_r($params, true) . '\'');
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach ($params as $param) {
|
|
|
|
$result[] = self::validate_parameters($description->content, $param);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
} else {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_parameter_exception('Invalid external api description');
|
2009-10-02 12:13:56 +00:00
|
|
|
}
|
2009-09-16 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 07:07:44 +00:00
|
|
|
/**
|
|
|
|
* Clean response
|
2010-05-22 19:06:34 +00:00
|
|
|
* If a response attribute is unknown from the description, we just ignore the attribute.
|
|
|
|
* If a response attribute is incorrect, invalid_response_exception is thrown.
|
2010-02-22 07:07:44 +00:00
|
|
|
* Note: this function is similar to validate parameters, however it is distinct because
|
|
|
|
* parameters validation must be distinct from cleaning return values.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2010-02-22 07:07:44 +00:00
|
|
|
* @param external_description $description description of the return values
|
|
|
|
* @param mixed $response the actual response
|
|
|
|
* @return mixed response with added defaults for optional items, invalid_response_exception thrown if any problem found
|
2012-01-18 10:52:25 +08:00
|
|
|
* @author 2010 Jerome Mouneyrac
|
|
|
|
* @since Moodle 2.0
|
2010-02-22 07:07:44 +00:00
|
|
|
*/
|
|
|
|
public static function clean_returnvalue(external_description $description, $response) {
|
|
|
|
if ($description instanceof external_value) {
|
|
|
|
if (is_array($response) or is_object($response)) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_response_exception('Scalar type expected, array or object received.');
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($description->type == PARAM_BOOL) {
|
|
|
|
// special case for PARAM_BOOL - we want true/false instead of the usual 1/0 - we can not be too strict here ;-)
|
|
|
|
if (is_bool($response) or $response === 0 or $response === 1 or $response === '0' or $response === '1') {
|
|
|
|
return (bool)$response;
|
|
|
|
}
|
|
|
|
}
|
2011-10-07 16:06:19 +08:00
|
|
|
$debuginfo = 'Invalid external api response: the value is "' . $response .
|
|
|
|
'", the server was expecting "' . $description->type . '" type';
|
|
|
|
try {
|
|
|
|
return validate_param($response, $description->type, $description->allownull, $debuginfo);
|
|
|
|
} catch (invalid_parameter_exception $e) {
|
|
|
|
//proper exception name, to be recursively catched to build the path to the faulty attribut
|
|
|
|
throw new invalid_response_exception($e->debuginfo);
|
|
|
|
}
|
2010-02-22 07:07:44 +00:00
|
|
|
|
|
|
|
} else if ($description instanceof external_single_structure) {
|
2013-01-07 11:11:14 +08:00
|
|
|
if (!is_array($response) && !is_object($response)) {
|
|
|
|
throw new invalid_response_exception('Only arrays/objects accepted. The bad value is: \'' .
|
2011-10-07 16:06:19 +08:00
|
|
|
print_r($response, true) . '\'');
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
2013-01-07 11:11:14 +08:00
|
|
|
|
|
|
|
// Cast objects into arrays.
|
|
|
|
if (is_object($response)) {
|
|
|
|
$response = (array) $response;
|
|
|
|
}
|
|
|
|
|
2010-02-22 07:07:44 +00:00
|
|
|
$result = array();
|
|
|
|
foreach ($description->keys as $key=>$subdesc) {
|
|
|
|
if (!array_key_exists($key, $response)) {
|
|
|
|
if ($subdesc->required == VALUE_REQUIRED) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_response_exception('Error in response - Missing following required key in a single structure: ' . $key);
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
if ($subdesc instanceof external_value) {
|
2010-07-27 09:13:27 +00:00
|
|
|
if ($subdesc->required == VALUE_DEFAULT) {
|
|
|
|
try {
|
2010-02-22 07:07:44 +00:00
|
|
|
$result[$key] = self::clean_returnvalue($subdesc, $subdesc->default);
|
2011-10-07 16:06:19 +08:00
|
|
|
} catch (invalid_response_exception $e) {
|
|
|
|
//build the path to the faulty attribut
|
|
|
|
throw new invalid_response_exception($key." => ".$e->getMessage() . ': ' . $e->debuginfo);
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-27 09:13:27 +00:00
|
|
|
}
|
2010-02-22 07:07:44 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$result[$key] = self::clean_returnvalue($subdesc, $response[$key]);
|
2011-10-07 16:06:19 +08:00
|
|
|
} catch (invalid_response_exception $e) {
|
|
|
|
//build the path to the faulty attribut
|
|
|
|
throw new invalid_response_exception($key." => ".$e->getMessage() . ': ' . $e->debuginfo);
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($response[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
} else if ($description instanceof external_multiple_structure) {
|
|
|
|
if (!is_array($response)) {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_response_exception('Only arrays accepted. The bad value is: \'' .
|
|
|
|
print_r($response, true) . '\'');
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach ($response as $param) {
|
|
|
|
$result[] = self::clean_returnvalue($description->content, $param);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
} else {
|
2011-10-07 16:06:19 +08:00
|
|
|
throw new invalid_response_exception('Invalid external api response description');
|
2010-02-22 07:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
/**
|
|
|
|
* Makes sure user may execute functions in this context.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @param stdClass $context
|
|
|
|
* @since Moodle 2.0
|
2009-09-14 23:52:08 +00:00
|
|
|
*/
|
2015-07-29 16:09:18 +02:00
|
|
|
public static function validate_context($context) {
|
2010-03-31 07:41:31 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2009-09-15 20:31:08 +00:00
|
|
|
if (empty($context)) {
|
|
|
|
throw new invalid_parameter_exception('Context does not exist');
|
|
|
|
}
|
2009-09-14 23:52:08 +00:00
|
|
|
if (empty(self::$contextrestriction)) {
|
2012-07-25 16:25:55 +08:00
|
|
|
self::$contextrestriction = context_system::instance();
|
2009-09-14 23:52:08 +00:00
|
|
|
}
|
|
|
|
$rcontext = self::$contextrestriction;
|
|
|
|
|
|
|
|
if ($rcontext->contextlevel == $context->contextlevel) {
|
2010-02-05 07:58:29 +00:00
|
|
|
if ($rcontext->id != $context->id) {
|
2009-09-14 23:52:08 +00:00
|
|
|
throw new restricted_context_exception();
|
|
|
|
}
|
|
|
|
} else if ($rcontext->contextlevel > $context->contextlevel) {
|
|
|
|
throw new restricted_context_exception();
|
|
|
|
} else {
|
2013-07-04 11:07:12 +08:00
|
|
|
$parents = $context->get_parent_context_ids();
|
2009-09-14 23:52:08 +00:00
|
|
|
if (!in_array($rcontext->id, $parents)) {
|
|
|
|
throw new restricted_context_exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->contextlevel >= CONTEXT_COURSE) {
|
2010-03-31 07:41:31 +00:00
|
|
|
list($context, $course, $cm) = get_context_info_array($context->id);
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
require_login($course, false, $cm, false, true);
|
2009-09-14 23:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 15:36:38 +08:00
|
|
|
|
|
|
|
/**
|
2013-06-17 15:04:59 +08:00
|
|
|
* Get context from passed parameters.
|
|
|
|
* The passed array must either contain a contextid or a combination of context level and instance id to fetch the context.
|
|
|
|
* For example, the context level can be "course" and instanceid can be courseid.
|
|
|
|
*
|
|
|
|
* See context_helper::get_all_levels() for a list of valid context levels.
|
2013-06-14 15:36:38 +08:00
|
|
|
*
|
|
|
|
* @param array $param
|
|
|
|
* @since Moodle 2.6
|
|
|
|
* @throws invalid_parameter_exception
|
|
|
|
* @return context
|
|
|
|
*/
|
2013-06-17 15:04:59 +08:00
|
|
|
protected static function get_context_from_params($param) {
|
2013-06-14 15:36:38 +08:00
|
|
|
$levels = context_helper::get_all_levels();
|
2014-09-03 22:26:40 +02:00
|
|
|
if (!empty($param['contextid'])) {
|
2013-06-14 15:36:38 +08:00
|
|
|
return context::instance_by_id($param['contextid'], IGNORE_MISSING);
|
2014-09-09 09:22:20 +02:00
|
|
|
} else if (!empty($param['contextlevel']) && isset($param['instanceid'])) {
|
2013-06-14 15:36:38 +08:00
|
|
|
$contextlevel = "context_".$param['contextlevel'];
|
|
|
|
if (!array_search($contextlevel, $levels)) {
|
|
|
|
throw new invalid_parameter_exception('Invalid context level = '.$param['contextlevel']);
|
|
|
|
}
|
|
|
|
return $contextlevel::instance($param['instanceid'], IGNORE_MISSING);
|
|
|
|
} else {
|
|
|
|
// No valid context info was found.
|
|
|
|
throw new invalid_parameter_exception('Missing parameters, please provide either context level with instance id or contextid');
|
|
|
|
}
|
|
|
|
}
|
2009-09-14 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-01 21:33:33 +00:00
|
|
|
/**
|
|
|
|
* Common ancestor of all parameter description classes
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
|
|
|
abstract class external_description {
|
2012-01-18 10:52:25 +08:00
|
|
|
/** @var string Description of element */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $desc;
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var bool Element value required, null not allowed */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $required;
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var mixed Default value */
|
2010-07-07 08:15:26 +00:00
|
|
|
public $default;
|
2009-10-01 21:33:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contructor
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-01 21:33:33 +00:00
|
|
|
* @param string $desc
|
|
|
|
* @param bool $required
|
2010-07-07 08:15:26 +00:00
|
|
|
* @param mixed $default
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
2010-07-07 08:15:26 +00:00
|
|
|
public function __construct($desc, $required, $default) {
|
2009-10-01 21:33:33 +00:00
|
|
|
$this->desc = $desc;
|
|
|
|
$this->required = $required;
|
2010-07-07 08:15:26 +00:00
|
|
|
$this->default = $default;
|
2009-10-01 21:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-18 10:52:25 +08:00
|
|
|
* Scalar value description class
|
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
2009-10-07 21:49:03 +00:00
|
|
|
class external_value extends external_description {
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var mixed Value type PARAM_XX */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $type;
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var bool Allow null values */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $allownull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-01 21:33:33 +00:00
|
|
|
* @param mixed $type
|
|
|
|
* @param string $desc
|
|
|
|
* @param bool $required
|
|
|
|
* @param mixed $default
|
|
|
|
* @param bool $allownull
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
2010-07-07 08:15:26 +00:00
|
|
|
public function __construct($type, $desc='', $required=VALUE_REQUIRED,
|
|
|
|
$default=null, $allownull=NULL_ALLOWED) {
|
|
|
|
parent::__construct($desc, $required, $default);
|
2010-07-25 13:35:05 +00:00
|
|
|
$this->type = $type;
|
2009-10-01 21:33:33 +00:00
|
|
|
$this->allownull = $allownull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Associative array description class
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
|
|
|
class external_single_structure extends external_description {
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var array Description of array keys key=>external_description */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $keys;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-01 21:33:33 +00:00
|
|
|
* @param array $keys
|
|
|
|
* @param string $desc
|
|
|
|
* @param bool $required
|
2010-07-07 08:15:26 +00:00
|
|
|
* @param array $default
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
2010-07-07 08:15:26 +00:00
|
|
|
public function __construct(array $keys, $desc='',
|
|
|
|
$required=VALUE_REQUIRED, $default=null) {
|
|
|
|
parent::__construct($desc, $required, $default);
|
2009-10-01 21:33:33 +00:00
|
|
|
$this->keys = $keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bulk array description class.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
|
|
|
class external_multiple_structure extends external_description {
|
2012-01-18 10:52:25 +08:00
|
|
|
|
|
|
|
/** @var external_description content */
|
2009-10-01 21:33:33 +00:00
|
|
|
public $content;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2009-10-01 21:33:33 +00:00
|
|
|
* @param external_description $content
|
|
|
|
* @param string $desc
|
|
|
|
* @param bool $required
|
2010-07-07 08:15:26 +00:00
|
|
|
* @param array $default
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:33:33 +00:00
|
|
|
*/
|
2010-07-07 08:15:26 +00:00
|
|
|
public function __construct(external_description $content, $desc='',
|
|
|
|
$required=VALUE_REQUIRED, $default=null) {
|
|
|
|
parent::__construct($desc, $required, $default);
|
2009-10-01 21:33:33 +00:00
|
|
|
$this->content = $content;
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 21:53:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of top level - PHP function parameters.
|
|
|
|
*
|
2012-01-18 10:52:25 +08:00
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2009 Petr Skodak
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
2009-10-01 21:53:14 +00:00
|
|
|
*/
|
|
|
|
class external_function_parameters extends external_single_structure {
|
2015-10-14 12:03:38 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor - does extra checking to prevent top level optional parameters.
|
|
|
|
*
|
|
|
|
* @param array $keys
|
|
|
|
* @param string $desc
|
|
|
|
* @param bool $required
|
|
|
|
* @param array $default
|
|
|
|
*/
|
|
|
|
public function __construct(array $keys, $desc='', $required=VALUE_REQUIRED, $default=null) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($CFG->debugdeveloper) {
|
|
|
|
foreach ($keys as $key => $value) {
|
|
|
|
if ($value instanceof external_value) {
|
|
|
|
if ($value->required == VALUE_OPTIONAL) {
|
|
|
|
debugging('External function parameters: invalid OPTIONAL value specified.', DEBUG_DEVELOPER);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::__construct($keys, $desc, $required, $default);
|
|
|
|
}
|
2009-10-01 21:53:14 +00:00
|
|
|
}
|
2010-03-22 14:20:18 +00:00
|
|
|
|
2012-01-18 10:52:25 +08:00
|
|
|
/**
|
|
|
|
* Generate a token
|
|
|
|
*
|
|
|
|
* @param string $tokentype EXTERNAL_TOKEN_EMBEDDED|EXTERNAL_TOKEN_PERMANENT
|
|
|
|
* @param stdClass|int $serviceorid service linked to the token
|
|
|
|
* @param int $userid user linked to the token
|
|
|
|
* @param stdClass|int $contextorid
|
|
|
|
* @param int $validuntil date when the token expired
|
|
|
|
* @param string $iprestriction allowed ip - if 0 or empty then all ips are allowed
|
|
|
|
* @return string generated token
|
|
|
|
* @author 2010 Jamie Pratt
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2010-03-22 14:20:18 +00:00
|
|
|
function external_generate_token($tokentype, $serviceorid, $userid, $contextorid, $validuntil=0, $iprestriction=''){
|
|
|
|
global $DB, $USER;
|
|
|
|
// make sure the token doesn't exist (even if it should be almost impossible with the random generation)
|
|
|
|
$numtries = 0;
|
|
|
|
do {
|
|
|
|
$numtries ++;
|
|
|
|
$generatedtoken = md5(uniqid(rand(),1));
|
|
|
|
if ($numtries > 5){
|
|
|
|
throw new moodle_exception('tokengenerationfailed');
|
|
|
|
}
|
|
|
|
} while ($DB->record_exists('external_tokens', array('token'=>$generatedtoken)));
|
2010-09-21 08:07:44 +00:00
|
|
|
$newtoken = new stdClass();
|
2010-03-22 14:20:18 +00:00
|
|
|
$newtoken->token = $generatedtoken;
|
|
|
|
if (!is_object($serviceorid)){
|
|
|
|
$service = $DB->get_record('external_services', array('id' => $serviceorid));
|
|
|
|
} else {
|
|
|
|
$service = $serviceorid;
|
|
|
|
}
|
|
|
|
if (!is_object($contextorid)){
|
2012-08-21 14:20:30 +08:00
|
|
|
$context = context::instance_by_id($contextorid, MUST_EXIST);
|
2010-03-22 14:20:18 +00:00
|
|
|
} else {
|
|
|
|
$context = $contextorid;
|
|
|
|
}
|
|
|
|
if (empty($service->requiredcapability) || has_capability($service->requiredcapability, $context, $userid)) {
|
|
|
|
$newtoken->externalserviceid = $service->id;
|
|
|
|
} else {
|
|
|
|
throw new moodle_exception('nocapabilitytousethisservice');
|
|
|
|
}
|
|
|
|
$newtoken->tokentype = $tokentype;
|
|
|
|
$newtoken->userid = $userid;
|
2010-04-28 13:16:58 +00:00
|
|
|
if ($tokentype == EXTERNAL_TOKEN_EMBEDDED){
|
|
|
|
$newtoken->sid = session_id();
|
|
|
|
}
|
2010-03-31 07:41:31 +00:00
|
|
|
|
|
|
|
$newtoken->contextid = $context->id;
|
2010-03-22 14:20:18 +00:00
|
|
|
$newtoken->creatorid = $USER->id;
|
|
|
|
$newtoken->timecreated = time();
|
|
|
|
$newtoken->validuntil = $validuntil;
|
|
|
|
if (!empty($iprestriction)) {
|
|
|
|
$newtoken->iprestriction = $iprestriction;
|
|
|
|
}
|
|
|
|
$DB->insert_record('external_tokens', $newtoken);
|
|
|
|
return $newtoken->token;
|
2010-04-28 13:16:58 +00:00
|
|
|
}
|
2012-01-18 10:52:25 +08:00
|
|
|
|
2010-04-28 13:16:58 +00:00
|
|
|
/**
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* Create and return a session linked token. Token to be used for html embedded client apps that want to communicate
|
2010-04-28 13:16:58 +00:00
|
|
|
* with the Moodle server through web services. The token is linked to the current session for the current page request.
|
|
|
|
* It is expected this will be called in the script generating the html page that is embedding the client app and that the
|
|
|
|
* returned token will be somehow passed into the client app being embedded in the page.
|
2012-01-18 10:52:25 +08:00
|
|
|
*
|
2010-04-28 13:16:58 +00:00
|
|
|
* @param string $servicename name of the web service. Service name as defined in db/services.php
|
|
|
|
* @param int $context context within which the web service can operate.
|
|
|
|
* @return int returns token id.
|
2012-01-18 10:52:25 +08:00
|
|
|
* @since Moodle 2.0
|
2010-04-28 13:16:58 +00:00
|
|
|
*/
|
|
|
|
function external_create_service_token($servicename, $context){
|
|
|
|
global $USER, $DB;
|
|
|
|
$service = $DB->get_record('external_services', array('name'=>$servicename), '*', MUST_EXIST);
|
|
|
|
return external_generate_token(EXTERNAL_TOKEN_EMBEDDED, $service, $USER->id, $context, 0);
|
2012-04-11 14:48:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all pre-built services (+ related tokens) and external functions information defined in the specified component.
|
|
|
|
*
|
|
|
|
* @param string $component name of component (moodle, mod_assignment, etc.)
|
|
|
|
*/
|
|
|
|
function external_delete_descriptions($component) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$params = array($component);
|
|
|
|
|
|
|
|
$DB->delete_records_select('external_tokens',
|
|
|
|
"externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
|
|
|
|
$DB->delete_records_select('external_services_users',
|
|
|
|
"externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
|
|
|
|
$DB->delete_records_select('external_services_functions',
|
|
|
|
"functionname IN (SELECT name FROM {external_functions} WHERE component = ?)", $params);
|
|
|
|
$DB->delete_records('external_services', array('component'=>$component));
|
|
|
|
$DB->delete_records('external_functions', array('component'=>$component));
|
2012-05-15 13:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-31 12:31:27 +08:00
|
|
|
* Standard Moodle web service warnings
|
2012-05-15 13:40:58 +08:00
|
|
|
*
|
2012-05-31 12:31:27 +08:00
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2012 Jerome Mouneyrac
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.3
|
|
|
|
*/
|
|
|
|
class external_warnings extends external_multiple_structure {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @since Moodle 2.3
|
|
|
|
*/
|
2012-12-13 11:15:50 +08:00
|
|
|
public function __construct($itemdesc = 'item', $itemiddesc = 'item id',
|
|
|
|
$warningcodedesc = 'the warning code can be used by the client app to implement specific behaviour') {
|
2012-05-31 12:31:27 +08:00
|
|
|
|
|
|
|
parent::__construct(
|
|
|
|
new external_single_structure(
|
|
|
|
array(
|
2012-12-13 11:15:50 +08:00
|
|
|
'item' => new external_value(PARAM_TEXT, $itemdesc, VALUE_OPTIONAL),
|
|
|
|
'itemid' => new external_value(PARAM_INT, $itemiddesc, VALUE_OPTIONAL),
|
|
|
|
'warningcode' => new external_value(PARAM_ALPHANUM, $warningcodedesc),
|
2012-05-31 12:31:27 +08:00
|
|
|
'message' => new external_value(PARAM_TEXT,
|
|
|
|
'untranslated english message to explain the warning')
|
|
|
|
), 'warning'),
|
|
|
|
'list of warnings', VALUE_OPTIONAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pre-filled external_value class for text format.
|
|
|
|
*
|
|
|
|
* Default is FORMAT_HTML
|
|
|
|
* This should be used all the time in external xxx_params()/xxx_returns functions
|
|
|
|
* as it is the standard way to implement text format param/return values.
|
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2012 Jerome Mouneyrac
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.3
|
2012-05-15 13:40:58 +08:00
|
|
|
*/
|
2012-05-31 12:31:27 +08:00
|
|
|
class external_format_value extends external_value {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $textfieldname Name of the text field
|
|
|
|
* @param int $required if VALUE_REQUIRED then set standard default FORMAT_HTML
|
|
|
|
* @since Moodle 2.3
|
|
|
|
*/
|
|
|
|
public function __construct($textfieldname, $required = VALUE_REQUIRED) {
|
|
|
|
|
|
|
|
$default = ($required == VALUE_DEFAULT) ? FORMAT_HTML : null;
|
|
|
|
|
|
|
|
$desc = $textfieldname . ' format (' . FORMAT_HTML . ' = HTML, '
|
|
|
|
. FORMAT_MOODLE . ' = MOODLE, '
|
|
|
|
. FORMAT_PLAIN . ' = PLAIN or '
|
|
|
|
. FORMAT_MARKDOWN . ' = MARKDOWN)';
|
|
|
|
|
2012-06-06 14:23:10 +08:00
|
|
|
parent::__construct(PARAM_INT, $desc, $required, $default);
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate text field format against known FORMAT_XXX
|
|
|
|
*
|
|
|
|
* @param array $format the format to validate
|
|
|
|
* @return the validated format
|
|
|
|
* @throws coding_exception
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.3
|
2012-05-31 12:31:27 +08:00
|
|
|
*/
|
|
|
|
function external_validate_format($format) {
|
|
|
|
$allowedformats = array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN);
|
|
|
|
if (!in_array($format, $allowedformats)) {
|
|
|
|
throw new moodle_exception('formatnotsupported', 'webservice', '' , null,
|
|
|
|
'The format with value=' . $format . ' is not supported by this Moodle site');
|
|
|
|
}
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
2015-08-27 14:21:56 +08:00
|
|
|
/**
|
|
|
|
* Format the string to be returned properly as requested by the either the web service server,
|
|
|
|
* either by an internally call.
|
|
|
|
* The caller can change the format (raw) with the external_settings singleton
|
|
|
|
* All web service servers must set this singleton when parsing the $_GET and $_POST.
|
|
|
|
*
|
|
|
|
* @param string $str The string to be filtered. Should be plain text, expect
|
|
|
|
* possibly for multilang tags.
|
|
|
|
* @param boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713
|
|
|
|
* @param int $contextid The id of the context for the string (affects filters).
|
|
|
|
* @param array $options options array/object or courseid
|
|
|
|
* @return string text
|
|
|
|
* @since Moodle 3.0
|
|
|
|
*/
|
|
|
|
function external_format_string($str, $contextid, $striplinks = true, $options = array()) {
|
|
|
|
|
|
|
|
// Get settings (singleton).
|
|
|
|
$settings = external_settings::get_instance();
|
|
|
|
if (empty($contextid)) {
|
|
|
|
throw new coding_exception('contextid is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$settings->get_raw()) {
|
|
|
|
$context = context::instance_by_id($contextid);
|
|
|
|
$options['context'] = $context;
|
2015-09-10 12:55:52 +08:00
|
|
|
$options['filter'] = $settings->get_filter();
|
2015-08-27 14:21:56 +08:00
|
|
|
$str = format_string($str, $striplinks, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2012-05-31 12:31:27 +08:00
|
|
|
/**
|
|
|
|
* Format the text to be returned properly as requested by the either the web service server,
|
|
|
|
* either by an internally call.
|
|
|
|
* The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton
|
|
|
|
* All web service servers must set this singleton when parsing the $_GET and $_POST.
|
|
|
|
*
|
|
|
|
* @param string $text The content that may contain ULRs in need of rewriting.
|
2015-04-13 14:38:30 +02:00
|
|
|
* @param int $textformat The text format.
|
2012-05-31 12:31:27 +08:00
|
|
|
* @param int $contextid This parameter and the next two identify the file area to use.
|
|
|
|
* @param string $component
|
|
|
|
* @param string $filearea helps identify the file area.
|
|
|
|
* @param int $itemid helps identify the file area.
|
|
|
|
* @return array text + textformat
|
|
|
|
* @since Moodle 2.3
|
|
|
|
*/
|
|
|
|
function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// Get settings (singleton).
|
|
|
|
$settings = external_settings::get_instance();
|
|
|
|
|
|
|
|
if ($settings->get_fileurl()) {
|
2013-05-07 14:55:55 +08:00
|
|
|
require_once($CFG->libdir . "/filelib.php");
|
2012-05-31 12:31:27 +08:00
|
|
|
$text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$settings->get_raw()) {
|
2015-08-27 14:21:56 +08:00
|
|
|
$context = context::instance_by_id($contextid);
|
|
|
|
$text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter(), 'context' => $context));
|
2015-04-13 14:38:30 +02:00
|
|
|
$textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML).
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return array($text, $textformat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton to handle the external settings.
|
|
|
|
*
|
|
|
|
* We use singleton to encapsulate the "logic"
|
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2012 Jerome Mouneyrac
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.3
|
|
|
|
*/
|
|
|
|
class external_settings {
|
|
|
|
|
|
|
|
/** @var object the singleton instance */
|
|
|
|
public static $instance = null;
|
|
|
|
|
|
|
|
/** @var boolean Should the external function return raw text or formatted */
|
|
|
|
private $raw = false;
|
|
|
|
|
|
|
|
/** @var boolean Should the external function filter the text */
|
|
|
|
private $filter = false;
|
|
|
|
|
|
|
|
/** @var boolean Should the external function rewrite plugin file url */
|
|
|
|
private $fileurl = true;
|
|
|
|
|
|
|
|
/** @var string In which file should the urls be rewritten */
|
|
|
|
private $file = 'webservice/pluginfile.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor - protected - can not be instanciated
|
|
|
|
*/
|
|
|
|
protected function __construct() {
|
2015-08-27 14:21:56 +08:00
|
|
|
if (!defined('AJAX_SCRIPT') && !defined('CLI_SCRIPT') && !defined('WS_SERVER')) {
|
|
|
|
// For normal pages, the default should match the default for format_text.
|
|
|
|
$this->filter = true;
|
|
|
|
}
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone - private - can not be cloned
|
|
|
|
*/
|
|
|
|
private final function __clone() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return only one instance
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function get_instance() {
|
|
|
|
if (self::$instance === null) {
|
|
|
|
self::$instance = new external_settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set raw
|
|
|
|
*
|
|
|
|
* @param boolean $raw
|
|
|
|
*/
|
|
|
|
public function set_raw($raw) {
|
|
|
|
$this->raw = $raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get raw
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function get_raw() {
|
|
|
|
return $this->raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set filter
|
|
|
|
*
|
|
|
|
* @param boolean $filter
|
|
|
|
*/
|
|
|
|
public function set_filter($filter) {
|
|
|
|
$this->filter = $filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get filter
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function get_filter() {
|
|
|
|
return $this->filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set fileurl
|
|
|
|
*
|
|
|
|
* @param boolean $fileurl
|
|
|
|
*/
|
|
|
|
public function set_fileurl($fileurl) {
|
|
|
|
$this->fileurl = $fileurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get fileurl
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function get_fileurl() {
|
|
|
|
return $this->fileurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public function set_file($file) {
|
|
|
|
$this->file = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_file() {
|
|
|
|
return $this->file;
|
|
|
|
}
|
2012-05-15 18:40:06 +08:00
|
|
|
}
|
2015-07-29 16:09:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions for the external API.
|
|
|
|
*
|
|
|
|
* @package core_webservice
|
|
|
|
* @copyright 2015 Juan Leyva
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 3.0
|
|
|
|
*/
|
|
|
|
class external_util {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a list of courses, returning the complete course objects for valid courses.
|
|
|
|
*
|
|
|
|
* @param array $courseids A list of course ids
|
2015-12-01 20:25:05 +00:00
|
|
|
* @param array $courses An array of courses already pre-fetched, indexed by course id.
|
2015-07-29 16:09:18 +02:00
|
|
|
* @return array An array of courses and the validation warnings
|
|
|
|
*/
|
2015-10-06 12:56:13 +08:00
|
|
|
public static function validate_courses($courseids, $courses = array()) {
|
2015-07-29 16:09:18 +02:00
|
|
|
// Delete duplicates.
|
|
|
|
$courseids = array_unique($courseids);
|
|
|
|
$warnings = array();
|
|
|
|
|
2015-10-06 12:56:13 +08:00
|
|
|
// Remove courses which are not even requested.
|
|
|
|
$courses = array_intersect_key($courses, array_flip($courseids));
|
|
|
|
|
2015-07-29 16:09:18 +02:00
|
|
|
foreach ($courseids as $cid) {
|
|
|
|
// Check the user can function in this context.
|
|
|
|
try {
|
|
|
|
$context = context_course::instance($cid);
|
|
|
|
external_api::validate_context($context);
|
2015-10-06 12:56:13 +08:00
|
|
|
|
|
|
|
if (!isset($courses[$cid])) {
|
|
|
|
$courses[$cid] = get_course($cid);
|
|
|
|
}
|
2015-07-29 16:09:18 +02:00
|
|
|
} catch (Exception $e) {
|
2015-10-06 12:56:13 +08:00
|
|
|
unset($courses[$cid]);
|
2015-07-29 16:09:18 +02:00
|
|
|
$warnings[] = array(
|
|
|
|
'item' => 'course',
|
|
|
|
'itemid' => $cid,
|
|
|
|
'warningcode' => '1',
|
|
|
|
'message' => 'No access rights in course context'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($courses, $warnings);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|