1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpXmlRpc; |
4
|
|
|
|
5
|
|
|
use PhpXmlRpc\Helper\XMLParser; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A helper class to easily convert between Value objects and php native values |
9
|
|
|
*/ |
10
|
|
|
class Encoder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Takes an xmlrpc value in object format and translates it into native PHP types. |
14
|
|
|
* |
15
|
|
|
* Works with xmlrpc requests objects as input, too. |
16
|
|
|
* |
17
|
|
|
* Given proper options parameter, can rebuild generic php object instances |
18
|
|
|
* (provided those have been encoded to xmlrpc format using a corresponding |
19
|
|
|
* option in php_xmlrpc_encode()) |
20
|
|
|
* PLEASE NOTE that rebuilding php objects involves calling their constructor function. |
21
|
|
|
* This means that the remote communication end can decide which php code will |
22
|
|
|
* get executed on your server, leaving the door possibly open to 'php-injection' |
23
|
|
|
* style of attacks (provided you have some classes defined on your server that |
24
|
|
|
* might wreak havoc if instances are built outside an appropriate context). |
25
|
|
|
* Make sure you trust the remote server/client before eanbling this! |
26
|
|
|
* |
27
|
|
|
* @author Dan Libby ([email protected]) |
28
|
|
|
* |
29
|
|
|
* @param Value|Request $xmlrpcVal |
30
|
|
|
* @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects; if 'dates_as_objects' is set xmlrpc datetimes are decoded as php DateTime objects (standard is |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
231 |
|
public function decode($xmlrpcVal, $options = array()) |
35
|
|
|
{ |
36
|
231 |
|
switch ($xmlrpcVal->kindOf()) { |
37
|
231 |
|
case 'scalar': |
38
|
231 |
|
if (in_array('extension_api', $options)) { |
39
|
|
|
$val = reset($xmlrpcVal->me); |
40
|
|
|
$typ = key($xmlrpcVal->me); |
41
|
|
|
switch ($typ) { |
42
|
|
|
case 'dateTime.iso8601': |
43
|
|
|
$xmlrpcVal->scalar = $val; |
44
|
|
|
$xmlrpcVal->type = 'datetime'; |
45
|
|
|
$xmlrpcVal->timestamp = \PhpXmlRpc\Helper\Date::iso8601Decode($val); |
46
|
|
|
|
47
|
|
|
return $xmlrpcVal; |
48
|
|
|
case 'base64': |
49
|
|
|
$xmlrpcVal->scalar = $val; |
50
|
|
|
$xmlrpcVal->type = $typ; |
51
|
|
|
|
52
|
|
|
return $xmlrpcVal; |
53
|
|
|
default: |
54
|
|
|
return $xmlrpcVal->scalarval(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
231 |
|
if (in_array('dates_as_objects', $options) && $xmlrpcVal->scalartyp() == 'dateTime.iso8601') { |
|
|
|
|
58
|
|
|
// we return a Datetime object instead of a string |
59
|
|
|
// since now the constructor of xmlrpc value accepts safely strings, ints and datetimes, |
60
|
|
|
// we cater to all 3 cases here |
61
|
|
|
$out = $xmlrpcVal->scalarval(); |
62
|
|
|
if (is_string($out)) { |
63
|
|
|
$out = strtotime($out); |
64
|
|
|
} |
65
|
|
|
if (is_int($out)) { |
66
|
|
|
$result = new \DateTime(); |
67
|
|
|
$result->setTimestamp($out); |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} elseif (is_a($out, 'DateTimeInterface')) { |
71
|
|
|
return $out; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
231 |
|
return $xmlrpcVal->scalarval(); |
76
|
231 |
|
case 'array': |
77
|
96 |
|
$arr = array(); |
78
|
96 |
|
foreach($xmlrpcVal as $value) { |
|
|
|
|
79
|
96 |
|
$arr[] = $this->decode($value, $options); |
80
|
96 |
|
} |
81
|
|
|
|
82
|
96 |
|
return $arr; |
83
|
174 |
|
case 'struct': |
|
|
|
|
84
|
|
|
// If user said so, try to rebuild php objects for specific struct vals. |
85
|
|
|
/// @todo should we raise a warning for class not found? |
86
|
|
|
// shall we check for proper subclass of xmlrpc value instead of |
87
|
|
|
// presence of _php_class to detect what we can do? |
88
|
58 |
|
if (in_array('decode_php_objs', $options) && $xmlrpcVal->_php_class != '' |
89
|
39 |
|
&& class_exists($xmlrpcVal->_php_class) |
90
|
39 |
|
) { |
91
|
20 |
|
$obj = @new $xmlrpcVal->_php_class(); |
92
|
20 |
|
foreach ($xmlrpcVal as $key => $value) { |
|
|
|
|
93
|
20 |
|
$obj->$key = $this->decode($value, $options); |
94
|
20 |
|
} |
95
|
|
|
|
96
|
20 |
|
return $obj; |
97
|
|
|
} else { |
98
|
39 |
|
$arr = array(); |
99
|
39 |
|
foreach ($xmlrpcVal as $key => $value) { |
|
|
|
|
100
|
39 |
|
$arr[$key] = $this->decode($value, $options); |
101
|
39 |
|
} |
102
|
|
|
|
103
|
39 |
|
return $arr; |
104
|
|
|
} |
105
|
117 |
|
case 'msg': |
106
|
117 |
|
$paramCount = $xmlrpcVal->getNumParams(); |
|
|
|
|
107
|
117 |
|
$arr = array(); |
108
|
117 |
|
for ($i = 0; $i < $paramCount; $i++) { |
109
|
117 |
|
$arr[] = $this->decode($xmlrpcVal->getParam($i), $options); |
|
|
|
|
110
|
117 |
|
} |
111
|
|
|
|
112
|
117 |
|
return $arr; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Takes native php types and encodes them into xmlrpc PHP object format. |
118
|
|
|
* It will not re-encode xmlrpc value objects. |
119
|
|
|
* |
120
|
|
|
* Feature creep -- could support more types via optional type argument |
121
|
|
|
* (string => datetime support has been added, ??? => base64 not yet) |
122
|
|
|
* |
123
|
|
|
* If given a proper options parameter, php object instances will be encoded |
124
|
|
|
* into 'special' xmlrpc values, that can later be decoded into php objects |
125
|
|
|
* by calling php_xmlrpc_decode() with a corresponding option |
126
|
|
|
* |
127
|
|
|
* @author Dan Libby ([email protected]) |
128
|
|
|
* |
129
|
|
|
* @param mixed $phpVal the value to be converted into an xmlrpc value object |
130
|
|
|
* @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api' |
131
|
|
|
* |
132
|
|
|
* @return \PhpXmlrpc\Value |
133
|
|
|
*/ |
134
|
216 |
|
public function encode($phpVal, $options = array()) |
135
|
|
|
{ |
136
|
216 |
|
$type = gettype($phpVal); |
137
|
|
|
switch ($type) { |
138
|
216 |
|
case 'string': |
139
|
213 |
|
if (in_array('auto_dates', $options) && preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $phpVal)) { |
140
|
1 |
|
$xmlrpcVal = new Value($phpVal, Value::$xmlrpcDateTime); |
141
|
1 |
|
} else { |
142
|
213 |
|
$xmlrpcVal = new Value($phpVal, Value::$xmlrpcString); |
143
|
|
|
} |
144
|
213 |
|
break; |
145
|
102 |
|
case 'integer': |
146
|
98 |
|
$xmlrpcVal = new Value($phpVal, Value::$xmlrpcInt); |
147
|
98 |
|
break; |
148
|
26 |
|
case 'double': |
149
|
1 |
|
$xmlrpcVal = new Value($phpVal, Value::$xmlrpcDouble); |
150
|
1 |
|
break; |
151
|
|
|
// <G_Giunta_2001-02-29> |
152
|
|
|
// Add support for encoding/decoding of booleans, since they are supported in PHP |
153
|
26 |
|
case 'boolean': |
154
|
1 |
|
$xmlrpcVal = new Value($phpVal, Value::$xmlrpcBoolean); |
155
|
1 |
|
break; |
156
|
|
|
// </G_Giunta_2001-02-29> |
157
|
26 |
|
case 'array': |
158
|
|
|
// PHP arrays can be encoded to either xmlrpc structs or arrays, |
159
|
|
|
// depending on whether they are hashes or plain 0..n integer indexed |
160
|
|
|
// A shorter one-liner would be |
161
|
|
|
// $tmp = array_diff(array_keys($phpVal), range(0, count($phpVal)-1)); |
|
|
|
|
162
|
|
|
// but execution time skyrockets! |
163
|
23 |
|
$j = 0; |
164
|
23 |
|
$arr = array(); |
165
|
23 |
|
$ko = false; |
166
|
23 |
|
foreach ($phpVal as $key => $val) { |
167
|
23 |
|
$arr[$key] = $this->encode($val, $options); |
168
|
23 |
|
if (!$ko && $key !== $j) { |
169
|
22 |
|
$ko = true; |
170
|
22 |
|
} |
171
|
23 |
|
$j++; |
172
|
23 |
|
} |
173
|
23 |
|
if ($ko) { |
174
|
22 |
|
$xmlrpcVal = new Value($arr, Value::$xmlrpcStruct); |
|
|
|
|
175
|
22 |
|
} else { |
176
|
2 |
|
$xmlrpcVal = new Value($arr, Value::$xmlrpcArray); |
|
|
|
|
177
|
|
|
} |
178
|
23 |
|
break; |
179
|
4 |
|
case 'object': |
180
|
2 |
|
if (is_a($phpVal, 'PhpXmlRpc\Value')) { |
181
|
1 |
|
$xmlrpcVal = $phpVal; |
182
|
2 |
|
} elseif (is_a($phpVal, 'DateTimeInterface')) { |
183
|
|
|
$xmlrpcVal = new Value($phpVal->format('Ymd\TH:i:s'), Value::$xmlrpcStruct); |
184
|
|
|
} else { |
185
|
1 |
|
$arr = array(); |
186
|
1 |
|
foreach($phpVal as $k => $v) { |
187
|
1 |
|
$arr[$k] = $this->encode($v, $options); |
188
|
1 |
|
} |
189
|
1 |
|
$xmlrpcVal = new Value($arr, Value::$xmlrpcStruct); |
|
|
|
|
190
|
1 |
|
if (in_array('encode_php_objs', $options)) { |
191
|
|
|
// let's save original class name into xmlrpc value: |
192
|
|
|
// might be useful later on... |
193
|
1 |
|
$xmlrpcVal->_php_class = get_class($phpVal); |
194
|
|
|
} |
195
|
|
|
} |
196
|
2 |
|
break; |
197
|
2 |
|
case 'NULL': |
198
|
2 |
|
if (in_array('extension_api', $options)) { |
199
|
|
|
$xmlrpcVal = new Value('', Value::$xmlrpcString); |
200
|
2 |
|
} elseif (in_array('null_extension', $options)) { |
201
|
|
|
$xmlrpcVal = new Value('', Value::$xmlrpcNull); |
202
|
|
|
} else { |
203
|
2 |
|
$xmlrpcVal = new Value(); |
204
|
|
|
} |
205
|
2 |
|
break; |
206
|
|
|
case 'resource': |
207
|
|
|
if (in_array('extension_api', $options)) { |
208
|
|
|
$xmlrpcVal = new Value((int)$phpVal, Value::$xmlrpcInt); |
209
|
|
|
} else { |
210
|
|
|
$xmlrpcVal = new Value(); |
211
|
|
|
} |
212
|
|
|
break; |
213
|
|
|
// catch "user function", "unknown type" |
|
|
|
|
214
|
|
|
default: |
215
|
|
|
// giancarlo pinerolo <[email protected]> |
216
|
|
|
// it has to return |
217
|
|
|
// an empty object in case, not a boolean. |
218
|
|
|
$xmlrpcVal = new Value(); |
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
|
222
|
216 |
|
return $xmlrpcVal; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Convert the xml representation of a method response, method request or single |
227
|
|
|
* xmlrpc value into the appropriate object (a.k.a. deserialize). |
228
|
|
|
* |
229
|
|
|
* @param string $xmlVal |
230
|
|
|
* @param array $options |
231
|
|
|
* |
232
|
|
|
* @return mixed false on error, or an instance of either Value, Request or Response |
233
|
|
|
*/ |
234
|
3 |
|
public function decodeXml($xmlVal, $options = array()) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
// 'guestimate' encoding |
237
|
3 |
|
$valEncoding = XMLParser::guessEncoding('', $xmlVal); |
238
|
3 |
View Code Duplication |
if ($valEncoding != '') { |
|
|
|
|
239
|
|
|
|
240
|
|
|
// Since parsing will fail if charset is not specified in the xml prologue, |
241
|
|
|
// the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
242
|
|
|
// The following code might be better for mb_string enabled installs, but |
243
|
|
|
// makes the lib about 200% slower... |
244
|
|
|
//if (!is_valid_charset($valEncoding, array('UTF-8')) |
|
|
|
|
245
|
3 |
|
if (!in_array($valEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($xmlVal)) { |
246
|
|
|
if ($valEncoding == 'ISO-8859-1') { |
247
|
|
|
$xmlVal = utf8_encode($xmlVal); |
248
|
|
|
} else { |
249
|
|
|
if (extension_loaded('mbstring')) { |
250
|
|
|
$xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding); |
251
|
|
|
} else { |
252
|
|
|
error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of xml text: ' . $valEncoding); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
3 |
|
} |
257
|
|
|
|
258
|
3 |
|
$parser = xml_parser_create(); |
259
|
3 |
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
260
|
|
|
// What if internal encoding is not in one of the 3 allowed? |
261
|
|
|
// we use the broadest one, ie. utf8! |
262
|
3 |
View Code Duplication |
if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) { |
|
|
|
|
263
|
|
|
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
264
|
|
|
} else { |
265
|
3 |
|
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, PhpXmlRpc::$xmlrpc_internalencoding); |
266
|
|
|
} |
267
|
|
|
|
268
|
3 |
|
$xmlRpcParser = new XMLParser(); |
269
|
3 |
|
xml_set_object($parser, $xmlRpcParser); |
270
|
|
|
|
271
|
3 |
|
xml_set_element_handler($parser, 'xmlrpc_se_any', 'xmlrpc_ee'); |
272
|
3 |
|
xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
273
|
3 |
|
xml_set_default_handler($parser, 'xmlrpc_dh'); |
274
|
3 |
|
if (!xml_parse($parser, $xmlVal, 1)) { |
275
|
|
|
$errstr = sprintf('XML error: %s at line %d, column %d', |
276
|
|
|
xml_error_string(xml_get_error_code($parser)), |
277
|
|
|
xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
278
|
|
|
error_log($errstr); |
279
|
|
|
xml_parser_free($parser); |
280
|
|
|
|
281
|
|
|
return false; |
282
|
|
|
} |
283
|
3 |
|
xml_parser_free($parser); |
284
|
3 |
|
if ($xmlRpcParser->_xh['isf'] > 1) { |
285
|
|
|
// test that $xmlrpc->_xh['value'] is an obj, too??? |
|
|
|
|
286
|
|
|
|
287
|
|
|
error_log($xmlRpcParser->_xh['isf_reason']); |
288
|
|
|
|
289
|
|
|
return false; |
290
|
|
|
} |
291
|
3 |
|
switch ($xmlRpcParser->_xh['rt']) { |
292
|
3 |
|
case 'methodresponse': |
293
|
3 |
|
$v = &$xmlRpcParser->_xh['value']; |
294
|
3 |
|
if ($xmlRpcParser->_xh['isf'] == 1) { |
295
|
|
|
$vc = $v['faultCode']; |
296
|
|
|
$vs = $v['faultString']; |
297
|
|
|
$r = new Response(0, $vc->scalarval(), $vs->scalarval()); |
298
|
|
|
} else { |
299
|
3 |
|
$r = new Response($v); |
300
|
|
|
} |
301
|
|
|
|
302
|
3 |
|
return $r; |
303
|
1 |
|
case 'methodcall': |
304
|
1 |
|
$req = new Request($xmlRpcParser->_xh['method']); |
305
|
1 |
View Code Duplication |
for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) { |
|
|
|
|
306
|
1 |
|
$req->addParam($xmlRpcParser->_xh['params'][$i]); |
307
|
1 |
|
} |
308
|
|
|
|
309
|
1 |
|
return $req; |
310
|
1 |
|
case 'value': |
311
|
1 |
|
return $xmlRpcParser->_xh['value']; |
312
|
|
|
default: |
313
|
|
|
return false; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: