-
Notifications
You must be signed in to change notification settings - Fork 2
/
Action.php
208 lines (183 loc) · 6.99 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* Typecho Ajax 评论 操作类
*
* @package AjaxComment
* @author 情留メ蚊子
* @version 1.0.0.2
* @link http://www.94qing.com/
*/
class AjaxComment_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* 发表评论
*
* @access public
* @return void
*/
public function post_comment()
{
if ('POST' != $_SERVER['REQUEST_METHOD']) {
header('Allow: POST');
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type: text/plain');
exit;
}
$comment_post_ID = $this->request->filter('int')->get('comment_post_ID');
$comment_parent = $this->request->filter('int')->get('parent');
if ($comment_parent == 0) {
$comment_parent = $this->request->filter('int')->get('comment_parent');
}
$post = Typecho_Widget::widget('Widget_Archive', 'type=single', 'cid=' . $comment_post_ID, false);
if (!isset($post) || !($post instanceof Widget_Archive) || !$post->have() || !$post->is('single')) {
$this->err('文章不存在');
}
if (!$post->allow('comment')) {
$this->err(_t('对不起,此内容的评论功能已经关闭!'));
}
$options = Typecho_Widget::widget('Widget_Options');
$settings = $options->plugin('AjaxComment');
$user = Typecho_Widget::widget('Widget_User');
/** 检查来源 */
if ($options->commentsCheckReferer) {
$referer = $this->request->getReferer();
if (empty($referer)) {
$this->err(_t('评论来源页错误'));
}
$refererPart = parse_url($referer);
$currentPart = parse_url($post->permalink);
if ($refererPart['host'] != $currentPart['host'] || 0 !== strpos($refererPart['path'], $currentPart['path'])) {
//自定义首页支持
if ('page:' . $post->cid == $options->frontPage) {
$currentPart = parse_url(rtrim($options->siteUrl, '/') . '/');
if ($refererPart['host'] != $currentPart['host'] || 0 !== strpos($refererPart['path'], $currentPart['path'])) {
$this->err(_t('评论来源页错误'));
}
} else {
$this->err(_t('评论来源页错误'));
}
}
}
$comment = array();
$comment['permalink'] = $post->pathinfo;
$comment['type'] = 'comment';
$comment['text'] = $this->request->text;
$comment['parent'] = $comment_parent;
if (!$user->hasLogin()) {
$comment['author'] = $this->request->filter('trim')->author;
$comment['mail'] = $this->request->filter('trim')->mail;
$comment['url'] = $this->request->filter('trim')->url;
/** 修正用户提交的url */
if (!empty($comment['url'])) {
$urlParams = parse_url($comment['url']);
if (!isset($urlParams['scheme'])) {
$comment['url'] = 'http://' . $comment['url'];
}
}
} else {
$comment['author'] = $user->screenName;
$comment['mail'] = $user->mail;
$comment['url'] = $user->url;
}
if ($settings->comment_needchinese) {
if (!preg_match('/[一-龥]/u', $comment['text'])) {
$this->err('评论内容中必须含有中文');
}
}
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array($this, 'Widget_Feedback_finishComment');
try {
$commentWidget = Typecho_Widget::widget('Widget_Feedback', 'checkReferer=false', $comment, false);
$commentWidget->action();
} catch (Exception $e) {
$this->err($e->getMessage());
}
}
/**
* 输出错误
*
* @param $errmsg
*/
public function err($errmsg)
{
header('HTTP/1.1 405 Method Not Allowed');
echo $errmsg;
exit;
}
/**
* 评论后续处理
*
* @access public
* @param object $comments 评论信息
* @return void
*/
public function Widget_Feedback_finishComment($comments)
{
$options = Typecho_Widget::widget('Widget_Options');
$options->commentsPageBreak = 0;
$_themeDir = rtrim($options->themeFile($options->theme), '/') . '/';
$ajaxfile = $_themeDir . 'comments.php';
if (file_exists($ajaxfile)) {
$file_content = file_get_contents($ajaxfile);
$check_code = "return true; ?>";
$file_content = $check_code . $file_content . "<?php ";
@eval($file_content);
}
$parameter = array('parentId' => $comments->cid);
$comments_Archive = Typecho_Widget::widget('Widget_Comments_Archive', $parameter);
if (!$comments_Archive->have()) {
$this->err('获取评论出错');
}
$db = Typecho_Db:: get();
$parentids = array();
$parentids[] = $comments->coid;
$parent = $comments->parent;
while ($parent) {
$parentids[] = $parent;
$parentRows = $db->fetchRow($db->select('parent')
->from('table.comments')
->where('coid = ? AND status = ?', $parent, 'approved')
->limit(1));
$parent = $parentRows['parent'];
}
$this->getlistComments($comments, $comments_Archive, $parentids);
return;
}
private function getlistComments($comments, $comments_Archive, $parentids, $lv = 0)
{
while ($comments_Archive->next()) {
if ($comments_Archive->coid == $comments->coid) {
/*
if ($lv == 0 && count($comments_Archive->stack) == 1) {
$singleCommentOptions = '';
} else {
$singleCommentOptions = 'before=&after=';
}
*/
$singleCommentOptions = 'before=&after=';
$comments_Archive->length = 1;
$comments_Archive->stack = array($comments->coid => $comments_Archive->row);
$comments_Archive->listComments($singleCommentOptions);
return true;
} elseif (in_array($comments_Archive->coid, $parentids)) {
$comments_Archive->stack = $comments_Archive->children;
if ($this->getlistComments($comments, $comments_Archive, $parentids, $lv++)) {
return true;
}
}
}
return false;
}
/**
* 绑定动作
*
* @access public
* @return void
*/
public function action()
{
$this->on($this->request->is('do=post'))->post_comment();
}
}