<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

eval ("\$title_search = \"".gettemplate("title_search")."\";");
echo $title_search;

if($_GET['action']=="search") {
  $title1 = $_POST['title1'];
  $title2 = $_POST['title2'];
  $text1 = $_POST['text1'];
  $text2 = $_POST['text2'];
  $am = $_POST['am'];
  $ad = $_POST['ad'];
  $ay = $_POST['ay'];
  $bm = $_POST['bm'];
  $bd = $_POST['bd'];
  $by = $_POST['by'];
  $results = $_POST['results'];

  echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=search\');return document.MM_returnValue" value="new search"><br><br>';
  
  if($_POST['title_op']=='1') $title_op ='AND';
  else $title_op = 'OR';
  if($_POST['text_op']=='1') $text_op ='AND';
  else $text_op = 'OR';
  
  if($_POST['order']=='1') $order='DESC';
  else $order='ASC';
  
  switch($_POST['table']) {
  	case '1': $table='news'; break;
  	case '2': $table='articles'; break;
  	default: $table='forum'; break;
  }

	$after=mktime(0,0,0,$am,$ad,$ay);
	$before=mktime(0,0,0,$bm,$bd,$by);
	
	if($after<1)  $after=0;
	if($before<1) $before=time();
	
	if($table=="forum") {
		$ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_posts WHERE ( message LIKE '%$title1%' $text_op message LIKE '%$title2%' ) AND 
		                                                                  ( message LIKE '%$text1%' $text_op message LIKE '%$text2%' ) AND 
																	 ( date between $after AND $before ) ORDER BY date $order ");																   
	
	}
	elseif($table=="news") {
	    $ergebnis=safe_query("SELECT * FROM ".PREFIX."news WHERE ( headline1 LIKE '%$title1%' OR headline2 LIKE '%$title1%' ) $title_op 
		                                                           ( headline1 LIKE '%$title2%' OR headline2 LIKE '%$title2%' ) AND
																   ( content1 LIKE '%$text1%' OR content2 LIKE '%$text1%' ) $text_op 
																   ( content1 LIKE '%$text2%' OR content2 LIKE '%$text2%' ) AND 
																   ( date between $after AND $before ) ORDER BY date $order");
	}
	else {
		$ergebnis=safe_query("SELECT * FROM ".PREFIX."$table WHERE ( title LIKE '%$title1%' $title_op title LIKE '%$title2%' ) AND 
		                                                             ( content LIKE '%$text1%' $text_op content LIKE '%$text2%' ) AND 
																	 ( date between $after AND $before ) ORDER BY date $order");
	}
	$anz=mysql_num_rows($ergebnis);
    echo"<center><b>$anz</b> results found</center><br><br>";
	if($anz) {
		$i=1;
		while($ds=mysql_fetch_array($ergebnis)) {
			if($i%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
		
			$date=date("d.m.Y", $ds[date]);
			$poster='<a class="titlelink" href="index.php?site=profile&id='.$ds[poster].'"><b>'.getnickname($ds[poster]).'</b></a>';
			if($table=="news") { 
				$title=$ds[headline1];
				$auszug=substr(strip_tags(cleartext($ds[content1])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=news_comments&newsID='.$ds[newsID].'"><b>link to news</b></a>';
			}
			elseif($table=="forum") {
				$title=gettopicname($ds[topicID]);
				$auszug=substr(strip_tags(cleartext($ds[message])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=forum_topic&topic='.$ds[topicID].'"><b>link to topic</b></a>';
			}
			elseif($table=="articles") {
				$title=$ds[title];
				$auszug=substr(strip_tags(cleartext($ds[content])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=articles&action=show&articlesID='.$ds[articlesID].'"><b>link to article</b></a>';
			}

		    eval ("\$search_result = \"".gettemplate("search_result")."\";");
		    echo $search_result;
			$i++;
		}
	}
}
elseif($_GET['action']=="quicksearch" && !empty($_POST['search'])) {
  $search = $_POST['search'];
  $results = Array();
  echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=search\');return document.MM_returnValue" value="new search"><br><br>';
	$tables=array('articles', 'forum_posts', 'news');
	foreach($tables as $table) {
		if($table=="forum_posts") $ergebnis=safe_query("SELECT * FROM ".PREFIX."forum_posts WHERE message LIKE '%$search%' ORDER BY date DESC");																   
		elseif($table=="news") $ergebnis=safe_query("SELECT * FROM ".PREFIX."news WHERE ( headline1 LIKE '%$search%' OR headline2 LIKE '%$search%' ) OR ( content1 LIKE '%$search%' OR content2 LIKE '%$search%' ) ORDER BY date DESC");
		else $ergebnis=safe_query("SELECT * FROM ".PREFIX."$table WHERE title LIKE '%$search%' OR content LIKE '%$search%' ORDER BY date DESC");						

		while($ds=mysql_fetch_array($ergebnis)) {
			if($table=="articles") $tableid="articlesID";
			if($table=="forum_posts") $results[]=$ds[date].':***:forum:***:'.$ds[topicID].':***:'.$ds[message].':***:'.$ds[poster].':***:'.$ds[topicID].':***:'.$ds[boardID];
			elseif($table=="news") $results[]=$ds[date].':***:news:***:'.$ds[headline1].':***:'.$ds[content1].':***:'.$ds[poster].':***:'.$ds[newsID].':***:0';
			else $results[] = $ds[date].':***:'.$table.':***:'.$ds[title].':***:'.$ds[content].':***:'.$ds[poster].':***:'.$ds[$tableid].':***:0';
		}
	}
	$anz=count($results);
	echo"<center><b>$anz</b> results found</center><br><br>";
	if($anz) {
		natcasesort($results);
		$i=1;
		foreach($results as $result) {
		    if($i%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			$rs=explode(":***:", $result);
			$date=date("d.m.Y", $rs[0]);
			$poster='<a class="titlelink" href="index.php?site=profile&id='.$rs[4].'"><b>'.getnickname($rs[4]).'</b></a>';
			if($rs[1]=="news") { 
				$title=$rs[2];
				$auszug=substr(strip_tags(cleartext($rs[3])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=news_comments&newsID='.$rs[5].'"><b>link to news</b></a>';
			}
			elseif($rs[1]=="forum") {
				$title=gettopicname($rs[2]);
				$auszug=substr(strip_tags(cleartext($rs[3])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=forum_topic&topic='.$rs[5].'"><b>link to topic</b></a>';
			}
			elseif($rs[1]=="articles") {
				$title=$rs[2];
				$auszug=substr(strip_tags(cleartext($rs[3])), 0, 180)."...";
				$link='&#8226; <a href="index.php?site=articles&action=show&articlesID='.$rs[5].'"><b>link to article</b></a>';
			}
		    eval ("\$search_result = \"".gettemplate("search_result")."\";");
		    echo $search_result;
			$i++;
		}
	}
}
else {
    $bg1=BG_1;
	eval ("\$search_form = \"".gettemplate("search_form")."\";");
	echo $search_form;
}
?>
