Skip to content

Commit

Permalink
MDL-52209 webservice_xmlrpc: Remove Zend from webservice_xmlrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Dec 4, 2015
1 parent c18acb8 commit 0af5593
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 87 deletions.
72 changes: 40 additions & 32 deletions webservice/xmlrpc/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once 'Zend/XmlRpc/Client.php';

/**
* Moodle XML-RPC client
*
Expand All @@ -34,10 +32,13 @@
* @copyright 2010 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_xmlrpc_client extends Zend_XmlRpc_Client {
class webservice_xmlrpc_client {

/** @var moodle_url The XML-RPC server url. */
protected $serverurl;

/** @var string server url e.g. https://yyyyy.com/server.php */
private $serverurl;
/** @var string The token for the XML-RPC call. */
protected $token;

/**
* Constructor
Expand All @@ -46,22 +47,8 @@ class webservice_xmlrpc_client extends Zend_XmlRpc_Client {
* @param string $token the token used to do the web service call
*/
public function __construct($serverurl, $token) {
global $CFG;
$this->serverurl = $serverurl;
$serverurl = $serverurl . '?wstoken=' . $token;
parent::__construct($serverurl);
if (!empty($CFG->proxyhost) && !is_proxybypass($serverurl)) {
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Proxy',
'proxy_host' => $CFG->proxyhost,
'proxy_user' => !empty($CFG->proxyuser) ? $CFG->proxyuser : null,
'proxy_pass' => !empty($CFG->proxypassword) ? $CFG->proxypassword : null
);
if (!empty($CFG->proxyport)) {
$config['proxy_port'] = $CFG->proxyport;
}
$this->getHttpClient()->setConfig($config);
}
$this->serverurl = new moodle_url($serverurl);
$this->token = $token;
}

/**
Expand All @@ -70,26 +57,47 @@ public function __construct($serverurl, $token) {
* @param string $token the token used to do the web service call
*/
public function set_token($token) {
$this->_serverAddress = $this->serverurl . '?wstoken=' . $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
* @param array $params An associative array containing the the parameters of the function being called.
* @return mixed The decoded XML RPC response.
* @throws moodle_exception
*/
public function call($functionname, $params=array()) {
global $DB, $CFG;
public function call($functionname, $params = array()) {
if ($this->token) {
$this->serverurl->param('wstoken', $this->token);
}

// Set output options.
$outputoptions = array(
'encoding' => 'utf-8'
);

//zend expects 0 based array with numeric indexes
$params = array_values($params);
// Encode the request.
$request = xmlrpc_encode_request($functionname, $params, $outputoptions);

//traditional Zend soap client call (integrating the token into the URL)
$result = parent::call($functionname, $params);
// Set the headers.
$headers = array(
'Content-Length' => strlen($request),
'Content-Type' => 'text/xml; charset=utf-8',
'Host' => $this->serverurl->get_host(),
'User-Agent' => 'Moodle XML-RPC Client/1.0',
);

// Get the response.
$response = download_file_content($this->serverurl, $headers, $request);

// Decode the response.
$result = xmlrpc_decode($response);
if (is_array($result) && xmlrpc_is_fault($result)) {
throw new moodle_exception($result['faultString']);
}

return $result;
}

}
}
228 changes: 173 additions & 55 deletions webservice/xmlrpc/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,89 +24,206 @@
*/

require_once("$CFG->dirroot/webservice/lib.php");
require_once 'Zend/XmlRpc/Server.php';

