<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.6 www.exoops.info 2006/03 
// $Id: search.php,v 1.5 2003/03/03 17:11:22 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Based on:
// phpBB: http://www.phpBB.com
// License type: GPL
// ------------------------------------------------------------------------- //
include_once("./header.php");
include_once(XOOPS_ROOT_PATH."/header.php");
OpenTable();
if ($_POST['submit']) {
	echo "
	<table border='0' width='$bbWidth' cellpadding='5' align='center'><tr>
	<td colspan='2' align='left'>
	<img src='".$bbImage['f_open']."' alt='/' />&nbsp;<a href='".$bbUrl['root']."index.php'>";
	echo _MD_FORUMINDEX;
	echo "
	</a>
	<br />&nbsp;&nbsp;&nbsp;<img src='".$bbImage['f_open']."' alt='/' />&nbsp;<a href='search.php'>". _SEARCH ."</a>
	<br /><img src='".$bbImage['f_content']."' alt='' />&nbsp;"._MD_SEARCHRESULTS."</td>
	</tr></table>";
$term            = $_POST['term'];
$addterms        = $_POST['addterms'];
$forum           = $_POST['forum'];
$search_username = $_POST['search_username'];
$sortby          = $_POST['sortby'];
$searchboth      = $_POST['searchboth'];
$query = "
	SELECT
	u.uid,
	f.forum_id,
	p.topic_id,
	u.uname,
	p.post_time,
	t.topic_title,
	f.forum_name
	FROM
	".$db->prefix("bb_posts")." p,
	".$db->prefix("users")." u,
	".$db->prefix("bb_forums")." f,
	".$db->prefix("bb_topics")." t";
if ( isset($term) && $term != "" ) {
	$terms = split(" ", addslashes($term));
	$addquery .= "(p.post_text LIKE '%$terms[0]%'";
	$subquery .= "(t.topic_title LIKE '%$terms[0]%'";
	if ( $addterms == "any" ) {
		$andor = "OR";
		} else {
			$andor = "AND";
		}
	$size = count($terms);
	for ($i=1; $i<$size; $i++) {
		$addquery.=" $andor p.post_text LIKE '%$terms[$i]%'";
		$subquery.=" $andor t.topic_title LIKE '%$terms[$i]%'";
	}
	$addquery.=")";
	$subquery.=")";
}
if ( isset($forum) && $forum!="all" ) {
	if ( isset($addquery) ) {
		$addquery .= " AND ";
		$subquery .= " AND ";
	}
	$addquery .=" p.forum_id=$forum";
	$subquery .=" p.forum_id=$forum";
}
if ( isset($search_username) && $search_username != "" ) {
	$search_username = addslashes($search_username);
	if ( !$result = $db->query("SELECT uid FROM ".$db->prefix("users")." WHERE uname='$search_username'") ) {
		error_die("<big>"._ERROROCCURED."</big><hr />"._MD_COULDNOTQUERY."");
	}
	$row = $db->fetchArray($result);
	if ( !$row ) {
		error_die(_MD_USERNOEXIST);
	}
	$userid = $row['uid'];
	if ( isset($addquery) ) {
		$addquery.=" AND p.uid=$userid AND u.uname='$search_username'";
		$subquery.=" AND p.uid=$userid AND u.uname='$search_username'";
		} else {
			$addquery.=" p.uid=$userid AND u.uname='$search_username'";
			$subquery.=" p.uid=$userid AND u.uname='$search_username'";
		}
}
if ( isset($addquery) ) {
	switch ( $searchboth ) {
		case "both" :
			$query .= " WHERE ( $subquery OR $addquery ) AND ";
			break;
		case "title" :
			$query .= " WHERE ( $subquery ) AND ";
			break;
		case "text" :
			$query .= " WHERE ( $addquery ) AND ";
			break;
	}
	} else {
		$query .= " WHERE ";
	}
$query .= "
	p.topic_id = t.topic_id
	AND p.forum_id = f.forum_id
	AND p.uid = u.uid
	AND f.forum_type != 1";
//$query .= " GROUP BY t.topic_id";
$query .= " ORDER BY $sortby";
if ( !$result = $db->query($query, 200, 0) ) {
	die("<big>"._ERROROCCURED."</big><hr />"._MD_COULDNOTQUERY);
}
if ( $db->getRowsNum($result) == 0 ) {
	echo "<h4>"._NOMATCHESFOUND."</h4>";
	} else {
		echo "
		<table border='0' cellpadding='0' cellspacing='0' align='center' valign='top' width='95%'><tr>
		<td class='bg2'>
		<table border='0' cellpadding='4' cellspacing='1' width='100%'><tr class='bg3' align='center'>
		<td><b>"._MD_FORUM."</b></td>
		<td><b>"._MD_TOPIC."</b></td>
		<td><b>"._MD_AUTHOR."</b></td>
		<td><b>"._MD_POSTTIME."</b></td>
		</tr>";
		while ($row = $db->fetchArray($result)) {
			echo "
			<tr class='bg1' align='center'>
			<td nowrap><a href='viewforum.php?forum=".$row['forum_id']."'>". $myts->makeTboxData4Show($row['forum_name']) . "</a></td>
			<td align='left'><a href='viewtopic.php?topic_id=".$row['topic_id']."&amp;forum=".$row['forum_id']."'>". $myts->makeTboxData4Show($row['topic_title']) . "</a></td>
			<td nowrap><a href='".XOOPS_URL."/userinfo.php?uid=".$row['uid']."'>".$myts->makeTboxData4Show($row['uname'])."</a></td>
			<td nowrap>".formatTimestamp($row['post_time'], "s")."</td>
			</tr>";
		}
		echo "</table></td></tr></table>";
	}
	} else {
		echo "
		<table border='0' width='$bbWidth' cellpadding='5' align='center'><tr>
		<td colspan='2' align='left'>&nbsp;&nbsp;
		<img src='".$bbImage['f_open']."' alt='/' />
		<a href='".$bbUrl['root']."index.php'>"._MD_FORUMINDEX."</a>
		<br /><img src='".$bbImage['f_content']."' alt='/' /> "._SEARCH."</td>
		</tr></table>";
	}
