You are here

function cloudflare_ip_address_in_range in CloudFlare 7.2

Given an IP range like 8.8.8.0/24, check to see if an IP address is in it

1 call to cloudflare_ip_address_in_range()
cloudflare_boot in ./cloudflare.module
Implementation of hook_boot().

File

./cloudflare.module, line 204

Code

function cloudflare_ip_address_in_range($checkip, $range = FALSE) {
  if ($range === FALSE) {
    $range = cloudflare_ip_ranges();
    if ($range === FALSE) {

      // unable to determine cloudflare IP ranges
      return FALSE;
    }
  }
  if (is_array($range)) {
    foreach ($range as $ip_range) {
      if (!empty($ip_range) && cloudflare_ip_address_in_range($checkip, $ip_range)) {
        return TRUE;
      }
    }
    return FALSE;
  }
  @(list($ip, $len) = explode('/', $range));
  if (($min = ip2long($ip)) !== false && !is_null($len)) {
    $clong = ip2long($checkip);
    $max = $min | (1 << 32 - $len) - 1;
    if ($clong > $min && $clong < $max) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
}