<?
require "include/header.php";

dbconn(false);

loggedinorreturn();

function bark($msg)
{
  stdhead();
  stdmsg("Error", $msg);
  stdfoot();
  exit;
}

$id = 0 + $_GET["id"];

if (!is_valid_id($id))
  bark("Bad ID $id.");

$r = @mysql_query("SELECT * FROM users WHERE id=$id") or sqlerr();
$user = mysql_fetch_array($r) or bark("No user with ID $id.");
if ($user["status"] == "pending") die;

if ($user["ip"] && (get_user_class() >= UC_MODERATOR || $user["id"] == $CURUSER["id"]))
{
  $ip = $user["ip"];
  $dom = @gethostbyaddr($user["ip"]);
  if ($dom == $user["ip"] || @gethostbyname($dom) != $user["ip"])
    $addr = $ip;
  else
  {
    $dom = strtoupper($dom);
    $domparts = explode(".", $dom);
    $domain = $domparts[count($domparts) - 2];
    if ($domain == "COM" || $domain == "CO" || $domain == "NET" || $domain == "NE" || $domain == "ORG" || $domain == "OR" )
      $l = 2;
    else
      $l = 1;
    $addr = "$ip ($dom)";
  }
}
if ($user[added] == "0000-00-00 00:00:00")
  $joindate = 'N/A';
else
  $joindate = "$user[added] (" . get_elapsed_time(sql_timestamp_to_unix_timestamp($user["added"])) . " ago)";
$lastseen = $user["last_access"];
if ($lastseen == "0000-00-00 00:00:00")
  $lastseen = "never";
else
{
  $lastseen .= " (" . get_elapsed_time(sql_timestamp_to_unix_timestamp($lastseen)) . " ago)";
}
  $res = mysql_query("SELECT COUNT(*) FROM posts WHERE userid=" . $user[id]) or sqlerr();
  $arr3 = mysql_fetch_row($res);
  $forumposts = $arr3[0];

//if ($user['donated'] > 0)
//  $don = "<img src=pic/starbig.gif>";

$res = mysql_query("SELECT name,flagpic FROM countries WHERE id=$user[country] LIMIT 1") or sqlerr();
if (mysql_num_rows($res) == 1)
{
  $arr = mysql_fetch_assoc($res);
  $country = "<img src=pic/flag/$arr[flagpic] alt=\"$arr[name]\" width=19 height=12 style='margin-left: 8pt'>";
}

//if ($user["donor"] == "yes") $donor = "<td class=embedded><img src=pic/starbig.gif alt='Donor' style='margin-left: 4pt'></td>";
//if ($user["warned"] == "yes") $warned = "<td class=embedded><img src=pic/warnedbig.gif alt='Warned' style='margin-left: 4pt'></td>";

/*$res = mysql_query("SELECT torrent,added,uploaded,downloaded,torrents.name as torrentname,categories.name as catname,size,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id WHERE userid=$id AND seeder='no'") or sqlerr();
if (mysql_num_rows($res) > 0)
  $leeching = maketable($res);
$res = mysql_query("SELECT torrent,added,uploaded,downloaded,torrents.name as torrentname,categories.name as catname,size,image,category,seeders,leechers FROM peers LEFT JOIN torrents ON peers.torrent = torrents.id LEFT JOIN categories ON torrents.category = categories.id WHERE userid=$id AND seeder='yes'") or sqlerr();
if (mysql_num_rows($res) > 0)
  $seeding = maketable($res);
*/
stdhead("Details for " . $user["username"]);
$enabled = $user["enabled"] == 'yes';


if (!$enabled)
  print("<p><b>This account has been disabled</b></p>\n");
