WP-Ban - Version 1.69

Version Description

N/A

Download this release

Release Info

Developer GamerZ
Plugin Icon WP-Ban
Version 1.69
Comparing to
See all releases

Code changes from version 1.68 to 1.69

Files changed (4) hide show
  1. ban-options.php +11 -6
  2. readme.txt +7 -3
  3. wp-ban.php +224 -204
  4. wp-ban.pot +0 -325
ban-options.php CHANGED
@@ -26,8 +26,8 @@ if( ! empty( $_POST['Submit'] ) ) {
26
  $banned_exclude_ips_post = ! empty( $_POST['banned_exclude_ips'] ) ? explode( "\n", trim( $_POST['banned_exclude_ips'] ) ) : array();
27
  $banned_message = ! empty( $_POST['banned_template_message'] ) ? trim( $_POST['banned_template_message'] ) : '';
28
 
 
29
  if(!empty($banned_ips_post)) {
30
- $banned_ips = array();
31
  foreach($banned_ips_post as $banned_ip) {
32
  if($admin_login == 'admin' && ($banned_ip == ban_get_ip() || is_admin_ip($banned_ip))) {
33
  $text .= '<p style="color: blue;">'.sprintf(__('This IP \'%s\' Belongs To The Admin And Will Not Be Added To Ban List', 'wp-ban'),$banned_ip).'</p>';
@@ -36,8 +36,9 @@ if( ! empty( $_POST['Submit'] ) ) {
36
  }
37
  }
38
  }
 
 
39
  if( ! empty( $banned_ips_range_post ) ) {
40
- $banned_ips_range = array();
41
  foreach( $banned_ips_range_post as $banned_ip_range ) {
42
  $range = explode( '-', $banned_ip_range );
43
  if( sizeof( $range ) === 2 ) {
@@ -51,8 +52,9 @@ if( ! empty( $_POST['Submit'] ) ) {
51
  }
52
  }
53
  }
 
 
54
  if(!empty($banned_hosts_post)) {
55
- $banned_hosts = array();
56
  foreach($banned_hosts_post as $banned_host) {
57
  if($admin_login == 'admin' && ($banned_host == @gethostbyaddr(ban_get_ip()) || is_admin_hostname($banned_host))) {
58
  $text .= '<p style="color: blue;">'.sprintf(__('This Hostname \'%s\' Belongs To The Admin And Will Not Be Added To Ban List', 'wp-ban'), $banned_host).'</p>';
@@ -61,8 +63,9 @@ if( ! empty( $_POST['Submit'] ) ) {
61
  }
62
  }
63
  }
 
 
64
  if(!empty($banned_referers_post)) {
65
- $banned_referers = array();
66
  foreach($banned_referers_post as $banned_referer) {
67
  if(is_admin_referer($banned_referer)) {
68
  $text .= '<p style="color: blue;">'.sprintf(__('This Referer \'%s\' Belongs To This Site And Will Not Be Added To Ban List', 'wp-ban'), $banned_referer).'</p>';
@@ -71,8 +74,9 @@ if( ! empty( $_POST['Submit'] ) ) {
71
  }
72
  }
73
  }
 
 
74
  if(!empty($banned_user_agents_post)) {
75
- $banned_user_agents = array();
76
  foreach($banned_user_agents_post as $banned_user_agent) {
77
  if(is_admin_user_agent($banned_user_agent)) {
78
  $text .= '<p style="color: blue;">'.sprintf(__('This User Agent \'%s\' Is Used By The Current Admin And Will Not Be Added To Ban List', 'wp-ban'), $banned_user_agent).'</p>';
@@ -81,8 +85,9 @@ if( ! empty( $_POST['Submit'] ) ) {
81
  }
82
  }
83
  }
 
 
84
  if(!empty($banned_exclude_ips_post)) {
85
- $banned_exclude_ips = array();
86
  foreach($banned_exclude_ips_post as $banned_exclude_ip) {
87
  $banned_exclude_ips[] = trim($banned_exclude_ip);
88
  }
26
  $banned_exclude_ips_post = ! empty( $_POST['banned_exclude_ips'] ) ? explode( "\n", trim( $_POST['banned_exclude_ips'] ) ) : array();
27
  $banned_message = ! empty( $_POST['banned_template_message'] ) ? trim( $_POST['banned_template_message'] ) : '';
28
 
29
+ $banned_ips = array();
30
  if(!empty($banned_ips_post)) {
 
31
  foreach($banned_ips_post as $banned_ip) {
32
  if($admin_login == 'admin' && ($banned_ip == ban_get_ip() || is_admin_ip($banned_ip))) {
33
  $text .= '<p style="color: blue;">'.sprintf(__('This IP \'%s\' Belongs To The Admin And Will Not Be Added To Ban List', 'wp-ban'),$banned_ip).'</p>';
36
  }
37
  }
38
  }
39
+
40
+ $banned_ips_range = array();
41
  if( ! empty( $banned_ips_range_post ) ) {
 
42
  foreach( $banned_ips_range_post as $banned_ip_range ) {
43
  $range = explode( '-', $banned_ip_range );
44
  if( sizeof( $range ) === 2 ) {
52
  }
53
  }
54
  }
55
+
56
+ $banned_hosts = array();
57
  if(!empty($banned_hosts_post)) {
 
58
  foreach($banned_hosts_post as $banned_host) {
59
  if($admin_login == 'admin' && ($banned_host == @gethostbyaddr(ban_get_ip()) || is_admin_hostname($banned_host))) {
60
  $text .= '<p style="color: blue;">'.sprintf(__('This Hostname \'%s\' Belongs To The Admin And Will Not Be Added To Ban List', 'wp-ban'), $banned_host).'</p>';
63
  }
64
  }
65
  }
66
+
67
+ $banned_referers = array();
68
  if(!empty($banned_referers_post)) {
 
69
  foreach($banned_referers_post as $banned_referer) {
70
  if(is_admin_referer($banned_referer)) {
71
  $text .= '<p style="color: blue;">'.sprintf(__('This Referer \'%s\' Belongs To This Site And Will Not Be Added To Ban List', 'wp-ban'), $banned_referer).'</p>';
74
  }
75
  }
76
  }
77
+
78
+ $banned_user_agents = array();
79
  if(!empty($banned_user_agents_post)) {
 
80
  foreach($banned_user_agents_post as $banned_user_agent) {
81
  if(is_admin_user_agent($banned_user_agent)) {
82
  $text .= '<p style="color: blue;">'.sprintf(__('This User Agent \'%s\' Is Used By The Current Admin And Will Not Be Added To Ban List', 'wp-ban'), $banned_user_agent).'</p>';
85
  }
86
  }
87
  }
88
+
89
+ $banned_exclude_ips = array();
90
  if(!empty($banned_exclude_ips_post)) {
 
91
  foreach($banned_exclude_ips_post as $banned_exclude_ip) {
92
  $banned_exclude_ips[] = trim($banned_exclude_ip);
93
  }
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === WP-Ban ===
2
  Contributors: GamerZ
3
- Donate link: http://lesterchan.net/site/donation/
4
  Tags: banned, ban, deny, denied, permission, ip, hostname, host, spam, bots, bot, exclude, referrer, url, referral, range
5
  Requires at least: 4.3
6
- Tested up to: 4.5
7
- Stable tag: 1.68
8
 
9
  Ban users by IP, IP Range, host name, user agent and referrer url from visiting your WordPress's blog.
10
 
@@ -27,6 +27,10 @@ It will display a custom ban message when the banned IP, IP range, host name or
27
  * I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
28
 
29
  == Changelog ==
 
 
 
 
30
  = Version 1.68 =
31
  * NEW: Use translate.wordpress.org to translate the plugin
32
  * NEW: Use HTML DOCTYPE
1
  === WP-Ban ===
2
  Contributors: GamerZ
3
+ Donate link: https://lesterchan.net/site/donation/
4
  Tags: banned, ban, deny, denied, permission, ip, hostname, host, spam, bots, bot, exclude, referrer, url, referral, range
5
  Requires at least: 4.3
6
+ Tested up to: 4.7
7
+ Stable tag: 1.69
8
 
9
  Ban users by IP, IP Range, host name, user agent and referrer url from visiting your WordPress's blog.
10
 
27
  * I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
28
 
29
  == Changelog ==
30
+ = Version 1.69 =
31
+ * NEW: Bump WordPress 4.7
32
+ * FIXED: Notices
33
+
34
  = Version 1.68 =
35
  * NEW: Use translate.wordpress.org to translate the plugin
36
  * NEW: Use HTML DOCTYPE
wp-ban.php CHANGED
@@ -1,252 +1,272 @@
1
  <?php
2
  /*
3
  Plugin Name: WP-Ban
4
- Plugin URI: http://lesterchan.net/portfolio/programming/php/
5
  Description: Ban users by IP, IP Range, host name, user agent and referer url from visiting your WordPress's blog. It will display a custom ban message when the banned IP, IP range, host name, user agent or referer url tries to visit you blog. You can also exclude certain IPs from being banned. There will be statistics recordered on how many times they attemp to visit your blog. It allows wildcard matching too.
6
- Version: 1.68
7
  Author: Lester 'GaMerZ' Chan
8
- Author URI: http://lesterchan.net
9
  Text Domain: wp-ban
10
  */
11
 
12
 
13
  /*
14
- Copyright 2015 Lester Chan (email : lesterchan@gmail.com)
15
 
16
- This program is free software; you can redistribute it and/or modify
17
- it under the terms of the GNU General Public License as published by
18
- the Free Software Foundation; either version 2 of the License, or
19
- (at your option) any later version.
20
 
21
- This program is distributed in the hope that it will be useful,
22
- but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
- GNU General Public License for more details.
25
 
26
- You should have received a copy of the GNU General Public License
27
- along with this program; if not, write to the Free Software
28
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
  */
30
 
31
 
32
  ### Create Text Domain For Translation
33
  add_action( 'plugins_loaded', 'ban_textdomain' );
34
  function ban_textdomain() {
35
- load_plugin_textdomain( 'wp-ban' );
36
  }
37
 
38
 
39
  ### Function: Ban Menu
40
  add_action('admin_menu', 'ban_menu');
41
  function ban_menu() {
42
- add_options_page(__('Ban', 'wp-ban'), __('Ban', 'wp-ban'), 'manage_options', 'wp-ban/ban-options.php');
43
  }
44
 
45
 
46
  ### Function: Get IP Address (http://stackoverflow.com/a/2031935)
47
  function ban_get_ip() {
48
- $banned_options = get_option( 'banned_options' );
49
-
50
- if( intval( $banned_options['reverse_proxy'] ) === 1 ) {
51
- foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
52
- if ( array_key_exists( $key, $_SERVER ) === true ) {
53
- foreach ( explode( ',', $_SERVER[$key] ) as $ip ) {
54
- $ip = trim( $ip );
55
- if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
56
- return esc_attr( $ip );
57
- }
58
- }
59
- }
60
- }
61
- } else if( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
62
- $ip = $_SERVER['REMOTE_ADDR'];
63
- if( strpos( $ip, ',' ) !== false ) {
64
- $ip = explode( ',', $ip );
65
- $ip = $ip[0];
66
- }
67
- return esc_attr( $ip );
68
- }
69
-
70
- return '';
71
  }
72
 
73
 
74
  ### Function: Preview Banned Message
75
  add_action('wp_ajax_ban-admin', 'preview_banned_message');
76
  function preview_banned_message() {
77
- $banned_stats = get_option('banned_stats');
78
- $banned_message = stripslashes(get_option('banned_message'));
79
- $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
80
- $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
81
- $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][ban_get_ip()]), $banned_message);
82
- $banned_message = str_replace("%USER_IP%", ban_get_ip(), $banned_message);
83
- $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(ban_get_ip()), $banned_message);
84
- $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
85
- echo $banned_message;
86
- exit();
87
  }
88
 
89
 
90
  ### Function: Print Out Banned Message
91
  function print_banned_message() {
92
- // Credits To Joe (Ttech) - http://blog.fileville.net/
93
- $banned_stats = get_option('banned_stats');
94
- $banned_stats['count'] = intval($banned_stats['count']) + 1;
95
- $banned_stats['users'][ban_get_ip()] = intval($banned_stats['users'][ban_get_ip()]) + 1;
96
- update_option('banned_stats', $banned_stats);
97
- $banned_message = stripslashes(get_option('banned_message'));
98
- $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
99
- $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
100
- $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][ban_get_ip()]), $banned_message);
101
- $banned_message = str_replace("%USER_IP%", ban_get_ip(), $banned_message);
102
- $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(ban_get_ip()), $banned_message);
103
- $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
104
- echo $banned_message;
105
- exit();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  }
107
 
