<?php

/*
// +--------------------------------------------------------------------------+
// | Project:    NVTracker - NetVision BitTorrent Tracker                     |
// +--------------------------------------------------------------------------+
// | This file is part of NVTracker. NVTracker is based on BTSource,          |
// | originally by RedBeard of TorrentBits, extensively modified by           |
// | Gartenzwerg.                                                             |
// |                                                                          |
// | NVTracker 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.                                      |
// |                                                                          |
// | NVTracker 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 NVTracker; if not, write to the Free Software Foundation,     |
// | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
*/

ob_start("ob_gzhandler");

require_once("include/bittorrent.php");

hit_start();

dbconn(false);

loggedinorreturn();
hit_count();

$cats = genrelist();

$searchstr = unesc($_GET["search"]);
$cleansearchstr = searchfield($searchstr);
if (empty($cleansearchstr))
    unset($cleansearchstr);

$addparam = "";
$wherea = array("`activated`='yes'");
$wherecatina = array();


if ($_GET["incldead"] == 1) {
    $addparam .= "incldead=1&amp;";
    if (!isset($CURUSER) || get_user_class < UC_CO_ADMIN)
        $wherea[] = "banned != 'yes'";
} elseif ($_GET["incldead"] == 2) {
    $addparam .= "incldead=2&amp;";
    $wherea[] = "visible = 'no'";
} else
    $wherea[] = "visible = 'yes'";
// BEGIN: Groups Edition by truenoir
$team=0+$_GET["team"];

$grpsel="";
$grpres=mysql_query("select * from teams where typ='crew'");
while($grparr=mysql_fetch_assoc($grpres))
  $grpsel.="<option value=\"$grparr[id]\"".($team==$grparr["id"]? " selected":"").">$grparr[name]</option>";
if($grpsel!="")
  $grpsel="<select name=\"team\"><option value=\"0\"".($team==0?" selected":"").">---[ ALLE ]---</option>$grpsel</select>";
else
  unset($grpsel);

if ($team>0) 
{
  $addparam .= "team=".$team."&amp;";
  $wherea[] = "team = ".sqlesc($team);
}
// END: Groups Edition by truenoir
$category = intval($_GET["cat"]);

$all = $_GET["all"];

if (!$all) {
    if (!$_GET && $CURUSER["notifs"]) {
        $all = true;
        foreach ($cats as $cat) {
            $all &= $cat[id];
            if (strpos($CURUSER["notifs"], "[cat" . $cat[id] . "]") !== false) {
                $wherecatina[] = $cat[id];
                $addparam .= "c$cat[id]=1&amp;";
            } 
        } 
    } elseif ($category) {
        if (!is_valid_id($category))
            stderr("Error", "Invalid category ID $category.");
        $wherecatina[] = $category;
        $addparam .= "cat=$category&amp;";
    } else {
        $all = true;
        foreach ($cats as $cat) {
            $all &= $_GET["c$cat[id]"];
            if ($_GET["c$cat[id]"]) {
                $wherecatina[] = $cat[id];
                $addparam .= "c$cat[id]=1&amp;";
            } 
        } 
    } 
} 

$orderby = "ORDER BY ";

if ($_GET["orderby"] != "")
    $addparam .= "orderby=" . urlencode($_GET["orderby"]) . "&amp;";

switch ($_GET["orderby"]) {
    case "name":
        $orderby .= "torrents.name";
        break;
    case "type":
        $orderby .= "categories.name";
        break;
    case "files":
        $orderby .= "torrents.numfiles";
        break;
    case "comments":
        $orderby .= "torrents.comments";
        break;
    case "added":
        $orderby .= "torrents.added";
        break;
    case "size":
        $orderby .= "torrents.size";
        break;
    case "snatched":
        $orderby .= "torrents.times_completed";
        break;
    case "seeds":
        $orderby .= "torrents.seeders";
        break;
    case "leeches":
        $orderby .= "torrents.leechers";
        break;
    case "uppedby":
        $orderby .= "users.username";
        break;
    default:
        $_GET["orderby"] = "added";
        $orderby .= "torrents.added";
        break;
} 

$orderby_sel = array("name" => "Torrent-Name",
    "type" => "Kategorie",
    "files" => "Anzahl Dateien",
    "comments" => "Anzahl Kommentare",
    "added" => "Upload-Datum",
    "size" => "Gesamtgröße",
    "snatched" => "Anzahl heruntergeladen",
    "seeds" => "Anzahl Seeder",
    "leeches" => "Anzahl Leecher",
    "uppedby" => "Uploader"
    );

