Server.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace IXR;
  3. use Typecho\Widget\Exception as WidgetException;
  4. /**
  5. * IXR服务器
  6. *
  7. * @package IXR
  8. */
  9. class Server
  10. {
  11. /**
  12. * 回调函数
  13. *
  14. * @var array
  15. */
  16. private $callbacks;
  17. /**
  18. * 默认参数
  19. *
  20. * @var array
  21. */
  22. private $capabilities;
  23. /**
  24. * @var Hook
  25. */
  26. private $hook;
  27. /**
  28. * 构造函数
  29. *
  30. * @param array $callbacks 回调函数
  31. */
  32. public function __construct(array $callbacks = [])
  33. {
  34. $this->setCapabilities();
  35. $this->callbacks = $callbacks;
  36. $this->setCallbacks();
  37. }
  38. /**
  39. * 获取默认参数
  40. *
  41. * @access public
  42. * @return array
  43. */
  44. public function getCapabilities(): array
  45. {
  46. return $this->capabilities;
  47. }
  48. /**
  49. * 列出所有方法
  50. *
  51. * @access public
  52. * @return array
  53. */
  54. public function listMethods(): array
  55. {
  56. // Returns a list of methods - uses array_reverse to ensure user defined
  57. // methods are listed before server defined methods
  58. return array_reverse(array_keys($this->callbacks));
  59. }
  60. /**
  61. * 一次处理多个请求
  62. *
  63. * @param array $methodcalls
  64. * @return array
  65. */
  66. public function multiCall(array $methodcalls): array
  67. {
  68. // See http://www.xmlrpc.com/discuss/msgReader$1208
  69. $return = [];
  70. foreach ($methodcalls as $call) {
  71. $method = $call['methodName'];
  72. $params = $call['params'];
  73. if ($method == 'system.multicall') {
  74. $result = new Error(-32600, 'Recursive calls to system.multicall are forbidden');
  75. } else {
  76. $result = $this->call($method, $params);
  77. }
  78. if (is_a($result, 'Error')) {
  79. $return[] = [
  80. 'faultCode' => $result->code,
  81. 'faultString' => $result->message
  82. ];
  83. } else {
  84. $return[] = [$result];
  85. }
  86. }
  87. return $return;
  88. }
  89. /**
  90. * @param string $methodName
  91. * @return string|Error
  92. */
  93. public function methodHelp(string $methodName)
  94. {
  95. if (!$this->hasMethod($methodName)) {
  96. return new Error(-32601, 'server error. requested method ' . $methodName . ' does not exist.');
  97. }
  98. [$object, $method] = $this->callbacks[$methodName];
  99. try {
  100. $ref = new \ReflectionMethod($object, $method);
  101. $doc = $ref->getDocComment();
  102. return $doc ?: '';
  103. } catch (\ReflectionException $e) {
  104. return '';
  105. }
  106. }
  107. /**
  108. * @param Hook $hook
  109. */
  110. public function setHook(Hook $hook)
  111. {
  112. $this->hook = $hook;
  113. }
  114. /**
  115. * 呼叫内部方法
  116. *
  117. * @param string $methodName 方法名
  118. * @param array $args 参数
  119. * @return mixed
  120. */
  121. private function call(string $methodName, array $args)
  122. {
  123. if (!$this->hasMethod($methodName)) {
  124. return new Error(-32601, 'server error. requested method ' . $methodName . ' does not exist.');
  125. }
  126. $method = $this->callbacks[$methodName];
  127. if (!is_callable($method)) {
  128. return new Error(
  129. -32601,
  130. 'server error. requested class method "' . $methodName . '" does not exist.'
  131. );
  132. }
  133. [$object, $objectMethod] = $method;
  134. try {
  135. $ref = new \ReflectionMethod($object, $objectMethod);
  136. $requiredArgs = $ref->getNumberOfRequiredParameters();
  137. if (count($args) < $requiredArgs) {
  138. return new Error(
  139. -32602,
  140. 'server error. requested class method "' . $methodName . '" require ' . $requiredArgs . ' params.'
  141. );
  142. }
  143. foreach ($ref->getParameters() as $key => $parameter) {
  144. if ($parameter->hasType() && !settype($args[$key], $parameter->getType()->getName())) {
  145. return new Error(
  146. -32602,
  147. 'server error. requested class method "'
  148. . $methodName . '" ' . $key . ' param has wrong type.'
  149. );
  150. }
  151. }
  152. if (isset($this->hook)) {
  153. $result = $this->hook->beforeRpcCall($methodName, $ref, $args);
  154. if (isset($result)) {
  155. return $result;
  156. }
  157. }
  158. $result = call_user_func_array($method, $args);
  159. if (isset($this->hook)) {
  160. $this->hook->afterRpcCall($methodName, $result);
  161. }
  162. return $result;
  163. } catch (\ReflectionException $e) {
  164. return new Error(
  165. -32601,
  166. 'server error. requested class method "' . $methodName . '" does not exist.'
  167. );
  168. } catch (Exception $e) {
  169. return new Error(
  170. $e->getCode(),
  171. $e->getMessage()
  172. );
  173. } catch (WidgetException $e) {
  174. return new Error(
  175. -32001,
  176. $e->getMessage()
  177. );
  178. } catch (\Exception $e) {
  179. return new Error(
  180. -32001,
  181. 'server error. requested class method "' . $methodName . '" failed.'
  182. );
  183. }
  184. }
  185. /**
  186. * 抛出错误
  187. *
  188. * @access private
  189. * @param integer|Error $error 错误代码
  190. * @param string|null $message 错误消息
  191. * @return void
  192. */
  193. private function error($error, ?string $message = null)
  194. {
  195. // Accepts either an error object or an error code and message
  196. if (!$error instanceof Error) {
  197. $error = new Error($error, $message);
  198. }
  199. $this->output($error->getXml());
  200. }
  201. /**
  202. * 输出xml
  203. *
  204. * @access private
  205. * @param string $xml 输出xml
  206. */
  207. private function output(string $xml)
  208. {
  209. $xml = '<?xml version="1.0"?>' . "\n" . $xml;
  210. $length = strlen($xml);
  211. header('Connection: close');
  212. header('Content-Length: ' . $length);
  213. header('Content-Type: text/xml');
  214. header('Date: ' . date('r'));
  215. echo $xml;
  216. exit;
  217. }
  218. /**
  219. * 是否存在方法
  220. *
  221. * @access private
  222. * @param string $method 方法名
  223. * @return mixed
  224. */
  225. private function hasMethod(string $method)
  226. {
  227. return in_array($method, array_keys($this->callbacks));
  228. }
  229. /**
  230. * 设置默认参数
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. private function setCapabilities()
  236. {
  237. // Initialises capabilities array
  238. $this->capabilities = [
  239. 'xmlrpc' => [
  240. 'specUrl' => 'http://www.xmlrpc.com/spec',
  241. 'specVersion' => 1
  242. ],
  243. 'faults_interop' => [
  244. 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
  245. 'specVersion' => 20010516
  246. ],
  247. 'system.multicall' => [
  248. 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
  249. 'specVersion' => 1
  250. ],
  251. ];
  252. }
  253. /**
  254. * 设置默认方法
  255. *
  256. * @access private
  257. * @return void
  258. */
  259. private function setCallbacks()
  260. {
  261. $this->callbacks['system.getCapabilities'] = [$this, 'getCapabilities'];
  262. $this->callbacks['system.listMethods'] = [$this, 'listMethods'];
  263. $this->callbacks['system.multicall'] = [$this, 'multiCall'];
  264. $this->callbacks['system.methodHelp'] = [$this, 'methodHelp'];
  265. }
  266. /**
  267. * 服务入口
  268. */
  269. public function serve()
  270. {
  271. $message = new Message(file_get_contents('php://input') ?: '');
  272. if (!$message->parse()) {
  273. $this->error(-32700, 'parse error. not well formed');
  274. } elseif ($message->messageType != 'methodCall') {
  275. $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
  276. }
  277. $result = $this->call($message->methodName, $message->params);
  278. // Is the result an error?
  279. if ($result instanceof Error) {
  280. $this->error($result);
  281. }
  282. // Encode the result
  283. $r = new Value($result);
  284. $resultXml = $r->getXml();
  285. // Create the XML
  286. $xml = <<<EOD
  287. <methodResponse>
  288. <params>
  289. <param>
  290. <value>
  291. $resultXml
  292. </value>
  293. </param>
  294. </params>
  295. </methodResponse>
  296. EOD;
  297. // Send it
  298. $this->output($xml);
  299. }
  300. }