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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-12-07 16:47:50 +08:00
|
|
|
use core_external\util;
|
2009-10-26 19:10:20 +00:00
|
|
|
|
2022-12-07 10:32:02 +08:00
|
|
|
class_alias(\core_external\external_api::class, 'external_api');
|
2022-12-07 12:28:45 +08:00
|
|
|
class_alias(\core_external\restricted_context_exception::class, 'restricted_context_exception');
|
2022-12-07 11:49:40 +08:00
|
|
|
class_alias(\core_external\external_description::class, 'external_description');
|
2022-12-07 12:07:35 +08:00
|
|
|
class_alias(\core_external\external_value::class, 'external_value');
|
2022-12-07 13:10:17 +08:00
|
|
|
class_alias(\core_external\external_format_value::class, 'external_format_value');
|
2022-12-07 12:13:13 +08:00
|
|
|
class_alias(\core_external\external_single_structure::class, 'external_single_structure');
|
2022-12-07 12:13:48 +08:00
|
|
|
class_alias(\core_external\external_multiple_structure::class, 'external_multiple_structure');
|
2022-12-07 12:24:31 +08:00
|
|
|
class_alias(\core_external\external_function_parameters::class, 'external_function_parameters');
|
2022-12-07 21:30:09 +08:00
|
|
|
class_alias(\core_external\util::class, 'external_util');
|
2022-12-07 12:33:43 +08:00
|
|
|
class_alias(\core_external\external_files::class, 'external_files');
|
2022-12-07 12:51:13 +08:00
|
|
|
class_alias(\core_external\external_warnings::class, 'external_warnings');
|
2022-12-07 13:14:18 +08:00
|
|
|
class_alias(\core_external\external_settings::class, 'external_settings');
|
2009-10-01 21:53:14 +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
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2022-12-07 15:15:04 +08:00
|
|
|
function external_generate_token($tokentype, $serviceorid, $userid, $contextorid, $validuntil = 0, $iprestriction = '') {
|
|
|
|
if (is_numeric($serviceorid)) {
|
|
|
|
$service = util::get_service_by_id($serviceorid);
|
|
|
|
} else if (is_string($serviceorid)) {
|
|
|
|
$service = util::get_service_by_name($serviceorid);
|
2010-03-22 14:20:18 +00:00
|
|
|
} else {
|
|
|
|
$service = $serviceorid;
|
|
|
|
}
|
2022-12-07 15:15:04 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-03-31 07:41:31 +00:00
|
|
|
|
2022-12-07 15:15:04 +08:00
|
|
|
return util::generate_token(
|
|
|
|
$tokentype,
|
|
|
|
$service,
|
|
|
|
$userid,
|
|
|
|
$context,
|
|
|
|
$validuntil,
|
|
|
|
$iprestriction
|
|
|
|
);
|
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
|
|
|
*/
|
2022-12-07 15:15:04 +08:00
|
|
|
function external_create_service_token($servicename, $contextid) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
return util::generate_token(
|
|
|
|
EXTERNAL_TOKEN_EMBEDDED,
|
|
|
|
util::get_service_by_name($servicename),
|
|
|
|
$USER->id,
|
|
|
|
\context::instance_by_id($contextid)
|
|
|
|
);
|
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) {
|
2022-12-16 10:49:02 +08:00
|
|
|
util::delete_service_descriptions($component);
|
2012-05-15 13:40:58 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-12-07 16:47:50 +08:00
|
|
|
return util::validate_format($format);
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2016-05-03 19:40:47 +08:00
|
|
|
* <pre>
|
|
|
|
* Options are the same that in {@link format_string()} with some changes:
|
|
|
|
* filter : Can be set to false to force filters off, else observes {@link external_settings}.
|
|
|
|
* </pre>
|
|
|
|
*
|
2015-08-27 14:21:56 +08:00
|
|
|
* @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
|
2017-11-24 06:36:09 +00:00
|
|
|
* @param context|int $contextorid The id of the context for the string or the context (affects filters).
|
2015-08-27 14:21:56 +08:00
|
|
|
* @param array $options options array/object or courseid
|
|
|
|
* @return string text
|
|
|
|
* @since Moodle 3.0
|
|
|
|
*/
|
2022-12-07 16:47:50 +08:00
|
|
|
function external_format_string($str, $context, $striplinks = true, $options = []) {
|
|
|
|
if (!$context instanceof context) {
|
|
|
|
$context = context::instance_by_id($context);
|
2015-08-27 14:21:56 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 16:47:50 +08:00
|
|
|
return util::format_string($str, $context, $striplinks, $options);
|
2015-08-27 14:21:56 +08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2016-03-30 18:45:27 +02:00
|
|
|
* <pre>
|
|
|
|
* Options are the same that in {@link format_text()} with some changes in defaults to provide backwards compatibility:
|
|
|
|
* trusted : If true the string won't be cleaned. Default false.
|
|
|
|
* noclean : If true the string won't be cleaned only if trusted is also true. Default false.
|
|
|
|
* nocache : If true the string will not be cached and will be formatted every call. Default false.
|
2016-05-03 19:40:47 +08:00
|
|
|
* filter : Can be set to false to force filters off, else observes {@link external_settings}.
|
2016-03-30 18:45:27 +02:00
|
|
|
* para : If true then the returned string will be wrapped in div tags. Default (different from format_text) false.
|
|
|
|
* Default changed because div tags are not commonly needed.
|
|
|
|
* newlines : If true then lines newline breaks will be converted to HTML newline breaks. Default true.
|
|
|
|
* context : Not used! Using contextid parameter instead.
|
|
|
|
* overflowdiv : If set to true the formatted text will be encased in a div with the class no-overflow before being
|
|
|
|
* returned. Default false.
|
|
|
|
* allowid : If true then id attributes will not be removed, even when using htmlpurifier. Default (different from
|
|
|
|
* format_text) true. Default changed id attributes are commonly needed.
|
2016-07-06 17:48:07 +08:00
|
|
|
* blanktarget : If true all <a> tags will have target="_blank" added unless target is explicitly specified.
|
2016-03-30 18:45:27 +02:00
|
|
|
* </pre>
|
|
|
|
*
|
2012-05-31 12:31:27 +08:00
|
|
|
* @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.
|
2022-12-07 16:47:50 +08:00
|
|
|
* @param context|int $context This parameter and the next two identify the file area to use.
|
2012-05-31 12:31:27 +08:00
|
|
|
* @param string $component
|
|
|
|
* @param string $filearea helps identify the file area.
|
|
|
|
* @param int $itemid helps identify the file area.
|
2016-03-30 18:45:27 +02:00
|
|
|
* @param object/array $options text formatting options
|
2012-05-31 12:31:27 +08:00
|
|
|
* @return array text + textformat
|
|
|
|
* @since Moodle 2.3
|
2016-06-20 16:25:35 +01:00
|
|
|
* @since Moodle 3.2 component, filearea and itemid are optional parameters
|
2012-05-31 12:31:27 +08:00
|
|
|
*/
|
2022-12-07 16:47:50 +08:00
|
|
|
function external_format_text($text, $textformat, $context, $component = null, $filearea = null, $itemid = null, $options = null) {
|
|
|
|
if (!$context instanceof context) {
|
|
|
|
$context = context::instance_by_id($context);
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 16:47:50 +08:00
|
|
|
return util::format_text($text, $textformat, $context, $component, $filearea, $itemid, $options);
|
2012-05-31 12:31:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 13:09:29 +02:00
|
|
|
/**
|
|
|
|
* Generate or return an existing token for the current authenticated user.
|
|
|
|
* This function is used for creating a valid token for users authenticathing via login/token.php or admin/tool/mobile/launch.php.
|
|
|
|
*
|
|
|
|
* @param stdClass $service external service object
|
|
|
|
* @return stdClass token object
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
|
|
|
function external_generate_token_for_current_user($service) {
|
2022-12-07 15:15:04 +08:00
|
|
|
return util::generate_token_for_current_user($service);
|
2016-04-28 13:09:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:13:47 +00:00
|
|
|
/**
|
|
|
|
* Set the last time a token was sent and trigger the \core\event\webservice_token_sent event.
|
|
|
|
*
|
|
|
|
* This function is used when a token is generated by the user via login/token.php or admin/tool/mobile/launch.php.
|
|
|
|
* In order to protect the privatetoken, we remove it from the event params.
|
|
|
|
*
|
|
|
|
* @param stdClass $token token object
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
2022-12-07 15:15:04 +08:00
|
|
|
function external_log_token_request($token): void {
|
|
|
|
util::log_token_request($token);
|
2016-11-03 19:13:47 +00:00
|
|
|
}
|