<?php

// geolocation.php

require_once('continents.php');
require_once('countries.php');
require_once('geoipcity.inc');
require_once('geoipregionvars.php');

$IP2C_NULL_GEOLOCATION = array(
	"countryCode2"     => "Unknown",
	"countryCode3"     => "Unknown",
	"continentCode"    => "Unknown",
	"continentName"    => "Unknown",
	"countryName"      => "Unknown",
	"regionName"       => "Unknown",
	"cityName"         => "Unknown",
	"cityLatitude"     => 0,
	"cityLongitude"    => 0,
	"countryLatitude"  => 0,
	"countryLongitude" => 0
);

function ip2c_geolocation($ip)
{
	global $IP2C_CONTINENT, $IP2C_CONTINENT_NAME;
	global $IP2C_COUNTRY_LOCATION, $GEOIP_REGION_NAME;
	global $IP2C_NULL_GEOLOCATION;

	$geolocation = $IP2C_NULL_GEOLOCATION;
	
	$gi = geoip_open(dirname(__FILE__)."/data/geocity.dat", GEOIP_STANDARD);
	
	if ($record = geoip_record_by_addr($gi, $ip))
	{
		$geolocation["continentCode"]    = $IP2C_CONTINENT[$record->country_code];
		$geolocation["continentName"]    = $IP2C_CONTINENT_NAME[$IP2C_CONTINENT[$record->country_code]];
		$geolocation["continent"]		 = $IP2C_CONTINENT[$record->country_code];
	 	$geolocation["countryCode2"]     = $record->country_code;
		$geolocation["countryCode3"]     = $record->country_code3;
		$geolocation["countryName"]      = $record->country_name;
		$geolocation["regionName"]       = $GEOIP_REGION_NAME[$record->country_code][$record->region];
		$geolocation["cityName"]         = $record->city;
		$geolocation["cityLatitude"]     = $record->latitude;
		$geolocation["cityLongitude"]    = $record->longitude;
		$geolocation["countryLatitude"]  = $IP2C_COUNTRY_LOCATION[$record->country_code][0];
		$geolocation["countryLongitude"] = $IP2C_COUNTRY_LOCATION[$record->country_code][1];
		if ($geolocation["regionName"] == "")
		{
			$geolocation["regionName"] = "Unknown";
		}
		if ($geolocation["cityName"] == "")
		{
			$geolocation["cityName"] = "Unknown";
		}
	}
	geoip_close($gi);
	return $geolocation;
}
?>