forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
314 lines (284 loc) · 11.1 KB
/
Plugin.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 评论过滤器 【<a href="https://github.com/typecho-fans/plugins" target="_blank">TF</a>社区维护版】
*
* @package CommentFilter
* @author jrotty,ghostry,Hanny
* @version 1.2.1
* @link https://github.com/typecho-fans/plugins/tree/master/CommentFilter
*
* version 1.2.1 at 2020-06-27[typecho-fans合并2012-12-31 ghostry修改版]
* 增加首次评论过滤,评论者可以在评论底部看到自己的未审核评论
*
* version 1.2.0 at 2017-10-10[非原作者更新修改,jrotty魔改更新]
* 增加评论者昵称/超链接过滤功能
*
* 历史版本
* version 1.1.0 at 2014-01-04
* 增加机器评论过滤
* version 1.0.2 at 2010-05-16
* 修正发表评论成功后,评论内容Cookie不清空的Bug
* version 1.0.1 at 2009-11-29
* 增加IP段过滤功能
* version 1.0.0 at 2009-11-14
* 实现评论内容按屏蔽词过滤功能
* 实现过滤非中文评论功能
*/
class CommentFilter_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Feedback')->comment = array('CommentFilter_Plugin', 'filter');
Typecho_Plugin::factory('Widget_Archive')->header = array('CommentFilter_Plugin', 'add_filter_spam_input');
return _t('评论过滤器启用成功,请配置需要过滤的内容');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$opt_spam = new Typecho_Widget_Helper_Form_Element_Radio('opt_spam', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "none",
_t('屏蔽机器人评论'), "如果为机器人评论,将执行该操作。如果需要开启该过滤功能,请尝试进行评论测试,以免不同模板造成误判。");
$form->addInput($opt_spam);
$opt_ip = new Typecho_Widget_Helper_Form_Element_Radio('opt_ip', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "none",
_t('屏蔽IP操作'), "如果评论发布者的IP在屏蔽IP段,将执行该操作");
$form->addInput($opt_ip);
$words_ip = new Typecho_Widget_Helper_Form_Element_Textarea('words_ip', NULL, "0.0.0.0",
_t('屏蔽IP'), _t('多条IP请用换行符隔开<br />支持用*号匹配IP段,如:192.168.*.*'));
$form->addInput($words_ip);
$opt_nocn = new Typecho_Widget_Helper_Form_Element_Radio('opt_nocn', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "none",
_t('非中文评论操作'), "如果评论中不包含中文,则强行按该操作执行");
$form->addInput($opt_nocn);
$opt_nopl = new Typecho_Widget_Helper_Form_Element_Radio('opt_nopl', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "none",
_t('首次评论操作'), "如果评论人没有评论过,则强行按该操作执行");
$form->addInput($opt_nopl);
$opt_ban = new Typecho_Widget_Helper_Form_Element_Radio('opt_ban', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "abandon",
_t('禁止词汇操作'), "如果评论中包含禁止词汇列表中的词汇,将执行该操作");
$form->addInput($opt_ban);
$words_ban = new Typecho_Widget_Helper_Form_Element_Textarea('words_ban', NULL, "fuck\n操你妈\n[url\n[/url]",
_t('禁止词汇'), _t('多条词汇请用换行符隔开'));
$form->addInput($words_ban);
$opt_chk = new Typecho_Widget_Helper_Form_Element_Radio('opt_chk', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "waiting",
_t('敏感词汇操作'), "如果评论中包含敏感词汇列表中的词汇,将执行该操作");
$form->addInput($opt_chk);
$words_chk = new Typecho_Widget_Helper_Form_Element_Textarea('words_chk', NULL, "http://",
_t('敏感词汇'), _t('多条词汇请用换行符隔开<br />注意:如果词汇同时出现于禁止词汇,则执行禁止词汇操作'));
$form->addInput($words_chk);
$opt_author = new Typecho_Widget_Helper_Form_Element_Radio('opt_author', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "spam",
_t('关键昵称操作'), "如果评论中包含关键昵称词汇列表中的词汇,将执行该操作");
$form->addInput($opt_author);
$words_author = new Typecho_Widget_Helper_Form_Element_Textarea('words_author', NULL, "澳门银座\n自动化软件\n量化交易",
_t('关键昵称词汇'), _t('多条词汇请用换行符隔开'));
$form->addInput($words_author);
$opt_url = new Typecho_Widget_Helper_Form_Element_Radio('opt_url', array("none" => "无动作", "waiting" => "标记为待审核", "spam" => "标记为垃圾", "abandon" => "评论失败"), "spam",
_t('垃圾链接过滤操作'), "如果评论中包含垃圾链接列表中字符串,将执行该操作");
$form->addInput($opt_url);
$words_url = new Typecho_Widget_Helper_Form_Element_Textarea('words_url', NULL, "www.vps521.cn",
_t('垃圾链接'), _t('多条词汇请用换行符隔开,链接格式请参考上边输入框默认的链接'));
$form->addInput($words_url);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 评论过滤器
*
*/
public static function filter($comment, $post)
{
$options = Typecho_Widget::widget('Widget_Options');
$filter_set = $options->plugin('CommentFilter');
$opt = "none";
$error = "";
//机器评论处理
if ($opt == "none" && $filter_set->opt_spam != "none") {
if ($_POST['filter_spam'] != '48616E6E79') {
$error = "请勿使用第三方工具进行评论";
$opt = $filter_set->opt_spam;
}
}
//屏蔽IP段处理
if ($opt == "none" && $filter_set->opt_ip != "none") {
if (CommentFilter_Plugin::check_ip($filter_set->words_ip, $comment['ip'])) {
$error = "评论发布者的IP已被管理员屏蔽";
$opt = $filter_set->opt_ip;
}
}
//纯中文评论处理
if ($opt == "none" && $filter_set->opt_nocn != "none") {
if (preg_match("/[\x{4e00}-\x{9fa5}]/u", $comment['text']) == 0) {
$error = "评论内容请不少于一个中文汉字";
$opt = $filter_set->opt_nocn;
}
}
//首次评论操作
if($opt == "none" && $filter_set->opt_nopl != "none"){
if($comment['mail']){
$db = Typecho_Db::get();
$select=$db->select('mail')
->from('table.comments')
->where('mail = ?', $comment['mail']);
$result = $db->query($select);
$row = $db->fetchRow($result);
if(!$row['mail']){
$opt = $filter_set->opt_nopl;
}
}
}
//检查禁止词汇
if ($opt == "none" && $filter_set->opt_ban != "none") {
if (CommentFilter_Plugin::check_in($filter_set->words_ban, $comment['text'])) {
$error = "评论内容中包含禁止词汇";
$opt = $filter_set->opt_ban;
}
}
//检查敏感词汇
if ($opt == "none" && $filter_set->opt_chk != "none") {
if (CommentFilter_Plugin::check_in($filter_set->words_chk, $comment['text'])) {
$error = "评论内容中包含敏感词汇";
$opt = $filter_set->opt_chk;
}
}
//检查关键昵称词汇
if ($opt == "none" && $filter_set->opt_author != "none") {
if (CommentFilter_Plugin::check_in($filter_set->words_author, $comment['author'])) {
$error = "该类型昵称已被禁止评论";
$opt = $filter_set->opt_author;
}
}
//检查评论者链接
if ($opt == "none" && $filter_set->opt_url != "none") {
if (CommentFilter_Plugin::check_in($filter_set->words_url, $comment['url'])) {
$error = "该类型评论者超链接被禁止评论";
$opt = $filter_set->opt_url;
}
}
//执行操作
if ($opt == "abandon") {
Typecho_Cookie::set('__typecho_remember_text', $comment['text']);
throw new Typecho_Widget_Exception($error);
}
else if ($opt == "spam") {
$comment['status'] = 'spam';
}
else if ($opt == "waiting") {
$comment['status'] = 'waiting';
}
$_SESSION['comment']=$comment;
Typecho_Cookie::delete('__typecho_remember_text');
return $comment;
}
/**
* 检查$str中是否含有$words_str中的词汇
*
*/
private static function check_in($words_str, $str)
{
$words = explode("\n", $words_str);
if (empty($words)) {
return false;
}
foreach ($words as $word) {
if (false !== strpos($str, trim($word))) {
return true;
}
}
return false;
}
/**
* 检查$ip中是否在$words_ip的IP段中
*
*/
private static function check_ip($words_ip, $ip)
{
$words = explode("\n", $words_ip);
if (empty($words)) {
return false;
}
foreach ($words as $word) {
$word = trim($word);
if (false !== strpos($word, '*')) {
$word = "/^".str_replace('*', '\d{1,3}', $word)."$/";
if (preg_match($word, $ip)) {
return true;
}
} else {
if (false !== strpos($ip, $word)) {
return true;
}
}
}
return false;
}
/**
* 在表单中增加 filter_spam 隐藏域
*
*/
public static function add_filter_spam_input($header, $archive)
{
$options = Typecho_Widget::widget('Widget_Options');
$filter_set = $options->plugin('CommentFilter');
if ($filter_set->opt_spam != "none" && $archive->is('single') && $archive->allow('comment')) {
echo '<script type="text/javascript">
function get_form(input) {
var node = input;
while (node) {
node = node.parentNode;
if (node.nodeName.toLowerCase() == "form") {
return node;
}
}
return null;
};
window.onload = function() {
var inputs = document.getElementsByTagName("textarea");
var i, input_author;
input_author = null;
for (i=0; i<inputs.length; i++) {
if (inputs[i].name.toLowerCase() == "text") {
input_author = inputs[i];
break;
}
}
var form_comment = get_form(input_author);
if (form_comment) {
var input_hd = document.createElement("input");
input_hd.type = "hidden";
input_hd.name = "filter_spam";
input_hd.value = "48616E6E79";
form_comment.appendChild(input_hd);
} else {
alert("find input author error!");
}
}
</script>
';
}
}
}