SO 1-final
[specialops1.git] / thepurge.php
blob5813698f81a5dd3797d98234e40beda17f2aeb7c
1 <?php
2 if (mysql_result (mysql_query ('SELECT `lastpurge` FROM `misc2`'), 0) != gmdate ('Y-m-d') || defined ('FORCE_PURGE'))
4 // user purge
5 $result = mysql_query ('SELECT `cookies`, `level`, `userid`, `lastsec` FROM `users` ORDER BY `userid` ASC');
6 while ($myrow = mysql_fetch_row ($result))
8 switch ($myrow[1])
10 case PENDING_EMAIL: case INACTIVE_USER: $timeout = 86400; break; //1 day
11 case SUSPENDED_USER: $timeout = 7776000; break; //90 days
12 case KOS: case BANNED_USER: $timeout = 86400*120; break;
13 default: $timeout = 31536000; //year
15 if (($myrow[3] < time() - $timeout) && mysql_result (mysql_query ('SELECT COUNT(*) FROM `msgmeta` WHERE `postedby` = '.$myrow[2]), 0) == 0)
16 mysql_query ('DELETE FROM `users` WHERE `userid` = '.$myrow[2].' LIMIT 1');
17 elseif (!mysql_result (mysql_query ('SELECT `nochange` FROM `levels` WHERE `levnum` = '.$myrow[1]), 0) && $myrow[0] >= 0) // in other word, if the level is changeable
19 $new_level = mysql_result (mysql_query ('SELECT `levnum` FROM `levels` WHERE `minaura` <= '.intval ($myrow[0]).' AND `nochange` = 0 ORDER BY `minaura` DESC LIMIT 1'), 0);
20 if ($new_level >= NEW_USER)
21 mysql_query ('UPDATE LOW_PRIORITY `users` SET `level` = '.intval ($new_level).' WHERE `userid` = '.$myrow[2]);
25 // Message purge
26 /* kill topics with no posts */
27 mysql_query ('DELETE FROM `topics` WHERE
28 (`topicid` NOT IN(SELECT `topic` FROM `msgmeta`)) OR
29 (`boardnum` NOT IN(SELECT `boardid` FROM `boards`))'
31 /* kill posts with no topic */
32 mysql_query ('DELETE FROM `msgmeta` WHERE
33 (`topic` NOT IN(SELECT `topicid` FROM `topics`)) OR
34 (`mesboard` NOT IN(SELECT `boardid` FROM `boards`)) OR
35 (`msgid` NOT IN(SELECT `messageid` FROM `messages`))'
37 mysql_query ('DELETE FROM `messages` WHERE `messageid` NOT IN(SELECT `msgid` FROM `msgmeta`)');
38 mysql_query ('OPTIMIZE TABLE `topics`, `messages`');
40 /* system cleanup */
41 mysql_query ('DELETE FROM `iplog` WHERE
42 (`actiontype` IN(\'message\', \'topic\')) OR
43 (`actiontype` IN(\'register\', \'resendpass\') AND `logsec` < (UNIX_TIMESTAMP() - 172800)) OR
44 (`actiontype` IN(\'login\', \'edituser\') AND `logsec` < (UNIX_TIMESTAMP() - 2592000))'
47 /* vim rape */
48 mysql_query ('UPDATE `users` SET `points` = (`points` - 1) WHERE `points` > -20 AND `level` >= '.NEW_USER.' AND `lastsec` >= (UNIX_TIMESTAMP() - 86400)');
50 /* closed accounts */
51 mysql_query ('UPDATE `users` SET `level` = '.CLOSED.' WHERE `level` = '.PENDING_CLOSE.' AND `lastsec` < (UNIX_TIMESTAMP() - 86400)');
53 /* activity thinger */
54 $myrow = mysql_result (mysql_query ('SELECT `mostactive` FROM `misc2`'), 0);
55 $myrow2 = mysql_result (mysql_query ('SELECT COUNT(*) FROM `msgmeta` WHERE `messsec` >= (UNIX_TIMESTAMP() - 86400)'), 0);
56 if ($myrow2 > $myrow)
57 mysql_query ('UPDATE `misc2` SET `mostactiveat` = '.(time() - 86400).', `mostactive` = '.$myrow2.'');
59 /* update purge run time */
60 mysql_query ('UPDATE `misc2` SET `lastpurge` = \''.gmdate ('Ymd').'\'');
61 mysql_query ('OPTIMIZE TABLE `users`, `iplog`, `systemnot`');