<?

/**
 * @author 7Max7
 * @copyright 2009
 */

require_once('include/bittorrent.php'); 
dbconn(); 

@ini_set('display_errors', 'On');

if (get_user_class() < UC_USER){
stderr("Ошибочка", "Дальше еще интереснее"); 
die();
}

$sqlk = sql_query("SELECT COUNT(*) FROM torrents WHERE multitracker = 'yes' AND multi_time='0000-00-00 00:00:00'") or sqlerr(__FILE__,__LINE__);
$toent = mysql_fetch_array($sqlk);

echo "<title>Всего ".$toent[0]."</title>";

global $announce_urls;

require_once(ROOT_PATH.'include/benc.php');
$dsdf=0;
$dt_multi = sqlesc(get_date_time(gmtime() - 36000000));///86400
///0000-00-00 00:00:00 ///multi_time<$dt_multi  ORDER BY (f_seeders + f_leechers) ASC 
$sql = sql_query("SELECT id, info_hash,name FROM torrents WHERE multitracker = 'yes' AND multi_time='0000-00-00 00:00:00' ORDER BY added DESC LIMIT 25") or sqlerr(__FILE__,__LINE__);

while($torrent = mysql_fetch_array($sql)) {
    $tracker_cache = array(); 
    $f_leechers = 0; 
    $f_seeders = 0; 
    $announce_list = $announce_urls;
    echo "[".get_date_time()."] ";
    foreach($announce_list as $announce) 
    {
        $response = get_remote_peers($announce, $torrent['info_hash'],true); 
       // echo $torrent['id'].": ";
        //print_r($response); 
        //var_dump($response); 
       	//echo "<br>"; 
 
	
        if($response['state']=='ok') 
        {
           $tracker_cache[] = $response['tracker'].':'.($response['leechers'] ? $response['leechers'] : 0).':'.($response['seeders'] ? $response['seeders'] : 0).':'.($response['downloaded'] ? $response['downloaded'] : 0); 
            // $f_leechers += $response['leechers']; 
            // $f_seeders += $response['seeders']; 
            if ($f_leechers < $response['leechers'])
            $f_leechers = $response['leechers'];
            
            if ($f_seeders < $response['seeders'])
            $f_seeders = $response['seeders']; 
        }
        else 
            $tracker_cache[] = $response['tracker'].':false'; 
    }
    $tracker_cache = implode("\n",$tracker_cache); 
       $now = get_date_time();
    sql_query("UPDATE LOW_PRIORITY torrents SET f_seeders = ".$f_seeders.", f_leechers = ".$f_leechers.", f_trackers = ".sqlesc($tracker_cache).", multi_time=".sqlesc($now)." WHERE id = ".$torrent['id']) or sqlerr(__FILE__,__LINE__);
    
	echo htmlspecialchars($torrent["name"]).": f_seeders = $f_seeders f_leechers = $f_leechers"; 
    echo "<br>"; 
++$dsdf;
}

$h = date('i'); // проверяем минуты
if (($h >= 00 )&&($h <= 10)) // расписание минут
{
sql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND multitracker='yes' AND (f_seeders+f_leechers)='0'") or sqlerr(__FILE__,__LINE__);

sql_query("UPDATE torrents SET visible='yes' WHERE visible='no' AND multitracker='yes' AND (f_seeders+f_leechers)>='1'") or sqlerr(__FILE__,__LINE__);

sql_query("UPDATE torrents SET f_seeders='no',f_leechers='0' WHERE multitracker='no'") or sqlerr(__FILE__,__LINE__);
}





$slk = sql_query("SELECT id, image1 FROM torrents WHERE multitracker = 'yes' AND multi_time<>'0000-00-00 00:00:00' AND (f_seeders+f_leechers)<=2 AND moderatedby='92' AND owner='92' AND (seeders+leechers)=0 LIMIT 5") or sqlerr(__FILE__,__LINE__);
while ($to = mysql_fetch_array($slk)){

sql_query("DELETE FROM torrents WHERE id=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);
sql_query("DELETE FROM snatched WHERE torrent=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);	
sql_query("DELETE FROM ratings WHERE torrent=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);
sql_query("DELETE FROM checkcomm WHERE checkid=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);
sql_query("DELETE FROM bookmarks WHERE torrentid=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);
sql_query("DELETE FROM files WHERE torrent=".sqlesc($to["id"])) or sqlerr(__FILE__,__LINE__);

@unlink(ROOT_PATH."torrents/$to[id].torrent");
@unlink(ROOT_PATH."torrents/images/$to[image1]");
//echo $to["id"]."<br>";
}




if (date('i')%2==0)
@unlink(ROOT_PATH."cache/block-last_files.txt");


if ($dsdf<>0)
echo "<script>setTimeout('document.location.href=\"remote_gets.php\"', 10000);</script>";


 
?>