-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
328 lines (292 loc) · 11.5 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
<?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/>.
/**
* SOAP web service implementation classes and methods.
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->dirroot/webservice/lib.php");
require_once 'Zend/Soap/Server.php';
/**
* The Zend XMLRPC server but with a fault that returns debuginfo
*
* @package webservice_soap
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
*/
class moodle_zend_soap_server extends Zend_Soap_Server {
/**
* Generate a server fault
*
* Note that the arguments are the reverse of those used by SoapFault.
*
* Moodle note: basically we return the faultactor (errorcode) and faultdetails (debuginfo)
*
* If an exception is passed as the first argument, its message and code
* will be used to create the fault object if it has been registered via
* {@Link registerFaultException()}.
*
* @link http://www.w3.org/TR/soap12-part1/#faultcodes
* @param string|Exception $fault
* @param string $code SOAP Fault Codes
* @return SoapFault
*/
public function fault($fault = null, $code = "Receiver")
{
// Run the zend code that clean/create a soapfault.
$soapfault = parent::fault($fault, $code);
// Intercept any exceptions and add the errorcode and debuginfo (optional).
$actor = null;
$details = null;
if ($fault instanceof Exception) {
// Add the debuginfo to the exception message if debuginfo must be returned.
$actor = $fault->errorcode;
if (debugging() and isset($fault->debuginfo)) {
$details = $fault->debuginfo;
}
}
return new SoapFault($soapfault->faultcode,
$soapfault->getMessage() . ' | ERRORCODE: ' . $fault->errorcode,
$actor, $details);
}
/**
* Handle a request
*
* NOTE: this is basically a copy of the Zend handle()
* but with $soap->fault returning faultactor + faultdetail
* So we don't require coding style checks within this method
* to keep it as similar as the original one.
*
* Instantiates SoapServer object with options set in object, and
* dispatches its handle() method.
*
* $request may be any of:
* - DOMDocument; if so, then cast to XML
* - DOMNode; if so, then grab owner document and cast to XML
* - SimpleXMLElement; if so, then cast to XML
* - stdClass; if so, calls __toString() and verifies XML
* - string; if so, verifies XML
*
* If no request is passed, pulls request using php:://input (for
* cross-platform compatability purposes).
*
* @param DOMDocument|DOMNode|SimpleXMLElement|stdClass|string $request Optional request
* @return void|string
*/
public function handle($request = null)
{
if (null === $request) {
$request = file_get_contents('php://input');
}
// Set Zend_Soap_Server error handler
$displayErrorsOriginalState = $this->_initializeSoapErrorContext();
$setRequestException = null;
/**
* @see Zend_Soap_Server_Exception
*/
require_once 'Zend/Soap/Server/Exception.php';
try {
$this->_setRequest($request);
} catch (Zend_Soap_Server_Exception $e) {
$setRequestException = $e;
}
$soap = $this->_getSoap();
ob_start();
if($setRequestException instanceof Exception) {
// Send SOAP fault message if we've catched exception
$soap->fault("Sender", $setRequestException->getMessage());
} else {
try {
$soap->handle($request);
} catch (Exception $e) {
$fault = $this->fault($e);
$faultactor = isset($fault->faultactor) ? $fault->faultactor : null;
$detail = isset($fault->detail) ? $fault->detail : null;
$soap->fault($fault->faultcode, $fault->faultstring, $faultactor, $detail);
}
}
$this->_response = ob_get_clean();
// Restore original error handler
restore_error_handler();
ini_set('display_errors', $displayErrorsOriginalState);
if (!$this->_returnResponse) {
echo $this->_response;
return;
}
return $this->_response;
}
}
/**
* SOAP service server implementation.
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class webservice_soap_server extends webservice_zend_server {
/**
* Contructor
*
* @param string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
*/
public function __construct($authmethod) {
// must not cache wsdl - the list of functions is created on the fly
ini_set('soap.wsdl_cache_enabled', '0');
require_once 'Zend/Soap/Server.php';
require_once 'Zend/Soap/AutoDiscover.php';
if (optional_param('wsdl', 0, PARAM_BOOL)) {
parent::__construct($authmethod, 'Zend_Soap_AutoDiscover');
} else {
parent::__construct($authmethod, 'moodle_zend_soap_server');
}
$this->wsname = 'soap';
}
/**
* Set up zend service class
*/
protected function init_zend_server() {
global $CFG;
parent::init_zend_server();
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {
$username = optional_param('wsusername', '', PARAM_RAW);
$password = optional_param('wspassword', '', PARAM_RAW);
// aparently some clients and zend soap server does not work well with "&" in urls :-(
//TODO MDL-31151 the zend error has been fixed in the last Zend SOAP version, check that is fixed and remove obsolete code
$url = $CFG->wwwroot.'/webservice/soap/simpleserver.php/'.urlencode($username).'/'.urlencode($password);
// the Zend server is using this uri directly in xml - weird :-(
$this->zend_server->setUri(htmlentities($url));
} else {
$wstoken = optional_param('wstoken', '', PARAM_RAW);
$url = $CFG->wwwroot.'/webservice/soap/server.php?wstoken='.urlencode($wstoken);
// the Zend server is using this uri directly in xml - weird :-(
$this->zend_server->setUri(htmlentities($url));
}
if (!optional_param('wsdl', 0, PARAM_BOOL)) {
$this->zend_server->setReturnResponse(true);
$this->zend_server->registerFaultException('moodle_exception');
$this->zend_server->registerFaultException('webservice_parameter_exception'); //deprecated since Moodle 2.2 - kept for backward compatibility
$this->zend_server->registerFaultException('invalid_parameter_exception');
$this->zend_server->registerFaultException('invalid_response_exception');
//when DEBUG >= NORMAL then the thrown exceptions are "casted" into a PHP SoapFault expception
//in order to diplay the $debuginfo (see moodle_zend_soap_server class - MDL-29435)
if (debugging()) {
$this->zend_server->registerFaultException('SoapFault');
}
}
}
/**
* This method parses the $_POST and $_GET superglobals and looks for
* the following information:
* user authentication - username+password or token (wsusername, wspassword and wstoken parameters)
*/
protected function parse_request() {
parent::parse_request();
if (!$this->username or !$this->password) {
//note: this is the workaround for the trouble with & in soap urls
$authdata = get_file_argument();
$authdata = explode('/', trim($authdata, '/'));
if (count($authdata) == 2) {
list($this->username, $this->password) = $authdata;
}
}
}
/**
* Send the error information to the WS client
* formatted as an XML document.
*
* @param exception $ex the exception to send back
*/
protected function send_error($ex=null) {
if ($ex) {
$info = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
$info .= ' - '.$ex->debuginfo;
}
} else {
$info = 'Unknown error';
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body><SOAP-ENV:Fault>
<faultcode>MOODLE:error</faultcode>
<faultstring>'.$info.'</faultstring>
</SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>';
$this->send_headers();
header('Content-Type: application/xml; charset=utf-8');
header('Content-Disposition: inline; filename="response.xml"');
echo $xml;
}
/**
* Generate 'struct' type name
* This type name is the name of a class generated on the fly.
*
* @param external_single_structure $structdesc
* @return string
*/
protected function generate_simple_struct_class(external_single_structure $structdesc) {
global $USER;
$fields = array();
foreach ($structdesc->keys as $name => $fieldsdesc) {
$type = $this->get_phpdoc_type($fieldsdesc);
$fields[] = ' /** @var '.$type." */\n" .
' public $'.$name.';';
}
// We do this after the call to get_phpdoc_type() to avoid duplicate class creation.
$classname = 'webservices_struct_class_000000';
while (class_exists($classname)) {
$classname++;
}
$code = '
/**
* Virtual struct class for web services for user id '.$USER->id.' in context '.$this->restricted_context->id.'.
*/
class '.$classname.' {
'.implode("\n", $fields).'
}
';
eval($code);
return $classname;
}
}
/**
* SOAP test client class
*
* @package webservice_soap
* @copyright 2009 Petr Skodak
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class webservice_soap_test_client implements webservice_test_client_interface {
/**
* Execute test client WS request
*
* @param string $serverurl server url (including token parameter or username/password parameters)
* @param string $function function name
* @param array $params parameters of the called function
* @return mixed
*/
public function simpletest($serverurl, $function, $params) {
//zend expects 0 based array with numeric indexes
$params = array_values($params);
require_once 'Zend/Soap/Client.php';
$client = new Zend_Soap_Client($serverurl.'&wsdl=1');
return $client->__call($function, $params);
}
}