Commit 79c7fffc authored by jun's avatar jun
Browse files

MDL-52208 webservice_soap: Remove Zend from webservice_soap

parent 2f45a11a
......@@ -1580,6 +1580,12 @@ abstract class webservice_base_server extends webservice_server {
/** @var mixed Function return value */
protected $returns = null;
/** @var array List of methods and their information provided by the web service. */
protected $servicemethods;
/** @var array List of struct classes generated for the web service methods. */
protected $servicestructs;
/**
* This method parses the request input, it needs to get:
* 1/ user authentication - username+password or token
......@@ -1778,6 +1784,317 @@ abstract class webservice_base_server extends webservice_server {
// execute - yay!
$this->returns = call_user_func_array(array($this->function->classname, $this->function->methodname), array_values($params));
}
/**
* Load the virtual class needed for the web service.
*
* Initialises the virtual class that contains the web service functions that the user is allowed to use.
* The web service function will be available if the user:
* - is validly registered in the external_services_users table.
* - has the required capability.
* - meets the IP restriction requirement.
* This virtual class can be used by web service protocols such as SOAP, especially when generating WSDL.
* NOTE: The implementation of this method has been mostly copied from webservice_zend_server::init_server_class().
*/
protected function init_service_class() {
global $USER, $DB;
// Initialise service methods and struct classes.
$this->servicemethods = array();
$this->servicestructs = array();
$params = array();
$wscond1 = '';
$wscond2 = '';
if ($this->restricted_serviceid) {
$params = array('sid1' => $this->restricted_serviceid, 'sid2' => $this->restricted_serviceid);
$wscond1 = 'AND s.id = :sid1';
$wscond2 = 'AND s.id = :sid2';
}
$sql = "SELECT s.*, NULL AS iprestriction
FROM {external_services} s
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 0)
WHERE s.enabled = 1 $wscond1
UNION
SELECT s.*, su.iprestriction
FROM {external_services} s
JOIN {external_services_functions} sf ON (sf.externalserviceid = s.id AND s.restrictedusers = 1)
JOIN {external_services_users} su ON (su.externalserviceid = s.id AND su.userid = :userid)
WHERE s.enabled = 1 AND (su.validuntil IS NULL OR su.validuntil < :now) $wscond2";
$params = array_merge($params, array('userid' => $USER->id, 'now' => time()));
$serviceids = array();
$remoteaddr = getremoteaddr();
// Query list of external services for the user.
$rs = $DB->get_recordset_sql($sql, $params);
// Check which service ID to include.
foreach ($rs as $service) {
if (isset($serviceids[$service->id])) {
continue; // Service already added.
}
if ($service->requiredcapability and !has_capability($service->requiredcapability, $this->restricted_context)) {
continue; // Cap required, sorry.
}
if ($service->iprestriction and !address_in_subnet($remoteaddr, $service->iprestriction)) {
continue; // Wrong request source ip, sorry.
}
$serviceids[$service->id] = $service->id;
}
$rs->close();
// Generate the virtual class name.
$classname = 'webservices_virtual_class_000000';
while (class_exists($classname)) {
$classname++;
}
$this->serviceclass = $classname;
// Get the list of all available external functions.
$wsmanager = new webservice();
$functions = $wsmanager->get_external_functions($serviceids);
// Generate code for the virtual methods for this web service.
$methods = '';
foreach ($functions as $function) {
$methods .= $this->get_virtual_method_code($function);
}
$code = <<<EOD
/**
* Virtual class web services for user id $USER->id in context {$this->restricted_context->id}.
*/
class $classname {
$methods
}
EOD;
// Load the virtual class definition into memory.
eval($code);
}
/**
* Generates a struct class.
*
* NOTE: The implementation of this method has been mostly copied from webservice_zend_server::generate_simple_struct_class().
* @param external_single_structure $structdesc The basis of the struct class to be generated.
* @return string The class name of the generated struct class.
*/
protected function generate_simple_struct_class(external_single_structure $structdesc) {
global $USER;
$propeties = array();
$fields = array();
foreach ($structdesc->keys as $name => $fieldsdesc) {
$type = $this->get_phpdoc_type($fieldsdesc);
$propertytype = array('type' => $type);
if (empty($fieldsdesc->allownull) || $fieldsdesc->allownull == NULL_ALLOWED) {
$propertytype['nillable'] = true;
}
$propeties[$name] = $propertytype;
$fields[] = ' /** @var ' . $type . ' $' . $name . '*/';
$fields[] = ' public $' . $name .';';
}
$fieldsstr = implode("\n", $fields);
// We do this after the call to get_phpdoc_type() to avoid duplicate class creation.
$classname = 'webservices_struct_class_000000';
while (class_exists($classname)) {
$classname++;
}
$code = <<<EOD
/**
* Virtual struct class for web services for user id $USER->id in context {$this->restricted_context->id}.
*/
class $classname {
$fieldsstr
}
EOD;
// Load into memory.
eval($code);
// Prepare struct info.
$structinfo = new stdClass();
$structinfo->classname = $classname;
$structinfo->properties = $propeties;
// Add the struct info the the list of service struct classes.
$this->servicestructs[] = $structinfo;
return $classname;
}
/**
* Returns a virtual method code for a web service function.
*
* NOTE: The implementation of this method has been mostly copied from webservice_zend_server::get_virtual_method_code().
* @param stdClass $function a record from external_function
* @return string The PHP code of the virtual method.
* @throws coding_exception
* @throws moodle_exception
*/
protected function get_virtual_method_code($function) {
$function = external_function_info($function);
// Parameters and their defaults for the method signature.
$paramanddefaults = array();
// Parameters for external lib call.
$params = array();
$paramdesc = array();
// The method's input parameters and their respective types.
$inputparams = array();
// The method's output parameters and their respective types.
$outputparams = array();
foreach ($function->parameters_desc->keys as $name => $keydesc) {
$param = '$' . $name;
$paramanddefault = $param;
if ($keydesc->required == VALUE_OPTIONAL) {
// It does not make sense to declare a parameter VALUE_OPTIONAL. VALUE_OPTIONAL is used only for array/object key.
throw new moodle_exception('erroroptionalparamarray', 'webservice', '', $name);
} else if ($keydesc->required == VALUE_DEFAULT) {
// Need to generate the default, if there is any.
if ($keydesc instanceof external_value) {
if ($keydesc->default === null) {
$paramanddefault .= ' = null';
} else {
switch ($keydesc->type) {
case PARAM_BOOL:
$default = (int)$keydesc->default;
break;
case PARAM_INT:
$default = $keydesc->default;
break;
case PARAM_FLOAT;
$default = $keydesc->default;
break;
default:
$default = "'$keydesc->default'";
}
$paramanddefault .= " = $default";
}
} else {
// Accept empty array as default.
if (isset($keydesc->default) && is_array($keydesc->default) && empty($keydesc->default)) {
$paramanddefault .= ' = array()';
} else {
// For the moment we do not support default for other structure types.
throw new moodle_exception('errornotemptydefaultparamarray', 'webservice', '', $name);
}
}
}
$params[] = $param;
$paramanddefaults[] = $paramanddefault;
$type = $this->get_phpdoc_type($keydesc);
$inputparams[$name]['type'] = $type;
$paramdesc[] = '* @param ' . $type . ' $' . $name . ' ' . $keydesc->desc;
}
$paramanddefaults = implode(', ', $paramanddefaults);
$paramdescstr = implode("\n ", $paramdesc);
$serviceclassmethodbody = $this->service_class_method_body($function, $params);
if (empty($function->returns_desc)) {
$return = '* @return void';
} else {
$type = $this->get_phpdoc_type($function->returns_desc);
$outputparams['return']['type'] = $type;
$return = '* @return ' . $type . ' ' . $function->returns_desc->desc;
}
// Now create the virtual method that calls the ext implementation.
$code = <<<EOD
/**
* $function->description.
*
$paramdescstr
$return
*/
public function $function->name($paramanddefaults) {
$serviceclassmethodbody
}
EOD;
// Prepare the method information.
$methodinfo = new stdClass();
$methodinfo->name = $function->name;
$methodinfo->inputparams = $inputparams;
$methodinfo->outputparams = $outputparams;
$methodinfo->description = $function->description;
// Add the method information into the list of service methods.
$this->servicemethods[] = $methodinfo;
return $code;
}
/**
* Get the phpdoc type for an external_description object.
* external_value => int, double or string
* external_single_structure => object|struct, on-fly generated stdClass name.
* external_multiple_structure => array
*
* @param mixed $keydesc The type description.
* @return string The PHP doc type of the external_description object.
*/
protected function get_phpdoc_type($keydesc) {
$type = null;
if ($keydesc instanceof external_value) {
switch ($keydesc->type) {
case PARAM_BOOL: // 0 or 1 only for now.
case PARAM_INT:
$type = 'int';
break;
case PARAM_FLOAT;
$type = 'double';
break;
default:
$type = 'string';
}
} else if ($keydesc instanceof external_single_structure) {
$type = $this->generate_simple_struct_class($keydesc);
} else if ($keydesc instanceof external_multiple_structure) {
$type = 'array';
}
return $type;
}
/**
* Generates the method body of the virtual external function.
*
* NOTE: The implementation of this method has been mostly copied from webservice_zend_server::service_class_method_body().
* @param stdClass $function a record from external_function.
* @param array $params web service function parameters.
* @return string body of the method for $function ie. everything within the {} of the method declaration.
*/
protected function service_class_method_body($function, $params) {
// Cast the param from object to array (validate_parameters except array only).
$castingcode = '';
$paramsstr = '';
if (!empty($params)) {
foreach ($params as $paramtocast) {
// Clean the parameter from any white space.
$paramtocast = trim($paramtocast);
$castingcode .= " $paramtocast = json_decode(json_encode($paramtocast), true);\n";
}
$paramsstr = implode(', ', $params);
}
$descriptionmethod = $function->methodname . '_returns()';
$callforreturnvaluedesc = $function->classname . '::' . $descriptionmethod;
$methodbody = <<<EOD
$castingcode
if ($callforreturnvaluedesc == null) {
$function->classname::$function->methodname($paramsstr);
return null;
}
return external_api::clean_returnvalue($callforreturnvaluedesc, $function->classname::$function->methodname($paramsstr));
EOD;
return $methodbody;
}
}
<?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']));
}
}
}
......@@ -14,7 +14,6 @@
// 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
*
......@@ -23,8 +22,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once 'Zend/Soap/Client.php';
/**
* Moodle SOAP client
*
......@@ -34,11 +31,17 @@ require_once 'Zend/Soap/Client.php';
* @copyright 2010 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_soap_client extends Zend_Soap_Client {
class webservice_soap_client {
/** @var string server url e.g. https://yyyyy.com/server.php */
/** @var moodle_url The server url. */
private $serverurl;
/** @var string The WS token. */