Feedback.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Typecho\Cookie;
  5. use Typecho\Db;
  6. use Typecho\Router;
  7. use Typecho\Validate;
  8. use Typecho\Widget\Exception;
  9. use Widget\Base\Comments;
  10. if (!defined('__TYPECHO_ROOT_DIR__')) {
  11. exit;
  12. }
  13. /**
  14. * 反馈提交组件
  15. *
  16. * @category typecho
  17. * @package Widget
  18. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  19. * @license GNU General Public License 2.0
  20. */
  21. class Feedback extends Comments implements ActionInterface
  22. {
  23. /**
  24. * 内容对象
  25. *
  26. * @access private
  27. * @var Archive
  28. */
  29. private $content;
  30. /**
  31. * 对已注册用户的保护性检测
  32. *
  33. * @param string $userName 用户名
  34. * @return bool
  35. * @throws \Typecho\Db\Exception
  36. */
  37. public function requireUserLogin(string $userName): bool
  38. {
  39. if ($this->user->hasLogin() && $this->user->screenName != $userName) {
  40. /** 当前用户名与提交者不匹配 */
  41. return false;
  42. } elseif (
  43. !$this->user->hasLogin() && $this->db->fetchRow($this->db->select('uid')
  44. ->from('table.users')->where('screenName = ? OR name = ?', $userName, $userName)->limit(1))
  45. ) {
  46. /** 此用户名已经被注册 */
  47. return false;
  48. }
  49. return true;
  50. }
  51. /**
  52. * 初始化函数
  53. *
  54. * @throws \Exception
  55. */
  56. public function action()
  57. {
  58. /** 回调方法 */
  59. $callback = $this->request->type;
  60. $this->content = Router::match($this->request->permalink);
  61. /** 判断内容是否存在 */
  62. if (
  63. $this->content instanceof Archive &&
  64. $this->content->have() && $this->content->is('single') &&
  65. in_array($callback, ['comment', 'trackback'])
  66. ) {
  67. /** 如果文章不允许反馈 */
  68. if ('comment' == $callback) {
  69. /** 评论关闭 */
  70. if (!$this->content->allow('comment')) {
  71. throw new Exception(_t('对不起,此内容的反馈被禁止.'), 403);
  72. }
  73. /** 检查来源 */
  74. if ($this->options->commentsCheckReferer && 'false' != $this->parameter->checkReferer) {
  75. $referer = $this->request->getReferer();
  76. if (empty($referer)) {
  77. throw new Exception(_t('评论来源页错误.'), 403);
  78. }
  79. $refererPart = parse_url($referer);
  80. $currentPart = parse_url($this->content->permalink);
  81. if (
  82. $refererPart['host'] != $currentPart['host'] ||
  83. 0 !== strpos($refererPart['path'], $currentPart['path'])
  84. ) {
  85. //自定义首页支持
  86. if ('page:' . $this->content->cid == $this->options->frontPage) {
  87. $currentPart = parse_url(rtrim($this->options->siteUrl, '/') . '/');
  88. if (
  89. $refererPart['host'] != $currentPart['host'] ||
  90. 0 !== strpos($refererPart['path'], $currentPart['path'])
  91. ) {
  92. throw new Exception(_t('评论来源页错误.'), 403);
  93. }
  94. } else {
  95. throw new Exception(_t('评论来源页错误.'), 403);
  96. }
  97. }
  98. }
  99. /** 检查ip评论间隔 */
  100. if (
  101. !$this->user->pass('editor', true) && $this->content->authorId != $this->user->uid &&
  102. $this->options->commentsPostIntervalEnable
  103. ) {
  104. $latestComment = $this->db->fetchRow($this->db->select('created')->from('table.comments')
  105. ->where('cid = ? AND ip = ?', $this->content->cid, $this->request->getIp())
  106. ->order('created', Db::SORT_DESC)
  107. ->limit(1));
  108. if (
  109. $latestComment && ($this->options->time - $latestComment['created'] > 0 &&
  110. $this->options->time - $latestComment['created'] < $this->options->commentsPostInterval)
  111. ) {
  112. throw new Exception(_t('对不起, 您的发言过于频繁, 请稍侯再次发布.'), 403);
  113. }
  114. }
  115. }
  116. /** 如果文章不允许引用 */
  117. if ('trackback' == $callback && !$this->content->allow('ping')) {
  118. throw new Exception(_t('对不起,此内容的引用被禁止.'), 403);
  119. }
  120. /** 调用函数 */
  121. $this->$callback();
  122. } else {
  123. throw new Exception(_t('找不到内容'), 404);
  124. }
  125. }
  126. /**
  127. * 评论处理函数
  128. *
  129. * @throws \Exception
  130. */
  131. private function comment()
  132. {
  133. // 使用安全模块保护
  134. $this->security->enable($this->options->commentsAntiSpam);
  135. $this->security->protect();
  136. $comment = [
  137. 'cid' => $this->content->cid,
  138. 'created' => $this->options->time,
  139. 'agent' => $this->request->getAgent(),
  140. 'ip' => $this->request->getIp(),
  141. 'ownerId' => $this->content->author->uid,
  142. 'type' => 'comment',
  143. 'status' => !$this->content->allow('edit')
  144. && $this->options->commentsRequireModeration ? 'waiting' : 'approved'
  145. ];
  146. /** 判断父节点 */
  147. if ($parentId = $this->request->filter('int')->get('parent')) {
  148. if (
  149. $this->options->commentsThreaded
  150. && ($parent = $this->db->fetchRow($this->db->select('coid', 'cid')->from('table.comments')
  151. ->where('coid = ?', $parentId))) && $this->content->cid == $parent['cid']
  152. ) {
  153. $comment['parent'] = $parentId;
  154. } else {
  155. throw new Exception(_t('父级评论不存在'));
  156. }
  157. }
  158. //检验格式
  159. $validator = new Validate();
  160. $validator->addRule('author', 'required', _t('必须填写用户名'));
  161. $validator->addRule('author', 'xssCheck', _t('请不要在用户名中使用特殊字符'));
  162. $validator->addRule('author', [$this, 'requireUserLogin'], _t('您所使用的用户名已经被注册,请登录后再次提交'));
  163. $validator->addRule('author', 'maxLength', _t('用户名最多包含150个字符'), 150);
  164. if ($this->options->commentsRequireMail && !$this->user->hasLogin()) {
  165. $validator->addRule('mail', 'required', _t('必须填写电子邮箱地址'));
  166. }
  167. $validator->addRule('mail', 'email', _t('邮箱地址不合法'));
  168. $validator->addRule('mail', 'maxLength', _t('电子邮箱最多包含150个字符'), 150);
  169. if ($this->options->commentsRequireUrl && !$this->user->hasLogin()) {
  170. $validator->addRule('url', 'required', _t('必须填写个人主页'));
  171. }
  172. $validator->addRule('url', 'url', _t('个人主页地址格式错误'));
  173. $validator->addRule('url', 'maxLength', _t('个人主页地址最多包含255个字符'), 255);
  174. $validator->addRule('text', 'required', _t('必须填写评论内容'));
  175. $comment['text'] = $this->request->text;
  176. /** 对一般匿名访问者,将用户数据保存一个月 */
  177. if (!$this->user->hasLogin()) {
  178. /** Anti-XSS */
  179. $comment['author'] = $this->request->filter('trim')->author;
  180. $comment['mail'] = $this->request->filter('trim')->mail;
  181. $comment['url'] = $this->request->filter('trim', 'url')->url;
  182. /** 修正用户提交的url */
  183. if (!empty($comment['url'])) {
  184. $urlParams = parse_url($comment['url']);
  185. if (!isset($urlParams['scheme'])) {
  186. $comment['url'] = 'http://' . $comment['url'];
  187. }
  188. }
  189. $expire = 30 * 24 * 3600;
  190. Cookie::set('__typecho_remember_author', $comment['author'], $expire);
  191. Cookie::set('__typecho_remember_mail', $comment['mail'], $expire);
  192. Cookie::set('__typecho_remember_url', $comment['url'], $expire);
  193. } else {
  194. $comment['author'] = $this->user->screenName;
  195. $comment['mail'] = $this->user->mail;
  196. $comment['url'] = $this->user->url;
  197. /** 记录登录用户的id */
  198. $comment['authorId'] = $this->user->uid;
  199. }
  200. /** 评论者之前须有评论通过了审核 */
  201. if (!$this->options->commentsRequireModeration && $this->options->commentsWhitelist) {
  202. if (
  203. $this->size(
  204. $this->select()->where(
  205. 'author = ? AND mail = ? AND status = ?',
  206. $comment['author'],
  207. $comment['mail'],
  208. 'approved'
  209. )
  210. )
  211. ) {
  212. $comment['status'] = 'approved';
  213. } else {
  214. $comment['status'] = 'waiting';
  215. }
  216. }
  217. if ($error = $validator->run($comment)) {
  218. /** 记录文字 */
  219. Cookie::set('__typecho_remember_text', $comment['text']);
  220. throw new Exception(implode("\n", $error));
  221. }
  222. /** 生成过滤器 */
  223. try {
  224. $comment = self::pluginHandle()->comment($comment, $this->content);
  225. } catch (\Typecho\Exception $e) {
  226. Cookie::set('__typecho_remember_text', $comment['text']);
  227. throw $e;
  228. }
  229. /** 添加评论 */
  230. $commentId = $this->insert($comment);
  231. Cookie::delete('__typecho_remember_text');
  232. $this->db->fetchRow($this->select()->where('coid = ?', $commentId)
  233. ->limit(1), [$this, 'push']);
  234. /** 评论完成接口 */
  235. self::pluginHandle()->finishComment($this);
  236. $this->response->goBack('#' . $this->theId);
  237. }
  238. /**
  239. * 引用处理函数
  240. *
  241. * @throws Exception|\Typecho\Db\Exception
  242. */
  243. private function trackback()
  244. {
  245. /** 如果不是POST方法 */
  246. if (!$this->request->isPost() || $this->request->getReferer()) {
  247. $this->response->redirect($this->content->permalink);
  248. }
  249. /** 如果库中已经存在当前ip为spam的trackback则直接拒绝 */
  250. if (
  251. $this->size($this->select()
  252. ->where('status = ? AND ip = ?', 'spam', $this->request->getIp())) > 0
  253. ) {
  254. /** 使用404告诉机器人 */
  255. throw new Exception(_t('找不到内容'), 404);
  256. }
  257. $trackback = [
  258. 'cid' => $this->content->cid,
  259. 'created' => $this->options->time,
  260. 'agent' => $this->request->getAgent(),
  261. 'ip' => $this->request->getIp(),
  262. 'ownerId' => $this->content->author->uid,
  263. 'type' => 'trackback',
  264. 'status' => $this->options->commentsRequireModeration ? 'waiting' : 'approved'
  265. ];
  266. $trackback['author'] = $this->request->filter('trim')->blog_name;
  267. $trackback['url'] = $this->request->filter('trim', 'url')->url;
  268. $trackback['text'] = $this->request->excerpt;
  269. //检验格式
  270. $validator = new Validate();
  271. $validator->addRule('url', 'required', 'We require all Trackbacks to provide an url.')
  272. ->addRule('url', 'url', 'Your url is not valid.')
  273. ->addRule('url', 'maxLength', 'Your url is not valid.', 255)
  274. ->addRule('text', 'required', 'We require all Trackbacks to provide an excerption.')
  275. ->addRule('author', 'required', 'We require all Trackbacks to provide an blog name.')
  276. ->addRule('author', 'xssCheck', 'Your blog name is not valid.')
  277. ->addRule('author', 'maxLength', 'Your blog name is not valid.', 150);
  278. $validator->setBreak();
  279. if ($error = $validator->run($trackback)) {
  280. $message = ['success' => 1, 'message' => current($error)];
  281. $this->response->throwXml($message);
  282. }
  283. /** 截取长度 */
  284. $trackback['text'] = Common::subStr($trackback['text'], 0, 100, '[...]');
  285. /** 如果库中已经存在重复url则直接拒绝 */
  286. if (
  287. $this->size($this->select()
  288. ->where('cid = ? AND url = ? AND type <> ?', $this->content->cid, $trackback['url'], 'comment')) > 0
  289. ) {
  290. /** 使用403告诉机器人 */
  291. throw new Exception(_t('禁止重复提交'), 403);
  292. }
  293. /** 生成过滤器 */
  294. $trackback = self::pluginHandle()->trackback($trackback, $this->content);
  295. /** 添加引用 */
  296. $this->insert($trackback);
  297. /** 评论完成接口 */
  298. self::pluginHandle()->finishTrackback($this);
  299. /** 返回正确 */
  300. $this->response->throwXml(['success' => 0, 'message' => 'Trackback has registered.']);
  301. }
  302. }