fix action

This commit is contained in:
joyqi 2021-08-27 18:27:21 +08:00
parent 539ef5e111
commit 93b5656109
18 changed files with 145 additions and 136 deletions

View File

@ -107,7 +107,7 @@ class Response
}
/**
* @param string $message
* @param mixed $message
*/
public function throwXml(string $message)
{

View File

@ -1,5 +1,9 @@
<?php
namespace Widget;
use Typecho\Widget;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
@ -9,7 +13,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
*
* @package Widget
*/
class Widget_Do extends Typecho_Widget
class Action extends Widget
{
/**
* 路由映射
@ -46,7 +50,7 @@ class Widget_Do extends Typecho_Widget
/**
* 入口函数,初始化路由器
*
* @throws Typecho_Widget_Exception|Typecho_Exception
* @throws Widget\Exception
*/
public function execute()
{
@ -61,13 +65,14 @@ class Widget_Do extends Typecho_Widget
}
if (isset($widgetName) && class_exists($widgetName)) {
$reflectionWidget = new ReflectionClass($widgetName);
if ($reflectionWidget->implementsInterface('Widget_Interface_Do')) {
self::widget($widgetName)->action();
$widget = self::widget($widgetName);
if ($widget instanceof ActionInterface) {
$widget->action();
return;
}
}
throw new Typecho_Widget_Exception(_t('请求的地址不存在'), 404);
throw new Widget\Exception(_t('请求的地址不存在'), 404);
}
}

View File

@ -5,7 +5,7 @@ namespace Widget;
/**
* 可以被Widget_Do调用的接口
*/
interface DoInterface
interface ActionInterface
{
/**
* 接口需要实现的入口函数

View File

@ -17,7 +17,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @category typecho
* @package Widget
*/
class Ajax extends BaseOptions implements DoInterface
class Ajax extends BaseOptions implements ActionInterface
{
/**
* 针对rewrite验证的请求返回

View File

@ -1834,13 +1834,11 @@ class Archive extends Contents
/**
* 处理分类
*
* @access private
* @param Query $select 查询对象
* @param boolean $hasPushed 是否已经压入队列
* @return void
* @throws WidgetException
* @throws WidgetException|Db\Exception
*/
private function categoryHandle(Query $select, &$hasPushed)
private function categoryHandle(Query $select, bool &$hasPushed)
{
/** 如果是分类 */
$categorySelect = $this->db->select()
@ -1920,13 +1918,11 @@ class Archive extends Contents
/**
* 处理标签
*
* @access private
* @param Query $select 查询对象
* @param boolean $hasPushed 是否已经压入队列
* @return void
* @throws WidgetException
* @throws WidgetException|Db\Exception
*/
private function tagHandle(Query $select, &$hasPushed)
private function tagHandle(Query $select, bool &$hasPushed)
{
$tagSelect = $this->db->select()->from('table.metas')
->where('type = ?', 'tag')->limit(1);
@ -1991,13 +1987,11 @@ class Archive extends Contents
/**
* 处理作者
*
* @access private
* @param Query $select 查询对象
* @param boolean $hasPushed 是否已经压入队列
* @return void
* @throws WidgetException
* @throws WidgetException|Db\Exception
*/
private function authorHandle(Query $select, &$hasPushed)
private function authorHandle(Query $select, bool &$hasPushed)
{
$uid = $this->request->filter('int')->uid;

View File

@ -282,7 +282,7 @@ class Comments extends Base
* 获取当前内容结构
*
* @return array|null
* @throws Exception|\Typecho\Widget\Exception
* @throws Exception
*/
protected function ___parentContent(): ?array
{

View File

@ -13,7 +13,7 @@ use Typecho\Router;
use Typecho\Widget;
use Widget\Base;
use Widget\Metas\Category\Rows;
use Widget\User\Author;
use Widget\Users\Author;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;

View File

@ -4,7 +4,7 @@ namespace Widget\Comments;
use Typecho\Db\Exception;
use Widget\Base\Comments;
use Widget\DoInterface;
use Widget\ActionInterface;
use Widget\Notice;
if (!defined('__TYPECHO_ROOT_DIR__')) {
@ -20,7 +20,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Comments implements DoInterface
class Edit extends Comments implements ActionInterface
{
/**
* 标记为待审核

View File

@ -6,7 +6,7 @@ use Typecho\Common;
use Typecho\Widget\Exception;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Layout;
use Widget\DoInterface;
use Widget\ActionInterface;
use Widget\Contents\Post\Edit as PostEdit;
use Widget\Notice;
use Widget\Upload;
@ -24,7 +24,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends PostEdit implements DoInterface
class Edit extends PostEdit implements ActionInterface
{
/**
* 执行函数

View File

@ -6,7 +6,7 @@ use Typecho\Common;
use Typecho\Date;
use Typecho\Widget\Exception;
use Widget\Contents\Post\Edit as PostEdit;
use Widget\DoInterface;
use Widget\ActionInterface;
use Widget\Notice;
use Widget\Service;
@ -23,7 +23,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends PostEdit implements DoInterface
class Edit extends PostEdit implements ActionInterface
{
/**
* 自定义字段的hook名称

View File

@ -10,7 +10,7 @@ use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
use Widget\Base\Contents;
use Widget\Base\Metas;
use Widget\DoInterface;
use Widget\ActionInterface;
use Typecho\Db\Exception as DbException;
use Typecho\Date as TypechoDate;
use Widget\Notice;
@ -29,7 +29,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Contents implements DoInterface
class Edit extends Contents implements ActionInterface
{
/**
* 自定义字段的hook名称

View File

@ -1,14 +1,17 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 反馈提交
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
namespace Widget;
use Typecho\Common;
use Typecho\Cookie;
use Typecho\Router;
use Typecho\Validate;
use Typecho\Widget\Exception;
use Widget\Base\Comments;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 反馈提交组件
@ -18,46 +21,32 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interface_Do
class Feedback extends Comments implements ActionInterface
{
/**
* 内容对象
*
* @access private
* @var Widget_Archive
* @var Archive
*/
private $_content;
/**
* 过滤评论内容
*
* @access public
* @param string $text 评论内容
* @return string
*/
public function filterText($text)
{
$text = str_replace("\r", '', trim($text));
$text = preg_replace("/\n{2,}/", "\n\n", $text);
return Typecho_Common::removeXSS(Typecho_Common::stripTags(
$text, $this->options->commentsHTMLTagAllowed));
}
private $content;
/**
* 对已注册用户的保护性检测
*
* @access public
* @param string $userName 用户名
* @return void
* @return bool
* @throws \Typecho\Db\Exception
*/
public function requireUserLogin($userName)
public function requireUserLogin(string $userName): bool
{
if ($this->user->hasLogin() && $this->user->screenName != $userName) {
/** 当前用户名与提交者不匹配 */
return false;
} elseif (!$this->user->hasLogin() && $this->db->fetchRow($this->db->select('uid')
->from('table.users')->where('screenName = ? OR name = ?', $userName, $userName)->limit(1))) {
} elseif (
!$this->user->hasLogin() && $this->db->fetchRow($this->db->select('uid')
->from('table.users')->where('screenName = ? OR name = ?', $userName, $userName)->limit(1))
) {
/** 此用户名已经被注册 */
return false;
}
@ -68,26 +57,26 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
/**
* 初始化函数
*
* @access public
* @return void
* @throws Typecho_Widget_Exception
* @throws \Exception
*/
public function action()
{
/** 回调方法 */
$callback = $this->request->type;
$this->_content = Typecho_Router::match($this->request->permalink);
$this->content = Router::match($this->request->permalink);
/** 判断内容是否存在 */
if (false !== $this->_content && $this->_content instanceof Widget_Archive &&
$this->_content->have() && $this->_content->is('single') &&
in_array($callback, ['comment', 'trackback'])) {
if (
$this->content instanceof Archive &&
$this->content->have() && $this->content->is('single') &&
in_array($callback, ['comment', 'trackback'])
) {
/** 如果文章不允许反馈 */
if ('comment' == $callback) {
/** 评论关闭 */
if (!$this->_content->allow('comment')) {
throw new Typecho_Widget_Exception(_t('对不起,此内容的反馈被禁止.'), 403);
if (!$this->content->allow('comment')) {
throw new Exception(_t('对不起,此内容的反馈被禁止.'), 403);
}
/** 检查来源 */
@ -95,63 +84,67 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
$referer = $this->request->getReferer();
if (empty($referer)) {
throw new Typecho_Widget_Exception(_t('评论来源页错误.'), 403);
throw new Exception(_t('评论来源页错误.'), 403);
}
$refererPart = parse_url($referer);
$currentPart = parse_url($this->_content->permalink);
if ($refererPart['host'] != $currentPart['host'] ||
0 !== strpos($refererPart['path'], $currentPart['path'])) {
$currentPart = parse_url($this->content->permalink);
if (
$refererPart['host'] != $currentPart['host'] ||
0 !== strpos($refererPart['path'], $currentPart['path'])
) {
//自定义首页支持
if ('page:' . $this->_content->cid == $this->options->frontPage) {
if ('page:' . $this->content->cid == $this->options->frontPage) {
$currentPart = parse_url(rtrim($this->options->siteUrl, '/') . '/');
if ($refererPart['host'] != $currentPart['host'] ||
0 !== strpos($refererPart['path'], $currentPart['path'])) {
throw new Typecho_Widget_Exception(_t('评论来源页错误.'), 403);
if (
$refererPart['host'] != $currentPart['host'] ||
0 !== strpos($refererPart['path'], $currentPart['path'])
) {
throw new Exception(_t('评论来源页错误.'), 403);
}
} else {
throw new Typecho_Widget_Exception(_t('评论来源页错误.'), 403);
throw new Exception(_t('评论来源页错误.'), 403);
}
}
}
/** 检查ip评论间隔 */
if (!$this->user->pass('editor', true) && $this->_content->authorId != $this->user->uid &&
$this->options->commentsPostIntervalEnable) {
if (
!$this->user->pass('editor', true) && $this->content->authorId != $this->user->uid &&
$this->options->commentsPostIntervalEnable
) {
$latestComment = $this->db->fetchRow($this->db->select('created')->from('table.comments')
->where('cid = ? AND ip = ?', $this->_content->cid, $this->request->getIp())
->where('cid = ? AND ip = ?', $this->content->cid, $this->request->getIp())
->order('created', Typecho_Db::SORT_DESC)
->limit(1));
if ($latestComment && ($this->options->time - $latestComment['created'] > 0 &&
$this->options->time - $latestComment['created'] < $this->options->commentsPostInterval)) {
throw new Typecho_Widget_Exception(_t('对不起, 您的发言过于频繁, 请稍侯再次发布.'), 403);
if (
$latestComment && ($this->options->time - $latestComment['created'] > 0 &&
$this->options->time - $latestComment['created'] < $this->options->commentsPostInterval)
) {
throw new Exception(_t('对不起, 您的发言过于频繁, 请稍侯再次发布.'), 403);
}
}
}
/** 如果文章不允许引用 */
if ('trackback' == $callback && !$this->_content->allow('ping')) {
throw new Typecho_Widget_Exception(_t('对不起,此内容的引用被禁止.'), 403);
if ('trackback' == $callback && !$this->content->allow('ping')) {
throw new Exception(_t('对不起,此内容的引用被禁止.'), 403);
}
/** 调用函数 */
$this->$callback();
} else {
throw new Typecho_Widget_Exception(_t('找不到内容'), 404);
throw new Exception(_t('找不到内容'), 404);
}
}
/**
* 评论处理函数
*
* @throws Typecho_Widget_Exception
* @throws Exception
* @throws Typecho_Exception
* @throws \Exception
*/
private function comment()
{
@ -160,27 +153,31 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
$this->security->protect();
$comment = [
'cid' => $this->_content->cid,
'cid' => $this->content->cid,
'created' => $this->options->time,
'agent' => $this->request->getAgent(),
'ip' => $this->request->getIp(),
'ownerId' => $this->_content->author->uid,
'ownerId' => $this->content->author->uid,
'type' => 'comment',
'status' => !$this->_content->allow('edit') && $this->options->commentsRequireModeration ? 'waiting' : 'approved'
'status' => !$this->content->allow('edit')
&& $this->options->commentsRequireModeration ? 'waiting' : 'approved'
];
/** 判断父节点 */
if ($parentId = $this->request->filter('int')->get('parent')) {
if ($this->options->commentsThreaded && ($parent = $this->db->fetchRow($this->db->select('coid', 'cid')->from('table.comments')
->where('coid = ?', $parentId))) && $this->_content->cid == $parent['cid']) {
if (
$this->options->commentsThreaded
&& ($parent = $this->db->fetchRow($this->db->select('coid', 'cid')->from('table.comments')
->where('coid = ?', $parentId))) && $this->content->cid == $parent['cid']
) {
$comment['parent'] = $parentId;
} else {
throw new Typecho_Widget_Exception(_t('父级评论不存在'));
throw new Exception(_t('父级评论不存在'));
}
}
//检验格式
$validator = new Typecho_Validate();
$validator = new Validate();
$validator->addRule('author', 'required', _t('必须填写用户名'));
$validator->addRule('author', 'xssCheck', _t('请不要在用户名中使用特殊字符'));
$validator->addRule('author', [$this, 'requireUserLogin'], _t('您所使用的用户名已经被注册,请登录后再次提交'));
@ -219,9 +216,9 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
}
$expire = $this->options->time + $this->options->timezone + 30 * 24 * 3600;
Typecho_Cookie::set('__typecho_remember_author', $comment['author'], $expire);
Typecho_Cookie::set('__typecho_remember_mail', $comment['mail'], $expire);
Typecho_Cookie::set('__typecho_remember_url', $comment['url'], $expire);
Cookie::set('__typecho_remember_author', $comment['author'], $expire);
Cookie::set('__typecho_remember_mail', $comment['mail'], $expire);
Cookie::set('__typecho_remember_url', $comment['url'], $expire);
} else {
$comment['author'] = $this->user->screenName;
$comment['mail'] = $this->user->mail;
@ -233,7 +230,16 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
/** 评论者之前须有评论通过了审核 */
if (!$this->options->commentsRequireModeration && $this->options->commentsWhitelist) {
if ($this->size($this->select()->where('author = ? AND mail = ? AND status = ?', $comment['author'], $comment['mail'], 'approved'))) {
if (
$this->size(
$this->select()->where(
'author = ? AND mail = ? AND status = ?',
$comment['author'],
$comment['mail'],
'approved'
)
)
) {
$comment['status'] = 'approved';
} else {
$comment['status'] = 'waiting';
@ -242,21 +248,21 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
if ($error = $validator->run($comment)) {
/** 记录文字 */
Typecho_Cookie::set('__typecho_remember_text', $comment['text']);
throw new Typecho_Widget_Exception(implode("\n", $error));
Cookie::set('__typecho_remember_text', $comment['text']);
throw new Exception(implode("\n", $error));
}
/** 生成过滤器 */
try {
$comment = $this->pluginHandle()->comment($comment, $this->_content);
} catch (Typecho_Exception $e) {
Typecho_Cookie::set('__typecho_remember_text', $comment['text']);
$comment = $this->pluginHandle()->comment($comment, $this->content);
} catch (\Typecho\Exception $e) {
Cookie::set('__typecho_remember_text', $comment['text']);
throw $e;
}
/** 添加评论 */
$commentId = $this->insert($comment);
Typecho_Cookie::delete('__typecho_remember_text');
Cookie::delete('__typecho_remember_text');
$this->db->fetchRow($this->select()->where('coid = ?', $commentId)
->limit(1), [$this, 'push']);
@ -269,29 +275,30 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
/**
* 引用处理函数
*
* @access private
* @return void
* @throws Exception|\Typecho\Db\Exception
*/
private function trackback()
{
/** 如果不是POST方法 */
if (!$this->request->isPost() || $this->request->getReferer()) {
$this->response->redirect($this->_content->permalink);
$this->response->redirect($this->content->permalink);
}
/** 如果库中已经存在当前ip为spam的trackback则直接拒绝 */
if ($this->size($this->select()
->where('status = ? AND ip = ?', 'spam', $this->request->getIp())) > 0) {
if (
$this->size($this->select()
->where('status = ? AND ip = ?', 'spam', $this->request->getIp())) > 0
) {
/** 使用404告诉机器人 */
throw new Typecho_Widget_Exception(_t('找不到内容'), 404);
throw new Exception(_t('找不到内容'), 404);
}
$trackback = [
'cid' => $this->_content->cid,
'cid' => $this->content->cid,
'created' => $this->options->time,
'agent' => $this->request->getAgent(),
'ip' => $this->request->getIp(),
'ownerId' => $this->_content->author->uid,
'ownerId' => $this->content->author->uid,
'type' => 'trackback',
'status' => $this->options->commentsRequireModeration ? 'waiting' : 'approved'
];
@ -301,7 +308,7 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
$trackback['text'] = $this->request->excerpt;
//检验格式
$validator = new Typecho_Validate();
$validator = new Validate();
$validator->addRule('url', 'required', 'We require all Trackbacks to provide an url.')
->addRule('url', 'url', 'Your url is not valid.')
->addRule('url', 'maxLength', 'Your url is not valid.', 255)
@ -317,17 +324,19 @@ class Widget_Feedback extends Widget_Abstract_Comments implements Widget_Interfa
}
/** 截取长度 */
$trackback['text'] = Typecho_Common::subStr($trackback['text'], 0, 100, '[...]');
$trackback['text'] = Common::subStr($trackback['text'], 0, 100, '[...]');
/** 如果库中已经存在重复url则直接拒绝 */
if ($this->size($this->select()
->where('cid = ? AND url = ? AND type <> ?', $this->_content->cid, $trackback['url'], 'comment')) > 0) {
if (
$this->size($this->select()
->where('cid = ? AND url = ? AND type <> ?', $this->content->cid, $trackback['url'], 'comment')) > 0
) {
/** 使用403告诉机器人 */
throw new Typecho_Widget_Exception(_t('禁止重复提交'), 403);
throw new Exception(_t('禁止重复提交'), 403);
}
/** 生成过滤器 */
$trackback = $this->pluginHandle()->trackback($trackback, $this->_content);
$trackback = $this->pluginHandle()->trackback($trackback, $this->content);
/** 添加引用 */
$this->insert($trackback);

View File

@ -44,7 +44,8 @@ class Init extends Widget
'Widget_Abstract_Users' => '\Widget\Base\Users',
'Widget_Metas_Category_List' => '\Widget\Metas\Category\Rows',
'Widget_Contents_Page_List' => '\Widget\Contents\Page\Rows',
'Widget_Interface_Do' => '\Widget\DoInterface'
'Widget_Interface_Do' => '\Widget\ActionInterface',
'Widget_Do' => '\Widget\Action',
]);
/** 对变量赋值 */

View File

@ -7,7 +7,7 @@ use Typecho\Db\Exception;
use Typecho\Validate;
use Typecho\Widget\Helper\Form;
use Widget\Base\Metas;
use Widget\DoInterface;
use Widget\ActionInterface;
use Widget\Notice;
if (!defined('__TYPECHO_ROOT_DIR__')) {
@ -22,7 +22,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Metas implements DoInterface
class Edit extends Metas implements ActionInterface
{
/**
* 入口函数

View File

@ -6,7 +6,7 @@ use Typecho\Common;
use Typecho\Db\Exception;
use Typecho\Widget\Helper\Form;
use Widget\Base\Metas;
use Widget\DoInterface;
use Widget\ActionInterface;
use Widget\Notice;
if (!defined('__TYPECHO_ROOT_DIR__')) {
@ -22,7 +22,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Edit extends Metas implements DoInterface
class Edit extends Metas implements ActionInterface
{
/**
* 入口函数

View File

@ -20,7 +20,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @category typecho
* @package Widget
*/
class Service extends BaseOptions implements DoInterface
class Service extends BaseOptions implements ActionInterface
{
/**
* 异步请求

View File

@ -20,7 +20,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
* @category typecho
* @package Widget
*/
class Upload extends Contents implements DoInterface
class Upload extends Contents implements ActionInterface
{
//上传文件目录
public const UPLOAD_DIR = '/usr/uploads';

View File

@ -1,6 +1,6 @@
<?php
namespace Widget\User;
namespace Widget\Users;
use Typecho\Db\Exception;
use Widget\Base\Users;