NXS_XMLRPC_Server

NXS_XMLRPC_Server.

Defined (1)

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

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