PluginProbe
WP-Ban / 1.40
WP-Ban v1.40
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.40, at wp-ban.php

248 lines 8.5 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.40
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 */
10
11
12 /*
13 Copyright 2008 Lester Chan (email : lesterchan@gmail.com)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
30
31 ### Create Text Domain For Translation
32 add_action('init', 'ban_textdomain');
33 function ban_textdomain() {
34 load_plugin_textdomain('wp-ban', false, 'wp-ban');
35 }
36
37
38 ### Function: Ban Menu
39 add_action('admin_menu', 'ban_menu');
40 function ban_menu() {
41 if (function_exists('add_management_page')) {
42 add_options_page(__('Ban', 'wp-ban'), __('Ban', 'wp-ban'), 'manage_options', 'wp-ban/ban-options.php');
43 }
44 }
45
46
47 ### Function: Get IP Address
48 if(!function_exists('get_IP')) {
49 function get_IP() {
50 if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
51 $ip_address = $_SERVER['HTTP_CLIENT_IP'];
52 } else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
53 $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
54 } else if(!empty($_SERVER['REMOTE_ADDR'])) {
55 $ip_address = $_SERVER['REMOTE_ADDR'];
56 } else {
57 $ip_address = '';
58 }
59 if(strpos($ip_address, ',') !== false) {
60 $ip_address = explode(',', $ip_address);
61 $ip_address = $ip_address[0];
62 }
63 return $ip_address;
64 }
65 }
66
67
68 ### Function: Print Out Banned Message
69 function print_banned_message() {
70 // Credits To Joe (Ttech) - http://blog.fileville.net/
71 $banned_stats = get_option('banned_stats');
72 $banned_stats['count'] = intval($banned_stats['count']) + 1;
73 $banned_stats['users'][get_IP()] = intval($banned_stats['users'][get_IP()]) + 1;
74 update_option('banned_stats', $banned_stats);
75 $banned_message = stripslashes(get_option('banned_message'));
76 $banned_message = str_replace("%SITE_NAME%", get_option('blogname'), $banned_message);
77 $banned_message = str_replace("%SITE_URL%", get_option('siteurl'), $banned_message);
78 $banned_message = str_replace("%USER_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['users'][get_IP()]), $banned_message);
79 $banned_message = str_replace("%USER_IP%", get_IP(), $banned_message);
80 $banned_message = str_replace("%USER_HOSTNAME%", @gethostbyaddr(get_IP()), $banned_message);
81 $banned_message = str_replace("%TOTAL_ATTEMPTS_COUNT%", number_format_i18n($banned_stats['count']), $banned_message);
82 echo $banned_message;
83 exit();
84 }
85
86
87 ### Function: Process Banning
88 function process_ban($banarray, $against) {
89 if(!empty($banarray) && !empty($against)) {
90 foreach($banarray as $cban) {
91 $regexp = str_replace ('.', '\\.', $cban);
92 $regexp = str_replace ('*', '.+', $regexp);
93 if(ereg("^$regexp$", $against)) {
94 print_banned_message();
95 }
96 }
97 }
98 return;
99 }
100
101
102 ### Function: Process Banned IP Range
103 function process_ban_ip_range($banned_ips_range) {
104 if(!empty($banned_ips_range)) {
105 foreach($banned_ips_range as $banned_ip_range) {
106 $range = explode('-', $banned_ip_range);
107 $range_start = trim($range[0]);
108 $range_end = trim($range[1]);
109 if(check_ip_within_range(get_IP(), $range_start, $range_end)) {
110 print_banned_message();
111 break;
112 }
113 }
114 }
115 }
116
117
118 ### Function: Banned
119 add_action('init', 'banned');
120 function banned() {
121 $ip = get_IP();
122 if($ip == 'unknown') {
123 return;
124 }
125 $banned_ips = get_option('banned_ips');
126 $banned_ips_range = get_option('banned_ips_range');
127 $banned_hosts = get_option('banned_hosts');
128 $banned_referers = get_option('banned_referers');
129 $banned_user_agents = get_option('banned_user_agents');
130 $banned_exclude_ips = get_option('banned_exclude_ips');
131 $is_excluded = false;
132 if(!empty($banned_exclude_ips)) {
133 foreach($banned_exclude_ips as $banned_exclude_ip) {
134 if($ip == $banned_exclude_ip) {
135 $is_excluded = true;
136 break;
137 }
138 }
139 }
140 if(!$is_excluded) {
141 process_ban($banned_ips, $ip);
142 process_ban_ip_range($banned_ips_range);
143 process_ban($banned_hosts, @gethostbyaddr($ip));
144 process_ban($banned_referers, $_SERVER['HTTP_REFERER']);
145 process_ban($banned_user_agents, $_SERVER['HTTP_USER_AGENT']);
146 }
147 }
148
149
150 ### Function: Check Whether Or Not The IP Address Belongs To Admin
151 function is_admin_ip($check) {
152 $admin_ip = get_IP();
153 $regexp = str_replace ('.', '\\.', $check);
154 $regexp = str_replace ('*', '.+', $regexp);
155 if(ereg("^$regexp$", $admin_ip)) {
156 return true;
157 }
158 return false;
159 }
160
161
162 ### Function: Check Whether IP Within A Given IP Range
163 function check_ip_within_range($ip, $range_start, $range_end) {
164 $range_start = ip2long($range_start);
165 $range_end = ip2long($range_end);
166 $ip = ip2long($ip);
167 if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
168 return true;
169 }
170 return false;
171 }
172
173
174 ### Function: Check Whether Or Not The Hostname Belongs To Admin
175 function is_admin_hostname($check) {
176 $admin_hostname = @gethostbyaddr(get_IP());
177 $regexp = str_replace ('.', '\\.', $check);
178 $regexp = str_replace ('*', '.+', $regexp);
179 if(ereg("^$regexp$", $admin_hostname)) {
180 return true;
181 }
182 return false;
183 }
184
185
186 ### Function: Check Whether Or Not The Referer Belongs To This Site
187 function is_admin_referer($check) {
188 $regexp = str_replace ('.', '\\.', $check);
189 $regexp = str_replace ('*', '.+', $regexp);
190 $url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
191 foreach($url_patterns as $url) {
192 if(ereg("^$regexp$", $url)) {
193 return true;
194 }
195 }
196 return false;
197 }
198
199
200 ### Function: Check Whether Or Not The User Agent Is Used by Admin
201 function is_admin_user_agent($check) {
202 $regexp = str_replace ('.', '\\.', $check);
203 $regexp = str_replace ('*', '.+', $regexp);
204 return ereg("^$regexp$", $_SERVER['HTTP_USER_AGENT']);
205 }
206
207 ### Function: Returns page's language attributes depends on WordPress language
208 function get_language_attributes($doctype = 'html') {
209 ob_start();
210 language_attributes();
211 $language_attributes = ob_get_contents();
212 ob_end_clean();
213 return $language_attributes;
214 }
215
216
217 ### Function: Create Ban Options
218 add_action('activate_wp-ban/wp-ban.php', 'ban_init');
219 function ban_init() {
220 global $wpdb;
221 ban_textdomain();
222 $banned_ips = array();
223 $banned_ips_range = array();
224 $banned_hosts = array();
225 $banned_referers = array();
226 $banned_exclude_ips = array();
227 $banned_stats = array('users' => array(), 'count' => 0);
228 add_option('banned_ips', $banned_ips, 'Banned IPs');
229 add_option('banned_hosts', $banned_hosts, 'Banned Hosts');
230 add_option('banned_stats', $banned_stats, 'WP-Ban Stats');
231 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".
232 '<html xmlns="http://www.w3.org/1999/xhtml" '.get_language_attributes().'>'."\n".
233 '<head>'."\n".
234 '<meta http-equiv="Content-Type" content="text/html; charset='.get_option('blog_charset').'" />'."\n".
235 '<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
236 '</head>'."\n".
237 '<body>'."\n".
238 '<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
239 '</body>'."\n".
240 '</html>', 'Banned Message');
241 // Database Upgrade For WP-Ban 1.11
242 add_option('banned_referers', $banned_referers, 'Banned Referers');
243 add_option('banned_exclude_ips', $banned_exclude_ips, 'Banned Exclude IP');
244 add_option('banned_ips_range', $banned_ips_range, 'Banned IP Range');
245 // Database Upgrade For WP-Ban 1.30
246 add_option('banned_user_agents', $banned_user_agents, 'Banned User Agents');
247 }
248 ?>