Open Monograph Press  3.3.0
generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php
1 <?php
2 
3 namespace PhpXmlRpc;
4 
9 
10 class Request
11 {
13  public $payload;
14  public $methodname;
15  public $params = array();
16  public $debug = 0;
17  public $content_type = 'text/xml';
18 
19  // holds data while parsing the response. NB: Not a full Response object
20  protected $httpResponse = array();
21 
26  public function __construct($methodName, $params = array())
27  {
28  $this->methodname = $methodName;
29  foreach ($params as $param) {
30  $this->addParam($param);
31  }
32  }
33 
34  public function xml_header($charsetEncoding = '')
35  {
36  if ($charsetEncoding != '') {
37  return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" . ">\n<methodCall>\n";
38  } else {
39  return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
40  }
41  }
42 
43  public function xml_footer()
44  {
45  return '</methodCall>';
46  }
47 
48  public function createPayload($charsetEncoding = '')
49  {
50  if ($charsetEncoding != '') {
51  $this->content_type = 'text/xml; charset=' . $charsetEncoding;
52  } else {
53  $this->content_type = 'text/xml';
54  }
55  $this->payload = $this->xml_header($charsetEncoding);
56  $this->payload .= '<methodName>' . Charset::instance()->encodeEntities($this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</methodName>\n";
57  $this->payload .= "<params>\n";
58  foreach ($this->params as $p) {
59  $this->payload .= "<param>\n" . $p->serialize($charsetEncoding) .
60  "</param>\n";
61  }
62  $this->payload .= "</params>\n";
63  $this->payload .= $this->xml_footer();
64  }
65 
73  public function method($methodName = '')
74  {
75  if ($methodName != '') {
76  $this->methodname = $methodName;
77  }
78 
79  return $this->methodname;
80  }
81 
89  public function serialize($charsetEncoding = '')
90  {
91  $this->createPayload($charsetEncoding);
92 
93  return $this->payload;
94  }
95 
103  public function addParam($param)
104  {
105  // add check: do not add to self params which are not xmlrpc values
106  if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) {
107  $this->params[] = $param;
108 
109  return true;
110  } else {
111  return false;
112  }
113  }
114 
122  public function getParam($i)
123  {
124  return $this->params[$i];
125  }
126 
132  public function getNumParams()
133  {
134  return count($this->params);
135  }
136 
152  public function parseResponseFile($fp)
153  {
154  $ipd = '';
155  while ($data = fread($fp, 32768)) {
156  $ipd .= $data;
157  }
158  return $this->parseResponse($ipd);
159  }
160 
170  public function parseResponse($data = '', $headersProcessed = false, $returnType = 'xmlrpcvals')
171  {
172  if ($this->debug) {
173  Logger::instance()->debugMessage("---GOT---\n$data\n---END---");
174  }
175 
176  $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array());
177 
178  if ($data == '') {
179  error_log('XML-RPC: ' . __METHOD__ . ': no response received from server.');
180  return new Response(0, PhpXmlRpc::$xmlrpcerr['no_data'], PhpXmlRpc::$xmlrpcstr['no_data']);
181  }
182 
183  // parse the HTTP headers of the response, if present, and separate them from data
184  if (substr($data, 0, 4) == 'HTTP') {
185  $httpParser = new Http();
186  try {
187  $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug);
188  } catch(\Exception $e) {
189  $r = new Response(0, $e->getCode(), $e->getMessage());
190  // failed processing of HTTP response headers
191  // save into response obj the full payload received, for debugging
192  $r->raw_data = $data;
193 
194  return $r;
195  }
196  }
197 
198  // be tolerant of extra whitespace in response body
199  $data = trim($data);
200 
202 
203  // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts)
204  // idea from Luca Mariano <luca.mariano@email.it> originally in PEARified version of the lib
205  $pos = strrpos($data, '</methodResponse>');
206  if ($pos !== false) {
207  $data = substr($data, 0, $pos + 17);
208  }
209 
210  // try to 'guestimate' the character encoding of the received response
211  $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data);
212 
213  if ($this->debug) {
214  $start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
215  if ($start) {
216  $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
217  $end = strpos($data, '-->', $start);
218  $comments = substr($data, $start, $end - $start);
219  Logger::instance()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" .
220  str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding);
221  }
222  }
223 
224  // if user wants back raw xml, give it to him
225  if ($returnType == 'xml') {
226  $r = new Response($data, 0, '', 'xml');
227  $r->hdrs = $this->httpResponse['headers'];
228  $r->_cookies = $this->httpResponse['cookies'];
229  $r->raw_data = $this->httpResponse['raw_data'];
230 
231  return $r;
232  }
233 
234  if ($respEncoding != '') {
235 
236  // Since parsing will fail if charset is not specified in the xml prologue,
237  // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
238  // The following code might be better for mb_string enabled installs, but
239  // makes the lib about 200% slower...
240  //if (!is_valid_charset($respEncoding, array('UTF-8')))
241  if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) {
242  if ($respEncoding == 'ISO-8859-1') {
243  $data = utf8_encode($data);
244  } else {
245  if (extension_loaded('mbstring')) {
246  $data = mb_convert_encoding($data, 'UTF-8', $respEncoding);
247  } else {
248  error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding);
249  }
250  }
251  }
252  }
253 
254  $parser = xml_parser_create();
255  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
256  // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell
257  // the xml parser to give us back data in the expected charset.
258  // What if internal encoding is not in one of the 3 allowed?
259  // we use the broadest one, ie. utf8
260  // This allows to send data which is native in various charset,
261  // by extending xmlrpc_encode_entities() and setting xmlrpc_internalencoding
262  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  xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, PhpXmlRpc::$xmlrpc_internalencoding);
266  }
267 
268  $xmlRpcParser = new XMLParser();
269  xml_set_object($parser, $xmlRpcParser);
270 
271  if ($returnType == 'phpvals') {
272  xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast');
273  } else {
274  xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
275  }
276 
277  xml_set_character_data_handler($parser, 'xmlrpc_cd');
278  xml_set_default_handler($parser, 'xmlrpc_dh');
279 
280  // first error check: xml not well formed
281  if (!xml_parse($parser, $data, count($data))) {
282  // thanks to Peter Kocks <peter.kocks@baygate.com>
283  if ((xml_get_current_line_number($parser)) == 1) {
284  $errStr = 'XML error at line 1, check URL';
285  } else {
286  $errStr = sprintf('XML error: %s at line %d, column %d',
287  xml_error_string(xml_get_error_code($parser)),
288  xml_get_current_line_number($parser), xml_get_current_column_number($parser));
289  }
290  error_log($errStr);
291  $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' (' . $errStr . ')');
292  xml_parser_free($parser);
293  if ($this->debug) {
294  print $errStr;
295  }
296  $r->hdrs = $this->httpResponse['headers'];
297  $r->_cookies = $this->httpResponse['cookies'];
298  $r->raw_data = $this->httpResponse['raw_data'];
299 
300  return $r;
301  }
302  xml_parser_free($parser);
303  // second error check: xml well formed but not xml-rpc compliant
304  if ($xmlRpcParser->_xh['isf'] > 1) {
305  if ($this->debug) {
307  }
308 
309  $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
310  PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason']);
311  }
312  // third error check: parsing of the response has somehow gone boink.
313  // NB: shall we omit this check, since we trust the parsing code?
314  elseif ($returnType == 'xmlrpcvals' && !is_object($xmlRpcParser->_xh['value'])) {
315  // something odd has happened
316  // and it's time to generate a client side error
317  // indicating something odd went on
318  $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'],
319  PhpXmlRpc::$xmlrpcstr['invalid_return']);
320  } else {
321  if ($this->debug > 1) {
322  Logger::instance()->debugMessage(
323  "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---"
324  );
325  }
326 
327  // note that using =& will raise an error if $xmlRpcParser->_xh['st'] does not generate an object.
328  $v = &$xmlRpcParser->_xh['value'];
329 
330  if ($xmlRpcParser->_xh['isf']) {
332  if ($returnType == 'xmlrpcvals') {
333  $errNo_v = $v['faultCode'];
334  $errStr_v = $v['faultString'];
335  $errNo = $errNo_v->scalarval();
336  $errStr = $errStr_v->scalarval();
337  } else {
338  $errNo = $v['faultCode'];
339  $errStr = $v['faultString'];
340  }
341 
342  if ($errNo == 0) {
343  // FAULT returned, errno needs to reflect that
344  $errNo = -1;
345  }
346 
347  $r = new Response(0, $errNo, $errStr);
348  } else {
349  $r = new Response($v, 0, '', $returnType);
350  }
351  }
352 
353  $r->hdrs = $this->httpResponse['headers'];
354  $r->_cookies = $this->httpResponse['cookies'];
355  $r->raw_data = $this->httpResponse['raw_data'];
356 
357  return $r;
358  }
359 
365  public function kindOf()
366  {
367  return 'msg';
368  }
369 
375  public function setDebug($in)
376  {
377  $this->debug = $in;
378  }
379 }
PhpXmlRpc\Request\__construct
__construct($methodName, $params=array())
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:26
PhpXmlRpc\Helper\Charset
Definition: Charset.php:7
PhpXmlRpc\Response
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Response.php:7
PhpXmlRpc\Request\serialize
serialize($charsetEncoding='')
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:89
PhpXmlRpc\Request\$methodname
$methodname
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:14
PhpXmlRpc\Request\parseResponseFile
parseResponseFile($fp)
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:152
PhpXmlRpc\Helper\Http
Definition: Http.php:7
PhpXmlRpc\Request\addParam
addParam($param)
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:103
PhpXmlRpc\Request\parseResponse
parseResponse($data='', $headersProcessed=false, $returnType='xmlrpcvals')
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:170
PhpXmlRpc\Request\kindOf
kindOf()
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:365
PhpXmlRpc\Request\xml_header
xml_header($charsetEncoding='')
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:34
PhpXmlRpc\Request\getNumParams
getNumParams()
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:132
PhpXmlRpc\Helper\XMLParser
Definition: XMLParser.php:11
PhpXmlRpc\Request\method
method($methodName='')
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:73
PhpXmlRpc\Request\$payload
$payload
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:13
PhpXmlRpc\Request\getParam
getParam($i)
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:122
Http
PhpXmlRpc\Request\xml_footer
xml_footer()
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:43
PhpXmlRpc\Request\createPayload
createPayload($charsetEncoding='')
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:48
PhpXmlRpc\Request\setDebug
setDebug($in)
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:375
PhpXmlRpc\Helper\Logger
Definition: Logger.php:5
PhpXmlRpc
Definition: Autoloader.php:3
PhpXmlRpc\Request
Definition: generic/plagiarism/vendor/phpxmlrpc/phpxmlrpc/src/Request.php:10