IXR_Server

IXR_Server.

Defined (1)

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

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