<?php

/*
	Scan through the entire stats database and update all country details.
	This is ONLY intended to be run ONCE, and only if you have a database
	that clearly contains inaccurate country data.
	
	This is definitely NOT intended to be run on a regular basis, as IP
	ranges may change over time, and this will overwrite any existing
	country data.
	
	Run this to clean up the existing data, then leave things running as
	normal, download the latest country database from MaxMind once per
	month.
*/

include("geoip.inc");

$gi = geoip_open("##GEOIP_DATABASE##",GEOIP_STANDARD);

// eprintstats db
$sqlserver = 'localhost';
$sqluser = 'eprintstatspriv';
$sqlpass = 'AuldGrizzel';
$sqldatabase = 'eprintstats';

/*
IP address ranges for your local Intranet(s). You can have multiple
ranges of IP addresses, each with a different "country name", so that
they will appear as separate entries in the by country stats pages. Note
that all sets are assigned the country code "T5", so they will all use
the flag icon for your local installation. If this isn't what you want,
you'll have to hack this yourself :)
*/
$local_IPs = array(
	'Repository Admin' => array(
		ip2long('139.80.75.110'),  // Nigel @ Uni
		ip2long('60.234.209.74'),  // Nigel @ home
		ip2long('139.80.92.138'),  // Monica & Jeremy
		ip2long('139.80.92.151'),  //   @ Uni
		ip2long('203.89.162.155'), // Monica @ home
		ip2long('139.80.81.50'),   // eprints.otago.ac.nz
	),
	'Otago Intranet' => array(
		array(
			'lower' => ip2long('139.80.0.0'),
			'upper' => ip2long('139.80.127.255'),
		),
	),
);

###########################################
##
## No configuration required below here.
##
###########################################

$connect = mysql_connect ($sqlserver,$sqluser,$sqlpass);
$db = mysql_select_db($sqldatabase,$connect) or die("Could not connect");

$query = "select id, ip, country_code from view";
$result = mysql_query($query, $connect);
while ($row = mysql_fetch_assoc($result))
{
	$id = $row['id'];
	$ip = $row['ip'];
	$stored_country = $row['country_code'];
	$ip_long = ip2long($ip);

	/* 
		Determine country code and name.
		Check whether the IP number falls into any of the local
		intranet ranges. If so, then use that.
	*/
	$ip_long = ip2long($ip);
	$found_country = FALSE;
	foreach ($local_IPs as $name => $addresses)
	{
		foreach ($addresses as $ip_range)
		{
			if (is_array($ip_range)) // check against lower/upper bounds
			{
				$found_country = (($ip_long >= $ip_range['lower']) 
					&& ($ip_long <= $ip_range['upper']));
				break;
			}
			else if (is_long($ip_range)) // data type sanity check
			{
				$found_country = ($ip_long == $ip_range);
				break;
			}
			else // something is seriously broken, ignore this entry
			{
				print "Unsupported data type " . gettype($ip_range) .
					" (value " . $ip_range .
					") in \$local_IPs (expected long).\n";
				continue; 
			}
		}
		
		if ($found_country)
		{
			$country_code = 'T5';
			$country_name = $name;
			break;
		}
	}
	
	// Otherwise, fall back to GeoIP.
	if (!$found_country)
	{
		$country_code = geoip_country_code_by_addr($gi, $ip);
		$country_name = geoip_country_name_by_addr($gi, $ip);
	}
	// end NJS 2005-12-16

	// Only update the row if its country has changed.
	if ($stored_country != $country_code)
	{
		$update = "update view set country_code='" . $country_code . "', country_name='" . $country_name . "' where id=" . $id;
		$result2 = mysql_query($update, $connect);
	}
}

	/*
		Keep track of where we are. Should avoid duplication of results
		if the script is run more than once on the same log file
	*/

$query = "INSERT into lastproc (lastproc) values('".$request_date."')";
$result = mysql_query($query,$connect);

#print "Records counted: $counter\n";
#print "Last count: $request_date\n";
mysql_close($connect);

?>

