<?php
// ================================================================================
//  <<-- Suchergebnisse zur Suchmaske -->>
// ================================================================================
$tmpl->assign('lang', $lang);
$tmpl->assign('redir', redir());

$q = $_GET['q'];
if(strlen($q)<2) {$q = ""; }

if ($q == "") {
	// es wurde kein suchbegriff eingegeben
	$EOUT = msg("searchsite", "searchsite_nochara", str_replace("__URL__", $_SERVER['HTTP_REFERER'], $lang['redirect']), $_SERVER['HTTP_REFERER']);
	$NOOUT = 1;
}



// art der suche
$search_type = 'REGEXP';
$search_prefix = '';

$search_type = ($_GET['regexp'] == 1) ? 'REGEXP' : 'LIKE';
$search_prefix = ($_GET['regexp'] == 1) ? '' : '%';

// schluesselwort
$pattern_or = @explode('or', strtolower($q));
$pattern_tmp = "";
$p_and_array = array();

foreach ($pattern_or as $part) {
	$pattern_and = @explode('and', strtolower($part));
	$sub_pattern = array();

	foreach ($pattern_and as $sub_part) {
		$sub_part = trim($sub_part);

		// NEWS
		if($_GET['search_news']==1){
			$sub_pattern[] = "
			(
			title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			text $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			textmore $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}
		// ARTIKEL
		if($_GET['search_articles']==1) {
			$sub_pattern_a[] = "
			(
			title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			content $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}
		// DOWNLOADS
		if($_GET['search_downloads']==1 || $_GET['search_links']==1) {
			$sub_pattern_b[] = "
			(
			title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			text $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}
		// FAQ
		if($_GET['search_faq']==1) {
			$sub_pattern_d[] = "
			(
			faq $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			answer $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}

		// STATISCHE DOKUMENTE
		if($_GET['search_static']==1) {
			$sub_pattern_e[] = "
			(
			title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			content $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}
		
		// GALLERY
		if($_GET['search_gallery']==1) {
			$sub_pattern_f[] = "
			(
			title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
			descr $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
			)";
		}
	}
	$pattern_tmp = @implode(' AND ', $sub_pattern);
	$pattern_tmp_a = @implode(' AND ', $sub_pattern_a);
	$pattern_tmp_b = @implode(' AND ', $sub_pattern_b);
	$pattern_tmp_c = @implode(' AND ', $sub_pattern_b);
	$pattern_tmp_d = @implode(' AND ', $sub_pattern_d);
	$pattern_tmp_e = @implode(' AND ', $sub_pattern_e);
	$pattern_tmp_f = @implode(' AND ', $sub_pattern_f);

	$p_and_array[] = $pattern_tmp;
	$p_and_array_a[] = $pattern_tmp_a;
	$p_and_array_b[] = $pattern_tmp_b;
	$p_and_array_c[] = $pattern_tmp_c;
	$p_and_array_d[] = $pattern_tmp_d;
	$p_and_array_e[] = $pattern_tmp_e;
	$p_and_array_f[] = $pattern_tmp_f;
}

	$pattern = @implode(' OR ', $p_and_array);
	$pattern_a = @implode(' OR ', $p_and_array_a);
	$pattern_b = @implode(' OR ', $p_and_array_b);
	$pattern_c = @implode(' OR ', $p_and_array_c);
	$pattern_d = @implode(' OR ', $p_and_array_d);
	$pattern_e = @implode(' OR ', $p_and_array_e);
	$pattern_f = @implode(' OR ', $p_and_array_f);

$limit = ($_REQUEST['pp']=="") ? 15 : $_REQUEST['pp'];

//=====================================================================
// <<-- GALLERY -->>
//=====================================================================
if($_GET['search_gallery']==1){

	$galleryitems = array();
	$sql = new dbc("SELECT id FROM ".PREFIX."_gallery WHERE area = '$area' AND $pattern_f");
	$count_gallery = $sql->numrows();
	$tmpl->assign('count_gallery',$count_gallery);

	$seiten = ceil($count_gallery / $limit);
	$a = prepage() * $limit - $limit;

	if($count_gallery > $limit){
		$tmpl->assign('pages', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?q=$q&amp;search_gallery=1&amp;p=query&amp;details=1&amp;pp=".$limit."&amp;page={s}\">{t}</a> "));
	}

	$sql = new dbc("SELECT id,parent_id,title,descr FROM ".PREFIX."_gallery WHERE area = '$area' AND $pattern_f limit $a,$limit");

	while($row = $sql->fetchrow()){
		if(@strpos(strtolower($row->title), strtolower($q))>1){
			$fundort = strip_tags($row->title);
		} else {
			$fundort = strip_tags($row->descr);
		}

		@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
		$orte = " ... ";
		while (@list($key, $val) = @each($fo)) {
			$orte .= $val . " ... ";}
			$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

			$row->erg = $orte;
			$row->erg_str = $fundort;
			array_push($galleryitems,$row);
	}
	$tmpl->assign('galleryitems', $galleryitems);
}

//=====================================================================
// <<-- FAQ -->>
//=====================================================================
if($_GET['search_faq']==1){

	$faqitems = array();
	$sql = new dbc("SELECT id FROM ".PREFIX."_faq WHERE area = '$area' AND $pattern_d");
	$count_faq = $sql->numrows();
	$tmpl->assign('count_faq',$count_faq);

	$seiten = ceil($count_faq / $limit);
	$a = prepage() * $limit - $limit;

	if($count_faq > $limit){
		$tmpl->assign('pages', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?q=$q&amp;search_faq=1&amp;p=query&amp;details=1&amp;pp=".$limit."&amp;page={s}\">{t}</a> "));
	}

	$sql = new dbc("SELECT id,parent_id,faq,answer FROM ".PREFIX."_faq WHERE area = '$area' AND $pattern_d limit $a,$limit");

	while($row = $sql->fetchrow()){
		if(@strpos(strtolower($row->faq), strtolower($q))>1){
			$fundort = strip_tags($row->faq);
		} else {
			$fundort = strip_tags($row->answer);
		}

		@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
		$orte = " ... ";
		while (@list($key, $val) = @each($fo)) {
			$orte .= $val . " ... ";}
			$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

			$row->erg = $orte;
			$row->erg_str = $fundort;
			array_push($faqitems,$row);
	}
	$tmpl->assign('faqitems', $faqitems);
}
//=====================================================================
// <<-- STATISCHE DOKUMENTE -->>
//=====================================================================
if($_GET['search_static']==1){
	$static_items	= array();
	$sql			= "SELECT id FROM ".PREFIX."_static WHERE $pattern_e ORDER BY ctime $ascdesc";
	$res			= new dbc($sql);
	$count_static	= $res->numrows();
	$tmpl->assign('count_static',$count_static);

	$seiten			= ceil($count_static / $limit);
	$a = prepage() * $limit - $limit;

	$sql			= "SELECT * FROM ".PREFIX."_static WHERE $pattern_e ORDER BY ctime $ascdesc";
	$res			= new dbc($sql);

	if($count_static > $limit){
		$tmpl->assign('pages', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?q=$q&amp;search_static=1&amp;p=query&amp;details=1&amp;pp=".$limit."&amp;page={s}\">{t}</a> "));
	}

	while($row = $res->fetchrow()){
		if(@strpos(strtolower($row->content), strtolower($q))>1){
			$fundort = strip_tags($row->content);
		} else {
			$fundort = strip_tags($row->content);
		}

		@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
		$orte = " ... ";
		while (@list($key, $val) = @each($fo)) {
			$orte .= $val . " ... ";}
			$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

			$row->erg = $orte;
			$row->erg_str = $fundort;
			array_push($static_items,$row);
	}
	$tmpl->assign('static_items', $static_items);
}

//=====================================================================
// <<-- NEWS -->>
//=====================================================================
if($_GET['search_news']==1){

	$newsitems = array();

	$db_search = " && ( (cansearch='yes') AND $pattern )";
	$sql = "SELECT newsid FROM ".PREFIX."_news WHERE
		((area_id='$area' || allareas='yes') && 
		((time_end>=".time().") || 
		(time_end='1') || 
		(time_end='0')) && 
		(((ispublic='1')) && 
		(time_start<=".time().") $db_search))  
		ORDER BY ntime $ascdesc";

	$res = new dbc($sql);
	$count_news = $res->numrows();
	$tmpl->assign('count_news',$count_news);

	$seiten = ceil($count_news / $limit);
	$a = prepage() * $limit - $limit;

	$sql = "SELECT
		area_id,
		newscat,
		uid,
		newsid,
		title,
		text, 
		textmore, 
		time_start, 
		ntime 
	FROM ".PREFIX."_news WHERE 
		((area_id='$area' || allareas='yes') && 
		((time_end>=".time().") || 
		(time_end='1') || 
		(time_end='0')) && 
		(((ispublic='1')) && 
		(time_start<=".time().") $db_search))  
	ORDER BY ntime $ascdesc limit $a,$limit";
	$res = new dbc($sql);

	if($count_news > $limit){
		$tmpl->assign('pages', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?q=$q&amp;search_news=1&amp;p=query&amp;details=1&amp;pp=".$limit."&amp;page={s}\">{t}</a> "));
	}

	while($row = $res->fetchrow()){
		if(@strpos(strtolower($row->text), strtolower($q))>1){
			$fundort = strip_tags($row->text);
		} else {
			$fundort = strip_tags($row->textmore);
		}

		@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
		$orte = " ... ";
		while (@list($key, $val) = @each($fo)) {
			$orte .= $val . " ... ";}
			$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

			$row->erg = $orte;
			$row->erg_str = $fundort;
			array_push($newsitems,$row);
	}
	$tmpl->assign('newsitems', $newsitems);
}


//=====================================================================
// <<-- ARTIKEL -->>
//=====================================================================
if($_GET['search_articles']==1){
	$articleitems = array();
	$db_search = " && ( (cansearch='yes') AND $pattern_a )";
	$sql = "
	SELECT id FROM ".PREFIX."_articles WHERE 
		((area='$area' || allareas='yes') && 
		((time_end>=".time().") || 
		(time_end='1') || 
		(time_end='0')) && 
		(((active='1')) && 
		(time_start<=".time().") && 
		(showonpage='1') $db_search ))";

	$res = new dbc($sql);
	$count_articles = $res->numrows();
	$tmpl->assign('count_articles',$count_articles);

	$seiten = ceil($count_articles / $limit);
	$a = prepage() * $limit - $limit;

	$sql = "
	SELECT 
		id,
		area,
		type,
		title,
		content,
		ctime
	FROM ".PREFIX."_articles WHERE 
		((area='$area' || allareas='yes') && 
		((time_end>=".time().") || 
		(time_end='1') || 
		(time_end='0')) && 
		(((active='1')) && 
		(time_start<=".time().") && 
		(showonpage='1') $db_search )) 
	ORDER BY ctime limit $a,$limit";
	$res = new dbc($sql);

	if($count_articles > $limit){
		$tmpl->assign('pages', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?q=$q&amp;search_articles=1&amp;p=query&amp;details=1&amp;pp=".$limit."&amp;page={s}\">{t}</a> "));
	}

	while($row = $res->fetchrow()){
		if(@strpos(strtolower($row->content), strtolower($q))>1){
			$fundort = strip_tags($row->content);
		}

		@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
		$orte = " ... ";
		while (@list($key, $val) = @each($fo)) {
			$orte .= $val . " ... ";}
			$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

			$row->erg = $orte;
			$row->erg_str = $fundort;
			array_push($articleitems,$row);
	}
	$tmpl->assign('articleitems', $articleitems);
}