108
 
109
  ### Function: Process Banning
110
  function process_ban($banarray, $against) {
111
- if(!empty($banarray) && !empty($against)) {
112
- foreach($banarray as $cban) {
113
- if(preg_match_wildcard($cban, $against)) {
114
- print_banned_message();
115
- }
116
- }
117
- }
118
- return;
119
  }
120
 
121
 
122
  ### Function: Process Banned IP Range
123
  function process_ban_ip_range($banned_ips_range) {
124
- if(!empty($banned_ips_range)) {
125
- foreach($banned_ips_range as $banned_ip_range) {
126
- $range = explode('-', $banned_ip_range);
127
- $range_start = trim($range[0]);
128
- $range_end = trim($range[1]);
129
- if(check_ip_within_range(ban_get_ip(), $range_start, $range_end)) {
130
- print_banned_message();
131
- break;
132
- }
133
- }
134
- }
135
  }
136
 
137
 
138
  ### Function: Banned
139
  add_action('init', 'banned');
140
  function banned() {
141
- $ip = ban_get_ip();
142
- if($ip == 'unknown') {
143
- return;
144
- }
145
- $banned_ips = get_option('banned_ips');
146
- if(is_array($banned_ips))
147
- $banned_ips = array_filter($banned_ips);
148
-
149
- $banned_ips_range = get_option('banned_ips_range');
150
- if(is_array($banned_ips_range))
151
- $banned_ips_range = array_filter($banned_ips_range);
152
-
153
- $banned_hosts = get_option('banned_hosts');
154
- if(is_array($banned_hosts))
155
- $banned_hosts = array_filter($banned_hosts);
156
-
157
- $banned_referers = get_option('banned_referers');
158
- if(is_array($banned_referers))
159
- $banned_referers = array_filter($banned_referers);
160
-
161
- $banned_user_agents = get_option('banned_user_agents');
162
- if(is_array($banned_user_agents))
163
- $banned_user_agents = array_filter($banned_user_agents);
164
-
165
- $banned_exclude_ips = get_option('banned_exclude_ips');
166
- if(is_array($banned_exclude_ips))
167
- $banned_exclude_ips = array_filter($banned_exclude_ips);
168
-
169
- $is_excluded = false;
170
- if(!empty($banned_exclude_ips)) {
171
- foreach($banned_exclude_ips as $banned_exclude_ip) {
172
- if($ip == $banned_exclude_ip) {
173
- $is_excluded = true;
174
- break;
175
- }
176
- }
177
- }
178
-
179
- if( ! $is_excluded ) {
180
- if( ! empty( $banned_ips ) ) {
181
- process_ban( $banned_ips, $ip );
182
- }
183
- if( ! empty( $banned_ips_range ) ) {
184
- process_ban_ip_range( $banned_ips_range );
185
- }
186
- if( ! empty( $banned_hosts ) ) {
187
- process_ban( $banned_hosts, @gethostbyaddr( $ip ) );
188
- }
189
- if( ! empty( $banned_referers ) && ! empty( $_SERVER['HTTP_REFERER'] ) ) {
190
- process_ban( $banned_referers, $_SERVER['HTTP_REFERER'] );
191
- }
192
- if( ! empty( $banned_user_agents ) && ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
193
- process_ban( $banned_user_agents, $_SERVER['HTTP_USER_AGENT'] );
194
- }
195
- }
196
  }
