📄 正在查看:twcms/control/comment_control.class.php
大小:5,041 字节 · 修改:2014-01-23 01:42:52 · 行数:150
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class comment_control extends control{
10 public $_cfg = array(); // 全站参数
11 public $_var = array(); // 内容页参数
12
13 public function index() {
14 // hook comment_control_index_before.php
15
16 $_GET['cid'] = (int)R('cid');
17 $_GET['id'] = (int)R('id');
18 $this->_var = $this->category->get_cache($_GET['cid']);
19 empty($this->_var) && core::error404();
20
21 $this->_cfg = $this->runtime->xget();
22
23 // 初始模型表名
24 $this->cms_content->table = 'cms_'.$this->_var['table'];
25
26 // 读取内容
27 $_show = $this->cms_content->read($_GET['id']);
28 if(empty($_show['cid']) || $_show['cid'] != $_GET['cid']) core::error404();
29
30 // SEO 相关
31 $this->_cfg['titles'] = $_show['title'];
32 $this->_cfg['seo_keywords'] = empty($_show['seo_keywords']) ? $_show['title'] : $_show['seo_keywords'];
33 $this->_cfg['seo_description'] = empty($_show['seo_description']) ? $_show['intro']: $_show['seo_description'];
34
35 $this->assign('tw', $this->_cfg);
36 $this->assign('tw_var', $this->_var);
37
38 $GLOBALS['run'] = &$this;
39 $GLOBALS['_show'] = &$_show;
40
41 // hook comment_control_index_after.php
42
43 $_ENV['_theme'] = &$this->_cfg['theme'];
44 $this->display('comment.htm');
45 }
46
47 // 发表评论
48 public function post() {
49 // hook comment_control_post_before.php
50
51 $cid = (int) R('cid', 'P');
52 $id = (int) R('id', 'P');
53 $content = htmlspecialchars(trim(R('content', 'P')));
54 $author = htmlspecialchars(trim(R('author', 'P')));
55 $ip = ip2long(ip());
56
57 if(empty($cid) || empty($id)) $this->message(0, '参数不完整!');
58 empty($author) && $this->message(0, '昵称不能为空!');
59 strlen($author)>20 && $this->message(0, '昵称太长了!');
60 empty($content) && $this->message(0, '评论内容不能为空!');
61 strlen($content)>3000 && $this->message(0, '评论内容太长了!');
62
63 // 关闭全站评论
64 $_cfg = $this->runtime->xget();
65 !empty($_cfg['dis_comment']) && $this->message(0, '已关闭全站评论,无法发表评论!');
66
67 $cates = $this->category->get_cache($cid);
68 empty($cates) && $this->message(0, '分类ID不正确!');
69
70 $this->cms_content->table = 'cms_'.$cates['table'];
71 $data = $this->cms_content->read($id);
72
73 $data['iscomment'] && $this->message(0, '不允许发表评论!');
74
75 // hook comment_control_post_create_before.php
76
77 $this->cms_content_comment->table = 'cms_'.$cates['table'].'_comment';
78 $maxid = $this->cms_content_comment->create(array(
79 'id' => $id,
80 'uid' => 0,
81 'author' => $author,
82 'content' => $content,
83 'ip' => $ip,
84 'dateline' => $_ENV['_time'],
85 ));
86 if(!$maxid) {
87 $this->message(0, '写入评论表出错!');
88 }
89
90 $data['comments']++;
91 if(!$this->cms_content->update($data)) {
92 $this->message(0, '写入内容表出错!');
93 }
94
95 $this->cms_content_comment_sort->table = 'cms_'.$cates['table'].'_comment_sort';
96 $ret = $this->cms_content_comment_sort->set($id, array(
97 'cid' => $cid,
98 'comments' => $data['comments'],
99 'lastdate' => $_ENV['_time'],
100 ));
101 if(!$ret) {
102 $this->message(0, '写入评论排序表出错!');
103 }
104
105 // hook comment_control_post_after.php
106
107 $this->message(1, '发表评论成功!');
108 }
109
110 // 获取评论JSON
111 public function json() {
112 $cid = (int)R('cid');
113 $id = (int)R('id');
114
115 $commentid = (int)R('commentid');
116 $orderway = isset($_GET['orderway']) && $_GET['orderway'] == 1 ? 1 : -1;
117 $pagenum = empty($_GET['pagenum']) ? 20 : max(1, (int)$_GET['pagenum']);
118 $dateformat = empty($_GET['dateformat']) ? 'Y-m-d H:i:s' : base64_decode($_GET['dateformat']);
119 $humandate = isset($_GET['humandate']) ? ($_GET['humandate'] == 1 ? 1 : 0) : 1;
120
121 if(empty($cid) || empty($id) || empty($commentid)) $this->message(0, '参数不完整!');
122
123 $cates = $this->category->get_cache($cid);
124 empty($cates) && $this->message(0, '分类ID不正确!');
125
126 // 获取评论列表
127 $key = $orderway == 1 ? '>' : '<';
128 $where = array('id' => $id, 'commentid' => array($key => $commentid));
129 $this->cms_content_comment->table = 'cms_'.$cates['table'].'_comment';
130 $ret = array();
131 $ret['list_arr'] = $this->cms_content_comment->find_fetch($where, array('commentid' => $orderway), 0, $pagenum);
132 foreach($ret['list_arr'] as &$v) {
133 $this->cms_content_comment->format($v, $dateformat, $humandate);
134 }
135
136 $end_arr = end($ret['list_arr']);
137 $commentid = $end_arr['commentid'];
138 $orderway = max(0, $orderway);
139 $dateformat = base64_encode($dateformat);
140 $_cfg = $this->runtime->xget();
141 $ret['next_url'] = $_cfg['webdir']."index.php?comment-json-cid-$cid-id-$id-commentid-$commentid-orderway-$orderway-pagenum-$pagenum-dateformat-$dateformat-humandate-$humandate-ajax-1";
142 $ret['isnext'] = count($ret['list_arr']) < $pagenum ? 0 : 1;
143
144 echo json_encode($ret);
145 exit;
146 }
147
148 // hook comment_control_after.php
149}
150