<?php
if( ! defined( 'DATALIFEENGINE' ) ) {
	die( "Hacking attempt!" );
}
$tpl->result['content'] = $cache->open( "stats", $config['skin'] );

if( ! $tpl->result['content'] ) {
	$db->query( "SHOW TABLE STATUS FROM `" . DBNAME . "`" );
	$mysql_size = 0;
	while ( $r = $db->get_row() ) {if( strpos( $r['Name'], PREFIX . "_" ) !== false ) $mysql_size += $r['Data_length'] + $r['Index_length'];}
	$db->free();
	$mysql_size = mksize( $mysql_size );

    $size_info = $db->super_query("SELECT sum(size) size FROM ".PREFIX."_torrents");
	
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . PREFIX . "_post" );
	$stats_news = $row['count'];
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . PREFIX . "_post WHERE approve ='1'" );
	$stats_approve = $row['count'];
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . PREFIX . "_comments" );
	$count_comments = $row['count'];
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . USERPREFIX . "_users" );
	$stats_users = $row['count'];
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . USERPREFIX . "_users WHERE banned='yes'" );
	$stats_banned = $row['count'];
	$row = $db->super_query( "SELECT COUNT(*) as count FROM " . PREFIX . "_post WHERE allow_main ='1' AND approve ='1'" );
	$stats_main = $row['count'];
	$tpl->load_template( 'stats.tpl' );
	
	$tpl->set( '{datenbank}', $mysql_size );
    $tpl->set( '{size_info}', mksize($size_info['size']) );
	$tpl->set( '{news_num}', $stats_news );
	$tpl->set( '{news_allow}', $stats_approve );
	$tpl->set( '{comm_num}', $count_comments );
	$tpl->set( '{user_num}', $stats_users );
	$tpl->set( '{user_banned}', $stats_banned );
	$tpl->set( '{news_main}', $stats_main );
	$tpl->set( '{news_moder}', $stats_news - $stats_approve );

//Статистика торрента
   $result = $db->super_query("SELECT sum(leechers) leechers, sum(seeders) seeders, sum(leechers or seeders) torrents FROM ".PREFIX."_torrents");
   $result['peers'] = $result['leechers'] + $result['seeders'];
   if($result['peers']) {
      $leechers = $result['leechers']." (".round($result['leechers']*100/$result['peers'])."%)";
      $seeders = $result['seeders']." (".round($result['seeders']*100/$result['peers'])."%)";
   } else {$leechers = $seeders = 0;}
   $tr_a = $db->super_query("SELECT count(id) as c FROM ".PREFIX."_torrents");
   $rt_dz = $tr_a['c'] - $result['torrents'];
   if($tr_a['c']) {
      $tr_d = $rt_dz." (".round($rt_dz*100/$tr_a['c'])."%)";
      $result['torrents'] = $result['torrents']." (".round($result['torrents']*100/$tr_a['c'])."%)";
   } else {$tr_d = $tr_dz;}
    $res_info = $db->super_query("SELECT sum(downloaded) downloaded, sum(uploaded) uploaded FROM ".PREFIX."_users");
    $res_info['all'] = $res_info['downloaded'] + $res_info['uploaded'];
	$tpl->set( '{all_tor}', $tr_a['c'] );
	$tpl->set( '{active_tor}', $result['torrents'] );
	$tpl->set( '{dead_tor}', $tr_d );
	$tpl->set( '{activeconn_tor}', $result['peers'] );
	$tpl->set( '{seeders_tor}', $seeders );
	$tpl->set( '{leechers_tor}', $leechers );
    $tpl->set( '{sumupl_tor}', mksize($res_info['uploaded']) );
    $tpl->set( '{sumdown_tor}', mksize($res_info['downloaded']) );
    $tpl->set( '{sumall_tor}', mksize($res_info['all']) );

    /*$res = $db->super_query("SELECT sum(down_speed) down_speed, sum(up_speed) up_speed FROM xbt_files_users");
    $tpl->set( '{speed_up}', mksize($res['up_speed']) );
	$tpl->set( '{speed_down}', mksize($result['down_speed']));*/
//!статистика торрента

//Топ юзеров по новостям
	$db->query( "SELECT user_id, name, user_group, reg_date, lastdate, news_num, comm_num FROM " . USERPREFIX . "_users WHERE news_num > '0' ORDER BY news_num DESC LIMIT 0,10" );
	$top_table = "<thead><td>{$lang['top_name']}</td><td align=\"center\">{$lang['top_status']}</td><td align=\"center\">{$lang['top_reg']}</td><td align=\"center\">{$lang['top_last']}</td><td align=\"center\">{$lang['top_nnum']}</td><td align=\"center\">{$lang['top_cnum']}</td></thead>";
	while ( $row = $db->get_row() ) {

		$registration = langdate( $config['timestamp_active'], $row['reg_date'] );
		$last = langdate( $config['timestamp_active'], $row['lastdate'] );
        $user_name = "<a href=\"" . $config['http_home_url'] . "user/" . urlencode( $row['name'] ) . "/\">" . $row['name']. "</a>";
        $user_pm = "<a href=\"$PHP_SELF?do=pm&doaction=newpm&user=" . $row['user_id'] . "\">{$lang['top_pm']}</a>";

        if ($user_group[$row['user_group']]['colour']){
      			$group_span = $user_group[$row['user_group']]['colour'];
      			$group_name =  "<span style=\"color:{$group_span}\">".$user_group[$row['user_group']]['group_name']."</span>";
      		}else{
      			$group_name = $user_group[$row['user_group']]['group_name'];
      		}

    $top_table .= "<tr><td>{$user_name}</td><td align=\"center\">{$group_name}</td><td align=\"center\">{$registration}</td><td align=\"center\">{$last}</td><td align=\"center\">{$row['news_num']}</td><td align=\"center\">{$row['comm_num']}</td></tr>";
	}
	$db->free();
	$tpl->set( '{topusers}', $top_table );
//!Топ юзеров по новостям

//Топ по разданному
$i=0;
$sql_upload = $db->query("SELECT name,uploaded FROM ".USERPREFIX."_users ORDER BY uploaded DESC LIMIT 10;");
$upload = "<thead><td>№</td><td align=\"center\">Пользователь</td><td align=\"center\">Раздал</td></thead>";
   while ($u = $db->get_row($sql_upload)) {
      $i++;
      $dw = mksize($u['uploaded']);
      $user = "<a href=\"/user/".urlencode($u['name'])."/\">".$u['name']."</a>";
      $upload .= "<tr><td>{$i}</td><td>{$user}</td><td>{$dw}</td></tr>";
   }
   $u=$user="";
   $tpl->set( '{topuploaded}', $upload );
//!Топ по разданному

	$tpl->compile( 'content' );
	$tpl->clear();
    $cache->save( "stats", $tpl->result['content'], $config['skin'] );
}
?>