<?
require_once("functions.php");
function docleanup() {
global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $autoclean_interval;
set_time_limit(0);
ignore_user_abort(1);
do {
$res = mysql_query("SELECT id FROM torrents");
$ar = array();
while ($row = mysql_fetch_array($res)) {
$id = $row[0];
$ar[$id] = 1;
}
if (!count($ar))
break;
$dp = @opendir($torrent_dir);
if (!$dp)
break;
$ar2 = array();
while (($file = readdir($dp)) !== false) {
if (!preg_match('/^(\d+)\.torrent$/', $file, $m))
continue;
$id = $m[1];
$ar2[$id] = 1;
if (isset($ar[$id]) && $ar[$id])
continue;
$ff = $torrent_dir . "/$file";
unlink($ff);
}
closedir($dp);
if (!count($ar2))
break;
$delids = array();
foreach (array_keys($ar) as $k) {
if (isset($ar2[$k]) && $ar2[$k])
continue;
$delids[] = $k;
unset($ar[$k]);
}
if (count($delids))
mysql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")");
$res = mysql_query("SELECT torrent FROM peers GROUP BY torrent");
$delids = array();
while ($row = mysql_fetch_array($res)) {
$id = $row[0];
if (isset($ar[$id]) && $ar[$id])
continue;
$delids[] = $id;
}
if (count($delids))
mysql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")");
$res = mysql_query("SELECT torrent FROM files GROUP BY torrent");
$delids = array();
while ($row = mysql_fetch_array($res)) {
$id = $row[0];
if ($ar[$id])
continue;
$delids[] = $id;
}
if (count($delids))
mysql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
} while (0);
// START LOTTERY - THADON
$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];
if ($arr_config['enable'] == 1)
{
if (get_date_time() > $arr_config['end_date'])
{
if ($arr_config["ticket_amount_type"] == GB)
$arr_config['ticket_amount'] = 1024 * 1024 * 1024 * $arr_config['ticket_amount'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['ticket_amount'] = 1024 * 1024 * $arr_config['ticket_amount'];
$size = $arr_config['ticket_amount'];
if ($arr_config["ticket_amount_type"] == GB)
$arr_config['prize_fund'] = 1024 * 1024 * 1024 * $arr_config['prize_fund'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['prize_fund'] = 1024 * 1024 * $arr_config['prize_fund'];
$prize_fund = $arr_config['prize_fund'];
$total = mysql_num_rows(mysql_query("SELECT * FROM tickets"));
if ($arr_config["use_prize_fund"])
{
$pot = $prize_fund / $arr_config['total_winners'];
$res = mysql_query("SELECT user FROM tickets ORDER BY RAND() LIMIT $arr_config[total_winners]") or sqlerr();
$who_won = array();
$msg = sqlesc("Congratulations, You have won: <b>".mksize($pot)."</b>.<br /><br />This has been added to your upload amount<br /><br />Thanks for playing Lottery.");
while ($arr = mysql_fetch_assoc($res))
{
$res2 = mysql_query("SELECT modcomment FROM users WHERE id = $arr[user]") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);
$modcomment = $arr2['modcomment'];
$modcom = sqlesc("User won the lottery: " . mksize($pot) . " at " . get_date_time() . "\n" . $modcomment);
mysql_query("UPDATE users SET uploaded = uploaded + $pot, modcomment = $modcom WHERE id = $arr[user]") or sqlerr();
mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, $arr[user], NOW(), 'You have won the Lottery', $msg, 0)") or sqlerr(__FILE__, __LINE__);
$who_won[] = $arr['user'];
}
}
else
{
$pot = $total * $size / $arr_config['total_winners'];
$res = mysql_query("SELECT user FROM tickets ORDER BY RAND() LIMIT $arr_config[total_winners]") or sqlerr();
$who_won = array();
$msg = sqlesc("Congratulations, You have won: <b>".mksize($pot)."</b>.<br /><br />This has been added to your upload amount<br /><br />Thanks for playing Lottery.");
while ($arr = mysql_fetch_assoc($res))
{
$res2 = mysql_query("SELECT modcomment FROM users WHERE id = $arr[user]") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);
$modcomment = $arr2['modcomment'];
$modcom = sqlesc("User won the lottery: " . mksize($pot) . " at " . get_date_time() . "\n" . $modcomment);
mysql_query("UPDATE users SET uploaded = uploaded + $pot WHERE id = $arr[user]") or sqlerr();
mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, $arr[user], NOW(), 'You have won the Lottery', $msg, 0)") or sqlerr(__FILE__, __LINE__);
$who_won[] = $arr['user'];
}
}
$who_won = implode("|", $who_won);
$who_won_date = get_date_time();
$who_won_prize = $pot;
mysql_query("TRUNCATE TABLE tickets") or sqlerr(__FILE__, __LINE__);
if ($who_won != '')
{
mysql_query("UPDATE lottery_config SET value = '$who_won' WHERE name = 'lottery_winners'") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE lottery_config SET value = '$who_won_prize' WHERE name = 'lottery_winners_amount'") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE lottery_config SET value = '$who_won_date' WHERE name = 'lottery_winners_time'") or sqlerr(__FILE__, __LINE__);
}
mysql_query("UPDATE lottery_config SET value = '0' WHERE name = 'enable'") or sqlerr(__FILE__, __LINE__);
}
}
// END LOTTERY - THADON
$deadtime = deadtime();
mysql_query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME($deadtime)");
$deadtime -= $max_dead_torrent_time;
mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME($deadtime)");
$deadtime = time() - $signup_timeout;
mysql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME($deadtime) AND last_login < FROM_UNIXTIME($deadtime) AND last_access < FROM_UNIXTIME($deadtime)");
$torrents = array();
$res = mysql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder");
while ($row = mysql_fetch_assoc($res)) {
if ($row["seeder"] == "yes")
$key = "seeders";
else
$key = "leechers";
$torrents[$row["torrent"]][$key] = $row["c"];
}
$res = mysql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent");
while ($row = mysql_fetch_assoc($res)) {
$torrents[$row["torrent"]]["comments"] = $row["c"];
}
$fields = explode(":", "comments:leechers:seeders");
$res = mysql_query("SELECT id, seeders, leechers, comments FROM torrents");
while ($row = mysql_fetch_assoc($res)) {
$id = $row["id"];
$torr = $torrents[$id];
foreach ($fields as $field) {
if (!isset($torr[$field]))
$torr[$field] = 0;
}
$update = array();
foreach ($fields as $field) {
if ($torr[$field] != $row[$field])
$update[] = "$field = " . $torr[$field];
}
if (count($update))
mysql_query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = $id");
}
//delete inactive user accounts
$secs = 42*86400;
$dt = sqlesc(get_date_time(gmtime() - $secs));
$maxclass = UC_POWER_USER;
mysql_query("DELETE FROM users WHERE status='confirmed' AND class <= $maxclass AND last_access < $dt");
// lock topics where last post was made more than x days ago
$secs = 7*86400;
$res = mysql_query("SELECT topics.id FROM topics JOIN posts ON topics.lastpost = posts.id AND topics.sticky = 'no' WHERE " . gmtime() . " - UNIX_TIMESTAMP(posts.added) > $secs") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
mysql_query("UPDATE topics SET locked='yes' WHERE id=$arr[id]") or sqlerr(__FILE__, __LINE__);
//remove expired warnings
$res = mysql_query("SELECT id FROM users WHERE warned='yes' AND warneduntil < NOW() AND warneduntil <> '0000-00-00 00:00:00'") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
$dt = sqlesc(get_date_time());
$msg = sqlesc("Your warning has been removed. Please keep in your best behaviour from now on.\n");
while ($arr = mysql_fetch_assoc($res))
{
mysql_query("UPDATE users SET warned = 'no', warneduntil = '0000-00-00 00:00:00' WHERE id = $arr[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $arr[id], $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
}
}
//LEECHWARN USERS WITH LOW RATIO
$minratio = 0.4; // ratio < 0.4
$downloaded = 4*1024*1024*1024; // + 4 GB
$length = 2*7; // warn users until 2 weeks
$res = mysql_query("SELECT id FROM users WHERE class = 0 AND leechwarn = 'no' AND uploaded / downloaded < $minratio AND downloaded >= $downloaded") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
$dt = sqlesc(get_date_time());
$msg = sqlesc("You have been warned because of having low ratio. You need to get a ratio 0.6 before next 2 weeks or your account will be banned.");
$until = sqlesc(get_date_time(gmtime() + ($length*86400)));
while ($arr = mysql_fetch_assoc($res))
{
writecomment($arr[id],"LeechWarned by System - Low Ratio.");
mysql_query("UPDATE users SET leechwarn = 'yes', leechwarnuntil = $until WHERE id=$arr[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $arr[id], $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
}
}
//END//
//REMOVE LEECHWARNING
$minratio = 0.6; // ratio > 0.6
$res = mysql_query("SELECT id FROM users WHERE leechwarn = 'yes' AND uploaded / downloaded >= $minratio") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
$dt = sqlesc(get_date_time());
$msg = sqlesc("Your warning of low ratio ave been removed. We highly recommend you to keep a your ratio up to not be warned again.\n");
while ($arr = mysql_fetch_assoc($res))
{
writecomment($arr[id],"LeechWarning removed by System.");
mysql_query("UPDATE users SET leechwarn = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = $arr[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $arr[id], $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
}
}
//END//
//BAN USERS WITH LEECHWARNING EXPIRED
$dt = sqlesc(get_date_time()); // take date time
$res = mysql_query("SELECT id FROM users WHERE enabled = 'yes' AND leechwarn = 'yes' AND leechwarnuntil < $dt") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
while ($arr = mysql_fetch_assoc($res))
{
writecomment($arr[id],"Banned by System because of LeechWarning expired.");
mysql_query("UPDATE users SET enabled = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = $arr[id]") or sqlerr(__FILE__, __LINE__);
}
}
//END//
// promote power users
$limit = 25*1024*1024*1024;
$minratio = 1.05;
$maxdt = sqlesc(get_date_time(gmtime() - 86400*28));
$res = mysql_query("SELECT id FROM users WHERE class = 0 AND uploaded >= $limit AND uploaded / downloaded >= $minratio AND added < $maxdt") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
$dt = sqlesc(get_date_time());
$msg = sqlesc("Congratulations, you have been auto-promoted to [b]Power User[/b]. :)\nYou can now download dox over 1 meg and view torrent NFOs.\n");
while ($arr = mysql_fetch_assoc($res))
{
mysql_query("UPDATE users SET class = 1 WHERE id = $arr[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $arr[id], $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
}
}
// demote power users
$minratio = 0.95;
$res = mysql_query("SELECT id FROM users WHERE class = 1 AND uploaded / downloaded < $minratio") or sqlerr(__FILE__, __LINE__);
if (mysql_num_rows($res) > 0)
{
$dt = sqlesc(get_date_time());
$msg = sqlesc("You have been auto-demoted from [b]Power User[/b] to [b]User[/b] because your share ratio has dropped below $minratio.\n");
while ($arr = mysql_fetch_assoc($res))
{
mysql_query("UPDATE users SET class = 0 WHERE id = $arr[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $arr[id], $dt, $msg, 0)") or sqlerr(__FILE__, __LINE__);
}
}
// Update stats
$seeders = get_row_count("peers", "WHERE seeder='yes'");
$leechers = get_row_count("peers", "WHERE seeder='no'");
mysql_query("UPDATE avps SET value_u=$seeders WHERE arg='seeders'") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE avps SET value_u=$leechers WHERE arg='leechers'") or sqlerr(__FILE__, __LINE__);
// update forum post/topic count
$forums = mysql_query("select id from forums");
while ($forum = mysql_fetch_assoc($forums))
{
$postcount = 0;
$topiccount = 0;
$topics = mysql_query("select id from topics where forumid=$forum[id]");
while ($topic = mysql_fetch_assoc($topics))
{
$res = mysql_query("select count(*) from posts where topicid=$topic[id]");
$arr = mysql_fetch_row($res);
$postcount += $arr[0];
++$topiccount;
}
mysql_query("update forums set postcount=$postcount, topiccount=$topiccount where id=$forum[id]");
}
// delete old torrents
$days = 28;
$dt = sqlesc(get_date_time(gmtime() - ($days * 86400)));
$res = mysql_query("SELECT id, name FROM torrents WHERE added < $dt");
while ($arr = mysql_fetch_assoc($res))
{
@unlink("$torrent_dir/$arr[id].torrent");
mysql_query("DELETE FROM torrents WHERE id=$arr[id]");
mysql_query("DELETE FROM peers WHERE torrent=$arr[id]");
mysql_query("DELETE FROM comments WHERE torrent=$arr[id]");
mysql_query("DELETE FROM files WHERE torrent=$arr[id]");
write_log("Torrent $arr[id] ($arr[name]) was deleted by system (older than $days days)");
}
}
?>