comment.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * 评论操作类
  4. * @author chenzhouyu
  5. *
  6. */
  7. class comment {
  8. //数据库连接
  9. private $comment_db, $comment_setting_db, $comment_data_db, $comment_table_db, $comment_check_db;
  10. public $msg_code = 0;
  11. public function __construct() {
  12. $this->comment_db = pc_base::load_model('comment_model');
  13. $this->comment_setting_db = pc_base::load_model('comment_setting_model');
  14. $this->comment_data_db = pc_base::load_model('comment_data_model');
  15. $this->comment_table_db = pc_base::load_model('comment_table_model');
  16. $this->comment_check_db = pc_base::load_model('comment_check_model');
  17. }
  18. /**
  19. * 添加评论
  20. * @param string $commentid 评论ID
  21. * @param integer $siteid 站点ID
  22. * @param array $data 内容数组应该包括array('userid'=>用户ID,'username'=>用户名,'content'=>内容,'direction'=>方向(0:没有方向 ,1:正方,2:反方,3:中立))
  23. * @param string $id 回复评论的内容
  24. * @param string $title 文章标题
  25. * @param string $url 文章URL地址
  26. */
  27. public function add($commentid, $siteid, $data, $id = '', $title = '', $url = '') {
  28. //开始查询评论这条评论是否存在。
  29. $title = new_addslashes($title);
  30. if (!$comment = $this->comment_db->get_one(array('commentid'=>$commentid, 'siteid'=>$siteid), 'tableid, commentid')) { //评论不存在
  31. //取得当前可以使用的内容数据表
  32. $r = $this->comment_table_db->get_one('', 'tableid, total', 'tableid desc');
  33. $tableid = $r['tableid'];
  34. if ($r['total'] >= 1000000) {
  35. //当上一张数据表存的数据已经达到1000000时,创建新的数据存储表,存储数据。
  36. if (!$tableid = $this->comment_table_db->creat_table()) {
  37. $this->msg_code = 4;
  38. return false;
  39. }
  40. }
  41. //新建评论到评论总表中。
  42. $comment_data = array('commentid'=>$commentid, 'siteid'=>$siteid, 'tableid'=>$tableid, 'display_type'=>($data['direction']>0 ? 1 : 0));
  43. if (!empty($title)) $comment_data['title'] = $title;
  44. if (!empty($url)) $comment_data['url'] = $url;
  45. if (!$this->comment_db->insert($comment_data)) {
  46. $this->msg_code = 5;
  47. return false;
  48. }
  49. } else {//评论存在时
  50. $tableid = $comment['tableid'];
  51. }
  52. if (empty($tableid)) {
  53. $this->msg_code = 1;
  54. return false;
  55. }
  56. //为数据存储数据模型设置 数据表名。
  57. $this->comment_data_db->table_name($tableid);
  58. //检查数据存储表。
  59. if (!$this->comment_data_db->table_exists('comment_data_'.$tableid)) {
  60. //当存储数据表不存时,尝试创建数据表。
  61. if (!$tableid = $this->comment_table_db->creat_table($tableid)) {
  62. $this->msg_code = 2;
  63. return false;
  64. }
  65. }
  66. //向数据存储表中写入数据。
  67. $data['commentid'] = $commentid;
  68. $data['siteid'] = $siteid;
  69. $data['ip'] = ip();
  70. $data['status'] = 1;
  71. $data['creat_at'] = SYS_TIME;
  72. //对评论的内容进行关键词过滤。
  73. $data['content'] = strip_tags($data['content']);
  74. $badword = pc_base::load_model('badword_model');
  75. $data['content'] = $badword->replace_badword($data['content']);
  76. if ($id) {
  77. $r = $this->comment_data_db->get_one(array('id'=>$id));
  78. if ($r) {
  79. pc_base::load_sys_class('format', '', 0);
  80. if ($r['reply']) {
  81. $data['content'] = '<div class="content">'.str_replace('<span></span>', '<span class="blue f12">'.$r['username'].' '.L('chez').' '.format::date($r['creat_at'], 1).L('release').'</span>', $r['content']).'</div><span></span>'.$data['content'];
  82. } else {
  83. $data['content'] = '<div class="content"><span class="blue f12">'.$r['username'].' '.L('chez').' '.format::date($r['creat_at'], 1).L('release').'</span><pre>'.$r['content'].'</pre></div><span></span>'.$data['content'];
  84. }
  85. $data['reply'] = 1;
  86. }
  87. }
  88. //判断当前站点是否需要审核
  89. $site = $this->comment_setting_db->site($siteid);
  90. if ($site['check']) {
  91. $data['status'] = 0;
  92. }
  93. $data['content'] = addslashes($data['content']);
  94. if ($comment_data_id = $this->comment_data_db->insert($data, true)) {
  95. //需要审核,插入到审核表
  96. if ($data['status']==0) {
  97. $this->comment_check_db->insert(array('comment_data_id'=>$comment_data_id, 'siteid'=>$siteid,'tableid'=>$tableid));
  98. } elseif (!empty($data['userid']) && !empty($site['add_point']) && module_exists('pay')) { //不需要审核直接给用户添加积分
  99. pc_base::load_app_class('receipts', 'pay', 0);
  100. receipts::point($site['add_point'], $data['userid'], $data['username'], '', 'selfincome', 'Comment');
  101. }
  102. //开始更新数据存储表数据总条数
  103. $this->comment_table_db->edit_total($tableid, '+=1');
  104. //开始更新评论总表数据总数
  105. $sql['lastupdate'] = SYS_TIME;
  106. //只有在评论通过的时候才更新评论主表的评论数
  107. if ($data['status'] == 1) {
  108. $sql['total'] = '+=1';
  109. switch ($data['direction']) {
  110. case 1: //正方
  111. $sql['square'] = '+=1';
  112. break;
  113. case 2://反方
  114. $sql['anti'] = '+=1';
  115. break;
  116. case 3://中立方
  117. $sql['neutral'] = '+=1';
  118. break;
  119. }
  120. }
  121. $this->comment_db->update($sql, array('commentid'=>$commentid));
  122. if ($site['check']) {
  123. $this->msg_code = 7;
  124. } else {
  125. $this->msg_code = 0;
  126. }
  127. return true;
  128. } else {
  129. $this->msg_code = 3;
  130. return false;
  131. }
  132. }
  133. /**
  134. * 支持评论
  135. * @param integer $commentid 评论ID
  136. * @param integer $id 内容ID
  137. */
  138. public function support($commentid, $id) {
  139. if ($data = $this->comment_db->get_one(array('commentid'=>$commentid), 'tableid')) {
  140. $this->comment_data_db->table_name($data['tableid']);
  141. if ($this->comment_data_db->update(array('support'=>'+=1'), array('id'=>$id))) {
  142. $this->msg_code = 0;
  143. return true;
  144. } else {
  145. $this->msg_code = 3;
  146. return false;
  147. }
  148. } else {
  149. $this->msg_code = 6;
  150. return false;
  151. }
  152. }
  153. /**
  154. * 更新评论的状态
  155. * @param string $commentid 评论ID
  156. * @param integer $id 内容ID
  157. * @param integer $status 状态{1:通过 ,0:未审核, -1:不通过,将做删除操作}
  158. */
  159. public function status($commentid, $id, $status = -1) {
  160. if (!$comment = $this->comment_db->get_one(array('commentid'=>$commentid), 'tableid, commentid')) {
  161. $this->msg_code = 6;
  162. return false;
  163. }
  164. //为数据存储数据模型设置 数据表名。
  165. $this->comment_data_db->table_name($comment['tableid']);
  166. if (!$comment_data = $this->comment_data_db->get_one(array('id'=>$id, 'commentid'=>$commentid))) {
  167. $this->msg_code = 6;
  168. return false;
  169. }
  170. //读取评论的站点配置信息
  171. $site = $this->comment_setting_db->get_one(array('siteid'=>$comment_data['siteid']));
  172. if ($status == 1) {//通过的时候
  173. $sql['total'] = '+=1';
  174. switch ($comment_data['direction']) {
  175. case 1: //正方
  176. $sql['square'] = '+=1';
  177. break;
  178. case 2://反方
  179. $sql['anti'] = '+=1';
  180. break;
  181. case 3://中立方
  182. $sql['neutral'] = '+=1';
  183. break;
  184. }
  185. //当评论被设置为通过的时候,更新评论总表的数量。
  186. $this->comment_db->update($sql, array('commentid'=>$comment['commentid']));
  187. //更新评论内容状态
  188. $this->comment_data_db->update(array('status'=>$status), array('id'=>$id, 'commentid'=>$commentid));
  189. //当评论用户ID不为空,而且站点配置了积分添加项,支付模块也存在的时候,为用户添加积分。
  190. if (!empty($comment_data['userid']) && !empty($site['add_point']) && module_exists('pay')) {
  191. pc_base::load_app_class('receipts', 'pay', 0);
  192. receipts::point($site['add_point'], $comment_data['userid'], $comment_data['username'], '', 'selfincome', 'Comment');
  193. }
  194. } elseif ($status == -1) { //删除数据
  195. //如果数据原有状态为已经通过,需要删除评论总表中的总数
  196. if ($comment_data['status'] == 1) {
  197. $sql['total'] = '-=1';
  198. switch ($comment_data['direction']) {
  199. case '1': //正方
  200. $sql['square'] = '-=1';
  201. break;
  202. case '2'://反方
  203. $sql['anti'] = '-=1';
  204. break;
  205. case '3'://中立方
  206. $sql['neutral'] = '-=1';
  207. break;
  208. }
  209. $this->comment_db->update($sql, array('commentid'=>$comment['commentid']));
  210. }
  211. //删除存储表的数据
  212. $this->comment_data_db->delete(array('id'=>$id, 'commentid'=>$commentid));
  213. //删除存储表总数记录,判断总数是否为0,否则不能再删除了。
  214. $this->comment_table_db->edit_total($comment['tableid'], '-=1');
  215. //当评论ID不为空,站点配置了删除的点数,支付模块存在的时候,删除用户的点数。
  216. if (!empty($comment_data['userid']) && !empty($site['del_point']) && module_exists('pay')) {
  217. pc_base::load_app_class('spend', 'pay', 0);
  218. $op_userid = param::get_cookie('userid');
  219. $op_username = param::get_cookie('admin_username');
  220. spend::point($site['del_point'], L('comment_point_del', '', 'comment'), $comment_data['userid'], $comment_data['username'], $op_userid, $op_username);
  221. }
  222. }
  223. //删除审核表中的数据
  224. $this->comment_check_db->delete(array('comment_data_id'=>$id));
  225. $this->msg_code = 0;
  226. return true;
  227. }
  228. /**
  229. *
  230. * 删除评论
  231. * @param string $commentid 评论ID
  232. * @param intval $siteid 站点ID
  233. * @param intval $id 内容ID
  234. * @param intval $catid 栏目ID
  235. */
  236. public function del($commentid, $siteid, $id, $catid) {
  237. if ($commentid != id_encode('content_'.$catid, $id, $siteid)) return false;
  238. //循环评论内容表删除commentid的评论内容
  239. for ($i=1; ;$i++) {
  240. $table = 'comment_data_'.$i; //构建评论内容存储表名
  241. if ($this->comment_data_db->table_exists($table)) { //检查构建的表名是否存在,如果存在执行删除操作
  242. $this->comment_data_db->table_name($i);
  243. $this->comment_data_db->delete(array('commentid'=>$commentid));
  244. } else { //不存在,则退出循环
  245. break;
  246. }
  247. }
  248. $this->comment_db->delete(array('commentid'=>$commentid)); //删除评论主表的内容
  249. return true;
  250. }
  251. /**
  252. *
  253. * 获取报错的详细信息。
  254. */
  255. public function get_error() {
  256. $msg = array('0'=>L('operation_success'),
  257. '1'=>L('coment_class_php_1'),
  258. '2'=>L('coment_class_php_2'),
  259. '3'=>L('coment_class_php_3'),
  260. '4'=>L('coment_class_php_4'),
  261. '5'=>L('coment_class_php_5'),
  262. '6'=>L('coment_class_php_6'),
  263. '7'=>L('coment_class_php_7'),
  264. );
  265. return $msg[$this->msg_code];
  266. }
  267. }