From 89649522c74caebb24984ccf6ec0086be646472e Mon Sep 17 00:00:00 2001 From: joyqi Date: Sat, 4 Sep 2021 21:27:57 +0800 Subject: [PATCH] fix sandbox --- var/Typecho/Widget.php | 2 +- var/Typecho/Widget/Sandbox.php | 58 ---------------------------------- var/Utils/Upgrade.php | 8 ++--- var/Widget/Service.php | 7 +--- var/Widget/XmlRpc.php | 42 +++++++++++++++--------- 5 files changed, 32 insertions(+), 85 deletions(-) delete mode 100644 var/Typecho/Widget/Sandbox.php diff --git a/var/Typecho/Widget.php b/var/Typecho/Widget.php index c1bcf696..dbc8c3b6 100644 --- a/var/Typecho/Widget.php +++ b/var/Typecho/Widget.php @@ -154,7 +154,7 @@ abstract class Widget call_user_func($call, $widget); } } catch (Terminal $e) { - $widget = null; + $widget = $widget ?? null; } finally { if ($sandbox) { Response::getInstance()->endSandbox(); diff --git a/var/Typecho/Widget/Sandbox.php b/var/Typecho/Widget/Sandbox.php deleted file mode 100644 index 9e1502af..00000000 --- a/var/Typecho/Widget/Sandbox.php +++ /dev/null @@ -1,58 +0,0 @@ -params = $params; - } - - /** - * @param mixed $params - * @return Sandbox - */ - public static function factory($params = null): Sandbox - { - return new self(new Config($params)); - } - - /** - * run function in a sandbox - * - * @param callable $call - * @return mixed - */ - public function run(callable $call) - { - HttpRequest::getInstance()->beginSandbox($this->params); - HttpResponse::getInstance()->beginSandbox(); - - try { - $result = call_user_func($call); - } catch (Terminal $e) { - $result = null; - } finally { - HttpResponse::getInstance()->endSandbox(); - HttpRequest::getInstance()->endSandbox(); - } - - return $result; - } -} diff --git a/var/Utils/Upgrade.php b/var/Utils/Upgrade.php index b0740987..ed93da2b 100644 --- a/var/Utils/Upgrade.php +++ b/var/Utils/Upgrade.php @@ -5,7 +5,6 @@ namespace Utils; use Typecho\Common; use Typecho\Db; use Typecho\Exception; -use Typecho\Widget\Sandbox; use Widget\Options; use Widget\Themes\Edit; use Widget\Upload; @@ -963,10 +962,9 @@ Typecho_Date::setTimezoneOffset($options->timezone); */ public static function v0_8r10_5_17($db, $options) { - Sandbox::factory('change=' . $options->theme) - ->run(function () { - Edit::alloc()->action(); - }); + Edit::alloc(null, 'change=' . $options->theme, function (Edit $edit) { + $edit->action(); + }); } diff --git a/var/Widget/Service.php b/var/Widget/Service.php index 6bd806c7..cd59e05a 100644 --- a/var/Widget/Service.php +++ b/var/Widget/Service.php @@ -4,10 +4,8 @@ namespace Widget; use Typecho\Common; use Typecho\Http\Client; -use Typecho\Plugin; use Typecho\Response; use Typecho\Widget\Exception; -use Typecho\Widget\Sandbox; use Widget\Base\Options as BaseOptions; if (!defined('__TYPECHO_ROOT_DIR__')) { @@ -54,10 +52,7 @@ class Service extends BaseOptions implements ActionInterface } /** 获取post */ - $post = Sandbox::factory("cid={$this->request->cid}") - ->run(function () { - return Archive::alloc('type=post'); - }); + $post = Archive::alloc('type=post', "cid={$this->request->cid}"); if ($post->have() && preg_match_all("|]*href=[\"'](.*?)[\"'][^>]*>(.*?)|", $post->text, $matches)) { $links = array_unique($matches[1]); diff --git a/var/Widget/XmlRpc.php b/var/Widget/XmlRpc.php index 9cecc5f6..aa2a14b9 100644 --- a/var/Widget/XmlRpc.php +++ b/var/Widget/XmlRpc.php @@ -756,8 +756,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function wpDeleteCategory(int $blogId, string $userName, string $password, int $categoryId): bool { - CategoryEdit::alloc(null, ['mid' => $categoryId], false) - ->deleteCategory(); + CategoryEdit::alloc(null, ['mid' => $categoryId], function (CategoryEdit $category) { + $category->deleteCategory(); + }); return true; } @@ -773,7 +774,7 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function wpGetCommentCount(int $blogId, string $userName, string $password, int $postId): array { - $stat = Stat::alloc(null, ['cid' => $postId], false); + $stat = Stat::alloc(null, ['cid' => $postId]); return [ 'approved' => $stat->currentPublishedCommentsNum, @@ -808,7 +809,6 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function wpGetPostStatusList(int $blogId, string $userName, string $password): array { - return [ 'draft' => _t('草稿'), 'pending' => _t('待审核'), @@ -942,8 +942,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function wpGetComment(int $blogId, string $userName, string $password, int $commentId): array { - $comment = CommentsEdit::alloc(null, ['coid' => $commentId], false); - $comment->getComment(); + $comment = CommentsEdit::alloc(null, ['coid' => $commentId], function (CommentsEdit $comment) { + $comment->getComment(); + }); if (!$comment->have()) { throw new Exception(_t('评论不存在'), 404); @@ -1040,7 +1041,10 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function wpDeleteComment(int $blogId, string $userName, string $password, int $commentId): bool { - return CommentsEdit::alloc(null, ['coid' => $commentId], false)->deleteComment() > 0; + CommentsEdit::alloc(null, ['coid' => $commentId], function (CommentsEdit $comment) { + $comment->deleteComment(); + }); + return true; } /** @@ -1084,8 +1088,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook } - $comment = CommentsEdit::alloc(null, $input, false); - $comment->editComment(); + $comment = CommentsEdit::alloc(null, $input, function (CommentsEdit $comment) { + $comment->editComment(); + }); return $comment->have(); } @@ -1137,8 +1142,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook $input['text'] = $struct['content']; } - $comment = Feedback::alloc(['checkReferer' => false], $input, false); - $comment->action(); + $comment = Feedback::alloc(['checkReferer' => false], $input, function (Feedback $comment) { + $comment->action(); + }); return $comment->have() ? $comment->coid : 0; } @@ -1488,8 +1494,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function mtSetPostCategories(int $postId, string $userName, string $password, array $categories): bool { - $post = PostEdit::alloc(null, ['cid' => $postId], false); - $post->setCategories($postId, array_column($categories, 'categoryId'), 'publish' == $post->status); + PostEdit::alloc(null, ['cid' => $postId], function (PostEdit $post) use ($postId, $categories) { + $post->setCategories($postId, array_column($categories, 'categoryId'), 'publish' == $post->status); + }); return true; } @@ -1504,7 +1511,10 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function mtPublishPost(int $postId, string $userName, string $password): bool { - $post = PostEdit::alloc(null, ['cid' => $postId, 'status' => 'publish'], false); + PostEdit::alloc(null, ['cid' => $postId, 'status' => 'publish'], function (PostEdit $post) { + $post->markPost(); + }); + return true; } @@ -1588,7 +1598,9 @@ class XmlRpc extends Contents implements ActionInterface, Hook */ public function bloggerDeletePost(int $blogId, int $postId, string $userName, string $password, $publish): bool { - PostEdit::alloc(null, ['cid' => $postId], false)->deletePost(); + PostEdit::alloc(null, ['cid' => $postId], function (PostEdit $post) { + $post->deletePost(); + }); return true; }