<?
if (ob_get_level() == 0) ob_start("ob_gzhandler");

require_once('include/bittorrent.inc.php');

dbconn();

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

$orderby = "ORDER BY torrents.id DESC";

$addparam = "";
$wherea = array();

if (isset($_GET["incldead"])) {
	$addparam .= "incldead=1&amp;";
	if (!isset($CURUSER) || $CURUSER["admin"] != "yes")
		$wherea[] = "banned != 'yes'";
}
else
	$wherea[] = "visible != 'no'";

if (isset($_GET["cat"]) && ($_GET["cat"] != 0)) {
	$wherea[] = "category = " . sqlesc($_GET["cat"]);
	$addparam .= "cat=" . urlencode($_GET["cat"]) . "&amp;";
}
$wherebase = $wherea;
if (isset($cleansearchstr)) {
	$wherea[] = "MATCH (search_text, ori_descr) AGAINST (" . sqlesc($searchstr) . ")";
	$addparam .= "search=" . urlencode($searchstr) . "&amp;";
	$orderby = "";
}
$where = implode(" AND ", $wherea);
if ($where != "")
	$where = "WHERE $where";

$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 id 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 $where");
		$row = mysql_fetch_array($res);
		$count = $row[0];
	}
}

if ($count) {
	list($pagertop, $pagerbottom, $limit) = pager(25, $count, "./?" . $addparam);

	$query = "SELECT torrents.*, DATE_FORMAT(torrents.added, '%m-%d-%y %T') as added, categories.name AS cat_name, users.username 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 ($count == 1) {
	$row = mysql_fetch_array($res);
	header("Refresh: 0; url=details.php?id=" . $row["id"] . "&searched=" . urlencode($searchstr));
	exit();
}

$additionals = 0;

if (isset($cleansearchstr))
	stdhead("Search results for \"$searchstr\"");
else {
	stdhead();

	if ($_SERVER["QUERY_STRING"] == "") {
		$additionals = 1;
		// motd
?>
<tr><td width="100%" colspan="2"><br><table border="0" cellspacing="0" cellpadding="0" width="70%" align=center>
<td bgcolor="#FFFFCC" height="1" style="border-style: solid; border-width: 1" bordercolor="#667766" width="100%"><font face=arial size=1><b>
<img src="<? echo $pic_base_url; ?>warn.gif" border=0 align=left>Disclaimer: None of the files shown here are actually hosted on this 
server. The links are provided solely by this site's users. The administrator of this site cannot be held responsible for what 
its users post, or any other actions of its users. You may not use this site to distribute or download any material 
for which you do not have the legal rights to do so. It is your own responsibility to adhere to these terms.</td></table>
</td></tr>

<tr><td width="100%" colspan="2"><br>
<?
	}
}

$cats = genrelist();

?>
<form method="get" action="./">
<p align="center">
Search:
<input name="search" type="text" value="<?= htmlspecialchars($searchstr) ?>" size="40" class="input">
in
<select class="input" name="cat">
<option value="0">(all types)</option>
<?

$catdropdown = "";
foreach ($cats as $cat) {
	$catdropdown .= "<option value=\"" . $cat["id"] . "\"";
	if (isset($_GET["cat"]) && $cat["id"] == $_GET["cat"])
		$catdropdown .= " selected=\"selected\"";
	$catdropdown .= ">" . htmlspecialchars($cat["name"]) . "</option>\n";
}

$deadchkbox = "<input type=\"checkbox\" name=\"incldead\" value=\"1\"";
if (isset($_GET["incldead"]))
	$deadchkbox .= " checked=\"checked\"";
$deadchkbox .= " /> include dead torrents\n";

?>
<?= $catdropdown ?>
</select>
<?= $deadchkbox ?>
<input type="submit" value="Search!" class="input"/>
</p>
</form>
<form method="get" action="./">
<p align="center">
<i>-- or --</i> Show all of:
<select class="input" name="cat">
<option value="0">(any type)</option>
<?= $catdropdown ?>
</select>
<?= $deadchkbox ?>
<input type="submit" value="Go!" class="input"/>
</p>
</form>
<?

if (isset($cleansearchstr))
	print("<h2>Search results for \"" . htmlspecialchars($searchstr) . "\"</h2>\n");

if ($count) {
	print($pagertop);
	echo "<center>Please keep your BitTorrent download windows open as long as possible! This will speed up the downloads of others and help them";
	echo " finish their downloads faster.</center>";

	torrenttable($res);

	print($pagerbottom);
}
else {
	if (isset($cleansearchstr)) {
		print("<h2>Nothing found!</h2>\n");
		print("<p>Try again with a refined search string.</p>\n");
	}
	else {
		print("<h2>No live torrents!</h2>\n");
	}
}

if ($additionals) {
	$time_end = getmicrotime();
	$time = round($time_end - $time_start,4);
	include('news.txt');
}

stdfoot();

?>
