<?
require "include/bittorrent.php";

dbconn();

loggedinorreturn();

$search = trim($HTTP_GET_VARS['search']);

if ($search != '' || $class)
{
  $query = "name LIKE " . sqlesc("%$search%") . "";
    if ($search)
          $q = "search=" . htmlspecialchars($search);
}
else
{
    $letter = trim($_GET["letter"]);
  if (strlen($letter) > 1)
    die;

  if ($letter == "" || strpos("abcdefghijklmnopqrstuvwxyz", $letter) === false)
    $letter = "a";
  $query = "name LIKE '$letter%'";
  $q = "letter=$letter";
}

   // xxx files ausblenden// cat 27 anpassen
   if(isset($query) && $query && $CURUSER['xxx'] != 'yes'){
      $query .= " AND category != 27 ";
   }

stdhead("Katalog");



print("<form method=get action=?>\n");
print("<font size=3><br><br><b>Torrent-Katalog</b></font><br><br>");
print ("<br><table cellpadding=4 cellspacing=1 border=0 style=width:90% class=tableinborder>");
print ("<tr class=tabletitle width=90%>");
print ("<td colspan=10 width=90%><span class=normalfont><center><b>Suche</b></center></span></td></tr>");
print("<td class=tablea><center>Suchen: <input type=text size=30 name=search>\n");
print("<input type=submit value='Okay'>\n");
print("</form>\n");
print("<p>\n");

for ($i = 97; $i < 123; ++$i)
{
    $l = chr($i);
    $L = chr($i - 32);
    if ($l == $letter)
    print("<b>$L</b>\n");
    else
    print("<a href=?letter=$l><b>$L</b></a>\n");
}



print("</p>\n");


$page = $_GET['page'];
$perpage = 5;

$res = mysql_query("SELECT COUNT(*) FROM torrents WHERE $query") or sqlerr();
$arr = mysql_fetch_row($res);
$pages = floor($arr[0] / $perpage);
if ($pages * $perpage < $arr[0])
  ++$pages;

if ($page < 1)
  $page = 1;
else
  if ($page > $pages)
    $page = $pages;

for ($i = 1; $i <= $pages; ++$i)
  if ($i == $page)
    $pagemenu .= "<b>$i</b>\n";
  else
    $pagemenu .= "<a href=?$q&page=$i><b>$i</b></a>\n";

if ($page == 1)
  $menu .= "<b>&lt;&lt; Zurück</b>";
else
  $menu .= "<a href=?$q&page=" . ($page - 1) . "><b>&lt;&lt; Zurück</b></a>";

$menu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if ($page == $pages)
  $menu .= "<b>Weiter &gt;&gt;</b>";
else
  $menu .= "<a href=?$q&page=" . ($page + 1) . "><b>Weiter &gt;&gt;</b></a>";

print("<p>$menu<br>$pagemenu</p>");

$offset = ($page * $perpage) - $perpage;

$res = mysql_query("SELECT * FROM torrents WHERE $query ORDER BY name ASC LIMIT $offset,$perpage") or sqlerr();
$num = mysql_num_rows($res);

print("</td><br></center></table>");

