<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2005 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------
| ASS ver 1.3
| - fixed SQL injection
+----------------------------------------------------
*/
require_once "maincore.php";
require_once "subheader.php";
require_once "side_left.php";
include LOCALE.LOCALESET."search.php";

$news_per_page = 11; $posts_per_page = 20;

if (!isset($rowstart) || !isNum($rowstart)) $rowstart = 0;

if (!isset($stype)) $stype = "a";
if (!isset($_POST['stype'])) $_POST['stype'] = "a";
if (isset($stext)) $stext = stripinput(trim($stext));

if (!isset($stype)) $stype = isset($_POST['stype']) ? $_POST['stype'] : "a";
if (!isset($stext)) $stext = isset($_POST['stext']) ? $_POST['stext'] : "";

//Options
if (!isset($method)) $method = isset($_POST['method']) ? (($_POST['method']=="OR" || $_POST['method']=="AND") ? $_POST['method'] : "OR") : "OR";
if (!isset($datelimit)) $datelimit = isset($_POST['datelimit']) ? (isNum($_POST['datelimit']) ? $_POST['datelimit'] : 0) : 0;
if (!isset($fields)) $fields = isset($_POST['fields']) ? (isNum($_POST['fields']) ? $_POST['fields'] : 2) : 2;
if (!isset($sort)) $sort = isset($_POST['sort']) ? (($_POST['sort']=="datestamp" || $_POST['sort']=="subject" || $_POST['sort']=="author") ? $_POST['sort'] : "datestamp") : "datestamp";
if (!isset($order)) $order = isset($_POST['order']) ? (isNum($_POST['order']) ? $_POST['order'] : 0) : 0;
if (!isset($chars)) $chars = isset($_POST['chars']) ? (isNum($_POST['chars']) ? $_POST['chars'] : 200) : 200;
if (!isset($forum_id)) $forum_id = isset($_POST['forum_id']) ? (isNum($_POST['forum_id']) ? $_POST['forum_id'] : 0) : 0;

if (!function_exists ("multilang_table")) {
	function multilang_table($param) {
		return false;
	}
}

$composevars = "method=$method&amp;datelimit=$datelimit&amp;fields=$fields&amp;sort=$sort&amp;order=$order&amp;chars=$chars&amp;forum_id=$forum_id&amp;";

$forum_list = ""; $current_cat = "";
$result = dbquery(
   "SELECT f.forum_id, f.forum_name, f2.forum_name AS forum_cat_name
   FROM ".$db_prefix."forums f
   INNER JOIN ".$db_prefix."forums f2 ON f.forum_cat=f2.forum_id
   WHERE ".groupaccess('f.forum_access').(multilang_table("F")?" AND f.forum_language='".LANGUAGE."'":"")." AND f.forum_cat!='0' ORDER BY f2.forum_order ASC, f.forum_order ASC"
);

$forum_list .= "<select name='forum_id' class='textbox' style='display:".(($_POST['stype']=='f'||$stype=='f')||($_POST['stype']=='all'||$stype=='all')?"block":"none")."'>";

$forum_list .= "<option value='0'>".$locale['491']."</option>\n";
while ($data2 = dbarray($result)) {
	if ($data2['forum_cat_name'] != $current_cat) {
		if ($current_cat != "") $forum_list .= "</optgroup>\n";
		$current_cat = $data2['forum_cat_name'];
		$forum_list .= "<optgroup label='".trimlink($data2['forum_cat_name'],20)."'>\n";
	}
	$sel = ($data2['forum_id'] == $forum_id ? " selected='selected'" : "");
	$forum_list .= "<option value='".$data2['forum_id']."'$sel>".trimlink($data2['forum_name'],20)."</option>\n";
}
$forum_list .= "</optgroup>\n";
$forum_list .= "</select>";

opentable($locale['400']);
echo "<script type='text/javascript'>

