This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspam-prevention.php
156 lines (136 loc) · 5.77 KB
/
spam-prevention.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
<?php
/**
* This file will introduce a basic spam filter
* mm_spam v1
*/
function mm_spam_stop_forum_spam_api( $args = array() ) {
$defaults = array(
'ip' => ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? $_SERVER['REMOTE_ADDR'] : '',
'email' => '',
'username' => '',
);
$url = 'https://www.stopforumspam.com/api?';
$args = wp_parse_args( $args, $defaults );
$args['f'] = 'json';
$args['confidence'] = true;
$args = array_filter( $args );
$query = $url . http_build_query( $args );
$key = md5( $query );
if ( false === ( $transient = get_transient( 'mm_spam_' . $key ) ) ) {
$result = wp_remote_get( $query );
if ( ! is_wp_error( $result ) ) {
if ( strlen( $result['body'] ) < 10 || 200 != $result['response']['code'] ) {
return false;
}
if ( $data = json_decode( $result['body'] ) ) {
// it is json. continue
if ( $data->success != 1 ) {
return false;
}
if ( isset( $data->ip ) || isset( $data->email ) || isset( $data->username ) ) {
$blocked = false;
if ( isset( $data->ip->confidence ) && $data->ip->confidence > get_option( 'mm_confidence_ip', 75 ) ) { $blocked = 'ip'; }
if ( isset( $data->username->confidence ) && $data->username->confidence > get_option( 'mm_confidence_username', 95 ) ) { $blocked = 'username'; }
if ( isset( $data->email->confidence ) && $data->email->confidence > get_option( 'mm_confidence_email', 75 ) ) { $blocked = 'email'; }
if ( $blocked ) {
$event = array(
't' => 'event',
'ec' => 'scheduled',
'ea' => 'spam_blocked_' . $blocked,
'el' => 0,
);
if ( isset( $events['weekly'][ $event['ea'] ] ) ) {
$events['weekly'][ $event['ea'] ]['el']++;
} else {
$events['weekly'][ $event['ea'] ] = $event;
}
update_option( 'mm_cron', $events );
set_transient( 'mm_spam_' . $key, 'yes', DAY_IN_SECONDS );
return true;
} else {
set_transient( 'mm_spam_' . $key, 'no', DAY_IN_SECONDS );
}
}
}
}
} else {
return ( 'yes' == $transient ) ? true : false;
}
return false;
}
//check ip on login pageload
function mm_spam_check_ip_init() {
if ( mm_spam_stop_forum_spam_api() ) {
wp_die( '<center>Your IP is on a <a href="http://stopforumspam.com">Spam Blacklist</a>.</center>', 'MOJO Spam Prevention' );
}
}
add_action( 'login_init', 'mm_spam_check_ip_init' );
function mm_spam_check_comment( $approved, $comment ) {
if ( ! empty( $comment['user_ID'] ) && get_user_by( 'id', $comment['user_ID'] ) ) {
return $approved;
}
$check = array( 'ip' => $comment['comment_author_IP'] );
if ( isset( $comment['comment_author_email'] ) && is_email( $comment['comment_author_email'] ) ) {
$check['email'] = $comment['comment_author_email'];
}
if ( isset( $comment['comment_author'] ) ) {
$check['username'] = $comment['comment_author'];
}
return ( mm_spam_stop_forum_spam_api( $check ) ) ? 'spam' : $approved;
}
add_action( 'pre_comment_approved' , 'mm_spam_check_comment', 99, 2 );
function mm_spam_add_blacklist_words( $words ) {
if ( isset( $_SERVER['PHP_SELF'] ) && strpos( $_SERVER['PHP_SELF'], '/options' ) || isset( $_SERVER['SCRIPT_NAME'] ) && strpos( $_SERVER['SCRIPT_NAME'], '/options' ) ) {
return $words;
}
$words = explode( "\n", $words );
$blocked_words = array( 'byob','poze','bdsm','paxil','cialis','incest','ambien','adipex','shemale','meridia','cumshot','adderall','hair-loss','bllogspot','hydrocodone','discreetordering','aceteminophen','augmentation','enhancement','phentermine','doxycycline','citalopram','cephalaxin','vicoprofen','lorazepam','oxycontin','oxycodone','percocet','tramadol','cymbalta','lesbian','lexapro','valtrex','titties','meridia','levitra','vicodin','ephedra','lipitor','breast','cyclen','viagra','valium','hqtube','ultram','clomid','vioxx','zolus','pussy','porno','xanax','penis','porn','dick','cock','tits','fuck','shit','gdf','gds' );
$words = array_merge( $words, $blocked_words );
$words = array_unique( $words );
$words = implode( "\n", $words );
return $words;
}
add_filter( 'option_blacklist_keys', 'mm_spam_add_blacklist_words' );
function mm_spam_add_moderation_words( $words ) {
if ( isset( $_SERVER['PHP_SELF'] ) && strpos( $_SERVER['PHP_SELF'], '/options' ) || isset( $_SERVER['SCRIPT_NAME'] ) && strpos( $_SERVER['SCRIPT_NAME'], '/options' ) ) {
return $words;
}
$words = explode( "\n", $words );
$moderated_words = array( 'д','и','ж','Ч','Б','[url=','[/url]','naked','sex','bitch','soma','gay','nude' );
$words = array_merge( $words, $moderated_words );
$words = array_unique( $words );
$words = implode( "\n", $words );
return $words;
}
add_filter( 'option_moderation_keys', 'mm_spam_add_moderation_words' );
function mm_spam_process_hidden_field( $data ) {
if ( is_user_logged_in() ) {
return $data;
}
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'comments' ) ) {
return $data;
}
$spam_key = md5( $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] );
if ( ! isset( $_POST['js-spam-prevention'] ) || $_POST['js-spam-prevention'] !== $spam_key ) {
if ( current_filter() !== 'registration_errors' ) {
die( 'Blocked as suspected bot.' );
} else {
$data->add( 'bot_error', 'Suspected bot.' );
}
}
return $data;
}
add_filter( 'preprocess_comment', 'mm_spam_process_hidden_field' );
add_filter( 'registration_errors', 'mm_spam_process_hidden_field' );
function mm_spam_add_hidden_field() {
$spam_key = md5( $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] );
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$( '.comment-form, #registerform' ).append( '<input type="hidden" name="js-spam-prevention" value="<?php echo $spam_key; ?>"/>' );
} );
</script>
<?php
}
add_action( 'comment_form_after', 'mm_spam_add_hidden_field', 20 );
add_action( 'register_form', 'mm_spam_add_hidden_field' );