View source
<?php
function ip_ranges_menu() {
$items = array();
$items['admin/config/people/ip-ranges'] = array(
'title' => 'IP range bans',
'description' => 'Manage banned IP addresse ranges.',
'page callback' => 'ip_ranges_page',
'access arguments' => array(
'ban IP address ranges',
),
'file' => 'ip_ranges.admin.inc',
'weight' => 10,
);
$items['admin/config/people/ip-ranges/delete/%'] = array(
'title' => 'Delete IP range',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ip_ranges_delete',
5,
),
'access arguments' => array(
'ban IP address ranges',
),
'file' => 'ip_ranges.admin.inc',
);
$items['admin/config/people/ip-ranges/whitelist_own'] = array(
'title' => 'Whitelist your own IP',
'page callback' => 'ip_ranges_whitelist_own_address',
'access arguments' => array(
'ban IP address ranges',
),
);
return $items;
}
function ip_ranges_permission() {
return array(
'ban IP address ranges' => array(
'title' => t('Ban IP address ranges'),
),
);
}
function ip_ranges_boot() {
$whitelist = ip_ranges_get_ip_list('whitelist');
$current_ip = ip_address();
foreach ($whitelist as $ip) {
if (ip_ranges_check_ip($ip->ip, $current_ip)) {
$whitelisted = 1;
break;
}
}
if (!isset($whitelisted)) {
$blacklist = ip_ranges_get_ip_list('blacklist');
foreach ($blacklist as $ip) {
if (ip_ranges_check_ip($ip->ip, $current_ip)) {
ip_ranges_deny_access();
}
}
}
}
function ip_ranges_deny_access() {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
print 'Sorry, ' . check_plain(ip_address()) . ' has been banned.';
exit;
}
function ip_ranges_check_ip($ip, $current_ip) {
$type = strpos($ip, '-') ? 'range' : 'single';
return $type == 'single' ? $ip == $current_ip : ip_ranges_check_range($ip, $current_ip);
}
function ip_ranges_check_range($ip, $current_ip) {
$ip = explode('-', $ip);
list($lower, $upper) = $ip;
$lower_dec = (double) sprintf("%u", ip2long($lower));
$upper_dec = (double) sprintf("%u", ip2long($upper));
$ip_dec = (double) sprintf("%u", ip2long($current_ip));
return $ip_dec >= $lower_dec && $ip_dec <= $upper_dec;
}
function ip_ranges_write_record($ip, $type, $bid = NULL) {
db_merge('ip_ranges')
->key(array(
'bid' => $bid,
))
->fields(array(
'bid' => $bid,
'ip' => $ip,
'type' => $type,
))
->execute();
}
function ip_ranges_get_ip_list($type = '') {
$query = db_select('ip_ranges', 'list');
if ($type) {
$query
->condition('list.type', $type, '=')
->fields('list', array(
'ip',
));
}
else {
$query
->fields('list');
}
return $query
->execute()
->fetchAll();
}
function ip_ranges_whitelist_own_address() {
ip_ranges_write_record(ip_address(), 'whitelist');
drupal_set_message(t('Your own IP-address (@ip) has been whitelisted.', array(
'@ip' => ip_address(),
)));
drupal_goto('admin/config/people/ip-ranges');
}