function display(val) {
	switch (val) {
		case 'a':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'n':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'c':
	      document.searchform.datelimit.disabled = true;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = true;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'f':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'block';
		break;
		case 'd':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'w':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'p':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'm':
	      document.searchform.datelimit.disabled = true;
	      document.searchform.fields.disabled = true;
	      document.searchform.sort.disabled = true;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = true;
	      document.searchform.forum_id.style.display = 'none';
		break;
		case 'all':
	      document.searchform.datelimit.disabled = false;
	      document.searchform.fields.disabled = false;
	      document.searchform.sort.disabled = false;
	      document.searchform.order.disabled = false;
	      document.searchform.chars.disabled = false;
	      document.searchform.forum_id.style.display = 'block';
		break;
	}	
}
</script>";

echo "<center>
<form name='searchform' method='post' action='".FUSION_SELF."'>
<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>
<tr>
<td class='tbl2' colspan='2'><b>".$locale['401']."</b></td>
</tr>
<tr>
<td class='tbl1' align='center' width='50%'>
<input type='text' name='stext' value='$stext' class='textbox' style='width:200px'>
<input type='submit' name='search' value='".$locale['408']."' class='button'>
</td>
<td class='tbl1' align='left' width='50%'>
<input type='radio' name='method' value='OR'".($method == "OR" ? " checked='checked'" : "")."> ".$locale['447']."<br>
<input type='radio' name='method' value='AND'".($method == "AND" ? " checked='checked'" : "")."> ".$locale['448']."</td>
</tr>
<tr>
<td class='tbl2'><b>".$locale['480']."</b></td>
<td class='tbl2'><b>".$locale['481']."</b></td>
</tr>
<tr>
<td class='tbl1'>
	<table width='100%' cellpadding='0' cellspacing='0'>
	<tr>
	<td>
	<input type='radio' name='stype' value='a'".($stype == "a" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['402']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='n'".($stype == "n" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['403']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='c'".($stype == "c" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['403a']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='f'".($stype == "f" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['404']."
	</td>
	<td>".$forum_list."</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='d'".($stype == "d" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['405']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='w'".($stype == "w" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['406']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='p'".($stype == "p" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['407a']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='m'".($stype == "m" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['407']."
	</td>
	<td>&nbsp;</td>
	</tr>
	<tr>
	<td>
	<input type='radio' name='stype' value='all'".($stype == "all" ? " checked='checked'" : "")." onClick=\"display(this.value)\"> ".$locale['407b']."
	</td>
	<td>&nbsp;</td>
	</tr>
	</table>
</td>
<td class='tbl1' align='left' valign='top'>
	<table cellpadding='2' cellspacing='0'>
	<tr>
	<td>
	".$locale['490']."
	</td>
	<td>
	<select name='datelimit' class='textbox'>
	<option value='0'".($datelimit==0?" selected='selected'":"").">".$locale['491']."</option>
	<option value='86400'".($datelimit==86400?" selected='selected'":"").">".$locale['492']."</option>
	<option value='604800'".($datelimit==604800?" selected='selected'":"").">".$locale['493']."</option>
	<option value='1209600'".($datelimit==1209600?" selected='selected'":"").">".$locale['494']."</option>
	<option value='2419200'".($datelimit==2419200?" selected='selected'":"").">".$locale['495']."</option>
	<option value='7257600'".($datelimit==7257600?" selected='selected'":"").">".$locale['496']."</option>
	<option value='14515200'".($datelimit==14515200?" selected='selected'":"").">".$locale['497']."</option>
	</select>	
	</td>
	</tr>
	<tr>
	<td>&nbsp;</td>
	<td>
	<input type='radio' name='fields' value='2'".($fields==2?" checked='checked'":"")."> ".$locale['498']."<br>
	<input type='radio' name='fields' value='1'".($fields==1?" checked='checked'":"")."> ".$locale['499']."
<br>
	<input type='radio' name='fields' value='0'".($fields==0?" checked='checked'":"")."> ".$locale['500']."
	</td>
	</tr>
	<tr>
	<td>
	".$locale['505']."&nbsp;
	</td>
	<td>
	<select name='sort' class='textbox'>
	<option value='datestamp'".($sort=="datestamp"?" selected='selected'":"").">".$locale['506']."</option>
	<option value='subject'".($sort=="subject"?" selected='selected'":"").">".$locale['507']."</option>
	<option value='author'".($sort=="author"?" selected='selected'":"").">".$locale['508']."</option>
	</select>
	<td>
	</tr>
	<tr>
	<td>&nbsp;</td>
	<td>
	<input type='radio' name='order' value='0'".($order==0?" checked='checked'":"")."> ".$locale['515']."<br>
	<input type='radio' name='order' value='1'".($order==1?" checked='checked'":"")."> ".$locale['516']."<br>
	</td>
	</tr>
	</table>
<br>
".$locale['520']." <select name='chars' class='textbox'>
<option value='50'".($chars==50?" selected='selected'":"").">50</option>
<option value='100'".($chars==100?" selected='selected'":"").">100</option>
<option value='200'".($chars==200?" selected='selected'":"").">200</option>
<option value='500'".($chars==500?" selected='selected'":"").">500</option>
<option value='1000'".($chars==1000?" selected='selected'":"").">1000</option>
<option value='0'".($chars==0?" selected='selected'":"").">".$locale['491']."</option>
</select> ".$locale['521']."
</td>
</tr>
</table>
</form>
</center>\n";
closetable();

function highlight_words($word, $subject) {
   $regex_chars = '\.+?(){}[]^$';
   for ($i=0; $i<strlen($regex_chars); $i++) {
       $char = substr($regex_chars, $i, 1);
       $word = str_replace($char, '\\'.$char, $word);
   }
   $word = '/(.*)('.$word.')(.*)/i';
   return preg_replace($word, '\1<span style="background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px">\2</span>\3', $subject);
}

if ($stext != "" && strlen($stext)>="3") {
	tablebreak();
	opentable($locale['409']);
   $fswords = explode(' ', $stext);
   $swords = array();
   for ($i=0;$i<sizeof($fswords);$i++) {
   	if (strlen($fswords[$i])>=3) {
   		$swords[] = $fswords[$i];
   	}
   }
   unset($fswords);
   
   if (sizeof($swords)==0) redirect(FUSION_SELF);
	
	if ($stype == "a" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "article_datestamp";
		} else if ($sort=="subject") {
			$sortby = "article_subject";
		} else if ($sort=="author") {
			$sortby = "article_name";
		}
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "article_subject LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "article_article LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT ta.*,tac.* FROM ".$db_prefix."articles ta
         INNER JOIN ".$db_prefix."article_cats tac ON ta.article_cat=tac.article_cat_id
         WHERE ".groupaccess('article_cat_access').
         (multilang_table("A")?"AND tac.article_cat_language='".LANGUAGE."'":"").
         " AND ".$fieldsvar
      );    
		$rows = dbrows($result);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['410'] : $locale['411']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT ta.*,tac.*, tu.user_id,user_name FROM ".$db_prefix."articles ta
            INNER JOIN ".$db_prefix."article_cats tac ON ta.article_cat=tac.article_cat_id
            LEFT JOIN ".$db_prefix."users tu ON ta.article_name=tu.user_id
            WHERE ".groupaccess('article_cat_access').
            (multilang_table("A")?" AND tac.article_cat_language='".LANGUAGE."'":""). 
            " AND ".$fieldsvar."
            ".($datelimit!=0?" AND article_datestamp>=".(time()-$datelimit):"")."
            ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
         );
			while ($data = dbarray($result)) {
            $text_all = $data['article_article'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['article_subject']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['article_article']), strtolower($swords[$i]));
	         }
            echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='readarticle.php?article_id=".$data['article_id']."'>".$data['article_subject']."</a>".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
            echo "<span class='small2'>".$locale['040']."<a href='profile.php?lookup=".$data['user_id']."'>".$data['user_name']."</a>\n";
            echo $locale['041'].showdate("longdate", $data['article_datestamp'])."</span><br>\n";
            echo "<span class='small'>".$subj_c." ".($subj_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['433'].", ";
            echo $text_c." ".($text_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['434']."</span><br><br>";
			}
		} else {
			echo "<center>0 ".$locale['411'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=a&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "n" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "news_datestamp";
		} else if ($sort=="subject") {
			$sortby = "news_subject";
		} else if ($sort=="author") {
			$sortby = "news_name";
		}
		$ssubject = "";
		$smessage = "";
		$sextended = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "news_subject LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "news_news LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $sextended .= "news_extended LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "((".$smessage.") || (".$sextended."))";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage.") || (".$sextended."))";
		} else {
			$fieldsvar = "";
		}
      $rows = dbcount("(news_id)", "news", groupaccess('news_visibility').(multilang_table("N")?" AND news_language='".LANGUAGE."'":"")." AND ".$fieldsvar);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['412'] : $locale['413']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT tn.*, user_id, user_name FROM ".$db_prefix."news tn
            LEFT JOIN ".$db_prefix."users tu ON tn.news_name=tu.user_id
            WHERE ".groupaccess('news_visibility').
            (multilang_table("N")?" AND tn.news_language='".LANGUAGE."'":"").
            " AND (news_start='0'||news_start<=".time().")
            AND (news_end='0'||news_end>=".time().") 
            AND ".$fieldsvar."
            ".($datelimit!=0?" AND news_datestamp>=".(time()-$datelimit):"")."
            ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
         );
			while ($data = dbarray($result)) {
            $text_all = $data['news_news']." ".$data['news_extended'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         $text_c2 = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['news_subject']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['news_news']), strtolower($swords[$i]));
               $text_c2 += substr_count(strtolower($data['news_extended']), strtolower($swords[$i]));
	         }
            $numrows = dbcount("(news_id)", "news", groupaccess('news_visibility').(multilang_table("N")?" AND news_language='".LANGUAGE."'":"")." AND news_id>='".$data['news_id']."'");
				if ($numrows > $news_per_page) {
					$rstart = ceil($numrows / $news_per_page);
					$rstart = "?rowstart=".(($rstart-1)*$news_per_page);
				} else {
					$rstart = "";
				}
            echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='news.php?readmore=".$data['news_id']."'>".$data['news_subject']."</a>".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
            echo "<span class='small2'>".$locale['040']."<a href='profile.php?lookup=".$data['user_id']."'>".$data['user_name']."</a>\n";
            echo $locale['041'].showdate("longdate", $data['news_datestamp'])."</span><br>\n";
            echo "<span class='small'>".$subj_c." ".($subj_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['435'].", ";
            echo $text_c." ".($text_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['436'].", ";
            echo $text_c2." ".($text_c2 == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['437']."</span><br><br>";
			}
		} else {
			echo "<center>0 ".$locale['413'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=n&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "f" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "post_datestamp";
		} else if ($sort=="subject") {
			$sortby = "post_subject";
		} else if ($sort=="author") {
			$sortby = "post_author";
		}
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "post_subject LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "post_message LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT tp.*, tf.* FROM ".$db_prefix."posts tp
         INNER JOIN ".$db_prefix."forums tf USING(forum_id)
         WHERE ".groupaccess('forum_access').
         (multilang_table("F")?" AND tf.forum_language='".LANGUAGE."'":"").
         ($forum_id!=0?"AND tf.forum_id=".$forum_id:"")."
         AND ".$fieldsvar."
         ".($datelimit!=0?" AND post_datestamp>=".(time()-$datelimit):"")
      );
		$rows = dbrows($result);
		if ($rows != 0) {
			
			echo $rows." ".($rows == 1 ? $locale['414'] : $locale['415']).$locale['422'].":<br><br>\n";
			$result = dbquery(
			"SELECT tp.*, tt.thread_sticky, tf.*, tu.user_id,user_name FROM ".$db_prefix."posts tp
			INNER JOIN ".$db_prefix."threads tt ON tp.thread_id = tt.thread_id
			INNER JOIN ".$db_prefix."forums tf ON tf.forum_id= tp.forum_id
			INNER JOIN ".$db_prefix."users tu ON tp.post_author=tu.user_id
			WHERE ".groupaccess('forum_access')."
			AND ".($fields==1?"((".$ssubject.") || (".$smessage."))":"(".$smessage.")")."
			".($datelimit!=0?" AND post_datestamp>=".(time()-$datelimit):"")."
			ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
			);
			         
			while ($data = dbarray($result)) {
            $text_all = $data['post_message'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['post_subject']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['post_message']), strtolower($swords[$i]));
	         }
            echo ($data['thread_sticky']==1?"<b>".$locale['440']."</b> ":($data['thread_sticky']==2?"<b>".$locale['441']."</b> ":"")).(($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='".FORUM."viewthread.php?forum_id=".$data['forum_id']."&amp;thread_id=".$data['thread_id']."&amp;pid=".$data['post_id']."#post_".$data['post_id']."'>".$data['post_subject']."</a>".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
            echo "<span class='small2'>".$locale['040']."<a href='profile.php?lookup=".$data['user_id']."'>".$data['user_name']."</a>\n";
            echo $locale['041'].showdate("longdate", $data['post_datestamp'])."</span><br>\n";
            echo "<span class='small'>".$subj_c." ".($subj_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['438'].", ";
            echo $text_c." ".($text_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['439']."</span><br><br>";
			}
		} else {
			echo "<center>0 ".$locale['415'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=f&amp;stext=".urlencode($stext)."&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "d" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "download_datestamp";
		} else if ($sort=="subject") {
			$sortby = "download_title";
		} else {
			$sortby = "download_datestamp";
		}
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "download_title LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "download_description LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT td.*,tdc.* FROM ".$db_prefix."downloads td
         INNER JOIN ".$db_prefix."download_cats tdc ON td.download_cat=tdc.download_cat_id
         WHERE ".groupaccess('download_cat_access').
         (multilang_table("D")?" AND download_cat_language='".LANGUAGE."'":"").
         " AND ".$fieldsvar."
         ".($datelimit!=0?" AND download_datestamp>=".(time()-$datelimit):"")
      );
		$rows = dbrows($result);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['416'] : $locale['417']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT td.*,tdc.* FROM ".$db_prefix."downloads td
            INNER JOIN ".$db_prefix."download_cats tdc ON td.download_cat=tdc.download_cat_id
            WHERE ".groupaccess('download_cat_access').
            (multilang_table("D")?" AND tdc.download_cat_language='".LANGUAGE."'":"").
            " AND ".$fieldsvar."
            ".($datelimit!=0?" AND download_datestamp>=".(time()-$datelimit):"")."
            ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
         );
			$i = 1;
			while ($data = dbarray($result)) {
				if ($data['download_datestamp']+604800 > time()+($settings['timeoffset']*3600)) {
					$new = " <span class='small'>".$locale['450']."</span>";
				} else {
					$new = "";
				}
            $text_all = $data['download_description'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['download_title']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['download_description']), strtolower($swords[$i]));
	         }
				echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='downloads.php?cat_id=".$data['download_cat']."&amp;download_id=".$data['download_id']."' target='_blank'>".$data['download_title']."</a> - ".$data['download_filesize']." $new".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            if ($text_frag != "") echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
				echo "<span class='small'><font class='alt'>".$locale['451']."</font> ".$data['download_license']." |
<font class='alt'>".$locale['452']."</font> ".$data['download_os']." |
<font class='alt'>".$locale['453']."</font> ".$data['download_version']."<br>
<font class='alt'>".$locale['454']."</font> ".showdate("%d.%m.%y", $data['download_datestamp'])." |
<font class='alt'>".$locale['455']."</font> ".$data['download_count']."</span>\n";
				if ($i != $rows) { echo "<br><br>\n"; } else { echo "\n"; }
				$i++;
			}
		} else {
			echo "<center>0 ".$locale['417'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=d&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "w" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "weblink_datestamp";
		} else if ($sort=="subject") {
			$sortby = "weblink_name";
		} else {
			$sortby = "weblink_datestamp";
		}
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "weblink_name LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "weblink_description LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT tw.*,twc.* FROM ".$db_prefix."weblinks tw
         INNER JOIN ".$db_prefix."weblink_cats twc ON tw.weblink_cat=twc.weblink_cat_id
         WHERE ".groupaccess('weblink_cat_access').
         (multilang_table("WL")?" AND twc.weblink_cat_language='".LANGUAGE."'":"").
         " AND ".$fieldsvar."
         ".($datelimit!=0?" AND post_datestamp>=".(time()-$datelimit):"")
      );
		$rows = dbrows($result);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['418'] : $locale['419']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT tw.*,twc.* FROM ".$db_prefix."weblinks tw
            INNER JOIN ".$db_prefix."weblink_cats twc ON tw.weblink_cat=twc.weblink_cat_id
            WHERE ".groupaccess('weblink_cat_access').
            (multilang_table("WL")?" AND twc.weblink_cat_language='".LANGUAGE."'":"").
            " AND ".$fieldsvar."
            ".($datelimit!=0?" AND post_datestamp>=".(time()-$datelimit):"")."
            ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
         );
			$i = 1;
			while ($data = dbarray($result)) {
				if ($data['weblink_datestamp']+604800 > time()+($settings['timeoffset']*3600)) {
					$new = " <span class='small'>".$locale['450']."</span>";
				} else {
					$new = "";
				}
            $text_all = $data['weblink_description'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['weblink_name']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['weblink_description']), strtolower($swords[$i]));
	         }
				echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='weblinks.php?cat_id=".$data['weblink_cat']."&amp;weblink_id=".$data['weblink_id']."' target='_blank'>".$data['weblink_name']."</a>$new".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            if ($text_frag != "") echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
				echo "<span class='small'><font class='alt'>".$locale['454']."</font> ".showdate("%d.%m.%y", $data['weblink_datestamp'])." |
