Comment.php 5.1 KB

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