<?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   ---------------------*
*/

include("header.php");
require('actb.php');
OpenTable(pic("icon_mini_search.gif")._btsearch, '728');
echo "<form method=\"get\" action=\"search.php\">";
echo "<br>\n";
echo "<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">";
echo "<tr><td></td><td><input id=\"actb_textbox\" type=\"text\" name=\"search\" value=\"".searchfield($search)."\" size=\"25\"> ";
echo "<td>";
echo <<<EOF
<SCRIPT type="text/javascript" language="JavaScript">
actb(document.getElementById("actb_textbox"),customarray);
</SCRIPT>
EOF;
echo " <select name=\"cat\">";
echo "<option value=\"0\">"._btalltypes."</option>";
foreach (catlist() as $category) {
        echo "<option value=\"".$category["id"]."\" ";
        if ($cat == $category["id"]) echo "selected";
        echo " >".$category["name"]."</option>";
}
echo "</select>";
echo "</td>";

echo "<td><input type=\"submit\" value=\""._btgo."\"";

echo "</td></tr></table>";

echo "<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\"><tr><td><p>"._btresfor."</p></td><td> <select name=\"orderby\">\n";
$orders = Array(_btinsdate,_btseeders,_btleechers,_bttotsorc,_btdownloaded,_btvote,_btname,_btdim,_btnfile,);
$i = 0;
foreach ($orders as $order) {
        echo "<option value=\"".$i."\" ";
        if ($orderby == $i) echo "selected";
        echo ">".$order."</option>\n";
        $i++;
}
echo "</select></td>\n";

echo "<td><select name=\"ordertype\">".
    "<option value=\"DESC\" ";
    if ($ordertype == "DESC") echo "selected";
    echo " >"._btdesc."</option>".
    "<option value=\"ASC\" ";
    if ($ordertype == "ASC") echo "selected";
    echo " >"._btord."</option>".
    "</select></td>";

echo "<td><p><input type=\"checkbox\" name=\"dead\" ";
if ($dead == "true") echo "checked";
echo " value=\"ok\">"._btitm."";

echo "</td></tr></table>";
echo "</form>";

CloseTable();
#OBT: Add joe's table-head sorting function:
$sort =  $_GET["sort"];
$type =  $_GET["type"];
$orderby = " ORDER BY ".$db_prefix."_torrents.evidence DESC, ";//
if ($sort == 1) $orderby = " ORDER BY ".$db_prefix."_torrents.name $type,  ";//

if ($sort == 2){
$orderby = " ORDER BY ".$db_prefix."_torrents.numfiles $type,  ";//
}
if ($sort == 3){
$orderby = " ORDER BY ".$db_prefix."_torrents.comments $type,  ";//
}
if ($sort == 4){
$orderby = " ORDER BY ".$db_prefix."_torrents.category $type,  ";//
}
if ($sort == 5){
$orderby = " ORDER BY ".$db_prefix."_torrents.size $type,  ";
}
if ($sort == 6){
$orderby = " ORDER BY ".$db_prefix."_torrents.times_completed $type,  ";
}
if ($sort == 7){
$orderby = " ORDER BY ".$db_prefix."_torrents.seeders $type,  ";//
}
if ($sort == 8){
$orderby = " ORDER BY ".$db_prefix."_torrents.leechers $type,  ";//
}

if ($onlysearch) {
        OpenTable(_btnosearchttl, '728');
        echo _btnosearch;
        CloseTable();
} else {
        if (!isset($page) OR !is_numeric($page) OR $page < 1) $page = 1;
        if (!isset($cat)) $cat = "";
        if (intval($cat) > 0) $catwhere = " AND ".$db_prefix."_torrents.category = ".intval($cat);
        else $catwhere = "";
        $passwhere = " AND ".$db_prefix."_torrents.password IS NULL ";
        $viswhere = "visible = 'yes' AND banned = 'no'";
        if ($user->moderator) $viswhere = "1";
        if ($user->premium) $passwhere = "";

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

        $totsql = "SELECT COUNT(*) as tot FROM ".$db_prefix."_torrents WHERE ".$viswhere.$catwhere.$passwhere.";";
        $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 ".$viswhere.$catwhere.$passwhere.$orderby.$db_prefix."_torrents.added DESC LIMIT ".$from.",".$torrent_per_page.";";

        $res = $db->sql_query($sql) or btsqlerror($sql);
        $totres = $db->sql_query($totsql);

if (isset($sort))$sort1= "&sort=".$sort."&type=".$type;
else
$sort1="";
if (isset($sort))$sort2= "?sort=".$sort."&type=".$type;
else
$sort2="";
        list ($tot) = $db->sql_fetchrow($totres);
        $db->sql_freeresult($totres);
        if ($db->sql_numrows($res) > 0) {
                $pages = ceil($tot / $torrent_per_page);

				if($page < $pages & $cat) {
                        $next = "<b><a href=\"torrents.php?page=".($page+1)."&cat=".$cat."\">&gt;&gt;&gt;&gt;</a></b>";
                } elseif($page < $pages ) {
                        $next = "<b><a href=\"torrents.php?page=".($page+1).$sort1."\">&gt;&gt;&gt;&gt;</a></b>";
						
                }else{
				        $next = "&gt;&gt;&gt;&gt;";
				}
				if($page > 1 & $cat) {
                        $prev = "<b><a href=\"torrents.php?page=".($page-1)."&cat=".$cat."\">&lt;&lt;&lt;&lt;</a></b>";
                } elseif($page > 1) {
                        $prev = "<b><a href=\"torrents.php?page=".($page-1).$sort1."\">&lt;&lt;&lt;&lt;</a></b>";
						
                }else{
				        $prev = "&lt;&lt;&lt;&lt;";
				}
                if($cat){
                $pager = "<a href=\"torrents.php?cat=".$cat."\">".(($page == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

                if (($page - 15) > 1) $pager .= "...";

                for ($i = max(2,$page - 15); $i < min($pages, $page + 15); $i++) {
                        $pager .= "<a href=\"torrents.php?page=".$i."&cat=".$cat."\">".(($i == $page) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
                }
                if (($page + 15) < $pages) $pager .= "...";
                $pager .= "<a href=\"torrents.php?page=".$pages."&cat=".$cat."\">".(($page == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";
                torrenttable($res);
				}else {
                $pager = "<a href=\"torrents.php".$sort2."\">".(($page == 1) ? "<b>1</b>" : "1")."</a>&nbsp;";

                if (($page - 15) > 1) $pager .= "...";

                for ($i = max(2,$page - 15); $i < min($pages, $page + 15); $i++) {
                        $pager .= "<a href=\"torrents.php?page=".$i.$sort1."\">".(($i == $page) ? "<b>".$i."</b>" : $i)."</a>&nbsp;";
                }
                if (($page + 15) < $pages) $pager .= "...";
                $pager .= "<a href=\"torrents.php?page=".$pages.$sort1."\">".(($page == $pages) ? "<b>".$pages."</b>" : $pages)."</a>";
                torrenttable($res);
				}                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>";
        } else {
                OpenTable2();
                echo "<h3>"._btnotorrents."</h3>";
                CloseTable2();
        }
}
include("footer.php");
?>
