1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpXmlRpc\Helper; |
4
|
|
|
|
5
|
|
|
use PhpXmlRpc\PhpXmlRpc; |
6
|
|
|
use PhpXmlRpc\Value; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Deals with parsing the XML. |
10
|
|
|
* @see http://xmlrpc.com/spec.md |
11
|
|
|
* |
12
|
|
|
* @todo implement an interface to allow for alternative implementations |
13
|
|
|
* - make access to $_xh protected, return more high-level data structures |
14
|
|
|
* - add parseRequest, parseResponse, parseValue methods |
15
|
|
|
* @todo if iconv() or mb_string() are available, we could allow to convert the received xml to a custom charset encoding |
16
|
|
|
* while parsing, which is faster than doing it later by going over the rebuilt data structure |
17
|
|
|
* @todo allow usage of a custom Logger via the DIC(ish) pattern we use in other classes |
18
|
|
|
*/ |
19
|
|
|
class XMLParser |
20
|
|
|
{ |
21
|
|
|
const RETURN_XMLRPCVALS = 'xmlrpcvals'; |
22
|
|
|
const RETURN_EPIVALS = 'epivals'; |
23
|
|
|
const RETURN_PHP = 'phpvals'; |
24
|
|
|
|
25
|
|
|
const ACCEPT_REQUEST = 1; |
26
|
|
|
const ACCEPT_RESPONSE = 2; |
27
|
|
|
const ACCEPT_VALUE = 4; |
28
|
|
|
const ACCEPT_FAULT = 8; |
29
|
|
|
|
30
|
|
|
protected static $logger; |
31
|
|
|
|
32
|
|
|
// Used to store state during parsing and to pass parsing results to callers. |
33
|
|
|
// Quick explanation of components: |
34
|
|
|
// private: |
35
|
|
|
// ac - used to accumulate values |
36
|
|
|
// stack - array with genealogy of xml elements names used to validate nesting of xmlrpc elements |
37
|
|
|
// valuestack - array used for parsing arrays and structs |
38
|
|
|
// lv - used to indicate "looking for a value": implements the logic to allow values with no types to be strings |
39
|
|
|
// public: |
40
|
|
|
// isf - used to indicate an xml parsing fault (3), invalid xmlrpc fault (2) or xmlrpc response fault (1) |
41
|
|
|
// isf_reason - used for storing xmlrpc response fault string |
42
|
|
|
// value - used to store the value in responses |
43
|
|
|
// method - used to store method name in requests |
44
|
|
|
// params - used to store parameters in requests |
45
|
|
|
// pt - used to store the type of each received parameter. Useful if parameters are automatically decoded to php values |
46
|
|
|
// rt - 'methodcall', 'methodresponse', 'value' or 'fault' (the last one used only in EPI emulation mode) |
47
|
|
|
public $_xh = array( |
48
|
|
|
'ac' => '', |
49
|
|
|
'stack' => array(), |
50
|
|
|
'valuestack' => array(), |
51
|
|
|
'isf' => 0, |
52
|
|
|
'isf_reason' => '', |
53
|
|
|
'value' => null, |
54
|
|
|
'method' => false, |
55
|
|
|
'params' => array(), |
56
|
|
|
'pt' => array(), |
57
|
|
|
'rt' => '', |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
public $xmlrpc_valid_parents = array( |
61
|
|
|
'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'), |
62
|
|
|
'BOOLEAN' => array('VALUE'), |
63
|
|
|
'I4' => array('VALUE'), |
64
|
|
|
'I8' => array('VALUE'), |
65
|
|
|
'EX:I8' => array('VALUE'), |
66
|
|
|
'INT' => array('VALUE'), |
67
|
|
|
'STRING' => array('VALUE'), |
68
|
|
|
'DOUBLE' => array('VALUE'), |
69
|
|
|
'DATETIME.ISO8601' => array('VALUE'), |
70
|
|
|
'BASE64' => array('VALUE'), |
71
|
|
|
'MEMBER' => array('STRUCT'), |
72
|
|
|
'NAME' => array('MEMBER'), |
73
|
|
|
'DATA' => array('ARRAY'), |
74
|
|
|
'ARRAY' => array('VALUE'), |
75
|
|
|
'STRUCT' => array('VALUE'), |
76
|
|
|
'PARAM' => array('PARAMS'), |
77
|
|
|
'METHODNAME' => array('METHODCALL'), |
78
|
|
|
'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), |
79
|
|
|
'FAULT' => array('METHODRESPONSE'), |
80
|
|
|
'NIL' => array('VALUE'), // only used when extension activated |
81
|
|
|
'EX:NIL' => array('VALUE'), // only used when extension activated |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
/** @var array $parsing_options */ |
85
|
|
|
protected $parsing_options = array(); |
86
|
|
|
/** @var int $accept self::ACCEPT_REQUEST | self::ACCEPT_RESPONSE by default */ |
87
|
|
|
protected $accept = 3; |
88
|
|
|
/** @var int $maxChunkLength 4 MB by default. Any value below 10MB should be good */ |
89
|
|
|
protected $maxChunkLength = 4194304; |
90
|
|
|
|
91
|
572 |
|
public function getLogger() |
92
|
|
|
{ |
93
|
572 |
|
if (self::$logger === null) { |
94
|
572 |
|
self::$logger = Logger::instance(); |
95
|
|
|
} |
96
|
|
|
return self::$logger; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $logger |
101
|
|
|
* @return void |
102
|
712 |
|
*/ |
103
|
|
|
public static function setLogger($logger) |
104
|
712 |
|
{ |
105
|
|
|
self::$logger = $logger; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $options passed to the xml parser |
110
|
|
|
*/ |
111
|
|
|
public function __construct(array $options = array()) |
112
|
|
|
{ |
113
|
|
|
$this->parsing_options = $options; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
712 |
|
* @param string $data |
118
|
|
|
* @param string $returnType |
119
|
|
|
* @param int $accept a bit-combination of self::ACCEPT_REQUEST, self::ACCEPT_RESPONSE, self::ACCEPT_VALUE |
120
|
712 |
|
* @param array $options passed to the xml parser, in addition to the options received in the constructor |
121
|
2 |
|
* @return void |
122
|
2 |
|
*/ |
123
|
2 |
|
public function parse($data, $returnType = self::RETURN_XMLRPCVALS, $accept = 3, $options = array()) |
124
|
|
|
{ |
125
|
|
|
$this->_xh = array( |
126
|
710 |
|
'ac' => '', |
127
|
|
|
'stack' => array(), |
128
|
710 |
|
'valuestack' => array(), |
129
|
|
|
'isf' => 0, |
130
|
|
|
'isf_reason' => '', |
131
|
710 |
|
'value' => null, |
132
|
709 |
|
'method' => false, // so we can check later if we got a methodname or not |
133
|
|
|
'params' => array(), |
134
|
|
|
'pt' => array(), |
135
|
710 |
|
'rt' => '', |
136
|
|
|
); |
137
|
710 |
|
|
138
|
|
|
$len = strlen($data); |
139
|
|
|
|
140
|
710 |
|
// we test for empty documents here to save on resource allocation and simply the chunked-parsing loop below |
141
|
27 |
|
if ($len == 0) { |
142
|
27 |
|
$this->_xh['isf'] = 3; |
143
|
708 |
|
$this->_xh['isf_reason'] = 'XML error 5: empty document'; |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
708 |
|
// NB: we use '' instead of null to force charset detection from the xml declaration |
148
|
|
|
$parser = xml_parser_create(''); |
149
|
|
|
|
150
|
710 |
|
foreach ($this->parsing_options as $key => $val) { |
151
|
710 |
|
xml_parser_set_option($parser, $key, $val); |
152
|
|
|
} |
153
|
710 |
|
foreach ($options as $key => $val) { |
154
|
|
|
xml_parser_set_option($parser, $key, $val); |
155
|
|
|
} |
156
|
710 |
|
// always set this, in case someone tries to disable it via options... |
157
|
710 |
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1); |
158
|
|
|
|
159
|
710 |
|
xml_set_object($parser, $this); |
160
|
3 |
|
|
161
|
3 |
|
switch ($returnType) { |
162
|
3 |
|
case self::RETURN_PHP: |
163
|
|
|
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); |
164
|
3 |
|
break; |
165
|
3 |
|
case self::RETURN_EPIVALS: |
166
|
3 |
|
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_epi'); |
167
|
|
|
break; |
168
|
|
|
default: |
169
|
|
|
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
170
|
710 |
|
} |
171
|
710 |
|
|
172
|
|
|
xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
173
|
|
|
xml_set_default_handler($parser, 'xmlrpc_dh'); |
174
|
|
|
|
175
|
|
|
$this->accept = $accept; |
176
|
|
|
|
177
|
|
|
// @see ticket #70 - we have to parse big xml docks in chunks to avoid errors |
178
|
|
|
for ($offset = 0; $offset < $len; $offset += $this->maxChunkLength) { |
179
|
|
|
$chunk = substr($data, $offset, $this->maxChunkLength); |
180
|
|
|
// error handling: xml not well formed |
181
|
710 |
|
if (!xml_parse($parser, $chunk, $offset + $this->maxChunkLength >= $len)) { |
182
|
|
|
$errCode = xml_get_error_code($parser); |
183
|
|
|
$errStr = sprintf('XML error %s: %s at line %d, column %d', $errCode, xml_error_string($errCode), |
184
|
710 |
|
xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
185
|
|
|
|
186
|
|
|
$this->_xh['isf'] = 3; |
187
|
710 |
|
$this->_xh['isf_reason'] = $errStr; |
188
|
|
|
break; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
710 |
|
xml_parser_free($parser); |
193
|
710 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* xml parser handler function for opening element tags. |
197
|
710 |
|
* @internal |
198
|
708 |
|
* |
199
|
3 |
|
* @param resource $parser |
200
|
703 |
|
* @param string $name |
201
|
710 |
|
* @param $attrs |
202
|
|
|
* @param bool $acceptSingleVals DEPRECATED use the $accept parameter instead |
203
|
2 |
|
* @return void |
204
|
2 |
|
*/ |
205
|
|
|
public function xmlrpc_se($parser, $name, $attrs, $acceptSingleVals = false) |
|
|
|
|
206
|
703 |
|
{ |
207
|
|
|
// if invalid xmlrpc already detected, skip all processing |
208
|
|
|
if ($this->_xh['isf'] < 2) { |
209
|
|
|
|
210
|
710 |
|
// check for correct element nesting |
211
|
710 |
|
if (count($this->_xh['stack']) == 0) { |
212
|
2 |
|
// top level element can only be of 2 types |
213
|
2 |
|
/// @todo optimization creep: save this check into a bool variable, instead of using count() every time: |
214
|
|
|
/// there is only a single top level element in xml anyway |
215
|
2 |
|
// BC |
216
|
|
|
if ($acceptSingleVals === false) { |
217
|
|
|
$accept = $this->accept; |
218
|
|
|
} else { |
219
|
710 |
|
$accept = self::ACCEPT_REQUEST | self::ACCEPT_RESPONSE | self::ACCEPT_VALUE; |
220
|
|
|
} |
221
|
710 |
|
if (($name == 'METHODCALL' && ($accept & self::ACCEPT_REQUEST)) || |
222
|
|
|
($name == 'METHODRESPONSE' && ($accept & self::ACCEPT_RESPONSE)) || |
223
|
708 |
|
($name == 'VALUE' && ($accept & self::ACCEPT_VALUE)) || |
224
|
708 |
|
($name == 'FAULT' && ($accept & self::ACCEPT_FAULT))) { |
225
|
708 |
|
$this->_xh['rt'] = strtolower($name); |
226
|
708 |
|
} else { |
227
|
708 |
|
$this->_xh['isf'] = 2; |
228
|
710 |
|
$this->_xh['isf_reason'] = 'missing top level xmlrpc element. Found: ' . $name; |
229
|
710 |
|
|
230
|
1 |
|
return; |
231
|
|
|
} |
232
|
|
|
} else { |
233
|
|
|
// not top level element: see if parent is OK |
234
|
|
|
$parent = end($this->_xh['stack']); |
235
|
|
|
if (!array_key_exists($name, $this->xmlrpc_valid_parents) || !in_array($parent, $this->xmlrpc_valid_parents[$name])) { |
236
|
|
|
$this->_xh['isf'] = 2; |
237
|
|
|
$this->_xh['isf_reason'] = "xmlrpc element $name cannot be child of $parent"; |
238
|
710 |
|
|
239
|
710 |
|
return; |
240
|
710 |
|
} |
241
|
710 |
|
} |
242
|
710 |
|
|
243
|
710 |
|
switch ($name) { |
244
|
710 |
|
// optimize for speed switch cases: most common cases first |
245
|
685 |
|
case 'VALUE': |
246
|
|
|
/// @todo we could check for 2 VALUE elements inside a MEMBER or PARAM element |
247
|
1 |
|
$this->_xh['vt'] = 'value'; // indicator: no value found yet |
248
|
1 |
|
$this->_xh['ac'] = ''; |
249
|
|
|
$this->_xh['lv'] = 1; |
250
|
1 |
|
$this->_xh['php_class'] = null; |
251
|
|
|
break; |
252
|
685 |
|
case 'I8': |
253
|
685 |
|
case 'EX:I8': |
254
|
710 |
|
if (PHP_INT_SIZE === 4) { |
255
|
710 |
|
// INVALID ELEMENT: RAISE ISF so that it is later recognized!!! |
256
|
400 |
|
$this->_xh['isf'] = 2; |
257
|
|
|
$this->_xh['isf_reason'] = "Received i8 element but php is compiled in 32 bit mode"; |
258
|
1 |
|
|
259
|
1 |
|
return; |
260
|
|
|
} |
261
|
1 |
|
// fall through voluntarily |
262
|
|
|
case 'I4': |
263
|
|
|
case 'INT': |
264
|
399 |
|
case 'STRING': |
265
|
399 |
|
case 'BOOLEAN': |
266
|
399 |
|
case 'DOUBLE': |
267
|
|
|
case 'DATETIME.ISO8601': |
268
|
|
|
case 'BASE64': |
269
|
399 |
|
if ($this->_xh['vt'] != 'value') { |
270
|
22 |
|
// two data elements inside a value: an error occurred! |
271
|
|
|
$this->_xh['isf'] = 2; |
272
|
399 |
|
$this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; |
273
|
399 |
|
|
274
|
399 |
|
return; |
275
|
710 |
|
} |
276
|
239 |
|
$this->_xh['ac'] = ''; // reset the accumulator |
277
|
|
|
break; |
278
|
1 |
|
case 'STRUCT': |
279
|
1 |
|
case 'ARRAY': |
280
|
|
|
if ($this->_xh['vt'] != 'value') { |
281
|
1 |
|
// two data elements inside a value: an error occurred! |
282
|
|
|
$this->_xh['isf'] = 2; |
283
|
710 |
|
$this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; |
284
|
710 |
|
|
285
|
710 |
|
return; |
286
|
|
|
} |
287
|
710 |
|
// create an empty array to hold child values, and push it onto appropriate stack |
288
|
710 |
|
$curVal = array(); |
289
|
710 |
|
$curVal['values'] = array(); |
290
|
|
|
$curVal['type'] = $name; |
291
|
637 |
|
// check for out-of-band information to rebuild php objs |
292
|
637 |
|
// and in case it is found, save it |
293
|
710 |
|
if (@isset($attrs['PHP_CLASS'])) { |
294
|
109 |
|
$curVal['php_class'] = $attrs['PHP_CLASS']; |
295
|
109 |
|
} |
296
|
710 |
|
$this->_xh['valuestack'][] = $curVal; |
297
|
|
|
$this->_xh['vt'] = 'data'; // be prepared for a data element next |
298
|
289 |
|
break; |
299
|
|
|
case 'DATA': |
300
|
|
|
if ($this->_xh['vt'] != 'data') { |
301
|
688 |
|
// two data elements inside a value: an error occurred! |
302
|
|
|
$this->_xh['isf'] = 2; |
303
|
710 |
|
$this->_xh['isf_reason'] = "found two data elements inside an array element"; |
304
|
710 |
|
|
305
|
23 |
|
return; |
306
|
23 |
|
} |
307
|
23 |
|
case 'METHODCALL': |
308
|
23 |
|
case 'METHODRESPONSE': |
309
|
|
|
case 'PARAMS': |
310
|
|
|
// valid elements that add little to processing |
311
|
|
|
break; |
312
|
|
|
case 'METHODNAME': |
313
|
|
|
case 'NAME': |
314
|
|
|
/// @todo we could check for 2 NAME elements inside a MEMBER element |
315
|
23 |
|
$this->_xh['ac'] = ''; |
316
|
23 |
|
break; |
317
|
|
|
case 'FAULT': |
318
|
|
|
$this->_xh['isf'] = 1; |
319
|
|
|
break; |
320
|
|
|
case 'MEMBER': |
321
|
|
|
// set member name to null, in case we do not find in the xml later on |
322
|
1 |
|
$this->_xh['valuestack'][count($this->_xh['valuestack']) - 1]['name'] = ''; |
323
|
1 |
|
//$this->_xh['ac']=''; |
324
|
1 |
|
// Drop trough intentionally |
325
|
|
|
case 'PARAM': |
326
|
|
|
// clear value type, so we can check later if no value has been passed for this param/member |
327
|
|
|
$this->_xh['vt'] = null; |
328
|
710 |
|
break; |
329
|
|
|
case 'NIL': |
330
|
|
|
case 'EX:NIL': |
331
|
710 |
|
if (PhpXmlRpc::$xmlrpc_null_extension) { |
332
|
710 |
|
if ($this->_xh['vt'] != 'value') { |
333
|
|
|
// two data elements inside a value: an error occurred! |
334
|
|
|
$this->_xh['isf'] = 2; |
335
|
710 |
|
$this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; |
336
|
|
|
|
337
|
|
|
return; |
338
|
|
|
} |
339
|
|
|
$this->_xh['ac'] = ''; // reset the accumulator |
340
|
|
|
break; |
341
|
|
|
} |
342
|
|
|
// if here, we do not support the <NIL/> extension, so |
343
|
|
|
// drop through intentionally |
344
|
|
|
default: |
345
|
|
|
// INVALID ELEMENT: RAISE ISF so that it is later recognized!!! |
346
|
|
|
$this->_xh['isf'] = 2; |
347
|
|
|
$this->_xh['isf_reason'] = "found not-xmlrpc xml element $name"; |
348
|
|
|
break; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// Save current element name to stack, to validate nesting |
352
|
|
|
$this->_xh['stack'][] = $name; |
353
|
|
|
|
354
|
|
|
/// @todo optimization creep: move this inside the big switch() above |
355
|
|
|
if ($name != 'VALUE') { |
356
|
|
|
$this->_xh['lv'] = 0; |
357
|
710 |
|
} |
358
|
|
|
} |
359
|
710 |
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* xml parser handler function for opening element tags. |
363
|
|
|
* Used in decoding xml chunks that might represent single xmlrpc values as well as requests, responses. |
364
|
709 |
|
* @deprecated |
365
|
|
|
* @param resource $parser |
366
|
709 |
|
* @param $name |
367
|
709 |
|
* @param $attrs |
368
|
|
|
* @return void |
369
|
707 |
|
*/ |
370
|
30 |
|
public function xmlrpc_se_any($parser, $name, $attrs) |
371
|
30 |
|
{ |
372
|
|
|
$this->xmlrpc_se($parser, $name, $attrs, true); |
373
|
|
|
} |
374
|
707 |
|
|
375
|
|
|
/** |
376
|
705 |
|
* xml parser handler function for close element tags. |
377
|
|
|
* @internal |
378
|
|
|
* |
379
|
705 |
|
* @param resource $parser |
380
|
22 |
|
* @param string $name |
381
|
|
|
* @param int $rebuildXmlrpcvals >1 for rebuilding xmlrpcvals, 0 for rebuilding php values, -1 for xmlrpc-extension compatibility |
382
|
705 |
|
* @return void |
383
|
27 |
|
*/ |
384
|
|
|
public function xmlrpc_ee($parser, $name, $rebuildXmlrpcvals = 1) |
|
|
|
|
385
|
|
|
{ |
386
|
|
|
if ($this->_xh['isf'] < 2) { |
387
|
|
|
// push this element name from stack |
388
|
|
|
// NB: if XML validates, correct opening/closing is guaranteed and |
389
|
|
|
// we do not have to check for $name == $currElem. |
390
|
|
|
// we also checked for proper nesting at start of elements... |
391
|
|
|
$currElem = array_pop($this->_xh['stack']); |
|
|
|
|
392
|
|
|
|
393
|
|
|
switch ($name) { |
394
|
|
|
case 'VALUE': |
395
|
|
|
// This if() detects if no scalar was inside <VALUE></VALUE> |
396
|
|
|
if ($this->_xh['vt'] == 'value') { |
397
|
|
|
$this->_xh['value'] = $this->_xh['ac']; |
398
|
|
|
$this->_xh['vt'] = Value::$xmlrpcString; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if ($rebuildXmlrpcvals > 0) { |
402
|
|
|
// build the xmlrpc val out of the data received, and substitute it |
403
|
|
|
$temp = new Value($this->_xh['value'], $this->_xh['vt']); |
404
|
|
|
// in case we got info about underlying php class, save it |
405
|
|
|
// in the object we're rebuilding |
406
|
707 |
|
if (isset($this->_xh['php_class'])) { |
407
|
707 |
|
$temp->_php_class = $this->_xh['php_class']; |
408
|
239 |
|
} |
409
|
|
|
$this->_xh['value'] = $temp; |
410
|
707 |
|
} elseif ($rebuildXmlrpcvals < 0) { |
411
|
709 |
|
if ($this->_xh['vt'] == Value::$xmlrpcDateTime) { |
412
|
709 |
|
$this->_xh['value'] = (object)array( |
413
|
709 |
|
'xmlrpc_type' => 'datetime', |
414
|
709 |
|
'scalar' => $this->_xh['value'], |
415
|
709 |
|
'timestamp' => \PhpXmlRpc\Helper\Date::iso8601Decode($this->_xh['value']) |
416
|
709 |
|
); |
417
|
708 |
|
} elseif ($this->_xh['vt'] == Value::$xmlrpcBase64) { |
418
|
708 |
|
$this->_xh['value'] = (object)array( |
419
|
708 |
|
'xmlrpc_type' => 'base64', |
420
|
685 |
|
'scalar' => $this->_xh['value'] |
421
|
|
|
); |
422
|
|
|
} |
423
|
685 |
|
} else { |
424
|
594 |
|
/// @todo this should handle php-serialized objects, |
425
|
477 |
|
/// since std deserializing is done by php_xmlrpc_decode, |
426
|
7 |
|
/// which we will not be calling... |
427
|
|
|
//if (isset($this->_xh['php_class'])) { |
428
|
|
|
//} |
429
|
7 |
|
} |
430
|
7 |
|
|
431
|
472 |
|
// check if we are inside an array or struct: |
432
|
|
|
// if value just built is inside an array, let's move it into array on the stack |
433
|
22 |
|
$vscount = count($this->_xh['valuestack']); |
434
|
451 |
|
if ($vscount && $this->_xh['valuestack'][$vscount - 1]['type'] == 'ARRAY') { |
435
|
|
|
$this->_xh['valuestack'][$vscount - 1]['values'][] = $this->_xh['value']; |
436
|
|
|
} |
437
|
|
|
break; |
438
|
|
|
case 'BOOLEAN': |
439
|
|
|
case 'I4': |
440
|
|
|
case 'I8': |
441
|
46 |
|
case 'EX:I8': |
442
|
46 |
|
case 'INT': |
443
|
|
|
case 'STRING': |
444
|
|
|
case 'DOUBLE': |
445
|
24 |
|
case 'DATETIME.ISO8601': |
446
|
|
|
case 'BASE64': |
447
|
|
|
$this->_xh['vt'] = strtolower($name); |
448
|
46 |
|
/// @todo: optimization creep - remove the if/elseif cycle below |
449
|
|
|
/// since the case() in which we are already did that |
450
|
408 |
|
if ($name == 'STRING') { |
451
|
|
|
$this->_xh['value'] = $this->_xh['ac']; |
452
|
|
|
} elseif ($name == 'DATETIME.ISO8601') { |
453
|
|
|
if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $this->_xh['ac'])) { |
454
|
25 |
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in DATETIME: ' . $this->_xh['ac']); |
455
|
|
|
} |
456
|
|
|
$this->_xh['vt'] = Value::$xmlrpcDateTime; |
457
|
|
|
$this->_xh['value'] = $this->_xh['ac']; |
458
|
|
|
} elseif ($name == 'BASE64') { |
459
|
|
|
/// @todo check for failure of base64 decoding / catch warnings |
460
|
25 |
|
$this->_xh['value'] = base64_decode($this->_xh['ac']); |
461
|
|
|
} elseif ($name == 'BOOLEAN') { |
462
|
|
|
// special case here: we translate boolean 1 or 0 into PHP |
463
|
|
|
// constants true or false. |
464
|
|
|
// Strings 'true' and 'false' are accepted, even though the |
465
|
387 |
|
// spec never mentions them (see eg. Blogger api docs) |
466
|
|
|
// NB: this simple checks helps a lot sanitizing input, ie no |
467
|
|
|
// security problems around here |
468
|
|
|
if ($this->_xh['ac'] == '1' || strcasecmp($this->_xh['ac'], 'true') == 0) { |
469
|
|
|
$this->_xh['value'] = true; |
470
|
|
|
} else { |
471
|
387 |
|
// log if receiving something strange, even though we set the value to false anyway |
472
|
|
|
if ($this->_xh['ac'] != '0' && strcasecmp($this->_xh['ac'], 'false') != 0) { |
473
|
|
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in BOOLEAN: ' . $this->_xh['ac']); |
474
|
685 |
|
} |
475
|
685 |
|
$this->_xh['value'] = false; |
476
|
708 |
|
} |
477
|
289 |
|
} elseif ($name == 'DOUBLE') { |
478
|
289 |
|
// we have a DOUBLE |
479
|
708 |
|
// we must check that only 0123456789-.<space> are characters here |
480
|
|
|
// NOTE: regexp could be much stricter than this... |
481
|
|
|
if (!preg_match('/^[+-eE0123456789 \t.]+$/', $this->_xh['ac'])) { |
482
|
289 |
|
/// @todo: find a better way of throwing an error than this! |
483
|
268 |
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in DOUBLE: ' . $this->_xh['ac']); |
484
|
268 |
|
$this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; |
485
|
|
|
} else { |
486
|
22 |
|
// it's ok, add it on |
487
|
|
|
$this->_xh['value'] = (double)$this->_xh['ac']; |
488
|
289 |
|
} |
489
|
708 |
|
} else { |
490
|
239 |
|
// we have an I4/I8/INT |
491
|
239 |
|
// we must check that only 0123456789-<space> are characters here |
492
|
707 |
|
if (!preg_match('/^[+-]?[0123456789 \t]+$/', $this->_xh['ac'])) { |
493
|
707 |
|
/// @todo find a better way of throwing an error than this! |
494
|
|
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in INT: ' . $this->_xh['ac']); |
495
|
398 |
|
$this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; |
496
|
398 |
|
} else { |
497
|
398 |
|
// it's ok, add it on |
498
|
398 |
|
$this->_xh['value'] = (int)$this->_xh['ac']; |
499
|
22 |
|
} |
500
|
|
|
} |
501
|
398 |
|
$this->_xh['lv'] = 3; // indicate we've found a value |
502
|
707 |
|
break; |
503
|
|
|
case 'NAME': |
504
|
|
|
$this->_xh['valuestack'][count($this->_xh['valuestack']) - 1]['name'] = $this->_xh['ac']; |
505
|
684 |
|
break; |
506
|
684 |
|
case 'MEMBER': |
507
|
684 |
|
// add to array in the stack the last element built, |
508
|
|
|
// unless no VALUE was found |
509
|
|
|
if ($this->_xh['vt']) { |
510
|
|
|
$vscount = count($this->_xh['valuestack']); |
511
|
684 |
|
$this->_xh['valuestack'][$vscount - 1]['values'][$this->_xh['valuestack'][$vscount - 1]['name']] = $this->_xh['value']; |
512
|
707 |
|
} else { |
513
|
562 |
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside STRUCT in received xml'); |
514
|
562 |
|
} |
515
|
706 |
|
break; |
516
|
706 |
|
case 'DATA': |
517
|
23 |
|
$this->_xh['vt'] = null; // reset this to check for 2 data elements in a row - even if they're empty |
518
|
23 |
|
break; |
519
|
23 |
|
case 'STRUCT': |
520
|
23 |
|
case 'ARRAY': |
521
|
23 |
|
// fetch out of stack array of values, and promote it to current value |
522
|
|
|
$currVal = array_pop($this->_xh['valuestack']); |
523
|
|
|
$this->_xh['value'] = $currVal['values']; |
524
|
706 |
|
$this->_xh['vt'] = strtolower($name); |
525
|
706 |
|
if (isset($currVal['php_class'])) { |
526
|
706 |
|
$this->_xh['php_class'] = $currVal['php_class']; |
527
|
705 |
|
} |
528
|
706 |
|
break; |
529
|
|
|
case 'PARAM': |
530
|
|
|
// add to array of params the current value, |
531
|
|
|
// unless no VALUE was found |
532
|
705 |
|
if ($this->_xh['vt']) { |
533
|
|
|
$this->_xh['params'][] = $this->_xh['value']; |
534
|
|
|
$this->_xh['pt'][] = $this->_xh['vt']; |
535
|
710 |
|
} else { |
536
|
|
|
$this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside PARAM in received xml'); |
537
|
|
|
} |
538
|
|
|
break; |
539
|
|
|
case 'METHODNAME': |
540
|
|
|
$this->_xh['method'] = preg_replace('/^[\n\r\t ]+/', '', $this->_xh['ac']); |
541
|
|
|
break; |
542
|
|
|
case 'NIL': |
543
|
27 |
|
case 'EX:NIL': |
544
|
|
|
if (PhpXmlRpc::$xmlrpc_null_extension) { |
545
|
27 |
|
$this->_xh['vt'] = 'null'; |
546
|
27 |
|
$this->_xh['value'] = null; |
547
|
|
|
$this->_xh['lv'] = 3; |
548
|
|
|
break; |
549
|
|
|
} |
550
|
|
|
// drop through intentionally if nil extension not enabled |
551
|
|
|
case 'PARAMS': |
552
|
|
|
case 'FAULT': |
553
|
|
|
case 'METHODCALL': |
554
|
|
|
case 'METHORESPONSE': |
555
|
|
|
break; |
556
|
|
|
default: |
557
|
|
|
// End of INVALID ELEMENT! |
558
|
|
|
// shall we add an assert here for unreachable code??? |
559
|
|
|
break; |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
710 |
|
* Used in decoding xmlrpc requests/responses without rebuilding xmlrpc Values. |
566
|
|
|
* @internal |
567
|
|
|
* |
568
|
710 |
|
* @param resource $parser |
569
|
|
|
* @param string $name |
570
|
|
|
* @return void |
571
|
710 |
|
*/ |
572
|
710 |
|
public function xmlrpc_ee_fast($parser, $name) |
573
|
|
|
{ |
574
|
|
|
$this->xmlrpc_ee($parser, $name, 0); |
575
|
710 |
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Used in decoding xmlrpc requests/responses while building xmlrpc-extension Values (plain php for all but base64 and datetime). |
579
|
|
|
* @internal |
580
|
|
|
* |
581
|
|
|
* @param resource $parser |
582
|
|
|
* @param string $name |
583
|
|
|
* @return void |
584
|
696 |
|
*/ |
585
|
|
|
public function xmlrpc_ee_epi($parser, $name) |
586
|
|
|
{ |
587
|
696 |
|
$this->xmlrpc_ee($parser, $name, -1); |
588
|
696 |
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* xml parser handler function for character data. |
592
|
|
|
* @internal |
593
|
|
|
* |
594
|
696 |
|
* @param resource $parser |
595
|
|
|
* @param string $data |
596
|
|
|
* @return void |
597
|
|
|
*/ |
598
|
|
|
public function xmlrpc_cd($parser, $data) |
|
|
|
|
599
|
|
|
{ |
600
|
|
|
// skip processing if xml fault already detected |
601
|
|
|
if ($this->_xh['isf'] < 2) { |
602
|
|
|
// "lookforvalue==3" means that we've found an entire value |
603
|
|
|
// and should discard any further character data |
604
|
|
|
if ($this->_xh['lv'] != 3) { |
605
|
|
|
$this->_xh['ac'] .= $data; |
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* xml parser handler function for 'other stuff', ie. not char data or element start/end tag. |
612
|
|
|
* In fact it only gets called on unknown entities... |
613
|
|
|
* @internal |
614
|
|
|
* |
615
|
|
|
* @param $parser |
616
|
|
|
* @param string data |
|
|
|
|
617
|
711 |
|
* @return void |
618
|
|
|
*/ |
619
|
|
|
public function xmlrpc_dh($parser, $data) |
|
|
|
|
620
|
|
|
{ |
621
|
|
|
// skip processing if xml fault already detected |
622
|
|
|
if ($this->_xh['isf'] < 2) { |
623
|
|
|
if (substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';') { |
624
|
|
|
$this->_xh['ac'] .= $data; |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
//return true; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* xml charset encoding guessing helper function. |
633
|
|
|
* Tries to determine the charset encoding of an XML chunk received over HTTP. |
634
|
711 |
|
* NB: according to the spec (RFC 3023), if text/xml content-type is received over HTTP without a content-type, |
635
|
711 |
|
* we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of non-conforming (legacy?) clients/servers, |
636
|
695 |
|
* which will be most probably using UTF-8 anyway... |
637
|
|
|
* In order of importance checks: |
638
|
|
|
* 1. http headers |
639
|
|
|
* 2. BOM |
640
|
|
|
* 3. XML declaration |
641
|
|
|
* 4. guesses using mb_detect_encoding() |
642
|
|
|
* |
643
|
|
|
* @param string $httpHeader the http Content-type header |
644
|
|
|
* @param string $xmlChunk xml content buffer |
645
|
|
|
* @param string $encodingPrefs comma separated list of character encodings to be used as default (when mb extension is enabled). |
646
|
529 |
|
* This can also be set globally using PhpXmlRpc::$xmlrpc_detectencodings |
647
|
|
|
* @return string the encoding determined. Null if it can't be determined and mbstring is enabled, |
648
|
529 |
|
* PhpXmlRpc::$xmlrpc_defencoding if it can't be determined and mbstring is not enabled |
649
|
|
|
* |
650
|
529 |
|
* @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!! |
651
|
|
|
*/ |
652
|
|
|
public static function guessEncoding($httpHeader = '', $xmlChunk = '', $encodingPrefs = null) |
653
|
|
|
{ |
654
|
|
|
// discussion: see http://www.yale.edu/pclt/encoding/ |
655
|
|
|
// 1 - test if encoding is specified in HTTP HEADERS |
656
|
|
|
|
657
|
|
|
// Details: |
658
|
529 |
|
// LWS: (\13\10)?( |\t)+ |
659
|
529 |
|
// token: (any char but excluded stuff)+ |
660
|
|
|
// quoted string: " (any char but double quotes and control chars)* " |
661
|
24 |
|
// header: Content-type = ...; charset=value(; ...)* |
662
|
|
|
// where value is of type token, no LWS allowed between 'charset' and value |
663
|
|
|
// Note: we do not check for invalid chars in VALUE: |
664
|
|
|
// this had better be done using pure ereg as below |
665
|
506 |
|
// Note 2: we might be removing whitespace/tabs that ought to be left in if |
666
|
506 |
|
// the received charset is a quoted string. But nobody uses such charset names... |
667
|
4 |
|
|
668
|
|
|
/// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it? |
669
|
506 |
|
$matches = array(); |
670
|
4 |
|
if (preg_match('/;\s*charset\s*=([^;]+)/i', $httpHeader, $matches)) { |
671
|
|
|
return strtoupper(trim($matches[1], " \t\"")); |
672
|
502 |
|
} |
673
|
|
|
|
674
|
|
|
// 2 - scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
675
|
|
|
// (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
676
|
506 |
|
// NOTE: actually, according to the spec, even if we find the BOM and determine |
677
|
499 |
|
// an encoding, we should check if there is an encoding specified |
678
|
|
|
// in the xml declaration, and verify if they match. |
679
|
|
|
/// @todo implement check as described above? |
680
|
506 |
|
/// @todo implement check for first bytes of string even without a BOM? (It sure looks harder than for cases WITH a BOM) |
681
|
|
|
if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) { |
682
|
|
|
return 'UCS-4'; |
683
|
|
|
} elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) { |
684
|
|
|
return 'UTF-16'; |
685
|
|
|
} elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) { |
686
|
|
|
return 'UTF-8'; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
// 3 - test if encoding is specified in the xml declaration |
690
|
|
|
/// @todo this regexp will fail if $xmlChunk uses UTF-32/UCS-4, and most likely UTF-16/UCS-2 as well. In that |
691
|
|
|
/// case we leave the guesswork up to mbstring - which seems to be able to detect it, starting with php 5.6. |
692
|
|
|
/// For lower versions, we could attempt usage of mb_ereg... |
693
|
|
|
// Details: |
694
|
|
|
// SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
695
|
|
|
// EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
696
|
82 |
|
if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . |
697
|
|
|
'\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
698
|
|
|
$xmlChunk, $matches)) { |
699
|
|
|
return strtoupper(substr($matches[2], 1, -1)); |
700
|
82 |
|
} |
701
|
|
|
|
702
|
82 |
|
// 4 - if mbstring is available, let it do the guesswork |
703
|
|
|
if (function_exists('mb_detect_encoding')) { |
704
|
82 |
|
if ($encodingPrefs == null && PhpXmlRpc::$xmlrpc_detectencodings != null) { |
|
|
|
|
705
|
|
|
$encodingPrefs = PhpXmlRpc::$xmlrpc_detectencodings; |
706
|
|
|
} |
707
|
|
|
if ($encodingPrefs) { |
708
|
|
|
$enc = mb_detect_encoding($xmlChunk, $encodingPrefs); |
709
|
|
|
} else { |
710
|
|
|
$enc = mb_detect_encoding($xmlChunk); |
711
|
|
|
} |
712
|
82 |
|
// NB: mb_detect likes to call it ascii, xml parser likes to call it US_ASCII... |
713
|
82 |
|
// IANA also likes better US-ASCII, so go with it |
714
|
|
|
if ($enc == 'ASCII') { |
715
|
78 |
|
$enc = 'US-' . $enc; |
716
|
|
|
} |
717
|
|
|
|
718
|
5 |
|
return $enc; |
719
|
|
|
} else { |
720
|
|
|
// no encoding specified: as per HTTP1.1 assume it is iso-8859-1? |
721
|
|
|
// Both RFC 2616 (HTTP 1.1) and 1945 (HTTP 1.0) clearly state that for text/xxx content types |
722
|
|
|
// this should be the standard. And we should be getting text/xml as request and response. |
723
|
|
|
// BUT we have to be backward compatible with the lib, which always used UTF-8 as default... |
724
|
|
|
return PhpXmlRpc::$xmlrpc_defencoding; |
725
|
|
|
} |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Helper function: checks if an xml chunk has a charset declaration (BOM or in the xml declaration). |
730
|
|
|
* |
731
|
|
|
* @param string $xmlChunk |
732
|
|
|
* @return bool |
733
|
|
|
*/ |
734
|
|
|
public static function hasEncoding($xmlChunk) |
735
|
|
|
{ |
736
|
|
|
// scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
737
|
|
|
// (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
738
|
|
|
if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) { |
739
|
|
|
return true; |
740
|
|
|
} elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) { |
741
|
|
|
return true; |
742
|
|
|
} elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) { |
743
|
|
|
return true; |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
// test if encoding is specified in the xml declaration |
747
|
|
|
// Details: |
748
|
|
|
// SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
749
|
|
|
// EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
750
|
|
|
if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . |
751
|
|
|
'\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
752
|
|
|
$xmlChunk, $matches)) { |
753
|
|
|
return true; |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
return false; |
757
|
|
|
} |
758
|
|
|
} |
759
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.