Exemplo n.º 1
0
<?php

require_once "include/bittorrent.php";
dbconn();
require_once get_langfile_path();
loggedinorreturn();
function bark($msg)
{
    stdhead();
    stdmsg($lang_takeflush['std_failed'], $msg);
    stdfoot();
    exit;
}
$id = 0 + $_GET['id'];
int_check($id, true);
if (get_user_class() >= UC_MODERATOR || $CURUSER[id] == "{$id}") {
    $deadtime = deadtime();
    //$deadtime2 = deadtime();
    //$deadtime = time()-600
    //sql_query("DELETE FROM peers WHERE ((last_action< FROM_UNIXTIME($deadtime) AND seeder = 'no)' or (last_action<FROM_UNIXTIME($deadtime2) AND seeder = 'yes')) and userid = " . sqlesc($id));
    sql_query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME({$deadtime}) AND userid=" . sqlesc($id));
    $effected = mysql_affected_rows();
    stderr($lang_takeflush['std_success'], "{$effected} " . $lang_takeflush['std_ghost_torrents_cleaned']);
} else {
    bark($lang_takeflush['std_cannot_flush_others']);
}
Exemplo n.º 2
0
function docleanup()
{
    global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $autoclean_interval, $queries, $query_stat, $tdeadtime, $delaccounts, $oldtorrents, $slotduration, $max_dead_topic_time, $max_dead_user_time, $ad_ratio, $ap_time, $ap_limit, $ap_ratio, $torrent_ttl;
    set_time_limit(1200);
    $result = mysql_query("show processlist") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_array($result)) {
        if ($row["Time"] > 100 || $row["Command"] == "Sleep") {
            $sql = "kill " . $row["Id"] . "";
            mysql_query($sql) or sqlerr(__FILE__, __LINE__);
        }
    }
    ignore_user_abort(1);
    do {
        $res = sql_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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")");
        }
        $res = sql_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)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")");
        }
        $res = sql_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)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
        }
    } while (0);
    $deadtime = deadtime();
    sql_query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME({$deadtime})");
    if ($tdeadtime) {
        $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 = sql_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 = sql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent");
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["torrent"]]["comments"] = $row["c"];
    }
    //=== hidden
    $res = sql_query("SELECT hidden, COUNT(*) AS c FROM comments GROUP BY hidden");
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["hidden"]]["comments"] = $row["c"];
    }
    //===staff
    $res = sql_query("SELECT staff, COUNT(*) AS c FROM comments GROUP BY staff");
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["staff"]]["comments"] = $row["c"];
    }
    $fields = explode(":", "comments:leechers:seeders");
    $res = sql_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)) {
            sql_query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = {$id}");
        }
    }
    // === Update karma seeding bonus
    /**
     * Use ONLY one of the two options below...
     * the first is per torrents seeded, the second will only give the bonus for ONE torrent     no matter how many are seeded.
     * also you will have to play with how much bonus you want to give...
     * ie: seedbonus+0.0225 = 0.25 bonus points per hour
     * seedbonus+0.125 = 0.5 bonus points per hour
     * seedbonus+0.225 = 1 bonus point per hour
     */
    // === Update karma seeding bonus... made nicer by devinkray :D
    // ==   Updated and optimized by pdq :)
    // === using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give the karma for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
    // /======seeding bonus per torrent
    $res = sql_query('SELECT COUNT(torrent) As tcount, userid FROM peers WHERE seeder =\'yes\' GROUP BY userid') or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000) {
                $arr['tcount'] = 5;
            }
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
        }
        if (sizeof($users_buffer) > 0) {
            sql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
            $count = mysql_affected_rows();
            write_log("autoclean", "Auto Cleanup - " . $count / 2 . " users received seedbonus");
        }
        unset($users_buffer);
    }
    // delete old login attempts
    $secs = 1 * 86400;
    // Delete failed login attempts per one day.
    $dt = sqlesc(get_date_time(gmtime() - $secs));
    // calculate date.
    sql_query("DELETE FROM loginattempts WHERE banned='no' AND added < {$dt}");
    // do job.
    // Update stats
    $seeders = get_row_count("peers", "WHERE seeder='yes'");
    $leechers = get_row_count("peers", "WHERE seeder='no'");
    sql_query("UPDATE avps SET value_u={$seeders} WHERE arg='seeders'") or sqlerr(__FILE__, __LINE__);
    sql_query("UPDATE avps SET value_u={$leechers} WHERE arg='leechers'") or sqlerr(__FILE__, __LINE__);
    // Cf's update forum post/topic count
    $forums = @sql_query("SELECT t.forumid, count( DISTINCT p.topicid ) AS topics, count( * ) AS posts FROM posts p LEFT JOIN topics t ON t.id = p.topicid LEFT JOIN forums f ON f.id = t.forumid GROUP BY t.forumid");
    while ($forum = mysql_fetch_assoc($forums)) {
        /*
                $postcount = 0;
                $topiccount = 0;
                $topics = sql_query("select id from topics where forumid=$forum[id]");
                while ($topic = mysql_fetch_assoc($topics))
                {
                    $res = sql_query("select count(*) from posts where topicid=$topic[id]");
                    $arr = mysql_fetch_row($res);
                    $postcount += $arr[0];
                    ++$topiccount;
                } */
        @sql_query("update forums set postcount={$forum['posts']}, topiccount={$forum['topics']} where id={$forum['forumid']}");
    }
    // Reduce Counters for all user rows
    sql_query("UPDATE users SET\npm_count = if( pm_count > 1, pm_count -2, pm_count ) ,\npost_count = if( post_count > 1, post_count -2, post_count ),\ncomment_count = if( comment_count > 1, comment_count -2, comment_count )") or sqlerr(__FILE__, __LINE__);
    $registered = get_row_count('users');
    $unverified = get_row_count('users', "WHERE status='pending'");
    $male = get_row_count("users", "WHERE gender='Male'");
    $female = get_row_count("users", "WHERE gender='Female'");
    $torrents = get_row_count('torrents');
    $seeders = get_row_count('peers', "WHERE seeder='yes'");
    $leechers = get_row_count('peers', "WHERE seeder='no'");
    $torrentstoday = get_row_count('torrents', 'WHERE added > DATE_SUB(NOW(), INTERVAL 1 DAY)');
    $donors = get_row_count('users', "WHERE donor='yes'");
    $unconnectables = get_row_count("peers", " WHERE connectable='no'");
    $forumposts = get_row_count("posts");
    $forumtopics = get_row_count("topics");
    $dt = sqlesc(get_date_time(gmtime() - 300));
    // Active users last 5 minutes
    $numactive = get_row_count("users", "WHERE last_access >= {$dt}");
    sql_query("UPDATE stats SET regusers = '{$registered}', unconusers = '{$unverified}', male = '{$male}', female= '{$female}', torrents = '{$torrents}', seeders = '{$seeders}', leechers = '{$leechers}', unconnectables = '{$unconnectables}', torrentstoday = '{$torrentstoday}', donors = '{$donors}', forumposts = '{$forumposts}', forumtopics = '{$forumtopics}', numactive = '{$numactive}' WHERE id = '1' LIMIT 1");
    write_log("autoclean", " -------------------- Auto cleanup Complete using {$queries} queries --------------------");
}
Exemplo n.º 3
0
function docleanup($forceAll = 0, $printProgress = false)
{
    //require_once(get_langfile_path("cleanup.php",true));
    global $lang_cleanup_target;
    global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $autoclean_interval_one, $autoclean_interval_two, $autoclean_interval_three, $autoclean_interval_four, $autoclean_interval_five, $SITENAME, $bonus, $invite_timeout, $offervotetimeout_main, $offeruptimeout_main, $iniupload_main;
    global $donortimes_bonus, $perseeding_bonus, $maxseeding_bonus, $tzero_bonus, $nzero_bonus, $bzero_bonus, $l_bonus;
    global $expirehalfleech_torrent, $expirefree_torrent, $expiretwoup_torrent, $expiretwoupfree_torrent, $expiretwouphalfleech_torrent, $expirethirtypercentleech_torrent, $expirenormal_torrent, $hotdays_torrent, $hotseeder_torrent, $halfleechbecome_torrent, $freebecome_torrent, $twoupbecome_torrent, $twoupfreebecome_torrent, $twouphalfleechbecome_torrent, $thirtypercentleechbecome_torrent, $normalbecome_torrent, $deldeadtorrent_torrent;
    global $neverdelete_account, $neverdeletepacked_account, $deletepacked_account, $deleteunpacked_account, $deletenotransfer_account, $deletenotransfertwo_account, $deletepeasant_account, $psdlone_account, $psratioone_account, $psdltwo_account, $psratiotwo_account, $psdlthree_account, $psratiothree_account, $psdlfour_account, $psratiofour_account, $psdlfive_account, $psratiofive_account, $putime_account, $pudl_account, $puprratio_account, $puderatio_account, $eutime_account, $eudl_account, $euprratio_account, $euderatio_account, $cutime_account, $cudl_account, $cuprratio_account, $cuderatio_account, $iutime_account, $iudl_account, $iuprratio_account, $iuderatio_account, $vutime_account, $vudl_account, $vuprratio_account, $vuderatio_account, $exutime_account, $exudl_account, $exuprratio_account, $exuderatio_account, $uutime_account, $uudl_account, $uuprratio_account, $uuderatio_account, $nmtime_account, $nmdl_account, $nmprratio_account, $nmderatio_account, $getInvitesByPromotion_class;
    global $enablenoad_advertisement, $noad_advertisement;
    global $Cache;
    set_time_limit(0);
    ignore_user_abort(1);
    $now = time();
    //Priority Class 1: cleanup every 15 mins
    //2.update peer status
    $deadtime = deadtime();
    $deadtime = date("Y-m-d H:i:s", $deadtime);
    sql_query("DELETE FROM peers WHERE last_action < " . sqlesc($deadtime)) or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress('update peer status');
    }
    //11.calculate seeding bonus
    $res = sql_query("SELECT DISTINCT userid FROM peers WHERE seeder = 'yes'") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        $sqrtof2 = sqrt(2);
        $logofpointone = log(0.1);
        $valueone = $logofpointone / $tzero_bonus;
        $pi = 3.141592653589793;
        $valuetwo = $bzero_bonus * (2 / $pi);
        $valuethree = $logofpointone / ($nzero_bonus - 1);
        $timenow = TIMENOW;
        $sectoweek = 7 * 24 * 60 * 60;
        while ($arr = mysql_fetch_assoc($res)) {
            $A = 0;
            $count = 0;
            $all_bonus = 0;
            $torrentres = sql_query("select torrents.added, torrents.size, torrents.seeders from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = {$arr['userid']} AND peers.seeder ='yes'") or sqlerr(__FILE__, __LINE__);
            while ($torrent = mysql_fetch_array($torrentres)) {
                $weeks_alive = ($timenow - strtotime($torrent[added])) / $sectoweek;
                $gb_size = $torrent[size] / 1073741824;
                $temp = (1 - exp($valueone * $weeks_alive)) * $gb_size * (1 + $sqrtof2 * exp($valuethree * ($torrent[seeders] - 1)));
                $A += $temp;
                $count++;
            }
            if ($count > $maxseeding_bonus) {
                $count = $maxseeding_bonus;
            }
            $all_bonus = ($valuetwo * atan($A / $l_bonus) + $perseeding_bonus * $count) / (3600 / $autoclean_interval_one);
            $is_donor = get_single_value("users", "donor", "WHERE id=" . $arr['userid']);
            if ($is_donor == 'yes' && $donortimes_bonus > 0) {
                $all_bonus = $all_bonus * $donortimes_bonus;
            }
            KPS("+", $all_bonus, $arr["userid"]);
        }
    }
    if ($printProgress) {
        printProgress('calculate seeding bonus');
    }
    //Priority Class 2: cleanup every 30 mins
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime2'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime2'," . sqlesc($now) . ")") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_two > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 1';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime2'") or sqlerr(__FILE__, __LINE__);
    }
    //2.5.update torrents' visibility
    $deadtime = deadtime() - $max_dead_torrent_time;
    sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME({$deadtime}) AND seeders=0") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("update torrents' visibility");
    }
    //Priority Class 3: cleanup every 60 mins
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime3'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime3',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_three > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 2';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime3'") or sqlerr(__FILE__, __LINE__);
    }
    //4.update count of seeders, leechers, comments for torrents
    $torrents = array();
    $res = sql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        if ($row["seeder"] == "yes") {
            $key = "seeders";
        } else {
            $key = "leechers";
        }
        $torrents[$row["torrent"]][$key] = $row["c"];
    }
    $res = sql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["torrent"]]["comments"] = $row["c"];
    }
    $fields = explode(":", "comments:leechers:seeders");
    $res = sql_query("SELECT id, seeders, leechers, comments FROM torrents") or sqlerr(__FILE__, __LINE__);
    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)) {
            sql_query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = {$id}") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("update count of seeders, leechers, comments for torrents");
    }
    //set no-advertisement-by-bonus time out
    sql_query("UPDATE users SET noad='no' WHERE noaduntil < " . sqlesc(date("Y-m-d H:i:s")) . ($enablenoad_advertisement == 'yes' ? " AND class < " . sqlesc($noad_advertisement) : ""));
    if ($printProgress) {
        printProgress("set no-advertisement-by-bonus time out");
    }
    //12. update forum post/topic count
    $forums = sql_query("select id from forums") or sqlerr(__FILE__, __LINE__);
    while ($forum = mysql_fetch_assoc($forums)) {
        $postcount = 0;
        $topiccount = 0;
        $topics = sql_query("select id from topics where forumid={$forum['id']}") or sqlerr(__FILE__, __LINE__);
        while ($topic = mysql_fetch_assoc($topics)) {
            $res = sql_query("select count(*) from posts where topicid={$topic['id']}") or sqlerr(__FILE__, __LINE__);
            $arr = mysql_fetch_row($res);
            $postcount += $arr[0];
            ++$topiccount;
        }
        sql_query("update forums set postcount={$postcount}, topiccount={$topiccount} where id={$forum['id']}") or sqlerr(__FILE__, __LINE__);
    }
    $Cache->delete_value('forums_list');
    if ($printProgress) {
        printProgress("update forum post/topic count");
    }
    //14.cleanup offers
    //Delete offers if not voted on after some time
    if ($offervotetimeout_main) {
        $secs = (int) $offervotetimeout_main;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $offervotetimeout_main));
        $res = sql_query("SELECT id, name FROM offers WHERE added < {$dt} AND allowed <> 'allowed'") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("DELETE FROM offers WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM offervotes WHERE offerid={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM comments WHERE offer={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            write_log("Offer {$arr['id']} ({$arr['name']}) was deleted by system (vote timeout)", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("delete offers if not voted on after some time");
    }
    //Delete offers if not uploaded after being voted on for some time.
    if ($offeruptimeout_main) {
        $secs = (int) $offeruptimeout_main;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $res = sql_query("SELECT id, name FROM offers WHERE allowedtime < {$dt} AND allowed = 'allowed'") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("DELETE FROM offers WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM offervotes WHERE offerid={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM comments WHERE offer={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            write_log("Offer {$arr['id']} ({$arr['name']}) was deleted by system (upload timeout)", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("delete offers if not uploaded after being voted on for some time.");
    }
    //15.cleanup torrents
    //Start: expire torrent promotion
    function torrent_promotion_expire($days, $type = 2, $targettype = 1)
    {
        $secs = (int) ($days * 86400);
        //XX days
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $res = sql_query("SELECT id, name FROM torrents WHERE added < {$dt} AND sp_state = " . sqlesc($type) . ' AND promotion_time_type=0') or sqlerr(__FILE__, __LINE__);
        switch ($targettype) {
            case 1:
                $sp_state = 1;
                $become = "normal";
                break;
            case 2:
                $sp_state = 2;
                $become = "Free";
                break;
            case 3:
                $sp_state = 3;
                $become = "2X";
                break;
            case 4:
                $sp_state = 4;
                $become = "2X Free";
                break;
            case 5:
                $sp_state = 5;
                $become = "50%";
                break;
            case 6:
                $sp_state = 6;
                $become = "2X 50%";
                break;
            default:
                $sp_state = 1;
                $become = "normal";
                break;
        }
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("UPDATE torrents SET sp_state = " . sqlesc($sp_state) . " WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            if ($sp_state == 1) {
                write_log("Torrent {$arr['id']} ({$arr['name']}) is no longer on promotion (time expired)", 'normal');
            } else {
                write_log("Promotion type for torrent {$arr['id']} ({$arr['name']}) is changed to " . $become . " (time expired)", 'normal');
            }
        }
    }
    if ($expirehalfleech_torrent) {
        torrent_promotion_expire($expirehalfleech_torrent, 5, $halfleechbecome_torrent);
    }
    if ($expirefree_torrent) {
        torrent_promotion_expire($expirefree_torrent, 2, $freebecome_torrent);
    }
    if ($expiretwoup_torrent) {
        torrent_promotion_expire($expiretwoup_torrent, 3, $twoupbecome_torrent);
    }
    if ($expiretwoupfree_torrent) {
        torrent_promotion_expire($expiretwoupfree_torrent, 4, $twoupfreebecome_torrent);
    }
    if ($expiretwouphalfleech_torrent) {
        torrent_promotion_expire($expiretwouphalfleech_torrent, 6, $twouphalfleechbecome_torrent);
    }
    if ($expirethirtypercentleech_torrent) {
        torrent_promotion_expire($expirethirtypercentleech_torrent, 7, $thirtypercentleechbecome_torrent);
    }
    if ($expirenormal_torrent) {
        torrent_promotion_expire($expirenormal_torrent, 1, $normalbecome_torrent);
    }
    //expire individual torrent promotion
    sql_query("UPDATE torrents SET sp_state = 1, promotion_time_type=0, promotion_until='0000-00-00 00:00:00' WHERE promotion_time_type=2 AND promotion_until < " . sqlesc(date("Y-m-d H:i:s", TIMENOW))) or sqlerr(__FILE__, __LINE__);
    //End: expire torrent promotion
    if ($printProgress) {
        printProgress("expire torrent promotion");
    }
    //automatically pick hot
    if ($hotdays_torrent) {
        $secs = (int) ($hotdays_torrent * 86400);
        //XX days
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        sql_query("UPDATE torrents SET picktype = 'hot' WHERE added > {$dt} AND picktype = 'normal' AND seeders > " . sqlesc($hotseeder_torrent)) or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("automatically pick hot");
    }
    //Priority Class 4: cleanup every 24 hours
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime4'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime4',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_four > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 3';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime4'") or sqlerr(__FILE__, __LINE__);
    }
    //3.delete unconfirmed accounts
    $deadtime = time() - $signup_timeout;
    sql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME({$deadtime}) AND last_login < FROM_UNIXTIME({$deadtime}) AND last_access < FROM_UNIXTIME({$deadtime})") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete unconfirmed accounts");
    }
    //5.delete old login attempts
    $secs = 12 * 60 * 60;
    // Delete failed login attempts per half day.
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    // calculate date.
    sql_query("DELETE FROM loginattempts WHERE banned='no' AND added < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete old login attempts");
    }
    //6.delete old invite codes
    $secs = $invite_timeout * 24 * 60 * 60;
    // when?
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    // calculate date.
    sql_query("DELETE FROM invites WHERE time_invited < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete old invite codes");
    }
    //7.delete regimage codes
    sql_query("TRUNCATE TABLE `regimages`") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete regimage codes");
    }
    //10.clean up user accounts
    // make sure VIP or above never get deleted
    $neverdelete_account = $neverdelete_account <= UC_VIP ? $neverdelete_account : UC_VIP;
    //delete inactive user accounts, no transfer. Alt. 1: last access time
    if ($deletenotransfer_account) {
        $secs = $deletenotransfer_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $maxclass = $neverdelete_account;
        sql_query("DELETE FROM users WHERE parked='no' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt} AND (uploaded = 0 || uploaded = " . sqlesc($iniupload_main) . ") AND downloaded = 0") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("delete inactive user accounts, no transfer. Alt. 1: last access time");
    }
    //delete inactive user accounts, no transfer. Alt. 2: registering time
    if ($deletenotransfertwo_account) {
        $secs = $deletenotransfertwo_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $maxclass = $neverdelete_account;
        sql_query("DELETE FROM users WHERE parked='no' AND status='confirmed' AND class < {$maxclass} AND added < {$dt} AND (uploaded = 0 || uploaded = " . sqlesc($iniupload_main) . ") AND downloaded = 0") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("delete inactive user accounts, no transfer. Alt. 2: registering time");
    }
    //delete inactive user accounts, not parked
    if ($deleteunpacked_account) {
        $secs = $deleteunpacked_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $maxclass = $neverdelete_account;
        sql_query("DELETE FROM users WHERE parked='no' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt}") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("delete inactive user accounts, not parked");
    }
    //delete parked user accounts, parked
    if ($deletepacked_account) {
        $secs = $deletepacked_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $maxclass = $neverdeletepacked_account;
        sql_query("DELETE FROM users WHERE parked='yes' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt}") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("delete parked user accounts, parked");
    }
    //remove VIP status if time's up
    $res = sql_query("SELECT id, modcomment FROM users WHERE vip_added='yes' AND vip_until < NOW()") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            $subject = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_vip_status_removed']);
            $msg = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_vip_status_removed_body']);
            ///---AUTOSYSTEM MODCOMMENT---//
            $modcomment = htmlspecialchars($arr["modcomment"]);
            $modcomment = date("Y-m-d") . " - VIP status removed by - AutoSystem.\n" . $modcomment;
            $modcom = sqlesc($modcomment);
            ///---end
            sql_query("UPDATE users SET class = '1', vip_added = 'no', vip_until = '0000-00-00 00:00:00', modcomment = {$modcom} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("INSERT INTO messages (sender, receiver, added, msg, subject) VALUES(0, {$arr['id']}, {$dt}, {$msg}, {$subject})") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("remove VIP status if time's up");
    }
    // promote peasant back to user
    function peasant_to_user($down_floor_gb, $down_roof_gb, $minratio)
    {
        global $lang_cleanup_target;
        if ($down_floor_gb) {
            $downlimit_floor = $down_floor_gb * 1024 * 1024 * 1024;
            $downlimit_roof = $down_roof_gb * 1024 * 1024 * 1024;
            $res = sql_query("SELECT id FROM users WHERE class = 0 AND downloaded >= {$downlimit_floor} " . ($downlimit_roof > $down_floor_gb ? " AND downloaded < {$downlimit_roof}" : "") . " AND uploaded / downloaded >= {$minratio}") or sqlerr(__FILE__, __LINE__);
            if (mysql_num_rows($res) > 0) {
                $dt = sqlesc(date("Y-m-d H:i:s"));
                while ($arr = mysql_fetch_assoc($res)) {
                    $subject = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_low_ratio_warning_removed']);
                    $msg = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_your_ratio_warning_removed']);
                    writecomment($arr[id], "Leech Warning removed by System.");
                    sql_query("UPDATE users SET class = 1, leechwarn = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, {$subject}, {$msg})") or sqlerr(__FILE__, __LINE__);
                }
            }
        }
    }
    peasant_to_user($psdlfive_account, 0, $psratiofive_account);
    peasant_to_user($psdlfour_account, $psdlfive_account, $psratiofour_account);
    peasant_to_user($psdlthree_account, $psdlfour_account, $psratiothree_account);
    peasant_to_user($psdltwo_account, $psdlthree_account, $psratiotwo_account);
    peasant_to_user($psdlone_account, $psdltwo_account, $psratioone_account);
    if ($printProgress) {
        printProgress("promote peasant back to user");
    }
    //end promote peasant back to user
    // start promotion
    function promotion($class, $down_floor_gb, $minratio, $time_week, $addinvite = 0)
    {
        global $lang_cleanup_target;
        $oriclass = $class - 1;
        if ($down_floor_gb) {
            $limit = $down_floor_gb * 1024 * 1024 * 1024;
            $maxdt = date("Y-m-d H:i:s", TIMENOW - 86400 * 7 * $time_week);
            $res = sql_query("SELECT id, max_class_once FROM users WHERE class = {$oriclass} AND downloaded >= {$limit} AND uploaded / downloaded >= {$minratio} AND added < " . sqlesc($maxdt)) or sqlerr(__FILE__, __LINE__);
            if (mysql_num_rows($res) > 0) {
                $dt = sqlesc(date("Y-m-d H:i:s"));
                while ($arr = mysql_fetch_assoc($res)) {
                    $subject = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_promoted_to'] . get_user_class_name($class, false, false, false));
                    $msg = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_now_you_are'] . get_user_class_name($class, false, false, false) . $lang_cleanup_target[get_user_lang($arr[id])]['msg_see_faq']);
                    if ($class <= $arr[max_class_once]) {
                        sql_query("UPDATE users SET class = {$class} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    } else {
                        sql_query("UPDATE users SET class = {$class}, max_class_once={$class}, invites=invites+{$addinvite} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    }
                    sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, {$subject}, {$msg})") or sqlerr(__FILE__, __LINE__);
                }
            }
        }
    }
    //do not change the ascending order
    promotion(UC_POWER_USER, $pudl_account, $puprratio_account, $putime_account, $getInvitesByPromotion_class[UC_POWER_USER]);
    promotion(UC_ELITE_USER, $eudl_account, $euprratio_account, $eutime_account, $getInvitesByPromotion_class[UC_ELITE_USER]);
    promotion(UC_CRAZY_USER, $cudl_account, $cuprratio_account, $cutime_account, $getInvitesByPromotion_class[UC_CRAZY_USER]);
    promotion(UC_INSANE_USER, $iudl_account, $iuprratio_account, $iutime_account, $getInvitesByPromotion_class[UC_INSANE_USER]);
    promotion(UC_VETERAN_USER, $vudl_account, $vuprratio_account, $vutime_account, $getInvitesByPromotion_class[UC_VETERAN_USER]);
    promotion(UC_EXTREME_USER, $exudl_account, $exuprratio_account, $exutime_account, $getInvitesByPromotion_class[UC_EXTREME_USER]);
    promotion(UC_ULTIMATE_USER, $uudl_account, $uuprratio_account, $uutime_account, $getInvitesByPromotion_class[UC_ULTIMATE_USER]);
    promotion(UC_NEXUS_MASTER, $nmdl_account, $nmprratio_account, $nmtime_account, $getInvitesByPromotion_class[UC_NEXUS_MASTER]);
    // end promotion
    if ($printProgress) {
        printProgress("promote users to other classes");
    }
    // start demotion
    function demotion($class, $deratio)
    {
        global $lang_cleanup_target;
        $newclass = $class - 1;
        $res = sql_query("SELECT id FROM users WHERE class = {$class} AND uploaded / downloaded < {$deratio}") or sqlerr(__FILE__, __LINE__);
        if (mysql_num_rows($res) > 0) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            while ($arr = mysql_fetch_assoc($res)) {
                $subject = $lang_cleanup_target[get_user_lang($arr[id])]['msg_demoted_to'] . get_user_class_name($newclass, false, false, false);
                $msg = $lang_cleanup_target[get_user_lang($arr[id])]['msg_demoted_from'] . get_user_class_name($class, false, false, false) . $lang_cleanup_target[get_user_lang($arr[id])]['msg_to'] . get_user_class_name($newclass, false, false, false) . $lang_cleanup_target[get_user_lang($arr[id])]['msg_because_ratio_drop_below'] . $deratio . ".\n";
                sql_query("UPDATE users SET class = {$newclass} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            }
        }
    }
    //do not change the descending order
    demotion(UC_NEXUS_MASTER, $nmderatio_account);
    demotion(UC_ULTIMATE_USER, $uuderatio_account);
    demotion(UC_EXTREME_USER, $exuderatio_account);
    demotion(UC_VETERAN_USER, $vuderatio_account);
    demotion(UC_INSANE_USER, $iuderatio_account);
    demotion(UC_CRAZY_USER, $cuderatio_account);
    demotion(UC_ELITE_USER, $euderatio_account);
    demotion(UC_POWER_USER, $puderatio_account);
    if ($printProgress) {
        printProgress("demote users to other classes");
    }
    // end demotion
    // start demote users to peasant
    function user_to_peasant($down_floor_gb, $minratio)
    {
        global $lang_cleanup_target;
        global $deletepeasant_account;
        $length = $deletepeasant_account * 86400;
        // warn users until xxx days
        $until = date("Y-m-d H:i:s", TIMENOW + $length);
        $downlimit_floor = $down_floor_gb * 1024 * 1024 * 1024;
        $res = sql_query("SELECT id FROM users WHERE class = 1 AND downloaded > {$downlimit_floor} AND uploaded / downloaded < {$minratio}") or sqlerr(__FILE__, __LINE__);
        if (mysql_num_rows($res) > 0) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            while ($arr = mysql_fetch_assoc($res)) {
                $subject = $lang_cleanup_target[get_user_lang($arr[id])]['msg_demoted_to'] . get_user_class_name(UC_PEASANT, false, false, false);
                $msg = $lang_cleanup_target[get_user_lang($arr[id])]['msg_must_fix_ratio_within'] . $deletepeasant_account . $lang_cleanup_target[get_user_lang($arr[id])]['msg_days_or_get_banned'];
                writecomment($arr[id], "Leech Warned by System - Low Ratio.");
                sql_query("UPDATE users SET class = 0 , leechwarn = 'yes', leechwarnuntil = " . sqlesc($until) . " WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            }
        }
    }
    user_to_peasant($psdlone_account, $psratioone_account);
    user_to_peasant($psdltwo_account, $psratiotwo_account);
    user_to_peasant($psdlthree_account, $psratiothree_account);
    user_to_peasant($psdlfour_account, $psratiofour_account);
    user_to_peasant($psdlfive_account, $psratiofive_account);
    if ($printProgress) {
        printProgress("demote Users to peasant");
    }
    // end Users to Peasant
    //ban users with leechwarning expired
    $dt = sqlesc(date("Y-m-d H:i:s"));
    // take date time
    $res = sql_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 Leech Warning expired.");
            sql_query("UPDATE users SET enabled = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("ban users with leechwarning expired");
    }
    //Remove warning of users
    $dt = sqlesc(date("Y-m-d H:i:s"));
    // take date time
    $res = sql_query("SELECT id FROM users WHERE enabled = 'yes' AND warned = 'yes' AND warneduntil < {$dt}") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            $subject = $lang_cleanup_target[get_user_lang($arr[id])]['msg_warning_removed'];
            $msg = $lang_cleanup_target[get_user_lang($arr[id])]['msg_your_warning_removed'];
            writecomment($arr[id], "Warning removed by System.");
            sql_query("UPDATE users SET warned = 'no', warneduntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("remove warning of users");
    }
    //17.update total seeding and leeching time of users
    $res = sql_query("SELECT * FROM users") or sqlerr(__FILE__, __LINE__);
    while ($arr = mysql_fetch_assoc($res)) {
        //die("s" . $arr['id']);
        $res2 = sql_query("SELECT SUM(seedtime) as st, SUM(leechtime) as lt FROM snatched where userid = " . $arr['id'] . " LIMIT 1") or sqlerr(__FILE__, __LINE__);
        $arr2 = mysql_fetch_assoc($res2) or sqlerr(__FILE__, __LINE__);
        //die("ss" . $arr2['st']);
        //die("sss" . "UPDATE users SET seedtime = " . $arr2['st'] . ", leechtime = " . $arr2['lt'] . " WHERE id = " . $arr['id']);
        sql_query("UPDATE users SET seedtime = " . intval($arr2['st']) . ", leechtime = " . intval($arr2['lt']) . " WHERE id = " . $arr['id']) or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("update total seeding and leeching time of users");
    }
    // delete torrents that have been dead for a long time
    if ($deldeadtorrent_torrent > 0) {
        $length = $deldeadtorrent_torrent * 86400;
        $until = date("Y-m-d H:i:s", TIMENOW - $length);
        $dt = sqlesc(date("Y-m-d H:i:s"));
        $res = sql_query("SELECT id, name, owner FROM torrents WHERE visible = 'no' AND last_action < " . sqlesc($until) . " AND seeders = 0 AND leechers = 0") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            deletetorrent($arr['id']);
            $subject = $lang_cleanup_target[get_user_lang($arr[owner])]['msg_your_torrent_deleted'];
            $msg = $lang_cleanup_target[get_user_lang($arr[owner])]['msg_your_torrent'] . "[i]" . $arr['name'] . "[/i]" . $lang_cleanup_target[get_user_lang($arr[owner])]['msg_was_deleted_because_dead'];
            sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['owner']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            write_log("Torrent {$arr['id']} ({$arr['name']}) is deleted by system because of being dead for a long time.", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("delete torrents that have been dead for a long time");
    }
    //Priority Class 5: cleanup every 15 days
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime5'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime5',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_five > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 4';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime5'") or sqlerr(__FILE__, __LINE__);
    }
    //update clients' popularity
    $res = sql_query("SELECT id FROM agent_allowed_family");
    while ($row = mysql_fetch_array($res)) {
        $count = get_row_count("users", "WHERE clientselect=" . sqlesc($row['id']));
        sql_query("UPDATE agent_allowed_family SET hits=" . sqlesc($count) . " WHERE id=" . sqlesc($row['id']));
    }
    if ($printProgress) {
        printProgress("update clients' popularity");
    }
    //delete old messages sent by system
    $length = 180 * 86400;
    //half a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM messages WHERE sender = 0 AND added < " . sqlesc($until));
    if ($printProgress) {
        printProgress("delete old messages sent by system");
    }
    //delete old readpost records
    $length = 180 * 86400;
    //half a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    $postIdHalfYearAgo = get_single_value('posts', 'id', 'WHERE added < ' . sqlesc($until) . ' ORDER BY added DESC');
    if ($postIdHalfYearAgo) {
        sql_query("UPDATE users SET last_catchup = " . sqlesc($postIdHalfYearAgo) . " WHERE last_catchup < " . sqlesc($postIdHalfYearAgo));
        sql_query("DELETE FROM readposts WHERE lastpostread < " . sqlesc($postIdHalfYearAgo));
    }
    if ($printProgress) {
        printProgress("delete old readpost records");
    }
    //delete old ip log
    $length = 365 * 86400;
    //a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM iplog WHERE access < " . sqlesc($until));
    if ($printProgress) {
        printProgress("delete old ip log");
    }
    //delete old general log
    $secs = 365 * 86400;
    //a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM sitelog WHERE added < " . sqlesc($until)) or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete old general log");
    }
    //1.delete torrents that doesn't exist any more
    do {
        $res = sql_query("SELECT id FROM torrents") or sqlerr(__FILE__, __LINE__);
        $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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM peers GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM files GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if ($ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
    } while (0);
    if ($printProgress) {
        printProgress("delete torrents that doesn't exist any more");
    }
    //8.lock topics where last post was made more than x days ago
    $secs = 365 * 24 * 60 * 60;
    sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND UNIX_TIMESTAMP(posts.added) < " . TIMENOW . " - {$secs}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("lock topics where last post was made more than x days ago");
    }
    //9.delete report items older than four week
    $secs = 4 * 7 * 24 * 60 * 60;
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    sql_query("DELETE FROM reports WHERE dealtwith=1 AND added < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete report items older than four week");
    }
    return 'Full cleanup is done';
}
Exemplo n.º 4
0
function docleanup()
{
    global $TBDEV;
    set_time_limit(0);
    ignore_user_abort(1);
    do {
        $res = mysql_query("SELECT id FROM torrents");
        $ar = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            $ar[$id] = 1;
        }
        if (!count($ar)) {
            break;
        }
        $dp = @opendir($TBDEV['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 = $TBDEV['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, MYSQL_NUM)) {
            $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, MYSQL_NUM)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            mysql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
        }
    } while (0);
    $deadtime = deadtime();
    @mysql_query("DELETE FROM peers WHERE last_action < {$deadtime}");
    $deadtime -= $TBDEV['max_dead_torrent_time'];
    @mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < {$deadtime}");
    $deadtime = time() - $TBDEV['signup_timeout'];
    @mysql_query("DELETE FROM users WHERE status = 'pending' AND added < {$deadtime} AND last_login < {$deadtime} AND last_access < {$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"];
        if (isset($torrents[$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}");
        }
    }
    // 09 Seeding Bonus by Bigjoos // start
    // modded by cybernet2u
    // support - http://bit.ly/8jd6WH
    // http://xList.ro/
    // http://tbdev.xlist.ro/
    // === Update karma seeding bonus... made nicer by devinkray :D
    // ==   Updated and optimized by pdq :)
    // === using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give the karma for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
    // /======seeding bonus per torrent
    $res = mysql_query('SELECT COUNT(torrent) As tcount, userid FROM peers WHERE seeder =\'yes\' GROUP BY userid') or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000) {
                $arr['tcount'] = 5;
            }
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
        }
        if (sizeof($users_buffer) > 0) {
            mysql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
            $count = mysql_affected_rows();
            write_log("Cleanup - " . $count / 2 . " users received seedbonus");
        }
        unset($users_buffer);
    }
    // === Updated remove karma vip by Bigjoos - change class number '1' in the users_buffer to whatever is under your vip class number
    $res = mysql_query("SELECT id, modcomment FROM users WHERE vip_added='yes' AND vip_until < " . TIME_NOW . "") or sqlerr(__FILE__, __LINE__);
    $msgs_buffer = $users_buffer = array();
    if (mysql_num_rows($res) > 0) {
        $subject = "VIP status expired.";
        $msg = "Your VIP status has timed out and has been auto-removed by the system. Become a VIP again by donating to {$TBDEV['site_name']} , or exchanging some Karma Bonus Points. Cheers !\n";
        while ($arr = mysql_fetch_assoc($res)) {
            $modcomment = sqlesc(get_date(time(), 'DATE', 1) . " - Vip status Automatically Removed By System\n");
            $msgs_buffer[] = '(0,' . $arr['id'] . ',' . time() . ', ' . sqlesc($msg) . ', ' . sqlesc($subject) . ')';
            $users_buffer[] = '(' . $arr['id'] . ',1, \'no\', \'0\' , ' . $modcomment . ')';
        }
        if (sizeof($msgs_buffer) > 0) {
            mysql_query("INSERT INTO messages (sender,receiver,added,msg,subject) VALUES " . implode(', ', $msgs_buffer)) or sqlerr(__FILE__, __LINE__);
            mysql_query("INSERT INTO users (id, class, vip_added, vip_until, modcomment) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE class=values(class),\nvip_added=values(vip_added),vip_until=values(vip_until),modcomment=concat(values(modcomment),modcomment)") or sqlerr(__FILE__, __LINE__);
            $count = mysql_affected_rows();
            write_log("Cleanup - Karma Vip status expired on - " . $count / 2 . " Member(s)");
        }
        unset($users_buffer);
        unset($msgs_buffer);
    }
    // === Updated remove custom smilies by Bigjoos:)
    $res = mysql_query("SELECT id FROM users WHERE smile_until < " . TIME_NOW . " AND smile_until <> '0'") or sqlerr(__FILE__, __LINE__);
    $msgs_buffer = $users_buffer = array();
    if (mysql_num_rows($res) > 0) {
        $subject = "Custom smilies expired.";
        $msg = "Your Custom smilies have timed out and has been auto-removed by the system. If you would like to have them again, exchange some Karma Bonus Points again. Cheers!\n";
        while ($arr = mysql_fetch_assoc($res)) {
            $modcomment = sqlesc(get_date(time(), 'DATE', 1) . " - Custom smilies Automatically Removed By System\n");
            $msgs_buffer[] = '(0,' . $arr['id'] . ',' . time() . ', ' . sqlesc($msg) . ', ' . sqlesc($subject) . ' )';
            $users_buffer[] = '(' . $arr['id'] . ', \'0\', ' . $modcomment . ')';
        }
        if (sizeof($msgs_buffer) > 0) {
            mysql_query("INSERT INTO messages (sender,receiver,added,msg,subject) VALUES " . implode(', ', $msgs_buffer)) or sqlerr(__FILE__, __LINE__);
            mysql_query("INSERT INTO users (id, smile_until, modcomment) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE smile_until=values(smile_until),\nmodcomment=concat(values(modcomment),modcomment)") or sqlerr(__FILE__, __LINE__);
            $count = mysql_affected_rows();
            write_log("Cleanup - Removed Custom smilies from " . $count / 2 . " members");
        }
        unset($users_buffer);
        unset($msgs_buffer);
    }
    // 09 Seeding Bonus by Bigjoos // end
    // modded by cybernet2u
    // support - http://bit.ly/8jd6WH
    // http://xList.ro/
    // http://tbdev.xlist.ro/
    //delete inactive user accounts
    $secs = 42 * 86400;
    $dt = time() - $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 LEFT JOIN posts ON topics.lastpost = posts.id WHERE topics.locked = 'no' AND topics.sticky = 'no' AND " . gmtime() . " - UNIX_TIMESTAMP(posts.added) > $secs") or sqlerr(__FILE__, __LINE__);
      if(mysql_num_rows($res) > 0) {
    	while ($arr = mysql_fetch_assoc($res))
        $pids[] = $arr['id'];
    		mysql_query("UPDATE topics SET locked='yes' WHERE id IN (".join(',', $pids).")") or sqlerr(__FILE__, __LINE__);
      }
    */
    //remove expired warnings
    $res = @mysql_query("SELECT id FROM users WHERE warned='yes' AND warneduntil < " . time() . " AND warneduntil <> 0") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        $dt = 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 = 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__);
        }
    }
    // promote power users
    $limit = 25 * 1024 * 1024 * 1024;
    $minratio = 1.05;
    $maxdt = time() - 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 = 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 = 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 t.forumid, count( DISTINCT p.topicid ) AS topics, count( * ) AS posts FROM posts p LEFT JOIN topics t ON t.id = p.topicid LEFT JOIN forums f ON f.id = t.forumid GROUP BY t.forumid");
    $forums = @mysql_query("SELECT f.id, count( DISTINCT t.id ) AS topics, count( * ) AS posts\r\n                          FROM forums f\r\n                          LEFT JOIN topics t ON f.id = t.forumid\r\n                          LEFT JOIN posts p ON t.id = p.topicid\r\n                          GROUP BY f.id");
    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;
        		} */
        $forum['posts'] = $forum['topics'] > 0 ? $forum['posts'] : 0;
        @mysql_query("update forums set postcount={$forum['posts']}, topiccount={$forum['topics']} where id={$forum['id']}");
    }
    // delete old torrents
    $days = 28;
    $dt = time() - $days * 86400;
    $res = mysql_query("SELECT id, name FROM torrents WHERE added < {$dt}");
    while ($arr = mysql_fetch_assoc($res)) {
        @unlink("{$TBDEV['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)");
    }
    // Remove expired readposts...
    $dt = time() - $TBDEV['readpost_expiry'];
    @mysql_query("DELETE readposts FROM readposts " . "LEFT JOIN posts ON readposts.lastpostread = posts.id " . "WHERE posts.added < {$dt}") or sqlerr(__FILE__, __LINE__);
    // Auto Demote Uploader by XiaNYdE // start
    // support - http://bit.ly/5NtmgT
    // http://xList.ro/
    // http://tbdev.xlist.ro/
    $days = 5;
    // <-- Set days before Warning
    $dt = sqlesc(get_date_time(gmtime() - $days * 86400));
    $res2 = mysql_query("SELECT id, modcomment, username FROM users WHERE upped = 'yes' AND class = 3 AND upload_warn = 'no' AND last_upload < {$dt} AND parked = 'no'") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res2) > 0) {
        $dt2 = sqlesc(get_date_time());
        $subject = sqlesc("Upload Failure Warning. \n");
        $msg = sqlesc("You have 2 days to upload a torrent. If you don't do this you will be Auto-Demoted from Uploader to Power User because you have failed to consistently upload. \n");
        while ($arr = mysql_fetch_assoc($res2)) {
            $modcomment = htmlspecialchars($arr["modcomment"]);
            $modcomment = gmdate("Y-m-d") . " - Upload Failure Warning Sent by AutoSystem.\n" . $modcomment;
            $modcom = sqlesc($modcomment);
            mysql_query("UPDATE users SET upload_warn = 'yes', modcomment = {$modcom} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
            mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, {$arr['id']}, {$dt2}, {$subject}, {$msg}, 0)") or sqlerr(__FILE__, __LINE__);
            // + write log by cybernet2u
            write_log("User {$arr['id']} ({$arr['username']}) was deleted by system ( because of his upload inactivity )");
        }
    }
    // THE ACTUAL DEMOTION
    $days = 7;
    // <-- Set Days for Actual Demotion
    $dt = sqlesc(get_date_time(gmtime() - $days * 86400));
    $res2 = mysql_query("SELECT id, modcomment FROM users WHERE upped = 'yes' AND class = '3' AND upload_warn = 'yes' AND last_upload < {$dt} AND parked = 'no'") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res2) > 0) {
        $dt2 = sqlesc(get_date_time());
        $subject = sqlesc("Upload Failure Demotion. \n");
        $msg = sqlesc("You have been Auto-Demoted from Uploader to Power User because you have not uploaded in {$days} days.\n");
        while ($arr2 = mysql_fetch_assoc($res2)) {
            $modcomment = htmlspecialchars($arr2["modcomment"]);
            $modcomment = gmdate("Y-m-d") . " - Demoted from Uploader by AutoSystem.\n" . $modcomment;
            $modcom = sqlesc($modcomment);
            mysql_query("UPDATE users SET upload_warn = 'no', class = '1', modcomment = {$modcom} WHERE id = {$arr2['id']}") or sqlerr(__FILE__, __LINE__);
            mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, {$arr2['id']}, {$dt2}, {$subject}, {$msg}, 0)") or sqlerr(__FILE__, __LINE__);
        }
    }
    // Auto Demote Uploader by XiaNYdE // end
}
Exemplo n.º 5
0
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 (isset($ar[$id]))
				continue;
			$delids[] = $id;
		}
