<?php
	
/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.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 opinion) 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, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$query = (isset($_REQUEST['query'])) ? inCode(trim(str_replace(","," ",$_REQUEST['query']))) : "" ;
$where = (isset($_REQUEST['where'])) ? inCode($_REQUEST['where']) : "" ;
$results = (isset($_REQUEST['results'])) ? inCode($_REQUEST['results']) : 10 ;

$ptitle = array();
$result = $dblink->get_list("SELECT nome,titolo FROM memht_pagine WHERE (nome='articles' OR nome='blog' OR nome='download' OR nome='faq' OR nome='forum' OR nome='gallery' OR nome='guide' OR nome='mypage' OR nome='news') AND enabled=1 ORDER BY titolo");
foreach ($result as $row) {
	$ptitle[$row['nome']] = $row['titolo'];
}

$fs = false;

openTable();

if (strlen($query)>=4) {
	echo "<b>"._RESFOR_."</b> $query<br><br>";
	switch($where) {
		case "articles": art($query); break;
		case "blog": blg($query); break;
		case "download": dwn($query); break;
		case "faq": faq($query); break;
		case "forum": frm($query); break;
		case "gallery": gal($query); break;
		case "guide": gui($query); break;
		case "mypage": myp($query); break;
		case "news": nes($query); break;
		default:				
		case "all":
			art($query);
			blg($query);
			dwn($query);
			faq($query);
			frm($query);
			gal($query);
			gui($query);
			myp($query);
			nes($query);
		break;
	}
	if (!$fs) { echo "<div align='center' id='errorText' class='box'><b>"._NORES_."</b></div><br>"; }	
	google($query);
} else {
	echo "<form name='search' action='index.php?page=search' method='post'>\n";
	
		echo "<table width='100%' align='center'>\n";
			echo "<tr><td><input name='query' type='text' size='40' maxlength='255'> <b>"._TEXT_."</b></td></tr>\n";
			echo "<tr><td class='small'><i>"._MINCHAR_."</i></td></tr>\n";
			echo "<tr><td class='box small'>"._SEARCH_LEGEND_."</td></tr>\n";
		echo "</table>\n";

		echo "<table width='100%' align='center'>\n";
			echo "<tr><td>\n";
				echo "<select name='where'>\n";
					echo "<option value='all' selected>"._ALLSITE_."</option>\n";
					if ($ptitle['articles']!="") { echo "<option value='articles'>".$ptitle['articles']."</option>\n"; }
					if ($ptitle['blog']!="") { echo "<option value='blog'>".$ptitle['blog']."</option>\n"; }
					if ($ptitle['download']!="") { echo "<option value='download'>".$ptitle['download']."</option>\n"; }
					if ($ptitle['faq']!="") { echo "<option value='faq'>".$ptitle['faq']."</option>\n"; }
					if ($ptitle['forum']!="") { echo "<option value='forum'>".$ptitle['forum']."</option>\n"; }
					if ($ptitle['gallery']!="") { echo "<option value='gallery'>".$ptitle['gallery']."</option>\n"; }
					if ($ptitle['guide']!="") { echo "<option value='guide'>".$ptitle['guide']."</option>\n"; }
					if ($ptitle['mypage']!="") { echo "<option value='mypage'>".$ptitle['mypage']."</option>\n"; }
					if ($ptitle['news']!="") { echo "<option value='news'>".$ptitle['news']."</option>\n"; }
				echo "</select>\n";
			echo " <b>"._WHERESEARCH_."</b></td></tr>\n";
			echo "<tr><td>\n";
				echo "<select name='results'>\n";
				echo "<option value='5'>5</option>\n";
				echo "<option value='10' selected>10</option>\n";
				echo "<option value='20'>20</option>\n";
				echo "<option value='50'>50</option>\n";
				echo "</select>\n";
			echo " <b>"._MAXRES_."</b></td></tr>\n";
		echo "</table>\n";
		
		echo "<div align='center'><input type='submit' name='Submit' value='"._SEARCH_."'> <input type='reset' name='Reset' value='"._RESET_."'></div>\n";
	echo "</form>\n";		
}

closeTable();

