5 use PhpXmlRpc\Helper\Charset;
6 use PhpXmlRpc\Helper\Http;
7 use PhpXmlRpc\Helper\Logger;
8 use PhpXmlRpc\Helper\XMLParser;
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.
16 protected static $logger;
17 protected static $parser;
18 protected static $charsetEncoder;
20 /// @todo: do these need to be public?
25 public $params = array();
27 public $content_type = 'text/xml';
29 // holds data while parsing the response. NB: Not a full Response object
30 protected $httpResponse = array();
32 public function getLogger()
34 if (self::$logger === null) {
35 self::$logger = Logger::instance();
40 public static function setLogger($logger)
42 self::$logger = $logger;
45 public function getParser()
47 if (self::$parser === null) {
48 self::$parser = new XMLParser();
53 public static function setParser($parser)
55 self::$parser = $parser;
58 public function getCharsetEncoder()
60 if (self::$charsetEncoder === null) {
61 self::$charsetEncoder = Charset::instance();
63 return self::$charsetEncoder;
66 public function setCharsetEncoder($charsetEncoder)
68 self::$charsetEncoder = $charsetEncoder;
72 * @param string $methodName the name of the method to invoke
73 * @param Value[] $params array of parameters to be passed to the method (NB: Value objects, not plain php values)
75 public function __construct($methodName, $params = array())
77 $this->methodname = $methodName;
78 foreach ($params as $param) {
79 $this->addParam($param);
84 * @internal this function will become protected in the future
85 * @param string $charsetEncoding
88 public function xml_header($charsetEncoding = '')
90 if ($charsetEncoding != '') {
91 return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n";
93 return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
98 * @internal this function will become protected in the future
101 public function xml_footer()
103 return '</methodCall>';
107 * @internal this function will become protected in the future
108 * @param string $charsetEncoding
110 public function createPayload($charsetEncoding = '')
112 if ($charsetEncoding != '') {
113 $this->content_type = 'text/xml; charset=' . $charsetEncoding;
115 $this->content_type = 'text/xml';
117 $this->payload = $this->xml_header($charsetEncoding);
118 $this->payload .= '<methodName>' . $this->getCharsetEncoder()->encodeEntities(
119 $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n";
120 $this->payload .= "<params>\n";
121 foreach ($this->params as $p) {
122 $this->payload .= "<param>\n" . $p->serialize($charsetEncoding) .
125 $this->payload .= "</params>\n";
126 $this->payload .= $this->xml_footer();
130 * Gets/sets the xmlrpc method to be invoked.
132 * @param string $methodName the method to be set (leave empty not to set it)
134 * @return string the method that will be invoked
136 public function method($methodName = '')
138 if ($methodName != '') {
139 $this->methodname = $methodName;
142 return $this->methodname;
146 * Returns xml representation of the message. XML prologue included.
148 * @param string $charsetEncoding
150 * @return string the xml representation of the message, xml prologue included
152 public function serialize($charsetEncoding = '')
154 $this->createPayload($charsetEncoding);
156 return $this->payload;
160 * Add a parameter to the list of parameters to be used upon method invocation.
162 * Checks that $params is actually a Value object and not a plain php value.
164 * @param Value $param
166 * @return boolean false on failure
168 public function addParam($param)
170 // check: do not add to self params which are not xmlrpc values
171 if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) {
172 $this->params[] = $param;
181 * Returns the nth parameter in the request. The index zero-based.
183 * @param integer $i the index of the parameter to fetch (zero based)
185 * @return Value the i-th parameter
187 public function getParam($i)
189 return $this->params[$i];
193 * Returns the number of parameters in the message.
195 * @return integer the number of parameters currently set
197 public function getNumParams()
199 return count($this->params);
203 * Given an open file handle, read all data available and parse it as an xmlrpc response.
205 * NB: the file handle is not closed by this function.
206 * NNB: might have trouble in rare cases to work on network streams, as we check for a read of 0 bytes instead of
207 * feof($fp). But since checking for feof(null) returns false, we would risk an infinite loop in that case,
208 * because we cannot trust the caller to give us a valid pointer to an open file...
210 * @param resource $fp stream pointer
211 * @param bool $headersProcessed
212 * @param string $returnType
216 public function parseResponseFile($fp, $headersProcessed = false, $returnType = 'xmlrpcvals')
219 while ($data = fread($fp, 32768)) {
222 return $this->parseResponse($ipd, $headersProcessed, $returnType);
226 * Parse the xmlrpc response contained in the string $data and return a Response object.
228 * When $this->debug has been set to a value greater than 0, will echo debug messages to screen while decoding.
230 * @param string $data the xmlrpc response, possibly including http headers
231 * @param bool $headersProcessed when true prevents parsing HTTP headers for interpretation of content-encoding and
232 * consequent decoding
233 * @param string $returnType decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or
238 * @todo parsing Responses is not really the responsibility of the Request class. Maybe of the Client...
240 public function parseResponse($data = '', $headersProcessed = false, $returnType = 'xmlrpcvals')
243 $this->getLogger()->debugMessage("---GOT---\n$data\n---END---");
246 $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array());
249 $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': no response received from server.');
250 return new Response(0, PhpXmlRpc::$xmlrpcerr['no_data'], PhpXmlRpc::$xmlrpcstr['no_data']);
253 // parse the HTTP headers of the response, if present, and separate them from data
254 if (substr($data, 0, 4) == 'HTTP') {
255 $httpParser = new Http();
257 $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug);
258 } catch(\Exception $e) {
259 $r = new Response(0, $e->getCode(), $e->getMessage());
260 // failed processing of HTTP response headers
261 // save into response obj the full payload received, for debugging
262 $r->raw_data = $data;
268 // be tolerant of extra whitespace in response body
271 /// @todo return an error msg if $data == '' ?
273 // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts)
274 // idea from Luca Mariano <luca.mariano@email.it> originally in PEARified version of the lib
275 $pos = strrpos($data, '</methodResponse>');
276 if ($pos !== false) {
277 $data = substr($data, 0, $pos + 17);
280 // try to 'guestimate' the character encoding of the received response
281 $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data);
284 $start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
286 $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
287 $end = strpos($data, '-->', $start);
288 $comments = substr($data, $start, $end - $start);
289 $this->getLogger()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" .
290 str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding);
294 // if user wants back raw xml, give it to her
295 if ($returnType == 'xml') {
296 $r = new Response($data, 0, '', 'xml');
297 $r->hdrs = $this->httpResponse['headers'];
298 $r->_cookies = $this->httpResponse['cookies'];
299 $r->raw_data = $this->httpResponse['raw_data'];
304 if ($respEncoding != '') {
306 // Since parsing will fail if charset is not specified in the xml prologue,
307 // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
308 // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower...
309 //if (!is_valid_charset($respEncoding, array('UTF-8')))
310 if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) {
311 if ($respEncoding == 'ISO-8859-1') {
312 $data = utf8_encode($data);
315 if (extension_loaded('mbstring')) {
316 $data = mb_convert_encoding($data, 'UTF-8', $respEncoding);
318 $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding);
324 // PHP internally might use ISO-8859-1, so we have to tell the xml parser to give us back data in the expected charset.
325 // What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8
326 // This allows to send data which is native in various charset, by extending xmlrpc_encode_entities() and
327 // setting xmlrpc_internalencoding
328 if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) {
329 /// @todo emit a warning
330 $options = array(XML_OPTION_TARGET_ENCODING => 'UTF-8');
332 $options = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding);
335 $xmlRpcParser = $this->getParser();
336 $xmlRpcParser->parse($data, $returnType, XMLParser::ACCEPT_RESPONSE, $options);
338 // first error check: xml not well formed
339 if ($xmlRpcParser->_xh['isf'] > 2) {
341 // BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL'
343 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
344 PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason']);
347 print $xmlRpcParser->_xh['isf_reason'];
350 // second error check: xml well formed but not xml-rpc compliant
351 elseif ($xmlRpcParser->_xh['isf'] == 2) {
352 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
353 PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason']);
356 /// @todo echo something for user?
359 // third error check: parsing of the response has somehow gone boink.
360 /// @todo shall we omit this check, since we trust the parsing code?
361 elseif ($returnType == 'xmlrpcvals' && !is_object($xmlRpcParser->_xh['value'])) {
362 // something odd has happened
363 // and it's time to generate a client side error
364 // indicating something odd went on
365 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
366 PhpXmlRpc::$xmlrpcstr['invalid_return']);
368 if ($this->debug > 1) {
369 $this->getLogger()->debugMessage(
370 "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---"
374 $v = $xmlRpcParser->_xh['value'];
376 if ($xmlRpcParser->_xh['isf']) {
377 /// @todo we should test here if server sent an int and a string, and/or coerce them into such...
378 if ($returnType == 'xmlrpcvals') {
379 $errNo_v = $v['faultCode'];
380 $errStr_v = $v['faultString'];
381 $errNo = $errNo_v->scalarval();
382 $errStr = $errStr_v->scalarval();
384 $errNo = $v['faultCode'];
385 $errStr = $v['faultString'];
389 // FAULT returned, errno needs to reflect that
393 $r = new Response(0, $errNo, $errStr);
395 $r = new Response($v, 0, '', $returnType);
399 $r->hdrs = $this->httpResponse['headers'];
400 $r->_cookies = $this->httpResponse['cookies'];
401 $r->raw_data = $this->httpResponse['raw_data'];
407 * Kept the old name even if Request class was renamed, for compatibility.
411 public function kindOf()
417 * Enables/disables the echoing to screen of the xmlrpc responses received.
419 * @param integer $level values 0, 1, 2 are supported
421 public function setDebug($level)
423 $this->debug = $level;