50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
// File name: geoip.inc.php
|
|
// Description: PHP subroutine to query GeoIP
|
|
// Date: 2004-11-21
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2004-2007 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Referenced subroutines
|
|
require_once "GeoIP/geoip.inc";
|
|
require_once "monica/cgiemu.inc.php";
|
|
require_once "monica/ipv4addr.inc.php";
|
|
|
|
// Settings
|
|
define("GEOIP_DAT", "/usr/share/GeoIP/GeoIP.dat");
|
|
define("GEOIP_CT_PRIVATE_NETWORK", "AA");
|
|
|
|
// geoiplookup: Query GeoIP for an IP
|
|
function geoiplookup($ip = null)
|
|
{
|
|
// Default to the client address
|
|
if (is_null($ip)) {
|
|
$ip = $_SERVER["REMOTE_ADDR"];
|
|
}
|
|
// Cache the handler
|
|
static $gi;
|
|
if (!isset($gi)) {
|
|
$gi = geoip_open(GEOIP_DAT, GEOIP_STANDARD);
|
|
}
|
|
// Look up in the GeoIP database
|
|
$ct = geoip_country_code_by_addr($gi, $ip);
|
|
if ($ct != "") {
|
|
return $ct;
|
|
}
|
|
// Check if it is in a private network
|
|
if ( ipv4_in_network("127.0.0.1/8", $ip)
|
|
|| ipv4_in_network("10.0.0.0/8", $ip)
|
|
|| ipv4_in_network("172.16.0.0/12", $ip)
|
|
|| ipv4_in_network("192.168.0.0/16", $ip)) {
|
|
return GEOIP_CT_PRIVATE_NETWORK;
|
|
}
|
|
// Not found at all
|
|
return null;
|
|
}
|
|
|
|
?>
|