2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * XML-RPC web service implementation classes and methods.
21 * @package webservice_xmlrpc
22 * @copyright 2009 Petr Skodak
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->dirroot/webservice/lib.php");
27 require_once 'Zend/XmlRpc/Server.php';
30 * The Zend XMLRPC server but with a fault that return debuginfo
32 * @package webservice_xmlrpc
33 * @copyright 2011 Jerome Mouneyrac
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class moodle_zend_xmlrpc_server
extends Zend_XmlRpc_Server
{
40 * Raise an xmlrpc server fault
42 * Moodle note: the difference with the Zend server is that we throw a plain PHP Exception
43 * with the debuginfo integrated to the exception message when DEBUG >= NORMAL
45 * @param string|Exception $fault
47 * @return Zend_XmlRpc_Server_Fault
49 public function fault($fault = null, $code = 404)
51 // Intercept any exceptions with debug info and transform it in Moodle exception.
52 if ($fault instanceof Exception
) {
53 // Code php exception must be a long
54 // we obtain a hash of the errorcode, and then to get an integer hash.
55 $code = base_convert(md5($fault->errorcode
), 16, 10);
56 // Code php exception being a long, it has a maximum number of digits.
57 // we strip the $code to 8 digits, and hope for no error code collisions.
58 // Collisions should be pretty rare, and if needed the client can retrieve
59 // the accurate errorcode from the last | in the exception message.
60 $code = substr($code, 0, 8);
61 // Add the debuginfo to the exception message if debuginfo must be returned.
62 if (debugging() and isset($fault->debuginfo
)) {
63 $fault = new Exception($fault->getMessage() . ' | DEBUG INFO: ' . $fault->debuginfo
64 . ' | ERRORCODE: ' . $fault->errorcode
, $code);
66 $fault = new Exception($fault->getMessage()
67 . ' | ERRORCODE: ' . $fault->errorcode
, $code);
71 return parent
::fault($fault, $code);
76 * XML-RPC service server implementation.
78 * @package webservice_xmlrpc
79 * @copyright 2009 Petr Skodak
80 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
83 class webservice_xmlrpc_server
extends webservice_zend_server
{
88 * @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
90 public function __construct($authmethod) {
91 require_once 'Zend/XmlRpc/Server.php';
92 parent
::__construct($authmethod, 'moodle_zend_xmlrpc_server');
93 $this->wsname
= 'xmlrpc';
97 * Set up zend service class
99 protected function init_zend_server() {
100 parent
::init_zend_server();
101 // this exception indicates request failed
102 Zend_XmlRpc_Server_Fault
::attachFaultException('moodle_exception');
103 //when DEBUG >= NORMAL then the thrown exceptions are "casted" into a plain PHP Exception class
104 //in order to display the $debuginfo (see moodle_zend_xmlrpc_server class - MDL-29435)
106 Zend_XmlRpc_Server_Fault
::attachFaultException('Exception');
113 * XML-RPC test client class
115 * @package webservice_xmlrpc
116 * @copyright 2009 Petr Skodak
117 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
120 class webservice_xmlrpc_test_client
implements webservice_test_client_interface
{
122 * Execute test client WS request
123 * @param string $serverurl server url (including token parameter or username/password parameters)
124 * @param string $function function name
125 * @param array $params parameters of the called function
128 public function simpletest($serverurl, $function, $params) {
129 //zend expects 0 based array with numeric indexes
130 $params = array_values($params);
132 require_once 'Zend/XmlRpc/Client.php';
133 $client = new Zend_XmlRpc_Client($serverurl);
134 return $client->call($function, $params);