MDL-55859 assign_editpdf: cleanup htmlfile in case of errors
[moodle.git] / webservice / xmlrpc / locallib.php
blobf6334bebb42a3c11ce5ba7f9612464cb48b6d0d0
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
18 /**
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");
28 /**
29 * XML-RPC service server implementation.
31 * @package webservice_xmlrpc
32 * @copyright 2009 Petr Skodak
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 * @since Moodle 2.0
36 class webservice_xmlrpc_server extends webservice_base_server {
38 /** @var string $response The XML-RPC response string. */
39 private $response;
41 /**
42 * Contructor
44 * @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
46 public function __construct($authmethod) {
47 parent::__construct($authmethod);
48 $this->wsname = 'xmlrpc';
51 /**
52 * This method parses the request input, it needs to get:
53 * 1/ user authentication - username+password or token
54 * 2/ function name
55 * 3/ function parameters
57 protected function parse_request() {
58 // Retrieve and clean the POST/GET parameters from the parameters specific to the server.
59 parent::set_web_service_call_settings();
61 if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
62 $this->username = isset($_GET['wsusername']) ? $_GET['wsusername'] : null;
63 $this->password = isset($_GET['wspassword']) ? $_GET['wspassword'] : null;
64 } else {
65 $this->token = isset($_GET['wstoken']) ? $_GET['wstoken'] : null;
68 // Get the XML-RPC request data.
69 $rawpostdata = $this->fetch_input_content();
70 $methodname = null;
72 // Decode the request to get the decoded parameters and the name of the method to be called.
73 $decodedparams = xmlrpc_decode_request($rawpostdata, $methodname, 'UTF-8');
74 $methodinfo = external_api::external_function_info($methodname);
75 $methodparams = array_keys($methodinfo->parameters_desc->keys);
76 $methodvariables = [];
78 // Add the decoded parameters to the methodvariables array.
79 if (is_array($decodedparams)) {
80 foreach ($decodedparams as $index => $param) {
81 // See MDL-53962 - XML-RPC requests will usually be sent as an array (as in, one with indicies).
82 // We need to use a bit of "magic" to add the correct index back. Zend used to do this for us.
83 $methodvariables[$methodparams[$index]] = $param;
87 $this->functionname = $methodname;
88 $this->parameters = $methodvariables;
91 /**
92 * Fetch content from the client.
94 * @return string
96 protected function fetch_input_content() {
97 return file_get_contents('php://input');
101 * Prepares the response.
103 protected function prepare_response() {
104 try {
105 if (!empty($this->function->returns_desc)) {
106 $validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
107 $encodingoptions = array(
108 "encoding" => "UTF-8",
109 "verbosity" => "no_white_space",
110 // See MDL-54868.
111 "escaping" => ["markup"]
113 // We can now convert the response to the requested XML-RPC format.
114 $this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions);
116 } catch (invalid_response_exception $ex) {
117 $this->response = $this->generate_error($ex);
122 * Send the result of function call to the WS client.
124 protected function send_response() {
125 $this->prepare_response();
126 $this->send_headers();
127 echo $this->response;
131 * Send the error information to the WS client.
133 * @param Exception $ex
135 protected function send_error($ex = null) {
136 $this->send_headers();
137 echo $this->generate_error($ex);
141 * Sends the headers for the XML-RPC response.
143 protected function send_headers() {
144 // Standard headers.
145 header('HTTP/1.1 200 OK');
146 header('Connection: close');
147 header('Content-Length: ' . strlen($this->response));
148 header('Content-Type: text/xml; charset=utf-8');
149 header('Date: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
150 header('Server: Moodle XML-RPC Server/1.0');
151 // Other headers.
152 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
153 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
154 header('Pragma: no-cache');
155 header('Accept-Ranges: none');
156 // Allow cross-origin requests only for Web Services.
157 // This allow to receive requests done by Web Workers or webapps in different domains.
158 header('Access-Control-Allow-Origin: *');
162 * Generate the XML-RPC fault response.
164 * @param Exception $ex The exception.
165 * @param int $faultcode The faultCode to be included in the fault response
166 * @return string The XML-RPC fault response xml containing the faultCode and faultString.
168 protected function generate_error(Exception $ex, $faultcode = 404) {
169 $error = $ex->getMessage();
171 if (!empty($ex->errorcode)) {
172 // The faultCode must be an int, so we obtain a hash of the errorcode then get an integer value of the hash.
173 $faultcode = base_convert(md5($ex->errorcode), 16, 10);
175 // We strip the $code to 8 digits (and hope for no error code collisions).
176 // Collisions should be pretty rare, and if needed the client can retrieve
177 // the accurate errorcode from the last | in the exception message.
178 $faultcode = substr($faultcode, 0, 8);
180 // Add the debuginfo to the exception message if debuginfo must be returned.
181 if (debugging() and isset($ex->debuginfo)) {
182 $error .= ' | DEBUG INFO: ' . $ex->debuginfo . ' | ERRORCODE: ' . $ex->errorcode;
183 } else {
184 $error .= ' | ERRORCODE: ' . $ex->errorcode;
188 $fault = array(
189 'faultCode' => (int) $faultcode,
190 'faultString' => $error
193 $encodingoptions = array(
194 "encoding" => "UTF-8",
195 "verbosity" => "no_white_space",
196 // See MDL-54868.
197 "escaping" => ["markup"]
200 return xmlrpc_encode_request(null, $fault, $encodingoptions);
205 * XML-RPC test client class
207 * @package webservice_xmlrpc
208 * @copyright 2009 Petr Skodak
209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
210 * @since Moodle 2.0
212 class webservice_xmlrpc_test_client implements webservice_test_client_interface {
214 * Execute test client WS request
215 * @param string $serverurl server url (including token parameter or username/password parameters)
216 * @param string $function function name
217 * @param array $params parameters of the called function
218 * @return mixed
220 public function simpletest($serverurl, $function, $params) {
221 global $CFG;
223 $url = new moodle_url($serverurl);
224 $token = $url->get_param('wstoken');
225 require_once($CFG->dirroot . '/webservice/xmlrpc/lib.php');
226 $client = new webservice_xmlrpc_client($serverurl, $token);
227 return $client->call($function, $params);