switch ($_GET["sort"]) {
    case "asc":
        $orderby .= " ASC";
        $addparam .= "sort=asc&amp;";
        break;

    default:
    case "desc":
        $addparam .= "sort=desc&amp;";
        $orderby .= " DESC";
        break;
} 

if ($all) {
    $wherecatina = array();
    $addparam = "";
} 

if ($_GET["showsearch"] == 1) {
    $CURUSER["displaysearch"] = "yes";
    mysql_query("UPDATE users SET displaysearch='yes' WHERE id=".$CURUSER["id"]);
    $_SESSION["userdata"]["displaysearch"] = "yes";
} elseif (isset($_GET["showsearch"]) && $_GET["showsearch"] == 0) {
    $CURUSER["displaysearch"] = "no";
    mysql_query("UPDATE users SET displaysearch='no' WHERE id=".$CURUSER["id"]);
    $_SESSION["userdata"]["displaysearch"] = "no";
}

if (count($wherecatina) > 1)
    $wherecatin = implode(",", $wherecatina);
elseif (count($wherecatina) == 1)
    $wherea[] = "category = $wherecatina[0]";

if ($CURUSER["hideuseruploads"]=="yes")
    $wherea[] = "users.class >= ".UC_UPLOADER;

$wherebase = $wherea;

if (isset($cleansearchstr)) {
    $wherea[] = "MATCH (search_text, ori_descr) AGAINST (" . sqlesc($searchstr) . ")"; 
    // $wherea[] = "0";
    $addparam .= "search=" . urlencode($searchstr) . "&amp;"; 
    // $orderby = "";
} 

$where = implode(" AND ", $wherea);
if ($wherecatin)
    $where .= ($where ? " AND " : "") . "category IN(" . $wherecatin . ")";

if ($where != "")
$where = "WHERE $where";

$emp = "";
global $CURUSER;
if ($CURUSER["xxx"] == 'no' && $where !="")
{
$emp = "AND category != '27'";
$where = "$where AND category != '27'";
}
if ($CURUSER["xxx"] == 'no' && $where =="")
{
$emp = "AND category != '27'";
$where = "WHERE category != '27'";
}


$res = mysql_query("SELECT COUNT(*) FROM torrents $where") or die(mysql_error());
$row = mysql_fetch_array($res);
$count = $row[0];

if (!$count && isset($cleansearchstr)) {
    $wherea = $wherebase;
    $orderby = "ORDER BY added DESC";
    $searcha = explode(" ", $cleansearchstr);
    $sc = 0;
    foreach ($searcha as $searchss) {
        if (strlen($searchss) <= 1)
            continue;
        $sc++;
        if ($sc > 5)
            break;
        $ssa = array();
        foreach (array("search_text", "ori_descr") as $sss)
        $ssa[] = "$sss LIKE '%" . sqlwildcardesc($searchss) . "%'";
        $wherea[] = "(" . implode(" OR ", $ssa) . ")";
    } 
    if ($sc) {
        $where = implode(" AND ", $wherea);
        if ($where != "")
            $where = "WHERE $where";
        $res = mysql_query("SELECT COUNT(*) FROM torrents LEFT JOIN users ON torrents.owner=users.id $where");
        $row = mysql_fetch_array($res);
        $count = $row[0];
    } 
} 

$torrentsperpage = $CURUSER["torrentsperpage"];
if (!$torrentsperpage)
    $torrentsperpage = 15;

if ($count) {
    list($pagertop, $pagerbottom, $limit) = pager($torrentsperpage, $count, "browse.php?" . $addparam);
    $query = "SELECT torrents.team,torrents.nuked,torrents.descr,torrents.freeleech, torrents.free,torrents.multi,torrents.poster,torrents.numpics,torrents.poster,torrents.id, torrents.category, torrents.leechers, torrents.seeders, torrents.name, torrents.times_completed, torrents.size, torrents.added, torrents.last_action, torrents.comments,torrents.numfiles,torrents.filename,torrents.owner,IF(torrents.nfo <> '', 1, 0) as nfoav," . 
    // "IF(torrents.numratings < ".$GLOBALS["MINVOTES"].", NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating, categories.name AS cat_name, categories.image AS cat_pic, users.username FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id $where $orderby $limit";
    "categories.name AS cat_name, categories.image AS cat_pic, users.username, users.anon, users.class AS uploaderclass FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id $where $orderby $limit";
    $res = mysql_query($query) or die(mysql_error());
} else
    unset($res);
