IXR_Server

IXR_Server.

Defined (1)

The class is defined in the following location(s).

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