<?php
class Search
{
	function ResLimit()
	{
		return getLim(15);
	}
	
	function displayForm()
	{
		// Suche gestartet
		if(isset($_GET['q']) && strlen($_GET['q'])>2)
		{	
			// Ungewuenschte Zeichen entfernen
			$q             = trim(preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9+_ ]*)/', '', $_GET['q']));
			$pattern_or    = @explode(' or ', strtolower($q));
			$pattern_tmp   = '';
			$p_and_array   = array();
			$search_type   = 'LIKE';
			$search_prefix = '%';
			$LC            = $_SESSION['Langcode'];
			
			queryLog($q, 'page', 1);
			
			$_GET['search_news'] = (get_active('News') && permission('news')) ? 1 : 0;
			$_GET['search_articles'] = (get_active('articles') && permission('articles')) ? 1 : 0;
			$_GET['search_content'] = 1;//(get_active('content')) ? 1 : 0;
			$_GET['search_faq'] = (get_active('faq') && permission('faq')) ? 1 : 0;
			$_GET['search_download'] = (get_active('downloads') && permission('downloads')) ? 1 : 0;
			$_GET['search_links'] = (get_active('links') && permission('links')) ? 1 : 0;
			$_GET['search_gallery'] = (get_active('gallery') && permission('gallery')) ? 1 : 0;
			$_GET['search_shop'] = (get_active('shop') && VERSION=='PRO') ? 1 : 0;
			$_GET['search_products'] = (get_active('products') && permission('products')) ? 1 : 0;
			$_GET['search_manufacturer'] = (get_active('manufacturer') && permission('manufacturer')) ? 1 : 0;
			$_GET['search_cheats'] = (get_active('cheats') && permission('cheats')) ? 1 : 0;
			
			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)
					{
						echo "";
						$sub_pattern[] = "
						(
							Titel{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Titel{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Intro{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Intro{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							News{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR News{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp = @implode(' AND ', $sub_pattern);
						$p_and_array[] = $pattern_tmp;
						$pattern = @implode(' OR ', $p_and_array);
						$newsitems = array();
					
						$db_search = " AND ( (Suche='1') AND $pattern )";
						$sql = "SELECT Id FROM ".PREFIX."_news WHERE
							((Sektion='".AREA."' OR AlleSektionen='1') AND
							((ZeitEnde>=".time().") OR 
							(ZeitEnde='1') OR 
							(ZeitEnde='0')) AND  
							(((Aktiv='1')) AND (Suche='1') AND 
							(ZeitStart<=".time().") $db_search))  
							ORDER BY Zeit DESC";
					
						$res = $GLOBALS['db']->Query($sql);
						$count_news = $res->numrows();
						$GLOBALS['tmpl']->assign('count_news',$count_news);
					
						if(isset($_GET['where']) && $_GET['where']=='news')
						{
							queryLog($q, 'news', 1);
							
							$limit = $this->ResLimit();
							$seiten = ceil($count_news / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Titel{$LC} AS Titel,
								Intro{$LC} AS Intro,
								News{$LC} AS News, 
								ZeitStart, 
								Zeit  
							FROM ".PREFIX."_news WHERE
								((Sektion='".AREA."' OR AlleSektionen='1') AND
								((ZeitEnde>=".time().") OR 
								(ZeitEnde='1') OR 
								(ZeitEnde='0')) AND  
								(((Aktiv='1')) AND (Suche='1') AND 
								(ZeitStart<=".time().") $db_search))
							ORDER BY Zeit DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_news > $limit)
							{
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=news\">{t}</a> "));
							}
						
							while($row = $res->fetchrow()){
								if(@strpos(strtolower($row->Intro), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Intro);
								} else {
									$fundort = strip_tags($row->News);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->News) : $orte . strip_tags($row->News);
									$row->erg_str = $fundort;
									array_push($newsitems,$row);
							}
							$GLOBALS['tmpl']->assign('newsitems', $newsitems);
						}
					}
					//============================================
					// Ende News
					//============================================
					
					//============================================
					// Artikel
					//============================================
					if($_GET['search_articles']==1)
					{
						$sub_pattern_b[] = "
						(
							Titel_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Titel_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Inhalt_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Inhalt_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp_b = @implode(' AND ', $sub_pattern_b);
						$p_and_array_b[] = $pattern_tmp_b;
						$pattern_b = @implode(' OR ', $p_and_array_b);
					
						$articleitems = array();
					
						$db_search = " AND ( (Suche='1') AND $pattern_b )";
						$sql = "SELECT Id FROM ".PREFIX."_artikel WHERE
							((Sektion='".AREA."' OR AlleSektionen='1') AND
							((ZeitEnde>=".time().") OR 
							(ZeitEnde='1') OR 
							(ZeitEnde='0')) AND  
							(((Aktiv='1')) AND (Kennwort='') AND (Suche='1') AND 
							(ZeitStart<=".time().") $db_search))  
							ORDER BY Zeit DESC";
					
						$res = $GLOBALS['db']->Query($sql);
						$count_articles = $res->numrows();
						$GLOBALS['tmpl']->assign('count_articles',$count_articles);
					
						if(isset($_GET['where']) && $_GET['where']=='articles')
						{
							queryLog($q, 'articles', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_articles / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Titel_{$LC} AS Titel,
								Inhalt_{$LC} AS Inhalt, 
								ZeitStart, 
								Zeit  
							FROM ".PREFIX."_artikel WHERE
								((Sektion='".AREA."' OR AlleSektionen='1') AND
								((ZeitEnde>=".time().") OR 
								(ZeitEnde='1') OR 
								(ZeitEnde='0')) AND  
								(((Aktiv='1')) AND (Kennwort='') AND (Suche='1') AND 
								(ZeitStart<=".time().") $db_search))
							ORDER BY Zeit DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_articles > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=articles\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($articleitems,$row);
							}
							$GLOBALS['tmpl']->assign('articleitems', $articleitems);
						}
					}
					//============================================
					// Artikel Ende
					//============================================
					
					//============================================
					// ContentSeiten
					//============================================
					if($_GET['search_content']==1)
					{
						$sub_pattern__c[] = "
						(
							Titel{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Titel{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Inhalt{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Inhalt{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__c = @implode(' AND ', $sub_pattern__c);
						$p_and_array__c[] = $pattern_tmp__c;
						$pattern__c = @implode(' OR ', $p_and_array__c);
					
						$where_gro = ($_SESSION['user_group']==1) ? "" : " AND (Gruppen='') ";
						
						$contentitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_content WHERE Sektion='".AREA."' {$where_gro} AND (Suche='1') AND (Aktiv='1') AND (Kennwort='') AND $pattern__c ";
						$res = $GLOBALS['db']->Query($sql);
						$count_content = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_content',$count_content);
					
						if(isset($_GET['where']) && $_GET['where']=='content')
						{
							queryLog($q, 'content', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_content / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Titel{$LC} AS Titel,
								Inhalt{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_content WHERE Sektion='".AREA."'{$where_gro}  AND (Suche='1') AND (Aktiv='1') AND (Kennwort='') AND $pattern__c ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_content > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=content\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($contentitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('contentitems', $contentitems);
						}
					}
					//============================================
					// ContentSeiten Ende
					//============================================
					
					
					//============================================
					// FAQ
					//============================================
					if($_GET['search_faq']==1)
					{
						$sub_pattern__d[] = "
						(
							Name_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Antwort_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Antwort_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__d = @implode(' AND ', $sub_pattern__d);
						$p_and_array__d[] = $pattern_tmp__d;
						$pattern__d = @implode(' OR ', $p_and_array__d);
					
						$faqitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_faq WHERE Sektion='".AREA."' AND (Parent_Id!='0') AND (Aktiv='1') AND $pattern__d ";
						$res = $GLOBALS['db']->Query($sql);
						$count_faq = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_faq',$count_faq);
					
						if(isset($_GET['where']) && $_GET['where']=='faq')
						{
							queryLog($q, 'faq', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_faq / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Benutzer,
								Id,
								Parent_Id,
								Name_{$LC} AS Titel,
								Antwort_{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_faq WHERE Sektion='".AREA."' AND (Parent_Id!='0') AND (Aktiv='1') AND $pattern__d ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_faq > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=faq\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($faqitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('faqitems', $faqitems);
						}
					}
					//============================================
					// FAQ Ende
					//============================================
					
					//============================================
					// Download
					//============================================
					if($_GET['search_download']==1)
					{
						$sub_pattern__e[] = "
						(
							Name_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__e = @implode(' AND ', $sub_pattern__e);
						$p_and_array__e[] = $pattern_tmp__e;
						$pattern__e = @implode(' OR ', $p_and_array__e);
					
						$downloaditems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_downloads WHERE Sektion='".AREA."' AND (Aktiv='1') AND $pattern__e ";
						$res = $GLOBALS['db']->Query($sql);
						$count_downloads = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_downloads',$count_downloads);
					
						if(isset($_GET['where']) && $_GET['where']=='downloads')
						{
							queryLog($q, 'downloads', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_downloads / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Name_{$LC} AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_downloads WHERE Sektion='".AREA."' AND (Aktiv='1')  AND $pattern__e ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_downloads > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=downloads\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($downloaditems,$row);
							}
							
							$GLOBALS['tmpl']->assign('downloaditems', $downloaditems);
						}
					}
					//============================================
					// Download Ende
					//============================================
					
					//============================================
					// Links
					//============================================
					if($_GET['search_links']==1)
					{
						$sub_pattern__f[] = "
						(
							Name_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__f = @implode(' AND ', $sub_pattern__f);
						$p_and_array__f[] = $pattern_tmp__f;
						$pattern__f = @implode(' OR ', $p_and_array__f);
					
						$linkitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_links WHERE Sektion='".AREA."' AND (Aktiv='1') AND $pattern__f ";
						$res = $GLOBALS['db']->Query($sql);
						$count_links = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_links',$count_links);
					
						if(isset($_GET['where']) && $_GET['where']=='links')
						{
							queryLog($q, 'links', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_links / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Name_{$LC} AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_links WHERE Sektion='".AREA."' AND (Aktiv='1') AND $pattern__f ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
					
							if($count_links > $limit)
							{
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=links\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($linkitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('linkitems', $linkitems);
						}
					}
					//============================================
					// Links Ende
					//============================================
					
					//============================================
					// Galerie
					//============================================
					if($_GET['search_gallery']==1)
					{
						$sub_pattern__g[] = "
						(
							Name_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__g = @implode(' AND ', $sub_pattern__g);
						$p_and_array__g[] = $pattern_tmp__g;
						$pattern__g = @implode(' OR ', $p_and_array__g);
					
						$galleryitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_galerie WHERE Sektion='".AREA."' AND (Aktiv='1') AND $pattern__g ";
						$res = $GLOBALS['db']->Query($sql);
						$count_galleries = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_galleries',$count_galleries);
					
						if(isset($_GET['where']) && $_GET['where']=='gallery')
						{
							queryLog($q, 'gallery', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_galleries / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Kategorie,
								Autor,
								Id,
								Name_{$LC} AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_galerie WHERE Sektion='".AREA."' AND (Aktiv='1') AND $pattern__g ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_galleries > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=gallery\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($galleryitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('galleryitems', $galleryitems);
						}
					}
					//============================================
					// Galerie Ende
					//============================================
					
					//============================================
					// Shop
					//============================================
					if($_GET['search_shop']==1)
					{
						$sub_pattern__h[] = "
						(
							Titel_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Titel_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_lang_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_lang_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__h = @implode(' AND ', $sub_pattern__h);
						$p_and_array__h[] = $pattern_tmp__h;
						$pattern__h = @implode(' OR ', $p_and_array__h);
					
						$shopitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_shop_produkte WHERE (Aktiv='1') AND $pattern__h ";
						
						$res = $GLOBALS['db']->Query($sql);
						$count_shoparticles = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_shoparticles',$count_shoparticles);
					
						if(isset($_GET['where']) && $_GET['where']=='shop')
						{
							queryLog($q, 'shop', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_shoparticles / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Id,
								Kategorie,
								Titel_{$LC} AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								Beschreibung_lang_{$LC} AS Inhalt_Lang, 
								Erstellt AS Datum  
							FROM ".PREFIX."_shop_produkte WHERE (Aktiv='1')  AND $pattern__h ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_shoparticles > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=shop\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt_Lang);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($shopitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('shopitems', $shopitems);
						}
					}
					//============================================
					// Shop Ende
					//============================================
					
					//============================================
					// Produkte
					//============================================
					if($_GET['search_products']==1)
					{
						$sub_pattern__i[] = "
						(
							Name{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__i = @implode(' AND ', $sub_pattern__i);
						$p_and_array__i[] = $pattern_tmp__i;
						$pattern__i = @implode(' OR ', $p_and_array__i);
					
						$productitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_produkte WHERE (Sektion='".AREA."') AND (Aktiv='1')  AND $pattern__i ";
						$res = $GLOBALS['db']->Query($sql);
						$count_products = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_products',$count_products);
					
						if(isset($_GET['where']) && $_GET['where']=='products')
						{
							queryLog($q, 'products', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_products / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Genre,
								Benutzer AS Autor,
								Id,
								Name{$LC} AS Titel,
								Beschreibung{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_produkte WHERE (Sektion='".AREA."') AND (Aktiv='1') AND $pattern__i ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_products > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=products\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($productitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('productitems', $productitems);
						}
					}
					//============================================
					// Produkte Ende
					//============================================
					
					//============================================
					// Hersteller
					//============================================
					if($_GET['search_manufacturer']==1)
					{
						$sub_pattern__j[] = "
						(
							Name $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__j = @implode(' AND ', $sub_pattern__j);
						$p_and_array__j[] = $pattern_tmp__j;
						$pattern__j = @implode(' OR ', $p_and_array__j);
					
						$manufactureritems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_hersteller WHERE (Sektion='".AREA."')  AND $pattern__j ";
						$res = $GLOBALS['db']->Query($sql);
						$count_manufacturer = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_manufacturer',$count_manufacturer);
					
						if(isset($_GET['where']) && $_GET['where']=='manufacturer')
						{
							queryLog($q, 'manufacturer', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_manufacturer / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Benutzer AS Autor,
								Id,
								Name AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								Datum  
							FROM ".PREFIX."_hersteller WHERE (Sektion='".AREA."') AND $pattern__j ORDER BY Datum DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_products > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=manufacturer\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($manufactureritems,$row);
							}
							
							$GLOBALS['tmpl']->assign('manufactureritems', $manufactureritems);
						}
					}
					//============================================
					// Hersteller Ende
					//============================================
					
					//============================================
					// Cheats
					//============================================
					if($_GET['search_cheats']==1)
					{
						$sub_pattern__k[] = "
						(
							Name_{$LC} $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Name_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') OR 
							Beschreibung_{$LC}  $search_type ('" . $search_prefix . $sub_part . $search_prefix . "') OR Beschreibung_{$LC} $search_type ('" . $search_prefix . koobispecialchars($sub_part) . $search_prefix . "') 
						)";
					
					
						$pattern_tmp__k = @implode(' AND ', $sub_pattern__k);
						$p_and_array__k[] = $pattern_tmp__k;
						$pattern__k = @implode(' OR ', $p_and_array__k);
					
						$cheatitems = array();
					
						$sql = "SELECT Id FROM ".PREFIX."_cheats WHERE (Aktiv='1') AND (Sektion='".AREA."')  AND $pattern__k ";
						$res = $GLOBALS['db']->Query($sql);
						$count_cheats = $res->numrows();
						
						$GLOBALS['tmpl']->assign('count_cheats',$count_cheats);
					
						if(isset($_GET['where']) && $_GET['where']=='cheats')
						{
							queryLog($q, 'cheats', 1);
							$limit = $this->ResLimit();
							$seiten = ceil($count_cheats / $limit);
							$a = prepage() * $limit - $limit;
						
							$sql = "SELECT
								Sektion,
								Benutzer AS Autor,
								Id,
								Plattform,
								Name_{$LC} AS Titel,
								Beschreibung_{$LC} AS Inhalt, 
								DatumUpdate  
							FROM ".PREFIX."_cheats WHERE (Aktiv='1') AND  (Sektion='".AREA."') AND $pattern__k ORDER BY DatumUpdate  DESC LIMIT $a,$limit";
							$res = $GLOBALS['db']->Query($sql);
						
							if($count_cheats > $limit){
								$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?q=".urlencode($q)."&amp;p=search&amp;pp=".$limit."&amp;page={s}&amp;where=cheats\">{t}</a> "));
							}
						
							while($row = $res->fetchrow())
							{
								
								if(@strpos(strtolower($row->Inhalt), strtolower($q))>1)
								{
									$fundort = strip_tags($row->Inhalt);
								} else {
									$fundort = strip_tags($row->Inhalt);
								}
						
								@eregi(".{0,10}" . $q . ".{0,10}", $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) ? strip_tags($row->Inhalt) : $orte . strip_tags($row->Inhalt);
									$row->erg_str = $fundort;
									array_push($cheatitems,$row);
							}
							
							$GLOBALS['tmpl']->assign('cheatitems', $cheatitems);
						}
					}
					//============================================
					// Cheats Ende
					//============================================
					
				} // Ende $pattern_and
			} // Ende $pattern_or
			
			$_GET['q'] = $q;
			
			$countall = @$count_news + @$count_articles + @$count_content + @$count_faq + @$count_downloads + @$count_links + @$count_galleries + @$count_shoparticles + @$count_products + @$count_manufacturer + @$count_cheats;
			$countall_text = str_replace("__MATCHES__", $countall, $GLOBALS['config_vars']['Page_Search_ResInf']);
			$GLOBALS['tmpl']->assign('numall', $countall);
			$GLOBALS['tmpl']->assign('countall_text', $countall_text);
			
			$out = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/search/results.tpl');
			$GLOBALS['tmpl']->assign('Results',$out);
		}
		
		$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['Page_Search']);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Page_Search']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/search/searchform.tpl'));
	}
}
?>