197
 
198
 
199
  ### Function: Check Whether Or Not The IP Address Belongs To Admin
200
  function is_admin_ip($check) {
201
- return preg_match_wildcard($check, ban_get_ip());
202
  }
203
 
204
 
205
  ### Function: Check Whether IP Within A Given IP Range
206
  function check_ip_within_range($ip, $range_start, $range_end) {
207
- $range_start = ip2long($range_start);
208
- $range_end = ip2long($range_end);
209
- $ip = ip2long($ip);
210
- if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
211
- return true;
212
- }
213
- return false;
214
  }
215
 
216
 
217
  ### Function: Check Whether Or Not The Hostname Belongs To Admin
218
  function is_admin_hostname($check) {
219
- return preg_match_wildcard($check, @gethostbyaddr(ban_get_ip()));
220
  }
221
 
222
 
223
  ### Function: Check Whether Or Not The Referer Belongs To This Site
224
  function is_admin_referer($check) {
225
- $url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
226
- foreach($url_patterns as $url) {
227
- if(preg_match_wildcard($check, $url)) {
228
- return true;
229
- }
230
- }
231
- return false;
232
  }
233
 
234
 
235
  ### Function: Check Whether Or Not The User Agent Is Used by Admin
236
  function is_admin_user_agent($check) {
237
- return preg_match_wildcard($check, $_SERVER['HTTP_USER_AGENT']);
238
  }
