Location: PHPKode > projects > H-tracker > backend/cleanup.php
<?php
function do_cleanup() {
global $site_config;
$seconds_old = 600;
$directory = "".$site_config['cache_dir']."";

if( !$dirhandle = @opendir($directory) )
return;

while( false !== ($filename = readdir($dirhandle)) ) {
if ($filename != '.' && $filename != '..' && $filename != "date.txt" && $filename != "browser.txt" && $filename != ".htaccess" && $filename != "cache_latestuploadsblock.txt" && $filename != "cache_latestrequestsblock.txt"){
$filename = $directory. "/". $filename;

if( @filemtime($filename) < (time()-$seconds_old) )
  @unlink($filename);
                        }
                }
//LOCAL TORRENTS - GET PEERS DATA AND UPDATE BROWSE STATS
$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"];
	}

	$fields = explode(":", "comments:leechers:seeders");
	$res = mysql_query("SELECT id, seeders, leechers FROM torrents WHERE external !='yes'");
	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 AND external !='yes'");
	}  


//LOCAL TORRENTS - MAKE NON-ACTIVE/OLD TORRENTS INVISIBLE
$deadtime = gmtime() - $site_config["max_dead_torrent_time"];
$points_per_cleanup = $site_config['points_per_cleanup'];
mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME($deadtime) AND external !='yes'");

$res = mysql_query("SELECT DISTINCT userid FROM peers WHERE seeder = 'yes'") or die (mysql_error());
   if (mysql_num_rows($res) > 0)
   {
       while ($arr = mysql_fetch_assoc($res))
       {
       mysql_query("UPDATE users SET seedbonus = seedbonus + $points_per_cleanup WHERE id = $arr[userid]");
       }
   }  

//DELETE OLD NON-ACTIVE PEERS
$res = mysql_query("DELETE FROM peers WHERE ".gmtime()."-UNIX_TIMESTAMP(last_action) >= 10800"); 

//DELETE PENDING USER ACCOUNTS OVER TIMOUT AGE
$deadtime = gmtime() - $site_config["signup_timeout"];
mysql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME($deadtime)");


 // delete old regimage codes
 $secs = 1 * 86400;
 $dt = time() - $secs;
 mysql_query("DELETE FROM captcha WHERE dateline < $dt") or sqlerr(__FILE__,__LINE__);


//LEECHWARN USERS WITH LOW RATIO

if ($site_config["ratiowarn_enable"]){
	$minratio = $site_config["ratiowarn_minratio"];
	$downloaded = $site_config["ratiowarn_mingigs"]*1024*1024*1024;
	$length = $site_config["ratiowarn_daystowarn"];

	//ADD WARNING
	$res = mysql_query("SELECT id,username FROM users WHERE class = 1 AND warned = 'no' AND enabled='yes' AND uploaded / downloaded < $minratio AND downloaded >= $downloaded");

	if (mysql_num_rows($res) > 0){
		$timenow = 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", gmtime() + (86400 * $length));

		while ($arr = mysql_fetch_assoc($res)){
			mysql_query("INSERT INTO warnings (userid, reason, added, expiry, warnedby, type) VALUES ('".$arr["id"]."','".$reason."','".$timenow."','".$expiretime."','0','Poor Ratio')");
			mysql_query("UPDATE users SET warned='yes' WHERE id='".$arr["id"]."'");
			mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES ('0', '".$arr["id"]."', '".$timenow."', '".$reason."', '0')");
			write_log("Auto Leech warning has been <B>added</B> for: <a href=account-details.php?id=".$arr["id"].">".$arr["username"]."</a>");
		}
	}

    //REMOVE WARNING
	$res1 = mysql_query("SELECT users.id, users.username FROM users INNER JOIN warnings ON users.id=warnings.userid WHERE type='Poor Ratio' AND warned = 'yes'  AND enabled='yes' AND uploaded / downloaded >= $minratio AND downloaded >= $downloaded");

	if (mysql_num_rows($res1) > 0){
		$timenow = 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 = mysql_fetch_assoc($res1)){
			write_log("Auto Leech warning has been removed for: <a href=account-details.php?id=".$arr1["id"].">".$arr1["username"]."</a>"); 
				
			mysql_query("UPDATE users SET warned = 'no' WHERE id = '".$arr1["id"]."'");
			mysql_query("UPDATE warnings SET expiry = '".$timenow."' WHERE userid = '".$arr1["id"]."'");
			mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES ('0', '".$arr1["id"]."', '".$timenow."', '".$reason."', '0')");
		}
	}

	//BAN WARNED USERS
	$res = mysql_query("SELECT id,username FROM users WHERE class = 1 AND enabled='yes' AND warned = 'yes' AND uploaded / downloaded < $minratio AND downloaded >= $downloaded");

	if (mysql_num_rows($res) > 0){
		$timenow = get_date_time();
		$expires = (86400 * $length);

		while ($arr = mysql_fetch_assoc($res)){
			$r = mysql_query("SELECT id, UNIX_TIMESTAMP(expiry) as expiry FROM warnings WHERE userid=$arr[id]");
			$row = mysql_fetch_assoc($r);

			if (gmtime() - $row["expiry"] >= 0) {
				mysql_query("UPDATE users SET enabled='no', warned='no' WHERE id='".$arr["id"]."'");
				write_log("User <a href=account-details.php?id=".$arr["id"].">".$arr["username"]."</a> has been banned (Auto Leech warning).");
			}
		}
	}

}//check if warning system is on
//END//


mysql_query("UPDATE users SET pm_count = if( pm_count > 1, pm_count -2, pm_count ) , comment_count = if( comment_count > 1, comment_count -2, comment_count )"); 
//OPTIMISE TABLES
mysql_query("OPTIMIZE TABLE `peers` , `torrents` , `tasks` , `guests` , `users` , `messages`; ");
}//end func


?>
Return current item: H-tracker