You are here

function access_filter_ip_match in Access Filter 7

Check ip address is match to filter condition.

Parameters

string $pattern: A string of pattern.

string $ip: A string of IP address to check.

Return value

bool Boolean TRUE if $ip matches to $pattern.

2 calls to access_filter_ip_match()
AccessFilterResrictionTestCase::assertIpAddressMatching in ./access_filter.test
Check IP address matching works correctly.
access_filter_check_access in ./access_filter.module
Check the access matches to filter.

File

./access_filter.module, line 377
Allows users to manage access filters.

Code

function access_filter_ip_match($pattern, $ip) {
  if ($pattern == '*') {
    return TRUE;
  }
  $ip_long = ip2long($ip);

  // Check as 2 IP address range format.
  $patterns = explode('-', $pattern);
  if (isset($patterns[1])) {
    return $ip_long >= ip2long($patterns[0]) && $ip_long <= ip2long($patterns[1]);
  }

  // Check as single IP address and subnet format.
  $check = explode('/', $pattern);
  if (!isset($check[1])) {
    $check[1] = 32;
  }
  $network_long = ip2long($check[0]);
  $mask_long = bindec(str_repeat('1', $check[1]) . str_repeat('0', 32 - $check[1]));
  return ($ip_long & $mask_long) == $network_long;
}