<?php
/*
*------------------------------phpMyBitTorrent V 2.0.4-------------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2009 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*-----------------   Sunday, September 14, 2008 9:05 PM   ---------------------*
*/
if (defined('IN_PMBT'))die ("You can't include this file");
define("IN_PMBT",true);
include("header.php");
  function get_ratio_color($ratio)
  {
    if ($ratio < 0.1) return "#ff0000";
    if ($ratio < 0.2) return "#ee0000";
    if ($ratio < 0.3) return "#dd0000";
    if ($ratio < 0.4) return "#cc0000";
    if ($ratio < 0.5) return "#bb0000";
    if ($ratio < 0.6) return "#aa0000";
    if ($ratio < 0.7) return "#990000";
    if ($ratio < 0.8) return "#880000";
    if ($ratio < 0.9) return "#770000";
    if ($ratio < 1) return "#660000";
    return "#000000";
  }

if (!$user->user) loginrequired("user");

function get_row_count($table, $suffix = "")
{
  if ($suffix)
    $suffix = " $suffix";
  ($r = mysql_query("SELECT COUNT(*) FROM $table$suffix")) or die(mysql_error());
  ($a = mysql_fetch_row($r)) or die(mysql_error());
  return $a[0];
}
function usertable($res, $frame_caption) {
$menu = '';
  print ("<div align=left><B>$frame_caption </B><BR>");
    if (mysql_num_rows($res) > 0)
	{
	print("<table border=1 cellspacing=0 cellpadding=2 class=table_table>\n");
  $num = 0;
  while ($a = mysql_fetch_assoc($res)) {
    ++$num;
    if ($a["uploaded"] == "0")
	  break;
    if ($a["downloaded"]) {
      $ratio = $a["uploaded"] / $a["downloaded"];
      $color = get_ratio_color($ratio);
      $ratio = number_format($ratio, 2);
      if ($color)
        $ratio = "<font color=$color>$ratio</font>";
    }
    else
      $ratio = "Inf.";
    if ($menu != "1") {
      echo "<tr>"
		."<td class=table_head>"._btstats_rank."</td>"
		."<td class=table_head align=left>"._btstats_user."</td>"
		."<td class=table_head>"._btstats_uploaded."</td>"
		."<td class=table_head>"._btstats_downloaded."</td>"
	    ."<td class=table_head align=right>"._btstats_ratio."</td>"
	    ."</tr>";
      $menu = 1;
    }
    print("<tr><td class=table_col1>$num</td><td class=table_col2 align=left><a href=user.php?op=profile&id=" . $a["id"] . "><b>" . $a["username"] .
          "</b></a></td><td class=table_col1 align=right>" . mksize($a["uploaded"]) .
          "</td><td class=table_col2 align=right>" . mksize($a["downloaded"]) .
          "</td><td class=table_col1 align=right>" . $ratio . "</td></tr>");
  }
  echo "</table></div>";
  	}else{
		echo "<font color=red>"._btstats_noshow."</font></div>";
	}
}

function _torrenttable($res, $frame_caption) {
$menu = '';
  print ("<div align=left><B>$frame_caption </B><BR>");
  if (mysql_num_rows($res) > 0)
	{
	  print("<table border=1 cellspacing=0 cellpadding=2 class=table_table>\n");
  $num = 0;
  while ($a = mysql_fetch_assoc($res)) {
      ++$num;
      if ($a["leechers"])
      {
        $r = $a["seeders"] / $a["leechers"];
        $ratio = "<font color=" . get_ratio_color($r) . ">" . number_format($r, 2) . "</font>";
      }
      else
        $ratio = "Inf.";
        if ($menu != "1") {
          echo "<tr>"
		      ."<td class=ttable_head>"._btstats_rank."</td>"
		      ."<td class=ttable_head align=left>"._btstats_user."</td>"
		      ."<td class=ttable_head align=right>"._btstats_completed."</td>"
		      ."<td class=ttable_head align=right>"._btstats_seeds."</td>"
		      ."<td class=ttable_head align=right>"._btstats_leech."</td>"
		      ."<td class=ttable_head align=right>"._btstats_peers."</td>"
	 	      ."<td class=ttable_head align=right>"._btstats_ratio."</td>"
 	          ."</tr>";
 	      $menu = 1;
        }
                $dispname = htmlspecialchars($a["name"]);
                $dispname = str_replace("_", " ", $dispname);
                $dispname = str_replace(".", " ", $dispname);
        print("<tr><td class=ttable_col1>$num</td><td class=ttable_col2 align=left><a   title=\"".$dispname."\" href=details.php?id=" . $a["id"] . "&hit=1><b>" .
        ((strlen($dispname) <= 25) ? $dispname : substr($dispname,0,24)."...")."</b></a></td><td class=ttable_col1 align=center>" . number_format($a["completed"]) .
        "</td><td class=ttable_col2 align=center>" . number_format($a["seeders"]) .
        "</td><td class=ttable_col1 align=center>" . number_format($a["leechers"]) .
        "</td><td class=ttable_col2 align=center>" . ($a["leechers"] + $a["seeders"]) .
        "</td><td class=ttable_col1 align=right>$ratio</td>\n");
    }
    echo "</table></div>";
	}else{
		echo "<font color=red>"._btstats_noshow."</font></div>";
	}
}

