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