01 <?php
02     $_VARIABLE{'SpamTableDat'} = "/(datapath)/spamtable.dat";
03     $_VARIABLE{'SpamTableBak'} = "/(datapath)/spamtable.bak";
04 
05     function UpdateSpamTable() {
06         global $_VARIABLE;
07         $DBHost = "(database-hostname)";
08         $DBUser = "(database-username)";
09         $DBPassword = "(database-password)";
10         $Database = "(database-name)";
11 
12         // Read Spam Table File
13         if(!$SpamTableDataHandle = fopen("{$_VARIABLE{'SpamTableDat'}}", "r")) {
14             die("fopen(ERROR): {$_VARIABLE{'SpamTableDat'}}");
15         }
16 
17         $SpamTableCnt = 0;
18         while($line = fgets($SpamTableDataHandle)) {
19             $word = preg_split("/[\t\n]/", $line);
20 
21             $SpamTable[$SpamTableCnt]{'ip'} = $word[0];
22             $SpamTable[$SpamTableCnt]{'host'} = $word[1];
23             $SpamTable[$SpamTableCnt]{'atime'} = $word[2];
24             $SpamTable[$SpamTableCnt]{'flag'} = 0;
25             $SpamTableCnt++;
26         }
27 
28         fclose($SpamTableDataHandle);
29 
30         // Read Spam Data from SQL server
31         if(!$link = mysql_connect($DBHost, $DBUser, $DBPassword)) {
32             die("mysql_connect(ERROR): " . mysql_error());
33         }
34         if(!mysql_select_db($Database)) {
35             die("mysql_select_db(ERROR): " . mysql_error());
36         }
37         if(!$result = mysql_query("SELECT comment_ip, comment_created_on FROM mt_comment WHERE comment_junk_status = '-1' ORDER BY comment_created_on ASC")) {
38             die("mysql_query(ERROR): " . mysql_error());
39         }
40 
41         while($line = mysql_fetch_row($result)) {
42             $flag = 0;
43             for($cnt = 0; $cnt < $SpamTableCnt; $cnt++) {
44                 if(!strcmp($SpamTable[$cnt]{'ip'}, $line[0])) {
45                     $flag = 1;
46                     break;
47                 }
48             }
49             if(!$flag) {
50                 $SpamTable[$SpamTableCnt]{'ip'} = $line[0];
51                 $SpamTable[$SpamTableCnt]{'host'} = gethostbyaddr($line[0]);
52                 $SpamTable[$SpamTableCnt]{'atime'} = $line[1];
53                 $SpamTable[$SpamTableCnt]{'flag'} = 1;
54                 $SpamTableCnt++;
55             }
56         }
57 
58         mysql_close($link);
59 
60         // Backup Spam Table File
61         if(!copy("{$_VARIABLE{'SpamTableDat'}}","{$_VARIABLE{'SpamTableBak'}}")) {
62             die("copy(ERROR): {$_VARIABLE{'SpamTableDat'}}");
63         }
64 
65         // Write Spam Table File
66         SetLockFile($_VARIABLE{'AcLock'});
67 
68         if(!$SpamTableDataHandle = fopen("{$_VARIABLE{'SpamTableDat'}}", "a")) {
69             die("fopen(ERROR): {$_VARIABLE{'SpamTableDat'}}");
70         }
71         flock($SpamTableDataHandle, LOCK_EX);
72 
73         for($cnt = 0; $cnt < $SpamTableCnt; $cnt++) {
74             if($SpamTable[$cnt]{'flag'}) {
75                 fprintf($SpamTableDataHandle, "%s\t%s\t%s\n", $SpamTable[$cnt]['ip'], $SpamTable[$cnt]['host'], $SpamTable[$cnt]['atime']);
76                 printf("<div>Write [%s] [%s] [%s]</div>", $SpamTable[$cnt]['ip'], $SpamTable[$cnt]['host'], $SpamTable[$cnt]['atime']);
77             }
78         }
79 
80         flock($SpamTableDataHandle, LOCK_UN);
81         fclose($SpamTableDataHandle);
82 
83         ResetLockFile($_VARIABLE{'AcLock'});
84 
85         return;
86     }
87 ?>