//=====================================================================
// <<-- DOWNLOADS - PRUEFEN OB DER BENUTZER DIE BERECHTIGUNG HAT -->>
//=====================================================================
if($_GET['search_downloads']==1){
	$group_ids = array();
	if (@is_numeric(UID)) {
		$queryfirst = "SELECT group_id_misc FROM " . PREFIX . "_user WHERE uid = " . UID;
		$result = new dbc($queryfirst);
		$user = $result->fetchrow();

		if($user->group_id_misc != ""){
			$group_ids_pre = UGROUP . ";" . $user->group_id_misc;
			$group_ids     = @explode(";", $group_ids_pre);

		} else {
			$group_ids[] = UGROUP;
		}
	} else {
		$group_ids[] = 2;
	}
	$downloads = array();
	$sql = "SELECT ctime,title,text,area,id,catid  FROM " . PREFIX . "_downloads WHERE $pattern_b limit 200";
	$res = new dbc($sql);
	$num = $res->numrows();

	while($item= $res->fetchrow()){
		$query = new dbc("SELECT group_id FROM " . PREFIX . "_downloadcat WHERE catid = '$item->catid'");

		$row = $query->fetchrow();
		$g_array = @explode(",", $row->group_id);

		if (array_intersect($group_ids, $g_array)) {
			$count_downloads += 1;
			$fundort = strip_tags($item->text);
			@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
			$orte = " ... ";
			while (@list($key, $val) = @each($fo)) {
				$orte .= $val . " ... ";}
				$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

				$item->erg = $orte;
				$item->erg_str = $fundort;

				array_push($downloads, $item);
		}


		$tmpl->assign('count_downloads',$count_downloads);
		$tmpl->assign('downloads', $downloads);
	}
}

