class-IXR-client.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * IXR_Client
  4. *
  5. * @package IXR
  6. * @since 1.5.0
  7. *
  8. */
  9. class IXR_Client
  10. {
  11. var $server;
  12. var $port;
  13. var $path;
  14. var $useragent;
  15. var $response;
  16. var $message = false;
  17. var $debug = false;
  18. var $timeout;
  19. var $headers = array();
  20. // Storage place for an error message
  21. var $error = false;
  22. /**
  23. * PHP5 constructor.
  24. */
  25. function __construct( $server, $path = false, $port = 80, $timeout = 15 )
  26. {
  27. if (!$path) {
  28. // Assume we have been given a URL instead
  29. $bits = parse_url($server);
  30. $this->server = $bits['host'];
  31. $this->port = isset($bits['port']) ? $bits['port'] : 80;
  32. $this->path = isset($bits['path']) ? $bits['path'] : '/';
  33. // Make absolutely sure we have a path
  34. if (!$this->path) {
  35. $this->path = '/';
  36. }
  37. if ( ! empty( $bits['query'] ) ) {
  38. $this->path .= '?' . $bits['query'];
  39. }
  40. } else {
  41. $this->server = $server;
  42. $this->path = $path;
  43. $this->port = $port;
  44. }
  45. $this->useragent = 'The Incutio XML-RPC PHP Library';
  46. $this->timeout = $timeout;
  47. }
  48. /**
  49. * PHP4 constructor.
  50. */
  51. public function IXR_Client( $server, $path = false, $port = 80, $timeout = 15 ) {
  52. self::__construct( $server, $path, $port, $timeout );
  53. }
  54. /**
  55. * @since 1.5.0
  56. * @since 5.5.0 Formalized the existing `...$args` parameter by adding it
  57. * to the function signature.
  58. *
  59. * @return bool
  60. */
  61. function query( ...$args )
  62. {
  63. $method = array_shift($args);
  64. $request = new IXR_Request($method, $args);
  65. $length = $request->getLength();
  66. $xml = $request->getXml();
  67. $r = "\r\n";
  68. $request = "POST {$this->path} HTTP/1.0$r";
  69. // Merged from WP #8145 - allow custom headers
  70. $this->headers['Host'] = $this->server;
  71. $this->headers['Content-Type'] = 'text/xml';
  72. $this->headers['User-Agent'] = $this->useragent;
  73. $this->headers['Content-Length']= $length;
  74. foreach( $this->headers as $header => $value ) {
  75. $request .= "{$header}: {$value}{$r}";
  76. }
  77. $request .= $r;
  78. $request .= $xml;
  79. // Now send the request
  80. if ($this->debug) {
  81. echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
  82. }
  83. if ($this->timeout) {
  84. $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
  85. } else {
  86. $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
  87. }
  88. if (!$fp) {
  89. $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
  90. return false;
  91. }
  92. fputs($fp, $request);
  93. $contents = '';
  94. $debugContents = '';
  95. $gotFirstLine = false;
  96. $gettingHeaders = true;
  97. while (!feof($fp)) {
  98. $line = fgets($fp, 4096);
  99. if (!$gotFirstLine) {
  100. // Check line for '200'
  101. if (strstr($line, '200') === false) {
  102. $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
  103. return false;
  104. }
  105. $gotFirstLine = true;
  106. }
  107. if (trim($line) == '') {
  108. $gettingHeaders = false;
  109. }
  110. if (!$gettingHeaders) {
  111. // merged from WP #12559 - remove trim
  112. $contents .= $line;
  113. }
  114. if ($this->debug) {
  115. $debugContents .= $line;
  116. }
  117. }
  118. if ($this->debug) {
  119. echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n";
  120. }
  121. // Now parse what we've got back
  122. $this->message = new IXR_Message($contents);
  123. if (!$this->message->parse()) {
  124. // XML error
  125. $this->error = new IXR_Error(-32700, 'parse error. not well formed');
  126. return false;
  127. }
  128. // Is the message a fault?
  129. if ($this->message->messageType == 'fault') {
  130. $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
  131. return false;
  132. }
  133. // Message must be OK
  134. return true;
  135. }
  136. function getResponse()
  137. {
  138. // methodResponses can only have one param - return that
  139. return $this->message->params[0];
  140. }
  141. function isError()
  142. {
  143. return (is_object($this->error));
  144. }
  145. function getErrorCode()
  146. {
  147. return $this->error->code;
  148. }
  149. function getErrorMessage()
  150. {
  151. return $this->error->message;
  152. }
  153. }