You are here

private function AutobanController::whitelistIp in Automatic IP ban (Autoban) 8

Is IP address in whitelist?

Parameters

string $ip: IP address for check.

Return value

bool IP address in whitelist.

1 call to AutobanController::whitelistIp()
AutobanController::canIpBan in src/Controller/AutobanController.php
Check IP address for ban.

File

src/Controller/AutobanController.php, line 396

Class

AutobanController
Provides an Autoban functional.

Namespace

Drupal\autoban\Controller

Code

private function whitelistIp($ip) {
  $autoban_whitelist = $this
    ->config('autoban.settings')
    ->get('autoban_whitelist');
  if (!empty($autoban_whitelist)) {
    $real_host = gethostbyaddr($ip);
    $autoban_whitelist_arr = explode("\n", $autoban_whitelist);
    foreach ($autoban_whitelist_arr as $whitelist_ip) {
      $whitelist_ip = trim($whitelist_ip);
      if (empty($whitelist_ip)) {
        continue;
      }

      // Block comment.
      if (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]);
      }
      $whitelist_ip_arr = explode('/', $whitelist_ip);

      // CIDR match.
      if (count($whitelist_ip_arr) > 1) {
        $in_list = $this
          ->cidrMatch($ip, $whitelist_ip_arr[0], (int) $whitelist_ip_arr[1]);
      }
      else {
        $in_list = $whitelist_ip == $ip;
      }
      if ($in_list) {
        return TRUE;
      }

      // Check for domain.
      if ($real_host) {
        $real_host_arr = explode($whitelist_ip, $real_host);
        if (count($real_host_arr) == 2 && empty($real_host_arr[1])) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}