<?php
################################################################################
# Project : ABG_IPDetails #
# File : ABG_IPDetails.cls.php #
# V1.1.0 02/12/2009 : Initial #
# (cy) G. BENABOU / ABG Soft PARIS FRANCE #
# #
# Classes to manage IP detailed information retrieval #
# #
#=== IMPORTS ==================================================================#
# None #
# #
################################################################################
###### E N V I R O N M E N T S E T I N G ######################################
define('K_IPInfoDB', 'ipinfodb.com');
define('K_IPInfoDBURL', 'http://'.K_IPInfoDB);
define('K_RGExt', 0xF00);
define('K_RGFatal', 0x100);
define('K_RGDNS', 0x200);
define('K_RGInfo', 0x300);
################################################################################
# Class ABG_IPDetails #
#=== PURPOSE ==================================================================#
# Do IP detailed information retieval #
# - Manage user entry : dotted IP or URL #
# - Do reverse query : host from IP or IP from host #
# - Permantly checks data validity (IP conformance, server answer...) #
# - Manage a very fine grained system of exceptions in case of error #
# - Build synthetic result table with : #
# o DNS information #
# o Detailed IP information provided by server like : #
# . Country, state, city ... #
# . Geographic data (latitude, longitude, Time Zone, ...) #
# .... #
#=== PROPERTIES ===============================================================#
# Public fields #
# str CurEntry : Current query entry (URL or dotted IP) #
# array SiteInfo : Hold the dtailed results #
#=== METHODS ==================================================================#
# object function __construct(str Entry=null) #
# - Create ABG_IPDetails object #
# - Check working environment and alert #
# - Manage the effective entry with : #
# o Param input #
# o GET CGI input #
# o Default value (Server URL) #
#..............................................................................#
# array function GetIPInfo(str Entry=null) #
# Retrieve site detailed informations #
# - Compute DNS information #
# o IP from Host, host from IP #
# o if LAN IP, substitute $REMOTE_ADDR data #
# - Ask designated server for details #
# - Parse XML content #
# - Store result in SiteInfo array and return it #
# #
################################################################################
Class ABG_IPDetails{
/*** Public Properties ******************************************************/
public $CurEntry = K_IPInfoDB;
public $SiteInfo = array( 'SiteSpec'=>null,
'SrvName' =>null,
'SrvIP' =>null
);
/*** Private Properties *****************************************************/
private $LocalIPS = array( array(0xC0A80000, 0xFFFF0000), // 192.168.0.0 - 192.168.255.255
array(0xAC100000, 0xFFF00000), // 172.16.0.0 - 172.31.255.255
array(0x0A000000, 0xFF000000), // 10.0.0.0 - 10.255.255.255
array(0x7F000001, 0xFFFFFFFF) // 127.0.0.1 - 127.0.0.1
);
### C O N S T R U C T O R ###################################################
/*** object function __construct() ******************************************
- Create the ABG_IPDetails object instance
- Check working environment and alert
*****************************************************************************/
public function __construct($_Entry=null){
if(!function_exists('shell_exec') || !function_exists('file_get_contents'))
throw new ABG_Except('', K_RGFatal + 0xF2);
if($_Entry==null){
if(isset($_GET['txtEntry'])){
$Entry_ = $_GET['txtEntry'];
$_Entry = empty($Entry_) ? $this->CurEntry : $Entry_;
$_GET['txtEntry'] = null;
} else
$_Entry = $this->CurEntry;
}
$this->CurEntry = $_Entry;
} // __construct
### P U B L I C M E T H O D S ##############################################
/*** mixed function GetIPInfo(str Entry) ************************************
Retrieve site detailed informations
- Ask designated server
- Parse the XML content
- Store result in SiteInfo array and return it
Paramss
- Entry : Domain/IP under question
*****************************************************************************/
public function GetIPInfo($_Entry=null){
$this->SiteSpec = isset($_Entry) ? $_Entry : $this->CurEntry;
$IPDetail_ = file_get_contents(K_IPInfoDBURL.'/ip_query.php?ip='.$this->SrvIP.'&output=xml');
if(!$IPDetail_)
throw new ABG_Except(K_IPInfoDBURL, K_RGInfo + 0x15);
preg_match_all('/<([a-zA-Z0-9].*)>(.*)<\/([a-zA-Z0-9].*?)>\n/', $IPDetail_, $DetailSS_);
if(count($DetailSS_[1])==0)
throw new ABG_Except(K_IPInfoDBURL, K_RGInfo + 0x15);
$DetailS_ = array_combine($DetailSS_[1], $DetailSS_[2]);
array_shift($DetailS_);
/// ksort($DetailS);
$this->SiteInfo = array_merge($this->SiteInfo, $DetailS_);
return($this->SiteInfo);
} // GetIPInfo
### P R I V A T E M E T H O D S ###########################################
/*** mixed function __Get(str Ppty) *****************************************
Returns properties values
- Ppty : property name
*****************************************************************************/
private function __Get($_Ppty){
switch($_Ppty){
case 'SrvIP' : // flow
case 'SrvName' : $Var_ = $this->SiteInfo[$_Ppty];
if($Var_)
return($Var_);
throw new ABG_Except($_Ppty, K_RGDNS + 0x17);
default : throw new ABG_Except($_Ppty, K_RGFatal + 0xF0);
}
}
/*** void function __Set(str Ppty, mixed Value) ******************************
Sets the property to the given value
- Ppty : property name
- Value : value to set
*****************************************************************************/
private function __Set($_Ppty, $_Value){
$_Value = trim($_Value);
switch($_Ppty){
case 'SiteSpec' : if(($this->SiteInfo['SiteSpec']<>$_Value)){
$this->CurEntry = $_Value;
$this->GetDNSInfo($_Value);
}
return;
default : throw new ABG_Except($_Ppty, K_RGFatal + 0xF1);
}
} // __Set
/*** void GetDNSInfo($_Entry) ***********************************************
Retrieve site DNS information to fill the result SiteInfo array
Params
- Entry : Thing (Domain or IP address) to get info about
*****************************************************************************/
private function GetDNSInfo($_Entry){
$_Entry = trim($_Entry);
$this->SiteInfo['SiteSpec'] = $_Entry;
$RecLevel = 1;
$this->GetDNSInfoRec($_Entry, $RecLevel);
} // GetDNSInfo
/*** void GetDNSInfoRec($_Entry) ********************************************
Retrieve elements for site DNS information
- Depending on input retrieve
. Name from address
. Address from Name
- Recurse if local adress to server remote address
- Return info in an array
Params
- Entry : Thing (Domain or IP address) to get info about
- RecLevel : to prevent infinite recursing on LANs
*****************************************************************************/
private function GetDNSInfoRec($_Entry, &$RecLevel){
$this->SiteInfo['SrvName'] = $_Entry;
$this->SiteInfo['SrvIP'] = $_Entry;
try {
if($this->IsLocalIP($_Entry) && (bool)$RecLevel--)
return($this->GetDNSInfoRec($_SERVER["REMOTE_ADDR"], $RecLevel));
else{
$Idx_ = 'SrvName';
$Code_ = 0x12;
$$Idx_ = @gethostbyaddr($_Entry);
$this->SiteInfo['SrvIP'] = gethostbyname($$Idx_);
}
} catch(Exception $Exc_){
$Idx_ = 'SrvIP';
$Code_ = 0x13;
$$Idx_ = gethostbyname($_Entry);
$this->SiteInfo['SrvName'] = @gethostbyaddr($$Idx_);
}
if($$Idx_==$_Entry){
$this->SiteInfo[$Idx_] = null;
throw new ABG_Except($_Entry, K_RGDNS + $Code_);
}
$this->SiteInfo[$Idx_] = $$Idx_;
} // GetDNSInfoRec
/*** bool function IsLocalIP(str IP) ****************************************
Checks an IP address
- Throw execption if not a valid IP address
- Return TRUE if adress in ranges
192.168.0.0 - 192.168.255.255
172.16.0.0 - 172.31.255.255
10.0.0.0 - 10.255.255.255
127.0.0.1 - 127.0.0.1
- else return FALSE
*****************************************************************************/
private function IsLocalIP($_IP){
$HexIP_ = IP2Long($_IP);
if(isset($_IP) && $HexIP_){
foreach($this->LocalIPS as $TempS_)
if((($HexIP_ & $TempS_[1])^$TempS_[0])==0)
return(true);
return(false);
}
throw new ABG_Except($_IP, K_RGFatal + 0x12);
} // IsLocalIP
} #===== End class ABG_IPDetails ===========================================
################################################################################
# Class ABG_Except extends Exception #
#=== PURPOSE ==================================================================#
# Enrich the basic exception mechanism #
#=== PROPERTIES ===============================================================#
# Computed read #
# - str ErrMsg : Formatted HTML error string depending on exception context #
#=== METHODS ==================================================================#
# Protected inherited #
# - str GetMessage(void) : message injected at exception construction #
# - int GetCode() : Used here to index the Error message table #
# #
################################################################################
Class ABG_Except extends Exception{
/*** Private Properties *****************************************************/
/// private $ErrStyle = 'background: #FFFFCC; border: 1px solid Navy; color=red;font: 14px sans-serif; width:600px;';
private $ErrStyle = 'color=#C02020;font: 14px sans-serif;';
private $ErrMsgS = array( 0x00 => "Oooops ?!?!",
0x12 => "Invalid IP address: \$Var",
0x13 => "Site \$Var unknown of DNS server",
0x15 => "No data from server \$Var",
0x17 => "\$Var data not available",
0xF0 => "No such a GET property : \$Var",
0xF1 => "No such a SET property : \$Var",
0xF2 => 'Function(s) missing in your installation; aborting',
0xF3 => "Object \$Var not available"
);
### P R I V A T E M E T H O D S ###########################################
/*** mixed function __Get(str Ppty) *****************************************
Returns properties values
- Ppty : property name
*****************************************************************************/
private function __Get($_Ppty){
switch($_Ppty){
case 'ErrMsg' : return($this->GetErrMsg($this->GetMessage(), $this->GetCode()));
default : throw new Excption("No such a GET property : <b>$_Ppty</b>");
}
} // __Get
/*** str function GetErrMsg(Var, Code) ***************************************
Builds formatted HTML string based on error context
*****************************************************************************/
private function GetErrMsg($Var, $_Code){
$Grp_ = $_Code & 0xFF00;
switch($Grp_){
case 0 : return($Var);
default : $Tra_ = array('$Var'=>'<b><u>$Var</u></b>');
$Res_ = $this->ErrMsgS[$_Code & 0xFF];
$Res_ = htmlentities($Res_);
$Res_ = strtr($Res_, $Tra_);
$Res_ = eval("return(\"$Res_\");");
}
return("<div style='$this->ErrStyle'>*** Error : $Res_</div>");
} // GetErrMsg(
} #===== End class ABG_Except =============================================
?>