/**
* The Zend XMLRPC server but with a fault that return debuginfo
* XML-RPC service server implementation.
*
* @package webservice_xmlrpc
* @copyright 2011 Jerome Mouneyrac
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
* @since Moodle 2.0
*/
class moodle_zend_xmlrpc_server extends Zend_XmlRpc_Server {
class webservice_xmlrpc_server extends webservice_base_server {

/** @var string $response The XML-RPC response string. */
private $response;

/**
* Raise an xmlrpc server fault
* Contructor
*
* Moodle note: the difference with the Zend server is that we throw a plain PHP Exception
* with the debuginfo integrated to the exception message when DEBUG >= NORMAL
* @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
*/
public function __construct($authmethod) {
parent::__construct($authmethod);
$this->wsname = 'xmlrpc';
}

/**
* This method parses the request input, it needs to get:
* 1/ user authentication - username+password or token
* 2/ function name
* 3/ function parameters
*/
protected function parse_request() {
// Retrieve and clean the POST/GET parameters from the parameters specific to the server.
parent::set_web_service_call_settings();

// Get GET and POST parameters.
$methodvariables = array_merge($_GET, $_POST);

if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
$this->username = isset($methodvariables['wsusername']) ? $methodvariables['wsusername'] : null;
unset($methodvariables['wsusername']);

$this->password = isset($methodvariables['wspassword']) ? $methodvariables['wspassword'] : null;
unset($methodvariables['wspassword']);
} else {
$this->token = isset($methodvariables['wstoken']) ? $methodvariables['wstoken'] : null;
unset($methodvariables['wstoken']);
}

// Get the XML-RPC request data.
$rawpostdata = file_get_contents("php://input");
$methodname = null;

// Decode the request to get the decoded parameters and the name of the method to be called.
$decodedparams = xmlrpc_decode_request($rawpostdata, $methodname);

// Add the decoded parameters to the methodvariables array.
if (is_array($decodedparams)) {
foreach ($decodedparams as $param) {
// Check if decoded param is an associative array.
if (is_array($param) && array_keys($param) !== range(0, count($param) - 1)) {
$methodvariables = array_merge($methodvariables, $param);
} else {
$methodvariables[] = $param;
}
}
}

$this->functionname = $methodname;
$this->parameters = $methodvariables;
}

/**
* Prepares the response.
*/
protected function prepare_response() {
try {
if (!empty($this->function->returns_desc)) {
$validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
$encodingoptions = array(
"encoding" => "utf-8"
);
// We can now convert the response to the requested XML-RPC format.
$this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions);
}
} catch (invalid_response_exception $ex) {
$this->response = $this->generate_error($ex);
}
}

/**
* Send the result of function call to the WS client.
*/
protected function send_response() {
$this->prepare_response();
$this->send_headers();
echo $this->response;
}

/**
* Send the error information to the WS client.
*
* @param string|Exception $fault
* @param int $code
* @return Zend_XmlRpc_Server_Fault
* @param Exception $ex
*/
protected function send_error($ex = null) {
$this->send_headers();
echo $this->generate_error($ex);
}

