From 6cedf6a0a11d8955e6c93c0a27a170da2b4f0c43 Mon Sep 17 00:00:00 2001 From: joyqi Date: Fri, 3 Sep 2021 00:15:22 +0800 Subject: [PATCH] fix xmlrpc --- var/IXR/Server.php | 37 ++++++++++++++++++++++++++++- var/Widget/XmlRpc.php | 54 ++++++++++++++++--------------------------- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/var/IXR/Server.php b/var/IXR/Server.php index 7a9e0402..75209d5e 100644 --- a/var/IXR/Server.php +++ b/var/IXR/Server.php @@ -2,6 +2,8 @@ namespace IXR; +use Typecho\Widget\Exception as WidgetException; + /** * IXR服务器 * @@ -71,7 +73,7 @@ class Server * @param array $methodcalls * @return array */ - public function multiCall(array $methodcalls) + public function multiCall(array $methodcalls): array { // See http://www.xmlrpc.com/discuss/msgReader$1208 $return = []; @@ -95,6 +97,28 @@ class Server return $return; } + /** + * @param string $methodName + * @return string|Error + */ + public function methodHelp(string $methodName) + { + if (!$this->hasMethod($methodName)) { + return new Error(-32601, 'server error. requested method ' . $methodName . ' does not exist.'); + } + + [$object, $method] = $this->callbacks[$methodName]; + + try { + $ref = new \ReflectionMethod($object, $method); + $doc = $ref->getDocComment(); + + return $doc ?: ''; + } catch (\ReflectionException $e) { + return ''; + } + } + /** * @param Hook $hook */ @@ -166,6 +190,16 @@ class Server -32601, 'server error. requested class method "' . $methodName . '" does not exist.' ); + } catch (Exception $e) { + return new Error( + $e->getCode(), + $e->getMessage() + ); + } catch (WidgetException $e) { + return new Error( + -32001, + $e->getMessage() + ); } catch (\Exception $e) { return new Error( -32001, @@ -258,6 +292,7 @@ class Server $this->callbacks['system.getCapabilities'] = [$this, 'getCapabilities']; $this->callbacks['system.listMethods'] = [$this, 'listMethods']; $this->callbacks['system.multicall'] = [$this, 'multiCall']; + $this->callbacks['system.methodHelp'] = [$this, 'methodHelp']; } /** diff --git a/var/Widget/XmlRpc.php b/var/Widget/XmlRpc.php index c95a3c9b..326a267d 100644 --- a/var/Widget/XmlRpc.php +++ b/var/Widget/XmlRpc.php @@ -364,6 +364,7 @@ class XmlRpc extends Contents implements ActionInterface, Hook * @param array $content * @param bool $publish * @return mixed + * @throws \Typecho\Db\Exception */ public function mwNewPost(int $blogId, string $userName, string $password, array $content, bool $publish) { @@ -469,20 +470,15 @@ class XmlRpc extends Contents implements ActionInterface, Hook } /** 调用已有组件 */ - try { - /** 插入 */ - if ('page' == $type) { - $widget = PageEdit::alloc(null, $input, false); - $widget->writePage(); - } else { - $widget = PostEdit::alloc(null, $input, false); - $widget->writePost(); - } - - return $widget->cid; - } catch (Exception $e) { - return new Error($e->getCode(), $e->getMessage()); + if ('page' == $type) { + $widget = PageEdit::alloc(null, $input, false); + $widget->writePage(); + } else { + $widget = PostEdit::alloc(null, $input, false); + $widget->writePost(); } + + return $widget->cid; } /** @@ -492,10 +488,10 @@ class XmlRpc extends Contents implements ActionInterface, Hook * @param string $userName * @param string $password * @param array $category - * @return mixed + * @return int * @throws \Typecho\Db\Exception */ - public function wpNewCategory(int $blogId, string $userName, string $password, array $category) + public function wpNewCategory(int $blogId, string $userName, string $password, array $category): int { /** 开始接受数据 */ @@ -505,14 +501,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook $input['description'] = $category['description'] ?? $category['name']; /** 调用已有组件 */ - try { - /** 插入 */ - $categoryWidget = CategoryEdit::alloc(null, $input, false); - $categoryWidget->insertCategory(); - return $categoryWidget->mid; - } catch (Exception $e) { - return new Error($e->getCode(), $e->getMessage()); - } + $categoryWidget = CategoryEdit::alloc(null, $input, false); + $categoryWidget->insertCategory(); + return $categoryWidget->mid; } /** @@ -522,18 +513,12 @@ class XmlRpc extends Contents implements ActionInterface, Hook * @param string $userName * @param string $password * @param int $pageId - * @return bool|Error + * @return bool + * @throws \Typecho\Db\Exception */ - public function wpDeletePage(int $blogId, string $userName, string $password, int $pageId) + public function wpDeletePage(int $blogId, string $userName, string $password, int $pageId): bool { - /** 删除页面 */ - try { - /** 此组件会进行复杂的权限检测 */ - PageEdit::alloc(null, ['cid' => $pageId], false)->deletePage(); - } catch (Exception $e) { - return new Error($e->getCode(), $e->getMessage()); - } - + PageEdit::alloc(null, ['cid' => $pageId], false)->deletePage(); return true; } @@ -555,9 +540,10 @@ class XmlRpc extends Contents implements ActionInterface, Hook string $password, array $content, bool $publish - ) { + ): bool { $content['post_type'] = 'page'; $this->mwEditPost($pageId, $userName, $password, $content, $publish); + return true; } /**