01 <?php
02     include "/lib/GeoIP/geoip.inc";
03 
04 # Functions
05 
06     function GetCountryCode($IP) {
07         $gi = geoip_open("/lib/GeoIP/GeoIP.dat", GEOIP_STANDARD);
08 
09         $code = geoip_country_code_by_addr($gi, $IP);
10 
11         geoip_close($gi);
12 
13         return $code;
14     }
15 
16     function GetWhoisCIDR($IP) {
17         exec("whois $IP", $lines);
18 
19         foreach($lines as $line) {
20             if(preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $line, $matches)) {
21                 $cidr = $matches[1] . "/" . (int)(32 - log((ip2long($matches[2]) + 1 - ip2long($matches[1])), 2));
22             }
23         }
24 
25         return $cidr;
26     }
27 
28 # Main (Example)
29 
30     $_VARIABLE{'Country'} = GetCountryCode($_VARIABLE{'TrueIP'});
31 
32     if(preg_match('/RU|CN|HK|TW|KP|KR/i', $_VARIABLE{'Country'})) {
33         $_VARIABLE{'CIDR'} = GetWhoisCIDR($_VARIABLE{'TrueIP'});
34         UpdateSpamDataFile();
35         UpdateHtaccessFile();
36     }
37 ?>