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
|
<?php
/**
* Elgg XML-RPC library.
* Contains functions and classes to handle XML-RPC services, currently only server only.
*
* @package Elgg
* @subpackage Core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @author Marcus Povey
* @copyright Curverider Ltd 2008
* @link http://elgg.org/
*/
// XMLRPC Call ////////////////////////////////////////////////////////////////////////////
/**
* @class XMLRPCCall
* This class represents
* @author Marcus Povey
*/
class XMLRPCCall
{
/** Method name */
private $methodname;
/** Parameters */
private $params;
/**
* Construct a new XML RPC Call
*
* @param string $xml
*/
function __construct($xml)
{
$this->parse($xml);
}
/**
* Return the method name associated with the call.
*
* @return string
*/
public function getMethodName() { return $this->methodname; }
/**
* Return the parameters.
* Returns a nested array of XmlElement.
*
* @see XmlElement
* @return array
*/
public function getParameters() { return $this->params; }
/**
* Parse the xml into its components according to spec.
* This first version is a little primitive.
*
* @param string $xml
*/
private function parse($xml)
{
$xml = xml_2_object($xml);
// sanity check
if ((isset($xml->name)) && (strcasecmp($xml->name, "methodCall")!=0))
throw new CallException(elgg_echo('CallException:NotRPCCall'));
// method name
$this->methodname = $xml->children[0]->content;
// parameters
$this->params = $xml->children[1]->children;
print_r($this);
}
}
// Response classes ///////////////////////////////////////////////////////////////////////
/**
* @class XMLRPCParameter Superclass for all RPC parameters.
* @author Marcus Povey
*/
abstract class XMLRPCParameter
{
protected $value;
function __construct() { }
}
/**
* @class XMLRPCIntParameter An Integer.
* @author Marcus Povey
*/
class XMLRPCIntParameter extends XMLRPCParameter
{
function __construct($value)
{
parent::__construct();
$this->value = (int)$value;
}
function __toString()
{
return "<value><i4>{$this->value}</i4></value>\n";
}
}
/**
* @class XMLRPCBoolParameter A boolean.
* @author Marcus Povey
*/
class XMLRPCBoolParameter extends XMLRPCParameter
{
function __construct($value)
{
parent::__construct();
$this->value = (bool)$value;
}
function __toString()
{
$code = ($this->value) ? "1" : "0";
return "<value><boolean>{$code}</boolean></value>\n";
}
}
/**
* @class XMLRPCStringParameter A string.
* @author Marcus Povey
*/
class XMLRPCStringParameter extends XMLRPCParameter
{
function __construct($value)
{
parent::__construct();
$this->value = $value;
}
function __toString()
{
$value = htmlentities($this->value);
return "<value><string>{$value}</string></value>\n";
}
}
/**
* @class XMLRPCDoubleParameter A double precision signed floating point number.
* @author Marcus Povey
*/
class XMLRPCDoubleParameter extends XMLRPCParameter
{
function __construct($value)
{
parent::__construct();
$this->value = (float)$value;
}
function __toString()
{
return "<value><double>{$this->value}</double></value>\n";
}
}
/**
* @class XMLRPCDateParameter An ISO8601 data and time.
* @author Marcus Povey
*/
class XMLRPCDateParameter extends XMLRPCParameter
{
/**
* Construct a date
*
* @param int $timestamp The unix timestamp, or blank for "now".
*/
function __construct($timestamp = 0)
{
parent::__construct();
$this->value = $timestamp;
if (!$timestamp)
$this->value = time();
}
function __toString()
{
$value = date('c', $this->value);
return "<value><dateTime.iso8601>{$value}</dateTime.iso8601></value>\n";
}
}
/**
* @class XMLRPCBase64Parameter A base 64 encoded blob of binary.
* @author Marcus Povey
*/
class XMLRPCBase64Parameter extends XMLRPCParameter
{
/**
* Construct a base64 encoded block
*
* @param string $blob Unencoded binary blob
*/
function __construct($blob)
{
parent::__construct();
$this->value = base64_encode($blob);
}
function __toString()
{
return "<value><base64>{$value}</base64></value>\n";
}
}
/**
* @class XMLRPCStructParameter A structure containing other XMLRPCParameter objects.
* @author Marcus Povey
*/
class XMLRPCStructParameter extends XMLRPCParameter
{
/**
* Construct a struct.
*
* @param array $parameters Optional associated array of parameters, if not provided then addField must be used.
*/
function __construct($parameters = NULL)
{
parent::__construct();
if (is_array($parameters))
{
foreach ($parameters as $k => $v)
$this->addField($k, $v);
}
}
/**
* Add a field to the container.
*
* @param string $name The name of the field.
* @param XMLRPCParameter $value The value.
*/
public function addField($name, XMLRPCParameter $value)
{
if (!is_array($this->value))
$this->value = array();
$this->value[$name] = $value;
}
function __toString()
{
$params = "";
foreach ($this->value as $k => $v)
{
$params .= "<member><name>$k</name>$v</member>";
}
return <<< END
<struct>
$params
</struct>
END;
}
}
/**
* @class XMLRPCArrayParameter An array containing other XMLRPCParameter objects.
* @author Marcus Povey
*/
class XMLRPCArrayParameter extends XMLRPCParameter
{
/**
* Construct an array.
*
* @param array $parameters Optional array of parameters, if not provided then addField must be used.
*/
function __construct($parameters = NULL)
{
parent::__construct();
if (is_array($parameters))
{
foreach ($parameters as $v)
$this->addField($v);
}
}
/**
* Add a field to the container.
*
* @param XMLRPCParameter $value The value.
*/
public function addField(XMLRPCParameter $value)
{
if (!is_array($this->value))
$this->value = array();
$this->value[] = $value;
}
function __toString()
{
$params = "";
foreach ($this->value as $value)
{
$params .= "$value";
}
return <<< END
<array>
<data>
$params
</data>
</array>
END;
}
}
/**
* @class XMLRPCResponse XML-RPC Response.
* @author Marcus Povey
*/
abstract class XMLRPCResponse
{
/** An array of parameters */
protected $parameters = array();
/**
* Add a parameter here.
*
* @param XMLRPCParameter $param The parameter.
*/
public function addParameter(XMLRPCParameter $param)
{
if (!is_array($this->parameters))
$this->parameters = array();
$this->parameters[] = $param;
}
public function addInt($value) { $this->addParameter(new XMLRPCIntParameter($value)); }
public function addString($value) { $this->addParameter(new XMLRPCStringParameter($value)); }
public function addDouble($value) { $this->addParameter(new XMLRPCDoubleParameter($value)); }
public function addBoolean($value) { $this->addParameter(new XMLRPCBoolParameter($value)); }
}
/**
* @class XMLRPCSuccessResponse
* @author Marcus Povey
*/
class XMLRPCSuccessResponse extends XMLRPCResponse
{
/**
* Output to XML.
*/
public function __toString()
{
$params = "";
foreach ($this->parameters as $param)
$params .= "<param>$param</param>\n";
return <<< END
<methodResponse>
<params>
$params
</params>
</methodResponse>
END;
}
}
/**
* @class XMLRPCErrorResponse
* @author Marcus Povey
*/
class XMLRPCErrorResponse extends XMLRPCResponse
{
/**
* Set the error response and error code.
*
* @param string $message The message
* @param int $code Error code (default = system error as defined by http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php)
*/
function __construct($message, $code = -32400)
{
$this->addParameter(
new XMLRPCStructParameter(
array (
'faultCode' => new XMLRPCIntParameter($code),
'faultString' => new XMLRPCStringParameter($message)
)
)
);
}
/**
* Output to XML.
*/
public function __toString()
{
return <<< END
<methodResponse>
<fault>
<value>
{$this->parameters[0]}
</value>
</fault>
</methodResponse>
END;
}
}
// Functions for adding handlers //////////////////////////////////////////////////////////
/** XML-RPC Handlers */
$XML_RPC_HANDLERS = array();
/**
* Register a method handler for a given XML-RPC method.
*
* @param string $method Method parameter.
* @param string $handler The handler function. This function accepts once XMLRPCCall object and must return a XMLRPCResponse object.
* @return bool
*/
function register_xmlrpc_handler($method, $handler)
{
global $XML_RPC_HANDLERS;
$XML_RPC_HANDLERS[$method] = $handler;
}
/**
* Trigger a method call and pass the relevant parameters to the funciton.
*
* @param XMLRPCCall $parameters The call and parameters.
* @return XMLRPCCall
*/
function trigger_xmlrpc_handler(XMLRPCCall $parameters)
{
global $XML_RPC_HANDLERS;
// Go through and see if we have a handler
if (isset($XML_RPC_HANDLERS[$parameters->getMethodName()]))
{
$result = $handler($parameters);
if (!($result instanceof XMLRPCResponse))
throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnexpectedReturnFormat'));
// Result in right format, return it.
return $result;
}
// if no handler then throw exception
throw new NotImplementedException(sprintf(elgg_echo('NotImplementedException:XMLRPCMethodNotImplemented'), $method));
}
// Error handler functions ////////////////////////////////////////////////////////////////
/**
* PHP Error handler function.
* This function acts as a wrapper to catch and report PHP error messages.
*
* @see http://uk3.php.net/set-error-handler
* @param unknown_type $errno
* @param unknown_type $errmsg
* @param unknown_type $filename
* @param unknown_type $linenum
* @param unknown_type $vars
*/
function __php_xmlrpc_error_handler($errno, $errmsg, $filename, $linenum, $vars)
{
$error = date("Y-m-d H:i:s (T)") . ": \"" . $errmsg . "\" in file " . $filename . " (line " . $linenum . ")";
switch ($errno) {
case E_USER_ERROR:
error_log("ERROR: " . $error);
// Since this is a fatal error, we want to stop any further execution but do so gracefully.
throw new Exception("ERROR: " . $error);
break;
case E_WARNING :
case E_USER_WARNING :
error_log("WARNING: " . $error);
break;
default:
error_log("DEBUG: " . $error);
}
}
/**
* PHP Exception handler for XMLRPC.
* @param Exception $exception
*/
function __php_xmlrpc_exception_handler($exception) {
error_log("*** FATAL EXCEPTION (XML-RPC) *** : " . $exception);
page_draw($exception->getMessage(), elgg_view("xml-rpc/output", array('result' => new XMLRPCErrorResponse($exception->getMessage(), $exception->getCode()==0 ? -32400 : $exception->getCode()))));
}
?>
|