elseif ($CURUSER["id"] <> $user["id"])
{
  $r = mysql_query("SELECT id FROM friends WHERE userid=$CURUSER[id] AND friendid=$id") or sqlerr(__FILE__, __LINE__);
  $friend = mysql_num_rows($r);
  $r = mysql_query("SELECT id FROM blocks WHERE userid=$CURUSER[id] AND blockid=$id") or sqlerr(__FILE__, __LINE__);
  $block = mysql_num_rows($r);

  if ($friend)
    print("<center>(<a href=friends.php?action=delete&type=friend&targetid=$id>remove from friends</a>)</center>\n");
  elseif($block)
    print("<center>(<a href=friends.php?action=delete&type=block&targetid=$id>remove from blocks</a>)</center>\n");
  else
  {
    print("<center>(<a href=friends.php?action=add&type=friend&targetid=$id>add to friends</a>)");
    print(" - (<a href=friends.php?action=add&type=block&targetid=$id>add to blocks</a>)</center>\n");
  }
}
$gheader = "$user[username]" . get_user_icons($user, true) . "$country";
begin_frame2("$gheader");
?>
<tr><td class=thumbnails width=1%>Join&nbsp;date</td><td class=thumbnails align=left width=99%><?=$joindate?></td></tr>
<tr><td class=thumbnails>Last&nbsp;seen</td><td class=thumbnails align=left><?=$lastseen?></td></tr>
<?
if ($user["email"])
  print("<tr><td class=thumbnails>Email</td><td class=thumbnails align=left><a href=mailto:$user[email]>$user[email]</a></td></tr>\n");
if ($addr)
  print("<tr><td class=thumbnails>Address</td><td class=thumbnails align=left>$addr</td></tr>\n");

if ($user["avatar"])
	print("<tr><td class=thumbnails>Avatar</td><td class=thumbnails align=left><img src=\"" . htmlspecialchars($user["avatar"]) . "\"></td></tr>\n");
print("<tr><td class=thumbnails>Class</td><td class=thumbnails align=left>" . get_user_class_name($user["class"]) . "</td></tr>\n");
if ($forum_on == "1"){
print("<tr><td class=thumbnails>Forum&nbsp;posts</td>");
if ($forumposts && (($user["class"] >= UC_POWER_USER && $user["id"] == $CURUSER["id"]) || get_user_class() >= UC_MODERATOR))
	print("<td class=thumbnails align=left><a href=userhistory.php?action=viewposts&id=$id>$forumposts</a></td></tr>\n");
else
	print("<td class=thumbnails align=left>$forumposts</td></tr>\n");
}
if ($user["info"])
 print("<tr valign=top><td align=left colspan=2 class=tableh1>" . format_comment($user["info"]) . "</td></tr>\n");

if ($CURUSER["id"] != $user["id"])
	if (get_user_class() >= UC_MODERATOR)
  	$showpmbutton = 1;
	elseif ($user["acceptpms"] == "yes")
	{
		$r = mysql_query("SELECT id FROM blocks WHERE userid=$user[id] AND blockid=$CURUSER[id]") or sqlerr(__FILE__,__LINE__);
		$showpmbutton = (mysql_num_rows($r) == 1 ? 0 : 1);
	}
	elseif ($user["acceptpms"] == "friends")
	{
		$r = mysql_query("SELECT id FROM friends WHERE userid=$user[id] AND friendid=$CURUSER[id]") or sqlerr(__FILE__,__LINE__);
		$showpmbutton = (mysql_num_rows($r) == 1 ? 1 : 0);
	}
if ($showpmbutton)
	print("<tr><td colspan=2 align=center><form method=get action=sendmessage.php><input type=hidden name=receiver value=" .
		$user["id"] . "><input type=submit value=\"Send message\" style='height: 23px'></form>");
end_frame();

