View source  
  <?php
include_once DRUPAL_ROOT . '/includes/locale.inc';
define('IP_BAN_NOBAN', 0);
define('IP_BAN_READONLY', 1);
define('IP_BAN_BANNED', 2);
function ip_ban_permission() {
  return array(
    'ignore ip_ban' => array(
      'title' => t('Bypass ban'),
      'description' => t('Allow a user from a banned IP address or country to bypass read only or complete ban.'),
    ),
  );
}
function ip_ban_menu() {
  $items = array();
  $items['admin/config/ip_ban'] = array(
    'title' => 'IP Ban',
    'description' => 'Set IP addresses and/or countries to read only, or ban them entirely.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ip_ban_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'ip_ban.admin.inc',
  );
  return $items;
}
function ip_ban_theme() {
  $themes = array(
    'ip_ban_country_table' => array(
      'render element' => 'ip_ban_table',
    ),
  );
  return $themes;
}
function ip_ban_block_list_alter(&$blocks) {
  
  if (user_access('ignore ip_ban')) {
    return;
  }
  $banvalue = _ip_ban_set_ban_value();
  $disabled_blocks = variable_get('ip_ban_disabled_blocks');
  $disabled_block_list = array();
  if (!empty($disabled_blocks)) {
    $disabled_block_array = explode(PHP_EOL, $disabled_blocks);
    foreach ($disabled_block_array as $disabled_block) {
      $disabled_block_list[] = preg_replace('/\\s+/', '', $disabled_block);
    }
  }
  if (!empty($disabled_block_list)) {
    foreach ($blocks as $key => $block) {
      if ($block->status == 1) {
        $block_module_delta = $block->module . ',' . $block->delta;
        if (in_array($block_module_delta, $disabled_block_list)) {
          unset($blocks[$key]);
        }
      }
    }
  }
  _ip_ban_determine_action($banvalue);
}
function _ip_ban_set_ban_value() {
  
  if (drupal_is_cli() && function_exists('drush_main')) {
    return;
  }
  $test_ip = variable_get('ip_ban_test_ip', '');
  
  $ip = empty($test_ip) ? ip_address() : $test_ip;
  $country_code = ip2country_get_country($ip);
  
  $banvalue =& drupal_static(__FUNCTION__);
  if (!isset($banvalue)) {
    $banvalue = (int) variable_get('ip_ban_' . $country_code, IP_BAN_NOBAN);
    
    $readonly_ips = variable_get('ip_ban_readonly_ips', '');
    if (!empty($readonly_ips)) {
      $ip_readonly_array = explode(PHP_EOL, $readonly_ips);
      if (in_array($ip, $ip_readonly_array)) {
        $banvalue = IP_BAN_READONLY;
      }
    }
    
    $banned_ips = variable_get('ip_ban_additional_ips', '');
    if (!empty($banned_ips)) {
      $ip_ban_array = explode(PHP_EOL, $banned_ips);
      if (in_array($ip, $ip_ban_array)) {
        $banvalue = IP_BAN_BANNED;
      }
    }
  }
  return $banvalue;
}
function _ip_ban_determine_action($banvalue) {
  if ($banvalue === IP_BAN_READONLY) {
    $uri = check_plain(filter_xss($_GET['q']));
    if ($uri == 'user' || strpos($uri, 'user/') !== FALSE) {
      drupal_set_message(t(variable_get('ip_ban_readonly', 'You may not create an account, attempt to log in, or request a password change from your current location.')), 'error');
      drupal_goto(variable_get('ip_ban_readonly_path', ''));
    }
  }
  if ($banvalue === IP_BAN_BANNED) {
    
    $complete_ban_path = variable_get('ip_ban_completeban_path');
    if (!empty($complete_ban_path) && check_plain(filter_xss($_GET['q'])) != drupal_get_normal_path($complete_ban_path)) {
      drupal_goto($complete_ban_path);
    }
    else {
      drupal_set_message(t(variable_get('ip_ban_completeban', 'You may not view this site from your current location.')), 'error');
    }
  }
}