239
 
240
 
241
  ### Function: Wildcard Check
242
  function preg_match_wildcard($regex, $subject) {
243
- $regex = preg_quote($regex, '#');
244
- $regex = str_replace('\*', '.*', $regex);
245
- if(preg_match("#^$regex$#", $subject)) {
246
- return true;
247
- } else {
248
- return false;
249
- }
250
  }
251
 
252
 
@@ -254,49 +274,49 @@ function preg_match_wildcard($regex, $subject) {
254
  register_activation_hook( __FILE__, 'ban_activation' );
255
  function ban_activation( $network_wide )
256
  {
257
- if ( is_multisite() && $network_wide )
258
- {
259
- $ms_sites = wp_get_sites();
260
-
261
- if( 0 < sizeof( $ms_sites ) )
262
- {
263
- foreach ( $ms_sites as $ms_site )
264
- {
265
- switch_to_blog( $ms_site['blog_id'] );
266
- ban_activate();
267
- }
268
- }
269
-
270
- restore_current_blog();
271
- }
272
- else
273
- {
274
- ban_activate();
275
- }
276
  }
277
 
278
  function ban_activate() {
279
- add_option('banned_ips', array());
280
- add_option('banned_hosts',array());
281
- add_option('banned_stats', array('users' => array(), 'count' => 0));
282
- add_option('banned_message', '<!DOCTYPE html>'."\n".
283
- '<html>'."\n".
284
- '<head>'."\n".
285
- '<meta charset="utf-8">'."\n".
286
- '<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
287
- '</head>'."\n".
288
- '<body>'."\n".
289
- '<div id="wp-ban-container">'."\n".
290
- '<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
291
- '</div>'."\n".
292
- '</body>'."\n".
293
- '</html>', 'Banned Message');
294
- // Database Upgrade For WP-Ban 1.11
295
- add_option('banned_referers', array());
296
- add_option('banned_exclude_ips', array());
297
- add_option('banned_ips_range', array());
298
- // Database Upgrade For WP-Ban 1.30
299
- add_option('banned_user_agents', array());
300
- // Database Upgrade For WP-Ban 1.64
301
- add_option( 'banned_options', array( 'reverse_proxy' => 0 ) );
302
  }
1
  <?php
2
  /*
3
  Plugin Name: WP-Ban
4
+ Plugin URI: https://lesterchan.net/portfolio/programming/php/
5
  Description: Ban users by IP, IP Range, host name, user agent and referer url from visiting your WordPress's blog. It will display a custom ban message when the banned IP, IP range, host name, user agent or referer url tries to visit you blog. You can also exclude certain IPs from being banned. There will be statistics recordered on how many times they attemp to visit your blog. It allows wildcard matching too.
6
+ Version: 1.69
7
  Author: Lester 'GaMerZ' Chan
8
+ Author URI: https://lesterchan.net
9
  Text Domain: wp-ban
10
  */
11
 
12
 
13
  /*
14
+ Copyright 2016 Lester Chan (email : lesterchan@gmail.com)
15
 
16
+ This program is free software; you can redistribute it and/or modify
17
+ it under the terms of the GNU General Public License as published by
18
+ the Free Software Foundation; either version 2 of the License, or
19
+ (at your option) any later version.
20
 
21
+ This program is distributed in the hope that it will be useful,
22
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ GNU General Public License for more details.
25
 
26
+ You should have received a copy of the GNU General Public License
27
+ along with this program; if not, write to the Free Software
28
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
  */
30
 
31
 
32
  ### Create Text Domain For Translation
33
  add_action( 'plugins_loaded', 'ban_textdomain' );
34
  function ban_textdomain() {
35
+ load_plugin_textdomain( 'wp-ban' );
36
  }
37
 
38
 
39
  ### Function: Ban Menu
40
  add_action('admin_menu', 'ban_menu');
41
  function ban_menu() {
42
+ add_options_page(__('Ban', 'wp-ban'), __('Ban', 'wp-ban'), 'manage_options', 'wp-ban/ban-options.php');
43
  }
44
 
45
 
46
  ### Function: Get IP Address (http://stackoverflow.com/a/2031935)
47
  function ban_get_ip() {
48
+ $banned_options = get_option( 'banned_options' );
49
+
50
+ if( intval( $banned_options['reverse_proxy'] ) === 1 ) {
51
+ foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
52
+ if ( array_key_exists( $key, $_SERVER ) === true ) {
53
+ foreach ( explode( ',', $_SERVER[$key] ) as $ip ) {
54
+ $ip = trim( $ip );
55
+ if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
56
+ return esc_attr( $ip );
57
+ }
58
+ }
59
+ }
60
+ }
61
+ } else if( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
62
+ $ip = $_SERVER['REMOTE_ADDR'];
63
+ if( strpos( $ip, ',' ) !== false ) {
64
+ $ip = explode( ',', $ip );
65
+ $ip = $ip[0];
66
+ }
67
+ return esc_attr( $ip );
68
+ }
69
+
70
+ return '';
71
  }
