心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentController.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @copyright (C)2020-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2020年06月27日
  7. * 评论控制器
  8. */
  9. namespace app\home\controller;
  10. use core\basic\Controller;
  11. use app\home\model\ParserModel;
  12. use core\basic\Url;
  13. class CommentController extends Controller
  14. {
  15. protected $parser;
  16. protected $model;
  17. protected $htmldir;
  18. public function __construct()
  19. {
  20. $this->model = new ParserModel();
  21. $this->parser = new ParserController();
  22. $this->htmldir = $this->config('tpl_html_dir') ? $this->config('tpl_html_dir') . '/' : '';
  23. }
  24. // 评论新增
  25. public function add()
  26. {
  27. if ($_POST) {
  28. if ($this->config('comment_status') === '0') {
  29. error('系统已经关闭评论功能,请到后台开启再试!');
  30. }
  31. if (time() - session('lastsub') < 10) {
  32. alert_back('您提交太频繁了,请稍后再试!');
  33. }
  34. if (! session('pboot_uid') && ! $this->config('comment_anonymous')) {
  35. if (! ! $backurl = $_SERVER['HTTP_REFERER']) {
  36. alert_location("请先注册登录后再评论!", Url::home('member/login', null, "backurl=" . urlencode($backurl)));
  37. } else {
  38. alert_location("请先注册登录后再评论!", Url::home('member/login'));
  39. }
  40. }
  41. // 验证码验证
  42. $checkcode = strtolower(post('checkcode', 'var'));
  43. if ($this->config('comment_check_code') !== '0') {
  44. if (! $checkcode) {
  45. alert_back('验证码不能为空!');
  46. }
  47. if ($checkcode != session('checkcode')) {
  48. alert_back('验证码错误!');
  49. }
  50. }
  51. // 接收数据
  52. $status = $this->config('comment_verify') === '0' ? 1 : 0;
  53. if (! $contentid = request('contentid', 'int')) {
  54. alert_back('文章ID未能正常获取,请使用POST或URL参数传递!');
  55. }
  56. $comment = post('comment');
  57. $data = array(
  58. 'pid' => request('pid', 'int') ?: 0,
  59. 'contentid' => $contentid,
  60. 'comment' => $comment,
  61. 'uid' => session('pboot_uid'),
  62. 'puid' => request('puid', 'int'),
  63. 'likes' => 0,
  64. 'oppose' => 0,
  65. 'status' => $status,
  66. 'user_ip' => ip2long(get_user_ip()),
  67. 'user_os' => get_user_os(),
  68. 'user_bs' => get_user_bs(),
  69. 'create_time' => get_datetime(),
  70. 'update_user' => '',
  71. 'update_time' => ''
  72. );
  73. if ($this->model->addComment($data)) {
  74. session('lastsub', time()); // 记录最后提交时间
  75. $this->log('文章' . $contentid . '评论提交成功!');
  76. if ($this->config('comment_send_mail') && $this->config('message_send_to')) {
  77. $mail_subject = "【" . CMSNAME . "】您有新的文章评论信息,请注意查收!";
  78. $mail_body = '评论内容:' . $comment . '<br>';
  79. $mail_body .= '<br>来自网站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
  80. sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
  81. }
  82. if ($status) {
  83. alert_location('评论提交成功!', '-1', 1);
  84. } else {
  85. alert_location('评论提交成功,请等待管理员审核!', '-1', 1);
  86. }
  87. } else {
  88. $this->log('文章评论提交失败!');
  89. alert_back('提交失败!');
  90. }
  91. } else {
  92. alert_back('提交失败,请使用POST方式提交!');
  93. }
  94. }
  95. // 我的评论
  96. public function my()
  97. {
  98. // 未登录时跳转到用户登录
  99. if (! session('pboot_uid')) {
  100. location(Url::home('member/login'));
  101. }
  102. $content = parent::parser($this->htmldir . 'member/mycomment.html'); // 框架标签解析
  103. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  104. $content = str_replace('{pboot:pagetitle}', '我的评论-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  105. $content = $this->parser->parserPositionLabel($content, 0, '我的评论', Url::home('comment/my')); // CMS当前位置标签解析
  106. $content = $this->parser->parserSpecialPageSortLabel($content, - 3, '我的评论', Url::home('comment/my')); // 解析分类标签
  107. $content = $this->parser->parserMyCommentLabel($content); // 我的评论
  108. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  109. echo $content;
  110. exit();
  111. }
  112. // 评论删除
  113. public function del()
  114. {
  115. // 未登录时跳转到用户登录
  116. if (! session('pboot_uid')) {
  117. location(Url::home('member/login'));
  118. }
  119. // 执行删除
  120. if (! ! $id = get('id', 'int')) {
  121. if ($this->model->delComment($id)) {
  122. alert_back('删除成功!', 1);
  123. } else {
  124. alert_back('删除失败!');
  125. }
  126. } else {
  127. alert_back('传递参数有误!');
  128. }
  129. }
  130. }