Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

226 рядки
6.8 KiB

  1. <?php
  2. /**
  3. * IXR_Server
  4. *
  5. * @package IXR
  6. * @since 1.5.0
  7. */
  8. class IXR_Server
  9. {
  10. var $data;
  11. var $callbacks = array();
  12. var $message;
  13. var $capabilities;
  14. /**
  15. * PHP5 constructor.
  16. */
  17. function __construct( $callbacks = false, $data = false, $wait = false )
  18. {
  19. $this->setCapabilities();
  20. if ($callbacks) {
  21. $this->callbacks = $callbacks;
  22. }
  23. $this->setCallbacks();
  24. if (!$wait) {
  25. $this->serve($data);
  26. }
  27. }
  28. /**
  29. * PHP4 constructor.
  30. */
  31. public function IXR_Server( $callbacks = false, $data = false, $wait = false ) {
  32. self::__construct( $callbacks, $data, $wait );
  33. }
  34. function serve($data = false)
  35. {
  36. if (!$data) {
  37. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
  38. if ( function_exists( 'status_header' ) ) {
  39. status_header( 405 ); // WP #20986
  40. header( 'Allow: POST' );
  41. }
  42. header('Content-Type: text/plain'); // merged from WP #9093
  43. die('XML-RPC server accepts POST requests only.');
  44. }
  45. global $HTTP_RAW_POST_DATA;
  46. if (empty($HTTP_RAW_POST_DATA)) {
  47. // workaround for a bug in PHP 5.2.2 - http://bugs.php.net/bug.php?id=41293
  48. $data = file_get_contents('php://input');
  49. } else {
  50. $data =& $HTTP_RAW_POST_DATA;
  51. }
  52. }
  53. $this->message = new IXR_Message($data);
  54. if (!$this->message->parse()) {
  55. $this->error(-32700, 'parse error. not well formed');
  56. }
  57. if ($this->message->messageType != 'methodCall') {
  58. $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
  59. }
  60. $result = $this->call($this->message->methodName, $this->message->params);
  61. // Is the result an error?
  62. if (is_a($result, 'IXR_Error')) {
  63. $this->error($result);
  64. }
  65. // Encode the result
  66. $r = new IXR_Value($result);
  67. $resultxml = $r->getXml();
  68. // Create the XML
  69. $xml = <<<EOD
  70. <methodResponse>
  71. <params>
  72. <param>
  73. <value>
  74. $resultxml
  75. </value>
  76. </param>
  77. </params>
  78. </methodResponse>
  79. EOD;
  80. // Send it
  81. $this->output($xml);
  82. }
  83. function call($methodname, $args)
  84. {
  85. if (!$this->hasMethod($methodname)) {
  86. return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
  87. }
  88. $method = $this->callbacks[$methodname];
  89. // Perform the callback and send the response
  90. if (count($args) == 1) {
  91. // If only one parameter just send that instead of the whole array
  92. $args = $args[0];
  93. }
  94. // Are we dealing with a function or a method?
  95. if (is_string($method) && substr($method, 0, 5) == 'this:') {
  96. // It's a class method - check it exists
  97. $method = substr($method, 5);
  98. if (!method_exists($this, $method)) {
  99. return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
  100. }
  101. //Call the method
  102. $result = $this->$method($args);
  103. } else {
  104. // It's a function - does it exist?
  105. if (is_array($method)) {
  106. if (!is_callable(array($method[0], $method[1]))) {
  107. return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.');
  108. }
  109. } else if (!function_exists($method)) {
  110. return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
  111. }
  112. // Call the function
  113. $result = call_user_func($method, $args);
  114. }
  115. return $result;
  116. }
  117. function error($error, $message = false)
  118. {
  119. // Accepts either an error object or an error code and message
  120. if ($message && !is_object($error)) {
  121. $error = new IXR_Error($error, $message);
  122. }
  123. $this->output($error->getXml());
  124. }
  125. function output($xml)
  126. {
  127. $charset = function_exists('get_option') ? get_option('blog_charset') : '';
  128. if ($charset)
  129. $xml = '<?xml version="1.0" encoding="'.$charset.'"?>'."\n".$xml;
  130. else
  131. $xml = '<?xml version="1.0"?>'."\n".$xml;
  132. $length = strlen($xml);
  133. header('Connection: close');
  134. if ($charset)
  135. header('Content-Type: text/xml; charset='.$charset);
  136. else
  137. header('Content-Type: text/xml');
  138. header('Date: '.date('r'));
  139. echo $xml;
  140. exit;
  141. }
  142. function hasMethod($method)
  143. {
  144. return in_array($method, array_keys($this->callbacks));
  145. }
  146. function setCapabilities()
  147. {
  148. // Initialises capabilities array
  149. $this->capabilities = array(
  150. 'xmlrpc' => array(
  151. 'specUrl' => 'http://www.xmlrpc.com/spec',
  152. 'specVersion' => 1
  153. ),
  154. 'faults_interop' => array(
  155. 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
  156. 'specVersion' => 20010516
  157. ),
  158. 'system.multicall' => array(
  159. 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
  160. 'specVersion' => 1
  161. ),
  162. );
  163. }
  164. function getCapabilities($args)
  165. {
  166. return $this->capabilities;
  167. }
  168. function setCallbacks()
  169. {
  170. $this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
  171. $this->callbacks['system.listMethods'] = 'this:listMethods';
  172. $this->callbacks['system.multicall'] = 'this:multiCall';
  173. }
  174. function listMethods($args)
  175. {
  176. // Returns a list of methods - uses array_reverse to ensure user defined
  177. // methods are listed before server defined methods
  178. return array_reverse(array_keys($this->callbacks));
  179. }
  180. function multiCall($methodcalls)
  181. {
  182. // See http://www.xmlrpc.com/discuss/msgReader$1208
  183. $return = array();
  184. foreach ($methodcalls as $call) {
  185. $method = $call['methodName'];
  186. $params = $call['params'];
  187. if ($method == 'system.multicall') {
  188. $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
  189. } else {
  190. $result = $this->call($method, $params);
  191. }
  192. if (is_a($result, 'IXR_Error')) {
  193. $return[] = array(
  194. 'faultCode' => $result->code,
  195. 'faultString' => $result->message
  196. );
  197. } else {
  198. $return[] = array($result);
  199. }
  200. }
  201. return $return;
  202. }
  203. }