//		if (count($delids))
//			mysql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
	} while (0);

	$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");
	}



	// 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 // old code for TTL
$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 snatched WHERE torrentid =$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]");
mysql_query("DELETE FROM thanks WHERE torrentid =$arr[id]");
write_log("Torrentinu $arr[id] ($arr[name]) var eytt af kerfinu (eldri en $days daga)");
}
//Uppfæra RSS
        $fd1 = @fopen("rss.xml", "w");
        $fd2 = fopen("rssdd.xml", "w");
        $cats = "";
        $query = mysql_query("SELECT id, name FROM categories");
        while ($flokkar = mysql_fetch_assoc($query))
                $cats[$flokkar["id"]] = $flokkar["name"];
        $s = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<rss version=\"0.91\">\n<channel>\n" .
                "<title>IsTorrent</title>\n<description>50 Nýjustu torrent</description>\n<link>$DEFAULTBASEURL/</link>\n";
        @fwrite($fd1, $s);
        @fwrite($fd2, $s);
        $query2 = mysql_query("SELECT id,name,descr,filename,category,seeders,leechers FROM torrents where seeders > 0 ORDER BY added DESC LIMIT 50") or sqlerr(__FILE__, __LINE__);
        while ($rssdot = mysql_fetch_assoc($query2))
        {
                $nafn = $rssdot["name"];
                $cat = $cats[$rssdot["category"]];
                $s = "<item>\n<title>" . htmlspecialchars("$nafn ($cat)") . "</title>\n" .
                        "<description>" . htmlspecialchars($rssdot["descr"]) . "</description>\n" .
                        "<seeders>" . htmlspecialchars($rssdot["seeders"]) . "</seeders>\n" .
                        "<leechers>" . htmlspecialchars($rssdot["leechers"]) . "</leechers>\n";
                @fwrite($fd1, $s);
                @fwrite($fd2, $s);
                @fwrite($fd1, "<link>$DEFAULTBASEURL/details.php?id=$rssdot[id]&amp;hit=1</link>\n</item>\n");
                $filename = htmlspecialchars($rssdot["filename"]);
                @fwrite($fd2, "<link>$DEFAULTBASEURL/download.php/$rssdot[id]/$filename</link>\n</item>\n");
        }
        $s = "</channel>\n</rss>\n";
        @fwrite($fd1, $s);
        @fwrite($fd2, $s);
        @fclose($fd1);
        @fclose($fd2);
}
Exemplo n.º 6
0
function docleanup()
{
    global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $use_ttl, $autoclean_interval, $points_per_cleanup, $ttl_days, $tracker_lang;
    @set_time_limit(0);
    @ignore_user_abort(1);
    do {
        $res = sql_query("SELECT id FROM torrents") or sqlerr(__FILE__, __LINE__);
        $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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM peers GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM files GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if ($ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(", ", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
    } while (0);
    $deadtime = deadtime();
    sql_query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME({$deadtime})") or sqlerr(__FILE__, __LINE__);
    $deadtime = deadtime();
    sql_query("UPDATE snatched SET seeder = 'no' WHERE seeder = 'yes' AND last_action < FROM_UNIXTIME({$deadtime})");
    $deadtime -= $max_dead_torrent_time;
    sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME({$deadtime}) AND multitracker = 'no'") or sqlerr(__FILE__, __LINE__);
    $torrents = array();
    $res = sql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        if ($row["seeder"] == "yes") {
            $key = "seeders";
        } else {
            $key = "leechers";
        }
        $torrents[$row["torrent"]][$key] = $row["c"];
    }
    $res = sql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["torrent"]]["comments"] = $row["c"];
    }
    $fields = explode(":", "comments:leechers:seeders");
    $res = sql_query("SELECT id, seeders, leechers, comments FROM torrents") or sqlerr(__FILE__, __LINE__);
    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)) {
            sql_query("UPDATE torrents SET " . implode(", ", $update) . " WHERE id = {$id}") or sqlerr(__FILE__, __LINE__);
        }
    }
    //delete inactive user accounts
    $secs = 31 * 86400;
    $dt = sqlesc(get_date_time(gmtime() - $secs));
    $maxclass = UC_POWER_USER;
    $res = sql_query("SELECT id FROM users WHERE parked='no' AND status='confirmed' AND class <= {$maxclass} AND last_access < {$dt} AND last_access <> '0000-00-00 00:00:00'") or sqlerr(__FILE__, __LINE__);
    while ($arr = mysql_fetch_assoc($res)) {
        sql_query("DELETE FROM users WHERE id = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM messages WHERE receiver = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM friends WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM friends WHERE friendid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM blocks WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM blocks WHERE blockid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM bookmarks WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM invites WHERE inviter = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM peers WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM readtorrents WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM simpaty WHERE fromuserid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM checkcomm WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
    }
    //delete parked user accounts
    $secs = 175 * 86400;
    // change the time to fit your needs
    $dt = sqlesc(get_date_time(gmtime() - $secs));
    $maxclass = UC_POWER_USER;
    $res = sql_query("SELECT id FROM users WHERE parked='yes' AND status='confirmed' AND class <= {$maxclass} AND last_access < {$dt}");
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_array($res)) {
            sql_query("DELETE FROM users WHERE id = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM messages WHERE receiver = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM friends WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM friends WHERE friendid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM blocks WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM blocks WHERE blockid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM bookmarks WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM invites WHERE inviter = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM peers WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM readtorrents WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM simpaty WHERE fromuserid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM checkcomm WHERE userid = " . sqlesc($arr["id"])) or sqlerr(__FILE__, __LINE__);
        }
    }
    // delete unconfirmed users if timeout.
    $deadtime = TIMENOW - $signup_timeout;
    $res = sql_query("SELECT id FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME({$deadtime}) AND last_login < FROM_UNIXTIME({$deadtime}) AND last_access < FROM_UNIXTIME({$deadtime})") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_array($res)) {
            sql_query("DELETE FROM users WHERE id = " . sqlesc($arr["id"]));
        }
    }
    // Update seed bonus
    sql_query("UPDATE users SET bonus = bonus + {$points_per_cleanup} WHERE users.id IN (SELECT userid FROM peers WHERE seeder = 'yes')") or sqlerr(__FILE__, __LINE__);
    //remove expired warnings
    $now = sqlesc(get_date_time());
    $modcomment = sqlesc(date("Y-m-d") . " - Предупреждение снято системой по таймауту.\n");
    $msg = sqlesc("Ваше предупреждение снято по таймауту. Постарайтесь больше не получать предупреждений и сделовать правилам.\n");
    sql_query("INSERT INTO messages (sender, receiver, added, msg, poster) SELECT 0, id, {$now}, {$msg}, 0 FROM users WHERE warned='yes' AND warneduntil < NOW() AND warneduntil <> '0000-00-00 00:00:00'") or sqlerr(__FILE__, __LINE__);
    sql_query("UPDATE users SET warned='no', warneduntil = '0000-00-00 00:00:00', modcomment = CONCAT({$modcomment}, modcomment) WHERE warned='yes' AND warneduntil < NOW() AND warneduntil <> '0000-00-00 00:00:00'") or sqlerr(__FILE__, __LINE__);
    //remove expired bans
    $modcomment = sqlesc(date("Y-m-d") . " - Включен системой по истечению бана.\n");
    sql_query("UPDATE users SET enabled = 'yes', modcomment = CONCAT({$modcomment}, modcomment) WHERE id IN (SELECT userid FROM users_ban WHERE disuntil < NOW() AND disuntil != '0000-00-00 00:00:00')") or sqlerr(__FILE__, __LINE__);
    sql_query("DELETE FROM users_ban WHERE disuntil < NOW() AND disuntil != '0000-00-00 00:00:00'") or sqlerr(__FILE__, __LINE__);
    // promote to power users
    $limit = 25 * 1024 * 1024 * 1024;
    $minratio = 1.05;
    $maxdt = sqlesc(get_date_time(gmtime() - 86400 * 28));
    $now = sqlesc(get_date_time());
    $msg = sqlesc("Наши поздравления, вы были авто-повышены до ранга [b]Опытный пользовать[/b].");
    $subject = sqlesc("Вы были повышены");
    $modcomment = sqlesc(date("Y-m-d") . " - Повышен до уровня \"" . $tracker_lang["class_power_user"] . "\" системой.\n");
    sql_query("INSERT INTO messages (sender, receiver, added, msg, poster, subject) SELECT 0, id, {$now}, {$msg}, 0, {$subject} FROM users WHERE class = " . UC_USER . " AND uploaded >= {$limit} AND uploaded / downloaded >= {$minratio} AND added < {$maxdt}") or sqlerr(__FILE__, __LINE__);
    sql_query("UPDATE users SET class = " . UC_POWER_USER . ", modcomment = CONCAT({$modcomment}, modcomment) WHERE class = " . UC_USER . " AND uploaded >= {$limit} AND uploaded / downloaded >= {$minratio} AND added < {$maxdt}") or sqlerr(__FILE__, __LINE__);
    // demote from power users
    $minratio = 0.95;
    $now = sqlesc(get_date_time());
    $msg = sqlesc("Вы были авто-понижены с ранга [b]Опытный пользователь[/b] до ранга [b]Пользователь[/b] потому-что ваш рейтинг упал ниже [b]{$minratio}[/b].");
    $subject = sqlesc("Вы были понижены");
    $modcomment = sqlesc(date("Y-m-d") . " - Понижен до уровня \"" . $tracker_lang["class_user"] . "\" системой.\n");
    sql_query("INSERT INTO messages (sender, receiver, added, msg, poster, subject) SELECT 0, id, {$now}, {$msg}, 0, {$subject} FROM users WHERE class = " . UC_POWER_USER . " AND uploaded / downloaded < {$minratio}") or sqlerr(__FILE__, __LINE__);
    sql_query("UPDATE users SET class = " . UC_USER . ", modcomment = CONCAT({$modcomment}, modcomment) WHERE class = " . UC_POWER_USER . " AND uploaded / downloaded < {$minratio}") or sqlerr(__FILE__, __LINE__);
    // delete old torrents
    if ($use_ttl) {
        $dt = sqlesc(get_date_time(gmtime() - $ttl_days * 86400));
        $res = sql_query("SELECT id, name, image1, image2, image3, image4, image5 FROM torrents WHERE added < {$dt}") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            unlink("{$torrent_dir}/{$arr['id']}.torrent");
            for ($x = 1; $x <= 5; $x++) {
                if ($arr['image' . $x] != "") {
                    unlink('torrents/images/' . $arr['image' . $x]);
                }
            }
            sql_query("DELETE FROM torrents WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM snatched WHERE torrent={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM peers WHERE torrent={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM comments WHERE torrent={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM files WHERE torrent={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM ratings WHERE torrent={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM checkcomm WHERE checkid={$arr['id']} AND torrent = 1") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM bookmarks WHERE torrentid={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            write_log("Торрент {$arr['id']} ({$arr['name']}) был удален системой (старше чем {$ttl_days} дней)", "", "torrent");
        }
    }
    // delete old regimage codes
    $secs = 1 * 86400;
    $dt = time() - $secs;
    sql_query("DELETE FROM captcha WHERE dateline < {$dt}") or sqlerr(__FILE__, __LINE__);
    $secs = 1 * 3600;
    $dt = time() - $secs;
    sql_query("DELETE FROM sessions WHERE time < {$dt}") or sqlerr(__FILE__, __LINE__);
}
Exemplo n.º 7
0
function docleanup()
{
    global $INSTALLER09, $queries, $mc1;
    set_time_limit(1200);
    $result = sql_query("show processlist") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_array($result)) {
        if ($row["Time"] > 100 || $row["Command"] == "Sleep") {
            $sql = "kill " . $row["Id"] . "";
            sql_query($sql) or sqlerr(__FILE__, __LINE__);
        }
    }
    ignore_user_abort(1);
    do {
        $res = sql_query("SELECT id FROM torrents");
        $ar = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            $ar[$id] = 1;
        }
        if (!count($ar)) {
            break;
        }
        $dp = opendir($INSTALLER09['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 = $INSTALLER09['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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")");
        }
        $res = sql_query("SELECT torrent FROM peers GROUP BY torrent");
        $delids = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")");
        }
        $res = sql_query("SELECT torrent FROM files GROUP BY torrent");
        $delids = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
        }
    } while (0);
    $deadtime = deadtime();
    sql_query("DELETE FROM peers WHERE last_action < {$deadtime}");
    $deadtime = TIME_NOW - $INSTALLER09['max_dead_torrent_time'];
    sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < {$deadtime}");
    $deadtime = TIME_NOW - $INSTALLER09['signup_timeout'];
    sql_query("DELETE FROM users WHERE status = 'pending' AND added < {$deadtime} AND last_login < {$deadtime} AND last_access < {$deadtime}");
    /** sync torrent counts - pdq **/
    $tsql = 'SELECT t.id, t.seeders, (
  SELECT COUNT(*)
  FROM peers
  WHERE torrent = t.id AND seeder = "yes"
) AS seeders_num,
t.leechers, (
  SELECT COUNT(*)
  FROM peers
  WHERE torrent = t.id
  AND seeder = "no"
) AS leechers_num,
t.comments, (
  SELECT COUNT(*)
  FROM comments
  WHERE torrent = t.id
) AS comments_num
FROM torrents AS t
ORDER BY t.id ASC';
    $updatetorrents = array();
    $tq = sql_query($tsql);
    while ($t = mysql_fetch_assoc($tq)) {
        if ($t['seeders'] != $t['seeders_num'] || $t['leechers'] != $t['leechers_num'] || $t['comments'] != $t['comments_num']) {
            $updatetorrents[] = '(' . $t['id'] . ', ' . $t['seeders_num'] . ', ' . $t['leechers_num'] . ', ' . $t['comments_num'] . ')';
        }
    }
    mysql_free_result($tq);
    if (count($updatetorrents)) {
        sql_query('INSERT INTO torrents (id, seeders, leechers, comments) VALUES ' . implode(', ', $updatetorrents) . ' ON DUPLICATE KEY UPDATE seeders = VALUES(seeders), leechers = VALUES(leechers), comments = VALUES(comments)');
    }
    unset($updatetorrents);
    //=== Update karma seeding bonus... made nicer by devinkray :D
    //==   Updated and optimized by pdq :)
    //=== Using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give the karma for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
    ///====== Seeding bonus per torrent
    $res = sql_query('SELECT COUNT(torrent) As tcount, userid, seedbonus FROM peers LEFT JOIN users ON users.id = userid WHERE seeder = "yes" AND connectable = "yes" GROUP BY userid') or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000) {
                $arr['tcount'] = 5;
            }
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
            $update['seedbonus'] = $arr['seedbonus'] + 0.225 * $arr['tcount'];
            $mc1->begin_transaction('MyUser_' . $arr['userid']);
            $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
            $mc1->commit_transaction(900);
            $mc1->begin_transaction('user' . $arr['userid']);
            $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
            $mc1->commit_transaction(900);
        }
        $count = count($users_buffer);
        if ($count > 0) {
            sql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
            write_log("Cleanup - " . $count . " users received seedbonus");
        }
        unset($users_buffer, $update, $count);
    }
    //== End
    //==Irc idle mod - pdq
    $res = sql_query("SELECT id, seedbonus, irctotal FROM users WHERE onirc = 'yes'") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            $users_buffer[] = '(' . $arr['id'] . ',0.225,' . $INSTALLER09['autoclean_interval'] . ')';
            // .250 karma
            //$users_buffer[] = '('.$arr['id'].',15728640,'.$INSTALLER09['autoclean_interval'].')'; // 15 mb
            $update['seedbonus'] = $arr['seedbonus'] + 0.225;
            $update['irctotal'] = $arr['irctotal'] + $INSTALLER09['autoclean_interval'];
            $mc1->begin_transaction('user' . $arr['id']);
            $mc1->update_row(false, array('seedbonus' => $update['seedbonus'], 'irctotal' => $update['irctotal']));
            $mc1->commit_transaction(900);
            $mc1->begin_transaction('MyUser_' . $arr['id']);
            $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
            $mc1->commit_transaction(900);
        }
        $count = count($users_buffer);
        if ($count > 0) {
            sql_query("INSERT INTO users (id,seedbonus,irctotal) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus),irctotal=irctotal+values(irctotal)") or sqlerr(__FILE__, __LINE__);
            //sql_query("INSERT INTO users (id,uploaded,irctotal) VALUES ".implode(', ',$users_buffer)." ON DUPLICATE key UPDATE uploaded=uploaded+values(uploaded),irctotal=irctotal+values(irctotal)") or sqlerr(__FILE__,__LINE__);
            write_log("Cleanup " . $count . " users idling on IRC");
        }
        unset($users_buffer, $update, $count);
    }
    //== End
    //== 09 Stats
    $registered = get_row_count('users');
    $unverified = get_row_count('users', "WHERE status='pending'");
    $torrents = get_row_count('torrents');
    $seeders = get_row_count('peers', "WHERE seeder='yes'");
    $leechers = get_row_count('peers', "WHERE seeder='no'");
    $torrentstoday = get_row_count('torrents', 'WHERE added > ' . time() . ' - 86400');
    $donors = get_row_count('users', "WHERE donor ='yes'");
    $unconnectables = get_row_count("peers", " WHERE connectable='no'");
    $forumposts = get_row_count("posts");
    $forumtopics = get_row_count("topics");
    $dt = sqlesc(time() - 300);
    // Active users last 5 minutes
    $numactive = get_row_count("users", "WHERE last_access >= {$dt}");
    $torrentsmonth = get_row_count('torrents', 'WHERE added > ' . time() . ' - 2592000');
    $gender_na = get_row_count('users', "WHERE gender='N/A'");
    $gender_male = get_row_count('users', "WHERE gender='Male'");
    $gender_female = get_row_count('users', "WHERE gender='Female'");
    $powerusers = get_row_count('users', "WHERE class='1'");
    $disabled = get_row_count('users', "WHERE enabled='no'");
    $uploaders = get_row_count('users', "WHERE class='3'");
    $moderators = get_row_count('users', "WHERE class='4'");
    $administrators = get_row_count('users', "WHERE class='5'");
    $sysops = get_row_count('users', "WHERE class='6'");
    sql_query("UPDATE stats SET regusers = '{$registered}', unconusers = '{$unverified}', torrents = '{$torrents}', seeders = '{$seeders}', leechers = '{$leechers}', unconnectables = '{$unconnectables}', torrentstoday = '{$torrentstoday}', donors = '{$donors}', forumposts = '{$forumposts}', forumtopics = '{$forumtopics}', numactive = '{$numactive}', torrentsmonth = '{$torrentsmonth}', gender_na = '{$gender_na}', gender_male = '{$gender_male}', gender_female = '{$gender_female}', powerusers = '{$powerusers}', disabled = '{$disabled}', uploaders = '{$uploaders}', moderators = '{$moderators}', administrators = '{$administrators}', sysops = '{$sysops}' WHERE id = '1' LIMIT 1");
    //=== delete from now viewing after 15 minutes
    sql_query('DELETE FROM now_viewing WHERE added < ' . (time() - 900));
    //=== fix any messed up counts
    $forums = sql_query('SELECT f.id, count( DISTINCT t.id ) AS topics, count(p.id) AS posts
                          FROM forums f
                          LEFT JOIN topics t ON f.id = t.forum_id
                          LEFT JOIN posts p ON t.id = p.topic_id
                          GROUP BY f.id');
    while ($forum = mysql_fetch_assoc($forums)) {
        $forum['posts'] = $forum['topics'] > 0 ? $forum['posts'] : 0;
        sql_query('update forums set post_count = ' . $forum['posts'] . ', topic_count = ' . $forum['topics'] . ' where id=' . $forum['id']);
    }
    write_log("Autoclean-------------------- Auto cleanup Complete using {$queries} queries --------------------");
}
Exemplo n.º 8
0
function docleanup($forceAll = 0, $printProgress = false)
{
    //require_once(get_langfile_path("cleanup.php",true));
    global $lang_cleanup_target;
    global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $autoclean_interval_one, $autoclean_interval_two, $autoclean_interval_three, $autoclean_interval_four, $autoclean_interval_five, $SITENAME, $bonus, $invite_timeout, $offervotetimeout_main, $offeruptimeout_main, $iniupload_main;
    global $donortimes_bonus, $perseeding_bonus, $maxseeding_bonus, $tzero_bonus, $nzero_bonus, $bzero_bonus, $l_bonus;
    global $expirehalfleech_torrent, $expirefree_torrent, $expiretwoup_torrent, $expiretwoupfree_torrent, $expiretwouphalfleech_torrent, $expirethirtypercentleech_torrent, $expirenormal_torrent, $hotdays_torrent, $hotseeder_torrent, $halfleechbecome_torrent, $freebecome_torrent, $twoupbecome_torrent, $twoupfreebecome_torrent, $twouphalfleechbecome_torrent, $thirtypercentleechbecome_torrent, $normalbecome_torrent, $deldeadtorrent_torrent;
    global $neverdelete_account, $neverdeletepacked_account, $deletepacked_account, $deleteunpacked_account, $deletenotransfer_account, $deletenotransfertwo_account, $deletepeasant_account, $psdlone_account, $psratioone_account, $psdltwo_account, $psratiotwo_account, $psdlthree_account, $psratiothree_account, $psdlfour_account, $psratiofour_account, $psdlfive_account, $psratiofive_account, $putime_account, $pudl_account, $puprratio_account, $puderatio_account, $eutime_account, $eudl_account, $euprratio_account, $euderatio_account, $cutime_account, $cudl_account, $cuprratio_account, $cuderatio_account, $iutime_account, $iudl_account, $iuprratio_account, $iuderatio_account, $vutime_account, $vudl_account, $vuprratio_account, $vuderatio_account, $exutime_account, $exudl_account, $exuprratio_account, $exuderatio_account, $uutime_account, $uudl_account, $uuprratio_account, $uuderatio_account, $nmtime_account, $nmdl_account, $nmprratio_account, $nmderatio_account, $getInvitesByPromotion_class;
    global $enablenoad_advertisement, $noad_advertisement;
    global $Cache;
    global $memcache;
    set_time_limit(0);
    ignore_user_abort(1);
    $now = time();
    //Priority Class 1: cleanup every 15 mins
    //2.update peer status
    $deadtime = deadtime();
    $deadtime = date("Y-m-d H:i:s", $deadtime);
    sql_query("DELETE FROM peers WHERE last_action < " . sqlesc($deadtime)) or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress('更新种子状态last_action');
    }
    //11.calculate seeding bonus
    $res = sql_query("SELECT DISTINCT userid FROM peers WHERE seeder = 'yes'") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        $sqrtof2 = sqrt(2);
        $logofpointone = log(0.1);
        $valueone = $logofpointone / $tzero_bonus;
        $pi = 3.141592653589793;
        $valuetwo = $bzero_bonus * (2 / $pi);
        $valuethree = $logofpointone / ($nzero_bonus - 1);
        $timenow = TIMENOW;
        $sectoweek = 7 * 24 * 60 * 60;
        while ($arr = mysql_fetch_assoc($res)) {
            $A = 0;
            $count = 0;
            $all_bonus = 0;
            $torrentres = sql_query("select torrents.added, torrents.size, torrents.seeders from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = {$arr['userid']} AND peers.seeder ='yes'") or sqlerr(__FILE__, __LINE__);
            while ($torrent = mysql_fetch_array($torrentres)) {
                $weeks_alive = ($timenow - strtotime($torrent['added'])) / $sectoweek;
                $gb_size = $torrent['size'] / 1073741824;
                $temp = (1 - exp($valueone * $weeks_alive)) * $gb_size * (1 + $sqrtof2 * exp($valuethree * ($torrent['seeders'] - 1)));
                $A += $temp;
                $count++;
            }
            if ($count > $maxseeding_bonus) {
                $count = $maxseeding_bonus;
            }
            $all_bonus = ($valuetwo * atan($A / $l_bonus) + $perseeding_bonus * $count) / (3600 / $autoclean_interval_one);
            $is_donor = get_single_value("users", "donor", "WHERE id=" . $arr['userid']);
            if ($is_donor == 'yes' && $donortimes_bonus > 0) {
                $all_bonus = $all_bonus * $donortimes_bonus;
            }
            KPS("+", $all_bonus, $arr["userid"]);
        }
    }
    if ($printProgress) {
        printProgress('为做种用户发放奖励');
    }
    //Priority Class 2: cleanup every 30 mins
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime2'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime2'," . sqlesc($now) . ")") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_two > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 1';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime2'") or sqlerr(__FILE__, __LINE__);
    }
    //2.5.update torrents' visibility
    $deadtime = deadtime() - $max_dead_torrent_time;
    sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME({$deadtime}) AND seeders=0") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("更新种子的可见状态");
    }
    //Priority Class 3: cleanup every 60 mins
    //自动清理过期短信
    if (time() > 1323069888 + 604800) {
        $deltime = date("Y-m-d H:i:s", time() - 2592000);
        //一个月前时间
        sql_query("DELETE FROM messages WHERE location='1' AND added < '" . $deltime . "'") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("清理过期短信");
    }
    //自动清理过期短信结束
    //将超过三天未登录用户重置salarynum
    $deltime = date("Y-m-d", time() - 86400 * 3);
    $res = sql_query("SELECT id FROM users WHERE  salarynum > 1 AND salary < '" . $deltime . "'") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        sql_query("UPDATE users SET salarynum = 1 WHERE id = {$row['id']}") or sqlerr(__FILE__, __LINE__);
        //write_log("系统qinglile yonghu salrty--- $row[id]",'normal');
    }
    if ($printProgress) {
        printProgress("将超过三天未登录用户重置salarynum");
    }
    //自动清理过期的回收站、候选区种子
    $deltime = date("Y-m-d H:i:s", time() - 86400 * 15);
    //这里默认15天
    $res = sql_query("SELECT id, name, status, owner FROM torrents WHERE status != 'normal' AND last_status < '" . $deltime . "'") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_array($res)) {
        deletetorrent($row['id']);
        $beforeStatus = getTorrentStatus($row['status']);
        sendMessage(0, $row['owner'], "你在回收站里的种子被删除了", "系统自动清理:{$beforeStatus} 种子 {$row['id']} ({$row['name']}) 被系统自动删除了 。原因是: (长时间未处理)");
        write_log("系统自动清理:{$beforeStatus} 种子 {$row['id']} ({$row['name']}) 被系统自动删除了 。原因是: (长时间未处理)", 'normal');
        //sendDelMsg($row['id'], "系统自动清理:$beforeStatus 种子 $row[id] ($row[name]) 被系统自动删除了 。原因是: (长时间未处理)", 'delete');
    }
    if ($printProgress) {
        printProgress("回收站过期种子清理");
    }
    //自动清理回收站,候选区结束
    //Priority Class 3: cleanup every 60 mins
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime3'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime3',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_three > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 2';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime3'") or sqlerr(__FILE__, __LINE__);
    }
    //4.update count of seeders, leechers, comments for torrents
    $torrents = array();
    $res = sql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        if ($row["seeder"] == "yes") {
            $key = "seeders";
        } else {
            $key = "leechers";
        }
        $torrents[$row["torrent"]][$key] = $row["c"];
    }
    $res = sql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_assoc($res)) {
        $torrents[$row["torrent"]]["comments"] = $row["c"];
    }
    $fields = explode(":", "comments:leechers:seeders");
    $res = sql_query("SELECT id, seeders, leechers, comments FROM torrents") or sqlerr(__FILE__, __LINE__);
    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)) {
            sql_query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = {$id}") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("更新做种、下载、评论数量");
    }
    //set no-advertisement-by-bonus time out
    sql_query("UPDATE users SET noad='no' WHERE noaduntil < " . sqlesc(date("Y-m-d H:i:s")) . ($enablenoad_advertisement == 'yes' ? " AND class < " . sqlesc($noad_advertisement) : ""));
    if ($printProgress) {
        printProgress("判断麦粒购买不显示广告截止set no-advertisement-by-bonus time out");
    }
    //12. update forum post/topic count
    $forums = sql_query("select id from forums") or sqlerr(__FILE__, __LINE__);
    while ($forum = mysql_fetch_assoc($forums)) {
        $postcount = 0;
        $topiccount = 0;
        $topics = sql_query("select id from topics where forumid={$forum['id']}") or sqlerr(__FILE__, __LINE__);
        while ($topic = mysql_fetch_assoc($topics)) {
            $res = sql_query("select count(*) from posts where topicid={$topic['id']}") or sqlerr(__FILE__, __LINE__);
            $arr = mysql_fetch_row($res);
            $postcount += $arr[0];
            ++$topiccount;
        }
        sql_query("update forums set postcount={$postcount}, topiccount={$topiccount} where id={$forum['id']}") or sqlerr(__FILE__, __LINE__);
    }
    $Cache->delete_value('forums_list');
    if ($printProgress) {
        printProgress("更新论坛主题/帖子数量update forum post/topic count");
    }
    //14.cleanup offers
    //Delete offers if not voted on after some time
    if ($offervotetimeout_main) {
        $secs = (int) $offervotetimeout_main;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $offervotetimeout_main));
        $res = sql_query("SELECT id, name FROM offers WHERE added < {$dt} AND allowed <> 'allowed'") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("DELETE FROM offers WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM offervotes WHERE offerid={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM comments WHERE offer={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            write_log("系统自动清理:Offer {$arr['id']} ({$arr['name']}) was deleted by system (vote timeout)", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("删除候选(无用)delete offers if not voted on after some time");
    }
    //Delete offers if not uploaded after being voted on for some time.
    if ($offeruptimeout_main) {
        $secs = (int) $offeruptimeout_main;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $res = sql_query("SELECT id, name FROM offers WHERE allowedtime < {$dt} AND allowed = 'allowed'") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("DELETE FROM offers WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM offervotes WHERE offerid={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("DELETE FROM comments WHERE offer={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            write_log("系统自动清理:Offer {$arr['id']} ({$arr['name']}) was deleted by system (upload timeout)", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("删除候选(无用)delete offers if not uploaded after being voted on for some time.");
    }
    //15.cleanup torrents
    //Start: expire torrent promotion
    function torrent_promotion_expire($days, $type = 2, $targettype = 1)
    {
        $secs = (int) ($days * 86400);
        //XX days
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $res = sql_query("SELECT id, name FROM torrents WHERE added < {$dt} AND sp_state = " . sqlesc($type) . ' AND promotion_time_type=0') or sqlerr(__FILE__, __LINE__);
        switch ($targettype) {
            case 1:
                $sp_state = 1;
                $become = "normal";
                break;
            case 2:
                $sp_state = 2;
                $become = "Free";
                break;
            case 3:
                $sp_state = 3;
                $become = "2X";
                break;
            case 4:
                $sp_state = 4;
                $become = "2X Free";
                break;
            case 5:
                $sp_state = 5;
                $become = "50%";
                break;
            case 6:
                $sp_state = 6;
                $become = "2X 50%";
                break;
            default:
                $sp_state = 1;
                $become = "normal";
                break;
        }
        while ($arr = mysql_fetch_assoc($res)) {
            sql_query("UPDATE torrents SET sp_state = " . sqlesc($sp_state) . " WHERE id={$arr['id']}") or sqlerr(__FILE__, __LINE__);
            if ($sp_state == 1) {
                write_log("系统自动清理:Torrent {$arr['id']} ({$arr['name']}) is no longer on promotion (time expired)", 'normal');
            } else {
                write_log("系统自动清理:Promotion type for torrent {$arr['id']} ({$arr['name']}) is changed to " . $become . " (time expired)", 'normal');
            }
        }
    }
    if ($expirehalfleech_torrent) {
        torrent_promotion_expire($expirehalfleech_torrent, 5, $halfleechbecome_torrent);
    }
    if ($expirefree_torrent) {
        torrent_promotion_expire($expirefree_torrent, 2, $freebecome_torrent);
    }
    if ($expiretwoup_torrent) {
        torrent_promotion_expire($expiretwoup_torrent, 3, $twoupbecome_torrent);
    }
    if ($expiretwoupfree_torrent) {
        torrent_promotion_expire($expiretwoupfree_torrent, 4, $twoupfreebecome_torrent);
    }
    if ($expiretwouphalfleech_torrent) {
        torrent_promotion_expire($expiretwouphalfleech_torrent, 6, $twouphalfleechbecome_torrent);
    }
    if ($expirethirtypercentleech_torrent) {
        torrent_promotion_expire($expirethirtypercentleech_torrent, 7, $thirtypercentleechbecome_torrent);
    }
    if ($expirenormal_torrent) {
        torrent_promotion_expire($expirenormal_torrent, 1, $normalbecome_torrent);
    }
    //expire individual torrent promotion
    sql_query("UPDATE torrents SET sp_state = 1, promotion_time_type=0, promotion_until='0000-00-00 00:00:00' WHERE promotion_time_type=2 AND promotion_until < " . sqlesc(date("Y-m-d H:i:s", TIMENOW))) or sqlerr(__FILE__, __LINE__);
    //End: expire torrent promotion
    if ($printProgress) {
        printProgress("判断种子促销截止expire torrent promotion");
    }
    //automatically pick hot
    if ($hotdays_torrent) {
        $secs = (int) ($hotdays_torrent * 86400);
        //XX days
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        sql_query("UPDATE torrents SET picktype = 'hot' WHERE added > {$dt} AND picktype = 'normal' AND seeders > " . sqlesc($hotseeder_torrent)) or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("判断种子是否热门automatically pick hot");
    }
    //Priority Class 4: cleanup every 24 hours
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime4'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime4',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_four > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 3';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime4'") or sqlerr(__FILE__, __LINE__);
    }
    //3.delete unconfirmed accounts
    $deadtime = time() - $signup_timeout;
    $delres = sql_query("SELECT id, username FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME({$deadtime}) AND last_login < FROM_UNIXTIME({$deadtime}) AND last_access < FROM_UNIXTIME({$deadtime})");
    while ($userinfo = mysql_fetch_assoc($delres)) {
        record_op_log(0, $userinfo['id'], $userinfo['username'], 'del', '账号审核未通过');
        sql_query("DELETE FROM users WHERE id='" . $userinfo['id'] . "'") or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("删除未通过审核的账户delete unconfirmed accounts");
    }
    //5.delete old login attempts
    $secs = 12 * 60 * 60;
    // Delete failed login attempts per half day.
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    // calculate date.
    sql_query("DELETE FROM loginattempts WHERE banned='no' AND added < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("删除长时间未活动的附件delete old login attempts");
    }
    //6.delete old invite codes
    $secs = $invite_timeout * 24 * 60 * 60;
    // when?
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    // calculate date.
    sql_query("DELETE FROM invites WHERE time_invited < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("删除未发布的邀请码delete old invite codes");
    }
    //7.delete regimage codes
    sql_query("TRUNCATE TABLE `regimages`") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete regimage codes");
    }
    //10.clean up user accounts
    $deletetitle = '你在麦田pt的账号将要被删除';
    $body = "你好,你在麦田pt注册的账号 {$userinfo['username']}  已经连续 " . $deletenotransfer_account * 0.8 . "天未登录,由于你的账号没有产生流量,如果连续{$deletenotransfer_account} 天未登录,你的账号将会被删除。\n 麦田pt期待您的回归,我们的地址是 pt.nwsuaf6.edu.cn  如果没有ipv6环境可以使用pt.nwsuaf6.edu.cn.ipv4.sixxs.org 登陆账号";
    // make sure VIP or above never get deleted
    $neverdelete_account = $neverdelete_account <= UC_VIP ? $neverdelete_account : UC_VIP;
    //delete inactive user accounts, no transfer. Alt. 1: last access time
    if ($deletenotransfer_account) {
        $secs = $deletenotransfer_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $maxclass = $neverdelete_account;
        /*发邮件
        		$secs2 = $secs*0.8;
        		$dt2 = sqlesc(date("Y-m-d H:i:s",(TIMENOW - $secs2)));
        		$delres = sql_query("SELECT id, username, email FROM users WHERE parked='no' AND status='confirmed' AND class < $maxclass AND last_access < $dt2 AND (uploaded = 0 || uploaded = ".sqlesc($iniupload_main).") AND downloaded = 0");
        		$notransfererr=0;$notransferdone=0;
        		$notransferemail = array();
        		while ($userinfo = mysql_fetch_assoc($delres)){
                            if(	sent_mail($userinfo['email'], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, false, '', get_email_encode(get_langfolder_cookie()),'noerror'))
        						{$emailflag =1;$notransferdone++;}
        					else
        					{$emailflag =0;
        					$notransfererr++;continue;}
        				$notransferemail[] = $userinfo['email'];$notransfererr++;
                        }
        			if(sent_mail($notransferemail[0], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, true, $notransferemail, get_email_encode(get_langfolder_cookie()),'noerror'))	
        			write_log("系统自动发送邮件:给$notransfererr 位注册后连续".$deletenotransfer_account*0.8 ." 天未登录且无流量用户发送邮件提醒",'normal');
        			else write_log("系统自动发送邮件:有$notransfererr 位无流量用户发送邮件失败!!!!!!!$notransferdone 位成功".__LINE__,'normal');
        			if ($printProgress) {
        		printProgress("给注册后连续".$deletenotransfer_account*0.8 ." 天未登录且无流量用户发送邮件提醒");
        	}
        	*/
        $delres = sql_query("SELECT id, username FROM users WHERE parked='no' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt} AND (uploaded = 0 || uploaded = " . sqlesc($iniupload_main) . ") AND downloaded = 0");
        while ($userinfo = mysql_fetch_assoc($delres)) {
            record_op_log(0, $userinfo['id'], $userinfo['username'], 'del', '无流量账号连续' . $deletenotransfer_account . '天未登录');
            sql_query("DELETE FROM users WHERE id='" . $userinfo['id'] . "'") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("删除无流量未登录用户delete inactive user accounts, no transfer. Alt. 1: last access time");
    }
    //delete inactive user accounts, no transfer. Alt. 2: registering time
    if ($deletenotransfertwo_account) {
        $secs = $deletenotransfertwo_account * 24 * 60 * 60;
        $secs2 = $secs * 1.8;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $dt2 = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs2));
        $maxclass = $neverdelete_account;
        /*发送email
        		$notransfererr1=0;$notransferdone1=0;
        		$notransferemail1 = array();
        		$delres = sql_query("SELECT id, username, email FROM users WHERE  parked='no' AND status='confirmed' AND class < $maxclass AND added < $dt2 AND (uploaded = 0 || uploaded = ".sqlesc($iniupload_main).") AND downloaded = 0");
                       while ($userinfo = mysql_fetch_assoc($delres)){
                            if(	sent_mail($userinfo['email'], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, false, '', get_email_encode(get_langfolder_cookie()),'noerror'))
        						$emailflag =1;
        					else
        					{$notransfererr1++;$emailflag =0;continue;}
        				$notransferemail1[] = $userinfo['email'];$notransfererr1++;
                        }
        			if(sent_mail($notransferemail1[0], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, true, $notransferemail1, get_email_encode(get_langfolder_cookie()),'noerror'))
        			write_log("系统自动发送邮件:给$notransfererr1 位注册后连续".$deletenotransfertwo_account*0.8 ." 天未登录且无流量用户发送邮件提醒",'normal');
        			else write_log("系统自动发送邮件:有$notransfererr1 位长时间未登录用户发送邮件失败!!!!$notransferdone1 位成功!!!".__LINE__,'normal');
        			if ($printProgress) {
        		printProgress("给注册后连续".$deletenotransfertwo_account*0.8 ." 天未登录用户发送邮件提醒");
        	}*/
        //delete
        $delres = sql_query("SELECT id, username FROM users WHERE  parked='no' AND status='confirmed' AND class < {$maxclass} AND added < {$dt} AND (uploaded = 0 || uploaded = " . sqlesc($iniupload_main) . ") AND downloaded = 0");
        while ($userinfo = mysql_fetch_assoc($delres)) {
            record_op_log(0, $userinfo['id'], $userinfo['username'], 'del', '注册' . $deletenotransfertwo_account . '天后仍未产生任何流量');
            sql_query("DELETE FROM users WHERE id='" . $userinfo['id'] . "'") or sqlerr(__FILE__, __LINE__);
        }
        if ($printProgress) {
            printProgress("删除无流量未登录用户delete inactive user accounts, no transfer. Alt. 2: registering time");
        }
    }
    //delete inactive user accounts, not parked
    if ($deleteunpacked_account) {
        $secs = $deleteunpacked_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $secs2 = $secs * 0.8;
        $dt2 = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs2));
        $maxclass = $neverdelete_account;
        /*
        	$uppackederrnum =0;$uppackeddonenum =0;
        	$uppackedemail = array();
        		$body = "你好,你在麦田pt注册的账号 {$userinfo['username']}  已经连续 ".$deleteunpacked_account*0.8 ."天未登录,由于你的账号没有封存,如果连续$deleteunpacked_account 天未登录,你的账号将会被删除。\n 麦田pt期待您的回归,我们的地址是 pt.nwsuaf6.edu.cn  如果没有ipv6环境可以使用pt.nwsuaf6.edu.cn.ipv4.sixxs.org 登陆账号";
                        $delres = sql_query("SELECT id, username,email FROM users WHERE parked='no' AND status='confirmed' AND class < $maxclass AND last_access < $dt2");
                        while ($userinfo = mysql_fetch_assoc($delres)){
                            if(	sent_mail($userinfo['email'], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, false, '', get_email_encode(get_langfolder_cookie()),'noerror'))
        			{$emailflag =1;$uppackeddonenum++;}
        				else
        				{$uppackederrnum++;$emailflag =0;continue;}
        			$uppackedemail[] = $userinfo['email'];$uppackederrnum++;
                        }
        		if(sent_mail($uppackedemail[0], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, true, $uppackedemail, get_email_encode(get_langfolder_cookie()),'noerror'))	
        		write_log("系统自动发送邮件:给$uppackederrnum 位未封存账号连续".$deleteunpacked_account*0.8 ." 天未登录用户发送邮件提醒",'normal');
        		else write_log("系统自动发送邮件:有$uppackederrnum 位封存长时间未登录用户发送邮件失败!!!$uppackeddonenum 位成功!!!!".__LINE__,'normal');
        		if ($printProgress) {
        	printProgress("给未封存账号连续".$deleteunpacked_account*0.8 ." 天未登录用户发送邮件提醒");
        }
        */
        //delete
        $delres = sql_query("SELECT id, username FROM users WHERE parked='no' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt}");
        while ($userinfo = mysql_fetch_assoc($delres)) {
            record_op_log(0, $userinfo['id'], $userinfo['username'], 'del', '未封存账号连续' . $deleteunpacked_account . '天未登录');
            sql_query("DELETE FROM users WHERE id='" . $userinfo['id'] . "'") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("删除未封存长时间未登录用户delete inactive user accounts, not parked");
    }
    //delete parked user accounts, parked
    if ($deletepacked_account) {
        $secs = $deletepacked_account * 24 * 60 * 60;
        $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
        $secs2 = $secs * 0.8;
        $dt2 = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs2));
        $maxclass = $neverdeletepacked_account;
        /*发邮件
        		$packednum=0;$packeddonenum=0;
        		 $packedemail[] = array();
        			$body ="你好,你在麦田pt注册的账号 {$userinfo['username']}  已经连续 ".$deletepacked_account*0.8 ."天未登录,虽然你的账号已经封存,但是如果连续$deletepacked_account 天未登录,你的账号仍然会被删除。\n 麦田pt期待您的回归,我们的地址是 pt.nwsuaf6.edu.cn  如果没有ipv6环境可以使用pt.nwsuaf6.edu.cn.ipv4.sixxs.org 登陆账号";
                        $delres = sql_query("SELECT id, username, email FROM users WHERE parked='yes' AND status='confirmed' AND class < $maxclass AND last_access < $dt2");
        				
                        while ($userinfo = mysql_fetch_assoc($delres)){
                            if(	sent_mail($userinfo['email'], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, false, '', get_email_encode(get_langfolder_cookie()),'noerror'))
        						{$emailflag =1;$packeddonenum++;}
        					else
        					{$emailflag =0;continue;}
        					$packednum++;
        					$packedemail[] = $userinfo['email'];
                        }
        				if (sent_mail($packedemail[0], $SITENAME, $SITEEMAIL, change_email_encode(get_langfolder_cookie(), $deletetitle), change_email_encode(get_langfolder_cookie(),$body), '', false, true, $packedemail, get_email_encode(get_langfolder_cookie()),'noerror'))
        			write_log("系统自动发送邮件:给$packednum 位封存账号连续".$deletepacked_account*0.8 ." 天未登录用户发送邮件提醒",'normal');
        			else write_log("系统自动发送邮件:有$packednum 位封存账号发送邮件失败!!!!!!!".__LINE__,'normal');
        			if ($printProgress) {
        		printProgress("给封存账号连续".$deletepacked_account*0.8 ." 天未登录用户发送邮件提醒");
        	}
        	*/
        //delete
        $delres = sql_query("SELECT id, username FROM users WHERE parked='yes' AND status='confirmed' AND class < {$maxclass} AND last_access < {$dt}");
        while ($userinfo = mysql_fetch_assoc($delres)) {
            record_op_log(0, $userinfo['id'], $userinfo['username'], 'del', '封存账号连续' . $deletepacked_account . '天未登录');
            sql_query("DELETE FROM users WHERE id='" . $userinfo['id'] . "'") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("删除长时间未登录的封存用户delete parked user accounts, parked");
    }
    //remove VIP status if time's up
    $res = sql_query("SELECT id, modcomment FROM users WHERE vip_added='yes' AND vip_until < NOW()") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            $subject = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_vip_status_removed']);
            $msg = sqlesc($lang_cleanup_target[get_user_lang($arr[id])]['msg_vip_status_removed_body']);
            ///---AUTOSYSTEM MODCOMMENT---//
            $modcomment = htmlspecialchars($arr["modcomment"]);
            $modcomment = date("Y-m-d") . " - VIP status removed by - AutoSystem.\n" . $modcomment;
            $modcom = sqlesc($modcomment);
            ///---end
            sql_query("UPDATE users SET class = '1', vip_added = 'no', vip_until = '0000-00-00 00:00:00', modcomment = {$modcom} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("INSERT INTO messages (sender, receiver, added, msg, subject) VALUES(0, {$arr['id']}, {$dt}, {$msg}, {$subject})") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("判断购买vip到期remove VIP status if time's up");
    }
    // promote peasant back to user
    function peasant_to_user($down_floor_gb, $down_roof_gb, $minratio)
    {
        global $lang_cleanup_target;
        if ($down_floor_gb) {
            $downlimit_floor = $down_floor_gb * 1024 * 1024 * 1024;
            $downlimit_roof = $down_roof_gb * 1024 * 1024 * 1024;
            $res = sql_query("SELECT id FROM users WHERE class = 0 AND downloaded >= {$downlimit_floor} " . ($downlimit_roof > $down_floor_gb ? " AND downloaded < {$downlimit_roof}" : "") . " AND uploaded / downloaded >= {$minratio}") or sqlerr(__FILE__, __LINE__);
            if (mysql_num_rows($res) > 0) {
                $dt = sqlesc(date("Y-m-d H:i:s"));
                while ($arr = mysql_fetch_assoc($res)) {
                    $subject = sqlesc($lang_cleanup_target[get_user_lang($arr['id'])]['msg_low_ratio_warning_removed']);
                    $msg = sqlesc($lang_cleanup_target[get_user_lang($arr['id'])]['msg_your_ratio_warning_removed']);
                    writecomment($arr['id'], "Leech Warning removed by System.");
                    sql_query("UPDATE users SET class = 1, leechwarn = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, {$subject}, {$msg})") or sqlerr(__FILE__, __LINE__);
                }
            }
        }
    }
    peasant_to_user($psdlfive_account, 0, $psratiofive_account);
    peasant_to_user($psdlfour_account, $psdlfive_account, $psratiofour_account);
    peasant_to_user($psdlthree_account, $psdlfour_account, $psratiothree_account);
    peasant_to_user($psdltwo_account, $psdlthree_account, $psratiotwo_account);
    peasant_to_user($psdlone_account, $psdltwo_account, $psratioone_account);
    if ($printProgress) {
        printProgress("土豆判断升级promote peasant back to user");
    }
    //end promote peasant back to user
    // start promotion
    function promotion($class, $down_floor_gb, $minratio, $time_week, $addinvite = 0)
    {
        global $lang_cleanup_target;
        $oriclass = $class - 1;
        if ($down_floor_gb) {
            $limit = $down_floor_gb * 1024 * 1024 * 1024;
            $maxdt = date("Y-m-d H:i:s", TIMENOW - 86400 * 7 * $time_week);
            $res = sql_query("SELECT id, max_class_once FROM users WHERE class = {$oriclass} AND downloaded >= {$limit} AND uploaded / downloaded >= {$minratio} AND added < " . sqlesc($maxdt)) or sqlerr(__FILE__, __LINE__);
            if (mysql_num_rows($res) > 0) {
                $dt = sqlesc(date("Y-m-d H:i:s"));
                while ($arr = mysql_fetch_assoc($res)) {
                    $subject = sqlesc($lang_cleanup_target[get_user_lang($arr['id'])]['msg_promoted_to'] . get_user_class_name($class, false, false, false));
                    $msg = sqlesc($lang_cleanup_target[get_user_lang($arr['id'])]['msg_now_you_are'] . get_user_class_name($class, false, false, false) . $lang_cleanup_target[get_user_lang($arr['id'])]['msg_see_faq']);
                    if ($class <= $arr[max_class_once]) {
                        sql_query("UPDATE users SET class = {$class} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    } else {
                        sql_query("UPDATE users SET class = {$class}, max_class_once={$class}, invites=invites+{$addinvite} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                    }
                    sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, {$subject}, {$msg})") or sqlerr(__FILE__, __LINE__);
                }
            }
        }
    }
    //do not change the ascending order
    promotion(UC_POWER_USER, $pudl_account, $puprratio_account, $putime_account, $getInvitesByPromotion_class[UC_POWER_USER]);
    promotion(UC_ELITE_USER, $eudl_account, $euprratio_account, $eutime_account, $getInvitesByPromotion_class[UC_ELITE_USER]);
    promotion(UC_CRAZY_USER, $cudl_account, $cuprratio_account, $cutime_account, $getInvitesByPromotion_class[UC_CRAZY_USER]);
    promotion(UC_INSANE_USER, $iudl_account, $iuprratio_account, $iutime_account, $getInvitesByPromotion_class[UC_INSANE_USER]);
    promotion(UC_VETERAN_USER, $vudl_account, $vuprratio_account, $vutime_account, $getInvitesByPromotion_class[UC_VETERAN_USER]);
    promotion(UC_EXTREME_USER, $exudl_account, $exuprratio_account, $exutime_account, $getInvitesByPromotion_class[UC_EXTREME_USER]);
    promotion(UC_ULTIMATE_USER, $uudl_account, $uuprratio_account, $uutime_account, $getInvitesByPromotion_class[UC_ULTIMATE_USER]);
    promotion(UC_NEXUS_MASTER, $nmdl_account, $nmprratio_account, $nmtime_account, $getInvitesByPromotion_class[UC_NEXUS_MASTER]);
    // end promotion
    if ($printProgress) {
        printProgress("升级判定promote users to other classes");
    }
    // start demotion
    function demotion($class, $deratio)
    {
        global $lang_cleanup_target;
        $newclass = $class - 1;
        $res = sql_query("SELECT id FROM users WHERE class = {$class} AND uploaded / downloaded < {$deratio}") or sqlerr(__FILE__, __LINE__);
        if (mysql_num_rows($res) > 0) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            while ($arr = mysql_fetch_assoc($res)) {
                $subject = $lang_cleanup_target[get_user_lang($arr['id'])]['msg_demoted_to'] . get_user_class_name($newclass, false, false, false);
                $msg = $lang_cleanup_target[get_user_lang($arr['id'])]['msg_demoted_from'] . get_user_class_name($class, false, false, false) . $lang_cleanup_target[get_user_lang($arr['id'])]['msg_to'] . get_user_class_name($newclass, false, false, false) . $lang_cleanup_target[get_user_lang($arr['id'])]['msg_because_ratio_drop_below'] . $deratio . ".\n";
                sql_query("UPDATE users SET class = {$newclass} WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            }
        }
    }
    //do not change the descending order
    demotion(UC_NEXUS_MASTER, $nmderatio_account);
    demotion(UC_ULTIMATE_USER, $uuderatio_account);
    demotion(UC_EXTREME_USER, $exuderatio_account);
    demotion(UC_VETERAN_USER, $vuderatio_account);
    demotion(UC_INSANE_USER, $iuderatio_account);
    demotion(UC_CRAZY_USER, $cuderatio_account);
    demotion(UC_ELITE_USER, $euderatio_account);
    demotion(UC_POWER_USER, $puderatio_account);
    if ($printProgress) {
        printProgress("降级判定demote users to other classes");
    }
    // end demotion
    // start demote users to peasant
    function user_to_peasant($down_floor_gb, $minratio)
    {
        global $lang_cleanup_target;
        global $deletepeasant_account;
        $length = $deletepeasant_account * 86400;
        // warn users until xxx days
        $until = date("Y-m-d H:i:s", TIMENOW + $length);
        $downlimit_floor = $down_floor_gb * 1024 * 1024 * 1024;
        $res = sql_query("SELECT id FROM users WHERE class = 1 AND downloaded > {$downlimit_floor} AND uploaded / downloaded < {$minratio}") or sqlerr(__FILE__, __LINE__);
        if (mysql_num_rows($res) > 0) {
            $dt = sqlesc(date("Y-m-d H:i:s"));
            while ($arr = mysql_fetch_assoc($res)) {
                $subject = $lang_cleanup_target[get_user_lang($arr['id'])]['msg_demoted_to'] . get_user_class_name(UC_PEASANT, false, false, false);
                $msg = $lang_cleanup_target[get_user_lang($arr['id'])]['msg_must_fix_ratio_within'] . $deletepeasant_account . $lang_cleanup_target[get_user_lang($arr['id'])]['msg_days_or_get_banned'];
                writecomment($arr['id'], "Leech Warned by System - Low Ratio.");
                sql_query("UPDATE users SET class = 0 , leechwarn = 'yes', leechwarnuntil = " . sqlesc($until) . " WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            }
        }
    }
    user_to_peasant($psdlone_account, $psratioone_account);
    user_to_peasant($psdltwo_account, $psratiotwo_account);
    user_to_peasant($psdlthree_account, $psratiothree_account);
    user_to_peasant($psdlfour_account, $psratiofour_account);
    user_to_peasant($psdlfive_account, $psratiofive_account);
    if ($printProgress) {
        printProgress("降级到土豆demote Users to peasant");
    }
    // end Users to Peasant
    //ban users with leechwarning expired
    $length = 1 * 86400;
    // warn users until xxx days
    $dt = date("Y-m-d H:i:s", TIMENOW - $length);
    $res = sql_query("SELECT id, seedbonus 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)) {
            $addup = $arr['seedbonus'] * 2222222;
            writeBonusComment($arr[id], "即将被封号,系统自动将所有麦粒换成上传量.");
            sql_query("UPDATE users SET uploaded = uploaded + {$addup},seedbonus = 0 WHERE id =" . $arr['id']) or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("系统自动将一天后即将被封号的土豆的所有麦粒换成上传量");
    }
    $dt = sqlesc(date("Y-m-d H:i:s"));
    // take date time
    $res = sql_query("SELECT id, username 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 Leech Warning expired.");
            record_op_log(0, $arr['id'], $arr['username'], 'ban', '持续未改善分享率');
            sql_query("UPDATE users SET enabled = 'no', leechwarnuntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("ban掉持续未改善分享率的土豆ban users with leechwarning expired");
    }
    //Remove warning of users//同时如果被禁言、禁止上传下载的话,一同恢复
    $dt = sqlesc(date("Y-m-d H:i:s"));
    // take date time
    $res = sql_query("SELECT id,forumpost,uploadpos,downloadpos FROM users WHERE enabled = 'yes' AND warned = 'yes' AND warneduntil < {$dt}") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            $subject = $lang_cleanup_target[get_user_lang($arr[id])]['msg_warning_removed'];
            $msg = $lang_cleanup_target[get_user_lang($arr[id])]['msg_your_warning_removed'];
            if ($arr['forumpost'] == 'no') {
                sql_query("UPDATE users SET forumpost = 'yes' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                $msg .= "。你的禁言同时被解除。";
            }
            if ($arr['uploadpos'] == 'no') {
                sql_query("UPDATE users SET uploadpos = 'yes' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                $msg .= "。你的禁止上传处罚同时被解除。";
            }
            if ($arr['downloadpos'] == 'no') {
                sql_query("UPDATE users SET downloadpos = 'yes' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
                $msg .= "。你的禁止下载处罚同时被解除。";
            }
            writecomment($arr[id], "Warning removed by System.");
            sql_query("UPDATE users SET warned = 'no', warneduntil = '0000-00-00 00:00:00' WHERE id = {$arr['id']}") or sqlerr(__FILE__, __LINE__);
            sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['id']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
        }
    }
    if ($printProgress) {
        printProgress("移除警告,同时移除处罚remove warning of users");
    }
    //17.update total seeding and leeching time of users
    $res = sql_query("SELECT * FROM users") or sqlerr(__FILE__, __LINE__);
    while ($arr = mysql_fetch_assoc($res)) {
        //die("s" . $arr['id']);
        $res2 = sql_query("SELECT SUM(seedtime) as st, SUM(leechtime) as lt FROM snatched where userid = " . $arr['id'] . " LIMIT 1") or sqlerr(__FILE__, __LINE__);
        $arr2 = mysql_fetch_assoc($res2) or sqlerr(__FILE__, __LINE__);
        //die("ss" . $arr2['st']);
        //die("sss" . "UPDATE users SET seedtime = " . $arr2['st'] . ", leechtime = " . $arr2['lt'] . " WHERE id = " . $arr['id']);
        sql_query("UPDATE users SET seedtime = " . intval($arr2['st']) . ", leechtime = " . intval($arr2['lt']) . " WHERE id = " . $arr['id']) or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("update total seeding and leeching time of users");
    }
    // delete torrents that have been dead for a long time
    if ($deldeadtorrent_torrent > 0) {
        $length = $deldeadtorrent_torrent * 86400;
        $until = date("Y-m-d H:i:s", TIMENOW - $length);
        $dt = sqlesc(date("Y-m-d H:i:s"));
        $res = sql_query("SELECT id, name, owner FROM torrents WHERE visible = 'no' AND last_action < " . sqlesc($until) . " AND seeders = 0 AND leechers = 0") or sqlerr(__FILE__, __LINE__);
        while ($arr = mysql_fetch_assoc($res)) {
            deletetorrent($arr['id']);
            $subject = $lang_cleanup_target[get_user_lang($arr[owner])]['msg_your_torrent_deleted'];
            $msg = $lang_cleanup_target[get_user_lang($arr[owner])]['msg_your_torrent'] . "[i]" . $arr['name'] . "[/i]" . $lang_cleanup_target[get_user_lang($arr[owner])]['msg_was_deleted_because_dead'];
            sql_query("INSERT INTO messages (sender, receiver, added, subject, msg) VALUES(0, {$arr['owner']}, {$dt}, " . sqlesc($subject) . ", " . sqlesc($msg) . ")") or sqlerr(__FILE__, __LINE__);
            write_log("系统自动清理:系统删除了断种 {$arr['id']} ({$arr['name']})", 'normal');
        }
    }
    if ($printProgress) {
        printProgress("清理断种delete torrents that have been dead for a long time");
    }
    //彩票开奖
    drawlotteryfun();
    if ($printProgress) {
        printProgress("彩票开奖");
    }
    //Priority Class 5: cleanup every 15 days
    $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime5'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime5',{$now})") or sqlerr(__FILE__, __LINE__);
        return;
    }
    $ts = $row[0];
    if ($ts + $autoclean_interval_five > $now && !$forceAll) {
        return 'Cleanup ends at Priority Class 4';
    } else {
        sql_query("UPDATE avps SET value_u = " . sqlesc($now) . " WHERE arg='lastcleantime5'") or sqlerr(__FILE__, __LINE__);
    }
    //update clients' popularity
    $res = sql_query("SELECT id FROM agent_allowed_family");
    while ($row = mysql_fetch_array($res)) {
        $count = get_row_count("users", "WHERE clientselect=" . sqlesc($row['id']));
        sql_query("UPDATE agent_allowed_family SET hits=" . sqlesc($count) . " WHERE id=" . sqlesc($row['id']));
    }
    if ($printProgress) {
        printProgress("update clients' popularity");
    }
    //delete old messages sent by system
    $length = 180 * 86400;
    //half a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM messages WHERE sender = 0 AND added < " . sqlesc($until));
    if ($printProgress) {
        printProgress("清理系统发的旧信息delete old messages sent by system");
    }
    //delete old readpost records
    $length = 180 * 86400;
    //half a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    //	$postIdHalfYearAgo = get_single_value('posts', 'id', 'WHERE added < ' . sqlesc($until).' ORDER BY added DESC');echo "333<br/>";这一行代码执行失败,暂时修改无能
    $postIdHalfYearAgo = 0;
    if ($postIdHalfYearAgo) {
        sql_query("UPDATE users SET last_catchup = " . sqlesc($postIdHalfYearAgo) . " WHERE last_catchup < " . sqlesc($postIdHalfYearAgo)) or sqlerr(__FILE__, __LINE__);
        sql_query("DELETE FROM readposts WHERE lastpostread < " . sqlesc($postIdHalfYearAgo)) or sqlerr(__FILE__, __LINE__);
    }
    if ($printProgress) {
        printProgress("delete old readpost records");
    }
    //delete old ip log
    $length = 365 * 86400;
    //a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM iplog WHERE access < " . sqlesc($until));
    if ($printProgress) {
        printProgress("删除旧的ip记录delete old ip log");
    }
    //delete old general log
    $secs = 365 * 86400;
    //a year
    $until = date("Y-m-d H:i:s", TIMENOW - $length);
    sql_query("DELETE FROM sitelog WHERE added < " . sqlesc($until)) or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete old general log");
    }
    //1.delete torrents that doesn't exist any more
    do {
        $res = sql_query("SELECT id FROM torrents") or sqlerr(__FILE__, __LINE__);
        $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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM peers GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
        $res = sql_query("SELECT torrent FROM files GROUP BY torrent") or sqlerr(__FILE__, __LINE__);
        $delids = array();
        while ($row = mysql_fetch_array($res)) {
            $id = $row[0];
            if ($ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")") or sqlerr(__FILE__, __LINE__);
        }
    } while (0);
    if ($printProgress) {
        printProgress("delete torrents that doesn't exist any more");
    }
    //8.lock topics where last post was made more than x days ago
    $secs = 365 * 24 * 60 * 60;
    sql_query("UPDATE topics, posts SET topics.locked='yes' WHERE topics.lastpost = posts.id AND topics.sticky = 'no' AND UNIX_TIMESTAMP(posts.added) < " . TIMENOW . " - {$secs}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("锁定长时间无活动帖子lock topics where last post was made more than x days ago");
    }
    //9.delete report items older than four week
    $secs = 4 * 7 * 24 * 60 * 60;
    $dt = sqlesc(date("Y-m-d H:i:s", TIMENOW - $secs));
    sql_query("DELETE FROM reports WHERE dealtwith=1 AND added < {$dt}") or sqlerr(__FILE__, __LINE__);
    if ($printProgress) {
        printProgress("delete report items older than four week");
    }
    return '所有清理结束Full cleanup is done';
}
Exemplo n.º 9
0
function docleanup()
{
    global $TBDEV, $queries, $C_queries;
    set_time_limit(1200);
    $result = sql_query("show processlist") or sqlerr(__FILE__, __LINE__);
    while ($row = mysql_fetch_array($result)) {
        if ($row["Time"] > 100 || $row["Command"] == "Sleep") {
            $sql = "kill " . $row["Id"] . "";
            sql_query($sql) or sqlerr(__FILE__, __LINE__);
        }
    }
    ignore_user_abort(1);
    do {
        $res = sql_query("SELECT id FROM torrents");
        $ar = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            $ar[$id] = 1;
        }
        if (!count($ar)) {
            break;
        }
        $dp = opendir($TBDEV['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 = $TBDEV['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)) {
            sql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")");
        }
        $res = sql_query("SELECT torrent FROM peers GROUP BY torrent");
        $delids = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")");
        }
        $res = sql_query("SELECT torrent FROM files GROUP BY torrent");
        $delids = array();
        while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $id = $row[0];
            if (isset($ar[$id]) && $ar[$id]) {
                continue;
            }
            $delids[] = $id;
        }
        if (count($delids)) {
            sql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
        }
    } while (0);
    $deadtime = deadtime();
    sql_query("DELETE FROM peers WHERE last_action < {$deadtime}");
    $deadtime -= $TBDEV['max_dead_torrent_time'];
    sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < {$deadtime}");
    $deadtime = time() - $TBDEV['signup_timeout'];
    sql_query("DELETE FROM users WHERE status = 'pending' AND added < {$deadtime} AND last_login < {$deadtime} AND last_access < {$deadtime}");
    /** sync torrent counts - pdq **/
    $tsql = 'SELECT t.id, t.seeders, (
  SELECT COUNT(*)
  FROM peers
  WHERE torrent = t.id AND seeder = "yes"
) AS seeders_num,
t.leechers, (
  SELECT COUNT(*)
  FROM peers
  WHERE torrent = t.id
  AND seeder = "no"
) AS leechers_num,
t.comments, (
  SELECT COUNT(*)
  FROM comments
  WHERE torrent = t.id
) AS comments_num
FROM torrents AS t
ORDER BY t.id ASC';
    $updatetorrents = array();
    $tq = sql_query($tsql);
    while ($t = mysql_fetch_assoc($tq)) {
        if ($t['seeders'] != $t['seeders_num'] || $t['leechers'] != $t['leechers_num'] || $t['comments'] != $t['comments_num']) {
            $updatetorrents[] = '(' . $t['id'] . ', ' . $t['seeders_num'] . ', ' . $t['leechers_num'] . ', ' . $t['comments_num'] . ')';
        }
    }
    mysql_free_result($tq);
    if (count($updatetorrents)) {
        sql_query('INSERT INTO torrents (id, seeders, leechers, comments) VALUES ' . implode(', ', $updatetorrents) . ' ON DUPLICATE KEY UPDATE seeders = VALUES(seeders), leechers = VALUES(leechers), comments = VALUES(comments)');
    }
    unset($updatetorrents);
    //=== Update karma seeding bonus... made nicer by devinkray :D
    //==   Updated and optimized by pdq :)
    //=== Using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give the karma for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
    ///====== Seeding bonus per torrent
    $res = sql_query('SELECT COUNT(torrent) As tcount, userid FROM peers WHERE seeder =\'yes\' GROUP BY userid') or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000) {
                $arr['tcount'] = 5;
            }
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
        }
        if (sizeof($users_buffer) > 0) {
            sql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
            $count = mysql_affected_rows();
            write_log("Cleanup - " . $count / 2 . " users received seedbonus");
        }
        unset($users_buffer);
    }
    //== End
    // === Update coins by Bigjoos
    // === using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give 1 Coin for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
    ///====== Coins per torrent
    if ($TBDEV['coins']) {
        $res = sql_query('SELECT COUNT(torrent) As tcount, userid FROM peers WHERE seeder =\'yes\' GROUP BY userid') or sqlerr(__FILE__, __LINE__);
        if (mysql_num_rows($res) > 0) {
            while ($arr = mysql_fetch_assoc($res)) {
                if ($arr['tcount'] >= 1000) {
                    $arr['tcount'] = 5;
                }
                $users_buffer[] = '(' . $arr['userid'] . ',0.500 * ' . $arr['tcount'] . ')';
            }
            if (sizeof($users_buffer) > 0) {
                sql_query("INSERT INTO users (id,coins) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE coins=coins+values(coins)") or sqlerr(__FILE__, __LINE__);
                $count = mysql_affected_rows();
                write_log("Cleanup - " . $count / 2 . " users received coins");
            }
            unset($users_buffer);
        }
    }
    //== 09 Stats
    $registered = get_row_count('users');
    $unverified = get_row_count('users', "WHERE status='pending'");
    $torrents = get_row_count('torrents');
    $seeders = get_row_count('peers', "WHERE seeder='yes'");
    $leechers = get_row_count('peers', "WHERE seeder='no'");
    $torrentstoday = get_row_count('torrents', 'WHERE added > ' . time() . ' - 86400');
    $donors = get_row_count('users', "WHERE donor='yes'");
    $unconnectables = get_row_count("peers", " WHERE connectable='no'");
    $forumposts = get_row_count("posts");
    $forumtopics = get_row_count("topics");
    $dt = sqlesc(time() - 300);
    // Active users last 5 minutes
    $numactive = get_row_count("users", "WHERE last_access >= {$dt}");
    sql_query("UPDATE stats SET regusers = '{$registered}', unconusers = '{$unverified}', torrents = '{$torrents}', seeders = '{$seeders}', leechers = '{$leechers}', unconnectables = '{$unconnectables}', torrentstoday = '{$torrentstoday}', donors = '{$donors}', forumposts = '{$forumposts}', forumtopics = '{$forumtopics}', numactive = '{$numactive}' WHERE id = '1' LIMIT 1");
    //== Cf's update forum post/topic count
    $forums = sql_query("SELECT t.forumid, count( DISTINCT p.topicid ) AS topics, count( * ) AS posts FROM posts p LEFT JOIN topics t ON t.id = p.topicid LEFT JOIN forums f ON f.id = t.forumid GROUP BY t.forumid");
    while ($forum = mysql_fetch_assoc($forums)) {
        sql_query("update forums set postcount={$forum['posts']}, topiccount={$forum['topics']} where id={$forum['forumid']}");
    }
    write_log("Autoclean-------------------- Auto cleanup Complete using {$queries} queries --------------------");
}
Exemplo n.º 10
0
function docleanup()
{
    global $torrent_dir, $signup_timeout, $max_dead_torrent_time, $autoclean_interval, $READPOST_EXPIRY;
    set_time_limit(0);
    ignore_user_abort(1);
    /*Morgan: Do not clean up torrents. This is too risky: (e.g. a problem with mysql or the torrents directory will cause deletion of torrents)
    	do {
    		$res = mysql_query("SELECT id FROM torrents");
    		$ar = array();
    		while ($row = mysql_fetch_array($res,MYSQL_NUM)) {
    			$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,MYSQL_NUM)) {
    			$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,MYSQL_NUM)) {
    			$id = $row[0];
    			if ($ar[$id])
    				continue;
    			$delids[] = $id;
    		}
    		if (count($delids))
    			mysql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
    	} while (0);
    */
    $deadtime = deadtime();
    @mysql_query("DELETE FROM peers WHERE last_action < {$deadtime}");
    $deadtime -= $max_dead_torrent_time;
    @mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < {$deadtime}");
    $deadtime = time() - $signup_timeout;
    @mysql_query("DELETE FROM users WHERE status = 'pending' AND added < {$deadtime} AND last_login < {$deadtime} AND last_access < {$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"];
        if (isset($torrents[$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
    #Morgan: Keep inactive user accounts
    /*
    $secs = 42*86400;
    $dt = (time() - $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 LEFT JOIN posts ON topics.lastpost = posts.id WHERE topics.locked = 'no' AND topics.sticky = 'no' AND " . time() . " - UNIX_TIMESTAMP(posts.added) > $secs") or sqlerr(__FILE__, __LINE__);
      if(mysql_num_rows($res) > 0) {
    	while ($arr = mysql_fetch_assoc($res))
        $pids[] = $arr['id'];
    		mysql_query("UPDATE topics SET locked='yes' WHERE id IN (".join(',', $pids).")") or sqlerr(__FILE__, __LINE__);
      }
    */
    //remove expired warnings
    $res = @mysql_query("SELECT id FROM users WHERE warned='yes' AND warneduntil < " . time() . " AND warneduntil <> 0") or sqlerr(__FILE__, __LINE__);
    if (mysql_num_rows($res) > 0) {
        $dt = 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 = 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__);
        }
    }
    // promote power users
    #Morgan: No use for power users, so do not bother promoting/demoting them
    /*
    $limit = 25*1024*1024*1024;
    $minratio = 1.05;
    $maxdt = (time() - 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 = time();
    	$msg = sqlesc("Congratulations, you have been auto-promoted to [b]Power User[/b]. :)\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 = 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 t.forumid, count( DISTINCT p.topicid ) AS topics, count( * ) AS posts FROM posts p LEFT JOIN topics t ON t.id = p.topicid LEFT JOIN forums f ON f.id = t.forumid GROUP BY t.forumid");
    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={$forum['posts']}, topiccount={$forum['topics']} where id={$forum['forumid']}");
    }
    // delete old torrents
    #Morgan: Keep old torrents forever (as long as there are seeders)
    /*
    $days = 28;
    $dt = (time() - ($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)");
    }
    */
    #Morgan: Delete torrents that have had no seeders for more than 1 year
    $days = 365;
    $dt = time() - $days * 86400;
    $res = mysql_query("SELECT id, name FROM torrents WHERE last_action < {$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)");
    }
    // Remove expired readposts...
    $dt = time() - $READPOST_EXPIRY;
    @mysql_query("DELETE readposts FROM readposts " . "LEFT JOIN posts ON readposts.lastpostread = posts.id " . "WHERE posts.added < {$dt}") or sqlerr(__FILE__, __LINE__);
}