l_definitions as $cidr => $server) { if ($this->_cidrMatch($cidr, $host)) { return $server; } } return $this->_defaultServer; } /** * Defined by ZendX_Whois_Adapter_Base * * @return string */ protected function _getDefaultDefinitionFile() { return (dirname(dirname(__FILE__)) . '/Definitions/Adapters/Cidr.xml'); } /** * Check an IP adress against a CIDR * * @param string $cidr * @param string $ip * @return boolean */ protected function _cidrMatch($cidr, $ip) { // Get the base and the bits from the CIDR list($base, $bits) = explode('/', $cidr); // Now split it up into it's classes list($a, $b, $c, $d) = explode('.', $base); // Now do some bit shifting/switching to convert to ints $i = ($a << 24) + ($b << 16) + ($c << 8) + $d; $mask = $bits == 0 ? 0: (~0 << (32 - $bits)); // Here's our lowest int $low = $i & $mask; // Here's our highest int $high = $i | (~$mask & 0xFFFFFFFF); // Now split the ip we're checking against up into classes list($a, $b, $c, $d) = explode('.', $ip); // Now convert the ip we're checking against to an int $check = ($a << 24) + ($b << 16) + ($c << 8) + $d; // If the ip is within the range, including highest/lowest values, // then it's witin the CIDR range if ($check >= $low && $check <= $high) { return true; } else { return false; } } }