Function convertIp ($ ip ){
$ Return = '';
If (preg_match ("/^ d {1, 3}. d {1, 3}. d {1, 3}. d {1, 3} $/", $ ip )){
$ Iparray = explode ('.', $ ip );
If ($ iparray [0] = 10 | $ iparray [0] = 127 | ($ iparray [0] = 192 & $ iparray [1] = 168) | ($ iparray [0] = 172 & ($ iparray [1]> = 16 & $ iparray [1] <= 31 ))){
$ Return = '-LAN ';
} Elseif ($ iparray [0]> 255 | $ iparray [1]> 255 | $ iparray [2]> 255 | $ iparray [3]> 255 ){
$ Return = '-Invalid IP Address ';
} Else {
$ Ipfile = MOOPHP_ROOT. '/plugins/ipdata/wry. dat ';
If (! @ File_exists ($ ipfile )){
$ Return = convertIpFull ($ ip, $ ipfile );
}
}
}
Return $ return;
}
Function convertIpFull ($ ip, $ ipdatafile ){
If (! $ Fd = @ fopen ($ ipdatafile )){
Return '-Invalid IP data file ';
}
$ Ip = explode ('.', $ ip );
$ IpNum = $ ip [0] X 16777216 + $ ip [1] * 65536 + $ ip [2] * 256 + $ ip [3];
If (! ($ DataBegin = fread ($ fd, 4) |! ($ DataEnd = fread ($ fd, 4) return;
@ $ Ipbegin = implode ('', unpack ('L', $ DataBegin ));
If ($ ipbegin <0) $ ipbegin + = pow (2, 32 );
@ $ Ipend = implode ('', unpack ('L', $ DataEnd ));
If ($ ipend <0) $ ipend + = pow (2, 32 );
$ IpAllNum = ($ ipend-$ ipbegin)/7 + 1;
$ BeginNum = $ ip2num = $ ip1num = 0;
$ IpAddr1 = $ ipAddr2 = '';
$ EndNum = $ ipAllNum;
While ($ ip1num> $ ipNum | $ ip2num <$ ipNum ){
$ Middle = intval ($ EndNum + $ BeginNum)/2 );
Fseek ($ fd, $ ipbegin + 7 * $ Middle );
$ IpData1 = fread ($ fd, 4 );
If (strlen ($ ipData1) <4 ){
Fclose ($ fd );
Return '-System error ';
}
$ Ip1num = implode ('', unpack ('L', $ ipData1 ));
If ($ ip1num <0) $ ip1num + = pow (2, 32 );
If ($ ip1num> $ ipNum ){
$ EndNum = $ Middle;
Continue;
}
$ DataSeek = fread ($ fd, 3 );
If (strlen ($ DataSeek) <3 ){
Fclose ($ fd );
Return '-System error ';
}
$ DataSeek = implode ('', unpack ('L', $ DataSeek. chr (0 )));
Fseek ($ fd, $ DataSeek );
$ IpData2 = fread ($ fd, 4 );
If (strlen ($ ipData2) <4 ){
Fclose ($ fd );
Return '-System error ';
}
$ Ip2num = implode ('', unpack ('L', $ ipData2 ));
If ($ ip2num <0) $ ip2num + = pow (2, 32 );
If ($ ip2num <$ ipNum ){
If ($ Middle = $ BeginNum ){
Fclose ($ fd );
Return '-Unknown ';
}
$ BeginNum = $ Middle;
}
}
$ IpFlag = fread ($ fd, 1 );
If ($ ipFlag = chr (1 )){
$ IpSeek = fread ($ fd, 3 );
If (strlen ($ ipSeek) <3 ){
Fclose ($ fd );
Return '-System error ';
}
$ IpSeek = implode ('', unpack ('L', $ ipSeek. chr (0 )));
Fseek ($ fd, $ ipSeek );
$ IpFlag = fread ($ fd, 1 );
}
If ($ ipFlag = chr (2 )){
$ AddrSeek = fread ($ fd, 3 );
If (strlen ($ AddrSeek) <3 ){
Fclose ($ fd );
Return '-System error ';
}
$ IpFlag = fread ($ fd, 1 );
If ($ ipFlag = chr (2 )){
$ AddrSeek2 = fread ($ fd, 3 );
If (strlen ($ AddrSeek2) <3 ){
Fclose ($ fd );
Return '-System error ';
}
$ AddrSeek2 = implode ('', unpack ('L', $ AddrSeek2.chr (0 )));
Fseek ($ fd, $ AddrSeek2 );
} Else {
Fseek ($ fd,-1, SEEK_CUR );
}
While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ IpAddr2. = $ char;
$ AddrSeek = implode ('', unpack ('L', $ AddrSeek. chr (0 )));
Fseek ($ fd, $ AddrSeek );
While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ IpAddr1. = $ char;
} Else {
Fseek ($ fd,-1, SEEK_CUR );
While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ IpAddr1. = $ char;
$ IpFlag = fread ($ fd, 1 );
If ($ ipFlag = chr (2 )){
$ AddrSeek2 = fread ($ fd, 3 );
If (strlen ($ AddrSeek2) <3 ){
Fclose ($ fd );
Return '-System error ';
}
$ AddrSeek2 = implode ('', unpack ('L', $ AddrSeek2.chr (0 )));
Fseek ($ fd, $ AddrSeek2 );
} Else {
Fseek ($ fd,-1, SEEK_CUR );
}
While ($ char = fread ($ fd, 1 ))! = Chr (0 ))
$ IpAddr2. = $ char;
}
Fclose ($ fd );
If (preg_match ('/http/I', $ ipAddr2 )){
$ IpAddr2 = '';
}
$ Ipaddr = "$ ipAddr1 $ ipAddr2 ";
$ Ipaddr = preg_replace ('/CZ88.NET/is', '', $ ipaddr );
$ Ipaddr = preg_replace ('/^ s */is', '', $ ipaddr );
$ Ipaddr = preg_replace ('/s * $/is', '', $ ipaddr );
If (preg_match ('/http/I', $ ipaddr) | $ ipaddr = ''){
$ Ipaddr = '-Unknown ';
}
Return '-'. $ ipaddr;
}