if (isset($cleansearchstr))
    stdhead("Suchergebnisse für \"$searchstr\"");
else
    stdhead();
	

?>
<form method="get" action="browse.php">
<input type="hidden" name="showsearch" value="1">
<table cellpadding="4" cellspacing="1" border="0" style="width:100%" class="tableinborder">
 <tr>

  <td class="tabletitle" colspan="10" style="width:100%;text-align:center;"> 
<?php if ($CURUSER["displaysearch"] == "yes") {

    ?>
<a href="browse.php?<?=$addparam?>showsearch=0<?php if ($_GET["page"] > 0) echo "&amp;page=" . intval($_GET["page"]);

    ?>"><img src="<?=$GLOBALS["PIC_BASE_URL"].$GLOBALS["ss_uri"]?>/minus.gif" alt="+" title="Zuklappen" border="0"> <b>Kategorien / Suchen</b></a></td> 
 </tr><tr><td width="100%" style="width:100%;" class="tablea"><center>
 <table border="0" cellspacing="1" cellpadding="3" class="tableinborder">
  <tr>
    <td colspan="<?=$GLOBALS["BROWSE_CATS_PER_ROW"]?>" class="tablecat" align="center"><b>Kategorien</b></td>
  </tr>
  <tr>
<?php
    $i = 0;
    foreach ($cats as $cat) {
        print(($i && $i % $GLOBALS["BROWSE_CATS_PER_ROW"] == 0) ? "</tr><tr>" : "");
        print("<td class=\"tablea\" style=\"padding-bottom: 2px;padding-left: 7px\" nowrap><input name=\"c$cat[id]\" type=\"checkbox\" " . (in_array($cat[id], $wherecatina) ? "checked " : "") . "value=\"1\"><a class=\"catlink\" href=\"browse.php?cat=$cat[id]&amp;showsearch=1\">" . htmlspecialchars($cat[name]) . "</a></td>\n");
        $i++;
    } 

    $alllink = "<a href=\"browse.php?all=1&amp;showsearch=1\"><b>Alle anzeigen</b></a>";

    $ncats = count($cats);
    $nrows = ceil($ncats / $GLOBALS["BROWSE_CATS_PER_ROW"]);
    $lastrowcols = $ncats % $GLOBALS["BROWSE_CATS_PER_ROW"];

    if ($lastrowcols != 0) {
        if ($GLOBALS["BROWSE_CATS_PER_ROW"] - $lastrowcols != 1) {
            print("<td class=\"tablea\" rowspan=\"" . ($GLOBALS["BROWSE_CATS_PER_ROW"] - $lastrowcols - 1) . "\">&nbsp;</td>");
        }
        print("<td class=\"tablea\" style=\"padding-left: 5px\">$alllink</td>\n");
    } 

    if ($ncats % $GLOBALS["BROWSE_CATS_PER_ROW"] == 0)
        print("<tr><td class=\"tablea\" colspan=\"" . $GLOBALS["BROWSE_CATS_PER_ROW"] . "\" style=\"text-align:center;vertical-align:middle\">$alllink</td>\n");

    ?>
  </tr>
<tr><td class=tablea><img src="pic/multi2.gif"><img src="pic/multi5.gif"><img src="pic/multi10.gif"></td><td class="lista" colspan="4" align="left">&nbsp;&nbsp;Bei <font color="orange">Multiplikator Torrents</font> wird der Upload mit dem angegebenem Wert multipliziert.</td></tr>
  <tr>
    <td colspan="<?=$GLOBALS["BROWSE_CATS_PER_ROW"]?>" class="tablea" nowrap="nowrap">
      Suchen:&nbsp;<input type="text" id="searchinput" name="search" autocomplete="off" style="width: 240px;" ondblclick="suggest(event.keyCode,this.value);" onkeyup="suggest(event.keyCode,this.value);" onkeypress="return noenter(event.keyCode);" value="<?= htmlspecialchars($searchstr) ?>" />
       in <select name="incldead">
<option value="0">aktiven</option>
<option value="1"<?php print($_GET["incldead"] == 1 ? " selected" : "");

    ?>>allen</option>
<option value="2"<?php print($_GET["incldead"] == 2 ? " selected" : "");

    ?>>toten</option>
		</select> Torrents
<script language="JavaScript" src="suggest.js" type="text/javascript"></script>
<div id="suggcontainer" style="text-align: left; width: 520px; display: none;">
<div id="suggestions" style="cursor: default; position: absolute; background-color: #FFFFFF; border: 1px solid #777777;"></div>
</div>
     </td>
  </tr>
<?php
// BEGIN: Groups Edition by truenoir
if($grpsel) {
?>

  <tr>
    <td colspan="<?=$GLOBALS["BROWSE_CATS_PER_ROW"]?>" class="tablea" nowrap="nowrap">
      Group:<?=$grpsel?>&nbsp; Torrents
     </td>
  </tr>
<?php
}
// END: Groups Edition by truenoir 
?>
  <tr>
    <td colspan="<?=$GLOBALS["BROWSE_CATS_PER_ROW"]?>" class="tablea" nowrap="nowrap">Sortieren nach <select name="orderby" size="1">
    <?php
    foreach ($orderby_sel as $orderparam => $description) {
        echo "<option value=\"$orderparam\"";
        if ($orderparam == $_GET["orderby"])
            echo " selected=\"selected\"";
        echo ">$description</option>\n";
    } 

    ?>
    </select> in <select name="sort" size="1">
      <option value="desc"<?php if ($_GET["sort"] == "desc") echo " selected=\"selected\"";

    ?>>absteigender</option>
      <option value="asc"<?php if ($_GET["sort"] == "asc") echo " selected=\"selected\"";

    ?>>aufsteigender</option>
      </select> Reihenfolge</td>
  </tr>
  <tr>
     <td colspan="<?=$GLOBALS["BROWSE_CATS_PER_ROW"]?>" class="tablea" nowrap="nowrap" style="text-align:center;"><input type="submit" value="Suchen / Aktualisieren"/></td>  	
  </tr>
  </table></center>
<?php } else {

    ?>
<?php } 

