<?php
/*
*----------------------------phpMyBitTorrent V 2.0-----------------------------*
*--- 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 (!defined('IN_PMBT')) die ("You can't access this file directly");

if (!$user->user) loginrequired("user");
$id = intval($id);
if (!get_magic_quotes_gpc()) $username = (isset($username)) ? addslashes($username) :false;
if (!isset($p1) OR !is_numeric($p1) OR $p1 < 1) $p1 = 1;
if (!isset($p2) OR !is_numeric($p2) OR $p2 < 1) $p2 = 1;
if (!isset($p3) OR !is_numeric($p3) OR $p3 < 1) $p3 = 1;
if (!isset($p4) OR !is_numeric($p4) OR $p4 < 1) $p4 = 1;

if (!$user->premium) $passwhere = " AND T.password IS NULL ";
else $passwhere = "";
if (isset($id) AND $id != 0) {
        $sql_profile = "SELECT U.id AS id, U.can_shout AS can_shout, U.username AS username, U.name AS name, U.tzoffset AS tzoffset, U.level AS level, U.regdate AS regdate, U.email AS email, U.avatar AS avatar, U.lastlogin AS lastlogin, U.lastip AS lastip, U.lasthost AS lasthost, U.uploaded AS uploaded, U.downloaded AS downloaded, U.uploaded/U.downloaded as ratio, U.accept_mail AS accept_mail, U.active AS active, U.skype AS skype, U.jabber AS jabber, U.aim AS aim, U.icq AS icq, U.msn AS msn, U.yahoo AS yahoo, U.invited_by AS invited_by, U.invitees AS invitees, U.invites AS invites, U.invitedate AS invitedate, U.warned AS warned, U.country AS country, U.seedbonus AS seedbonus, U.hitruns AS hitruns, U.can_do AS can_do, U.helper AS helper, U.help_able AS help_able FROM ".$db_prefix."_users U where U.id = '".$id."' LIMIT 1;";
        $sql_owned_torrents = "SELECT T.*, IF(T.numratings < '$minvotes', NULL, ROUND(T.ratingsum / T.numratings, 1)) AS rating, C.name AS cat_name, C.image AS cat_pic FROM ".$db_prefix."_users U, ".$db_prefix."_torrents T, ".$db_prefix."_categories C WHERE T.category = C.id AND T.owner = U.id AND T.ownertype = 0 AND U.id = '".$id."' ".$passwhere." ORDER BY T.added DESC;";
} elseif (isset($username) AND $username != "") {
        $sql_profile = "SELECT U.id AS id ,U.can_shout AS can_shout, U.username AS username, U.name AS name, U.level AS level, U.regdate AS regdate, U.email AS email, U.avatar AS avatar, U.lastlogin AS lastlogin, U.lastip AS lastip, U.lasthost AS lasthost, U.uploaded AS uploaded, U.downloaded AS downloaded, U.uploaded/U.downloaded AS ratio, U.accept_mail AS accept_mail, U.active AS active, U.skype AS skype, U.jabber AS jabber, U.aim AS aim, U.icq AS icq, U.msn AS msn, U.yahoo AS yahoo, U.invited_by AS invited_by, U.invitees AS invitees, U.invites AS invites, U.invitedate AS invitedate, U.warned AS warned, U.country AS country, U.seedbonus AS seedbonus, U.hitruns AS hitruns, U.can_do AS can_do, U.helper AS helper, U.help_able AS help_able FROM ".$db_prefix."_users U where U.username = '".$username."' LIMIT 1;";
        $sql_owned_torrents = "SELECT T.*, IF(T.numratings < '$minvotes', NULL, ROUND(T.ratingsum / T.numratings, 1)) AS rating, C.name AS cat_name, C.image AS cat_pic FROM ".$db_prefix."_users U, ".$db_prefix."_torrents T, ".$db_prefix."_categories C WHERE T.category = C.id AND T.owner = U.id AND T.ownertype = 0 AND U.username = '".$username."' ".$passwhere." ORDER BY T.added DESC;";
}else{
        $sql_profile = "SELECT U.id AS id ,U.can_shout AS can_shout, U.username AS username, U.name AS name, U.level AS level, U.regdate AS regdate, U.email AS email, U.avatar AS avatar, U.lastlogin AS lastlogin, U.lastip AS lastip, U.lasthost AS lasthost, U.uploaded AS uploaded, U.downloaded AS downloaded, U.uploaded/U.downloaded AS ratio, U.accept_mail AS accept_mail, U.active AS active, U.skype AS skype, U.jabber AS jabber, U.aim AS aim, U.icq AS icq, U.msn AS msn, U.yahoo AS yahoo, U.invited_by AS invited_by, U.invitees AS invitees, U.invites AS invites, U.invitedate AS invitedate, U.warned AS warned, U.country AS country, U.seedbonus AS seedbonus, U.hitruns AS hitruns, U.can_do AS can_do, U.helper AS helper, U.help_able AS help_able FROM ".$db_prefix."_users U where U.username = '".$user->name."' LIMIT 1;";
        $sql_owned_torrents = "SELECT T.*, IF(T.numratings < '$minvotes', NULL, ROUND(T.ratingsum / T.numratings, 1)) AS rating, C.name AS cat_name, C.image AS cat_pic FROM ".$db_prefix."_users U, ".$db_prefix."_torrents T, ".$db_prefix."_categories C WHERE T.category = C.id AND T.owner = U.id AND T.ownertype = 0 AND U.username = '".$user->name."' ".$passwhere." ORDER BY T.added DESC;";
}
$res = $db->sql_query($sql_profile) OR btsqlerror($sql_profile);
if ($db->sql_numrows($res) < 1) bterror(_btusernoexist,_btuserprofile);
$userrow = $db->sql_fetchrow($res);
// Country code
$sql = ("SELECT name,flagpic FROM ".$db_prefix."_countries WHERE id=".$userrow['country']." LIMIT 1");
$res = $db->sql_query($sql) OR btsqlerror($sql);
if ($db->sql_numrows($res) == 1) 
{
  $arr = $db->sql_fetchrow($res);
  $country = $arr[name];
  $flag = $arr[flagpic];
}
//end Country code
//invite code
if (isset($type) AND $type == "invite" && mkglobal("email")) {
OpenTable2("User invite");
		print("<CENTER>Invite successful!</CENTER><br><BR>A confirmation email has been sent to the address you specified (" . htmlspecialchars($email) . "). They need to read and respond to this email before they can use their account. If they don't do this, the new account will be deleted automatically after a few days.");
CloseTable2();
}//end invite code
OpenTable(_btuserprofile);
echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"3\" width=\"100%\">\n";

#Avatar
if (file_exists("avatars/".$userrow["avatar"]) AND $userrow["avatar"] != "blank.gif") echo "<tr><td><p>"._btavatar."</p></td><td><p><img src=\"avatars/".$userrow["avatar"]."\" alt=\"Avatar\"></p></td></tr>\n";

#edit profile
#edit profile
if(($user->admin AND $user->id!=$userrow["id"])){
	echo "<tr><td><p>"._btactions."</p></td><td><p>";
	echo pic("editprofile.png","user.php?op=editprofile&id=".$userrow["id"],_btedituserprofile);
	echo pic("ban.png","admin.php?op=addban&username=".$userrow["username"],_btuserban);
	echo pic("drop.gif","user.php?op=delete&id=".$userrow["id"],_btuserdelete);
	//echo pic("icon_chat.gif","user.php?op=banchat&id=".$userrow["id"],_btuserdelete);
}elseif ($user->id==$userrow["id"]){
	echo "<tr><td><p>"._btactions."</p></td><td><p>";
	echo pic("editprofile.png","user.php?op=editprofile",_btedituserprofile);
}echo pic("rules.png","rules.php",_btrulez);
	if(($user->moderator))echo pic("icon_chat.gif","user.php?op=".(($userrow["can_shout"] == 'true') ? 'banchat' : 'unbanchat')."&id=".$userrow["id"],(($userrow["can_shout"] == 'true') ? 'banchat' : 'unbanchat'));


#Username
echo "<tr><td><p>"._btusername."</p></td><td><p>".$userrow["username"]."</p></td></tr>\n";
print("<tr><td><p>WARNED:</p></td><td ><p>");
if ($userrow["warned"])
{
echo pic("warning.gif");
print("Yes</p></td></tr>");
}
 else
 {
  print("No</p></td></tr>");
  
 }

#Complete name
if (!empty($userrow["name"])) echo "<tr><td><p>"._btcompletename."</p></td><td><p>".$userrow["name"]."</p></td></tr>\n";

#User Class
echo "<tr><td><p>"._btclass."</p></td><td><p>";
switch ($userrow["level"]) {
        case "banned": {
                echo _btclassbanned;
                break;
        }
        case "user": {
                echo _btclassuser;
                break;
        }
        case "moderator": {
                echo _btclassmoderator;
                break;
        }
        case "premium": {
                echo _btclasspremium;
                break;
        }
        case "admin": {
                echo _btclassadmin;
                break;
        }
}
echo "</p></td><tr>\n";
echo "<tr><td><p>Group</p></td><td><p>".$userrow['can_do'];
echo "</p></td><tr>\n";
if($user->id!=$userrow["id"]){
$sqlbook = "SELECT slave FROM ".$db_prefix."_private_messages_bookmarks WHERE master = '".$user->id."' AND slave = '".$userrow["id"]."';";
$sqlblack = "SELECT slave FROM ".$db_prefix."_private_messages_blacklist WHERE master = '".$user->id."' AND slave = '".$userrow["id"]."';";
$resbook = $db->sql_query($sqlbook) or btsqlerror($sqlbook);
$resblack = $db->sql_query($sqlblack) or btsqlerror($sqlblack);
echo "<tr><td><p>"._btpm."</p></td><td><p>".pic("pm_write.png","pm.php?op=send&to=".$userrow["id"],_btpmnewmsg);
if (!$db->sql_numrows($resbook)) echo pic("pm_bookmark.png","pm.php?op=bookmark&id=".$userrow["id"],_btpmbookmarkuser);
if ($db->sql_numrows($resbook)) echo pic("pm_rmbookmark.png","pm.php?op=removebookmark&id=".$userrow["id"],_btpmunbookmarkuser);
if (!$db->sql_numrows($resblack)) echo pic("pm_blacklist.png","pm.php?op=blacklist&id=".$userrow["id"],_btpmblacklistuser);
if ($db->sql_numrows($resblack)) echo pic("pm_rmblacklist.png","pm.php?op=removeblacklist&id=".$userrow["id"],_btpmunblacklistuser);
echo "</p></td></tr>\n";
$db->sql_freeresult($sqlbook);
$db->sql_freeresult($sqlblack);
}
#Contacts
echo "<tr><td><p>"._btcontacts."</p></td><td><p>";
if ($userrow["accept_mail"] == "yes") echo pic("icon_mail.gif","mailto:".$userrow["email"]);
if (!empty($userrow["aim"])) echo pic("button_aim.gif","aim:goim?screenname=".$userrow["aim"]);
if (!empty($userrow["icq"])) echo pic("button_icq.gif","http://www.icq.com/whitepages/wwp.php?to=".$userrow["icq"]);
if (!empty($userrow["jabber"])) echo pic("button_jabber.gif",null,$userrow["jabber"]);
if (!empty($userrow["msn"])) echo pic("button_msn.gif","http://members.msn.com/".$userrow["msn"]);
if (!empty($userrow["skype"])) echo pic("button_skype.gif","skype:".$userrow["skype"]."?chat",$userrow["skype"]);
if (!empty($userrow["yahoo"])) echo pic("button_yahoo.gif","http://edit.yahoo.com/config/send_webmesg?.target=".$userrow["yahoo"]);
echo "</p></td></tr>\n";

#Email (Admin Only)
if ($user->admin) echo "<tr><td><p>"._btemailaddress."</p></td><td><p>".$userrow["email"]."</p></td></tr>\n";
if (!$userrow["country"] == 0) echo "<tr><td><p>Country</p></td><td><p><img src=\"images/flag/".$flag."\" alt=\"$country\"></p></td></tr>\n";
#Registered
echo "<tr><td><p>"._btregistered."</p></td><td><p>".formatTimeStamp($userrow["regdate"])."</p></td></tr>\n";
echo "<tr><td><p>Last seen</p></td><td><p>".formatTimeStamp($userrow["lastlogin"])."</p></td></tr>\n";
echo "<tr><td><p>Site Helper</p></td><td><p>";
echo ($userrow["helper"] == 'true')? "Yes":"No";
echo "</p></td></tr>\n";
if($userrow["helper"] == 'true')echo "<tr><td><p>Help For</p></td><td><p>".$userrow["help_able"]."</p></td></tr>\n";

#Up & Down
echo "<tr><td><p>"._btuploadedbts."</p></td><td><p>".mksize($userrow["uploaded"])."</p></td></tr>\n";
echo "<tr><td><p>"._btdownloadedbts."</p></td><td><p>".mksize($userrow["downloaded"])."</p></td></tr>\n";
echo "<tr><td><p>"._btratio."</p></td><td><p>";
if ($userrow["downloaded"] == 0)
        echo "&infin;";
elseif ($userrow["ratio"] < 0.1)
        echo "<font color=#ff0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.2)
        echo "<font color=#ee0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.3)
        echo "<font color=#dd0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.4)
        echo "<font color=#cc0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.5)
        echo "<font color=#bb0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.6)
        echo "<font color=#aa0000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.7)
        echo "<font color=#990000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.8)
        echo "<font color=#880000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 0.9)
        echo "<font color=#770000>" . number_format($userrow["ratio"], 2) . "</font>";
elseif ($userrow["ratio"] < 1)
        echo "<font color=#660000>" . number_format($userrow["ratio"], 2) . "</font>";
else
        echo "<font color=#00FF00>".  number_format($userrow["ratio"], 2) . "</font>";

echo "</p></td></tr>\n";
print("<tr><td><p>Bonus Points: </p></td><td><p>$userrow[seedbonus]</p></td></tr>\n");
	//invite code start
if ($user->admin){
echo"<tr><td class=rowhead>User IP: </td><td align=left>".long2ip($userrow['lastip'])."</a></td></tr>\n";
echo"<tr><td class=rowhead>User Host: </td><td align=left>$userrow[lasthost]</a></td></tr>\n";
echo"<tr><td class=rowhead>Hit And Runs: </td><td align=left><a href=hitruns.php?userid=$userrow[id]>View</a></td></tr>\n";

}
if ($user->admin && $userrow["invites"] > 0 || $userrow["id"] == $user->id && $userrow["invites"] > 0)
{
print("<tr><td class=rowhead>Invitations: </td><td align=left>$userrow[invites]</a></td></tr>\n");
}
if ($user->admin && $userrow["invited_by"] > 0 || $userrow["id"] == $user->id && $userrow["invited_by"] > 0)
{
$sql = ("SELECT username, warned FROM ".$db_prefix."_users WHERE id='".$userrow['invited_by']."'");
$invited_by = $db->sql_query($sql);
$invited_by2 = $db->sql_fetchrow($invited_by);
		if($invited_by2["warned"] == "1") $warned = '<img src="images/warning.gif" alt="warned" />';
		else
		$warned = '';

print("<tr><td>Invited By : </td><td align=left><a href=user.php?op=profile&id=" .$userrow['invited_by'] .">".$invited_by2['username']."</a>".$warned."</td></tr>\n");
}
if ($user->premium && $userrow["invitees"] > 0 || $userrow["id"] == $user->id && $userrow["invitees"] > 0)
{
$compl = $userrow["invitees"];
$compl_list = explode(" ", $compl);
$arr = array();

foreach($compl_list as $array_list)
$arr[] = $array_list;

$compl_arr = array_reverse($arr, TRUE);
$f=0;
foreach($compl_arr as $user_id)
{

$sql = ("SELECT id, username FROM ".$db_prefix."_users WHERE id='$user_id' and active='1'");
$compl_user = $db->sql_query($sql);
$compl_users = $db->sql_fetchrow($compl_user);


if ($compl_users["id"] > 0)
{
echo("<tr><td>Memberes Invited: </td><td>");

$compl = $userrow["invitees"];
$compl_list = explode(" ", $compl);
$arr = array();

foreach($compl_list as $array_list)
$arr[] = $array_list;

$compl_arr = array_reverse($arr, TRUE);

$i = 0;
foreach($compl_arr as $user_id)
{

$sql = ("SELECT id, username, warned FROM ".$db_prefix."_users WHERE id='$user_id' and active='1' ORDER BY username");
$compl_user = $db->sql_query($sql);
$compl_users = $db->sql_fetchrow($compl_user);
		if($compl_users["warned"] == "1") $warned = '<img src="images/warning.gif" alt="warned" />';
		else
		$warned = '';

echo("<a href=user.php?op=profile&id=" . $compl_users["id"] . ">" . $compl_users["username"] . "</a>".$warned."&nbsp;");
if ($i == "9")
break;
$i++;
}
echo ("</td></tr>");
$f = 1;
}
if ($f == "1")
break;
}
}
//invite code end


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

#Torrent by user
echo "<a name=\"#p1\" id=\"p1\"></a>";
OpenTable(_btpersonal);
if (!$user->moderator) $ownertype = "AND ownertype = 0";
else $ownertype = "";

$from = ($p1 - 1) * $torrent_per_page;

$totsql = "SELECT COUNT(*) as tot FROM ".$db_prefix."_torrents WHERE banned = 'no' AND owner = '".$userrow["id"]."' ".$ownertype.";";
$totres = $db->sql_query($totsql);
list ($tot) = $db->sql_fetchrow($totres);
$db->sql_freeresult($totres);

$pages = ceil($tot / $torrent_per_page);

if($p1 < $pages) {
        $next = "<b><a href=\"user.php?op=profile&id=".$userrow["id"]."&p1=".($p1 + 1);
        if ($p2 > 1) $next .= "&p2=".$p2;
        if ($p3 > 1) $next .= "&p3=".$p3;
        $next .= "#p1\">&gt;&gt;&gt;&gt;</a></b>";
} else {
        $next = "&gt;&gt;&gt;&gt;";
}
if ($p1 > 1) {
        $prev = "<b><a href=\"user.php?op=profile&id=".$userrow["id"]."&p1=".($p1 - 1);
        if ($p2 > 1) $prev .= "&p2=".$p2;
        if ($p3 > 1) $prev .= "&p3=".$p3;
        $prev .= "#p1\">&lt;&lt;&lt;&lt;</a></b>";
} else {
        $prev = "&lt;&lt;&lt;&lt;";
}
$pager = "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p2 > 1) $pager .= "&p2=".$p2;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "#p1\">".(($p1 == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

if (($p1 - 15) > 1) $pager .= "...";
for ($i = max(2,$p1 - 15); $i < min($pages, $p1 + 15); $i++) {
        $pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"]."&p1=".$i;
        if ($p2 > 1) $pager .= "&p2=".$p2;
        if ($p3 > 1) $pager .= "&p3=".$p3;
        $pager .= "#p1\">".(($i == $p1) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
}
if (($p1 + 15) < $pages) $pager .= "...";
$pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"]."&p1=".$pages;
if ($p2 > 1) $pager .= "&p2=".$p2;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "#p1\">".(($p1 == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";

$sql = "SELECT ".$db_prefix."_torrents.*, IF(".$db_prefix."_torrents.numratings < '$minvotes', NULL, ROUND(".$db_prefix."_torrents.ratingsum / ".$db_prefix."_torrents.numratings, 1)) AS rating, ".$db_prefix."_categories.name AS cat_name, ".$db_prefix."_categories.image AS cat_pic, U.username, IF(U.name IS NULL, U.username, U.name) as user_name, U.level as user_level FROM ".$db_prefix."_torrents LEFT JOIN ".$db_prefix."_categories ON category = ".$db_prefix."_categories.id LEFT JOIN ".$db_prefix."_users U ON ".$db_prefix."_torrents.owner = U.id WHERE banned = 'no' AND owner = '".$userrow["id"]."' ".$ownertype." ORDER BY ".$db_prefix."_torrents.added DESC LIMIT ".$from.",".$torrent_per_page.";";
$res = $db->sql_query($sql);
if ($db->sql_numrows($res) < 1) {
        OpenTable2();
        echo "<p>"._btnotorrents."</p>";
        CloseTable2();
} else {
        torrenttable($res,"usertorrent");
        echo "<table border=\"0\" width=\"100%\"><tr><td align=\"left\"><p>".$prev."</p></td><td align=\"center\"><p>".(($pages > 1) ? $pager : "")."</p></td><td align=\"right\"><p>".$next."</p></td></tr></table>";
}
$db->sql_freeresult($res);
CloseTable();

#Seeded Torrents
echo "<a name=\"#p2\" id=\"p2\"></a>";
OpenTable(_btseedby);

$from = ($p2 - 1) * $torrent_per_page;

$totsql = "SELECT COUNT(DISTINCT torrent) AS tot FROM ".$db_prefix."_peers WHERE uid = '".$userrow["id"]."' AND seeder = 'yes';";
$totres = $db->sql_query($totsql) or btsqlerror($totsql);
list ($tot) = $db->sql_fetchrow($totres);
$db->sql_freeresult($totres);

$pages = ceil($tot / $torrent_per_page);

if($p2 < $pages) {
        $next = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $next .= "&p1=".$p1;
        $next .= "&p2=".($p2 + 1);
        if ($p3 > 1) $next .= "&p3=".$p3;
        $next .= "#p2\">&gt;&gt;&gt;&gt;</a></b>";
} else {
        $next = "&gt;&gt;&gt;&gt;";
}
if ($p2 > 1) {
        $prev = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $prev .= "&p1=".$p1;
        $prev .= "&p2=".($p2 - 1);
        if ($p3 > 1) $prev .= "&p3=".$p3;
        $prev .= "#p2\">&lt;&lt;&lt;&lt;</a></b>";
} else {
        $prev = "&lt;&lt;&lt;&lt;";
}
$pager = "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $prev .= "&p1=".$p1;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "#p2\">".(($p2 == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

if (($p2 - 15) > 1) $pager .= "...";
for ($i = max(2,$p2 - 15); $i < min($pages, $p2 + 15); $i++) {
        $pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $pager .= "&p1=".$p1;
        $pager .= "&p2=".$i;
        if ($p3 > 1) $pager .= "&p3=".$p3;
        $pager .= "#p2\">".(($i == $p2) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
}
if (($p2 + 15) < $pages) $pager .= "...";
$pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $pager .= "&p1=".$p1;
$pager .= "p2=".$pages;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "#p2\">".(($p2 == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";

$sql = "SELECT P.uid, T.*, IF(T.numratings < '".$minvotes."', NULL, ROUND(T.ratingsum / T.numratings, 1)) AS rating, C.name AS cat_name, C.image AS cat_pic, U.username, IF(U.name IS NULL, U.username, U.name) as user_name, U.level as user_level FROM ".$db_prefix."_peers P LEFT JOIN ".$db_prefix."_torrents T ON P.torrent = T.id LEFT JOIN ".$db_prefix."_categories C ON category = C.id LEFT JOIN ".$db_prefix."_users U ON P.uid = U.id WHERE banned = 'no' AND P.uid = '".$userrow["id"]."' AND P.seeder = 'yes' ORDER BY T.added DESC LIMIT ".$from.",".$torrent_per_page.";";
$res = $db->sql_query($sql) or btsqlerror($sql);
if ($db->sql_numrows($res) < 1) {
        OpenTable2();
        echo "<p>"._btnotorrents."</p>";
        CloseTable2();
} else {
        torrenttable($res,"usertorrent");
        echo "<table border=\"0\" width=\"100%\"><tr><td align=\"left\"><p>".$prev."</p></td><td align=\"center\"><p>".(($pages > 1) ? $pager : "")."</p></td><td align=\"right\"><p>".$next."</p></td></tr></table>";
}
$db->sql_freeresult($res);
CloseTable();


#Leeched Torrents
echo "<a name=\"#p3\" id=\"p3\"></a>";
OpenTable(_btleechby);

$from = ($p2 - 1) * $torrent_per_page;

$totsql = "SELECT COUNT(DISTINCT torrent) AS tot FROM ".$db_prefix."_peers WHERE uid = '".$userrow["id"]."' AND seeder = 'no';";
$totres = $db->sql_query($totsql) or btsqlerror($totsql);
list ($tot) = $db->sql_fetchrow($totres);
$db->sql_freeresult($totres);

$pages = ceil($tot / $torrent_per_page);

if($p3 < $pages) {
        $next = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $next .= "&p1=".$p1;
        if ($p2 > 1) $next .= "&p2=".$p2;
        $next .= "&p3=".($p3 + 1);
        $next .= "#p3\">&gt;&gt;&gt;&gt;</a></b>";
} else {
        $next = "&gt;&gt;&gt;&gt;";
}
if ($p3 > 1) {
        $prev = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $prev .= "&p1=".$p1;
        if ($p2 > 1) $prev .= "&p2=".$p2;
        $prev .= "&p3=".($p3 - 1);
        $prev .= "#p3\">&lt;&lt;&lt;&lt;</a></b>";
} else {
        $prev = "&lt;&lt;&lt;&lt;";
}
$pager = "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $prev .= "&p1=".$p1;
if ($p2 > 1) $pager .= "&p2=".$p2;
$pager .= "#p3\">".(($p3 == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

if (($p3 - 15) > 1) $pager .= "...";
for ($i = max(2,$p3 - 15); $i < min($pages, $p3 + 15); $i++) {
        $pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $pager .= "&p1=".$p1;
        if ($p2 > 1) $pager .= "&p2=".$p2;
        $pager .= "&p3=".$i;
        $pager .= "#p3\">".(($i == $p3) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
}
if (($p3 + 15) < $pages) $pager .= "...";
$pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $pager .= "&p1=".$p1;
if ($p2 > 1) $pager .= "&p2=".$p2;
$pager .= "p3=".$pages;
$pager .= "#p3\">".(($p3 == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";

$sql = "SELECT P.uid, T.*, IF(T.numratings < '".$minvotes."', NULL, ROUND(T.ratingsum / T.numratings, 1)) AS rating, C.name AS cat_name, C.image AS cat_pic, U.username, IF(U.name IS NULL, U.username, U.name) as user_name, U.level as user_level FROM ".$db_prefix."_peers P LEFT JOIN ".$db_prefix."_torrents T ON P.torrent = T.id LEFT JOIN ".$db_prefix."_categories C ON category = C.id LEFT JOIN ".$db_prefix."_users U ON P.uid = U.id WHERE banned = 'no' AND P.uid = '".$userrow["id"]."' AND P.seeder = 'no' ORDER BY T.added DESC LIMIT ".$from.",".$torrent_per_page.";";
$res = $db->sql_query($sql) or btsqlerror($sql);
if ($db->sql_numrows($res) < 1) {
        OpenTable2();
        echo "<p>"._btnotorrents."</p>";
        CloseTable2();
} else {
        torrenttable($res,"usertorrent");
        echo "<table border=\"0\" width=\"100%\"><tr><td align=\"left\"><p>".$prev."</p></td><td align=\"center\"><p>".(($pages > 1) ? $pager : "")."</p></td><td align=\"right\"><p>".$next."</p></td></tr></table>";
}
$db->sql_freeresult($res);
CloseTable();
echo "<a name=\"#p4\" id=\"p4\"></a>";
OpenTable("Torrents Snatched By: ".htmlspecialchars($userrow["username"]));
$from = ($p4 - 1) * $torrent_per_page;

$totsql = "SELECT COUNT(DISTINCT torrentid) AS tot FROM ".$db_prefix."_snatched WHERE userid = '".$userrow["id"]."' ;";
$totres = $db->sql_query($totsql) or btsqlerror($totsql);
list ($tot) = $db->sql_fetchrow($totres);
$db->sql_freeresult($totres);

$pages = ceil($tot / $torrent_per_page);

if($p4 < $pages) {
        $next = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $next .= "&p1=".$p1;
        if ($p2 > 1) $next .= "&p2=".$p2;
        if ($p3 > 1) $next .= "&p3=".$p3;
        $next .= "&p4=".($p4 + 1);
        $next .= "#p4\">&gt;&gt;&gt;&gt;</a></b>";
} else {
        $next = "&gt;&gt;&gt;&gt;";
}
if ($p4 > 1) {
        $prev = "<b><a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $prev .= "&p1=".$p1;
        if ($p2 > 1) $prev .= "&p2=".$p2;
        if ($p3 > 1) $prev .= "&p3=".$p3;
        $prev .= "&p4=".($p4 - 1);
        $prev .= "#p4\">&lt;&lt;&lt;&lt;</a></b>";
} else {
        $prev = "&lt;&lt;&lt;&lt;";
}
$pager = "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $pager .= "&p1=".$p1;
if ($p2 > 1) $pager .= "&p2=".$p2;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "#p4\">".(($p4 == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

if (($p4 - 15) > 1) $pager .= "...";
for ($i = max(2,$p4 - 15); $i < min($pages, $p4 + 15); $i++) {
        $pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
        if ($p1 > 1) $pager .= "&p1=".$p1;
        if ($p2 > 1) $pager .= "&p2=".$p2;
        if ($p3 > 1) $pager .= "&p3=".$p3;
        $pager .= "&p4=".$i;
        $pager .= "#p4\">".(($i == $p4) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
}
if (($p4 + 15) < $pages) $pager .= "...";
$pager .= "<a href=\"user.php?op=profile&id=".$userrow["id"];
if ($p1 > 1) $pager .= "&p1=".$p1;
if ($p2 > 1) $pager .= "&p2=".$p2;
if ($p3 > 1) $pager .= "&p3=".$p3;
$pager .= "&p4=".$pages;
$pager .= "#p4\">".(($p4 == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";

$sql = ("select ".$db_prefix."_users.id, ".$db_prefix."_users.username,  ".$db_prefix."_users.uploaded, ".$db_prefix."_users.downloaded, ".$db_prefix."_snatched.torrentid, ".$db_prefix."_snatched.seeding_time, ".$db_prefix."_snatched.completedat, ".$db_prefix."_snatched.torrent_name, ".$db_prefix."_snatched.last_action, ".$db_prefix."_snatched.ip, ".$db_prefix."_snatched.seeder, ".$db_prefix."_snatched.userid from ".$db_prefix."_snatched inner join ".$db_prefix."_users on ".$db_prefix."_snatched.userid = ".$db_prefix."_users.id inner join ".$db_prefix."_torrents on ".$db_prefix."_snatched.torrentid = ".$db_prefix."_torrents.id where  ".$db_prefix."_snatched.userid ='".$userrow["id"]."'  ORDER BY ".$db_prefix."_snatched.torrentid desc LIMIT ".$from.",".$torrent_per_page."")or print(mysql_error());
$res2 = mysql_query("select ".$db_prefix."_users.warned, ".$db_prefix."_users.lastlogin, ".$db_prefix."_snatched.uploaded, ".$db_prefix."_snatched.downloaded, ".$db_prefix."_snatched.speedup, ".$db_prefix."_snatched.ip, ".$db_prefix."_snatched.speeddown, ".$db_prefix."_snatched.userid from ".$db_prefix."_snatched inner join ".$db_prefix."_users on ".$db_prefix."_snatched.userid = ".$db_prefix."_users.id inner join ".$db_prefix."_torrents on ".$db_prefix."_snatched.torrentid = ".$db_prefix."_torrents.id WHERE  ".$db_prefix."_snatched.userid ='".$userrow["id"]."'  ORDER BY ".$db_prefix."_snatched.torrentid desc LIMIT ".$from.",".$torrent_per_page."")or print(mysql_error());
$res = $db->sql_query($sql) or btsqlerror($sql);
if ($db->sql_numrows($res) < 1) {
        OpenTable2();
        echo "<p>"._btnotorrents."</p>";
        CloseTable2();
} else {
print("<table border=1 cellspacing=0 cellpadding=1 align=center>\n");
print("<tr><td class=table_head align=center>Torrent</td><td class=table_head align=center>Uploaded</td><td class=table_head align=center>Downloaded</td><td class=table_head align=center>Seed Time</td><td class=table_head align=center>Ratio</td><td class=table_head align=center>Up Speed</td><td class=table_head align=center>Down Speed</td><td class=table_head align=center>Completed</td><td class=table_head align=center>Seeding</td></tr>");

while ($arr = mysql_fetch_assoc($res))
{
$arr2 = mysql_fetch_assoc($res2);
//start Global
if ($arr["downloaded"] > 0)
{
$ratio = number_format($arr["uploaded"] / $arr["downloaded"], 3);
if ($arr["downloaded"] > 0)
{
$ratio = number_format($arr["uploaded"] / $arr["downloaded"], 3);
        if ($downloaded == 0)
                $ratio = "&infin;";
        elseif ($ratio < 0.1)
                $ratio = "<font color=\"#ff0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.2)
                $ratio = "<font color=\"#ee0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.3)
                $ratio = "<font color=\"#dd0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.4)
                $ratio = "<font color=\"#cc0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.5)
                $ratio = "<font color=\"#bb0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.6)
                $ratio = "<font color=\"#aa0000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.7)
                $ratio = "<font color=\"#990000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.8)
                $ratio = "<font color=\"#880000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 0.9)
                $ratio = "<font color=\"#770000\">" . number_format($ratio, 2) . "</font>";
        elseif ($ratio < 1)
                $ratio = "<font color=\"#660000\">" . number_format($ratio, 2) . "</font>";
        else
                $ratio = "<font color=\"#00FF00\">".  number_format($ratio, 2) . "</font>";
}
}
else
if ($arr["uploaded"] > 0)
$ratio = "Inf.";
else
$ratio = "---";
$uploaded =mksize($arr["uploaded"]);
$downloaded = mksize($arr["downloaded"]);
$uid = $arr[username];
//start torrent
if ($arr2["downloaded"] > 0)
{
$ratio2 = number_format($arr2["uploaded"] / $arr2["downloaded"], 3);
        if ($downloaded == 0)
                $ratio2 = "&infin;";
        elseif ($ratio2 < 0.1)
                $ratio2 = "<font color=\"#ff0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.2)
                $ratio2 = "<font color=\"#ee0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.3)
                $ratio2 = "<font color=\"#dd0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.4)
                $ratio2 = "<font color=\"#cc0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.5)
                $ratio2 = "<font color=\"#bb0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.6)
                $ratio2 = "<font color=\"#aa0000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.7)
                $ratio2 = "<font color=\"#990000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.8)
                $ratio2 = "<font color=\"#880000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 0.9)
                $ratio2 = "<font color=\"#770000\">" . number_format($ratio2, 2) . "</font>";
        elseif ($ratio2 < 1)
                $ratio2 = "<font color=\"#660000\">" . number_format($ratio2, 2) . "</font>";
        else
                $ratio2 = "<font color=\"#00FF00\">".  number_format($ratio2, 2) . "</font>";
}
else
if ($arr2["uploaded"] > 0)
$ratio2 = "&infin;";
else
$ratio2 = "---";
$uploaded2 = mksize($arr2["uploaded"]);
$downloaded2 = mksize($arr2["downloaded"]);
//en
if ($user->id == $arr["id"])
$highlight = "bgcolor=#$btback1"; 
else 
$highlight = "bgcolor=#$btback2";

$res77 = mysql_query("SELECT  warned FROM ".$db_prefix."_users WHERE id = $arr[id]") or die(mysql_error());
$arr77 = mysql_fetch_assoc($res77);
if ($arr77["donated"] > '0') {
		$dons = "<img src=$SITEURL/images/star.gif border=0 alt='Donateur'>";
	} 
	else
	$dons ="";
	
if ($arr77["warned"] == "yes") {
		$warn = "<img src=\"images/warned.gif\" alt=\"Averti\">";
	}
	else
	$warn =""; 	
//print("<td align=center class=table_col1><a href=account-details.php?id=$arr[userid]><b>$arr[username]</b></a>$dons</td><td align=left class=table_col2>$uploaded Global<br>$uploaded2 Torrent</td><td align=left class=table_col1>$downloaded Global<br>$downloaded2 Torrent</td><td align=left class=table_col2>$ratio Global<br>$ratio2 Torrent</td><td align=center class=table_col1>$arr[completedat]</td><td align=center class=table_col2>$arr[last_action]</td><td align=center class=table_col1>" . ($arr["seeder"] == "yes" ? "<b><font color=green>Yes</font>" : "<font color=red>No</font></b>") . "</td>
//<td align=center class=table_col2><a href=$SITEURL/account-inbox.php?receiver=$arr[userid]><img src=images/button_pm.gif></a></td><td align=center class=table_col1><a href=$SITEURL/report.php?user=$arr[userid]><img src=images/button_report.gif></a></td><td align=center class=table_col2>".("'".$arr2['last_access']."'">$dt?"<img src=images/button_online.gif border=0 alt=\"Online\">":"<img src=images/button_offline.gif border=0 alt=\"Offline\">" )."</td>"."
//</tr>\n");
if ($arr["seeder"] == 'yes') {
$seed = "<b><font color=green>Yes</font>";
}
else {
$seed = "<font color=red>No</font></b>";
}
if($arr['completedat'] != '0000-00-00 00:00:00'){
$complete = "<b><font color=green>Yes</font>";
}
else {
$complete = "<font color=red>No</font></b>";
}
 
                $dispname = htmlspecialchars($arr['torrent_name']);
                $dispname = str_replace("_", " ", $dispname);
                $dispname = str_replace(".", " ", $dispname);

echo("<td $highlight class=table_col1><a href=details.php?id=$arr[torrentid]><b>".((strlen($dispname) <= 27) ? $dispname: substr($dispname,0,26)."...")."</b></a></td><td $highlight align=left class=table_col2>$uploaded2</td><td $highlight align=left class=table_col1>$downloaded2</td><td $highlight align=left class=table_col1>".get_formatted_timediff(sql_timestamp_to_unix_timestamp($arr['completedat']),sql_timestamp_to_unix_timestamp($arr['completedat'])+$arr['seeding_time'])."</td><td $highlight align=left class=table_col2>$ratio2</td><td $highlight align=center class=table_col1>".mksize($arr2[speedup])."</td><td $highlight align=center class=table_col1>".mksize($arr2[speeddown])."</td><td $highlight align=center class=table_col1>$complete</td><td $highlight align=center class=table_col1>$seed</td></tr>\n");
}

//$db->sql_freeresult($arr);
print("</table>\n");
echo "<center>Page ".$p4."</center><br />";
        echo "<table border=\"0\" width=\"100%\"><tr><td align=\"left\"><p>".$prev."</p></td><td align=\"center\"><p>".(($pages > 1) ? $pager : "")."</p></td><td align=\"right\"><p>".$next."</p></td></tr></table>";
}
CloseTable();


include("footer.php");
?>
