public function getLocation($info)
{
$ip = $this->getIpFromInfo($info);
$isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
$result = array();
$locationGeoIp = $this->getGeoIpInstance($key = 'loc');
if ($locationGeoIp) {
switch ($locationGeoIp->databaseType) {
case GEOIP_CITY_EDITION_REV0:
// city database type
// city database type
case GEOIP_CITY_EDITION_REV1:
case GEOIP_CITYCOMBINED_EDITION:
if ($isIPv6) {
$location = geoip_record_by_addr_v6($locationGeoIp, $ip);
} else {
$location = geoip_record_by_addr($locationGeoIp, $ip);
}
if (!empty($location)) {
$result[self::COUNTRY_CODE_KEY] = $location->country_code;
$result[self::REGION_CODE_KEY] = $location->region;
$result[self::CITY_NAME_KEY] = utf8_encode($location->city);
$result[self::AREA_CODE_KEY] = $location->area_code;
$result[self::LATITUDE_KEY] = $location->latitude;
$result[self::LONGITUDE_KEY] = $location->longitude;
$result[self::POSTAL_CODE_KEY] = $location->postal_code;
}
break;
case GEOIP_REGION_EDITION_REV0:
// region database type
// region database type
case GEOIP_REGION_EDITION_REV1:
if ($isIPv6) {
// NOTE: geoip_region_by_addr_v6 does not exist (yet?), so we
// return the country code and an empty region code
$location = array(geoip_country_code_by_addr_v6($locationGeoIp, $ip), '');
} else {
$location = geoip_region_by_addr($locationGeoIp, $ip);
}
if (!empty($location)) {
$result[self::COUNTRY_CODE_KEY] = $location[0];
$result[self::REGION_CODE_KEY] = $location[1];
}
break;
case GEOIP_COUNTRY_EDITION:
// country database type
if ($isIPv6) {
$result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip);
} else {
$result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip);
}
break;
default:
// unknown database type, log warning and fallback to country edition
Log::warning("Found unrecognized database type: %s", $locationGeoIp->databaseType);
if ($isIPv6) {
$result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip);
} else {
$result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip);
}
break;
}
}
// NOTE: ISP & ORG require commercial dbs to test. The code has been tested manually,
// but not by system tests.
$ispGeoIp = $this->getGeoIpInstance($key = 'isp');
if ($ispGeoIp) {
if ($isIPv6) {
$isp = geoip_name_by_addr_v6($ispGeoIp, $ip);
} else {
$isp = geoip_org_by_addr($ispGeoIp, $ip);
}
if (!empty($isp)) {
$result[self::ISP_KEY] = utf8_encode($isp);
}
}
$orgGeoIp = $this->getGeoIpInstance($key = 'org');
if ($orgGeoIp) {
if ($isIPv6) {
$org = geoip_name_by_addr_v6($orgGeoIp, $ip);
} else {
$org = geoip_org_by_addr($orgGeoIp, $ip);
}
if (!empty($org)) {
$result[self::ORG_KEY] = utf8_encode($org);
}
}
if (empty($result)) {
return false;
}
$this->completeLocationResult($result);
return $result;
}