<?php
/*
*----------------------------phpMyBitTorrent V 2.0-beta4-----------------------*
*--- 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 --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*-----------------   Sunday, September 14, 2008 9:05 PM   ---------------------*
*/
if (!eregi("admin.php",$_SERVER["PHP_SELF"])) die ("You can't include this file");

/* FULL ADMIN CHECK
THE STRONGEST DEFENSE AGAINST HACKED M0DS
*/

function fulladmincheck($user) {
        if (!$user->admin) return false;
        //THIS is the full check part
        global $db, $db_prefix, $_COOKIE;
        $userdata = cookie_decode($_COOKIE["btuser"]);
        if ($userdata[0] != $user->id) return false;
        if (addslashes($userdata[1]) != $user->name) return false;
        $sql = "SELECT id FROM ".$db_prefix."_users WHERE id = '".$user->id."' AND username = '".$user->name."' AND level = 'admin' AND act_key = '".addslashes($userdata[3])."' AND password = '".addslashes($userdata[2])."';";
        $res = $db->sql_query($sql) or btsqlerror($sql);
        $n = $db->sql_numrows($res);
        $db->sql_freeresult($res);
        if (!$n) return false;
        return true;
}

/* ADMIN ENTRY FUNCTION
DISPLAYS THE APPROPRIATE BUTTON FOR ADMIN SCRIPT

IN:
   NAME - NAME OF THE BUTTON
   OP - OPERATOR TO APPEND IN QUERY STRING
   TITLE - TEXT TO DISPLAY
OUT: NOTHING
*/
function adminentry($name, $op, $title) {
        global $theme;
        $image = "admin_".$name;
        if (file_exists("themes/$theme/pics/admin/".$image.".png")) {
                $image = "themes/$theme/pics/admin/".$image.".png";
        } elseif (file_exists("themes/$theme/pics/admin/".$image.".jpg")) {
                $image = "themes/$theme/pics/admin/".$image.".jpg";
        } elseif (file_exists("themes/$theme/pics/admin/".$image.".gif")) {
                $image = "themes/$theme/pics/admin/".$image.".gif";
        } elseif (file_exists("admin/buttons/".$image.".png")) {
                $image = "admin/buttons/".$image.".png";
        } elseif (file_exists("admin/buttons/".$image.".jpg")) {
                $image = "admin/buttons/".$image.".jpg";
        } elseif (file_exists("admin/buttons/".$image.".gif")) {
                $image = "admin/buttons/".$image.".gif";
        }

        $img = "<img src=\"".$image."\" border=\"0\" alt=\"".$title."\" title=\"".$title."\" /></a><br>";

        echo "<td align=\"center\" width=\"16%\"><p><a href=\"admin.php?op=".$op."#".$op."\">".$img."<b>".$title."</b></a><br /><br /></p></td>\n";
		unset($op);
}
/* ADMIN PICTURE FUNCTION
RETRIEVES AN IMAGE FROM ADMIN FOLDER

IN:
   NAME -  FILENAME OF THE IMAGE TO DISPLAY
   URL - (OPTIONAL) URL TO POINT A LINK TO
   ALT - (OPTIONAL) ALTERNATE TEXT FOR IMAGE
OUT:
   HTML <IMG> TAG POINTING TO ADMIN/PICS DIRECTORY
*/
function adminpic($name, $url = "", $alt = "") {
        if ($alt == "" AND $alt != null AND defined("_btalt_".$name)) $alt = constant("_btalt_".$name);

        $ret = "<img src=\"admin/pics/".$name."\" border=\"0\" alt=\"".$alt."\" title=\"".$alt."\" />";
        if ($url != "") {
                return "<a href=\"".$url."\">".$ret."</a>";
        }
        return $ret;
}

include("header.php");
if (!fulladmincheck($user)) loginrequired("admin");
require_once("admin/language/$language.php");

OpenTable(_admmenu);
echo "<table border=\"0\" width=\"100%\" cellspacing=\"1\"><tr>\n";
$counter = 2;
adminentry("home","home","Home");
#Fetching operators list and displaying Admin menu

$operators = Array();
$op_keys = Array();
$opdir = "admin/items";
$ophandle = opendir($opdir);
while ($opfile = readdir($ophandle)) {
        $op_keys = Array();
        if (!eregi("\.php$",$opfile)) continue;
        include($opdir."/".$opfile);
        foreach ($op_keys as $key) {
                $operators[$key] = $opfile;
        }
        if ($counter == 4) {
                echo "</tr>\n<tr>\n";
                $counter = 1;
        } else {
                $counter++;
        }
}
closedir($ophandle);
unset($opdir,$opfile,$op_keys);

