1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpXmlRpc; |
4
|
|
|
|
5
|
|
|
use PhpXmlRpc\Helper\Charset; |
6
|
|
|
use PhpXmlRpc\Helper\Http; |
7
|
|
|
use PhpXmlRpc\Helper\Logger; |
8
|
|
|
use PhpXmlRpc\Helper\XMLParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class provides the representation of a request to an XML-RPC server. |
12
|
|
|
* A client sends a PhpXmlrpc\Request to a server, and receives back an PhpXmlrpc\Response. |
13
|
|
|
*/ |
14
|
|
|
class Request |
15
|
|
|
{ |
16
|
|
|
/// @todo: do these need to be public? |
17
|
|
|
public $payload; |
18
|
|
|
public $methodname; |
19
|
|
|
public $params = array(); |
20
|
|
|
public $debug = 0; |
21
|
|
|
public $content_type = 'text/xml'; |
22
|
|
|
|
23
|
|
|
// holds data while parsing the response. NB: Not a full Response object |
24
|
|
|
protected $httpResponse = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $methodName the name of the method to invoke |
28
|
|
|
* @param Value[] $params array of parameters to be passed to the method (NB: Value objects, not plain php values) |
29
|
|
|
*/ |
30
|
609 |
|
public function __construct($methodName, $params = array()) |
31
|
|
|
{ |
32
|
609 |
|
$this->methodname = $methodName; |
33
|
609 |
|
foreach ($params as $param) { |
34
|
434 |
|
$this->addParam($param); |
35
|
|
|
} |
36
|
609 |
|
} |
37
|
|
|
|
38
|
550 |
|
public function xml_header($charsetEncoding = '') |
39
|
|
|
{ |
40
|
550 |
|
if ($charsetEncoding != '') { |
41
|
56 |
|
return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n"; |
42
|
|
|
} else { |
43
|
494 |
|
return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n"; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
550 |
|
public function xml_footer() |
48
|
|
|
{ |
49
|
550 |
|
return '</methodCall>'; |
50
|
|
|
} |
51
|
|
|
|
52
|
550 |
|
public function createPayload($charsetEncoding = '') |
53
|
|
|
{ |
54
|
550 |
|
if ($charsetEncoding != '') { |
55
|
56 |
|
$this->content_type = 'text/xml; charset=' . $charsetEncoding; |
56
|
|
|
} else { |
57
|
494 |
|
$this->content_type = 'text/xml'; |
58
|
|
|
} |
59
|
550 |
|
$this->payload = $this->xml_header($charsetEncoding); |
60
|
550 |
|
$this->payload .= '<methodName>' . Charset::instance()->encodeEntities( |
61
|
550 |
|
$this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n"; |
62
|
550 |
|
$this->payload .= "<params>\n"; |
63
|
550 |
|
foreach ($this->params as $p) { |
64
|
511 |
|
$this->payload .= "<param>\n" . $p->serialize($charsetEncoding) . |
65
|
506 |
|
"</param>\n"; |
66
|
|
|
} |
67
|
550 |
|
$this->payload .= "</params>\n"; |
68
|
550 |
|
$this->payload .= $this->xml_footer(); |
69
|
550 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets/sets the xmlrpc method to be invoked. |
73
|
|
|
* |
74
|
|
|
* @param string $methodName the method to be set (leave empty not to set it) |
75
|
|
|
* |
76
|
|
|
* @return string the method that will be invoked |
77
|
|
|
*/ |
78
|
489 |
|
public function method($methodName = '') |
79
|
|
|
{ |
80
|
489 |
|
if ($methodName != '') { |
81
|
|
|
$this->methodname = $methodName; |
82
|
|
|
} |
83
|
|
|
|
84
|
489 |
|
return $this->methodname; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns xml representation of the message. XML prologue included. |
89
|
|
|
* |
90
|
|
|
* @param string $charsetEncoding |
91
|
|
|
* |
92
|
|
|
* @return string the xml representation of the message, xml prologue included |
93
|
|
|
*/ |
94
|
3 |
|
public function serialize($charsetEncoding = '') |
95
|
|
|
{ |
96
|
3 |
|
$this->createPayload($charsetEncoding); |
97
|
|
|
|
98
|
3 |
|
return $this->payload; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add a parameter to the list of parameters to be used upon method invocation. |
103
|
|
|
* |
104
|
|
|
* Checks that $params is actually a Value object and not a plain php value. |
105
|
|
|
* |
106
|
|
|
* @param Value $param |
107
|
|
|
* |
108
|
|
|
* @return boolean false on failure |
109
|
|
|
*/ |
110
|
555 |
|
public function addParam($param) |
111
|
|
|
{ |
112
|
|
|
// check: do not add to self params which are not xmlrpc values |
113
|
555 |
|
if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) { |
114
|
555 |
|
$this->params[] = $param; |
115
|
|
|
|
116
|
555 |
|
return true; |
117
|
|
|
} else { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the nth parameter in the request. The index zero-based. |
124
|
|
|
* |
125
|
|
|
* @param integer $i the index of the parameter to fetch (zero based) |
126
|
|
|
* |
127
|
|
|
* @return Value the i-th parameter |
128
|
|
|
*/ |
129
|
449 |
|
public function getParam($i) |
130
|
|
|
{ |
131
|
449 |
|
return $this->params[$i]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the number of parameters in the message. |
136
|
|
|
* |
137
|
|
|
* @return integer the number of parameters currently set |
138
|
|
|
*/ |
139
|
468 |
|
public function getNumParams() |
140
|
|
|
{ |
141
|
468 |
|
return count($this->params); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Given an open file handle, read all data available and parse it as an xmlrpc response. |
146
|
|
|
* |
147
|
|
|
* NB: the file handle is not closed by this function. |
148
|
|
|
* NNB: might have trouble in rare cases to work on network streams, as we check for a read of 0 bytes instead of |
149
|
|
|
* feof($fp). But since checking for feof(null) returns false, we would risk an infinite loop in that case, |
150
|
|
|
* because we cannot trust the caller to give us a valid pointer to an open file... |
151
|
|
|
* |
152
|
|
|
* @param resource $fp stream pointer |
153
|
|
|
* @param bool $headersProcessed |
154
|
|
|
* @param string $returnType |
155
|
|
|
* |
156
|
|
|
* @return Response |
157
|
|
|
*/ |
158
|
|
|
public function parseResponseFile($fp, $headersProcessed = false, $returnType = 'xmlrpcvals') |
159
|
|
|
{ |
160
|
|
|
$ipd = ''; |
161
|
|
|
while ($data = fread($fp, 32768)) { |
162
|
|
|
$ipd .= $data; |
163
|
|
|
} |
164
|
|
|
return $this->parseResponse($ipd, $headersProcessed, $returnType); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Parse the xmlrpc response contained in the string $data and return a Response object. |
169
|
|
|
* |
170
|
|
|
* When $this->debug has been set to a value greater than 0, will echo debug messages to screen while decoding. |
171
|
|
|
* |
172
|
|
|
* @param string $data the xmlrpc response, possibly including http headers |
173
|
|
|
* @param bool $headersProcessed when true prevents parsing HTTP headers for interpretation of content-encoding and |
174
|
|
|
* consequent decoding |
175
|
|
|
* @param string $returnType decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or |
176
|
|
|
* 'phpvals' |
177
|
|
|
* |
178
|
|
|
* @return Response |
179
|
|
|
*/ |
180
|
606 |
|
public function parseResponse($data = '', $headersProcessed = false, $returnType = 'xmlrpcvals') |
181
|
|
|
{ |
182
|
606 |
|
if ($this->debug) { |
183
|
3 |
|
Logger::instance()->debugMessage("---GOT---\n$data\n---END---"); |
184
|
|
|
} |
185
|
|
|
|
186
|
606 |
|
$this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array()); |
187
|
|
|
|
188
|
606 |
|
if ($data == '') { |
189
|
|
|
Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': no response received from server.'); |
190
|
|
|
return new Response(0, PhpXmlRpc::$xmlrpcerr['no_data'], PhpXmlRpc::$xmlrpcstr['no_data']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// parse the HTTP headers of the response, if present, and separate them from data |
194
|
606 |
|
if (substr($data, 0, 4) == 'HTTP') { |
195
|
594 |
|
$httpParser = new Http(); |
196
|
|
|
try { |
197
|
594 |
|
$this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug); |
198
|
1 |
|
} catch(\Exception $e) { |
199
|
1 |
|
$r = new Response(0, $e->getCode(), $e->getMessage()); |
200
|
|
|
// failed processing of HTTP response headers |
201
|
|
|
// save into response obj the full payload received, for debugging |
202
|
1 |
|
$r->raw_data = $data; |
203
|
|
|
|
204
|
1 |
|
return $r; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// be tolerant of extra whitespace in response body |
209
|
605 |
|
$data = trim($data); |
210
|
|
|
|
211
|
|
|
/// @todo return an error msg if $data == '' ? |
212
|
|
|
|
213
|
|
|
// be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts) |
214
|
|
|
// idea from Luca Mariano <[email protected]> originally in PEARified version of the lib |
215
|
605 |
|
$pos = strrpos($data, '</methodResponse>'); |
216
|
605 |
|
if ($pos !== false) { |
217
|
605 |
|
$data = substr($data, 0, $pos + 17); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// try to 'guestimate' the character encoding of the received response |
221
|
605 |
|
$respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data); |
222
|
|
|
|
223
|
605 |
|
if ($this->debug) { |
224
|
3 |
|
$start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
225
|
3 |
|
if ($start) { |
226
|
3 |
|
$start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
227
|
3 |
|
$end = strpos($data, '-->', $start); |
228
|
3 |
|
$comments = substr($data, $start, $end - $start); |
229
|
3 |
|
Logger::instance()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" . |
230
|
3 |
|
str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// if user wants back raw xml, give it to him |
235
|
605 |
|
if ($returnType == 'xml') { |
236
|
1 |
|
$r = new Response($data, 0, '', 'xml'); |
237
|
1 |
|
$r->hdrs = $this->httpResponse['headers']; |
238
|
1 |
|
$r->_cookies = $this->httpResponse['cookies']; |
239
|
1 |
|
$r->raw_data = $this->httpResponse['raw_data']; |
240
|
|
|
|
241
|
1 |
|
return $r; |
242
|
|
|
} |
243
|
|
|
|
244
|
604 |
|
if ($respEncoding != '') { |
245
|
|
|
|
246
|
|
|
// Since parsing will fail if charset is not specified in the xml prologue, |
247
|
|
|
// the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
248
|
|
|
// The following code might be better for mb_string enabled installs, but makes the lib about 200% slower... |
249
|
|
|
//if (!is_valid_charset($respEncoding, array('UTF-8'))) |
250
|
604 |
|
if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) { |
251
|
1 |
|
if ($respEncoding == 'ISO-8859-1') { |
252
|
1 |
|
$data = utf8_encode($data); |
253
|
|
|
} else { |
254
|
|
|
if (extension_loaded('mbstring')) { |
255
|
|
|
$data = mb_convert_encoding($data, 'UTF-8', $respEncoding); |
256
|
|
|
} else { |
257
|
|
|
Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// PHP internally might use ISO-8859-1, so we have to tell the xml parser to give us back data in the expected charset. |
264
|
|
|
// What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8 |
265
|
|
|
// This allows to send data which is native in various charset, by extending xmlrpc_encode_entities() and |
266
|
|
|
// setting xmlrpc_internalencoding |
267
|
604 |
|
if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) { |
268
|
|
|
$options = array(XML_OPTION_TARGET_ENCODING => 'UTF-8'); |
269
|
|
|
} else { |
270
|
604 |
|
$options = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding); |
271
|
|
|
} |
272
|
|
|
|
273
|
604 |
|
$xmlRpcParser = new XMLParser($options); |
274
|
604 |
|
$xmlRpcParser->parse($data, $returnType, XMLParser::ACCEPT_RESPONSE); |
275
|
|
|
|
276
|
|
|
// first error check: xml not well formed |
277
|
604 |
|
if ($xmlRpcParser->_xh['isf'] > 2) { |
278
|
|
|
|
279
|
|
|
// BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL' |
280
|
|
|
|
281
|
3 |
|
$r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], |
282
|
3 |
|
PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason']); |
283
|
|
|
|
284
|
3 |
|
if ($this->debug) { |
285
|
3 |
|
print $xmlRpcParser->_xh['isf_reason']; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
// second error check: xml well formed but not xml-rpc compliant |
289
|
604 |
|
elseif ($xmlRpcParser->_xh['isf'] == 2) { |
290
|
4 |
|
if ($this->debug) { |
291
|
|
|
/// @todo echo something for user? |
292
|
|
|
} |
293
|
|
|
|
294
|
4 |
|
$r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], |
295
|
4 |
|
PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason']); |
296
|
|
|
} |
297
|
|
|
// third error check: parsing of the response has somehow gone boink. |
298
|
|
|
// NB: shall we omit this check, since we trust the parsing code? |
299
|
601 |
|
elseif ($returnType == 'xmlrpcvals' && !is_object($xmlRpcParser->_xh['value'])) { |
300
|
|
|
// something odd has happened |
301
|
|
|
// and it's time to generate a client side error |
302
|
|
|
// indicating something odd went on |
303
|
|
|
$r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], |
304
|
|
|
PhpXmlRpc::$xmlrpcstr['invalid_return']); |
305
|
|
|
} else { |
306
|
601 |
|
if ($this->debug > 1) { |
307
|
2 |
|
Logger::instance()->debugMessage( |
308
|
2 |
|
"---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---" |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
601 |
|
$v = $xmlRpcParser->_xh['value']; |
313
|
|
|
|
314
|
601 |
|
if ($xmlRpcParser->_xh['isf']) { |
315
|
|
|
/// @todo we should test here if server sent an int and a string, and/or coerce them into such... |
316
|
80 |
|
if ($returnType == 'xmlrpcvals') { |
317
|
80 |
|
$errNo_v = $v['faultCode']; |
318
|
80 |
|
$errStr_v = $v['faultString']; |
319
|
80 |
|
$errNo = $errNo_v->scalarval(); |
320
|
80 |
|
$errStr = $errStr_v->scalarval(); |
321
|
|
|
} else { |
322
|
|
|
$errNo = $v['faultCode']; |
323
|
|
|
$errStr = $v['faultString']; |
324
|
|
|
} |
325
|
|
|
|
326
|
80 |
|
if ($errNo == 0) { |
327
|
|
|
// FAULT returned, errno needs to reflect that |
328
|
|
|
$errNo = -1; |
329
|
|
|
} |
330
|
|
|
|
331
|
80 |
|
$r = new Response(0, $errNo, $errStr); |
332
|
|
|
} else { |
333
|
598 |
|
$r = new Response($v, 0, '', $returnType); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
604 |
|
$r->hdrs = $this->httpResponse['headers']; |
338
|
604 |
|
$r->_cookies = $this->httpResponse['cookies']; |
339
|
604 |
|
$r->raw_data = $this->httpResponse['raw_data']; |
340
|
|
|
|
341
|
604 |
|
return $r; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Kept the old name even if Request class was renamed, for compatibility. |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
117 |
|
public function kindOf() |
350
|
|
|
{ |
351
|
117 |
|
return 'msg'; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Enables/disables the echoing to screen of the xmlrpc responses received. |
356
|
|
|
* |
357
|
|
|
* @param integer $level values 0, 1, 2 are supported |
358
|
|
|
*/ |
359
|
607 |
|
public function setDebug($level) |
360
|
|
|
{ |
361
|
607 |
|
$this->debug = $level; |
362
|
607 |
|
} |
363
|
|
|
} |
364
|
|
|
|