01 <?php
02     function ip2cidr($ip_start, $ip_end) {
03         if(long2ip(ip2long($ip_start)) != $ip_start || long2ip(ip2long($ip_end)) != $ip_end) {
04             return NULL;
05         }
06 
07         $ipl_start = (int)ip2long($ip_start);
08         $ipl_end = (int)ip2long($ip_end);
09 
10         if($ipl_start > 0 && $ipl_end < 0) {
11             $delta = ($ipl_end + 4294967296) - $ipl_start;
12         } else {
13             $delta = $ipl_end - $ipl_start;
14         }
15 
16         $netmask = str_pad(decbin($delta), 32, "0", "STR_PAD_LEFT");
17 
18         if(ip2long($ip_start) == 0 && substr_count($netmask, "1") == 32) {
19             return "0.0.0.0/0";
20         }
21         if($delta < 0 || ($delta > 0 && $delta % 2 == 0)) {
22             return NULL;
23         }
24 
25         for($mask = 0; $mask < 32; $mask++) {
26             if($netmask[$mask] == 1) {
27                 break;
28             }
29         }
30 
31         if(substr_count($netmask, "0") != $mask) {
32             return NULL;
33         }
34 
35         return "$ip_start/$mask";
36     }
37 
38     function GetWhoisInfo($Ip) {
39         exec("whois $Ip", $output);
40         $WhoisInfoCnt = count($output);
41 
42         for($cnt = 0; $cnt < $WhoisInfoCnt; $cnt++) {
43             if(preg_match('/inetnum:[ \t]+(\d+\.\d+\.\d+\.\d+)[ \t]-[ \t](\d+\.\d+\.\d+\.\d+)/i', $output[$cnt], $match)) {
44                 $whois{'ip2cidr'} = ip2cidr($match[1], $match[2]);
45             }
46             if(preg_match('/country:[ \t]+(\S+)/i', $output[$cnt], $match)) {
47                 $whois{'country'} = strtolower($match[1]);
48             }
49         }
50 
51         if(!$whois{'ip2cidr'}) {
52             $mask0 = 0;
53             for ($cnt = 0; $cnt < $WhoisInfoCnt; $cnt++) {
54                 if(preg_match('/[ \t]+(\d+\.\d+\.\d+\.\d+)[ \t]-[ \t](\d+\.\d+\.\d+\.\d+)/', $output[$cnt], $match)) {
55                     $ip2cidr = ip2cidr($match[1], $match[2]);
56                     list($ip_start, $mask) = explode("/", $ip2cidr);
57                     if($mask > $mask0) {
58                         $whois{'ip2cidr'} = $ip2cidr;
59                         $mask0 = $mask;
60                     }
61                 }
62             }
63         }
64 
65         return $whois;
66     }
67 
68     function UpdateSpamTable() {
69         global $_VARIABLE;
70         $DBHost = "(database-hostname)";
71         $DBUser = "(database-username)";
72         $DBPassword = "(database-password)";
73         $Database = "(database-name)";
74 
75         // Read Spam Table File
76         if(!$SpamTableDataHandle = fopen("{$_VARIABLE{'SpamTableDat'}}", "r")) {
77             die("fopen(ERROR): {$_VARIABLE{'SpamTableDat'}}");
78         }
79 
80         $SpamTableCnt = 0;
81         while($line = fgets($SpamTableDataHandle)) {
82             list($ip, $host, $atime, $flag, $ip2cidr, $country) = preg_split("/[\t\n]/", $line);
83 
84             $SpamTable[$SpamTableCnt]{'ip'} = $ip;
85             $SpamTable[$SpamTableCnt]{'host'} = $host;
86             $SpamTable[$SpamTableCnt]{'atime'} = $atime;
87             $SpamTable[$SpamTableCnt]{'flag'} = 0;
88             $SpamTable[$SpamTableCnt]{'ip2cidr'} = $ip2cidr;
89             $SpamTable[$SpamTableCnt]{'country'} = $country;
90             $SpamTableCnt++;
91         }
92 
93         fclose($SpamTableDataHandle);
94 
95         // Read Spam Data from SQL server
96         if(!$link = mysql_connect($DBHost, $DBUser, $DBPassword)) {
97             die("mysql_connect(ERROR): " . mysql_error());
98         }
99         if(!mysql_select_db($Database)) {
100             die("mysql_select_db(ERROR): " . mysql_error());
101         }
102         if(!$result = mysql_query("SELECT comment_ip, comment_created_on FROM mt_comment WHERE comment_junk_status = '-1' ORDER BY comment_created_on ASC")) {
103             die("mysql_query(ERROR): " . mysql_error());
104         }
105 
106         while($line = mysql_fetch_row($result)) {
107             $flag = 0;
108             for($cnt = 0; $cnt < $SpamTableCnt; $cnt++) {
109                 if(!strcmp($SpamTable[$cnt]{'ip'}, $line[0])) {
110                     $flag = 1;
111                     break;
112                 }
113             }
114             if(!$flag) {
115                 $SpamTable[$SpamTableCnt]{'ip'} = $line[0];
116                 $SpamTable[$SpamTableCnt]{'host'} = gethostbyaddr($line[0]);
117                 $SpamTable[$SpamTableCnt]{'atime'} = str_replace("-", ".", $line[1]);
118                 $SpamTable[$SpamTableCnt]{'flag'} = 1;
119                 $whois = GetWhoisInfo($line[0]);
120                 $SpamTable[$SpamTableCnt]{'ip2cidr'} = $whois{'ip2cidr'};
121                 $SpamTable[$SpamTableCnt]{'country'} = $whois{'country'};
122                 $SpamTableCnt++;
123             }
124         }
125 
126         mysql_close($link);
127 
128         // Backup Spam Table File
129         if(!rename("{$_VARIABLE{'SpamTableDat'}}","{$_VARIABLE{'SpamTableBak'}}")) {
130             die("rename(ERROR): {$_VARIABLE{'SpamTableDat'}}");
131         }
132 
133         // Write Spam Table File
134         SetLockFile($_VARIABLE{'AcLock'});
135 
136         if(!$SpamTableDataHandle = fopen("{$_VARIABLE{'SpamTableDat'}}", "w")) {
137             die("fopen(ERROR): {$_VARIABLE{'SpamTableDat'}}");
138         }
139         flock($SpamTableDataHandle, LOCK_EX);
140 
141         for($cnt = 0; $cnt < $SpamTableCnt; $cnt++) {
142             fprintf($SpamTableDataHandle, "%s\t%s\t%s\t%s\t%s\t%s\n", $SpamTable[$cnt]['ip'], $SpamTable[$cnt]['host'], $SpamTable[$cnt]['atime'], $SpamTable[$cnt]['flag'], $SpamTable[$cnt]['ip2cidr'], $SpamTable[$cnt]['country']);
143         }
144 
145         flock($SpamTableDataHandle, LOCK_UN);
146         fclose($SpamTableDataHandle);
147 
148         ResetLockFile($_VARIABLE{'AcLock'});
149 
150         return;
151     }
152 ?>