first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,46 @@
<?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/>.
/**
* Privacy provider implementation for webservice_soap.
*
* @package webservice_soap
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace webservice_soap\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy provider implementation for webservice_soap.
*
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+261
View File
@@ -0,0 +1,261 @@
<?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/>.
/**
* WSDL generator for the SOAP web service.
*
* @package webservice_soap
* @copyright 2016 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace webservice_soap;
/**
* WSDL generator for the SOAP web service.
*
* @package webservice_soap
* @copyright 2016 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wsdl {
/** Namespace URI for the WSDL framework. */
const NS_WSDL = 'http://schemas.xmlsoap.org/wsdl/';
/** Encoding namespace URI as defined by SOAP 1.1 */
const NS_SOAP_ENC = 'http://schemas.xmlsoap.org/soap/encoding/';
/** Namespace URI for the WSDL SOAP binding. */
const NS_SOAP = 'http://schemas.xmlsoap.org/wsdl/soap/';
/** Schema namespace URI as defined by XSD. */
const NS_XSD = 'http://www.w3.org/2001/XMLSchema';
/** WSDL namespace for the WSDL HTTP GET and POST binding. */
const NS_SOAP_TRANSPORT = 'http://schemas.xmlsoap.org/soap/http';
/** BINDING - string constant attached to the service class name to identify binding nodes. */
const BINDING = 'Binding';
/** IN - string constant attached to the function name to identify input nodes. */
const IN = 'In';
/** OUT - string constant attached to the function name to identify output nodes. */
const OUT = 'Out';
/** PORT - string constant attached to the service class name to identify port nodes. */
const PORT = 'Port';
/** SERVICE string constant attached to the service class name to identify service nodes. */
const SERVICE = 'Service';
/** @var string The name of the service class. */
private $serviceclass;
/** @var string The WSDL namespace. */
private $namespace;
/** @var array The WSDL's message nodes. */
private $messagenodes;
/** @var \SimpleXMLElement The WSDL's binding node. */
private $nodebinding;
/** @var \SimpleXMLElement The WSDL's definitions node. */
private $nodedefinitions;
/** @var \SimpleXMLElement The WSDL's portType node. */
private $nodeporttype;
/** @var \SimpleXMLElement The WSDL's service node. */
private $nodeservice;
/** @var \SimpleXMLElement The WSDL's types node. */
private $nodetypes;
/**
* webservice_soap_wsdl constructor.
*
* @param string $serviceclass The service class' name.
* @param string $namespace The WSDL namespace.
*/
public function __construct($serviceclass, $namespace) {
$this->serviceclass = $serviceclass;
$this->namespace = $namespace;
// Initialise definitions node.
$this->nodedefinitions = new \SimpleXMLElement('<definitions />');
$this->nodedefinitions->addAttribute('xmlns', self::NS_WSDL);
$this->nodedefinitions->addAttribute('x:xmlns:tns', $namespace);
$this->nodedefinitions->addAttribute('x:xmlns:soap', self::NS_SOAP);
$this->nodedefinitions->addAttribute('x:xmlns:xsd', self::NS_XSD);
$this->nodedefinitions->addAttribute('x:xmlns:soap-enc', self::NS_SOAP_ENC);
$this->nodedefinitions->addAttribute('x:xmlns:wsdl', self::NS_WSDL);
$this->nodedefinitions->addAttribute('name', $serviceclass);
$this->nodedefinitions->addAttribute('targetNamespace', $namespace);
// Initialise types node.
$this->nodetypes = $this->nodedefinitions->addChild('types');
$typeschema = $this->nodetypes->addChild('x:xsd:schema');
$typeschema->addAttribute('targetNamespace', $namespace);
// Initialise the portType node.
$this->nodeporttype = $this->nodedefinitions->addChild('portType');
$this->nodeporttype->addAttribute('name', $serviceclass . self::PORT);
// Initialise the binding node.
$this->nodebinding = $this->nodedefinitions->addChild('binding');
$this->nodebinding->addAttribute('name', $serviceclass . self::BINDING);
$this->nodebinding->addAttribute('type', 'tns:' . $serviceclass . self::PORT);
$soapbinding = $this->nodebinding->addChild('x:soap:binding');
$soapbinding->addAttribute('style', 'rpc');
$soapbinding->addAttribute('transport', self::NS_SOAP_TRANSPORT);
// Initialise the service node.
$this->nodeservice = $this->nodedefinitions->addChild('service');
$this->nodeservice->addAttribute('name', $serviceclass . self::SERVICE);
$serviceport = $this->nodeservice->addChild('port');
$serviceport->addAttribute('name', $serviceclass . self::PORT);
$serviceport->addAttribute('binding', 'tns:' . $serviceclass . self::BINDING);
$soapaddress = $serviceport->addChild('x:soap:address');
$soapaddress->addAttribute('location', $namespace);
// Initialise message nodes.
$this->messagenodes = array();
}
/**
* Adds a complex type to the WSDL.
*
* @param string $classname The complex type's class name.
* @param array $properties An associative array containing the properties of the complex type class.
*/
public function add_complex_type($classname, $properties) {
$typeschema = $this->nodetypes->children();
// Append the complex type.
$complextype = $typeschema->addChild('x:xsd:complexType');
$complextype->addAttribute('name', $classname);
$child = $complextype->addChild('x:xsd:all');
foreach ($properties as $name => $options) {
$param = $child->addChild('x:xsd:element');
$param->addAttribute('name', $name);
$param->addAttribute('type', $this->get_soap_type($options['type']));
if (!empty($options['nillable'])) {
$param->addAttribute('nillable', 'true');
}
}
}
/**
* Registers the external service method to the WSDL.
*
* @param string $functionname The name of the web service function to be registered.
* @param array $inputparams Contains the function's input parameters with their associated types.
* @param array $outputparams Contains the function's output parameters with their associated types.
* @param string $documentation The function's description.
*/
public function register($functionname, $inputparams = array(), $outputparams = array(), $documentation = '') {
// Process portType operation nodes.
$porttypeoperation = $this->nodeporttype->addChild('operation');
$porttypeoperation->addAttribute('name', $functionname);
// Documentation node.
$porttypeoperation->addChild('documentation', $documentation);
// Process binding operation nodes.
$bindingoperation = $this->nodebinding->addChild('operation');
$bindingoperation->addAttribute('name', $functionname);
$soapoperation = $bindingoperation->addChild('x:soap:operation');
$soapoperation->addAttribute('soapAction', $this->namespace . '#' . $functionname);
// Input nodes.
$this->process_params($functionname, $porttypeoperation, $bindingoperation, $inputparams);
// Output nodes.
$this->process_params($functionname, $porttypeoperation, $bindingoperation, $outputparams, true);
}
/**
* Outputs the WSDL in XML format.
*
* @return mixed The string value of the WSDL in XML format. False, otherwise.
*/
public function to_xml() {
// Return WSDL in XML format.
return $this->nodedefinitions->asXML();
}
/**
* Utility method that returns the encoded SOAP type based on the given type string.
*
* @param string $type The input type string.
* @return string The encoded type for the WSDL.
*/
private function get_soap_type($type) {
switch($type) {
case 'int':
case 'double':
case 'string':
return 'xsd:' . $type;
case 'array':
return 'soap-enc:Array';
default:
return 'tns:' . $type;
}
}
/**
* Utility method that creates input/output nodes from input/output params.
*
* @param string $functionname The name of the function being registered.
* @param \SimpleXMLElement $porttypeoperation The port type operation node.
* @param \SimpleXMLElement $bindingoperation The binding operation node.
* @param array $params The function's input/output parameters.
* @param bool $isoutput Flag to indicate if the nodes to be generated are for input or for output.
*/
private function process_params($functionname, \SimpleXMLElement $porttypeoperation, \SimpleXMLElement $bindingoperation,
array $params = null, $isoutput = false) {
// Do nothing if parameter array is empty.
if (empty($params)) {
return;
}
$postfix = self::IN;
$childtype = 'input';
if ($isoutput) {
$postfix = self::OUT;
$childtype = 'output';
}
// For portType operation node.
$child = $porttypeoperation->addChild($childtype);
$child->addAttribute('message', 'tns:' . $functionname . $postfix);
// For binding operation node.
$child = $bindingoperation->addChild($childtype);
$soapbody = $child->addChild('x:soap:body');
$soapbody->addAttribute('use', 'encoded');
$soapbody->addAttribute('encodingStyle', self::NS_SOAP_ENC);
$soapbody->addAttribute('namespace', $this->namespace);
// Process message nodes.
$messagein = $this->nodedefinitions->addChild('message');
$messagein->addAttribute('name', $functionname . $postfix);
foreach ($params as $name => $options) {
$part = $messagein->addChild('part');
$part->addAttribute('name', $name);
$part->addAttribute('type', $this->get_soap_type($options['type']));
}
}
}
+36
View File
@@ -0,0 +1,36 @@
<?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/>.
/**
* SOAP server related capabilities
*
* @package webservice_soap
* @category access
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$capabilities = array(
'webservice/soap:use' => array(
'captype' => 'read', // in fact this may be considered read and write at the same time
'contextlevel' => CONTEXT_COURSE, // the context level should be probably CONTEXT_MODULE
'archetypes' => array(
),
),
);
@@ -0,0 +1,29 @@
<?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/>.
/**
* Strings for component 'webservice_soap', language 'en', branch 'MOODLE_20_STABLE'
*
* @package webservice_soap
* @category string
* @copyright 2010 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'SOAP protocol';
$string['privacy:metadata'] = 'The SOAP protocol plugin does not store any personal data.';
$string['soap:use'] = 'Use SOAP protocol';
+93
View File
@@ -0,0 +1,93 @@
<?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/>.
/**
* Moodle SOAP library
*
* @package webservice_soap
* @copyright 2009 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Moodle SOAP client
*
* It has been implemented for unit testing purpose (all protocols have similar client)
*
* @package webservice_soap
* @copyright 2010 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_soap_client {
/** @var moodle_url The server url. */
private $serverurl;
/** @var string The WS token. */
private $token;
/** @var array|null SOAP options. */
private $options;
/**
* Constructor
*
* @param string $serverurl a Moodle URL
* @param string $token the token used to do the web service call
* @param array $options PHP SOAP client options - see php.net
*/
public function __construct($serverurl, $token = null, array $options = null) {
$this->serverurl = new moodle_url($serverurl);
$this->token = $token ?: $this->serverurl->get_param('wstoken');
$this->options = $options ?: array();
}
/**
* Set the token used to do the SOAP call
*
* @param string $token the token used to do the web service call
*/
public function set_token($token) {
$this->token = $token;
}
/**
* Execute client WS request with token authentication
*
* @param string $functionname the function name
* @param array $params the parameters of the function
* @return mixed
*/
public function call($functionname, $params) {
if ($this->token) {
$this->serverurl->param('wstoken', $this->token);
}
$this->serverurl->param('wsdl', 1);
$opts = array(
'http' => array(
'user_agent' => 'Moodle SOAP Client'
)
);
$context = stream_context_create($opts);
$this->options['stream_context'] = $context;
$this->options['cache_wsdl'] = WSDL_CACHE_NONE;
$client = new SoapClient($this->serverurl->out(false), $this->options);
return $client->__soapCall($functionname, $params);
}
}
+336
View File
@@ -0,0 +1,336 @@
<?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/>.
/**
* SOAP web service implementation classes and methods.
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
global $CFG;
require_once($CFG->dirroot . '/webservice/lib.php');
use webservice_soap\wsdl;
/**
* SOAP service server implementation.
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class webservice_soap_server extends webservice_base_server {
/** @var moodle_url The server URL. */
protected $serverurl;
/** @var SoapServer The Soap */
protected $soapserver;
/** @var string The response. */
protected $response;
/** @var string The class name of the virtual class generated for this web service. */
protected $serviceclass;
/** @var bool WSDL mode flag. */
protected $wsdlmode;
/** @var \webservice_soap\wsdl The object for WSDL generation. */
protected $wsdl;
/**
* Contructor.
*
* @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
*/
public function __construct($authmethod) {
parent::__construct($authmethod);
// Must not cache wsdl - the list of functions is created on the fly.
ini_set('soap.wsdl_cache_enabled', '0');
$this->wsname = 'soap';
$this->wsdlmode = false;
}
/**
* This method parses the $_POST and $_GET superglobals and looks for the following information:
* - User authentication parameters:
* - Username + password (wsusername and wspassword), or
* - Token (wstoken)
*/
protected function parse_request() {
// Retrieve and clean the POST/GET parameters from the parameters specific to the server.
parent::set_web_service_call_settings();
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
$this->username = optional_param('wsusername', null, PARAM_RAW);
$this->password = optional_param('wspassword', null, PARAM_RAW);
if (!$this->username or !$this->password) {
// Workaround for the trouble with & in soap urls.
$authdata = get_file_argument();
$authdata = explode('/', trim($authdata, '/'));
if (count($authdata) == 2) {
list($this->username, $this->password) = $authdata;
}
}
$this->serverurl = new moodle_url('/webservice/soap/simpleserver.php/' . $this->username . '/' . $this->password);
} else {
$this->token = optional_param('wstoken', null, PARAM_RAW);
$this->serverurl = new moodle_url('/webservice/soap/server.php');
$this->serverurl->param('wstoken', $this->token);
}
if ($wsdl = optional_param('wsdl', 0, PARAM_INT)) {
$this->wsdlmode = true;
}
}
/**
* Runs the SOAP web service.
*
* @throws coding_exception
* @throws moodle_exception
* @throws webservice_access_exception
*/
public function run() {
// We will probably need a lot of memory in some functions.
raise_memory_limit(MEMORY_EXTRA);
// Set some longer timeout since operations may need longer time to finish.
\core_external\external_api::set_timeout();
// Set up exception handler.
set_exception_handler(array($this, 'exception_handler'));
// Init all properties from the request data.
$this->parse_request();
// Authenticate user, this has to be done after the request parsing. This also sets up $USER and $SESSION.
$this->authenticate_user();
// Make a list of all functions user is allowed to execute.
$this->init_service_class();
if ($this->wsdlmode) {
// Generate the WSDL.
$this->generate_wsdl();
}
// Log the web service request.
$params = array(
'other' => array(
'function' => 'unknown'
)
);
/** @var \core\event\webservice_function_called $event */
$event = \core\event\webservice_function_called::create($params);
$event->trigger();
// Handle the SOAP request.
$this->handle();
// Session cleanup.
$this->session_cleanup();
die;
}
/**
* Generates the WSDL.
*/
protected function generate_wsdl() {
// Initialise WSDL.
$this->wsdl = new wsdl($this->serviceclass, $this->serverurl);
// Register service struct classes as complex types.
foreach ($this->servicestructs as $structinfo) {
$this->wsdl->add_complex_type($structinfo->classname, $structinfo->properties);
}
// Register the method for the WSDL generation.
foreach ($this->servicemethods as $methodinfo) {
$this->wsdl->register($methodinfo->name, $methodinfo->inputparams, $methodinfo->outputparams, $methodinfo->description);
}
}
/**
* Handles the web service function call.
*/
protected function handle() {
if ($this->wsdlmode) {
// Prepare the response.
$this->response = $this->wsdl->to_xml();
// Send the results back in correct format.
$this->send_response();
} else {
$wsdlurl = clone($this->serverurl);
$wsdlurl->param('wsdl', 1);
$options = array(
'uri' => $this->serverurl->out(false)
);
// Initialise the SOAP server.
$this->soapserver = new SoapServer($wsdlurl->out(false), $options);
if (!empty($this->serviceclass)) {
$this->soapserver->setClass($this->serviceclass);
// Get all the methods for the generated service class then register to the SOAP server.
$functions = get_class_methods($this->serviceclass);
$this->soapserver->addFunction($functions);
}
// Get soap request from raw POST data.
$soaprequest = file_get_contents('php://input');
// Handle the request.
try {
$this->soapserver->handle($soaprequest);
} catch (Exception $e) {
$this->fault($e);
}
}
}
/**
* Send the error information to the WS client formatted as an XML document.
*
* @param Exception $ex the exception to send back
*/
protected function send_error($ex = null) {
if ($ex) {
$info = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
$info .= ' - '.$ex->debuginfo;
}
} else {
$info = 'Unknown error';
}
// Initialise new DOM document object.
$dom = new DOMDocument('1.0', 'UTF-8');
// Fault node.
$fault = $dom->createElement('SOAP-ENV:Fault');
// Faultcode node.
$fault->appendChild($dom->createElement('faultcode', 'MOODLE:error'));
// Faultstring node.
$fault->appendChild($dom->createElement('faultstring', $info));
// Body node.
$body = $dom->createElement('SOAP-ENV:Body');
$body->appendChild($fault);
// Envelope node.
$envelope = $dom->createElement('SOAP-ENV:Envelope');
$envelope->setAttribute('xmlns:SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/');
$envelope->appendChild($body);
$dom->appendChild($envelope);
$this->response = $dom->saveXML();
$this->send_response();
}
/**
* Send the result of function call to the WS client.
*/
protected function send_response() {
$this->send_headers();
echo $this->response;
}
/**
* Internal implementation - sending of page headers.
*/
protected function send_headers() {
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
header('Pragma: no-cache');
header('Accept-Ranges: none');
header('Content-Length: ' . strlen($this->response));
header('Content-Type: application/xml; charset=utf-8');
header('Content-Disposition: inline; filename="response.xml"');
}
/**
* Generate a server fault.
*
* Note that the parameter order is the reverse of SoapFault's constructor parameters.
*
* Moodle note: basically we return the faultactor (errorcode) and faultdetails (debuginfo).
*
* If an exception is passed as the first argument, its message and code
* will be used to create the fault object.
*
* @link http://www.w3.org/TR/soap12-part1/#faultcodes
* @param string|Exception $fault
* @param string $code SOAP Fault Codes
*/
public function fault($fault = null, $code = 'Receiver') {
$allowedfaultmodes = array(
'VersionMismatch', 'MustUnderstand', 'DataEncodingUnknown',
'Sender', 'Receiver', 'Server'
);
if (!in_array($code, $allowedfaultmodes)) {
$code = 'Receiver';
}
// Intercept any exceptions and add the errorcode and debuginfo (optional).
$actor = null;
$details = null;
$errorcode = 'unknownerror';
$message = get_string($errorcode);
if ($fault instanceof Exception) {
// Add the debuginfo to the exception message if debuginfo must be returned.
$actor = isset($fault->errorcode) ? $fault->errorcode : null;
$errorcode = $actor;
if (debugging()) {
$message = $fault->getMessage();
$details = isset($fault->debuginfo) ? $fault->debuginfo : null;
}
} else if (is_string($fault)) {
$message = $fault;
}
$this->soapserver->fault($code, $message . ' | ERRORCODE: ' . $errorcode, $actor, $details);
}
}
/**
* SOAP test client class
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class webservice_soap_test_client implements webservice_test_client_interface {
/**
* Execute test client WS request
*
* @param string $serverurl server url (including token parameter or username/password parameters)
* @param string $function function name
* @param array $params parameters of the called function
* @return mixed
*/
public function simpletest($serverurl, $function, $params) {
global $CFG;
require_once($CFG->dirroot . '/webservice/soap/lib.php');
$client = new webservice_soap_client($serverurl);
return $client->call($function, $params);
}
}
+56
View File
@@ -0,0 +1,56 @@
<?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/>.
/**
* SOAP web service entry point. The authentication is done via tokens.
*
* @package webservice_soap
* @copyright 2009 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* NO_DEBUG_DISPLAY - disable moodle specific debug messages and any errors in output
*/
define('NO_DEBUG_DISPLAY', true);
define('WS_SERVER', true);
require('../../config.php');
require_once("$CFG->dirroot/webservice/soap/locallib.php");
if (!webservice_protocol_is_enabled('soap')) {
debugging('The server died because the web services or the SOAP protocol are not enable',
DEBUG_DEVELOPER);
die;
}
$server = new webservice_soap_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->run();
die;
/**
* Raises Early WS Exception in SOAP format.
*
* @param Exception $ex Raised exception.
*/
function raise_early_ws_exception(Exception $ex): void {
global $CFG;
require_once("$CFG->dirroot/webservice/soap/locallib.php");
$server = new webservice_soap_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->exception_handler($ex);
}
+44
View File
@@ -0,0 +1,44 @@
<?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/>.
/**
* SOAP web service entry point. The authentication is done via username/password.
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* NO_DEBUG_DISPLAY - disable moodle specific debug messages and any errors in output
*/
define('NO_DEBUG_DISPLAY', true);
define('WS_SERVER', true);
require('../../config.php');
require_once("$CFG->dirroot/webservice/soap/locallib.php");
if (!webservice_protocol_is_enabled('soap')) {
die;
}
$server = new webservice_soap_server(WEBSERVICE_AUTHMETHOD_USERNAME);
$server->run();
die;
+372
View File
@@ -0,0 +1,372 @@
<?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/>.
/**
* Unit tests for the WSDL class.
*
* @package webservice_soap
* @category test
* @copyright 2016 Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace webservice_soap;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/soap/classes/wsdl.php');
/**
* Unit tests for the WSDL class.
*
* @package webservice_soap
* @category test
* @copyright 2016 Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wsdl_test extends \advanced_testcase {
/**
* Test generated WSDL with no added complex types nor functions.
*/
public function test_minimum_wsdl(): void {
$this->resetAfterTest();
$serviceclass = 'testserviceclass';
$namespace = 'testnamespace';
$wsdl = new wsdl($serviceclass, $namespace);
// Test definitions node.
$definitions = new \SimpleXMLElement($wsdl->to_xml());
$defattrs = $definitions->attributes();
$this->assertEquals($serviceclass, $defattrs->name);
$this->assertEquals($namespace, $defattrs->targetNamespace);
// Test types node and attributes.
$this->assertNotNull($definitions->types);
$this->assertEquals($namespace, $definitions->types->children('xsd', true)->schema->attributes()->targetNamespace);
// Test portType node and attributes.
$this->assertNotNull($definitions->portType);
$this->assertEquals($serviceclass . wsdl::PORT, $definitions->portType->attributes()->name);
// Test binding node and attributes.
$this->assertNotNull($definitions->binding);
$this->assertEquals($serviceclass . wsdl::BINDING, $definitions->binding->attributes()->name);
$this->assertEquals('tns:' . $serviceclass . wsdl::PORT, $definitions->binding->attributes()->type);
$bindingattrs = $definitions->binding->children('soap', true)->binding->attributes();
$this->assertNotEmpty('rpc', $bindingattrs);
$this->assertEquals('rpc', $bindingattrs->style);
$this->assertEquals(wsdl::NS_SOAP_TRANSPORT, $bindingattrs->transport);
// Test service node.
$this->assertNotNull($definitions->service);
$this->assertEquals($serviceclass . wsdl::SERVICE, $definitions->service->attributes()->name);
$serviceport = $definitions->service->children()->port;
$this->assertNotEmpty($serviceport);
$this->assertEquals($serviceclass . wsdl::PORT, $serviceport->attributes()->name);
$this->assertEquals('tns:' . $serviceclass . wsdl::BINDING, $serviceport->attributes()->binding);
$serviceportaddress = $serviceport->children('soap', true)->address;
$this->assertNotEmpty($serviceportaddress);
$this->assertEquals($namespace, $serviceportaddress->attributes()->location);
}
/**
* Test output WSDL with complex type added.
*/
public function test_add_complex_type(): void {
$this->resetAfterTest();
$classname = 'testcomplextype';
$classattrs = array(
'doubleparam' => array(
'type' => 'double',
'nillable' => true
),
'stringparam' => array(
'type' => 'string',
'nillable' => true
),
'intparam' => array(
'type' => 'int',
'nillable' => true
),
'boolparam' => array(
'type' => 'int',
'nillable' => true
),
'classparam' => array(
'type' => 'teststruct'
),
'arrayparam' => array(
'type' => 'array',
'nillable' => true
),
);
$serviceclass = 'testserviceclass';
$namespace = 'testnamespace';
$wsdl = new wsdl($serviceclass, $namespace);
$wsdl->add_complex_type($classname, $classattrs);
$definitions = new \SimpleXMLElement($wsdl->to_xml());
// Test types node and attributes.
$this->assertNotNull($definitions->types);
$this->assertEquals($namespace, $definitions->types->children('xsd', true)->schema->attributes()->targetNamespace);
$complextype = $definitions->types->children('xsd', true)->schema->children('xsd', true);
$this->assertNotEmpty($complextype);
// Test the complex type's attributes.
foreach ($complextype->children('xsd', true)->all->children('xsd', true) as $element) {
foreach ($classattrs as $name => $options) {
if (strcmp($name, $element->attributes()->name) != 0) {
continue;
}
switch ($options['type']) {
case 'double':
case 'int':
case 'string':
$this->assertEquals('xsd:' . $options['type'], $element->attributes()->type);
break;
case 'array':
$this->assertEquals('soap-enc:' . ucfirst($options['type']), $element->attributes()->type);
break;
default:
$this->assertEquals('tns:' . $options['type'], $element->attributes()->type);
break;
}
if (!empty($options['nillable'])) {
$this->assertEquals('true', $element->attributes()->nillable);
}
break;
}
}
}
/**
* Test output WSDL when registering a web service function.
*/
public function test_register(): void {
$this->resetAfterTest();
$serviceclass = 'testserviceclass';
$namespace = 'testnamespace';
$wsdl = new wsdl($serviceclass, $namespace);
$functionname = 'testfunction';
$documentation = 'This is a test function';
$in = array(
'doubleparam' => array(
'type' => 'double'
),
'stringparam' => array(
'type' => 'string'
),
'intparam' => array(
'type' => 'int'
),
'boolparam' => array(
'type' => 'int'
),
'classparam' => array(
'type' => 'teststruct'
),
'arrayparam' => array(
'type' => 'array'
)
);
$out = array(
'doubleparam' => array(
'type' => 'double'
),
'stringparam' => array(
'type' => 'string'
),
'intparam' => array(
'type' => 'int'
),
'boolparam' => array(
'type' => 'int'
),
'classparam' => array(
'type' => 'teststruct'
),
'arrayparam' => array(
'type' => 'array'
),
'return' => array(
'type' => 'teststruct2'
)
);
$wsdl->register($functionname, $in, $out, $documentation);
$definitions = new \SimpleXMLElement($wsdl->to_xml());
// Test portType operation node.
$porttypeoperation = $definitions->portType->operation;
$this->assertEquals($documentation, $porttypeoperation->documentation);
$this->assertEquals('tns:' . $functionname . wsdl::IN, $porttypeoperation->input->attributes()->message);
$this->assertEquals('tns:' . $functionname . wsdl::OUT, $porttypeoperation->output->attributes()->message);
// Test binding operation nodes.
$bindingoperation = $definitions->binding->operation;
$soapoperation = $bindingoperation->children('soap', true)->operation;
$this->assertEquals($namespace . '#' . $functionname, $soapoperation->attributes()->soapAction);
$inputbody = $bindingoperation->input->children('soap', true);
$this->assertEquals('encoded', $inputbody->attributes()->use);
$this->assertEquals(wsdl::NS_SOAP_ENC, $inputbody->attributes()->encodingStyle);
$this->assertEquals($namespace, $inputbody->attributes()->namespace);
$outputbody = $bindingoperation->output->children('soap', true);
$this->assertEquals('encoded', $outputbody->attributes()->use);
$this->assertEquals(wsdl::NS_SOAP_ENC, $outputbody->attributes()->encodingStyle);
$this->assertEquals($namespace, $outputbody->attributes()->namespace);
// Test messages.
$messagein = $definitions->message[0];
$this->assertEquals($functionname . wsdl::IN, $messagein->attributes()->name);
foreach ($messagein->children() as $part) {
foreach ($in as $name => $options) {
if (strcmp($name, $part->attributes()->name) != 0) {
continue;
}
switch ($options['type']) {
case 'double':
case 'int':
case 'string':
$this->assertEquals('xsd:' . $options['type'], $part->attributes()->type);
break;
case 'array':
$this->assertEquals('soap-enc:' . ucfirst($options['type']), $part->attributes()->type);
break;
default:
$this->assertEquals('tns:' . $options['type'], $part->attributes()->type);
break;
}
break;
}
}
$messageout = $definitions->message[1];
$this->assertEquals($functionname . wsdl::OUT, $messageout->attributes()->name);
foreach ($messageout->children() as $part) {
foreach ($out as $name => $options) {
if (strcmp($name, $part->attributes()->name) != 0) {
continue;
}
switch ($options['type']) {
case 'double':
case 'int':
case 'string':
$this->assertEquals('xsd:' . $options['type'], $part->attributes()->type);
break;
case 'array':
$this->assertEquals('soap-enc:' . ucfirst($options['type']), $part->attributes()->type);
break;
default:
$this->assertEquals('tns:' . $options['type'], $part->attributes()->type);
break;
}
break;
}
}
}
/**
* Test output WSDL when registering a web service function with no input parameters.
*/
public function test_register_without_input(): void {
$this->resetAfterTest();
$serviceclass = 'testserviceclass';
$namespace = 'testnamespace';
$wsdl = new wsdl($serviceclass, $namespace);
$functionname = 'testfunction';
$documentation = 'This is a test function';
$out = array(
'return' => array(
'type' => 'teststruct2'
)
);
$wsdl->register($functionname, null, $out, $documentation);
$definitions = new \SimpleXMLElement($wsdl->to_xml());
// Test portType operation node.
$porttypeoperation = $definitions->portType->operation;
$this->assertEquals($documentation, $porttypeoperation->documentation);
$this->assertFalse(isset($porttypeoperation->input));
$this->assertTrue(isset($porttypeoperation->output));
// Test binding operation nodes.
$bindingoperation = $definitions->binding->operation;
// Confirm that there is no input node.
$this->assertFalse(isset($bindingoperation->input));
$this->assertTrue(isset($bindingoperation->output));
// Test messages.
// Assert there's only the output message node.
$this->assertEquals(1, count($definitions->message));
$messageout = $definitions->message[0];
$this->assertEquals($functionname . wsdl::OUT, $messageout->attributes()->name);
}
/**
* Test output WSDL when registering a web service function with no output parameters.
*/
public function test_register_without_output(): void {
$this->resetAfterTest();
$serviceclass = 'testserviceclass';
$namespace = 'testnamespace';
$wsdl = new wsdl($serviceclass, $namespace);
$functionname = 'testfunction';
$documentation = 'This is a test function';
$in = array(
'return' => array(
'type' => 'teststruct2'
)
);
$wsdl->register($functionname, $in, null, $documentation);
$definitions = new \SimpleXMLElement($wsdl->to_xml());
// Test portType operation node.
$porttypeoperation = $definitions->portType->operation;
$this->assertEquals($documentation, $porttypeoperation->documentation);
$this->assertTrue(isset($porttypeoperation->input));
$this->assertFalse(isset($porttypeoperation->output));
// Test binding operation nodes.
$bindingoperation = $definitions->binding->operation;
// Confirm that there is no input node.
$this->assertTrue(isset($bindingoperation->input));
$this->assertFalse(isset($bindingoperation->output));
// Test messages.
// Assert there's only the output message node.
$this->assertEquals(1, count($definitions->message));
$messagein = $definitions->message[0];
$this->assertEquals($functionname . wsdl::IN, $messagein->attributes()->name);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Version details
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'webservice_soap'; // Full name of the plugin (used for diagnostics)