function art($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_articoli WHERE MATCH(nome,descrizione,testo) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=articles' title='".$ptitle['articles']."'><b>".$ptitle['articles']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=articles&op=readArticle&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['nome']))."'>".outCode($row['nome'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function blg($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_blog_posts WHERE MATCH(title,home_text,full_text) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=blog' title='".$ptitle['blog']."'><b>".$ptitle['blog']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=blog&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['title']))."'>".outCode($row['title'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function dwn($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_download WHERE MATCH(nome,descrizione) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=download' title='".$ptitle['download']."'><b>".$ptitle['download']."</b></a></div>\n";
		foreach ($result as $row) {		
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=download&op=getFile&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['nome']))."'>".outCode($row['nome'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function faq($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_faq WHERE MATCH(domanda,risposta) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=faq' title='".$ptitle['faq']."'><b>".$ptitle['faq']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=faq&op=read&id=".$row['id']."'>".outCode($row['domanda'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function frm($query) {
	global $dblink,$results,$fs,$ptitle;

	$result = $dblink->get_list("SELECT * FROM memht_forum_posts WHERE MATCH(name,text) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		require_once("pages/forum/inc_functions.php");
		
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=forum' title='".$ptitle['forum']."'><b>".$ptitle['forum']."</b></a></div>\n";
		$listed = array();
		foreach ($result as $row) {
			$id = intval($row['id']);
			$parent = intval($row['parent']);
			$forum = intval($row['forum']);
			$ftitle = outCode($row['name']);
			
			if ($parent==0) {
				$threadid = $id;
				$link = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($ftitle);
			} else {
				$threadid = $parent;
				
				$pstnum = $dblink->get_num("SELECT id FROM memht_forum_posts WHERE id='$threadid' OR parent='$threadid'");
				if ($pstnum>10) { $plus = "&pg=".ceil($pstnum/10); } else { $plus = ""; }
				
				$trow = $dblink->get_row("SELECT * FROM memht_forum_posts WHERE id='$threadid'");
				$ftitle = outCode($trow['name']);
				$link = "index.php?page=forum&op=viewThread&id=$threadid&title=".mem_urlencode($ftitle)."$plus#post$id";
			}
	
			if (!in_array($threadid,$listed)) {
				$listed[] = $threadid;
				if (isAuthorized($forum,"view") AND isAuthorized($forum,"read") AND $ftitle!="") {
					echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='$link'>$ftitle</a></div>\n";
				}
			}
		}
		echo "<br>";
		$fs = true;
	}
}

function gal($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_gallery_images WHERE MATCH(title,description) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=gallery' title='".$ptitle['gallery']."'><b>".$ptitle['gallery']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=gallery&op=showGallery&id=".intval($row['category'])."&title=".mem_urlencode(outCode($row['title']))."'>".outCode($row['title'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function gui($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_guide WHERE MATCH(nome,descrizione,testo) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=guide' title='".$ptitle['guide']."'><b>".$ptitle['guide']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=guide&op=readGuide&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['nome']))."'>".outCode($row['nome'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function myp($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_mypage WHERE MATCH(title,text) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=mypage' title='".$ptitle['mypage']."'><b>".$ptitle['mypage']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=mypage&op=openPage&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['title']))."'>".outCode($row['title'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function nes($query) {
	global $dblink,$results,$fs,$ptitle;
	
	$result = $dblink->get_list("SELECT * FROM memht_news WHERE MATCH(nome,testo_home,testo) AGAINST('$query' IN BOOLEAN MODE) LIMIT $results");
	if ($result) {
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=news' title='".$ptitle['news']."'><b>".$ptitle['news']."</b></a></div>\n";
		foreach ($result as $row) {
			echo "<div class='small'>&nbsp;&nbsp;&nbsp;&nbsp;- <a href='index.php?page=news&op=readNews&id=".intval($row['id'])."&title=".mem_urlencode(outCode($row['nome']))."'>".outCode($row['nome'])."</a></div>\n";
		}
		echo "<br>";
		$fs = true;
	}
}

function google($query) {
		echo "<div class='box small'><a href='http://www.google.com/search?q=".$query."' target='_blank' title='"._SEARCH_." $query "._ON_." Google'>"._SEARCH_." <b>$query</b> "._ON_." Google</a></div>";
}

?>