72
 
73
 
74
  ### Function: Preview Banned Message
75
  add_action('wp_ajax_ban-admin', 'preview_banned_message');
76
  function preview_banned_message() {
77
+ $banned_stats = get_option('banned_stats');
78
+ $banned_message = stripslashes(get_option('banned_message'));
79
+ $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
80
+ $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
81
+ $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][ban_get_ip()]), $banned_message);
82
+ $banned_message = str_replace("%USER_IP%", ban_get_ip(), $banned_message);
83
+ $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(ban_get_ip()), $banned_message);
84
+ $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
85
+ echo $banned_message;
86
+ exit();
87
  }
88
 
89
 
90
  ### Function: Print Out Banned Message
91
  function print_banned_message() {
92
+ $banned_ip = ban_get_ip();
93
+ $banned_stats = get_option( 'banned_stats' );
94
+ if( isset( $banned_stats['count'] ) ) {
95
+ $banned_stats['count'] += 1;
96
+ } else {
97
+ $banned_stats['count'] = 1;
98
+ }
99
+ if( isset( $banned_stats['users'][$banned_ip] ) ) {
100
+ $banned_stats['users'][$banned_ip] += 1;
101
+ } else {
102
+ $banned_stats['users'][$banned_ip] = 1;
103
+ }
104
+ update_option( 'banned_stats', $banned_stats );
105
+ $banned_message = str_replace(
106
+ array(
107
+ '%SITE_NAME%',
108
+ '%SITE_URL%',
109
+ '%USER_ATTEMPTS_COUNT%',
110
+ '%USER_IP%',
111
+ '%USER_HOSTNAME%',
112
+ '%TOTAL_ATTEMPTS_COUNT%'
113
+ ),
114
+ array(
115
+ get_option( 'blogname' ),
116
+ get_option( 'siteurl' ),
117
+ number_format_i18n( $banned_stats['users'][$banned_ip] ),
118
+ $banned_ip,
119
+ @gethostbyaddr( $banned_ip ),
120
+ number_format_i18n( $banned_stats['count'] )
121
+ ),
122
+ stripslashes( get_option( 'banned_message' ) )
123
+ );
124
+ echo $banned_message;
125
+ exit();
126
  }
127
 
128
 
129
  ### Function: Process Banning
130
  function process_ban($banarray, $against) {
131
+ if(!empty($banarray) && !empty($against)) {
132
+ foreach($banarray as $cban) {
133
+ if(preg_match_wildcard($cban, $against)) {
134
+ print_banned_message();
135
+ }
136
+ }
137
+ }
138
+ return;
139
  }
140
 
141
 
142
  ### Function: Process Banned IP Range
143
  function process_ban_ip_range($banned_ips_range) {
144
+ if(!empty($banned_ips_range)) {
145
+ foreach($banned_ips_range as $banned_ip_range) {
146
+ $range = explode('-', $banned_ip_range);
147
+ $range_start = trim($range[0]);
148
+ $range_end = trim($range[1]);
149
+ if(check_ip_within_range(ban_get_ip(), $range_start, $range_end)) {
150
+ print_banned_message();
151
+ break;
152
+ }
153
+ }
154
+ }
155
  }
156
 
157
 
158
  ### Function: Banned
159
  add_action('init', 'banned');
