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

234 lines 8.2 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.30
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', 'wp-content/plugins/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_management_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 $banned_ips = get_option('banned_ips');
122 $banned_ips_range = get_option('banned_ips_range');
123 $banned_hosts = get_option('banned_hosts');
124 $banned_referers = get_option('banned_referers');
125 $banned_user_agents = get_option('banned_user_agents');
126 $banned_exclude_ips = get_option('banned_exclude_ips');
127 $is_excluded = false;
128 if(!empty($banned_exclude_ips)) {
129 foreach($banned_exclude_ips as $banned_exclude_ip) {
130 if(get_IP() == $banned_exclude_ip) {
131 $is_excluded = true;
132 break;
133 }
134 }
135 }
136 if(!$is_excluded) {
137 process_ban($banned_ips, get_IP());
138 process_ban_ip_range($banned_ips_range);
139 process_ban($banned_hosts, @gethostbyaddr(get_IP()));
140 process_ban($banned_referers, $_SERVER['HTTP_REFERER']);
141 process_ban($banned_user_agents, $_SERVER['HTTP_USER_AGENT']);
142 }
143 }
144
145
146 ### Function: Check Whether Or Not The IP Address Belongs To Admin
147 function is_admin_ip($check) {
148 $admin_ip = get_IP();
149 $regexp = str_replace ('.', '\\.', $check);
150 $regexp = str_replace ('*', '.+', $regexp);
151 if(ereg("^$regexp$", $admin_ip)) {
152 return true;
153 }
154 return false;
155 }
156
157
158 ### Function: Check Whether IP Within A Given IP Range
159 function check_ip_within_range($ip, $range_start, $range_end) {
160 $range_start = ip2long($range_start);
161 $range_end = ip2long($range_end);
162 $ip = ip2long($ip);
163 if($ip !== false && $ip >= $range_start && $ip <= $range_end) {
164 return true;
165 }
166 return false;
167 }
168
169
170 ### Function: Check Whether Or Not The Hostname Belongs To Admin
171 function is_admin_hostname($check) {
172 $admin_hostname = @gethostbyaddr(get_IP());
173 $regexp = str_replace ('.', '\\.', $check);
174 $regexp = str_replace ('*', '.+', $regexp);
175 if(ereg("^$regexp$", $admin_hostname)) {
176 return true;
177 }
178 return false;
179 }
180
181
182 ### Function: Check Whether Or Not The Referer Belongs To This Site
183 function is_admin_referer($check) {
184 $regexp = str_replace ('.', '\\.', $check);
185 $regexp = str_replace ('*', '.+', $regexp);
186 $url_patterns = array(get_option('siteurl'), get_option('home'), get_option('siteurl').'/', get_option('home').'/', get_option('siteurl').'/ ', get_option('home').'/ ', $_SERVER['HTTP_REFERER']);
187 foreach($url_patterns as $url) {
188 if(ereg("^$regexp$", $url)) {
189 return true;
190 }
191 }
192 return false;
193 }
194
195
196 ### Function: Check Whether Or Not The User Agent Is Used by Admin
197 function is_admin_user_agent($check) {
198 $regexp = str_replace ('.', '\\.', $check);
199 $regexp = str_replace ('*', '.+', $regexp);
200 return ereg("^$regexp$", $_SERVER['HTTP_USER_AGENT']);
201 }
202
203
204 ### Function: Create Ban Options
205 add_action('activate_wp-ban/wp-ban.php', 'ban_init');
206 function ban_init() {
207 global $wpdb;
208 $banned_ips = array();
209 $banned_ips_range = array();
210 $banned_hosts = array();
211 $banned_referers = array();
212 $banned_exclude_ips = array();
213 $banned_stats = array('users' => array(), 'count' => 0);
214 add_option('banned_ips', $banned_ips, 'Banned IPs');
215 add_option('banned_hosts', $banned_hosts, 'Banned Hosts');
216 add_option('banned_stats', $banned_stats, 'WP-Ban Stats');
217 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".
218 '<html xmlns="http://www.w3.org/1999/xhtml">'."\n".
219 '<head>'."\n".
220 '<meta http-equiv="Content-Type" content="text/html; charset='.get_option('blog_charset').'" />'."\n".
221 '<title>%SITE_NAME% - %SITE_URL%</title>'."\n".
222 '</head>'."\n".
223 '<body>'."\n".
224 '<p style="text-align: center; font-weight: bold;">'.__('You Are Banned.', 'wp-ban').'</p>'."\n".
225 '</body>'."\n".
226 '</html>', 'Banned Message');
227 // Database Upgrade For WP-Ban 1.11
228 add_option('banned_referers', $banned_referers, 'Banned Referers');
229 add_option('banned_exclude_ips', $banned_exclude_ips, 'Banned Exclude IP');
230 add_option('banned_ips_range', $banned_ips_range, 'Banned IP Range');
231 // Database Upgrade For WP-Ban 1.30
232 add_option('banned_user_agents', $banned_user_agents, 'Banned User Agents');
233 }
234 ?>