Comment.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use \think\Request;
  5. class Comment extends Base
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. //关闭中
  11. if($GLOBALS['config']['comment']['status'] == 0){
  12. echo 'comment is close';
  13. exit;
  14. }
  15. }
  16. public function index()
  17. {
  18. if (Request()->isPost()) {
  19. return $this->saveData();
  20. }
  21. $param = mac_param_url();
  22. $this->assign('param',$param);
  23. $this->assign('comment',$GLOBALS['config']['comment']);
  24. return $this->label_fetch('comment/index');
  25. }
  26. public function ajax() {
  27. $param = mac_param_url();
  28. $this->assign('param',$param);
  29. $this->assign('comment',$GLOBALS['config']['comment']);
  30. return $this->label_fetch('comment/ajax',0,'json');
  31. }
  32. public function saveData() {
  33. $param = input();
  34. // 兼容:部分模板/历史版本会使用 mid/rid 作为参数名
  35. if (empty($param['comment_mid']) && isset($param['mid'])) {
  36. $param['comment_mid'] = $param['mid'];
  37. }
  38. if (empty($param['comment_rid']) && isset($param['rid'])) {
  39. $param['comment_rid'] = $param['rid'];
  40. }
  41. // 兼容:部分调用会传模块字符串(vod/art/topic/actor/role/website)
  42. if (!empty($param['comment_mid']) && !is_numeric($param['comment_mid'])) {
  43. $mid_map = [
  44. 'vod' => 1,
  45. 'art' => 2,
  46. 'topic' => 3,
  47. 'actor' => 8,
  48. 'role' => 9,
  49. 'website' => 11,
  50. 'manga' => 12,
  51. ];
  52. $mid_key = strtolower(trim((string)$param['comment_mid']));
  53. if (isset($mid_map[$mid_key])) {
  54. $param['comment_mid'] = (string)$mid_map[$mid_key];
  55. }
  56. }
  57. // 兼容:有些模板会误把 comment_mid 传为 4(评论自身),若同时存在合法 mid 则优先使用之
  58. if (isset($param['comment_mid']) && (string)$param['comment_mid'] === '4' && isset($param['mid']) && in_array((string)$param['mid'], ['1','2','3','8','9','11','12'], true)) {
  59. $param['comment_mid'] = (string)$param['mid'];
  60. }
  61. if($GLOBALS['config']['comment']['verify'] == 1){
  62. if(!captcha_check($param['verify'])){
  63. return ['code'=>1002,'msg'=>lang('verify_err')];
  64. }
  65. }
  66. if($GLOBALS['config']['comment']['login'] ==1){
  67. if(empty(cookie('user_id'))){
  68. return ['code' => 1003, 'msg' =>lang('index/require_login')];
  69. }
  70. $res = model('User')->checkLogin();
  71. if($res['code']>1) {
  72. return ['code' => 1003, 'msg' => lang('index/require_login')];
  73. }
  74. }
  75. if(empty($param['comment_content'])){
  76. return ['code'=>1004,'msg'=>lang('index/require_content')];
  77. }
  78. $cookie = 'comment_timespan';
  79. if(!empty(cookie($cookie))){
  80. return ['code'=>1005,'msg'=>lang('frequently')];
  81. }
  82. $param['comment_content']= htmlentities(mac_filter_words($param['comment_content']));
  83. // if(!preg_match('/[^\x00-\x80]/',$param['comment_content'])){
  84. // return ['code'=>1005,'msg'=>lang('index/require_cn')];
  85. // }
  86. if(!in_array($param['comment_mid'],['1','2','3','8','9','11','12'])){
  87. return ['code'=>1006,'msg'=>lang('index/mid_err')];
  88. }
  89. if(empty(cookie('user_id'))){
  90. $param['comment_name'] = lang('controller/visitor');
  91. }
  92. else{
  93. $param['comment_name'] = cookie('user_name');
  94. $param['user_id'] = intval(cookie('user_id'));
  95. $user_data = model('User')->field('user_nick_name')->where(['user_id' => $param['user_id']])->find();
  96. if (!empty($user_data['user_nick_name'])) {
  97. $param['comment_name'] = $user_data['user_nick_name'];
  98. }
  99. }
  100. $param['comment_name'] = htmlentities(trim($param['comment_name']));
  101. $param['comment_rid'] = intval($param['comment_rid']);
  102. $param['comment_pid'] = intval($param['comment_pid']);
  103. if($GLOBALS['config']['comment']['audit'] ==1){
  104. $param['comment_status'] = 0;
  105. }
  106. $param['comment_ip'] = mac_get_ip_long();
  107. $blcaks = config('blacks');
  108. //判断黑名单关键字是否为空 不为空并且大于0则循环判断是否包含黑名单关键字
  109. if(!empty($blcaks['black_keyword_list']) && count($blcaks['black_keyword_list']) > 0){
  110. foreach ($blcaks['black_keyword_list'] as $key => $value) {
  111. if(strpos($param['comment_content'], $value) !== false){
  112. return ['code'=>1007,'msg'=>lang('index/blacklist_keyword')];
  113. }
  114. }
  115. }
  116. //判断黑名单IP是否为空 不为空并且大于0则循环判断客户端ip是否包含黑名单ip
  117. if(!empty($blcaks['black_ip_list']) && count($blcaks['black_ip_list']) > 0){
  118. $client_ip = long2ip($param['comment_ip']);
  119. if (in_array($client_ip, $blcaks['black_ip_list'])){
  120. return ['code'=>1008,'msg'=>lang('index/blacklist_ip')];
  121. }
  122. }
  123. $res = model('Comment')->saveData($param);
  124. if($res['code']>1){
  125. return $res;
  126. }
  127. else{
  128. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  129. if($GLOBALS['config']['comment']['audit'] ==1){
  130. $res['msg'] = lang('index/thanks_msg_audit');
  131. }
  132. else{
  133. $res['msg'] = lang('index/thanks_msg');
  134. }
  135. return $res;
  136. }
  137. }
  138. public function report()
  139. {
  140. $param = input();
  141. $id = intval($param['id']);
  142. if(empty($id) ) {
  143. return json(['code'=>1001,'msg'=>lang('param_err')]);
  144. }
  145. $cookie = 'comment-report-' . $id;
  146. if(!empty(cookie($cookie))){
  147. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  148. }
  149. $where = [];
  150. $where['comment_id'] = $id;
  151. model('comment')->where($where)->setInc('comment_report');
  152. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  153. return json(['code'=>1,'msg'=>lang('opt_ok')]);
  154. }
  155. public function digg()
  156. {
  157. $param = input();
  158. $id = intval($param['id']);
  159. $type = $param['type'];
  160. if(empty($id) || empty($type) ) {
  161. return json(['code'=>1001,'msg'=>lang('param_err')]);
  162. }
  163. $pre = 'comment';
  164. $where = [];
  165. $where[$pre.'_id'] = $id;
  166. $field = $pre.'_up,'.$pre.'_down';
  167. $model = model($pre);
  168. if($type) {
  169. $cookie = $pre . '-digg-' . $id;
  170. if(!empty(cookie($cookie))){
  171. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  172. }
  173. if ($type == 'up') {
  174. $model->where($where)->setInc($pre . '_up');
  175. } elseif ($type == 'down') {
  176. $model->where($where)->setInc($pre . '_down');
  177. }
  178. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  179. }
  180. $res = $model->infoData($where,$field);
  181. if($res['code']>1) {
  182. return json($res);
  183. }
  184. $info = $res['info'];
  185. if ($info) {
  186. $data = $info;
  187. }
  188. else{
  189. $data[$pre.'_up'] = 0;
  190. $data[$pre.'_down'] = 0;
  191. }
  192. return json(['code'=>1,'msg'=>lang('opt_ok'),'data'=>$data]);
  193. }
  194. }