forked from catalyst/moodle-webservice_restful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
522 lines (456 loc) · 17.9 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* RESTful web service implementation classes and methods.
*
* @package webservice_restful
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/webservice/lib.php");
/**
* REST service server implementation.
*
* @package webservice_restful
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_restful_server extends webservice_base_server
{
/** @var string return method ('xml' or 'json') */
protected $responseformat;
/** @var string request method ('xml', 'json', or 'urlencode') */
protected $requestformat;
protected $secondary_authorization_token = '';
/**
* Contructor
*
* @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
* @param string $secondary_authorization_token secondary authorization field seach, if original was removed
*/
public function __construct($authmethod, $secondary_authorization_token = null)
{
parent::__construct($authmethod);
$this->wsname = 'restful';
$this->responseformat = 'json'; // Default to json.
$this->requestformat = 'json'; // Default to json.
if (!is_null($secondary_authorization_token))
$this->secondary_authorization_token = strtolower($secondary_authorization_token);
}
/**
* Get headers from Apache websever.
*
* @return array $returnheaders The headers from Apache.
*/
private function get_apache_headers()
{
$capitalizearray = array(
'content-type',
'accept',
'authorization',
'content-length',
'user-agent',
'host'
);
array_push($capitalizearray, $this->secondary_authorization_token);
$headers = apache_request_headers();
$returnheaders = array();
foreach ($headers as $key => $value) {
if (in_array(strtolower($key), $capitalizearray)) {
$header = 'HTTP_' . strtoupper($key);
$header = str_replace('-', '_', $header);
$returnheaders[$header] = $value;
}
}
return $returnheaders;
}
/**
* Extract the HTTP headers out of the request.
*
* @param array $headers Optional array of headers, to assist with testing.
* @return array $headers HTTP headers.
*/
private function get_headers($headers = null)
{
$returnheaders = array();
if (!$headers) {
if (function_exists('apache_request_headers')) { // Apache websever.
$headers = $this->get_apache_headers();
} else { // Nginx webserver.
$headers = $_SERVER;
}
}
foreach ($headers as $key => $value) {
if (substr($key, 0, 5) == 'HTTP_') {
$returnheaders[$key] = $value;
}
}
return $returnheaders;
}
/**
* Get the webservice authorization token from the request.
* Throws error and notifies caller on failure.
*
* @param array $headers The extracted HTTP headers.
* @return string $wstoken The extracted webservice authorization token.
*/
private function get_wstoken($headers)
{
$wstoken = '';
if (isset($headers['HTTP_AUTHORIZATION'])) {
$wstoken = $headers['HTTP_AUTHORIZATION'];
} else if (
!is_null($this->secondary_authorization_token) &&
$this->secondary_authorization_token != '' &&
isset($headers['HTTP_' . strtoupper($this->secondary_authorization_token)])
) {
$wstoken = $headers['HTTP_' . strtoupper($this->secondary_authorization_token)];
} else {
// Raise an error if auth header not supplied.
$ex = new \moodle_exception('noauthheader', 'webservice_restful', '');
$this->send_error($ex, 401);
}
return $wstoken;
}
/**
* Extract the web service funtion to use from the request URL.
* Throws error and notifies caller on failure.
*
* @param array $getvars Optional get variables, used for testing.
* @return string $wsfunction The webservice function to call.
*/
private function get_wsfunction($getvars = null)
{
$wsfunction = '';
// Testing has found that there is varying methods across webservers,
// so we try a few ways.
if ($getvars) { // Check to see if we are passing hte function explictly.
$wsfunction = ltrim($getvars['file'], '/');
} else if (isset($_GET['file'])) { // Try get variables.
$wsfunction = ltrim($_GET['file'], '/');
} else if (isset($_SERVER['PATH_INFO'])) { // Try path info from server super global.
$wsfunction = ltrim($_SERVER['PATH_INFO'], '/');
} else if (isset($_SERVER['REQUEST_URI'])) { // Try request URI from server super global.
$wsfunction = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/') + 1);
}
if ($wsfunction == '') {
// Raise an error if function not supplied.
$ex = new \moodle_exception('nowsfunction', 'webservice_restful', '');
$this->send_error($ex, 400);
}
return $wsfunction;
}
/**
* Get the format to use for the client response.
* Throws error and notifies caller on failure.
*
* @param array $headers The HTTP headers.
* @return string $responseformat The format of the client response.
*/
private function get_responseformat($headers)
{
$responseformat = '';
if (isset($headers['HTTP_ACCEPT'])) {
$responseformat = ltrim($headers['HTTP_ACCEPT'], 'application/');
} else {
// Raise an error if accept header not supplied.
$ex = new \moodle_exception('noacceptheader', 'webservice_restful', '');
$this->send_error($ex, 400);
}
return $responseformat;
}
/**
* Get the format of the client request.
* Throws error and notifies caller on failure.
*
* @param array $headers The HTTP headers.
* @return string $requestformat The format of the client request.
*/
private function get_requestformat($headers)
{
$requestformat = '';
if (isset($headers['HTTP_CONTENT_TYPE'])) {
$requestformat = ltrim($headers['HTTP_CONTENT_TYPE'], 'application/');
} else {
// Raise an error if content header not supplied.
$ex = new \moodle_exception('notypeheader', 'webservice_restful', '');
$this->send_error($ex, 400);
}
return $requestformat;
}
/**
* Get the parameters to pass to the webservice function
*
* @param array $content the content to parse.
* @return mixed $input The parameters to use with the webservice.
*/
private function get_parameters($content = '')
{
if (!$content) {
$content = file_get_contents('php://input');
}
if ($this->requestformat == 'json') {
$parameters = json_decode($content, true); // Convert JSON into array.
} else if ($this->requestformat == 'xml') {
$parametersxml = simplexml_load_string($content);
$parameters = json_decode(json_encode($parametersxml), true); // Dirty XML to JSON to PHP array conversion.
} else { // Data provided in as URL encoded.
$parameters = $_POST;
}
return $parameters;
}
/**
* This method parses the request sent to Moodle
* and extracts and validates the supplied data.
*
* @return bool
*/
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 the HTTP Headers.
$headers = $this->get_headers();
// Get the webservice token or return false.
if (!($this->token = $this->get_wstoken($headers))) {
return false;
}
// Get response format or return false.
if (!($this->responseformat = $this->get_responseformat($headers))) {
return false;
}
// Get request format or return false.
if (!($this->requestformat = $this->get_requestformat($headers))) {
return false;
}
// Get the webservice function or return false.
if (!($this->functionname = $this->get_wsfunction())) {
return false;
}
// Get the webservice function parameters or return false.
if (empty($this->get_parameters())) {
$this->parameters = array();
} else if (!($this->parameters = $this->get_parameters())) {
return false;
}
return true;
}
/**
* Process request from client.
*
* @uses die
*/
public function run()
{
global $CFG, $SESSION;
// We will probably need a lot of memory in some functions.
raise_memory_limit(MEMORY_EXTRA);
// Set some longer timeout, this script is not sending any output,
// this means we need to manually extend the timeout operations
// that need longer time to finish.
external_api::set_timeout();
// Set up exception handler first, we want to sent them back in correct format that
// the other system understands.
// We do not need to call the original default handler because this ws handler does everything.
set_exception_handler(array($this, 'exception_handler'));
// Init all properties from the request data.
if (!$this->parse_request()) {
die;
};
// Authenticate user, this has to be done after the request parsing
// this also sets up $USER and $SESSION.
$this->authenticate_user();
// Find all needed function info and make sure user may actually execute the function.
$this->load_function_info();
// Log the web service request.
$params = array(
'other' => array(
'function' => $this->functionname
)
);
$event = \core\event\webservice_function_called::create($params);
$event->set_legacy_logdata(array(SITEID, 'webservice', $this->functionname, '', getremoteaddr(), 0, $this->userid));
$event->trigger();
// Do additional setup stuff.
$settings = external_settings::get_instance();
if (method_exists($settings, 'get_lang')) {
$sessionlang = $settings->get_lang();
if (!empty($sessionlang)) {
$SESSION->lang = $sessionlang;
}
setup_lang_from_browser();
if (empty($CFG->lang)) {
if (empty($SESSION->lang)) {
$CFG->lang = 'en';
} else {
$CFG->lang = $SESSION->lang;
}
}
}
// Finally, execute the function - any errors are catched by the default exception handler.
$this->execute();
// Send the results back in correct format.
$this->send_response();
// Session cleanup.
$this->session_cleanup();
die;
}
/**
* Send the result of function call to the WS client.
*
* @return void
*/
protected function send_response()
{
// Check that the returned values are valid.
try {
if ($this->function->returns_desc != null) {
$validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
} else {
$validatedvalues = null;
}
} catch (Exception $ex) {
$exception = $ex;
}
if (!empty($exception)) {
$response = $this->generate_error($exception);
} else {
// We can now convert the response to the requested REST format.
if ($this->responseformat == 'json') {
$response = json_encode($validatedvalues);
} else {
$response = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
$response .= '<RESPONSE>' . "\n";
$response .= self::xmlize_result($validatedvalues, $this->function->returns_desc);
$response .= '</RESPONSE>' . "\n";
}
}
$this->send_headers();
echo $response;
}
/**
* Send the error information to the WS client
* formatted as XML document.
* Note: the exception is never passed as null,
* it only matches the abstract function declaration.
*
* @param exception $ex the exception that we are sending.
* @param integer $code The HTTP response code to return.
*/
protected function send_error($ex = null, $code = 400)
{
// Sniffing for unit tests running alwasys feels like a hack.
// We need to do this otherwise it will conflict with the headers
// sent by PHPUNIT.
if (!PHPUNIT_TEST) {
http_response_code($code);
$this->send_headers($code);
}
echo $this->generate_error($ex);
}
/**
* Build the error information matching the REST returned value format (JSON or XML)
* @param exception $ex the exception we are converting in the server rest format
* @return string the error in the requested REST format
*/
protected function generate_error($ex)
{
if ($this->responseformat != 'xml') {
$errorobject = new stdClass;
$errorobject->exception = get_class($ex);
$errorobject->errorcode = $ex->errorcode;
$errorobject->message = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
$errorobject->debuginfo = $ex->debuginfo;
}
$error = json_encode($errorobject);
} else {
$error = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
$error .= '<EXCEPTION class="' . get_class($ex) . '">' . "\n";
$error .= '<ERRORCODE>' . htmlspecialchars($ex->errorcode, ENT_COMPAT, 'UTF-8')
. '</ERRORCODE>' . "\n";
$error .= '<MESSAGE>' . htmlspecialchars($ex->getMessage(), ENT_COMPAT, 'UTF-8') . '</MESSAGE>' . "\n";
if (debugging() and isset($ex->debuginfo)) {
$error .= '<DEBUGINFO>' . htmlspecialchars($ex->debuginfo, ENT_COMPAT, 'UTF-8') . '</DEBUGINFO>' . "\n";
}
$error .= '</EXCEPTION>' . "\n";
}
return $error;
}
/**
* Internal implementation - sending of page headers.
*
* @param integer $code The HTTP response code to return.
*/
protected function send_headers($code = 200)
{
if ($this->responseformat == 'json') {
header('Content-type: application/json');
} else {
header('Content-Type: application/xml; charset=utf-8');
header('Content-Disposition: inline; filename="response.xml"');
}
header('X-PHP-Response-Code: ' . $code, true, $code);
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: *');
}
/**
* Internal implementation - recursive function producing XML markup.
*
* @param mixed $returns the returned values
* @param external_description $desc
* @return string
*/
protected static function xmlize_result($returns, $desc)
{
if ($desc === null) {
return '';
} else if ($desc instanceof external_value) {
if (is_bool($returns)) {
// We want 1/0 instead of true/false here.
$returns = (int)$returns;
}
if (is_null($returns)) {
return '<VALUE null="null"/>' . "\n";
} else {
return '<VALUE>' . htmlspecialchars($returns, ENT_COMPAT, 'UTF-8') . '</VALUE>' . "\n";
}
} else if ($desc instanceof external_multiple_structure) {
$mult = '<MULTIPLE>' . "\n";
if (!empty($returns)) {
foreach ($returns as $val) {
$mult .= self::xmlize_result($val, $desc->content);
}
}
$mult .= '</MULTIPLE>' . "\n";
return $mult;
} else if ($desc instanceof external_single_structure) {
$single = '<SINGLE>' . "\n";
foreach ($desc->keys as $key => $subdesc) {
$value = isset($returns[$key]) ? $returns[$key] : null;
$single .= '<KEY name="' . $key . '">' . self::xmlize_result($value, $subdesc) . '</KEY>' . "\n";
}
$single .= '</SINGLE>' . "\n";
return $single;
}
}
}