if (get_user_class() >= UC_MODERATOR && $user["class"] < get_user_class())
{
  begin_frame2("Edit User");
  print("<form method=post action=modtask.php>");
  print("<input type=hidden name='action' value='edituser'>");
  print("<input type=hidden name='userid' value='$id'>");
  print("<input type=hidden name='returnto' value='userdetails.php?id=$id'>");
  print("<tr><td class=thumbnails>Title</td><td class=thumbnails colspan=2 align=left><input type=text size=60 name=title value=\"" . htmlspecialchars($user[title]) . "\"></tr>\n");
	$avatar = htmlspecialchars($user["avatar"]);
  print("<tr><td class=thumbnails>Avatar&nbsp;URL</td><td class=thumbnails colspan=2 align=left><input type=text size=60 name=avatar value=\"$avatar\"></tr>\n");
	// we do not want mods to be able to change user classes or amount donated...
	if ($CURUSER["class"] < UC_ADMINISTRATOR)
	  print("<input type=hidden name=donor value=$user[donor]>\n");
	else
	{
	  print("<tr><td class=thumbnails>Donor</td><td class=thumbnails colspan=2 align=left><input type=radio name=donor value=yes" .($user["donor"] == "yes" ? " checked" : "").">Yes <input type=radio name=donor value=no" .($user["donor"] == "no" ? " checked" : "").">No</td></tr>\n");
	}

	if (get_user_class() == UC_MODERATOR && $user["class"] > UC_VIP)
	  print("<input type=hidden name=class value=$user[class]>\n");
	else
	{
	  print("<tr><td class=thumbnails>Class</td><td class=thumbnails colspan=2 align=left><select name=class>\n");
	  if (get_user_class() == UC_MODERATOR)
	    $maxclass = UC_VIP;
	  else
	    $maxclass = get_user_class() - 1;
	  for ($i = 0; $i <= $maxclass; ++$i)
	    print("<option value=$i" . ($user["class"] == $i ? " selected" : "") . ">$prefix" . get_user_class_name($i) . "\n");
	  print("</select></td></tr>\n");
	}

	$modcomment = htmlspecialchars($user["modcomment"]);
	print("<tr><td class=thumbnails>Comment</td><td class=thumbnails colspan=2 align=left><textarea cols=60 rows=6 name=modcomment>$modcomment</textarea></td></tr>\n");
	$warned = $user["warned"] == "yes";

 	print("<tr><td class=thumbnails" . (!$warned ? " rowspan=2": "") . ">Warned</td>
 	<td class=thumbnails align=left width=20%>" .
  ( $warned
  ? "<input name=warned value='yes' type=radio checked>Yes<input name=warned value='no' type=radio>No"
 	: "No" ) ."</td>");

	if ($warned)
	{
		$warneduntil = $user['warneduntil'];
		if ($warneduntil == '0000-00-00 00:00:00')
    	print("<td class=thumbnails align=center>(arbitrary duration)</td></tr>\n");
		else
		{
    	print("<td class=thumbnails align=center>Until $warneduntil");
	    print(" (" . mkprettytime(strtotime($warneduntil) - gmtime()) . " to go)</td></tr>\n");
 	  }
  }
  else
  {
    print("<td class=thumbnails>Warn for <select name=warnlength>\n");
    print("<option value=0>------</option>\n");
    print("<option value=1>1 week</option>\n");
    print("<option value=2>2 weeks</option>\n");
    print("<option value=4>4 weeks</option>\n");
    print("<option value=8>8 weeks</option>\n");
    print("<option value=255>Unlimited</option>\n");
    print("</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PM comment:</td></tr>\n");
    print("<tr><td class=thumbnails colspan=2 align=left><input type=text size=60 name=warnpm></td></tr>");
  }
  print("<tr><td class=thumbnails>Enabled</td><td class=thumbnails colspan=2 align=left><input name=enabled value='yes' type=radio" . ($enabled ? " checked" : "") . ">Yes <input name=enabled value='no' type=radio" . (!$enabled ? " checked" : "") . ">No</td></tr>\n");
  $supportfor = htmlspecialchars($user["supportfor"]);
  print("<tr><td class=thumbnails>Support</td><td class=thumbnails colspan=2 align=left><input type=radio name=support value=yes" .($user["support"] == "yes" ? " checked" : "").">Yes <input type=radio name=support value=no" .($user["support"] == "no" ? " checked" : "").">No</td></tr>\n");
  print("<tr><td class=thumbnails>Support for:</td><td class=thumbnails colspan=2 align=left><textarea cols=60 rows=6 name=supportfor>$supportfor</textarea></td></tr>\n");

  print("<tr><td colspan=3 align=center><input type=submit class=btn value='Okay'><form method=post action=ratio.php>\n");
  print("\n");
end_frame();
  print("</form>\n");
  end_frame();
}

stdfoot();

?>