Comment.php 5.0 KB

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