I rewrote this as I wanted a fresh solution to a clean up class. I did some reading and figured out a way to minimize my declared methods and instead implement a interface, which holds every task to execute. It works perfectly, as long as you have PDO installed (PHP PDO).
This is 100% working. Use as you will. Works perfectly for us.
Add to /classes as class.cleanup.php
run_jobs.php and save to / (where index.php is)
Our version uses nullable types, like ?string, ?int etc. I left this out as people seem to use PHP 5 still. Best to rule that out. Enjoy.
Required (for torrenttrader):
Search and Replace "torrent_id" => "id", "user_id" => "id"
This is 100% working. Use as you will. Works perfectly for us.
Add to /classes as class.cleanup.php
<?
// STorrents2020 Cleanup Class - Class rewrite & fancy syntax - Mar 3, 2021
class Database
{
public $pdo;
public $CONFIG;
public function __construct($db = 'database', $username = 'username', $password = 'password', $host = 'localhost', $port = 3306, $options = [])
{
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=utf8mb4";
try {
$this->pdo = new PDO($dsn, $username, $password, $options);
}
catch (PDOException $e) {
$this->log_exception($e->getMessage(), $e->getLine());
}
}
private function log_exception($msg, $line)
{
$query = "INSERT INTO sql_logging(exception, added) VALUES(?, NOW())";
$stt = $this->pdo->prepare($query);
$stt->bindValue(1, $msg . $line);
$stt->execute();
}
public function get_date_time($timestamp = 0)
{
if ($timestamp)
return date('Y-m-d H:i:s', $timestamp);
else
return gmdate('Y-m-d H:i:s');
}
public function preprocess($query, $get_id)
{
return $query;
}
public function write_log($text)
{
$this->query_params('INSERT INTO log (added, txt) VALUES(NOW(), :log)', array(
'log' => $text
));
}
public function sql_timestamp_to_unix_timestamp($s)
{
return mktime(substr($s, 11, 2), substr($s, 14, 2), substr($s, 17, 2), substr($s, 5, 2), substr($s, 8, 2), substr($s, 0, 4));
}
public function gmtime()
{
return $this->sql_timestamp_to_unix_timestamp($this->get_date_time());
}
public function interval($s)
{
return sprintf("INTERVAL '%s'", $s);
}
public function query_params($query, $params = null, $get_id = null)
{
if (is_null($params)) {
$params = array();
}
try {
$query = $this->preprocess($query, $get_id);
$stmt = $this->pdo->prepare($query);
foreach ($params as $key => $param) {
if ($key == "data") {
$stmt->bindValue(':' . $key, $param, PDO::PARAM_LOB);
} else if ($key == "limit") {
$stmt->bindValue(':' . $key, (int) trim($param), PDO::PARAM_INT);
} else {
$stmt->bindValue(':' . $key, $param);
}
}
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
if (is_null($get_id)) {
return $stmt;
} else {
return $this->get_last_id($stmt, $get_id);
}
}
catch (PDOException $e) {
$this->log_exception($e->getMessage(), $e->getLine());
}
}
}
interface Tasks
{
public function seedbonus();
public function comments();
public function updatetorrents();
public function checkexpiredwarns();
public function ratiowarn();
public function cleanlogs();
public function autopromote();
public function autofreeleech();
public function autodemote();
public function checkdead();
public function pendingusers();
public function privseeders();
public function cleanpeers();
}
class Cleanup extends Database implements Tasks
{
public function cleanlogs()
{
$ts = $this->gmtime() - $this->CONFIG['log_clean'];
$this->query_params('DELETE FROM log WHERE added < FROM_UNIXTIME(:ts)', array(
'ts' => $ts
));
}
public function checkdead()
{
$deadtime = $this->gmtime() - $this->CONFIG['max_dead_torrent_time'];
$founddead = $this->query_params('SELECT COUNT(torrent_id) AS dead FROM torrents WHERE seeders = 0 AND leechers = 0');
$find = $founddead->fetch();
//$this->query_params("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME(:dead_time) AND seeders = '0' AND leechers = '0' AND external !='yes'", array('dead_time' => $deadtime));
$event = "$find[dead] torrents found dead. Opening Graveyard
";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array('receiver' => 'NerbBot', 'event' => $event));
}
public function seedbonus()
{
$res = $this->query_params("SELECT DISTINCT user_id as peer, (SELECT DISTINCT COUNT(torrent_id) FROM peers WHERE seeder = 'yes' AND user_id = peer) AS count FROM peers WHERE seeder = 'yes'");
while ($row = $res->fetch()) {
$this->query_params('UPDATE users SET seedbonus = seedbonus + :bonustime_rowcount WHERE user_id = :peer', array(
'bonustime_rowcount' => ($this->CONFIG['bonuspertime'] * $row['count']),
'peer' => $row['peer']
));
}
}
public function autofreeleech()
{
$gigs = 17179869184; // 16gb
$query = $this->query_params("SELECT torrent_id, name FROM torrents WHERE banned = 'no' AND freeleech = '0' AND size >= :gigs", array('gigs' => $gigs));
if ($query) {
while ($row = $query->fetch()) {
$event = "$row[name] has been cycled to Freeleech (16GB+)";
$this->query_params("UPDATE torrents SET freeleech = '1' WHERE torrent_id = :torrent_id", array('torrent_id' => $row['torrent_id']));
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array('receiver' => 'NerbBot', 'event' => $event));
$this->write_log("Freeleech added on <a href='/details/$row[torrent_id]'>$row[name]</a> because it is bigger than 8GB.");
}
}
}
public function pendingusers()
{
$deadtime = $this->gmtime() - $this->CONFIG['signup_timeout'];
$this->query_params("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME(:dead_time)", array('dead_time' => $deadtime));
}
public function privseeders()
{
$res = $this->query_params("SELECT t.torrent_id, (SELECT COUNT(torrent_id) FROM peers WHERE seeder = 'yes' AND completed = 1) AS seeders, COUNT(seeders) AS c FROM torrents t LEFT JOIN peers p ON p.torrent_id = t.torrent_id WHERE t.banned = 'no' AND t.external = 'no'");
$getcount = $res->fetch();
$event = "Found $getcount[c] Private Seeders connected (global)
";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array('receiver' => 'NerbBot', 'event' => $event));
}
public function comments()
{
$getcount = $this->query_params("SELECT COUNT(torrent) as c FROM comments");
$restotal = $getcount->fetch();
$event = "Currently there are $restotal[c] comments posted
";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => 'NerbBot',
'event' => $event
));
}
public function cleanpeers()
{
$cleantable = $this->query_params('DELETE FROM peers WHERE last_announce + ' . $this->interval('1 hour') . ' < CURRENT_TIMESTAMP');
$released = $this->query_params('SELECT COUNT(user_id) AS c FROM peers WHERE last_announce + ' . $this->interval('1 hour') . ' < CURRENT_TIMESTAMP');
$row = $released->fetch();
if ($cleantable) {
$event = "Cleaning $row[c][c] peers found idle or stale
";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => 'NerbBot',
'event' => $event
));
}
}
public function updatetorrents()
{
$fields = explode(':', 'comments:leechers:seeders');
$res = $this->query_params("SELECT p.torrent_id, t.external, t.comments, (SELECT COUNT(torrent_id) FROM peers WHERE seeder = 'yes' AND torrent_id = p.torrent_id) AS seeders, (SELECT COUNT(torrent_id) FROM peers WHERE seeder = 'no' AND torrent_id = p.torrent_id) AS leechers FROM torrents t LEFT JOIN peers p ON p.torrent_id = t.torrent_id WHERE t.banned = 'no'");
$torrents = array();
while ($row = $res->fetch()) {
$id = $row['torrent_id'];
$torr = $torrents[$id];
foreach ($fields as $field) {
if (!isset($torr[$field]))
$torr[$field] = 0;
}
$update = array();
foreach ($fields as $field) {
if ($row['external'] == 'no' || $field == 'comments') {
if ($torr[$field] != $row[$field])
$update[] = "$field = " . $torr[$field];
}
}
if (count($update))
$this->query_params("UPDATE torrents SET " . implode(",", $update) . " WHERE torrent_id = :torrent_id", array(
'torrent_id' => $id
));
}
}
public function autopromote()
{
$limit = 25 * 1024 * 1024 * 1024;
$minratio = 1.05;
$maxdt = $this->get_date_time(time() - 86400 * 28);
$res = $this->query_params('SELECT user_id, username FROM users WHERE class = 1 AND uploaded >= :limit AND uploaded / downloaded >= :minratio AND added < :maxdt', array(
'limit' => $limit,
'minratio' => $minratio,
'maxdt' => $maxdt
));
if ($res) {
$dt = $this->get_date_time();
$msg = "Congratulations, you have been automatically promoted to Power User. You can now do more stuff.";
while ($arr = $res->fetch()) {
$this->query_params('UPDATE users SET class = 2 WHERE user_id = :user_id', array(
'user_id' => $arr['user_id']
));
$txt = "You have been promoted to Power User!";
$event = "$arr[username] has been promoted Power User";
$this->query_params('INSERT INTO messages (sender, receiver, added, msg, poster, subject) VALUES(0, :user_id, NOW(), :message, 0, :subject)', array(
'user_id' => $arr['user_id'],
'message' => $txt,
'subject' => 'Promoted!'
));
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => $arr['username'],
'event' => $event
));
$this->write_log("The user <a href=\"/user/?id=$arr[user_id]\">$arr[username]</a> was automatically promoted to power user.");
}
}
}
public function autodemote()
{
$minratio = 0.95;
$res = $this->query_params('SELECT user_id, username FROM users WHERE class = 2 AND uploaded / downloaded < :minratio', array(
'minratio' => $minratio
));
if ($res) {
$dt = $this->get_date_time();
$msg = "You were automatically demoted from Power User to Regular because your share ratio fell below $minratio.";
while ($arr = $res->fetch()) {
$this->query_params('UPDATE users SET class = 1 WHERE user_id = :user_id', array(
'user_id' => $arr['user_id']
));
$txt = "You have been demoted to Regular.";
$event = "$arr[username] has been demoted to Regular";
$this->query_params('INSERT INTO messages (sender, receiver, added, msg, poster, subject) VALUES(0, :user_id, NOW(), :message, 0, :subject)', array(
'user_id' => $arr['user_id'],
'message' => $txt,
'subject' => 'Demoted!'
));
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => $arr['username'],
'event' => $event
));
$this->write_log("The user <a href=\"/user/?id=$arr[user_id]\">$arr[username]</a> was automatically promoted to power user.");
}
}
}
public function checkexpiredwarns()
{
$res = $this->query_params("SELECT users.user_id, users.username, warnings.expiry FROM users INNER JOIN warnings ON users.user_id=warnings.userid WHERE type != 'Poor Ratio' AND warned = 'yes' AND enabled='yes' AND warnings.active = 'yes' AND warnings.expiry < :date_time", array(
'date_time' => $this->get_date_time()
));
while ($arr1 = $res->fetch()) {
$this->query_params("UPDATE users SET warned = 'no' WHERE user_id = :user_id", array(
'user_id' => $arr1['user_id']
));
$this->query_params("UPDATE warnings SET active = 'no' WHERE userid = :user_id AND expiry < :date_time", array(
'user_id' => $arr1['user_id'],
'date_time' => $this->get_date_time()
));
$event = "Warning removed for $arr1[username]. Expiry: $arr1[expiry]";
$this->write_log("Removed warning for $arr1[username]. Expiry: $arr1[expiry]");
}
$this->query_params("UPDATE users SET warned = 'yes' WHERE warned = 'no' AND user_id IN (SELECT userid FROM warnings WHERE active = 'yes')");
while ($active = $res->fetch()) {
$event = "Active warning renewed for $active[username]";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => 'NerbBot',
'event' => $event
));
}
}
public function ratiowarn()
{
if ($this->CONFIG['ratiowarn_enable']) {
$minratio = $this->CONFIG["ratiowarn_minratio"];
$downloaded = $this->CONFIG["ratiowarn_mingigs"] * 1024 * 1024 * 1024;
$length = $this->CONFIG["ratiowarn_daystowarn"];
$res = $this->query_params("SELECT user_id, username FROM users WHERE class = 1 AND warned = 'no' AND enabled='yes' AND uploaded / downloaded < :minratio AND downloaded >= :downloaded", array(
'minratio' => $minratio,
'downloaded' => $downloaded
));
if ($res) {
$timenow = $this->get_date_time();
$reason = "You have been warned because of having low ratio. You need to get a " . $minratio . " before next " . $length . " days or your account may be banned.";
$expiretime = gmdate('Y-m-d H:i:s', $this->gmtime() + (86400 * $length));
while ($arr = $res->fetch()) {
$warneduser = $arr['username'];
$event = "Low ratio warning added for [url=https://www.switch-torrents.com/user/?id=$arr[user_id]]$arr[username][/url]";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => $warneduser,
'event' => $event
));
$this->query_params('INSERT INTO warnings (userid, reason, added, expiry, warnedby, type) VALUES (:user_id, :reason, NOW(), :expiretime, 0, :type)', array(
'user_id' => $arr['user_id'],
'reason' => $reason,
'expiretime' => $expiretime,
'type' => 'Poor Ratio'
));
$this->query_params("UPDATE users SET warned = 'yes' WHERE user_id = :user_id", array(
'user_id' => $arr['user_id']
));
$this->query_params('INSERT INTO messages (sender, receiver, added, msg, poster, subject) VALUES (0, :user_id, NOW(), :reason, 0, :subject)', array(
'user_id' => $arr['user_id'],
'reason' => $reason,
'subject' => 'Warning: Poor Ratio'
));
$this->write_log("Auto Leech warning has been <b>added</b> for <a href='/user/?id=" . $arr["user_id"] . "'>" . $arr["username"] . "</a>");
}
}
$res1 = $this->query_params("SELECT users.user_id, users.username FROM users INNER JOIN warnings ON users.user_id=warnings.userid WHERE type='Poor Ratio' AND active = 'yes' AND warned = 'yes' AND enabled='yes' AND uploaded / downloaded >= :minratio AND downloaded >= :downloaded", array(
'minratio' => $minratio,
'downloaded' => $downloaded
));
if ($res1) {
$timenow = $this->get_date_time();
$reason = "Your warning of low ratio has been removed. We highly recommend you to keep a your ratio up to not be warned again.\n";
while ($arr1 = $res1->fetch()) {
$warnremoved = $arr1['username'];
$event = "Low ratio warning removed for [url=https://www.switch-torrents.com/user/?id=$arr1[user_id]]$arr1[username][/url]";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => $warnremoved,
'event' => $event
));
$this->query_params("UPDATE users SET warned = 'no' WHERE user_id = :user_id", array(
'user_id' => $arr1['user_id']
));
$this->query_params("UPDATE warnings SET expiry = :time_now, active = 'no' WHERE userid = :user_id", array(
'time_now' => $timenow,
'user_id' => $arr1['user_id']
));
$this->query_params('INSERT INTO messages (sender, receiver, added, msg, poster, subject) VALUES (0, :user_id, NOW(), :reason, 0, :subject)', array(
'user_id' => $arr1['user_id'],
'reason' => $reason,
'subject' => 'Ratio Warning Removed'
));
$this->write_log("Auto Leech warning has been removed for: <a href='/user/?id=" . $arr1["user_id"] . "'>" . $arr1["username"] . "</a>");
}
}
$res = $this->query_params("SELECT users.user_id, users.username, users.limited, UNIX_TIMESTAMP(warnings.expiry) AS expiry FROM users INNER JOIN warnings ON users.user_id=warnings.userid WHERE type='Poor Ratio' AND active = 'yes' AND class = 1 AND enabled='yes' AND warned = 'yes' AND uploaded / downloaded < :minratio AND downloaded >= :downloaded", array(
'minratio' => $minratio,
'downloaded' => $downloaded
));
if ($res->fetch() > 0) {
$timenow = $this->get_date_time();
$expires = (86400 * $length);
while ($arr = $res->fetch()) {
if ($this->gmtime() - $arr['expiry'] >= 0) {
$limiteduser = $arr['username'];
$event = "[url=https://www.switch-torrents.com/user/?id=$arr[user_id]]$arr[username][/url] has been limited (Auto Leech warning)";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => $limiteduser,
'event' => $event
));
$this->query_params("UPDATE users SET limited = 'yes', warned = 'no' WHERE user_id = :user_id", array(
'user_id' => $arr['user_id']
));
$this->write_log("User <a href='/user/?id=" . $arr["user_id"] . "'>" . $arr["username"] . "</a> has been <b>limited</b> (Auto Leech warning)");
}
}
}
}
$res = $this->query_params("SELECT users.user_id, users.username, warnings.expiry FROM users INNER JOIN warnings ON users.user_id=warnings.userid WHERE type != 'Poor Ratio' AND warned = 'yes' AND enabled='yes' AND warnings.active = 'yes' AND warnings.expiry < :date_time", array(
'date_time' => $this->get_date_time()
));
while ($arr1 = $res->fetch()) {
$this->query_params("UPDATE users SET warned = 'no' WHERE user_id = :user_id", array(
'user_id' => $arr1['user_id']
));
$this->query_params("UPDATE warnings SET active = 'no' WHERE userid = :user_id AND expiry < :date_time", array(
'user_id' => $arr1['user_id'],
'date_time' => $this->get_date_time()
));
$event = "Warning removed for $arr1[username]. Expiry: $arr1[expiry]";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => 'NerbBot',
'event' => $event
));
$this->write_log("Removed warning for $arr1[username]. Expiry: $arr1[expiry]");
}
$this->query_params("UPDATE users SET warned = 'yes' WHERE warned = 'no' AND user_id IN (SELECT userid FROM warnings WHERE active = 'yes')");
while ($active = $res->fetch()) {
$event = "Active warning renewed for $active[username]";
$this->query_params('INSERT INTO shoutbox (msgid, user, message, date, userid) VALUES (NULL, :receiver, :event, NOW(), 0)', array(
'receiver' => 'NerbBot',
'event' => $event
));
}
}
}
run_jobs.php and save to / (where index.php is)
<?
// Cleanup Runner - Mar 3, 2021
require_once('config.php');
require_once('classes/class.cleanup.php');
echo 'Running Jobs...';
$task = new Cleanup;
$task->seedbonus();
$task->comments();
$task->updatetorrents();
$task->checkexpiredwarns();
$task->ratiowarn();
$task->cleanlogs();
$task->autopromote();
$task->autofreeleech();
$task->autodemote();
$task->checkdead();
$task->pendingusers();
$task->privseeders();
$task->cleanpeers();
Our version uses nullable types, like ?string, ?int etc. I left this out as people seem to use PHP 5 still. Best to rule that out. Enjoy.
Required (for torrenttrader):
Search and Replace "torrent_id" => "id", "user_id" => "id"
Last edited by STorrents2020 on 2021-03-04 01:29:52