160
  function banned() {
161
+ $ip = ban_get_ip();
162
+ if($ip == 'unknown') {
163
+ return;
164
+ }
165
+ $banned_ips = get_option('banned_ips');
166
+ if(is_array($banned_ips))
167
+ $banned_ips = array_filter($banned_ips);
168
+
169
+ $banned_ips_range = get_option('banned_ips_range');
170
+ if(is_array($banned_ips_range))
171
+ $banned_ips_range = array_filter($banned_ips_range);
172
+
173
+ $banned_hosts = get_option('banned_hosts');
174
+ if(is_array($banned_hosts))
175
+ $banned_hosts = array_filter($banned_hosts);
176
+
177
+ $banned_referers = get_option('banned_referers');
178
+ if(is_array($banned_referers))
179
+ $banned_referers = array_filter($banned_referers);
180
+
181
+ $banned_user_agents = get_option('banned_user_agents');
182
+ if(is_array($banned_user_agents))
183
+ $banned_user_agents = array_filter($banned_user_agents);
184
+
185
+ $banned_exclude_ips = get_option('banned_exclude_ips');
186
+ if(is_array($banned_exclude_ips))
187
+ $banned_exclude_ips = array_filter($banned_exclude_ips);
188
+
189
+ $is_excluded = false;
190
+ if(!empty($banned_exclude_ips)) {
191
+ foreach($banned_exclude_ips as $banned_exclude_ip) {
192
+ if($ip == $banned_exclude_ip) {
193
+ $is_excluded = true;
194
+ break;
195
+ }
196
+ }
197
+ }
198
+
199
+ if( ! $is_excluded ) {
200
+ if( ! empty( $banned_ips ) ) {
201
+ process_ban( $banned_ips, $ip );
202
+ }
203
+ if( ! empty( $banned_ips_range ) ) {
204
+ process_ban_ip_range( $banned_ips_range );
205
+ }
206
+ if( ! empty( $banned_hosts ) ) {
207
+ process_ban( $banned_hosts, @gethostbyaddr( $ip ) );
208
+ }
209
+ if( ! empty( $banned_referers ) && ! empty( $_SERVER['HTTP_REFERER'] ) ) {
210
+ process_ban( $banned_referers, $_SERVER['HTTP_REFERER'] );
211
+ }
212
+ if( ! empty( $banned_user_agents ) && ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
213
+ process_ban( $banned_user_agents, $_SERVER['HTTP_USER_AGENT'] );
214
+ }
215
+ }
216
  }
217
 
218
 
219
  ### Function: Check Whether Or Not The IP Address Belongs To Admin
220
  function is_admin_ip($check) {
221
+ return preg_match_wildcard($check, ban_get_ip());
222
  }
223
 
224
 
225
  ### Function: Check Whether IP Within A Given IP Range
226
  function check_ip_within_range($ip, $range_start, $range_end) {
227
+ $range_start = ip2long($range_start);
228
+ $range_end = ip2long($range_end);
229
+ $ip = ip2long($ip);
230
+ if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
231
+ return true;
232
+ }
233
+ return false;
234
  }
235
 
236
 
237
  ### Function: Check Whether Or Not The Hostname Belongs To Admin
238
  function is_admin_hostname($check) {
239
+ return preg_match_wildcard($check, @gethostbyaddr(ban_get_ip()));
240
  }
241
 
242
 
243
  ### Function: Check Whether Or Not The Referer Belongs To This Site
244
  function is_admin_referer($check) {
245
+ $url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
246
+ foreach($url_patterns as $url) {
247
+ if(preg_match_wildcard($check, $url)) {
248
+ return true;
249
+ }
250
+ }
251
+ return false;
252
  }
253
 
254
 
255
  ### Function: Check Whether Or Not The User Agent Is Used by Admin
256
  function is_admin_user_agent($check) {
257
+ return preg_match_wildcard($check, $_SERVER['HTTP_USER_AGENT']);
258
  }
259
 
260
 
261
  ### Function: Wildcard Check
262
  function preg_match_wildcard($regex, $subject) {
263
+ $regex = preg_quote($regex, '#');
264
+ $regex = str_replace('\*', '.*', $regex);
265
+ if(preg_match("#^$regex$#", $subject)) {
266
+ return true;
267
+ } else {
268
+ return false;
269
+ }
270
  }
271
 
272
 
274
  register_activation_hook( __FILE__, 'ban_activation' );
275
  function ban_activation( $network_wide )
276
  {
277
+ if ( is_multisite() && $network_wide )
278
+ {
279
+ $ms_sites = wp_get_sites();
280
+
281
+ if( 0 < sizeof( $ms_sites ) )
282
+ {
283
+ foreach ( $ms_sites as $ms_site )
284
+ {
285
+ switch_to_blog( $ms_site['blog_id'] );
286
+ ban_activate();
287
+ }
288
+ }
289
+
290
+ restore_current_blog();
291
+ }
292
+ else
293
+ {
294
+ ban_activate();
295
+ }
296
  }
297
 
298
  function ban_activate() {
299
+ add_option('banned_ips', array());
300
+ add_option('banned_hosts',array());
301
+ add_option('banned_stats', array('users' => array(), 'count' => 0));
302
+ add_option('banned_message', '<!DOCTYPE html>'."\n".
303
+ '<html>'."\n".
304
+ '<head>'."\n".
305
+ '<meta charset="utf-8">'."\n".
306
+ '<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
307
+ '</head>'."\n".
308
+ '<body>'."\n".
309
+ '<div id="wp-ban-container">'."\n".
310
+ '<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
311
+ '</div>'."\n".
312
+ '</body>'."\n".
313
+ '</html>', 'Banned Message');
314
+ // Database Upgrade For WP-Ban 1.11
315
+ add_option('banned_referers', array());
316
+ add_option('banned_exclude_ips', array());
317
+ add_option('banned_ips_range', array());
318
+ // Database Upgrade For WP-Ban 1.30
319
+ add_option('banned_user_agents', array());
320
+ // Database Upgrade For WP-Ban 1.64
321
+ add_option( 'banned_options', array( 'reverse_proxy' => 0 ) );
322
  }