//=====================================================================
// <<-- LINKS -->>
//=====================================================================
if($_GET['search_links']==1){
	$group_ids = array();
	if (@is_numeric(UID)) {
		$queryfirst = "SELECT group_id_misc FROM " . PREFIX . "_user WHERE uid = " . UID;
		$result = new dbc($queryfirst);
		$user = $result->fetchrow();

		if($user->group_id_misc != ""){
			$group_ids_pre = UGROUP . ";" . $user->group_id_misc;
			$group_ids     = @explode(";", $group_ids_pre);

		} else {
			$group_ids[] = UGROUP;
		}
	} else {
		$group_ids[] = 2;
	}
	$links = array();
	$sql = "SELECT ctime,title,text,area,id,catid FROM " . PREFIX . "_links WHERE $pattern_c  limit 200";
	$res = new dbc($sql);

	while($item= $res->fetchrow()){
		$query = new dbc("SELECT group_id FROM " . PREFIX . "_linkcat WHERE catid = '$item->catid'");

		$row = $query->fetchrow();
		$g_array = @explode(",", $row->group_id);

		if (array_intersect($group_ids, $g_array)) {
			$count_links += 1;
			$fundort = strip_tags($item->text);
			@eregi(".{0,36}" . $q . ".{0,36}", $fundort, $fo);
			$orte = " ... ";
			while (@list($key, $val) = @each($fo)) {
				$orte .= $val . " ... ";}
				$orte = @eregi_replace("(".quotemeta($q).")", "<span class=\"highlight\">\\1</span>", $orte);

				$item->erg = $orte;
				$item->erg_str = $fundort;

				array_push($links, $item);
		}
		$tmpl->assign('count_links',$count_links);
		$tmpl->assign('links', $links);
	}
}


$countall = $count_links + $count_downloads + $count_articles + $count_news + $count_faq + $count_static + $count_gallery;
$countall_text = str_replace("__MATCHES__", $countall, $lang['search_result_matches_text']);
$tmpl->assign('numall', $countall);
$tmpl->assign('countall', $countall_text);
$tmpl->assign('q', $q);
$sname = simpletitle($lang['searchsite']);
$tmpl->assign("content", parsetrue("container/".container("news"), $lang['searchsite'], $tmpl->fetch("search/query.tpl")));
?>