echo"</tr>\n</table>\n";
CloseTable();

//The "Core"
if (isset($op) AND array_key_exists($op,$operators)){
echo "<a name=\"".$op."\"></a>";
require_once("admin/files/".$operators[$op]);
}else {
        OpenTable(_admoverview);
        //$sql = "SELECT COUNT(U.id) AS users, COUNT(T.id) AS torrents, COUNT(P.id) AS peers, SUM(P.upload_speed) AS speed FROM torrent_users U, torrent_torrents T, torrent_peers P;";
        echo "<p>";
        //Total users
        $sql = "SELECT COUNT(id) FROM ".$db_prefix."_users;";
        $res = $db->sql_query($sql);
        list ($totuser) = $db->sql_fetchrow($res);
        $db->sql_freeresult($res);
        echo "<b>"._admtotalusers."</b> ".$totuser."<br />\n";
        //Total Torrents and their size
        $sql = "SELECT COUNT(id), SUM(size) FROM ".$db_prefix."_torrents;";
        $res = $db->sql_query($sql);
        list ($tottorrent, $totshare) = $db->sql_fetchrow($res);
        $db->sql_freeresult($res);
        echo "<b>"._admtotaltorrents."</b> ".$tottorrent."<br />\n";
        echo "<b>"._admtotalshare."</b> ".mksize($totshare)."<br />\n";
        //Total peers and their speed
        $sql = "SELECT COUNT(id), (SUM(upload_speed)+SUM(download_speed))/2 FROM ".$db_prefix."_peers;";
        $res = $db->sql_query($sql);
        list ($totpeers, $totspeed) = $db->sql_fetchrow($res);
        $db->sql_freeresult($res);
        echo "<b>"._admtotalpeers."</b> ".$totpeers."<br />\n";
        echo "<b>"._admtotalspeed."</b> ".mksize($totspeed)."/s<br />\n";
        //Total seeders and total leechers
        $sql = "SELECT COUNT(id) FROM ".$db_prefix."_peers GROUP BY seeder ORDER BY seeder ASC;";
        $res = $db->sql_query($sql);
        list ($totseeders) = $db->sql_fetchrow($res);
        list ($totleechers) = $db->sql_fetchrow($res);
        $db->sql_freeresult($res);
        echo "<b>"._admtotalseeders."</b> ".$totseeders."<br />\n";
        echo "<b>"._admtotalleechers."</b> ".$totleechers."<br />\n";
        $sql = "SELECT COUNT(id) as cnt, client FROM ".$db_prefix."_peers WHERE client IS NOT NULL GROUP BY client ORDER BY cnt DESC LIMIT 1;";
        $res = $db->sql_query($sql);
        list ($cnt, $client) = $db->sql_fetchrow($res);
        $db->sql_freeresult($res);
        echo "<b>"._admmostusedclient."</b> ".$client." (".$cnt.")<br />\n";
        echo "</p>\n";
        CloseTable();
OpenTable("load");
if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
if (class_exists("COM")) {
	function mkprettytime2($s){
	    foreach (array("60:sec","60:min","24:hour","1:day") as $x) {
	        $y = explode(":", $x);
	        if ($y[0] > 1) {
	            $v = $s % $y[0];
	            $s = floor($s / $y[0]);
	        }
	        else
	            $v = $s;
	        $t[$y[1]] = $v;
	    }

	    if ($t['week'] > 1 || $t['week'] == 0) $wk = " weeks";
	    else $wk = " week";
	    if ($t['day'] > 1 || $t['day'] == 0) $day = " days";
	    else $day = " day";
	    if ($t['hour'] > 1 || $t['hour'] == 0) $hr = " hrs";
	    else $hr = " hr";
	    if ($t['min'] > 1 || $t['min'] == 0) $min = " mins";
	    else $min = " min";
	    if ($t['sec'] > 1 || $t['sec'] == 0) $sec = " secs";
	    else $sec = " sec";

	    if ($t["month"])
	        return "{$t['month']}$mth {$t['week']}$wk {$t['day']}$day ".sprintf("%d$hr %02d$min %02d$sec", $t["hour"], $t["min"], $t["sec"], $f["month"]);
	    if ($t["week"])
	        return "{$t['week']}$wk {$t['day']}$day ".sprintf("%d$hr %02d$min %02d$sec", $t["hour"], $t["min"], $t["sec"], $f["month"]);
	    if ($t["day"])
	        return "{$t['day']}$day ".sprintf("%d$hr %02d$min %02d$sec", $t["hour"], $t["min"], $t["sec"]);
	    if ($t["hour"])
	        return sprintf("%d$hr %02d$min %02d$sec", $t["hour"], $t["min"], $t["sec"]);
	    if ($t["min"])
	        return sprintf("%d$min %02d$sec", $t["min"], $t["sec"]);
	    return $t["sec"].$sec;
	}
	$wmi = new COM("Winmgmts://");
	$cpus = $wmi->InstancesOf("Win32_Processor");
	$os = $wmi->InstancesOf("Win32_OperatingSystem");
	$os = $os->Next();
	$os = $os->Caption." - ".$os->CSDVersion." ".$os->Version;
	$system = $wmi->InstancesOf("Win32_ComputerSystem");
	$system = $system->Next();
	$cpucount = $system->NumberOfProcessors;
	
	$ram = $wmi->InstancesOf("Win32_LogicalMemoryConfiguration");
	$ram = $ram->Next();
	$ramtotal = $ram->TotalPhysicalMemory*1024;
	
	$ram = $wmi->InstancesOf("Win32_PerfRawData_PerfOS_Memory");
	$ram = $ram->Next();
	$ramused = $ramtotal-$ram->AvailableBytes;
	$ramused = mksize($ramused);
	$ramtotal = mksize($ramtotal);
	
	$uptime = $wmi->InstancesOf("Win32_PerfFormattedData_PerfOS_System");
	$uptime = $uptime->Next();
	$uptime = mkprettytime2($uptime->SystemUpTime);

	while ($cpu = $cpus->Next()) {
		$cpus1[] = $cpu->LoadPercentage;
		$totalusage += $cpu->LoadPercentage;
	}
	
	$totalusage = round($totalusage/$cpucount, 2);	

	echo "<b>OS:</b> $os<BR>";
	echo "<b>Number of CPUs:</b> $cpucount<BR>";
	for ($i=0;$i<count($cpus1);$i++)
		echo "<b>CPU$i Usage:</b> $cpus1[$i]%<BR>";
	echo "<b>Total CPU Usage:</b> $totalusage%<BR>";
	echo "<B>RAM Usage:</b> $ramused/$ramtotal<BR>";
	echo "<b>Uptime:</b> $uptime";
}
}else{
// Users and load information
$reguptime = trim(exec("uptime"));
if ($reguptime) {
  if (preg_match("/, *(\d) (users?), .*: (.*), (.*), (.*)/", $reguptime, $uptime)) {
    $users[0] = $uptime[1];
    $users[1] = $uptime[2];
    $loadnow = $uptime[3];
    $load15 = $uptime[4];
    $load30 = $uptime[5];
  }
} else {
  $users[0] = "NA";
  $users[1] = "--";
  $loadnow = "NA";
  $load15 = "--";
  $load30 = "--";
}

$percent = min(100, round(exec('ps ax | grep -c apache') / 256 * 30 ));
echo "<B>Tracker Load:</B> ($percent%)<br>";

echo("<b>Current Users:</b> $users[0]<br>");
echo("<b>Current Load:</b> $loadnow<br><b>Load 15 mins ago:</b> $load15<br><b>Load 30 mins ago:</b> $load30<br><hr>");

// Operating system
$fp = @fopen("/proc/version", "r");
if ($fp) {
  $temp = fgets($fp);
  fclose($fp);

  if (preg_match("/version (.*?) /", $temp, $osarray)) {
    $kernel = $osarray[1];
    preg_match("/[0-9]{5,} (\((.* *)\)\))/", $temp, $osarray);
    $flavour = $osarray[2];
    $operatingsystem = $flavour." (".PHP_OS." ".$kernel.")";
    if (preg_match("/SMP/", $buf)) {
      $operatingsystem .= " (SMP)";
    }
  } else {
    $result = "(N/A)";
  }
} else {
  $result = "(N/A)";
}

echo("<b>Operating System:</b><br>$operatingsystem");
}
CloseTable();
}

include("footer.php");
?>