Comment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $res = model('Comment')->saveData($param);
  81. if($res['code']>1){
  82. return $res;
  83. }
  84. else{
  85. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  86. if($GLOBALS['config']['comment']['audit'] ==1){
  87. $res['msg'] = lang('index/thanks_msg_audit');
  88. }
  89. else{
  90. $res['msg'] = lang('index/thanks_msg');
  91. }
  92. return $res;
  93. }
  94. }
  95. public function report()
  96. {
  97. $param = input();
  98. $id = intval($param['id']);
  99. if(empty($id) ) {
  100. return json(['code'=>1001,'msg'=>lang('param_err')]);
  101. }
  102. $cookie = 'comment-report-' . $id;
  103. if(!empty(cookie($cookie))){
  104. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  105. }
  106. $where = [];
  107. $where['comment_id'] = $id;
  108. model('comment')->where($where)->setInc('comment_report');
  109. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  110. return json(['code'=>1,'msg'=>lang('opt_ok')]);
  111. }
  112. public function digg()
  113. {
  114. $param = input();
  115. $id = intval($param['id']);
  116. $type = $param['type'];
  117. if(empty($id) || empty($type) ) {
  118. return json(['code'=>1001,'msg'=>lang('param_err')]);
  119. }
  120. $pre = 'comment';
  121. $where = [];
  122. $where[$pre.'_id'] = $id;
  123. $field = $pre.'_up,'.$pre.'_down';
  124. $model = model($pre);
  125. if($type) {
  126. $cookie = $pre . '-digg-' . $id;
  127. if(!empty(cookie($cookie))){
  128. return json(['code'=>1002,'msg'=>lang('index/haved')]);
  129. }
  130. if ($type == 'up') {
  131. $model->where($where)->setInc($pre . '_up');
  132. } elseif ($type == 'down') {
  133. $model->where($where)->setInc($pre . '_down');
  134. }
  135. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  136. }
  137. $res = $model->infoData($where,$field);
  138. if($res['code']>1) {
  139. return json($res);
  140. }
  141. $info = $res['info'];
  142. if ($info) {
  143. $data = $info;
  144. }
  145. else{
  146. $data[$pre.'_up'] = 0;
  147. $data[$pre.'_down'] = 0;
  148. }
  149. return json(['code'=>1,'msg'=>lang('opt_ok'),'data'=>$data]);
  150. }
  151. }