<span class='alt'>".$locale['456']."</span> ".$data['weblink_count']."</span>";
				echo ($i != $numrows ? "<br><br>\n" : "\n"); $i++;
			}
		} else {
			echo "<center>0 ".$locale['419'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=w&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "m" || $stype=="all") {
		$rows = dbcount("(user_id)", "users", "user_name LIKE '%$stext%'");
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['420'] : $locale['421']).$locale['422'].":<br><br>\n";
			$result = dbquery("SELECT * FROM ".$db_prefix."users WHERE user_name LIKE '%$stext%' ORDER BY user_name");
			while ($data = dbarray($result)) {
				echo "<a href='profile.php?lookup=".$data['user_id']."'>".$data['user_name']."</a><br>\n";
			}
		} else {
			echo "<center>0 ".$locale['421'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "p" || $stype=="all") {
		if ($sort=="datestamp") {
			$sortby = "photo_datestamp";
		} else if ($sort=="subject") {
			$sortby = "photo_title";
		} else if ($sort=="author") {
			$sortby = "photo_user";
		}
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "photo_title LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "photo_description LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT tp.*,ta.* FROM ".$db_prefix."photos tp
         INNER JOIN ".$db_prefix."photo_albums ta ON tp.album_id=ta.album_id
         WHERE ".groupaccess('album_access').
         (multilang_table("PA")?" AND ta.album_language='".LANGUAGE."'":"").
         " AND ".$fieldsvar."
         ".($datelimit!=0?" AND photo_datestamp>=".(time()-$datelimit):"")
      );
		$rows = dbrows($result);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['460'] : $locale['461']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT tp.*,ta.* FROM ".$db_prefix."photos tp
            INNER JOIN ".$db_prefix."photo_albums ta ON tp.album_id=ta.album_id
            WHERE ".groupaccess('album_access').
            (multilang_table("PA")?" AND ta.album_language='".LANGUAGE."'":"").
            " AND ".$fieldsvar."
            ".($datelimit!=0?" AND photo_datestamp>=".(time()-$datelimit):"")."
            ORDER BY ".$sortby." ".($order==1?"ASC":"DESC")." LIMIT $rowstart,10"
         );
			$i = 1;
			while ($data = dbarray($result)) {
				if ($data['photo_datestamp']+604800 > time()+($settings['timeoffset']*3600)) {
					$new = " <span class='small'>".$locale['450']."</span>";
				} else {
					$new = "";
				}
            $text_all = $data['photo_description'];
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['photo_title']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['photo_description']), strtolower($swords[$i]));
	         }
				echo "<table width='100%'>";
				echo "<tr><td width='".$settings['thumb_w']."'>";
				if ($data['photo_thumb1']!='' && file_exists(PHOTOS."album_".$data['album_id']."/".$data['photo_thumb1'])) {
					echo "<img src='".PHOTOS."album_".$data['album_id']."/".$data['photo_thumb1']."' style='border:none' alt='".$data['photo_title']."'>";
				} else if ($data['photo_thumb2']!='' && file_exists(PHOTOS."album_".$data['album_id']."/".$data['photo_thumb2'])) {
					echo "<img src='".PHOTOS."album_".$data['album_id']."/".$data['photo_thumb2']."' style='border:none' alt='".$data['photo_title']."'>";
				}
				echo "</td><td>";
				echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='photogallery.php?photo_id=".$data['photo_id']."'>".$data['photo_title']."</a>$new".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            if ($text_frag != "") echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
				echo "<span class='small'><font class='alt'>".$locale['454']."</font> ".showdate("%d.%m.%y", $data['photo_datestamp'])." |