if (!$num){
print("<table width=800 cellpadding=5><tr><td class=colhead><b>Torrent - Katalog</b></td></tr>".
"<tr><td>Nothing Here With Letter $letter</td></tr>".
"<tr><td align=right><font class=small>Katalog by CoFfInHuNtEr</font></td></tr>".
"</table>");
} else {






for ($i = 0; $i < $num; ++$i)
{
  $arr = mysql_fetch_assoc($res);
  {
  $id = $arr["id"];


  $ret = mysql_query("SELECT seeder, finishedat, downloadoffset, uploadoffset, ip, port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st, UNIX_TIMESTAMP(last_action) AS la, userid FROM peers WHERE torrent = $id AND seeder = 'yes' ORDER BY to_go ASC LIMIT 5");
  $nul = mysql_num_rows($ret);

  if (!$nul) {
  $s .= "<table width=100% cellspacing=1 cellpadding=4 class=tableinborder>";
  $s .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tableb class=tabla>keine Peers.</td>";
  $s .= "</table>";

   } else {
  $s = "<table width=100% cellspacing=1 cellpadding=4 class=tableinborder>";
  $s .= "<tr class=tabletitle><td>Username</td><td align=right>Uploaded</td><td align=right>Rate</td><td align=right>Downloaded</td><td align=right>Rate</td>";
  $s .= "<td align=right>Ratio</td><td align=right>Completed</td></tr>";

  for ($m = 0; $m < $nul; ++$m)
  {
  $arrs = mysql_fetch_assoc($ret);
  {
  $cres = mysql_query("SELECT username, anon FROM users WHERE id=$arrs[userid]");
  $cros = mysql_fetch_assoc($cres);
  $username = $cros[username];
  $now = time();
  $secs = max(1, ($now - $arrs["st"]) - ($now - $arrs["la"]));
  $revived = $arrs["revived"] == "yes";

  if ($cros["anon"] == "no"||get_user_class()>=UC_MODERATOR)
$s .= "<tr class=tableb><td class=outer><a href=userdetails.php?id=$arrs[userid]><b>$username</b></a></td>";


  else
  $s .= "<tr class=tableb><td class=outer><b>Anonym*</b></td>";

  $s .= "<td align=right class=outer><nobr>" . mksize($arrs[uploaded]) . "</nobr></td>";
  $s .= "<td align=right class=outer><nobr>" . mksize(($arrs["uploaded"] - $arrs["uploadoffset"]) / $secs) . "/s</nobr></td>";
  $s .= "<td align=right class=outer><nobr>" . mksize($arrs[downloaded]) . "</nobr></td>";

  if ($arrs["seeder"] == "no")
  $s .= "<td align=right class=outer><nobr>" . mksize(($arrs["downloaded"] - $arrs["downloadoffset"]) / $secs) . "/s</nobr></td>";
  else
  $s .= "<td align=right class=outer><nobr>" . mksize(($arrs["downloaded"] - $arrs["downloadoffset"]) / max(1, $arrs["finishedat"] - $arrs[st])) .    "/s</nobr></td>";

  if ($arrs["downloaded"]) {
  $ratio = floor(($arrs["uploaded"] / $arrs["downloaded"]) * 1000) / 1000;
  $s .= "<td align=right class=outer><font color=" . get_ratio_color($ratio) . ">" . number_format($ratio, 3) . "</font></td>";
  }
  else
  if ($arrs["uploaded"])
  $s .= "<td align=right class=outer>Inf.</td>";
  else
  $s .= "<td align=right class=outer>---</td>";
  $s .= "<td align=right class=outer>" . sprintf("%.2f%%", 100 * (1 - ($arrs["to_go"] / $arr["size"]))) . "</td>";
  }
  }
  $s .= "</table>";
  }

  $bah = mysql_query("SELECT COUNT(*) FROM peers WHERE seeder = 'yes' AND torrent = $id");
  $count = mysql_fetch_row($bah);
  $seeders = $count[0];
  $bah1 = mysql_query("SELECT COUNT(*) FROM peers WHERE seeder = 'no' AND torrent = $id");
  $count1 = mysql_fetch_row($bah1);
  $leechers = $count1[0];






print ("<br><table cellpadding=4 cellspacing=1 border=0 style=width:90% class=tableinborder>");
print ("<tr class=tabletitle width=90%>");
print ("<td colspan=10 width=90%><span class=normalfont><center><b><a href=details.php?id=$id&hit=1>" . htmlspecialchars($arr[name]) . "</a></b></center></span></td>");


print ("</tr>");

if ($GLOBALS["DOWNLOAD_METHOD"] == DOWNLOAD_REWRITE)
$download_url = "download/" . $id . "/" . rawurlencode($row["filename"]);
else
$download_url = "download.php?torrent=" . $id;

$x = $arr["name"];

$info = "<td class=tablea valign=top><table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea class=tablea> <a href=$download_url><img src=pic/DL1.gif></a></td></tr></table><br>";

$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=16%><b> Beschreibung: </b></td><td class=tablea align=left width=99%><a href=\"javascript: klappe_nfo('$id".$array['id']."')\"><img border=\"0\" src=\"pic/zeig.png\" id=\"picb".$array['id']."\" alt=\"Show/Hide\">" ."</a><div id=\"k$id".$array['id']."\" style=\"display: none;\"><br>".format_urls(format_comment(htmlspecialchars($arr["descr"])))."</div></td></tr></table><br>";

$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=16%><b> NFO:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></td><td class=tablea align=left width=99%><a href=\"javascript: klappe_nfo('$x".$array['id']."')\"><img border=\"0\" src=\"pic/zeig.png\" id=\"picb".$array['id']."\" alt=\"Show/Hide\">" ."</a><div id=\"k$x".$array['id']."\" style=\"display: none;\"><br><br><img src=/bitbucket/nfo-$id.png border=0><br><br><a href=\"viewnfo.php?id=$id&amp;dl=1\"><img src=/pic/DL1.gif border=0></a></div></td></tr></table><br>";

//////////////WEBSEED////////////
$arr1 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS cnt FROM users LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND webseed='yes'"));
$seeds1 = $arr1["cnt"];
$webseed = get_row_count("users", "LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND webseed='yes'");

if ($torrent_info["webseed"] = $seeds1)
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Webseed:</b></td><td class=tablea align=left width=99%><font color=green>Ja | $webseed</font></td></tr></table><br>";
}
else
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Webseed:</b></td><td class=tablea align=left width=99%><font color=red>Nein</font></td></tr></table><br>";
}
//////////////WEBSEED////////////

//////ADSL ANZEIGE//////////////
$arr1 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS cnt FROM users LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND adsl='yes'"));
$seeds2 = $arr1["cnt"];
$adsl = get_row_count("users", "LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND adsl='yes'");

if ($torrent_info["adsl"] = $seeds2)
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>ADSL:</b></td><td class=tablea align=left width=99%><font color=green>Ja | $adsl</font></td></tr></table><br>";
}
else
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>ADSL:</b></td><td class=tablea align=left width=99%><font color=red>Nein</font></td></tr></table><br>";
}
//////ADSL ANZEIGE//////////////

