fix sandbox

This commit is contained in:
joyqi 2021-09-04 21:27:57 +08:00
parent 675efe1e43
commit 89649522c7
5 changed files with 32 additions and 85 deletions

View File

@ -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();

View File

@ -1,58 +0,0 @@
<?php
namespace Typecho\Widget;
use Typecho\Config;
use Typecho\Request as HttpRequest;
use Typecho\Response as HttpResponse;
/**
* sandbox env
*/
class Sandbox
{
/**
* @var Config
*/
private $params;
/**
* @param Config $params
*/
public function __construct(Config $params)
{
$this->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;
}
}

View File

@ -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();
});
}

View File

@ -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("|<a[^>]*href=[\"'](.*?)[\"'][^>]*>(.*?)</a>|", $post->text, $matches)) {
$links = array_unique($matches[1]);

View File

@ -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;
}