PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/webservice/xmlrpc/locallib.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 229 lines | 103 code | 26 blank | 100 comment | 8 complexity | 4bf4c3514ac2dc17e67a84a3ccf63a7b MD5 | raw file
  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.
  13. //
  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/>.
  16. /**
  17. * XML-RPC web service implementation classes and methods.
  18. *
  19. * @package webservice_xmlrpc
  20. * @copyright 2009 Petr Skodak
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once("$CFG->dirroot/webservice/lib.php");
  24. /**
  25. * XML-RPC service server implementation.
  26. *
  27. * @package webservice_xmlrpc
  28. * @copyright 2009 Petr Skodak
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. * @since Moodle 2.0
  31. */
  32. class webservice_xmlrpc_server extends webservice_base_server {
  33. /** @var string $response The XML-RPC response string. */
  34. private $response;
  35. /**
  36. * Contructor
  37. *
  38. * @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
  39. */
  40. public function __construct($authmethod) {
  41. parent::__construct($authmethod);
  42. $this->wsname = 'xmlrpc';
  43. }
  44. /**
  45. * This method parses the request input, it needs to get:
  46. * 1/ user authentication - username+password or token
  47. * 2/ function name
  48. * 3/ function parameters
  49. */
  50. protected function parse_request() {
  51. // Retrieve and clean the POST/GET parameters from the parameters specific to the server.
  52. parent::set_web_service_call_settings();
  53. if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
  54. $this->username = isset($_GET['wsusername']) ? $_GET['wsusername'] : null;
  55. $this->password = isset($_GET['wspassword']) ? $_GET['wspassword'] : null;
  56. } else {
  57. $this->token = isset($_GET['wstoken']) ? $_GET['wstoken'] : null;
  58. }
  59. // Get the XML-RPC request data.
  60. $rawpostdata = $this->fetch_input_content();
  61. $methodname = null;
  62. // Decode the request to get the decoded parameters and the name of the method to be called.
  63. $decodedparams = xmlrpc_decode_request($rawpostdata, $methodname, 'UTF-8');
  64. $methodinfo = external_api::external_function_info($methodname);
  65. $methodparams = array_keys($methodinfo->parameters_desc->keys);
  66. $methodvariables = [];
  67. // Add the decoded parameters to the methodvariables array.
  68. if (is_array($decodedparams)) {
  69. foreach ($decodedparams as $index => $param) {
  70. // See MDL-53962 - XML-RPC requests will usually be sent as an array (as in, one with indicies).
  71. // We need to use a bit of "magic" to add the correct index back. Zend used to do this for us.
  72. $methodvariables[$methodparams[$index]] = $param;
  73. }
  74. }
  75. $this->functionname = $methodname;
  76. $this->parameters = $methodvariables;
  77. }
  78. /**
  79. * Fetch content from the client.
  80. *
  81. * @return string
  82. */
  83. protected function fetch_input_content() {
  84. return file_get_contents('php://input');
  85. }
  86. /**
  87. * Prepares the response.
  88. */
  89. protected function prepare_response() {
  90. try {
  91. if (!empty($this->function->returns_desc)) {
  92. $validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
  93. $encodingoptions = array(
  94. "encoding" => "UTF-8",
  95. "verbosity" => "no_white_space",
  96. // See MDL-54868.
  97. "escaping" => ["markup"]
  98. );
  99. // We can now convert the response to the requested XML-RPC format.
  100. $this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions);
  101. }
  102. } catch (invalid_response_exception $ex) {
  103. $this->response = $this->generate_error($ex);
  104. }
  105. }
  106. /**
  107. * Send the result of function call to the WS client.
  108. */
  109. protected function send_response() {
  110. $this->prepare_response();
  111. $this->send_headers();
  112. echo $this->response;
  113. }
  114. /**
  115. * Send the error information to the WS client.
  116. *
  117. * @param Exception $ex
  118. */
  119. protected function send_error($ex = null) {
  120. $this->send_headers();
  121. echo $this->generate_error($ex);
  122. }
  123. /**
  124. * Sends the headers for the XML-RPC response.
  125. */
  126. protected function send_headers() {
  127. // Standard headers.
  128. header('HTTP/1.1 200 OK');
  129. header('Connection: close');
  130. header('Content-Length: ' . strlen($this->response));
  131. header('Content-Type: text/xml; charset=utf-8');
  132. header('Date: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
  133. header('Server: Moodle XML-RPC Server/1.0');
  134. // Other headers.
  135. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  136. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  137. header('Pragma: no-cache');
  138. header('Accept-Ranges: none');
  139. // Allow cross-origin requests only for Web Services.
  140. // This allow to receive requests done by Web Workers or webapps in different domains.
  141. header('Access-Control-Allow-Origin: *');
  142. }
  143. /**
  144. * Generate the XML-RPC fault response.
  145. *
  146. * @param Exception|Throwable $ex The exception.
  147. * @param int $faultcode The faultCode to be included in the fault response
  148. * @return string The XML-RPC fault response xml containing the faultCode and faultString.
  149. */
  150. protected function generate_error($ex, $faultcode = 404) {
  151. $error = $ex->getMessage();
  152. if (!empty($ex->errorcode)) {
  153. // The faultCode must be an int, so we obtain a hash of the errorcode then get an integer value of the hash.
  154. $faultcode = base_convert(md5($ex->errorcode), 16, 10);
  155. // We strip the $code to 8 digits (and hope for no error code collisions).
  156. // Collisions should be pretty rare, and if needed the client can retrieve
  157. // the accurate errorcode from the last | in the exception message.
  158. $faultcode = substr($faultcode, 0, 8);
  159. // Add the debuginfo to the exception message if debuginfo must be returned.
  160. if (debugging() and isset($ex->debuginfo)) {
  161. $error .= ' | DEBUG INFO: ' . $ex->debuginfo . ' | ERRORCODE: ' . $ex->errorcode;
  162. } else {
  163. $error .= ' | ERRORCODE: ' . $ex->errorcode;
  164. }
  165. }
  166. $fault = array(
  167. 'faultCode' => (int) $faultcode,
  168. 'faultString' => $error
  169. );
  170. $encodingoptions = array(
  171. "encoding" => "UTF-8",
  172. "verbosity" => "no_white_space",
  173. // See MDL-54868.
  174. "escaping" => ["markup"]
  175. );
  176. return xmlrpc_encode_request(null, $fault, $encodingoptions);
  177. }
  178. }
  179. /**
  180. * XML-RPC test client class
  181. *
  182. * @package webservice_xmlrpc
  183. * @copyright 2009 Petr Skodak
  184. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  185. * @since Moodle 2.0
  186. */
  187. class webservice_xmlrpc_test_client implements webservice_test_client_interface {
  188. /**
  189. * Execute test client WS request
  190. * @param string $serverurl server url (including token parameter or username/password parameters)
  191. * @param string $function function name
  192. * @param array $params parameters of the called function
  193. * @return mixed
  194. */
  195. public function simpletest($serverurl, $function, $params) {
  196. global $CFG;
  197. $url = new moodle_url($serverurl);
  198. $token = $url->get_param('wstoken');
  199. require_once($CFG->dirroot . '/webservice/xmlrpc/lib.php');
  200. $client = new webservice_xmlrpc_client($serverurl, $token);
  201. return $client->call($function, $params);
  202. }
  203. }