/**
* Sends the headers for the XML-RPC response.
*/
public function fault($fault = null, $code = 404)
{
// Intercept any exceptions with debug info and transform it in Moodle exception.
if ($fault instanceof Exception) {
// Code php exception must be a long
// we obtain a hash of the errorcode, and then to get an integer hash.
$code = base_convert(md5($fault->errorcode), 16, 10);
// Code php exception being a long, it has a maximum number of digits.
// we strip the $code to 8 digits, and hope for no error code collisions.
protected function send_headers() {
// Standard headers.
header('HTTP/1.1 200 OK');
header('Connection: close');
header('Content-Length: ' . strlen($this->response));
header('Content-Type: text/xml; charset=utf-8');
header('Date: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
header('Server: Moodle XML-RPC Server/1.0');
// Other 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');
// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
header('Access-Control-Allow-Origin: *');
}

/**
* Generate the XML-RPC fault response.
*
* @param Exception $ex The exception.
* @param int $faultcode The faultCode to be included in the fault response
* @return string The XML-RPC fault response xml containing the faultCode and faultString.
*/
protected function generate_error(Exception $ex, $faultcode = 404) {
$error = $ex->getMessage();

if (!empty($ex->errorcode)) {
// The faultCode must be an int, so we obtain a hash of the errorcode then get an integer value of the hash.
$faultcode = base_convert(md5($ex->errorcode), 16, 10);

// We strip the $code to 8 digits (and hope for no error code collisions).
// Collisions should be pretty rare, and if needed the client can retrieve
// the accurate errorcode from the last | in the exception message.
$code = substr($code, 0, 8);
$faultcode = substr($faultcode, 0, 8);

// Add the debuginfo to the exception message if debuginfo must be returned.
if (debugging() and isset($fault->debuginfo)) {
$fault = new Exception($fault->getMessage() . ' | DEBUG INFO: ' . $fault->debuginfo
. ' | ERRORCODE: ' . $fault->errorcode, $code);
if (debugging() and isset($ex->debuginfo)) {
$error .= ' | DEBUG INFO: ' . $ex->debuginfo . ' | ERRORCODE: ' . $ex->errorcode;
} else {
$fault = new Exception($fault->getMessage()
. ' | ERRORCODE: ' . $fault->errorcode, $code);
$error .= ' | ERRORCODE: ' . $ex->errorcode;
}
}

return parent::fault($fault, $code);
$fault = array(
'faultCode' => (int) $faultcode,
'faultString' => $error
);

$encodingoptions = array(
"encoding" => "utf-8"
);

return xmlrpc_encode_request(null, $fault, $encodingoptions);
}
}

/**
* XML-RPC service server implementation.
* The Zend XMLRPC server but with a fault that return debuginfo.
*
* MDL-52209: Since Zend is being removed from Moodle, this class will be deprecated and eventually removed.
* Please use webservice_xmlrpc_server instead.
*
* @package webservice_xmlrpc
* @copyright 2009 Petr Skodak
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
* @since Moodle 2.2
* @deprecated since 3.1, see {@link webservice_xmlrpc_server}.
*/
class webservice_xmlrpc_server extends webservice_zend_server {
class moodle_zend_xmlrpc_server extends webservice_xmlrpc_server {

/**
* Contructor
* Raise an xmlrpc server fault.
*
* @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
* Moodle note: the difference with the Zend server is that we throw a plain PHP Exception
* with the debuginfo integrated to the exception message when DEBUG >= NORMAL.
*
* @param string|Exception $fault
* @param int $code
* @return Zend_XmlRpc_Server_Fault
* @deprecated since 3.1, see {@link webservice_xmlrpc_server::generate_error()}.
*/
public function __construct($authmethod) {
require_once 'Zend/XmlRpc/Server.php';
parent::__construct($authmethod, 'moodle_zend_xmlrpc_server');
$this->wsname = 'xmlrpc';
}
public function fault($fault = null, $code = 404) {
debugging('moodle_zend_xmlrpc_server::fault() is deprecated, please use ' .
'webservice_xmlrpc_server::generate_error() instead.', DEBUG_DEVELOPER);

/**
* Set up zend service class
*/
protected function init_zend_server() {
parent::init_zend_server();
// this exception indicates request failed
Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception');
//when DEBUG >= NORMAL then the thrown exceptions are "casted" into a plain PHP Exception class
//in order to display the $debuginfo (see moodle_zend_xmlrpc_server class - MDL-29435)
if (debugging()) {
Zend_XmlRpc_Server_Fault::attachFaultException('Exception');
}
return $this->generate_error($fault, $code);
}

}

/**
Expand All @@ -126,11 +243,12 @@ class webservice_xmlrpc_test_client implements webservice_test_client_interface
* @return mixed
*/
public function simpletest($serverurl, $function, $params) {
//zend expects 0 based array with numeric indexes
$params = array_values($params);
global $CFG;

require_once 'Zend/XmlRpc/Client.php';
$client = new Zend_XmlRpc_Client($serverurl);
$url = new moodle_url($serverurl);
$token = $url->get_param('wstoken');
require_once($CFG->dirroot . '/webservice/xmlrpc/lib.php');
$client = new webservice_xmlrpc_client($serverurl, $token);
return $client->call($function, $params);
}
}
Loading

0 comments on commit 0af5593

Please sign in to comment.