|
<? Php
Class QQWry {
Var $ StartIP = 0;
Var $ EndIP = 0;
Var $ Country = '';
Var $ Local = '';
Var $ CountryFlag = 0;
// Identify the Country location
// 0x01, followed by a Country offset of 3 bytes without Local
// 0x02, followed by the Country offset in 3 bytes, followed by the Local
// Others, Country, Local, and Local have similar compression. Multiple references are allowed.
Var $ fp;
Var $ FirstStartIp = 0;
Var $ LastStartIp = 0;
Var $ EndIpOff = 0;
Function getStartIp ($ RecNo ){
$ Offset = $ this-> FirstStartIp + $ RecNo * 7;
@ Fseek ($ this-> fp, $ offset, SEEK_SET );
$ Buf = fread ($ this-> fp, 7 );
$ This-> EndIpOff = ord ($ buf [4]) + (ord ($ buf [5]) * 256) + (ord ($ buf [6]) * 256*256 );
$ This-> StartIp = ord ($ buf [0]) + (ord ($ buf [1]) * 256) + (ord ($ buf [2]) * 256*256) + (ord ($ buf [3]) * 256*256*256 );
Return $ this-> StartIp;
}
Function getEndIp (){
@ Fseek ($ this-> fp, $ this-> EndIpOff, SEEK_SET );
$ Buf = fread ($ this-> fp, 5 );
$ This-> EndIp = ord ($ buf [0]) + (ord ($ buf [1]) * 256) + (ord ($ buf [2]) * 256*256) + (ord ($ buf [3]) * 256*256*256 );
$ This-> CountryFlag = ord ($ buf [4]);
Return $ this-> EndIp;
}
Function getCountry (){
Switch ($ this-> CountryFlag ){
Case 1:
Case 2:
$ This-> Country = $ this-> getFlagStr ($ this-> EndIpOff + 4 );
// Echo sprintf ('endipoffset = (% x) ', $ this-> EndIpOff );
$ This-> Local = (1 = $ this-> CountryFlag )? '': $ This-> getFlagStr ($ this-> EndIpOff + 8 );
Break;
Default:
$ This-> Country = $ this-> getFlagStr ($ this-> EndIpOff + 4 );
$ This-> Local = $ this-> getFlagStr (ftell ($ this-> fp ));
}
}
Function getFlagStr ($ offset ){
$ Flag = 0;
While (1 ){
@ Fseek ($ this-> fp, $ offset, SEEK_SET );
$ Flag = ord (fgetc ($ this-> fp ));
If ($ flag = 1 | $ flag = 2 ){
$ Buf = fread ($ this-> fp, 3 );
If ($ flag = 2 ){
$ This-> CountryFlag = 2;
$ This-> EndIpOff = $ offset-4;
}
$ Offset = ord ($ buf [0]) + (ord ($ buf [1]) * 256) + (ord ($ buf [2]) * 256*256 );
}
Else {
Break;
}
}
If ($ offset <12)
Return '';
@ Fseek ($ this-> fp, $ offset, SEEK_SET );
Return $ this-> getStr ();
}
Function getStr ()
{
$ Str = '';
While (1 ){
$ C = fgetc ($ this-> fp );
// Echo "$ cn ";
If (ord ($ c [0]) = 0)
Break;
$ Str. = $ c;
}
// Echo "$ str n ";
Return $ str;
}
Function qqwry ($ dotip = ''){
If (! Is_string ($ dotip) | $ dotip = '') {return ;}
If (preg_match ("/^ 127/", $ dotip) {$ this-> Country = "Local Network"; return ;}
Elseif (preg_match ("/^ 192/", $ dotip) {$ this-> Country = "Lan"; return ;}
$ NRet;
$ Ip = $ this-> IpToInt ($ dotip );
$ This-> fp = fopen (_ QQWRY __, "rb ");
If ($ this-> fp = NULL ){
$ SzLocal = "OpenFileError ";
Return 1;
}
@ Fseek ($ this-> fp, 0, SEEK_SET );
$ Buf = fread ($ this-> fp, 8 );
$ This-> FirstStartIp = ord ($ buf [0]) + (ord ($ buf [1]) * 256) + (ord ($ buf [2]) * 256*256) + (ord ($ buf [3]) * 256*256*256 );
$ This-> LastStartIp = ord ($ buf [4]) + (ord ($ buf [5]) * 256) + (ord ($ buf [6]) * 256*256) + (ord ($ buf [7]) * 256*256*256 );
$ RecordCount = floor ($ this-> LastStartIp-$ this-> FirstStartIp)/7 );
If ($ RecordCount <= 1 ){
$ This-> Country = "FileDataError ";
Fclose ($ this-> fp );
Return 2;
}
$ RangB = 0;
$ RangE = $ RecordCount;
// Match...
While ($ RangB <$ RangE-1)
{
$ RecNo = floor ($ RangB + $ RangE)/2 );
$ This-> getStartIp ($ RecNo );
If ($ ip = $ this-> StartIp)
{
$ RangB = $ RecNo;
Break;
}
If ($ ip> $ this-> StartIp)
$ RangB = $ RecNo;
Else
$ RangE = $ RecNo;
}
$ This-> getStartIp ($ RangB );
$ This-> getEndIp ();
If ($ this-> StartIp <= $ ip) & ($ this-> EndIp >=$ ip )){
$ NRet = 0;
$ This-> getCountry ();
// This is not very good ..........
$ This-> Local = str_replace ("area error", "", $ this-> Local );
}
Else {
$ NRet = 3;
$ This-> Country = 'unknown ';
$ This-> Local = '';
}
Fclose ($ this-> fp );
$ This-> Country = preg_replace ("/(CZ88.NET) | (pure network)/", "LAN/unknown", $ this-> Country );
$ This-> Local = preg_replace ("/(CZ88.NET) | (pure network)/", "LAN/unknown", $ this-> Local );
///// // Check the values 0 and 3 of $ nRet. Then, comment out the following lines.
Return $ nRet;
// Return "$ this-> Country $ this-> Local"; # return the location and Country directly.
}
Function IpToInt ($ Ip ){
$ Array = explode ('.', $ Ip );
$ Int = ($ array [0] * 256*256*256) + ($ array [1] * 256*256) + ($ array [2] * 256) + $ array [3];
Return $ Int;
}
}
?>
|