//////VDSL ANZEIGE//////////////
$arr1 = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS cnt FROM users LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND vdsl='yes'"));
$seeds3 = $arr1["cnt"];
$vdsl = get_row_count("users", "LEFT JOIN peers ON peers.userid = users.id WHERE torrent=$id AND seeder='yes' AND vdsl='yes'");

if ($torrent_info["vdsl"] = $seeds3)
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>VDSL:</b></td><td class=tablea align=left width=99%><font color=green>Ja | $vdsl</font></td></tr></table><br>";
}
else
{
$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>VDSL:</b></td><td class=tablea align=left width=99%><font color=red>Nein</font></td></tr></table><br>";
}
//////VDSL ANZEIGE//////////////

$free1 = mysql_query("SELECT * FROM torrents WHERE free = 'yes'") or sqlerr();
if  ($row[free]=="yes") {
$info .="<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Only Up:</b></td><td class=tablea align=left width=99%><font color=green>Ja</font></td></tr></table><br>";
            }
else
{
$info .="<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Only Up:</b></td><td class=tablea align=left width=99%><font color=red>Nein</font></td></tr></table><br>";
            }

$highlight1 = mysql_query("SELECT * FROM torrents WHERE highlight = 'yes'") or sqlerr();
if  ($row[highlight]=="yes") {
$info .="<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Highlight:</b></td><td class=tablea align=left width=99%><font color=green>Ja</font></td></tr></table><br>";
            }
else
{
$info .="<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tablea width=12%><b>Highlight:</b></td><td class=tablea align=left width=99%><font color=red>Nein</font></td></tr></table><br>";
            }



$info .= "<table width=100% cellpadding=4 cellspacing=1 class=tableinborder><tr><td class=tableb colspan=2>Seeder Info (Top 5 Seeders)</td><td align=right class=tableb width=175>$seeders Seeder(s) | $leechers Leecher(s)</td></tr></table><br>$s</td>";

}
$user = mysql_fetch_assoc(mysql_query("SELECT id, username, anon FROM users WHERE id = $arr[owner]")) or sqlerr();

if ($user["anon"] == "no"||get_user_class()>=UC_MODERATOR) {
$owner = "<tr class=tablea><center>Hochgeladen von: <a href=userdetails.php?id=$user[id]><font color=#ff0000> $user[username]</font></a><br></center>";
}else{
$owner = "<tr class=tablea><center>Hochgeladen von: <font color=#ff0000>Anonym*</font><br></center>";
}

if ($arr["poster"] == '')
$poster = "<tr class=tablea><center><img src=pic/poster.jpg></center>";
else
$poster = "<center><img src=$arr[poster] width=150></center>";
print("<tr><td  class=tablea valign=top width=150><table cellpadding=3><tr><td class=colhead align=left>$owner<br>$poster<br><tr class=tablea><center>Fertiggestellt: $arr[times_completed] mal</center></td></tr></table></td>$info</tr>");
}

print("</td></tr><br></table>");
print("<tr><td colspan=2 align=center><font size=0 color=grey>Katalog © by Coffinhunter modifiziert by gotthummer</font></td></tr>");

}
stdfoot();
?>