Beispiel #1
0
 function getCountryIdByIP($ip)
 {
     static $cache = array();
     $ipf = fbGeoIP_Free_Binary::_formatIP4address($ip);
     if (!$ipf) {
         return false;
     }
     /*		
     		if (isset($cache[$ipf])) {
     			return $cache[$ipf];
     		}
     */
     // \todo make a parameter
     $file = FREEBEER_BASE . '/etc/geo/geo-ips-binary.txt';
     if (!is_file($file)) {
         trigger_error(sprintf("File not found: '%s'", $file));
         return false;
     }
     $bsf =& new fbBinarySearch_File($file);
     $bsf->setRecordLength(10);
     $found = $bsf->search($ipf);
     $midval = $bsf->getRecordNumber();
     $rec_len = $bsf->getRecordLength();
     if ($found === false) {
         return false;
     }
     fbDebug::log(sprintf("ip=%s found=%s midval=%s rec_len=%s\n\n", $ip, $found, $midval, $rec_len));
     $a = unpack('C*', $found);
     $cc = sprintf('%c%c', $a[9], $a[10]);
     $ips = sprintf('%d.%d.%d.%d', $a[1], $a[2], $a[3], $a[4]);
     $ipe = sprintf('%d.%d.%d.%d', $a[5], $a[6], $a[7], $a[8]);
     if ($ipf < substr($found, 0, 4) || $ipf > substr($found, 4, 4)) {
         trigger_error(sprintf('Unexpected result: \'%s\' is not between \'%s\' and \'%s\'', $ip, $ips, $ipe), E_USER_NOTICE);
         return false;
     }
     //		$cache[$ipf] = $cc;
     return $cc;
 }