geo.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 646 行 · 第 1/2 页
PHP
646 行
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Graeme Merrall <graeme@inetix.com.au> |
// | |
// +----------------------------------------------------------------------+
//
// $Id: Geo.php 6819 2007-06-20 13:09:21Z kevin_fourie $
require_once 'PEAR.php';
require_once 'Cache/Function.php';
/**
* NetGeo - determine geographic information on an internet address
*
* Can accept input of an AS number, an IP address or a host name
* Input can be individual or an array of addresses
*
* $geo = new Net_Geo();
* $geo->getRecord("php.net");
* $geo->getRecord(array("php.net", "google.com"));
*
* Results returned are a single array of results if a string is passed in
* or in the case of an array, a multi-dim array with the as the key
*
* Query service type (CAIDA or localizer) is not available as a constructer
* to retain compatibility with existing CAIDA NetGeo classes (perl + java)
*
* @version 1.0
* @package NetGeo
* @author Graeme Merrall <graeme@inetix.com.au>
*/
define('NETGEO_INPUT_ERROR', 'INPUT_ERROR');
define('NETGEO_HTTP_ERROR', 'HTTP_ERROR');
define('NETGEO_NO_MATCH', 'NO MATCH');
define('NETGEO_NO_COUNTRY', 'NO_COUNTRY');
define('NETGEO_LIMIT_EXCEEDED', 'NETGEO_LIMIT_EXCEEDED');
class Net_Geo
{
/**
* Path to local cache file.
* Caching is compulsory to reduce load on CAIDA server
*
* @var string
* @access public
*/
var $cache_path = "/tmp/";
/**
* How long to wait befire rechecking cached entries in *days*
* This should be comething nice and high
*
* @var in
* @access public
*/
var $cache_ttl = 30;
/**
* CAIDA only
*
* Maximum length of time, in seconds, which will be allowed during a whois
* lookup by the NetGeo server.
* The actual default value is maintained by the server.
*
* @var int
* @access public
*/
var $default_timeout = 60;
/**
* CAIDA only
*
* Location of the default netgeo server
* If port not speicifed, defaults to 80
*
* @var string
* @access public
*/
var $default_server = "http://netgeo.caida.org/perl/netgeo.cgi";
/**
* localizer only
*
* Location of the localizer data file
*
* @var string
* @access public
*/
var $localizer_data = "./demo.csv";
/**
* Type of service to use. May be either 'caida' or 'localizer'
* Default is 'caida'
*
* @var string
@ @access private
*/
var $service;
/**
* Cache filename prefix
*
* @var string
* @access private
*/
var $cache_prefix = "netgeo";
/**
* CAIDA only
*
* User Agent string.
*
* @var string
* @access private
*/
var $useragent = "PHP/NetGeo";
/**
* CAIDA only
*
* Class version
*
* @var string
* @access private
*/
var $useragent_version = "1.0";
/**
* CAIDA only
*
* How many targets can be read in at once
* Should be enough for most everyone
*
* @var string
* @access private
*/
var $array_limit = 100;
/**
* Function cache object
*
* @var object
* @access private
*/
var $cache;
/**
* Name of global var for copying $this when calling function cache
* This is needed for the cache function to operate correctly
*
* @var string
* @access private
*/
var $netgeo_global = "netgeo_global";
/**
* Constructor
* Both $applicationName and $alternateServerUrl are for compatibility
* with the perl and java netgeo classes.
* I don't guarantee to use these variables
*
* @param string $applicationName Application using the NetGeo class.
* @param string $alternateServerUrl Alternate NetGeo server url
* @return bool
* @access public
*/
function Net_Geo($applicationName="", $alternateServerUrl="")
{
$this->applicationName = $applicationName;
$this->alternateServerUrl = $alternateServerUrl;
// init cache object
$this->cache = new Cache_Function('file',
array('cache_dir' => $this->cache_path,
'filename_prefix' => $this->cache_prefix
),
$this->cache_ttl * 86400
);
return true;
}
function setService($service = "caida") {
if ($service == "localizer") {
if (@localizer_read($this->localizer_data, FALSE) == FALSE) {
PEAR::raiseError("Can't read localizer data file ".$this->localizer_data);
return false;
}
} elseif ($service == "caida") {
// check to see if an alternate server URL is used
if (!empty($alternateServerUrl)) {
$this->default_server = $this->alternateServerUrl;
}
$this->useragent = sprintf("%s %s", $this->useragent,
$this->useragent_version
);
// set the custom user agent
if (!empty($applicationName)) {
// trim whitespace
$applicationName = trim($applicationName);
// also set the agent name
$this->useragent = sprintf("%s/%s", $this->applicationName,
$this->useragent
);
}
} else {
// return error
return new PEAR_Error("No service specified");
}
$this->service = $service;
return true;
}
/**
* Gets a complete record for an address
* Returns either a single or multidimentional arrray
* if input is a string or an array respectively
*
* @param mixed $target Single or list of addresses
* @return array
* @access public
*/
function getRecord($target)
{
return $this->_execute("getRecord", $target);
}
/**
* Returns the 2-letter ISO 3166 country code
* Returns NO_MATCH if the AS number has been looked up
* but nothing was found in the whois lookups.
* Returns NO_COUNTRY if the lookup returned a record
* but no country could be found.
* Returns an empty string if nothing was found in the database
*
* @param string $target single address
* @return array
* @access public
*/
function getCountry($target)
{
$result = $this->_execute("getCountry", $target);
if (is_array($result)) {
return $result["COUNTRY"];
}
return $result;
}
/**
* Returns an array with keys LAT, LONG, LAT_LONG_GRAN, and STATUS.
* Lat/Long will be (0,0) if the target has been looked up but there was no
* match in the whois lookups, or if no address could be parsed from the
* whois record, or if the lat/long for the address is unknown.
* Returns an empty string if nothing was found in the database
*
* @param string $target single address
* @return array
* @access public
*/
function getLatLong($target)
{
return $this->_execute("getLatLong", $target);
}
/**
* Included here to make the NetGeo class as similar as possible to
* the NetGeoClient.java interface.
* It's probably just as easy for the user to extract lat and long directly
* from the array.
*
* @param string $target single address
* @return double
* @access public
*/
function getLat($latLongRef)
{
if (is_array($latLongRef)) {
$lat = $latLongRef["LAT"];
} else {
$lat = 0;
}
return sprintf("%.2f", $lat);
}
/**
* Included here to make the NetGeo class as similar as possible to
* the NetGeoClient.java interface.
* It's probably just as easy for the user to extract lat and long directly
* from the array
*
* @param string $target single address
* @return double
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?