<?php

include('geoipcity.inc');

$gi = geoip_open('/usr/local/share/GeoIP/GeoLiteCity.dat',GEOIP_STANDARD);

$connect = mysql_pconnect ('localhost','eprintstatspriv','AuldGrizzel');
$db = mysql_select_db('eprintstats',$connect) or die("Could not connect");

$query = "SELECT ip, COUNT(*) AS count FROM view WHERE (view_type = 'download') GROUP BY ip ORDER BY count DESC LIMIT 500";
$result = mysql_query($query,$connect);

if (mysql_num_rows($result) > 0)
{
	$i = 0;
	while ($row = mysql_fetch_assoc($result))
	{
		$ip = $row["ip"];
		$count = $row["count"];
		
		$location = GeoIP_record_by_addr($gi, $ip);
		
		if (isset($location->latitude))
		{
			$lat = $location->latitude;
			$long = $location->longitude;
			$city = $location->city;
			
			if ($city == '') $city = 'Unknown';

			$cities[$city]['lat'] = $lat;
			$cities[$city]['long'] = $long;
			if (!isset($cities[$city]['count'])) $cities[$city]['count'] = 0;
			$cities[$city]['count'] += $count;
			
		}
	}

	print "<markers>\n";
	foreach ($cities as $city => $loc)
	{
		print '<marker lat="' .
			$loc['lat'] .
			'" lng="' .
			$loc['long'] .
			'" city="' .
			$city .
			'" num="' .
			$loc['count'] .
			'" />' . "\n";
	}
	print "</markers>\n";

}

?>
