forked from typecho/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
124 lines (112 loc) · 3.91 KB
/
Plugin.php
File metadata and controls
124 lines (112 loc) · 3.91 KB
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
<?php
/**
* 增加评论黑名单(根据 IP 地址过滤)
*
* @package Block Comment
* @author 明城
* @version 0.0.1
* @link http://typecho.org
*/
class BlockComment_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Feedback')->comment = array('BlockComment_Plugin', 'filter');
Typecho_Plugin::factory('Widget_Feedback')->trackback = array('BlockComment_Plugin', 'filter');
Typecho_Plugin::factory('Widget_XmlRpc')->pingback = array('BlockComment_Plugin', 'filter');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @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)
{
$hosts = new Typecho_Widget_Helper_Form_Element_Textarea('hosts', NULL, NULL,
_t('地址列表'), _t('每行单个地址,请仔细匹配以免误封杀'));
$form->addInput($hosts);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 标记评论状态时的插件接口
*
* @access public
* @param array $comment 评论数据的结构体
* @param Typecho_Widget $commentWidget 评论组件
* @param string $status 评论状态
* @return void
*/
public static function mark($comment, $commentWidget, $status)
{
if ('spam' == $comment['status'] && $status != 'spam') {
self::filter($comment, $commentWidget, NULL, 'submit-ham');
} else if ('spam' != $comment['status'] && $status == 'spam') {
self::filter($comment, $commentWidget, NULL, 'submit-spam');
}
}
/**
* 评论过滤器
*
* @access public
* @param array $comment 评论结构
* @param Typecho_Widget $post 被评论的文章
* @param array $result 返回的结果上下文
* @param string $api api地址
* @return void
*/
public static function filter($comment, $post, $result, $api = 'comment-check')
{
$comment = empty($result) ? $comment : $result;
$options = Typecho_Widget::widget('Widget_Options');
$hosts = $options->plugin('BlockComment')->hosts;
$data = array(
'blog' => $options->siteUrl,
'user_ip' => $comment['ip'],
'user_agent' => $comment['agent'],
'referrer' => Typecho_Request::getInstance()->getReferer(),
'permalink' => $post->permalink,
'comment_type' => $comment['type'],
'comment_author' => $comment['author'],
'comment_author_email' => $comment['mail'],
'comment_author_url' => $comment['url'],
'comment_content' => $comment['text']
);
foreach(split("\n", $hosts) as $key => $value){
$value = trim($value);
if (strlen($value)) {
$regex = sprintf("/^%s/i", preg_quote($value));
// 如果提交者符合指定的 IP,则扔进垃圾评论中
if (preg_match($regex, $data['user_ip'])) {
$comment['status'] = 'spam';
break;
}
}
}
return $comment;
}
}