Comment.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if($GLOBALS['config']['comment']['verify'] == 1){
  35. if(!captcha_check($param['verify'])){
  36. return ['code'=>1002,'msg'=>lang('verify_err')];
  37. }
  38. }
  39. if($GLOBALS['config']['comment']['login'] ==1){
  40. if(empty(cookie('user_id'))){
  41. return ['code' => 1003, 'msg' =>lang('index/require_login')];
  42. }
  43. $res = model('User')->checkLogin();
  44. if($res['code']>1) {
  45. return ['code' => 1003, 'msg' => lang('index/require_login')];
  46. }
  47. }
  48. if(empty($param['comment_content'])){
  49. return ['code'=>1004,'msg'=>lang('index/require_content')];
  50. }
  51. $cookie = 'comment_timespan';
  52. if(!empty(cookie($cookie))){
  53. return ['code'=>1005,'msg'=>lang('frequently')];
  54. }
  55. $param['comment_content']= htmlentities(mac_filter_words($param['comment_content']));
  56. // if(!preg_match('/[^\x00-\x80]/',$param['comment_content'])){
  57. // return ['code'=>1005,'msg'=>lang('index/require_cn')];
  58. // }
  59. if(!in_array($param['comment_mid'],['1','2','3','8','9','11'])){
  60. return ['code'=>1006,'msg'=>lang('index/mid_err')];
  61. }
  62. if(empty(cookie('user_id'))){
  63. $param['comment_name'] = lang('controller/visitor');
  64. }
  65. else{
  66. $param['comment_name'] = cookie('user_name');
  67. $param['user_id'] = intval(cookie('user_id'));
  68. $user_data = model('User')->field('user_nick_name')->where(['user_id' => $param['user_id']])->find();
  69. if (!empty($user_data['user_nick_name'])) {
  70. $param['comment_name'] = $user_data['user_nick_name'];
  71. }
  72. }
  73. $param['comment_name'] = htmlentities(trim($param['comment_name']));
  74. $param['comment_rid'] = intval($param['comment_rid']);
  75. $param['comment_pid'] = intval($param['comment_pid']);
  76. if($GLOBALS['config']['comment']['audit'] ==1){
  77. $param['comment_status'] = 0;
  78. }
  79. $param['comment_ip'] = mac_get_ip_long();
  80. $blcaks = config('blacks');
  81. //判断黑名单关键字是否为空 不为空并且大于0则循环判断是否包含黑名单关键字
  82. if(!empty($blcaks['black_keyword_list']) && count($blcaks['black_keyword_list']) > 0){
  83. foreach ($blcaks['black_keyword_list'] as $key => $value) {
  84. if(strpos($param['comment_content'], $value) !== false){
  85. return ['code'=>1007,'msg'=>lang('index/blacklist_keyword')];
  86. }
  87. }
  88. }
  89. //判断黑名单IP是否为空 不为空并且大于0则循环判断客户端ip是否包含黑名单ip
  90. if(!empty($blcaks['black_ip_list']) && count($blcaks['black_ip_list']) > 0){
  91. $client_ip = long2ip($param['comment_ip']);
  92. if (in_array($client_ip, $blcaks['black_ip_list'])){
  93. return ['code'=>1008,'msg'=>lang('index/blacklist_ip')];
  94. }
  95. }
  96. $res = model('Comment')->saveData($param);
  97. if($res['code']>1){
  98. return $res;
  99. }
  100. else{
  101. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  102. if($GLOBALS['config']['comment']['audit'] ==1){
  103. $res['msg'] = lang('index/thanks_msg_audit');
  104. }
  105. else{
  106. $res['msg'] = lang('index/thanks_msg');
  107. }
  108. return $res;
  109. }
  110. }
  111. public function report()
  112. {
  113. $param = input();
  114. $id = intval($param['id']);
  115. if(empty($id) ) {
  116. return json(['code'=>1001,'msg'=>lang('param_err')]);
  117. }
  118. $cookie = 'comment-report-' . $id;
  119. if(!empty(cookie($cookie))){
  120. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  121. }
  122. $where = [];
  123. $where['comment_id'] = $id;
  124. model('comment')->where($where)->setInc('comment_report');
  125. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  126. return json(['code'=>1,'msg'=>lang('opt_ok')]);
  127. }
  128. public function digg()
  129. {
  130. $param = input();
  131. $id = intval($param['id']);
  132. $type = $param['type'];
  133. if(empty($id) || empty($type) ) {
  134. return json(['code'=>1001,'msg'=>lang('param_err')]);
  135. }
  136. $pre = 'comment';
  137. $where = [];
  138. $where[$pre.'_id'] = $id;
  139. $field = $pre.'_up,'.$pre.'_down';
  140. $model = model($pre);
  141. if($type) {
  142. $cookie = $pre . '-digg-' . $id;
  143. if(!empty(cookie($cookie))){
  144. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  145. }
  146. if ($type == 'up') {
  147. $model->where($where)->setInc($pre . '_up');
  148. } elseif ($type == 'down') {
  149. $model->where($where)->setInc($pre . '_down');
  150. }
  151. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  152. }
  153. $res = $model->infoData($where,$field);
  154. if($res['code']>1) {
  155. return json($res);
  156. }
  157. $info = $res['info'];
  158. if ($info) {
  159. $data = $info;
  160. }
  161. else{
  162. $data[$pre.'_up'] = 0;
  163. $data[$pre.'_down'] = 0;
  164. }
  165. return json(['code'=>1,'msg'=>lang('opt_ok'),'data'=>$data]);
  166. }
  167. }