-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimpleblacklist.plugin.php
143 lines (130 loc) · 5.36 KB
/
simpleblacklist.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
<?php
class SimpleBlacklist extends Plugin
{
public function action_init()
{
$this->load_text_domain( 'simpleblacklist' );
}
public function configure()
{
$ui = new FormUI( strtolower( get_class( $this ) ) );
$blacklist = $ui->append( 'textarea', 'blacklist', 'option:simpleblacklist__blacklist', _t( 'Items to blacklist (words, IP addresses, URLs, etc):', 'simpleblacklist' ) );
$blacklist->rows = 8;
$blacklist->class[] = 'resizable';
$frequency = $ui->append('checkbox', 'frequency', 'option:simpleblacklist__frequency', _t( 'Bypass blacklist for frequent commenters:', 'simpleblacklist' ) );
$keep = $ui->append('checkbox', 'keepcomments', 'option:simpleblacklist__keepcomments', _t( 'Keep comments (only mark them as spam):', 'simpleblacklist' ) );
$ui->append('fieldset', 'learning', _t('Learning from spam', 'simpleblacklist'));
$ui->learning->append('checkbox', 'blacklistauthor', 'option:simpleblacklist__blacklistauthor', _t( 'Auto-blacklist author', 'simpleblacklist' ) );
$ui->learning->append('checkbox', 'blacklistmail', 'option:simpleblacklist__blacklistmail', _t( 'Auto-blacklist mail', 'simpleblacklist' ) );
$ui->learning->append('checkbox', 'blacklisturl', 'option:simpleblacklist__blacklisturl', _t( 'Auto-blacklist url', 'simpleblacklist' ) );
$ui->learning->append('checkbox', 'blacklistip', 'option:simpleblacklist__blacklistip', _t( 'Auto-blacklist ip', 'simpleblacklist' ) );
$ui->on_success( array( $this, 'updated_config' ) );
$ui->append( 'submit', 'save', _t( 'Save', 'simpleblacklist' ) );
return $ui;
}
public function updated_config( FormUI $ui )
{
$blacklist = explode( "\n", $ui->blacklist->value );
$blacklist = array_unique( $blacklist );
natsort( $blacklist );
$_POST[$ui->blacklist->field] = implode( "\n", $blacklist );
Session::notice( _t( 'Blacklist saved.' , 'simpleblacklist' ) );
$ui->save();
}
public function filter_comment_insert_allow( $allow, $comment )
{
// Don't discard comments at all when the user disabled that (action_comment_insert_before will mark them as spam then)
if(Options::get( 'simpleblacklist__keepcomments' )) { return true; }
return $this->check_comment( $comment );
}
public function action_comment_insert_before ( $comment )
{
if( $comment->type == Comment::COMMENT && $comment->status != Comment::STATUS_SPAM)
{
if( $this->check_comment( $comment ) === false )
{
$comment->status = Comment::STATUS_SPAM;
EventLog::log( sprintf(_t("Comment by %s automatically marked as spam", 'simpleblacklist'), $comment->name), 'info', 'Simple Blacklist', 'plugin' );
}
}
return $comment;
}
function check_comment( $comment )
{
// don't blacklist logged-in users: they can speak freely
if ( User::identify()->loggedin ) { return true; }
// and if the person has more than 5 comments approved,
// they're likely not a spammer, so don't blacklist them
$bypass = Options::get( 'simpleblacklist__frequency', false );
if ( $bypass ) {
$comments = Comments::get( array( 'email' => $comment->email,
'name' => $comment->name,
'url' => $comment->url,
'status' => Comment::STATUS_APPROVED )
);
if ( $comments->count >= 5 ) {
return true;
}
}
$allow = true;
$reason = "";
$blacklist = explode( "\n", Options::get( 'simpleblacklist__blacklist' ) );
foreach ( $blacklist as $item ) {
$item = trim( strtolower( $item ) );
if ( '' == $item ) { continue; }
// check against the commenter name
if ( false !== strpos( strtolower( $comment->name ), $item ) ) {
$allow = false;
}
// check against the commenter email
if ( false !== strpos( strtolower( $comment->email ), $item ) ) {
$allow = false;
}
// check against the commenter URL
if ( false !== strpos( strtolower( $comment->url ), $item ) ) {
$allow = false;
}
// check against the commenter IP address
if ( (strpos($comment->ip, '.') > 0 ? $comment->ip : long2ip( $comment->ip )) == $item ) {
$allow = false;
}
// now check the body of the comment
if ( false !== strpos( strtolower( $comment->content ), $item ) ) {
$allow = false;
}
if( $allow === false ) {
break;
}
}
return $allow;
}
function action_comment_update_status ($comment, $status, $value)
{
// This is semi-optimal because field changes are always invoked so this applies to every comment and not only to comments marked as spam manually. Doesn't matter but costs resources. There's no other way, so do it.
if( $value == Comment::STATUS_SPAM )
{
$blacklistauthor = Options::get( 'simpleblacklist__blacklistauthor', false );
$blacklistmail = Options::get( 'simpleblacklist__blacklistmail', false );
$blacklisturl = Options::get( 'simpleblacklist__blacklisturl', false );
$blacklistip = Options::get( 'simpleblacklist__blacklistip', false );
$blacklist = Options::get( 'simpleblacklist__blacklist', "");
$newblacklist = explode( "\n", $blacklist );
if ( $blacklistauthor ) {
$newblacklist[] = $comment->author;
}
if ( $blacklistmail ) {
$newblacklist[] = $comment->email;
}
if ( $blacklisturl ) {
$newblacklist[] = $comment->url;
}
if ( $blacklistip ) {
$newblacklist[] = (strpos($comment->ip, '.') > 0 ? $comment->ip : long2ip( $comment->ip ));
}
$newblacklist = array_unique( $newblacklist );
natsort( $newblacklist );
Options::set( 'simpleblacklist__blacklist', implode( "\n", $newblacklist ));
}
}
}
?>