?>
</td>
</tr>
</table>
</form>
<?
//Letzen 10 empfohlenen torrents mit Poster/a//


 begin_frame("Die 8 neusten empfohlenen Torrents","true","100%");
 $sql = "SELECT * FROM torrents where visible='yes' AND recommended='yes' ORDER BY added DESC LIMIT 8";
 $result = mysql_query($sql) or die('No torrents found');
 if( mysql_num_rows($result) != 0 )
 {
 print '<center><table border=0 cellspacing=0 cellpadding=5><tr>';

 while( $row = mysql_fetch_assoc($result) )
 {


 print("<td>");
print("<a href=\"details.php?id=".$row['id']."&hit=1\"><img src=".$row['poster']."
width=\"70\" height=\"100\" title=\"".$row['name']."\" border=\"0\"></a>");
print("</td>");
 }
 print '</tr></table>';

 }
 else
 {
 print 'Keine Torrents Vorhanden';
 }
 end_frame();

 //Letzen 10 empfohlenen Torrents mit Poster Ende/E//
?>
<?php

if (isset($cleansearchstr)) {

    ?>
<br>
<table cellpadding="4" cellspacing="1" border="0" style="width:85%" class="tableinborder">
 <tr class="tabletitle" width="85%">
  <td colspan="10" width="85%" style="text-align:center;"><b>
  Suchergebnisse zu "<?=htmlspecialchars($searchstr)?>" </b></td></tr></table>
<?php
} 

if ($count) {
    print($pagertop);

    torrenttable($res, "index", $addparam);

    print($pagerbottom);
} else {
    if (isset($cleansearchstr)) {

        ?>
<br>
<table cellpadding="4" cellspacing="1" border="0" style="width:85%" class="tableinborder">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%" style="text-align:center;"><b>Nichts gefunden!</b></td> 
 </tr><tr><td width="100%" class="tablea">Es existieren keine Torrents, die Deinen Suchkriterien entsprechen! Bitte versuche es noch einmal mit einem anderen Suchbegriff.</td></tr></table>
<?php
    } else {

        ?>
<br>
<table cellpadding="4" cellspacing="1" border="0" style="width:85%" class="tableinborder">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%" style="text-align:center;"><b>Keine Torrents vorhanden</b></td> 
 </tr><tr><td width="100%" class="tablea">Es existieren keine Torrents, die Deinen Kriterien entsprechen!</td></tr></table>
     <?php
    } 
} 

stdfoot();

hit_end();

?>
