sql_query("DELETE FROM peers WHERE last_action < ".sqlesc($deadtime))orsqlerr(__FILE__,__LINE__);
if($printProgress){
printProgress('update peer status');
}
//11.calculate seeding bonus
$res=sql_query("SELECT DISTINCT userid FROM peers WHERE seeder = 'yes'")orsqlerr(__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))//loop for different users
{
$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'")orsqlerr(__FILE__,__LINE__);
sql_query("UPDATE avps SET value_u = ".sqlesc($now)." WHERE arg='lastcleantime3'")orsqlerr(__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")orsqlerr(__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")orsqlerr(__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")orsqlerr(__FILE__,__LINE__);
while($row=mysql_fetch_assoc($res)){
$id=$row["id"];
$torr=$torrents[$id];
foreach($fieldsas$field){
if(!isset($torr[$field]))
$torr[$field]=0;
}
$update=array();
foreach($fieldsas$field){
if($torr[$field]!=$row[$field])
$update[]="$field = ".$torr[$field];
}
if(count($update))
sql_query("UPDATE torrents SET ".implode(",",$update)." WHERE id = $id")orsqlerr(__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")orsqlerr(__FILE__,__LINE__);
while($forum=mysql_fetch_assoc($forums))
{
$postcount=0;
$topiccount=0;
$topics=sql_query("select id from topics where forumid=$forum[id]")orsqlerr(__FILE__,__LINE__);
while($topic=mysql_fetch_assoc($topics))
{
$res=sql_query("select count(*) from posts where topicid=$topic[id]")orsqlerr(__FILE__,__LINE__);
$arr=mysql_fetch_row($res);
$postcount+=$arr[0];
++$topiccount;
}
sql_query("update forums set postcount=$postcount, topiccount=$topiccount where id=$forum[id]")orsqlerr(__FILE__,__LINE__);
$res=sql_query("SELECT id, name FROM torrents WHERE added < $dt AND sp_state = ".sqlesc($type).' AND promotion_time_type=0')orsqlerr(__FILE__,__LINE__);
switch($targettype)
{
case1://normal
{
$sp_state=1;
$become="normal";
break;
}
case2://Free
{
$sp_state=2;
$become="Free";
break;
}
case3://2X
{
$sp_state=3;
$become="2X";
break;
}
case4://2X Free
{
$sp_state=4;
$become="2X Free";
break;
}
case5://Half Leech
{
$sp_state=5;
$become="50%";
break;
}
case6://2X Half Leech
{
$sp_state=6;
$become="2X 50%";
break;
}
default://normal
{
$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]")orsqlerr(__FILE__,__LINE__);
if($sp_state==1)
write_log("Torrent $arr[id] ($arr[name]) is no longer on promotion (time expired)",'normal');
elsewrite_log("Promotion type for torrent $arr[id] ($arr[name]) is changed to ".$become." (time expired)",'normal');
sql_query("UPDATE torrents SET sp_state = 1, promotion_time_type=0, promotion_until=null WHERE promotion_time_type=2 AND promotion_until < ".sqlesc(date("Y-m-d H:i:s",TIMENOW)))orsqlerr(__FILE__,__LINE__);
sql_query("UPDATE torrents SET picktype = 'hot' WHERE added > $dt AND picktype = 'normal' AND seeders > ".sqlesc($hotseeder_torrent))orsqlerr(__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)")orsqlerr(__FILE__,__LINE__);
sql_query("UPDATE avps SET value_u = ".sqlesc($now)." WHERE arg='lastcleantime4'")orsqlerr(__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)")orsqlerr(__FILE__,__LINE__);
if($printProgress){
printProgress("delete unconfirmed accounts");
}
//5.delete old login attempts
$secs=12*60*60;// Delete failed login attempts per half day.
//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")orsqlerr(__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")orsqlerr(__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")orsqlerr(__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")orsqlerr(__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()")orsqlerr(__FILE__,__LINE__);
$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")orsqlerr(__FILE__,__LINE__);
$res=sql_query("SELECT id, max_class_once FROM users WHERE class = $oriclass AND downloaded >= $limit AND uploaded / downloaded >= $minratio AND added < ".sqlesc($maxdt))orsqlerr(__FILE__,__LINE__);
$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")orsqlerr(__FILE__,__LINE__);
//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'])orsqlerr(__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")orsqlerr(__FILE__,__LINE__);
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))orsqlerr(__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")orsqlerr(__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).")")orsqlerr(__FILE__,__LINE__);
$res=sql_query("SELECT torrent FROM peers GROUP BY torrent")orsqlerr(__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).")")orsqlerr(__FILE__,__LINE__);
$res=sql_query("SELECT torrent FROM files GROUP BY torrent")orsqlerr(__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).")")orsqlerr(__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")orsqlerr(__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")orsqlerr(__FILE__,__LINE__);
if($printProgress){
printProgress("delete report items older than four week");