Moodle release 3.9.21
[moodle.git] / webservice / rest / locallib.php
blob49f0c6d1040be9a68b67abc82bb8738a975520f7
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 * REST web service implementation classes and methods.
21 * @package webservice_rest
22 * @copyright 2009 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->dirroot/webservice/lib.php");
28 /**
29 * REST service server implementation.
31 * @package webservice_rest
32 * @copyright 2009 Petr Skoda (skodak)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class webservice_rest_server extends webservice_base_server {
37 /** @var string return method ('xml' or 'json') */
38 protected $restformat;
40 /**
41 * Contructor
43 * @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
44 * @param string $restformat Format of the return values: 'xml' or 'json'
46 public function __construct($authmethod) {
47 parent::__construct($authmethod);
48 $this->wsname = 'rest';
51 /**
52 * Set the request format to.
54 public function set_rest_format(): void {
55 // Get GET and POST parameters.
56 $methodvariables = array_merge($_GET, $_POST);
58 // Retrieve REST format parameter - 'xml' (default) or 'json'.
59 $restformatisset = isset($methodvariables['moodlewsrestformat'])
60 && (($methodvariables['moodlewsrestformat'] == 'xml' || $methodvariables['moodlewsrestformat'] == 'json'));
61 $this->restformat = $restformatisset ? $methodvariables['moodlewsrestformat'] : 'xml';
64 /**
65 * This method parses the $_POST and $_GET superglobals and looks for
66 * the following information:
67 * 1/ user authentication - username+password or token (wsusername, wspassword and wstoken parameters)
68 * 2/ function name (wsfunction parameter)
69 * 3/ function parameters (all other parameters except those above)
70 * 4/ text format parameters
71 * 5/ return rest format xml/json
73 protected function parse_request() {
75 // Retrieve and clean the POST/GET parameters from the parameters specific to the server.
76 parent::set_web_service_call_settings();
78 // Get GET and POST parameters.
79 $methodvariables = array_merge($_GET, $_POST);
80 $this->set_rest_format();
81 unset($methodvariables['moodlewsrestformat']);
83 if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
84 $this->username = isset($methodvariables['wsusername']) ? $methodvariables['wsusername'] : null;
85 unset($methodvariables['wsusername']);
87 $this->password = isset($methodvariables['wspassword']) ? $methodvariables['wspassword'] : null;
88 unset($methodvariables['wspassword']);
90 $this->functionname = isset($methodvariables['wsfunction']) ? $methodvariables['wsfunction'] : null;
91 unset($methodvariables['wsfunction']);
93 $this->parameters = $methodvariables;
95 } else {
96 $this->token = isset($methodvariables['wstoken']) ? $methodvariables['wstoken'] : null;
97 unset($methodvariables['wstoken']);
99 $this->functionname = isset($methodvariables['wsfunction']) ? $methodvariables['wsfunction'] : null;
100 unset($methodvariables['wsfunction']);
102 $this->parameters = $methodvariables;
107 * Send the result of function call to the WS client
108 * formatted as XML document.
110 protected function send_response() {
112 //Check that the returned values are valid
113 try {
114 if ($this->function->returns_desc != null) {
115 $validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
116 } else {
117 $validatedvalues = null;
119 } catch (Exception $ex) {
120 $exception = $ex;
123 if (!empty($exception)) {
124 $response = $this->generate_error($exception);
125 } else {
126 //We can now convert the response to the requested REST format
127 if ($this->restformat == 'json') {
128 $response = json_encode($validatedvalues);
129 } else {
130 $response = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
131 $response .= '<RESPONSE>'."\n";
132 $response .= self::xmlize_result($validatedvalues, $this->function->returns_desc);
133 $response .= '</RESPONSE>'."\n";
137 $this->send_headers();
138 echo $response;
142 * Send the error information to the WS client
143 * formatted as XML document.
144 * Note: the exception is never passed as null,
145 * it only matches the abstract function declaration.
146 * @param exception $ex the exception that we are sending
148 protected function send_error($ex=null) {
149 $this->send_headers();
150 echo $this->generate_error($ex);
154 * Build the error information matching the REST returned value format (JSON or XML)
155 * @param exception $ex the exception we are converting in the server rest format
156 * @return string the error in the requested REST format
158 protected function generate_error($ex) {
159 if ($this->restformat == 'json') {
160 $errorobject = new stdClass;
161 $errorobject->exception = get_class($ex);
162 if (isset($ex->errorcode)) {
163 $errorobject->errorcode = $ex->errorcode;
165 $errorobject->message = $ex->getMessage();
166 if (debugging() and isset($ex->debuginfo)) {
167 $errorobject->debuginfo = $ex->debuginfo;
169 $error = json_encode($errorobject);
170 } else {
171 $error = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
172 $error .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
173 if (isset($ex->errorcode)) {
174 $error .= '<ERRORCODE>' . htmlspecialchars($ex->errorcode, ENT_COMPAT, 'UTF-8')
175 . '</ERRORCODE>' . "\n";
177 $error .= '<MESSAGE>'.htmlspecialchars($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
178 if (debugging() and isset($ex->debuginfo)) {
179 $error .= '<DEBUGINFO>'.htmlspecialchars($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
181 $error .= '</EXCEPTION>'."\n";
183 return $error;
187 * Internal implementation - sending of page headers.
189 protected function send_headers() {
190 if ($this->restformat == 'json') {
191 header('Content-type: application/json');
192 } else {
193 header('Content-Type: application/xml; charset=utf-8');
194 header('Content-Disposition: inline; filename="response.xml"');
196 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
197 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
198 header('Pragma: no-cache');
199 header('Accept-Ranges: none');
200 // Allow cross-origin requests only for Web Services.
201 // This allow to receive requests done by Web Workers or webapps in different domains.
202 header('Access-Control-Allow-Origin: *');
206 * Internal implementation - recursive function producing XML markup.
208 * @param mixed $returns the returned values
209 * @param external_description $desc
210 * @return string
212 protected static function xmlize_result($returns, $desc) {
213 if ($desc === null) {
214 return '';
216 } else if ($desc instanceof external_value) {
217 if (is_bool($returns)) {
218 // we want 1/0 instead of true/false here
219 $returns = (int)$returns;
221 if (is_null($returns)) {
222 return '<VALUE null="null"/>'."\n";
223 } else {
224 return '<VALUE>'.htmlspecialchars($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
227 } else if ($desc instanceof external_multiple_structure) {
228 $mult = '<MULTIPLE>'."\n";
229 if (!empty($returns)) {
230 foreach ($returns as $val) {
231 $mult .= self::xmlize_result($val, $desc->content);
234 $mult .= '</MULTIPLE>'."\n";
235 return $mult;
237 } else if ($desc instanceof external_single_structure) {
238 $single = '<SINGLE>'."\n";
239 foreach ($desc->keys as $key=>$subdesc) {
240 $value = isset($returns[$key]) ? $returns[$key] : null;
241 $single .= '<KEY name="'.$key.'">'.self::xmlize_result($value, $subdesc).'</KEY>'."\n";
243 $single .= '</SINGLE>'."\n";
244 return $single;
251 * REST test client class
253 * @package webservice_rest
254 * @copyright 2009 Petr Skoda (skodak)
255 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
257 class webservice_rest_test_client implements webservice_test_client_interface {
259 * Execute test client WS request
260 * @param string $serverurl server url (including token parameter or username/password parameters)
261 * @param string $function function name
262 * @param array $params parameters of the called function
263 * @return mixed
265 public function simpletest($serverurl, $function, $params) {
266 return download_file_content($serverurl.'&wsfunction='.$function, null, $params);