function countriestable($res, $frame_caption) {
    OpenTable2($frame_caption);
	  if (mysql_num_rows($res) > 0)
	{
    print("<table border=1 cellspacing=0 cellpadding=2 class=table_table>\n");
	
	echo "<tr>";
	echo "<td class=table_head>"._btstats_rank."</td>";
	echo "<td class=table_head align=left>"._btstats_country."</td>";
	echo "<td class=table_head align=right>"._btstats_users."</td>";
	echo "</tr>";
	
    $num = 0;
    while ($a = mysql_fetch_assoc($res))
    {
      ++$num;
      print("<tr><td class=table_col1>$num</td>
	  <td class=table_col2 align=left><img align=center src=images/flag/$a[flagpic]>&nbsp;<b>$a[name]</b></td>
	  <td align=right class=table_col1>$a[num]</td></tr>\n");
    }
    echo "</table>";
		CloseTable2();

		}else{
		echo "<font color=red>"._btstats_noshow."</font></div>";
	}
}
function postertable($res, $frame_caption) {
    OpenTable2($frame_caption);
	  if (mysql_num_rows($res) > 0)
	{
	print("<table border=1 cellspacing=0 cellpadding=2 class=table_table>\n");
	
	echo "<tr>";
	echo "<td class=table_head width=80>"._btstats_rank."</td>";
	echo "<td class=table_head align=left>"._btstats_user."</td>";
	echo "<td class=table_head align=left width=100>"._btstats_posted."</td>";
	echo "</tr>";
	
    $num = 0;
    while ($a = mysql_fetch_assoc($res))
    {
      ++$num;
      print("<tr><td class=table_col1>$num</td><td class=table_col2 align=left><a href=user.php?op=profile&id=$a[id]><b>$a[username]</b></td><td align=right class=table_col1>$a[num]</td></tr>\n");
    }
    echo "</table>";
	CloseTable2();
		}else{
		echo "<font color=red>"._btstats_noshow."</font>";
		CloseTable2();
	}
}

//main stats here
$a = @mysql_fetch_assoc(@mysql_query("SELECT id,username FROM ".$db_prefix."_users WHERE active = '1' ORDER BY id DESC LIMIT 5"));
if ($user->user)
  $latestuser = "<a href=user.php?op=profile&id=" . $a["id"] . ">" . $a["username"] . "</a>";
else
  $latestuser = "<b>$a[username]</b>";
$registered = number_format(get_row_count("".$db_prefix."_users"));
$torrents = number_format(get_row_count("".$db_prefix."_torrents"));

$result = mysql_query("SELECT SUM(downloaded) AS totaldl FROM ".$db_prefix."_users") or sqlerr(__FILE__, __LINE__); 

while ($row = mysql_fetch_array ($result)) 
{ 
$totaldownloaded      = $row["totaldl"]; 
} 
$result = mysql_query("SELECT SUM(uploaded) AS totalul FROM ".$db_prefix."_users") or sqlerr(__FILE__, __LINE__); 

while ($row = mysql_fetch_array ($result)) 
{ 
$totaluploaded      = $row["totalul"]; 
}
$seeders = get_row_count("".$db_prefix."_peers", "WHERE seeder='yes'");
$leechers = get_row_count("".$db_prefix."_peers", "WHERE seeder='no'");
$localtorrent = number_format(get_row_count("".$db_prefix."_torrents", "WHERE tracker IS NULL AND exeem IS NOT NULL"));
$externaltorrent = number_format(get_row_count("".$db_prefix."_torrents", "WHERE tracker IS NOT NULL"));

$usersactive = 0;
if ($leechers == 0)
  $ratio = "100";
else
  $ratio = round($seeders / $leechers * 100);
 if ($ratio < 20)
    $ratio = "<font class=red>" . $ratio . "%</font>";
 else
	$ratio .= "%";
$peers = number_format($seeders + $leechers);
$seeders = number_format($seeders);
$leechers = number_format($leechers);
// start count registered today
$res = mysql_query("SELECT COUNT(*) FROM ".$db_prefix."_users WHERE UNIX_TIMESTAMP(" . gmdate("YmdHis") . ") - UNIX_TIMESTAMP(regdate) < 86400");
$arr44 = mysql_fetch_row($res);
$regtoday = $arr44[0];

function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
//end here

///////////////////////////////////////// PAGE LAYOUT //////////////////////////////

OpenTable(_btstats_extra);


echo "<div align=left><font class=stats>"._btstats_welcome." $latestuser</font>";
echo "<br><font class=stats>"._btstats_total." $registered </font>";
echo "<br><font class=stats>"._btstats_new_today." $regtoday </font>";
echo "<br><font class=stats>"._btstats_active."  $peers </font>";
ECHO "<br><font class=stats>"._btstats_downloaded."  ".mksize($totaldownloaded)." </FONT>";
ECHO "<br><font class=stats>"._btstats_uploaded."  ".mksize($totaluploaded)." </FONT>";
echo "<br><font class=stats>"._btstats_tracking."  $torrents  "._btstats_torrents."</font>";
echo "<br><font class=stats>"._btstats_local."  $localtorrent  Torrents </font>";
echo "<br><font class=stats>"._btstats_external."  $externaltorrent  Torrents </font>";
echo "<br><font class=stats>"._btstats_seeds."$seeders </font>";
echo "<br><font class=stats>"._btstats_leech."$leechers </font>";
echo "<br><font class=stats>"._btstats_seed_ratio."  $ratio </font>";
echo "<br><br></div>";



  $r = mysql_query("SELECT ".$db_prefix."_users.id, ".$db_prefix."_users.username, COUNT(".$db_prefix."_torrents.owner) as num FROM ".$db_prefix."_torrents LEFT JOIN ".$db_prefix."_users ON ".$db_prefix."_users.id = ".$db_prefix."_torrents.owner GROUP BY owner ORDER BY num DESC LIMIT 10") or sqlerr();
  postertable($r, _btstats_top10_posters); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_users  ORDER BY uploaded DESC LIMIT 10") or die;
  usertable($r, _btstats_top10_uploaders); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_users  ORDER BY downloaded DESC LIMIT 10") or die;
  usertable($r, _btstats_top10_leechers); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_users WHERE downloaded > 104857600 ORDER BY uploaded - downloaded DESC LIMIT 10") or die(mysql_error());
  usertable($r, ""._btstats_top10_best_shares."<font class=small>"._btstats_100mb."</font>"); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_users WHERE downloaded > 104857600   ORDER BY downloaded - uploaded DESC, downloaded DESC LIMIT 10") or die(mysql_error());
  usertable($r, ""._btstats_top10_worst_shares."<font class=small>"._btstats_100mb."</font>"); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_torrents ORDER BY seeders + leechers DESC, seeders DESC, added ASC LIMIT 10") or sqlerr();
  _torrenttable($r, ""._btstats_top10_active."</font>"); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_torrents WHERE seeders >= 5 ORDER BY seeders / leechers DESC, seeders DESC, added ASC LIMIT 10") or sqlerr();
  _torrenttable($r, ""._btstats_top10_best_seed."<font class=small>"._btstats_top10_5seeds."</font>"); echo "<br>";

  $r = mysql_query("SELECT * FROM ".$db_prefix."_torrents WHERE leechers >= 5 AND completed > 0 ORDER BY seeders / leechers ASC, leechers DESC LIMIT 10") or sqlerr();
  _torrenttable($r, ""._btstats_top10_worst_seeded."<font class=small>"._btstats_top10_5leech."</font>"); echo "<br>";
  
    $r = mysql_query("SELECT * FROM ".$db_prefix."_torrents WHERE  completed > 0 ORDER BY seeders / leechers ASC, completed DESC LIMIT 10") or sqlerr();
  _torrenttable($r, ""._btstats_top10_most_complete."<font class=small></font>"); echo "<br>";

  $r = mysql_query("SELECT ".$db_prefix."_countries.name, flagpic, COUNT(".$db_prefix."_users.country) as num FROM ".$db_prefix."_countries LEFT JOIN ".$db_prefix."_users ON ".$db_prefix."_users.country = ".$db_prefix."_countries.id GROUP BY ".$db_prefix."_countries.name ORDER BY num DESC LIMIT 10") or sqlerr();
  countriestable($r, "<font class=small>"._btstats_top10_countries."</font>");echo "<br><br>";

  CloseTable();
include ("footer.php");
?>