wp-ban.pot DELETED
@@ -1,325 +0,0 @@
1
- # Copyright (C) 2010 WP-Ban
2
- # This file is distributed under the same license as the WP-Ban package.
3
- msgid ""
4
- msgstr ""
5
- "Project-Id-Version: WP-Ban 1.50\n"
6
- "Report-Msgid-Bugs-To: http://wordpress.org/tag/wp-ban\n"
7
- "POT-Creation-Date: 2011-02-23 15:01:05+00:00\n"
8
- "MIME-Version: 1.0\n"
9
- "Content-Type: text/plain; charset=UTF-8\n"
10
- "Content-Transfer-Encoding: 8bit\n"
11
- "PO-Revision-Date: 2011-02-23 23:04+0800\n"
12
- "Last-Translator: Lester Chan <lesterchan@gmail.com>\n"
13
- "Language-Team: Lester Chan <lesterchan@gmail.com>\n"
14
- "Plural-Forms: nplurals=2; plural=n != 1;\n"
15
- "X-Poedit-Language: English\n"
16
- "X-Poedit-Country: SINGAPORE\n"
17
- "X-Poedit-KeywordsList: __\n"
18
- "X-Poedit-Basepath: .\n"
19
- "X-Poedit-SearchPath-0: .\n"
20
-
21
- #: ban-options.php:51
22
- msgid "This IP '%s' Belongs To The Admin And Will Not Be Added To Ban List"
23
- msgstr ""
24
-
25
- #: ban-options.php:64
26
- msgid "The Admin's IP '%s' Fall Within This Range (%s - %s) And Will Not Be Added To Ban List"
27
- msgstr ""
28
-
29
- #: ban-options.php:74
30
- msgid "This Hostname '%s' Belongs To The Admin And Will Not Be Added To Ban List"
31
- msgstr ""
32
-
33
- #: ban-options.php:84
34
- msgid "This Referer '%s' Belongs To This Site And Will Not Be Added To Ban List"
35
- msgstr ""
36
-
37
- #: ban-options.php:94
38
- msgid "This User Agent '%s' Is Used By The Current Admin And Will Not Be Added To Ban List"
39
- msgstr ""
40
-
41
- #: ban-options.php:113
42
- #: ban-options.php:310
43
- msgid "Banned IPs"
44
- msgstr ""
45
-
46
- #: ban-options.php:114
47
- #: ban-options.php:324
48
- msgid "Banned IP Range"
49
- msgstr ""
50
-
51
- #: ban-options.php:115
52
- #: ban-options.php:337
53
- msgid "Banned Host Names"
54
- msgstr ""
55
-
56
- #: ban-options.php:116
57
- #: ban-options.php:351
58
- msgid "Banned Referers"
59
- msgstr ""
60
-
61
- #: ban-options.php:117
62
- #: ban-options.php:365
63
- msgid "Banned User Agents"
64
- msgstr ""
65
-
66
- #: ban-options.php:118
67
- msgid "Banned Excluded IPs"
68
- msgstr ""
69
-
70
- #: ban-options.php:119
71
- #: ban-options.php:395
72
- msgid "Banned Message"
73
- msgstr ""
74
-
75
- #: ban-options.php:123
76
- msgid "Updated"
77
- msgstr ""
78
-
79
- #: ban-options.php:128
80
- msgid "No Ban Option Updated"
81
- msgstr ""
82
-
83
- #: ban-options.php:135
84
- #: ban-options.php:460
85
- msgid "Reset Ban Stats"
86
- msgstr ""
87
-
88
- #: ban-options.php:139
89
- msgid "All IP Ban Stats And Total Ban Stat Reseted"
90
- msgstr ""
91
-
92
- #: ban-options.php:147
93
- msgid "Selected IP Ban Stats Reseted"
94
- msgstr ""
95
-
96
- #: ban-options.php:151
97
- #: ban-options.php:500
98
- msgid "UNINSTALL WP-Ban"
99
- msgstr ""
100
-
101
- #: ban-options.php:159
102
- msgid "Setting Key '%s' has been deleted."
103
- msgstr ""
104
-
105
- #: ban-options.php:163
106
- msgid "Error deleting Setting Key '%s'."
107
- msgstr ""
108
-
109
- #: ban-options.php:185
110
- #: ban-options.php:468
111
- msgid "Uninstall WP-Ban"
112
- msgstr ""
113
-
114
- #: ban-options.php:186
115
- msgid "<a href=\"%s\">Click Here</a> To Finish The Uninstallation And WP-Ban Will Be Deactivated Automatically."
116
- msgstr ""
117
-
118
- #: ban-options.php:247
119
- #: wp-ban.php:238
120
- msgid "You Are Banned."
121
- msgstr ""
122
-
123
- #: ban-options.php:276
124
- msgid "Ban Options"
125
- msgstr ""
126
-
127
- #: ban-options.php:280
128
- msgid "Your Details"
129
- msgstr ""
130
-
131
- #: ban-options.php:281
132
- msgid "Value"
133
- msgstr ""
134
-
135
- #: ban-options.php:285
136
- msgid "IP"
137
- msgstr ""
138
-
139
- #: ban-options.php:289
140
- msgid "Host Name"
141
- msgstr ""
142
-
143
- #: ban-options.php:293
144
- msgid "User Agent"
145
- msgstr ""
146
-
147
- #: ban-options.php:297
148
- msgid "Site URL"
149
- msgstr ""
150
-
151
- #: ban-options.php:302
152
- msgid "Please <strong>DO NOT</strong> ban yourself."
153
- msgstr ""
154
-
155
- #: ban-options.php:311
156
- msgid "Use <strong>*</strong> for wildcards."
157
- msgstr ""
158
-
159
- #: ban-options.php:312
160
- #: ban-options.php:325
161
- #: ban-options.php:339
162
- #: ban-options.php:353
163
- #: ban-options.php:367
164
- #: ban-options.php:382
165
- msgid "Start each entry on a new line."
166
- msgstr ""
167
-
168
- #: ban-options.php:313
169
- #: ban-options.php:326
170
- #: ban-options.php:340
171
- #: ban-options.php:354
172
- #: ban-options.php:368
173
- #: ban-options.php:383
174
- msgid "Examples:"
175
- msgstr ""
176
-
177
- #: ban-options.php:328
178
- #: ban-options.php:356
179
- #: ban-options.php:385
180
- msgid "Notes:"
181
- msgstr ""
182
-
183
- #: ban-options.php:329
184
- #: ban-options.php:386
185
- msgid "No Wildcards Allowed."
186
- msgstr ""
187
-
188
- #: ban-options.php:338
189
- #: ban-options.php:352
190
- #: ban-options.php:366
191
- msgid "Use <strong>*</strong> for wildcards"
192
- msgstr ""
193
-
194
- #: ban-options.php:357
195
- msgid "There are ways to bypass this method of banning."
196
- msgstr ""
197
-
198
- #: ban-options.php:372
199
- msgid "Suggestions:"
200
- msgstr ""
201
-
202
- #: ban-options.php:373
203
- msgid "See <a href=\"http://www.user-agents.org/\">http://www.user-agents.org/</a>"
204
- msgstr ""
205
-
206
- #: ban-options.php:381
207
- msgid "Banned Exclude IPs"
208
- msgstr ""
209
-
210
- #: ban-options.php:387
211
- msgid "These Users Will Not Get Banned."
212
- msgstr ""
213
-
214
- #: ban-options.php:396
215
- msgid "Allowed Variables:"
216
- msgstr ""
217
-
218
- #: ban-options.php:403
219
- msgid "Restore Default Template"
220
- msgstr ""
221
-
222
- #: ban-options.php:404
223
- msgid "Preview Banned Message"
224
- msgstr ""
225
-
226
- #: ban-options.php:412
227
- msgid "Save Changes"
228
- msgstr ""
229
-
230
- #: ban-options.php:420
231
- msgid "Ban Stats"
232
- msgstr ""
233
-
234
- #: ban-options.php:425
235
- msgid "IPs"
236
- msgstr ""
237
-
238
- #: ban-options.php:426
239
- msgid "Attempts"
240
- msgstr ""
241
-
242
- #: ban-options.php:427
243
- msgid "Action"
244
- msgstr ""
245
-
246
- #: ban-options.php:444
247
- msgid "Reset this IP ban stat?"
248
- msgstr ""
249
-
250
- #: ban-options.php:450
251
- msgid "No Attempts"
252
- msgstr ""
253
-
254
- #: ban-options.php:455
255
- msgid "Total Attempts:"
256
- msgstr ""
257
-
258
- #: ban-options.php:457
259
- msgid "Reset all IP ban stats and total ban stat?"
260
- msgstr ""
261
-
262
- #: ban-options.php:460
263
- msgid "You Are About To Reset Ban Stats."
264
- msgstr ""
265
-
266
- #: ban-options.php:460
267
- msgid "This Action Is Not Reversible. Are you sure?"
268
- msgstr ""
269
-
270
- #: ban-options.php:470
271
- msgid "Deactivating WP-Ban plugin does not remove any data that may have been created, such as the ban options. To completely remove this plugin, you can uninstall it here."
272
- msgstr ""
273
-
274
- #: ban-options.php:473
275
- msgid "WARNING:"
276
- msgstr ""
277
-
278
- #: ban-options.php:474
279
- msgid "Once uninstalled, this cannot be undone. You should use a Database Backup plugin of WordPress to back up all the data first."
280
- msgstr ""
281
-
282
- #: ban-options.php:477
283
- msgid "The following WordPress Options will be DELETED:"
284
- msgstr ""
285
-
286
- #: ban-options.php:482
287
- msgid "WordPress Options"
288
- msgstr ""
289
-
290
- #: ban-options.php:499
291
- msgid "Yes"
292
- msgstr ""
293
-
294
- #: ban-options.php:500
295
- msgid ""
296
- "You Are About To Uninstall WP-Ban From WordPress.\\n"
297
- "This Action Is Not Reversible.\\n"
298
- "\\n"
299
- " Choose [Cancel] To Stop, [OK] To Uninstall."
300
- msgstr ""
301
-
302
- #: wp-ban.php:42
303
- msgid "Ban"
304
- msgstr ""
305
-
306
- #. Plugin Name of the plugin/theme
307
- msgid "WP-Ban"
308
- msgstr ""
309
-
310
- #. Plugin URI of the plugin/theme
311
- msgid "http://lesterchan.net/portfolio/programming/php/"
312
- msgstr ""
313
-
314
- #. Description of the plugin/theme
315
- msgid "Ban users by IP, IP Range, host name, user agent and referer url from visiting your WordPress's blog. It will display a custom ban message when the banned IP, IP range, host name, user agent or referer url tries to visit you blog. You can also exclude certain IPs from being banned. There will be statistics recordered on how many times they attemp to visit your blog. It allows wildcard matching too."
316
- msgstr ""
317
-
318
- #. Author of the plugin/theme
319
- msgid "Lester 'GaMerZ' Chan"
320
- msgstr ""
321
-
322
- #. Author URI of the plugin/theme
323
- msgid "http://lesterchan.net"
324
- msgstr ""
325
-