<span class='alt'>".$locale['462']."</span> ".$data['photo_views']."</span>";
				echo "</td></tr></table>";
				echo ($i != $rows ? "<br><br>\n" : "\n"); $i++;
			}
		} else {
			echo "<center>0 ".$locale['461'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=p&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
	if ($stype == "c" || $stype=="all") {
      $sortby = "page_title";
		$ssubject = "";
		$smessage = "";
      for ($i=0;$i<sizeof($swords);$i++) {
         $ssubject .= "page_title LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
         $smessage .= "page_content LIKE '%".$swords[$i]."%'".($i<sizeof($swords)-1?" ".$method." ":"");
      }
		if ($fields==0) {
			$fieldsvar = "(".$ssubject.")";
		} else if ($fields==1) {
			$fieldsvar = "(".$smessage.")";
		} else if ($fields==2) {
			$fieldsvar = "((".$ssubject.") || (".$smessage."))";
		} else {
			$fieldsvar = "";
		}
      $result = dbquery(
         "SELECT * FROM ".$db_prefix."custom_pages
         WHERE ".groupaccess('page_access').
         (multilang_table("CP")?" AND page_language='".LANGUAGE."'":"").
         " AND ".$fieldsvar
      );    
		$rows = dbrows($result);
		if ($rows != 0) {
			echo $rows." ".($rows == 1 ? $locale['413a'] : $locale['413b']).$locale['422'].":<br><br>\n";
         $result = dbquery(
            "SELECT * FROM ".$db_prefix."custom_pages
            WHERE ".groupaccess('page_access').
            (multilang_table("CP")?" AND page_language='".LANGUAGE."'":""). 
            " AND ".$fieldsvar
         );    
			while ($data = dbarray($result)) {
            $text_all = stripslashes($data['page_content']);
	         ob_start();
	         eval ('?>'.$text_all.'<?php ');
	         $text_all = ob_get_contents();
	         ob_end_clean();
            $text_all = preg_replace("[\[(.*?)\]]", "", $text_all);
            $text_all = preg_replace("<\<(.*?)\>>", "", $text_all);
            if ($chars!=0) {
	            $text_frag = substr($text_all, 0, $chars);
	         } else {
	            $text_frag = $text_all;
	         }
	         $text_frag = nl2br(stripslashes($text_frag));
	         $subj_c = 0;
	         $text_c = 0;
	         for ($i=0;$i<sizeof($swords);$i++) {
               $text_frag = highlight_words($swords[$i], $text_frag);
               $subj_c += substr_count(strtolower($data['page_title']), strtolower($swords[$i]));
               $text_c += substr_count(strtolower($data['page_content']), strtolower($swords[$i]));
	         }
            echo (($subj_c!=0&&$fields!=1)?"<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>":"")."<a href='viewpage.php?page_id=".$data['page_id']."'>".$data['page_title']."</a>".(($subj_c!=0&&$fields!=1)?"</span>":"")."<br><br>\n";
            echo "<div class='quote' style='width:auto;height:auto;overflow:auto'>".$text_frag."</div><br>";
            echo "<span class='small'>".$subj_c." ".($subj_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['439a'].", ";
            echo $text_c." ".($text_c == 1 ? $locale['445'] : $locale['446']).$locale['432'].$locale['439b']."</span><br><br>";
			}
		} else {
			echo "<center>0 ".$locale['413b'].$locale['422'].".</center>\n";
		}
		if ($stype!="all") closetable();
		if ($rows > 10) echo "<div align='center' style='margin-top:5px;'>\n".makePageNav($rowstart,10,$rows,3,FUSION_SELF."?stype=c&amp;stext=$stext&amp;".$composevars)."\n</div>\n";
		if ($stype=="all") echo "<br><br>";
	}
	
   if ($stype=="all") closetable();

} 

require_once "side_right.php";
require_once "footer.php";
?>
