5 use PhpXmlRpc\Exception\HttpException;
6 use PhpXmlRpc\Helper\Charset;
7 use PhpXmlRpc\Helper\Http;
8 use PhpXmlRpc\Helper\Logger;
9 use PhpXmlRpc\Helper\XMLParser;
12 * This class provides the representation of a request to an XML-RPC server.
13 * A client sends a PhpXmlrpc\Request to a server, and receives back an PhpXmlrpc\Response.
17 protected static $logger;
18 protected static $parser;
19 protected static $charsetEncoder;
21 /// @todo: do these need to be public?
26 public $params = array();
28 public $content_type = 'text/xml';
30 // holds data while parsing the response. NB: Not a full Response object
31 /** @deprecated will be removed in a future release */
32 protected $httpResponse = array();
34 public function getLogger()
36 if (self::$logger === null) {
37 self::$logger = Logger::instance();
42 public static function setLogger($logger)
44 self::$logger = $logger;
47 public function getParser()
49 if (self::$parser === null) {
50 self::$parser = new XMLParser();
55 public static function setParser($parser)
57 self::$parser = $parser;
60 public function getCharsetEncoder()
62 if (self::$charsetEncoder === null) {
63 self::$charsetEncoder = Charset::instance();
65 return self::$charsetEncoder;
68 public function setCharsetEncoder($charsetEncoder)
70 self::$charsetEncoder = $charsetEncoder;
74 * @param string $methodName the name of the method to invoke
75 * @param Value[] $params array of parameters to be passed to the method (NB: Value objects, not plain php values)
77 public function __construct($methodName, $params = array())
79 $this->methodname = $methodName;
80 foreach ($params as $param) {
81 $this->addParam($param);
86 * @internal this function will become protected in the future
87 * @param string $charsetEncoding
90 public function xml_header($charsetEncoding = '')
92 if ($charsetEncoding != '') {
93 return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n";
95 return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
100 * @internal this function will become protected in the future
103 public function xml_footer()
105 return '</methodCall>';
109 * @internal this function will become protected in the future
110 * @param string $charsetEncoding
112 public function createPayload($charsetEncoding = '')
114 if ($charsetEncoding != '') {
115 $this->content_type = 'text/xml; charset=' . $charsetEncoding;
117 $this->content_type = 'text/xml';
119 $this->payload = $this->xml_header($charsetEncoding);
120 $this->payload .= '<methodName>' . $this->getCharsetEncoder()->encodeEntities(
121 $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n";
122 $this->payload .= "<params>\n";
123 foreach ($this->params as $p) {
124 $this->payload .= "<param>\n" . $p->serialize($charsetEncoding) .
127 $this->payload .= "</params>\n";
128 $this->payload .= $this->xml_footer();
132 * Gets/sets the xmlrpc method to be invoked.
134 * @param string $methodName the method to be set (leave empty not to set it)
136 * @return string the method that will be invoked
138 public function method($methodName = '')
140 if ($methodName != '') {
141 $this->methodname = $methodName;
144 return $this->methodname;
148 * Returns xml representation of the message. XML prologue included.
150 * @param string $charsetEncoding
152 * @return string the xml representation of the message, xml prologue included
154 public function serialize($charsetEncoding = '')
156 $this->createPayload($charsetEncoding);
158 return $this->payload;
162 * Add a parameter to the list of parameters to be used upon method invocation.
164 * Checks that $params is actually a Value object and not a plain php value.
166 * @param Value $param
168 * @return boolean false on failure
170 public function addParam($param)
172 // check: do not add to self params which are not xmlrpc values
173 if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) {
174 $this->params[] = $param;
183 * Returns the nth parameter in the request. The index zero-based.
185 * @param integer $i the index of the parameter to fetch (zero based)
187 * @return Value the i-th parameter
189 public function getParam($i)
191 return $this->params[$i];
195 * Returns the number of parameters in the message.
197 * @return integer the number of parameters currently set
199 public function getNumParams()
201 return count($this->params);
205 * Given an open file handle, read all data available and parse it as an xmlrpc response.
207 * NB: the file handle is not closed by this function.
208 * NNB: might have trouble in rare cases to work on network streams, as we check for a read of 0 bytes instead of
209 * feof($fp). But since checking for feof(null) returns false, we would risk an infinite loop in that case,
210 * because we cannot trust the caller to give us a valid pointer to an open file...
212 * @param resource $fp stream pointer
213 * @param bool $headersProcessed
214 * @param string $returnType
218 public function parseResponseFile($fp, $headersProcessed = false, $returnType = 'xmlrpcvals')
221 while ($data = fread($fp, 32768)) {
224 return $this->parseResponse($ipd, $headersProcessed, $returnType);
228 * Parse the xmlrpc response contained in the string $data and return a Response object.
230 * When $this->debug has been set to a value greater than 0, will echo debug messages to screen while decoding.
232 * @param string $data the xmlrpc response, possibly including http headers
233 * @param bool $headersProcessed when true prevents parsing HTTP headers for interpretation of content-encoding and
234 * consequent decoding
235 * @param string $returnType decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or
240 * @todo parsing Responses is not really the responsibility of the Request class. Maybe of the Client...
242 public function parseResponse($data = '', $headersProcessed = false, $returnType = XMLParser::RETURN_XMLRPCVALS)
245 $this->getLogger()->debugMessage("---GOT---\n$data\n---END---");
248 $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array());
251 $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': no response received from server.');
252 return new Response(0, PhpXmlRpc::$xmlrpcerr['no_data'], PhpXmlRpc::$xmlrpcstr['no_data']);
255 // parse the HTTP headers of the response, if present, and separate them from data
256 if (substr($data, 0, 4) == 'HTTP') {
257 $httpParser = new Http();
259 $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug);
260 } catch (HttpException $e) {
261 // failed processing of HTTP response headers
262 // save into response obj the full payload received, for debugging
263 return new Response(0, $e->getCode(), $e->getMessage(), '', array('raw_data' => $data, 'status_code', $e->statusCode()));
264 } catch(\Exception $e) {
265 return new Response(0, $e->getCode(), $e->getMessage(), '', array('raw_data' => $data));
269 // be tolerant of extra whitespace in response body
272 /// @todo return an error msg if $data == '' ?
274 // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts)
275 // idea from Luca Mariano <luca.mariano@email.it> originally in PEARified version of the lib
276 $pos = strrpos($data, '</methodResponse>');
277 if ($pos !== false) {
278 $data = substr($data, 0, $pos + 17);
281 // try to 'guestimate' the character encoding of the received response
282 $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data);
285 $start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
287 $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
288 $end = strpos($data, '-->', $start);
289 $comments = substr($data, $start, $end - $start);
290 $this->getLogger()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" .
291 str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding);
295 // if user wants back raw xml, give it to her
296 if ($returnType == 'xml') {
297 return new Response($data, 0, '', 'xml', $this->httpResponse);
300 if ($respEncoding != '') {
302 // Since parsing will fail if charset is not specified in the xml prologue,
303 // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
304 // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower...
305 //if (!is_valid_charset($respEncoding, array('UTF-8')))
306 if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) {
307 if ($respEncoding == 'ISO-8859-1') {
308 $data = utf8_encode($data);
311 if (extension_loaded('mbstring')) {
312 $data = mb_convert_encoding($data, 'UTF-8', $respEncoding);
314 $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding);
320 // PHP internally might use ISO-8859-1, so we have to tell the xml parser to give us back data in the expected charset.
321 // What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8
322 // This allows to send data which is native in various charset, by extending xmlrpc_encode_entities() and
323 // setting xmlrpc_internalencoding
324 if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) {
325 /// @todo emit a warning
326 $options = array(XML_OPTION_TARGET_ENCODING => 'UTF-8');
328 $options = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding);
331 $xmlRpcParser = $this->getParser();
332 $xmlRpcParser->parse($data, $returnType, XMLParser::ACCEPT_RESPONSE, $options);
334 // first error check: xml not well formed
335 if ($xmlRpcParser->_xh['isf'] > 2) {
337 // BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL'
339 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
340 PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '',
345 print $xmlRpcParser->_xh['isf_reason'];
348 // second error check: xml well formed but not xml-rpc compliant
349 elseif ($xmlRpcParser->_xh['isf'] == 2) {
350 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
351 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 == XMLParser::RETURN_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'], PhpXmlRpc::$xmlrpcstr['invalid_return'],
366 '', $this->httpResponse
369 if ($this->debug > 1) {
370 $this->getLogger()->debugMessage(
371 "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---"
375 $v = $xmlRpcParser->_xh['value'];
377 if ($xmlRpcParser->_xh['isf']) {
378 /// @todo we should test here if server sent an int and a string, and/or coerce them into such...
379 if ($returnType == XMLParser::RETURN_XMLRPCVALS) {
380 $errNo_v = $v['faultCode'];
381 $errStr_v = $v['faultString'];
382 $errNo = $errNo_v->scalarval();
383 $errStr = $errStr_v->scalarval();
385 $errNo = $v['faultCode'];
386 $errStr = $v['faultString'];
390 // FAULT returned, errno needs to reflect that
394 $r = new Response(0, $errNo, $errStr, '', $this->httpResponse);
396 $r = new Response($v, 0, '', $returnType, $this->httpResponse);
404 * Kept the old name even if Request class was renamed, for compatibility.
408 public function kindOf()
414 * Enables/disables the echoing to screen of the xmlrpc responses received.
416 * @param integer $level values 0, 1, 2 are supported
418 public function setDebug($level)
420 $this->debug = $level;