Client.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace dokuwiki\Remote\IXR;
  3. use dokuwiki\HTTP\HTTPClient;
  4. use IXR\Message\Message;
  5. use IXR\Request\Request;
  6. /**
  7. * This implements a XML-RPC client using our own HTTPClient
  8. *
  9. * Note: this now inherits from the IXR library's client and no longer from HTTPClient. Instead composition
  10. * is used to add the HTTP client.
  11. */
  12. class Client extends \IXR\Client\Client
  13. {
  14. /** @var HTTPClient */
  15. protected $httpClient;
  16. /** @var string */
  17. protected $posturl = '';
  18. /** @inheritdoc */
  19. public function __construct($server, $path = false, $port = 80, $timeout = 15, $timeout_io = null)
  20. {
  21. parent::__construct($server, $path, $port, $timeout, $timeout_io);
  22. if (!$path) {
  23. // Assume we have been given an URL instead
  24. $this->posturl = $server;
  25. } else {
  26. $this->posturl = 'http://' . $server . ':' . $port . $path;
  27. }
  28. $this->httpClient = new HTTPClient();
  29. $this->httpClient->timeout = $timeout;
  30. }
  31. /** @inheritdoc */
  32. public function query()
  33. {
  34. $args = func_get_args();
  35. $method = array_shift($args);
  36. $request = new Request($method, $args);
  37. $length = $request->getLength();
  38. $xml = $request->getXml();
  39. $this->headers['Content-Type'] = 'text/xml';
  40. $this->headers['Content-Length'] = $length;
  41. $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
  42. if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
  43. $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
  44. return false;
  45. }
  46. // Check HTTP Response code
  47. if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
  48. $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
  49. return false;
  50. }
  51. // Now parse what we've got back
  52. $this->message = new Message($this->httpClient->resp_body);
  53. if (!$this->message->parse()) {
  54. // XML error
  55. return $this->handleError(-32700, 'Parse error. Message not well formed');
  56. }
  57. // Is the message a fault?
  58. if ($this->message->messageType == 'fault') {
  59. return $this->handleError($this->message->faultCode, $this->message->faultString);
  60. }
  61. // Message must be OK
  62. return true;
  63. }
  64. /**
  65. * Direct access to the underlying HTTP client if needed
  66. *
  67. * @return HTTPClient
  68. */
  69. public function getHTTPClient()
  70. {
  71. return $this->httpClient;
  72. }
  73. }