You are here

function autoban_whitelist_ip in Automatic IP ban (Autoban) 7

Is IP address in whitelist

Parameters

string $ip: IP address for check.

Return value

bool IP address in whitelist.

2 calls to autoban_whitelist_ip()
autoban_can_banned in ./autoban.module
May IP address can be banned
autoban_test in ./autoban.admin.inc
Menu callback. Test autoban rule page.

File

./autoban.module, line 856
Main file for autoban module.

Code

function autoban_whitelist_ip($ip) {

  // Check whitelist by domain.
  $autoban_whitelist_domains = variable_get('autoban_whitelist_domains', '');
  if (!empty($autoban_whitelist_domains)) {
    $real_host = autoban_gethostbyaddr($ip);
    if ($real_host) {
      $autoban_whitelist_domains_arr = explode(",", $autoban_whitelist_domains);
      foreach ($autoban_whitelist_domains_arr as $whitelist_domain) {
        $whitelist_domain = trim($whitelist_domain);
        $real_host_arr = explode($whitelist_domain, $real_host);

        // Like crawl-66-249-66-1.googlebot.com
        if (count($real_host_arr) == 2 && empty($real_host_arr[1])) {
          return TRUE;
        }
      }
    }
  }

  // Check manual IP whitelist.
  $autoban_whitelist = _autoban_get_whitelist();
  if (!empty($autoban_whitelist)) {
    $autoban_whitelist_arr = explode("\n", $autoban_whitelist);
    foreach ($autoban_whitelist_arr as $whitelist_ip) {
      $whitelist_ip = trim($whitelist_ip);
      if (drupal_substr($whitelist_ip, 0, 1) == '#') {
        continue;
      }

      //Inline comment.
      $whitelist_ip_arr = explode('#', $whitelist_ip);
      if (count($whitelist_ip_arr) > 1) {
        $whitelist_ip = trim($whitelist_ip_arr[0]);
      }

      // Single IP check.
      $in_list = $whitelist_ip == $ip;

      // CIDR format check
      if (!$in_list) {
        $whitelist_ip_arr = explode('/', $whitelist_ip);
        if (count($whitelist_ip_arr) > 1) {
          $in_list = _autoban_cidr_match($ip, $whitelist_ip_arr[0], (int) $whitelist_ip_arr[1]);
        }
      }

      // IP range check.
      if (!$in_list) {
        $whitelist_ip_arr = explode('-', $whitelist_ip);
        if (count($whitelist_ip_arr) > 1) {
          $in_list = _autoban_range_match($ip, $whitelist_ip_arr[0], (int) $whitelist_ip_arr[1]);
        }
      }
      if (!$in_list) {
        $whitelist_ip_arr = explode('...', $whitelist_ip);
        if (count($whitelist_ip_arr) > 1) {
          $in_list = _autoban_range_match($ip, $whitelist_ip_arr[0], (int) $whitelist_ip_arr[1]);
        }
      }
      if ($in_list) {
        return TRUE;
      }
    }
  }
  return FALSE;
}