PluginProbe
WP-Ban / 1.62
WP-Ban v1.62
trunk 1.00 1.10 1.11 1.20 1.30 1.31 1.40 1.50 1.62 1.63 1.64 1.65 1.66 1.67 1.68 1.69 1.69.1 1.69.2 2.0.0
wp-ban / wp-ban.php

wp-ban.php in WP-Ban 1.62, at wp-ban.php

290 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.62
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 Text Domain: wp-ban
10 */
11
12
13 /*
14 Copyright 2013 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('init', 'ban_textdomain');
34 function ban_textdomain() {
35 load_plugin_textdomain('wp-ban', false, 'wp-ban');
36 }
37
38
39 ### Function: Ban Menu
40 add_action('admin_menu', 'ban_menu');
41 function ban_menu() {
42 if (function_exists('add_management_page')) {
43 add_options_page(__('Ban', 'wp-ban'), __('Ban', 'wp-ban'), 'manage_options', 'wp-ban/ban-options.php');
44 }
45 }
46
47
48 ### Function: Get IP Address
49 if(!function_exists('get_IP')) {
50 function get_IP() {
51 if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
52 $ip_address = $_SERVER['HTTP_CLIENT_IP'];
53 } else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
54 $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
55 } else if(!empty($_SERVER['REMOTE_ADDR'])) {
56 $ip_address = $_SERVER['REMOTE_ADDR'];
57 } else {
58 $ip_address = '';
59 }
60 if(strpos($ip_address, ',') !== false) {
61 $ip_address = explode(',', $ip_address);
62 $ip_address = $ip_address[0];
63 }
64 return esc_attr($ip_address);
65 }
66 }
67
68
69 ### Function: Preview Banned Message
70 add_action('wp_ajax_ban-admin', 'preview_banned_message');
71 function preview_banned_message()
72 {
73 $banned_stats = get_option('banned_stats');
74 $banned_message = stripslashes(get_option('banned_message'));
75 $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
76 $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
77 $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][get_IP()]), $banned_message);
78 $banned_message = str_replace("%USER_IP%", get_IP(), $banned_message);
79 $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(get_IP()), $banned_message);
80 $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
81 echo $banned_message;
82 exit();
83 }
84
85
86 ### Function: Print Out Banned Message
87 function print_banned_message() {
88 // Credits To Joe (Ttech) - http://blog.fileville.net/
89 $banned_stats = get_option('banned_stats');
90 $banned_stats['count'] = intval($banned_stats['count']) + 1;
91 $banned_stats['users'][get_IP()] = intval($banned_stats['users'][get_IP()]) + 1;
92 update_option('banned_stats', $banned_stats);
93 $banned_message = stripslashes(get_option('banned_message'));
94 $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
95 $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
96 $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][get_IP()]), $banned_message);
97 $banned_message = str_replace("%USER_IP%", get_IP(), $banned_message);
98 $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(get_IP()), $banned_message);
99 $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
100 echo $banned_message;
101 exit();
102 }
103
104
105 ### Function: Process Banning
106 function process_ban($banarray, $against) {
107 if(!empty($banarray) && !empty($against)) {
108 foreach($banarray as $cban) {
109 if(preg_match_wildcard($cban, $against)) {
110 print_banned_message();
111 }
112 }
113 }
114 return;
115 }
116
117
118 ### Function: Process Banned IP Range
119 function process_ban_ip_range($banned_ips_range) {
120 if(!empty($banned_ips_range)) {
121 foreach($banned_ips_range as $banned_ip_range) {
122 $range = explode('-', $banned_ip_range);
123 $range_start = trim($range[0]);
124 $range_end = trim($range[1]);
125 if(check_ip_within_range(get_IP(), $range_start, $range_end)) {
126 print_banned_message();
127 break;
128 }
129 }
130 }
131 }
132
133
134 ### Function: Banned
135 add_action('init', 'banned');
136 function banned() {
137 $ip = get_IP();
138 if($ip == 'unknown') {
139 return;
140 }
141 $banned_ips = get_option('banned_ips');
142 if(is_array($banned_ips))
143 $banned_ips = array_filter($banned_ips);
144
145 $banned_ips_range = get_option('banned_ips_range');
146 if(is_array($banned_ips_range))
147 $banned_ips_range = array_filter($banned_ips_range);
148
149 $banned_hosts = get_option('banned_hosts');
150 if(is_array($banned_hosts))
151 $banned_hosts = array_filter($banned_hosts);
152
153 $banned_referers = get_option('banned_referers');
154 if(is_array($banned_referers))
155 $banned_referers = array_filter($banned_referers);
156
157 $banned_user_agents = get_option('banned_user_agents');
158 if(is_array($banned_user_agents))
159 $banned_user_agents = array_filter($banned_user_agents);
160
161 $banned_exclude_ips = get_option('banned_exclude_ips');
162 if(is_array($banned_exclude_ips))
163 $banned_exclude_ips = array_filter($banned_exclude_ips);
164
165 $is_excluded = false;
166 if(!empty($banned_exclude_ips)) {
167 foreach($banned_exclude_ips as $banned_exclude_ip) {
168 if($ip == $banned_exclude_ip) {
169 $is_excluded = true;
170 break;
171 }
172 }
173 }
174
175 if(!$is_excluded) {
176 if(!empty($banned_ips))
177 process_ban($banned_ips, $ip);
178 if(!empty($banned_ips_range))
179 process_ban_ip_range($banned_ips_range);
180 if(!empty($banned_hosts))
181 process_ban($banned_hosts, @gethostbyaddr($ip));
182 if(!empty($banned_referers))
183 process_ban($banned_referers, $_SERVER['HTTP_REFERER']);
184 if(!empty($banned_user_agents))
185 process_ban($banned_user_agents, $_SERVER['HTTP_USER_AGENT']);
186 }
187 }
188
189
190 ### Function: Check Whether Or Not The IP Address Belongs To Admin
191 function is_admin_ip($check) {
192 return preg_match_wildcard($check, get_IP());
193 }
194
195
196 ### Function: Check Whether IP Within A Given IP Range
197 function check_ip_within_range($ip, $range_start, $range_end) {
198 $range_start = ip2long($range_start);
199 $range_end = ip2long($range_end);
200 $ip = ip2long($ip);
201 if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
202 return true;
203 }
204 return false;
205 }
206
207
208 ### Function: Check Whether Or Not The Hostname Belongs To Admin
209 function is_admin_hostname($check) {
210 return preg_match_wildcard($check, @gethostbyaddr(get_IP()));
211 }
212
213
214 ### Function: Check Whether Or Not The Referer Belongs To This Site
215 function is_admin_referer($check) {
216 $url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
217 foreach($url_patterns as $url) {
218 if(preg_match_wildcard($check, $url)) {
219 return true;
220 }
221 }
222 return false;
223 }
224
225
226 ### Function: Check Whether Or Not The User Agent Is Used by Admin
227 function is_admin_user_agent($check) {
228 return preg_match_wildcard($check, $_SERVER['HTTP_USER_AGENT']);
229 }
230
231
232 ### Function: Returns page's language attributes depends on WordPress language
233 function get_language_attributes($doctype = 'html') {
234 ob_start();
235 language_attributes();
236 $language_attributes = ob_get_contents();
237 ob_end_clean();
238 return $language_attributes;
239 }
240
241
242 ### Function: Wildcard Check
243 function preg_match_wildcard($regex, $subject) {
244 $regex = preg_quote($regex, '#');
245 $regex = str_replace('\*', '.*', $regex);
246 if(preg_match("#^$regex$#", $subject))
247 {
248 return true;
249 }
250 else
251 {
252 return false;
253 }
254 }
255
256
257 ### Function: Create Ban Options
258 add_action('activate_wp-ban/wp-ban.php', 'ban_init');
259 function ban_init() {
260 global $wpdb;
261 ban_textdomain();
262 $banned_ips = array();
263 $banned_ips_range = array();
264 $banned_hosts = array();
265 $banned_referers = array();
266 $banned_exclude_ips = array();
267 $banned_stats = array('users' => array(), 'count' => 0);
268 add_option('banned_ips', $banned_ips, 'Banned IPs');
269 add_option('banned_hosts', $banned_hosts, 'Banned Hosts');
270 add_option('banned_stats', $banned_stats, 'WP-Ban Stats');
271 add_option('banned_message', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n".
272 '<html xmlns="http://www.w3.org/1999/xhtml" '.get_language_attributes().'>'."\n".
273 '<head>'."\n".
274 '<meta http-equiv="Content-Type" content="text/html; charset='.get_option('blog_charset').'" />'."\n".
275 '<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
276 '</head>'."\n".
277 '<body>'."\n".
278 '<div id="wp-ban-container">'."\n".
279 '<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
280 '</div>'."\n".
281 '</body>'."\n".
282 '</html>', 'Banned Message');
283 // Database Upgrade For WP-Ban 1.11
284 add_option('banned_referers', $banned_referers, 'Banned Referers');
285 add_option('banned_exclude_ips', $banned_exclude_ips, 'Banned Exclude IP');
286 add_option('banned_ips_range', $banned_ips_range, 'Banned IP Range');
287 // Database Upgrade For WP-Ban 1.30
288 add_option('banned_user_agents', $banned_user_agents, 'Banned User Agents');
289 }
290 ?>