echo "
<form name='Search' action='./search.php' method='post'>
<table border='0' cellpadding='1' cellspacing='0' align='center' valign='top'><tr>
<td class='bg2'>
<table border='0' cellpadding='1' cellspacing='1' width='100%' class='bg3'><tr>
<td class='bg3' align='right'><b>"._MD_KEYWORDS."</b></td>
<td class='bg1'><input type='text' class='text' name='term' value='".$myts->makeTareaData4Show($term)."' /></td>
</tr><tr>
<td class='bg3'>&nbsp;</td>
<td class='bg1'><input type='radio' class='radio' name='addterms' value='any' checked='checked' />"._MD_SEARCHANY."</td>
</tr><tr>
<td class='bg3'>&nbsp;</td>
<td class='bg1'><input type='radio' class='radio' name='addterms' value='all' />"._MD_SEARCHALL."</td>
</tr><tr>
<td class='bg3' align='right'><b>". _MD_FORUMC ."</b></td>
<td class='bg1'>
<select class='select' name='forum'><option value='all'>". _MD_SEARCHALLFORUMS ."</option>";
$query = "SELECT forum_name, forum_id FROM ".$db->prefix("bb_forums")." WHERE forum_type != 1";
if ( !$result = $db->query($query) ) {
	die("<big>"._ERROROCCURED."</big><hr />"._MD_COULDNOTQUERY."");
}
while ( $row = $db->fetchArray($result) ) {
	echo "<option value='".$row['forum_id']."'>".$row['forum_name']."</option>";
}
echo "
</select></td>
</tr><tr>
<td class='bg3' align='right'><b>"._MD_AUTHORC."</b></td>
<td class='bg1'><input type='text' class='text' name='search_username' value='".$myts->makeTareaData4Show($search_username)."' /></td>
</tr><tr>
<td class='bg3' align='right'><b>"._SORTBY."</b></td>
<td class='bg1'>
<input type='radio' class='radio' name='sortby' value='p.post_time desc' checked='checked' />"._DATE."
<input type='radio' class='radio' name='sortby' value='t.topic_title' />"._MD_TOPIC."
<input type='radio' class='radio' name='sortby' value='f.forum_name' />"._MD_FORUM."
<input type='radio' class='radio' name='sortby' value='u.uname' />"._MD_USERNAME."
</td>
</tr><tr>
<td class='bg3' align='right'><b>"._MD_SEARCHIN."</b></td>
<td class='bg1'>
<input type='radio' class='radio' name='searchboth' value='both' checked='checked' />"._SUBJECT." & "._MD_BODY."
<input type='radio' class='radio' name='searchboth' value='title' />"._SUBJECT."
<input type='radio' class='radio' name='searchboth' value='text' />"._MD_BODY."
</td>
</tr><tr>
<td colspan='2' align='center'>
<input type='submit' class='button' name='submit' value='"._SEARCH."' /></td>
</tr></table>
</td>
</tr></table>
</form>";
CloseTable();
include_once(XOOPS_ROOT_PATH."/footer.php");
?>
