<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
if(!defined('BASE_DIR')) exit;
if(!get_active('forums'))
{
	notActive();
}

$GLOBALS['tmpl']->register_function('get_post_icon', 'getPostIcon');
$GLOBALS['tmpl']->assign('SearchPop', popSearch());

$stitle = $GLOBALS['config_vars']['Forums_Title'];
$sname = $stitle;




switch ($_REQUEST['action'])
{
	//=================================================================
	// Forum-Hilfe
	//=================================================================
	case 'help':
		if(!isset($_REQUEST['hid']) || $_REQUEST['hid'] == '') $_REQUEST['hid'] = 'all';
		
		switch($_REQUEST['hid'])
		{
			case 'all': forumsHelp(); break;
			default: forumsHelpDetail($_REQUEST['hid']); break;
		}
	break;
	
	//=================================================================
	// Ignorierliste
	//=================================================================
	case 'ignorelist':
		if(UGROUP == 2) koobi_message('Ignorelist', 'NoAccess', HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA, 7);
		
		if(isset($_REQUEST['sub']) && $_REQUEST['sub'] != '')
		{
			switch($_REQUEST['sub'])
			{
				case 'del':
					$GLOBALS['db']->RowQuery("DELETE FROM " . PREFIX . "_ignorierliste WHERE BenutzerId = '" . UID . "' AND IgnorierId = '" . $_REQUEST['id'] . "'");
					koobi_message('Ignorelist', 'Ignorelist_DelMsg', $_SERVER['HTTP_REFERER']);
				break;
				
				case 'add':
					$user  = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9. ]*)/', '', @trim($_REQUEST['UserName']));
					$reason = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9. ]*)/', '', @trim($_REQUEST['Reason']));
					
					$check =  $GLOBALS['db']->RowQuery("SELECT Id FROM " . PREFIX . "_Benutzer WHERE Benutzername='".$user."' AND Id!='".UID."'");
					if(is_object($check ))
					{
						$check_exists =  $GLOBALS['db']->RowQuery("SELECT IgnorierId FROM " . PREFIX . "_ignorierliste WHERE BenutzerId='".UID."' AND IgnorierId='".$check->Id."'");
						if(!is_object($check_exists))
						{
							$GLOBALS['db']->RowQuery("INSERT INTO " . PREFIX . "_ignorierliste (BenutzerId,IgnorierId,Grund,Datum) VALUES ('" . UID . "', '" . $check->Id. "', '".$reason."', '" . stime() . "')");
						}
						refer('index.php?p=forum&action=ignorelist');
					} else {
						refer('index.php?p=forum&action=ignorelist');
					}
					
					
				break;
				
				case 'del_multi':
					if(isset($_POST['del']) && $_POST['del'] >= 1)
					{
						foreach($_POST['del'] as $id => $key)
						{
							$GLOBALS['db']->RowQuery("DELETE FROM " . PREFIX . "_ignorierliste WHERE BenutzerId = '" . UID . "' AND IgnorierId = '" . $id . "'");
						}
						refer('index.php?p=forum&action=ignorelist');
					}
				break;
			}
		}
		
		$data = array();
		$sql = $GLOBALS['db']->Query("
		SELECT 
			a.BenutzerId,
			a.IgnorierId,
			a.Grund,
			a.Datum,
			b.Gruppe,
			b.Benutzername,
			b.Avatar,
			b.Avatar_Default   
		FROM 
			" . PREFIX . "_ignorierliste as a,
			" . PREFIX . "_benutzer as b 
		WHERE 
			a.BenutzerId = '" . $_SESSION['benutzer_id'] . "' 
		 AND
			b.Id = a.IgnorierId 
		ORDER BY 
			Benutzername ASC");
		while($row = $sql->fetchrow())
		{	
			$row->Avatar = getAvatar($row->Gruppe,$row->Avatar,$row->Avatar_Default);
			$row->UserPop = userDropDownPop($row->IgnorierId,base64_encode($row->Benutzername));
			array_push($data, $row);
		}
		
		$GLOBALS['tmpl']->assign('data', $data);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Ignorelist']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/ignorelist.tpl'));
	break;
	
	//=================================================================
	// Im Forum suchen
	//=================================================================
	case 'search':
		$pattern = ($_REQUEST['pattern'] != '') ? addslashes($_REQUEST['pattern']) : addslashes($_REQUEST['pattern']);
		$f_id = $_REQUEST['f_id'];
		$t_id = $_REQUEST['t_id'];
		
		$permissions = getForumPermissions($f_id, UGROUP);
		
		if ($pattern == '') 
		{
			//=================================================================
			// es wurde kein suchbegriff eingegeben
			//=================================================================
			if ($f_id == 0 && $f_id != "") {
				koobi_message('Forums_Title', 'Forums_ErrorNoPattern', HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
			} elseif ($f_id != "") {
				koobi_message('Forums_Title', 'Forums_ErrorNoPattern', HOME_PATH . '/index.php?p=showforum&amp;fid=' . $f_id);
			} elseif ($t_id != "") {
				koobi_message('Forums_Title', 'Forums_ErrorNoPattern', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $t_id);
			}
		} else {
			$matches = array();
			//=================================================================
			// passende topics finden
			//=================================================================
			$r_topic = $GLOBALS['db']->Query("
				SELECT
					t.id,
					t.forum_id,
					t.title,
					t.status,
					t.type,
					t.datum,
					t.views,
					t.posticon,
					t.uid,
					t.replies,
					u.uname,
					r.rating,
					f.title AS f_title
				FROM
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_user AS u,
					" . PREFIX . "_f_rating AS r,
					" . PREFIX . "_f_forum AS f
				WHERE
					t.title LIKE '%$pattern%' AND
					u.uid = t.uid AND
					r.topic_id = t.id AND
					f.id = t.forum_id
			");
			
			while ($topic = $r_topic->fetchrow())
			{
				$topic->link = "index.php?p=showtopic&amp;toid=" . $topic->id . "&amp;high=$_REQUEST[pattern]";
				$topic->closelink = "index.php?p=closetopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
				$topic->openlink = "index.php?p=opentopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
				$topic->dellink = "index.php?p=deltopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
				$topic->movelink = "index.php?p=move&amp;item=t&amp;id=$topic->id";
				$topic->autorlink = "index.php?p=user&amp;id=$topic->uid";
				$topic->autor = $topic->uname;
				
				$rating = @explode(',', $topic->rating);
				$topic->rating = (int) (array_sum($rating) / count($rating));
				
				$forum = $GLOBALS['db']->RowQuery("SELECT id, status FROM " . PREFIX . "_f_forum WHERE id = '" . $topic->forum_id . "'");
				
				
				if ($topic->status == FORUM_STATUS_MOVED) {
					$topic->statusicon = getIcon('thread_moved.png', 'moved');
				} else {
					if (ISLOGGED == 0 || ($forum->status == FORUM_STATUS_CLOSED) )
					{
						// nicht eingeloggt oder forum geschlossen
						$topic->statusicon = getIcon('thread_lock.png', $GLOBALS['config_vars']['Forums_Icons_TopicClosed']);
					} else {
						setTopicIconObj($topic, $forum);
					}
				}
				
				array_push($matches, $topic);
			}
			
			$q_post = "
				SELECT
					p.id,
					p.title,
					p.topic_id,
					p.datum,
					p.uid,
					p.message,
					u.name,
					u.uname
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_user AS u
				WHERE
					(p.title LIKE '%$pattern%' OR p.message LIKE '%$pattern%') AND
					u.uid = p.uid
			";
			
			$r_post = $GLOBALS['db']->Query($q_post);
			
			while ($post = $r_post->fetchrow()) {
				$q_topic = "
					SELECT
						t.id,
						t.title,
						t.status,
						t.type,
						t.datum,
						t.views,
						r.rating,
						t.posticon,
						t.uid,
						t.replies,
						u.uname
					FROM
						" . PREFIX . "_f_topic AS t,
						" . PREFIX . "_user AS u,
						" . PREFIX . "_f_rating AS r
					WHERE
						id = " . $post->topic_id . " AND 
						u.uid = t.uid AND 
						r.topic_id = t.id
					";
				
				$r_topic = $GLOBALS['db']->Query($q_topic);
				
				while ($topic = $r_topic->fetchrow())
				{
					$topic->link = "index.php?p=showtopic&amp;toid=" . $topic->id;
					$topic->closelink = "index.php?p=closetopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
					$topic->openlink = "index.php?p=opentopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
					$topic->dellink = "index.php?p=deltopic&amp;fid=$_REQUEST[f_id]&amp;toid=$topic->id";
					$topic->movelink = "index.php?p=move&amp;item=t&amp;id=$topic->id";
					$topic->autorlink = "index.php?p=user&amp;id=$topic->uid";
					$topic->autor = $topic->uname;
					
					$rating = @explode(",", $topic->rating);
					
					$topic->rating = (int) (array_sum($rating) / count($rating));
					
					array_push($matches, $topic);
				}
			}
			
			if ($f_id != "") {
				$navi_id = $f_id;
				$type = "forum";
			} else {
				$navi_id = $t_id;
				$type = "topic";
			}
			
			//=================================================================
			//  seitennavigation
			//=================================================================
			$order_by = ($_REQUEST["sortby"] != "") ? addslashes($_REQUEST['sortby']) : "datum";
			$order = ($order != "") ? $order : "DESC";
			$num = count($matches);
			$limit = $_REQUEST['pp'];
			
			$page = (!isset($page)) ? 1 : $page;
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			if ($limit < $num)
			{
				$GLOBALS['tmpl']->assign('pages', pagenav($seiten, $GLOBALS['config_vars']['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=forum&amp;action=search&amp;pattern=$pattern&amp;f_id=$f_id&amp;t_id=$t_id&amp;pp=$limit&amp;page={s}\">{t}</a> "));
			}
			
			$navigation = getNavigation($navi_id, $type);
			
			$stitle = $GLOBALS["config_vars"]["Forums_Title"];
			$sname = str_replace($GLOBALS['config_vars']['Forums_Splitter'], " - ", strip_tags($navigation)) . " - " . $GLOBALS['config_vars']['Forums_Link_search'];
			
			$GLOBALS['tmpl']->assign('navigation', $navigation);
			
			$GLOBALS['tmpl']->assign('matches', array_slice($matches, $a, $limit));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/result.tpl')); 
		}
	break;
	
	//=================================================================
	// Suchmaske
	//=================================================================
	case 'search_mask':
		$forums_dropdown = array();
		getForums(0, $forums_dropdown, '');
		$GLOBALS['tmpl']->assign('navigation', '<a class=\"forums_link_navi\" href=\"index.php?p=showforums\">' . $GLOBALS['config_vars']['Forums_Link_forums'] .'</a>');
		$GLOBALS['tmpl']->assign('forums_dropdown', $forums_dropdown);
		$GLOBALS['tmpl']->assign('pagetitle_html',$stitle);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/search_mask.tpl')); 
	break;
	
	//=================================================================
	// Suchergebnisse zur Suchmaske
	//=================================================================
	case 'xsearch':
		// es wurde kein suchbegriff eingegeben
		if(@strlen($_REQUEST['pattern']) < 2 && @strlen($_REQUEST['user_name']) < 2)
		{
			koobi_message('Forums_Title', 'Forums_ErrorNoPattern', HOME_PATH . '/index.php?p=forum&amp;action=search_mask');
		}
		
		//=================================================================
		// typ des topics
		//=================================================================
		$type = "1";
		if(isset($_REQUEST['type']) && $_REQUEST['type'] != '')
		{
			switch ($_REQUEST['type']) {
				case 1:
					$type = "t.type = " . TOPIC_TYPE_STICKY;
				break;
				case 2:
					$type = "t.type = " . TOPIC_TYPE_ANNOUNCE;
				break;
				case 3:
					$type = "t.status = " . FORUM_STATUS_MOVED;
				default:
				break;
			}
		}
		
		$_REQUEST['pattern'] = eregi_replace('[^-A-Za-zÀ-ÖØ-öø-ÿ0-9 ]', '', $_REQUEST['pattern']);
		
		//=================================================================
		// art der suche
		//=================================================================
		$search_type = 'REGEXP';
		$search_prefix = '';
		
		$search_type = (isset($_REQUEST['regexp']) && $_REQUEST['regexp'] == 1) ? 'REGEXP' : 'LIKE';
		$search_prefix = (isset($_REQUEST['regexp']) && $_REQUEST['regexp'] == 1) ? '' : '%';
		
		//=================================================================
		// schluesselwort
		//=================================================================
		$pattern_or = @explode(' or ', strtolower($_REQUEST['pattern']));
		$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);
				
				//=================================================================
				// die gesamten beitraege durchsuchen
				//=================================================================
				if ($_REQUEST['search_post'] == 1) 
				{
					$sub_pattern[] = "
					(
					p.title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
					p.message $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
					t.title $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
					)";
					// nur den beitrag durchsuchen
				} else {
					$sub_pattern[] = "
					(
					p.title $search_type '" . $search_prefix . $sub_part . $search_prefix . "' OR 
					t.title $search_type '" . $search_prefix . $sub_part . $search_prefix . "'
					)";
				}
			}
			$pattern_tmp = @implode(' AND ', $sub_pattern);
			$p_and_array[] = $pattern_tmp;
		}
		
		$pattern = @implode(' OR ', $p_and_array);
		
		//=================================================================
		// sortieren nach
		//=================================================================
		$order_by = "t.title";
		
		if(isset($_REQUEST['search_sort']) && $_REQUEST['search_sort'] != '')
		{
			switch ($_REQUEST['search_sort'])
			{
				case 1:
					$order_by = "t.title";
				break;
				case 2:
					$order_by = "t.replies";
				break;
				case 3:
					$order_by = "author";
				break;
				case 4:
					$order_by = "forum";
				break;
				case 5:
					$order_by = "views";
				break;
				case 6:
					$order_by = "datum";
				break;
			}
		}
		//=================================================================
		// sortieren
		//=================================================================
		$order = (isset($_REQUEST['ascdesc']) && $_REQUEST['ascdesc'] != '') ? $_REQUEST['ascdesc'] : 'ASC';
		
		// nach benutzer suchen
		$search_by_user = "(1)";
		
		if (isset($_REQUEST['user_name']) && $_REQUEST['user_name'] != "")
		{
			$_REQUEST['user_name'] = eregi_replace('[^-A-Za-zÀ-ÖØ-öø-ÿ0-9 ]', '', $_REQUEST['user_name']);
			// exakte suche
			if ($_REQUEST['user_opt'] == 1)
			{
				$search_by_user = "(u.Benutzername LIKE '" . $_REQUEST['user_name'] . "')";
			} elseif ($_REQUEST['user_opt'] == 0) {
				$search_by_user = "(u.Benutzername LIKE '%" . $_REQUEST['user_name'] . "%')";
			}
		}
		//=================================================================
		// suche in foren
		//=================================================================
		$search_in_forums = "(1)";
		
		//=================================================================
		// Dummy-Array
		//=================================================================
		$_REQUEST['search_in_forums'] = (isset($_REQUEST['search_in_forums']) && $_REQUEST['search_in_forums'] != '' || (isset($_REQUEST['search_in_forums']) && $_REQUEST['search_in_forums'] != 'Array')) ? '' : array('0','') ;
		
		
		if ($_REQUEST['search_in_forums'] != "") {
			//=================================================================
			// foren die zu durchsuchen sind durchgehen
			// und die foren rausfiltern die die gruppe
			// nicht durchsuchen kann
			//=================================================================
			$allowed_forums = array();
			
			//=================================================================
			// wenn alle foren durchsucht werden sollen,
			// muessen zunaechst die foren ins array geschoben
			// werden, die auch durchsucht werden duerfen
			//=================================================================
			if (in_array(0, $_REQUEST['search_in_forums'])) 
			{
				$q_all_forums = "SELECT id FROM " . PREFIX . "_f_forum";
				$r_all_forums = $GLOBALS['db']->Query($q_all_forums);
				
				while ($one_forum = $r_all_forums->fetchrow()) 
				{
					$f_perms = getForumPermissions($one_forum->id, UGROUP);
					if ( ($f_perms[FORUM_PERMISSION_CAN_SEARCH_FORUM] == 1) && ($f_perms[FORUM_PERMISSION_CAN_SEE] == 1) ) 
					{
						$allowed_forums[] = $one_forum->id;
					}
					$f_perms = "";
				}
				//=================================================================
				// ansonsten gehe alle ausgewaehlten foren durch und
				// ueberpruefe die berechtigung
				//=================================================================
			} else { 
				foreach ($_REQUEST['search_in_forums'] as $forum)
				{
					$f_perms = getForumPermissions($forum, UGROUP);
					if ( ($f_perms[FORUM_PERMISSION_CAN_SEARCH_FORUM] == 1) && ($f_perms[FORUM_PERMISSION_CAN_SEE] == 1) ) 
					{
						$allowed_forums[] = $forum;
					}
				}
			}
			
			$search_in_forums = "(f.id = " . @implode(' OR f.id = ', $allowed_forums) . ")";	
		}
		
		//=================================================================
		// suche nach dem datum
		//=================================================================
		$search_by_date = "(1)";
		$date_comparator = (@$_REQUEST['b4after'] == 0) ? ' <= ' : ' >= ';
		
		//=================================================================
		// Tage
		//=================================================================
		$divisor = 60 * 60 * 24;
		$search_by_date = (@$_REQUEST['date'] == 0) ? "(1)" : "((UNIX_TIMESTAMP(NOW()) / $divisor - (UNIX_TIMESTAMP(t.datum) / $divisor)) $date_comparator " . $_REQUEST['date'] . ")";
		
		$query = "
			SELECT DISTINCT
				t.id,
				t.forum_id,
				t.title,
				t.replies,
				t.views,
				t.type,
				t.datum,
				t.status,
				t.posticon,
				t.uid,
				r.rating,
				f.status AS f_status,
				f.title AS f_title,
				u.Benutzername AS autor,
				f.title AS forum,
				t.opened,
				p.opened
			FROM
				" . PREFIX . "_f_topic AS t,
				" . PREFIX . "_f_post AS p,
				" . PREFIX . "_f_forum AS f,
				" . PREFIX . "_benutzer AS u,
				" . PREFIX . "_f_rating AS r
			WHERE
				(" . $pattern . ") AND
				(" . $type . ") AND
				(t.id = p.topic_id AND t.forum_id = f.id AND u.Id = t.uid AND r.topic_id = t.id) AND 
				f.active = 1 AND
				$search_by_user AND
				$search_in_forums AND
				$search_by_date
			ORDER BY
				t.type DESC,
				" . $order_by . " " . $order;
	
		$result = $GLOBALS['db']->Query($query);
		$matches = array();
		
		while ($hit = $result->fetcharray())
		{
			// freizuschaltende beitrage nicht beruecksichtigen!
			if(is_mod($hit['forum_id']) || ($hit['opened'] == 1) )
			{
				$q_forum = "SELECT id, status FROM " . PREFIX . "_f_forum WHERE id = " . $hit['forum_id'];
				$r_forum = $GLOBALS['db']->Query($q_forum);
				$forum = $r_forum->fetchrow();
				
				$rating_array = @explode(",", $hit->rating);
				$hit['rating'] = (int) ( array_sum($rating_array) / count($rating_array) );
				
				$hit['link'] = "index.php?p=showtopic&amp;toid=" . $hit['id'];
				
				if ($hit['status'] == FORUM_STATUS_MOVED)
				{
					$hit['statusicon'] = getIcon("thread_moved.png", "moved");
				} else {
					if (ISLOGGED == 0 || ($hit['f_status'] == FORUM_STATUS_CLOSED) ) {
						// nicht eingeloggt oder forum geschlossen
						$hit['statusicon'] = getIcon("thread_lock.png", $GLOBALS['config_vars']['Forums_Icons_TopicClosed']);
					} else {
						setTopicIcon($hit, $forum);
					}
				}
				
				$hit['autorlink'] = "index.php?p=user&amp;id=" . $hit['uid'];
				
				$matches[] = $hit;
			}
		}
		//=================================================================
		// wenn keinen ergebnisse gefunden wurden...
		//=================================================================
		if(!$matches)
		{
			koobi_message('Forums_NoMatchesTitle', 'Forums_NoMatchesBody', HOME_PATH . '/index.php?p=forum&amp;action=search_mask');
		}
		
		//=================================================================
		// seitennavigation
		//=================================================================
		$page_order_by = (isset($_REQUEST["sortby"]) && $_REQUEST["sortby"] != "") ? $_REQUEST["sortby"] : "datum";
		$page_order = ($order != "") ? $order : "DESC";
		$num = count($matches);
		$limit = getLim(15);
		$page = (!isset($page)) ? 1 : $page;
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		if ($limit < $num)
		{
			$GLOBALS['tmpl']->assign('pages', pagenav($seiten, $GLOBALS['config_vars']['aktpos'], 
			" <a class=\"page_navigation\" href=\"index.php?p=forum&amp;action=xsearch" . 
			"&amp;type=" . $_REQUEST['type'] . 
			"&amp;pattern=" . $_REQUEST['pattern'] . 
			"&amp;user_name=" . $_REQUEST['user_name'] . 
			"&amp;search_in_forums=" . $_REQUEST['search_in_forums'] . 
			"&amp;user_opt=" . $_REQUEST['user_opt'] . 
			"&amp;search_post=" . $_REQUEST['search_post'] . 
			"&amp;date=" . $_REQUEST['date'] . 
			"&amp;b4after=" . $_REQUEST['b4after'] . 
			"&amp;search_sort=" . $_REQUEST['search_sort'] . 
			"&amp;ascdesc=" . $_REQUEST['ascdesc'] . 
			"&amp;pp=" . $limit . 
			"&amp;page={s}\">{t}</a> "));
		}
		
		$GLOBALS['tmpl']->assign('matches', array_slice($matches, $a, $limit));
		$GLOBALS['tmpl']->assign('navigation', "<a class='Forums_Link_navi' href='index.php?p=showforums'>" . $GLOBALS['config_vars']['Forums_Link_forums'] . "</a>");
		$GLOBALS['tmpl']->assign('matches_count', $result->numrows());
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_ForumsSearch']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/result.tpl')); 
	break;

	//=================================================================
	// Den Typ des Themas aendern
	//=================================================================
	case 'change_type':
		$f_id = $_REQUEST['fid'];
		$permissions = getForumPermissionsByUser($f_id, UID);
		
		if ($permissions[FORUM_PERMISSIONS_CAN_CHANGE_TOPICTYPE] == 0)
		{
			koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
		} else {
			$topic  = $GLOBALS['db']->RowQuery("SELECT id, title, type FROM " . PREFIX . "_f_topic WHERE id = '" . $_REQUEST['id'] . "'");
			$GLOBALS['tmpl']->assign('topic', $topic);
			$GLOBALS['tmpl']->assign('navigation', getNavigation($_REQUEST["id"], "topic"));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/change_type.tpl')); 
		}
	break;
	
	//=================================================================
	// Den Typ des Themas uebernehmen
	//=================================================================
	case 'save_type':
		$f_id = ($_REQUEST['f_id'] != "") ? $_REQUEST['f_id'] : $_REQUEST['f_id'];
		$permissions = getForumPermissionsByUser($f_id, UID);
		
		if ( (ISLOGGED != 1) ||  ($permissions[FORUM_PERMISSIONS_CAN_CHANGE_TOPICTYPE] == 0) )
		{
			koobi_message('Global_NoPermission','Global_NoPermission_t', HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
		} else {
			$q_topic_type = "UPDATE " . PREFIX . "_f_topic SET type = " . $_REQUEST["type"] . " WHERE id = " . $_REQUEST["t_id"];
			$r_topic_type = $GLOBALS['db']->Query($q_topic_type);
			koobi_message('Forums_Title','Forums_ChangeTypePost_message', HOME_PATH . '/index.php?p=showforum&amp;fid=' . $_REQUEST['f_id']);
		}
	break;
	
	//=================================================================
	// Abstimmen
	//=================================================================
	case 'rating':
		$forum = $GLOBALS['db']->RowQuery("SELECT f.id FROM " . PREFIX . "_f_forum AS f, " . PREFIX . "_f_topic AS t WHERE t.id = '" . $_REQUEST['t_id'] . "' AND t.forum_id = f.id");
		$permissions = getForumPermissionsByUser($forum->id, UID);
		
		if ($permissions[FORUM_PERMISSION_CAN_RATE_TOPIC] == 0)
		{
			koobi_message('Global_NoPermission', 'Global_NoPermission_t', HOME_PATH . '/index.php?p=showforums&amp;area=' . AREA);
		} else {		
			$q_sel_rating = "SELECT uid, rating, ip FROM " . PREFIX . "_f_rating WHERE topic_id = " . $_REQUEST['t_id'];
			$r_sel_rating = $GLOBALS['db']->Query($q_sel_rating);
			
			$rating = $r_sel_rating->fetchrow();
			$r_uid = @explode(",", $rating->uid);
			$ip = @explode(",", $rating->ip);
			$v_uid = (@is_numeric(UID)) ? UID : '';
			
			$_REQUEST['rating'] = abs((int)$_REQUEST['rating']);
			if($_REQUEST['rating']<1)
				$_REQUEST['rating'] = 1;
			else if($_REQUEST['rating']>5)
				$_REQUEST['rating'] = 5;
			
			if(UID != 0 || UID != "UID" || UID != "")
			{
				// wenn user eingeloggt ist und kein gast abstimmen darf
				if (!in_array(UID, $r_uid))
				{
					// wenn noch niemand bewertet hat
					if ($rating->rating == "")
					{
						$q_rating = "
							UPDATE
								" . PREFIX . "_f_rating
							SET
								rating = '" . $_REQUEST['rating'] . "',
								ip = '" . $_SERVER['REMOTE_ADDR'] . "',
								uid = '" . $v_uid . "'
							WHERE
								topic_id = " . $_REQUEST['t_id'];
					} else {
						$q_rating = "
							UPDATE
								" . PREFIX . "_f_rating
							SET
								rating = CONCAT(rating, ',', '" . $_REQUEST['rating'] . "'),
								uid = CONCAT(uid, ',', '" . $v_uid . "'),
								ip = CONCAT(ip, ',', '" . $_SERVER['REMOTE_ADDR'] . "')
							WHERE
								topic_id = " . $_REQUEST['t_id'];
					}
					
					$r_rating = $GLOBALS['db']->Query($q_rating);
				}
				// ansonstenn nicht angemeldete benutzer
			} else {
				
				// wenn keine uid gloabl ist (gast)
				if (!in_array($_SERVER['REMOTE_ADDR'], $ip)) {
					if ($rating->rating == "") {
						$q_rating = "
						UPDATE
							" . PREFIX . "_f_rating
						SET
							rating = '" . $_REQUEST['rating'] . "',
							ip = '" . $_SERVER['REMOTE_ADDR'] . "',
							uid = '" . $v_uid . "'
						WHERE
							topic_id = " . $_REQUEST['t_id'];
					} else {
						$q_rating = "
						UPDATE
							" . PREFIX . "_f_rating
						SET
							rating = CONCAT(rating, ',', '" . $_REQUEST['rating'] . "'),
							ip = CONCAT(ip, ',', '" . $_SERVER['REMOTE_ADDR'] . "'),
							uid = CONCAT(uid, ',', '" . $v_uid . "'),
						WHERE
							topic_id = " . $_REQUEST['t_id'];
					}
					
					$r_rating = $GLOBALS['db']->Query($q_rating);
				}
			} // ende wenn gast abstimmen darf
			koobi_message('Forums_rating_thread_message', 'Forums_VoteThankyou', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['t_id']);
		}
	
	break;
	
	//=================================================================
	// Thema abonnieren / Abo abbestellen
	//=================================================================
	case 'addsubscription':
		if($_REQUEST['t_id'] >= 1)
		{
			$r_subscriptions = $GLOBALS['db']->Query("SELECT notification FROM " . PREFIX . "_f_topic WHERE id = '" . escs($_REQUEST['t_id']) . "'");
			$subscriptions = $r_subscriptions->fetchrow();
			$user_id = split(",", $subscriptions->notification);
			if (!in_array(UID, $user_id))$r_subscribe = $GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_topic SET notification = CONCAT(notification, ';', '" . UID . "') WHERE id = '" . escs($_REQUEST['t_id']) . "'");
		}
		koobi_message('Forums_AboMessage', 'Forums_TopicAboOk', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['t_id']);
	break;
	
	case 'unsubscription':
		if($_REQUEST['t_id'] >= 1)
		{
			$sql = $GLOBALS['db']->Query("SELECT notification FROM " . PREFIX . "_f_topic WHERE id = '" . $_REQUEST['t_id'] . "'");
			$row = $sql->fetchrow();
			$new_insert = str_replace(";" . UID, "", $row->notification);
			$sql = $GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_topic set notification='$new_insert' WHERE id = '" . $_REQUEST['t_id'] . "'");
		}
		koobi_message('Forums_AboMessage', 'Forums_TopicAboEndOk', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['t_id']);
	break;
	
	
	
	//=================================================================
	// Aktive Themen der ausgewaehlten Zeitspanne anzeigen
	//=================================================================
	case 'show':
		$unit = (!$_REQUEST['unit']) ? 'h' : $_REQUEST['unit'];
		$period = (!$_REQUEST['period']) ? 24 : $_REQUEST['period'];
		$period = ($period > 72) ? 72 : $period;
		$fid = $_REQUEST['fid'];
		
		$forum_stat = ($fid != "") ? " AND t.forum_id = $fid" : "";
		$sort = (isset($_REQUEST['sort']) && $_REQUEST['sort'] != "") ? $_REQUEST['sort'] : "ASC";
		
		switch ($unit) 
		{
			// Stunden
			case 'h':
				$divisor = 60 * 60;
				$where_time_stat = "((UNIX_TIMESTAMP(NOW()) / $divisor) - (UNIX_TIMESTAMP(p.datum) / $divisor)) <= $period";
			break;
			
			// Tage
			case 'd':
				$divisor = 60 * 60 * 24;
				$where_time_stat = "((UNIX_TIMESTAMP(NOW()) / $divisor) - (UNIX_TIMESTAMP(p.datum) / $divisor)) <= $period";
			break;
			
			// Monat
			case 'm':
				$divisor = 60 * 60 * 24 * 30;
				$where_time_stat = "((UNIX_TIMESTAMP(NOW()) / $divisor) - (UNIX_TIMESTAMP(p.datum) / $divisor)) <= $period";
			break;
			
			case 'all':
				$where_time_stat = "1";
			break;
		}
		
		$q_last_active = "
			SELECT DISTINCT
				t.id,
				t.forum_id,
				t.title,
				t.status,
				t.type,
				t.datum,
				t.views,
				t.posticon,
				t.uid,
				t.replies,
				u.Benutzername as uname,
				r.rating,
				f.group_id,
				f.title AS f_title	
			FROM
				" . PREFIX . "_f_topic AS t,
				" . PREFIX . "_benutzer AS u,
				" . PREFIX . "_f_rating AS r,
				" . PREFIX . "_f_post AS p,
				" . PREFIX . "_f_forum AS f
			WHERE
				$where_time_stat AND
				u.Id = t.uid AND
				r.topic_id = t.id AND
				p.topic_id = t.id AND
				f.id = t.forum_id
				$forum_stat
			ORDER BY
				t.datum
				$sort 
		";
		
		$r_last_active = $GLOBALS['db']->Query($q_last_active);
		$matches = array();
		
		//=================================================================
		// MISC ID FUER DEN AKTUELLEN BENUTZER
		//=================================================================
		$group_ids_misc = groupIdsMisc();
	
		while ($topic = $r_last_active->fetcharray())
		{
			$forum = $GLOBALS['db']->RowQuery("SELECT id, status FROM " . PREFIX . "_f_forum WHERE id = '" . $topic['id'] . "'");
			// GRUPPEN ARRAY
			$group_ids = explode(",", $topic['group_id']);
	
			// DIESE ID BENOETIGEN WIR UM DIE RECHTE ZU HOLEN
			$forum_id = $topic['forum_id'];
			
			if (array_intersect($group_ids_misc, $group_ids))
			{
				foreach($group_ids_misc as $gids_misc)
				{
					if(in_array($gids_misc, $group_ids))
					{
						$permissions = getForumPermissions($forum_id, $gids_misc);
						 
						if ($permissions[FORUM_PERMISSION_CAN_SEE_TOPIC] == 1)
						{
							//=================================================================
							// HIER WERDEN NUR THEMEN ANGEZEIGT, DIE AUS EINEM FORUM STAMMEN, FÜR DAS DER BENUTZER
							// AUCH DIE RECHTE BESITZT
							//=================================================================
							$topic['autorlink'] = "index.php?p=user&amp;id=$topic[uid]";
							$topic['autor'] = $topic['uname'];
							$topic['link'] = "index.php?p=showtopic&amp;toid=$topic[id]";
							
							$rating = @explode(",", $topic['rating']);
							$topic['rating'] = (int) (array_sum($rating) / count($rating));
							
							//=================================================================
							// Anzahl der Postings ermitteln
							// Als limit fuer die maximale anzahl der postings auf
							// einer seite wird 15 als standardwert genommen
							//=================================================================
							$limit = 50;
							
							$r_post = $GLOBALS['db']->Query("SELECT COUNT(id) as count FROM " . PREFIX . "_f_post WHERE topic_id = '" . $topic['id'] . "'");
							$post = $r_post->fetchrow();
							
							$count = (($post->count / $limit) > ((int) ($post->count / $limit))) ? ((int) ($post->count / $limit)) + 1 : ((int) ($post->count / $limit));
							
							$topic['navigation_count'] = ($count == 1) ? 0 : $count;
							
							// Forum zum thema
							$r_forum = $GLOBALS['db']->Query("SELECT id, status FROM " . PREFIX . "_f_forum WHERE id = '".$topic['forum_id']."'");
							
							if ($topic['status'] == FORUM_STATUS_MOVED)
							{
								$topic['statusicon'] = getIcon("thread_moved.png", "moved");
							} else {
								if (ISLOGGED == 0 || (@$forum->status == FORUM_STATUS_CLOSED || $topic['status'] == FORUM_STATUS_CLOSED) ) //
								{
									// nicht eingeloggt oder forum geschlossen
									$topic['statusicon'] = getIcon("thread_lock.png", $GLOBALS['config_vars']['Forums_Icons_TopicClosed']);
								} else {
									setTopicIcon($topic, $forum);
								}
							}
							array_push($matches, $topic);
						}
					}
				}
			}
		}
		
		$GLOBALS['tmpl']->assign('matches', $matches);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_ShowLastActive']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/result.tpl')); 
	break;
	
	
	//=================================================================
	//=================================================================
	case 'print':
		switch ($_REQUEST['what']) 
		{
			case 'subscription':
			if (is_numeric(UID)) 
			{
				$f_id = ($_REQUEST['id'] == "") ? "t.forum_id" : "'" . $_REQUEST['id'] . "'";
				
				$query = "
					SELECT
						t.id,
						t.title,
						t.forum_id,
						t.views,
						t.type,
						t.status,
						t.posticon,
						t.rating,
						t.uid,
						f.title AS f_title,
						t.notification,
						t.replies,
						t.status,
						u.Benutzername as uname
					FROM
						" . PREFIX . "_f_topic AS t,
						" . PREFIX . "_f_forum AS f,
						" . PREFIX . "_benutzer AS u
					WHERE
						f.id = $f_id AND
						t.forum_id = f.id AND
						u.Id = t.uid";
				
				$result = $GLOBALS['db']->Query($query);
				$matches = array();
				
				
				
				while ($topic = $result->fetcharray()) 
				{
					$forum = $GLOBALS['db']->RowQuery("SELECT id, status FROM " . PREFIX . "_f_forum WHERE id = '" . $topic['id'] . "'");
					
					$notification = @explode(';', $topic['notification']);
					
					if (in_array(UID, $notification)) 
					{
						if ($topic['status'] == FORUM_STATUS_MOVED) 
						{
							$topic['statusicon'] = getIcon("thread_moved.png", "moved");
						} else {
							if (ISLOGGED == 0 || ($topic['status'] == FORUM_STATUS_CLOSED) ) 
							{
								// nicht eingeloggt oder forum geschlossen
								$topic['statusicon'] = getIcon("thread_lock.png", $GLOBALS['config_vars']['Forums_Icons_TopicClosed']);
							} else {
								setTopicIcon($topic, $forum);
							}
						}
						
						$topic['autorlink'] = "index.php?p=user&amp;id=" . $topic['uid'];
						$topic['link'] = "index.php?p=showtopic&amp;toid=$topic[id]&amp;fid=$topic[forum_id]";
						$topic['autor'] = $topic['uname'];
						$rating = @explode(",", $topic['rating']);
						$topic['rating'] = (int) (array_sum($rating) / count($rating));
						
						$matches[] = $topic;
					}
				}
				
				$GLOBALS['tmpl']->assign('matches', $matches);
				$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_ShowAllAbos']);
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/result.tpl')); 
			}
		
		break;
		
		case 'posting':
		if ("" != $_REQUEST['id'])
		{
			$limit = getLim(15);
			
			$query = "
				SELECT
					p.id,
					p.title,
					p.topic_id,
					p.datum,
					p.use_bbcode,
					p.use_smilies,
					p.use_sig,
					p.message,
					p.attachment,
					f.id AS forum_id,
					f.title AS forum_title,
					t.title AS topic_title,
					u.Benutzername as uname
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_f_forum AS f,
					" . PREFIX . "_benutzer AS u
				WHERE
					p.uid = " . $_REQUEST['id'] . " AND
					u.Id = p.uid AND
					t.id = p.topic_id AND
					t.forum_id = f.id AND
					f.active = 1
				ORDER BY datum DESC
			";
			
			$result = $GLOBALS['db']->Query($query);
			$num = $result->numrows();
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$query2 = "
				SELECT
					p.id,
					p.title,
					p.topic_id,
					p.datum,
					p.use_bbcode,
					p.use_smilies,
					p.use_sig,
					p.message,
					p.attachment,
					f.id AS forum_id,
					f.title AS forum_title,
					t.title AS topic_title,
					u.Benutzername as uname
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_f_forum AS f,
					" . PREFIX . "_benutzer AS u
				WHERE
					p.uid = " . $_REQUEST['id'] . " AND
					u.Id = p.uid AND
					t.id = p.topic_id AND
					t.forum_id = f.id AND
					f.active = 1
				ORDER BY
					datum DESC
					LIMIT $a,$limit
			";
			$result = $GLOBALS['db']->Query($query2);
			$matches = array();
			
			while ($post = $result->fetchrow())
			{
				$post->topic_title_raw = $post->topic_title;
				$post->datum = strtotime($post->datum);
				$permissions = getForumPermissionsByUser($post->forum_id, UID);
				if (@$permissions[FORUM_PERMISSION_CAN_SEE_TOPIC] == 1)
				{
					// soll bbcode verwendet werden
					if ($post->use_bbcode == 1) 
					{
						$post->message = kcodes($post->message);
					} else {
						$post->message = nl2br($post->message);
					}
					
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$matches[] = $post;
				} else {
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$post->message = "denied";
					$matches[] = $post;
				}
				
			}
			
			$stitle = $GLOBALS['config_vars']['Forums_ShowPostUser'];
			$sname = $stitle;
			
			if ($limit < $num)
			{
				$id = (!is_numeric($_REQUEST['id'])) ? 1 : $_REQUEST['id'];
				$GLOBALS['tmpl']->assign('pages', pagenav($seiten, $GLOBALS['config_vars']['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=forum&amp;action=print&amp;what=posting&amp;id=$id&amp;page={s}&amp;pp=$limit\">{t}</a> "));
			}
			
			$GLOBALS['tmpl']->assign("navigation", "<a href='index.php?p=showforums'>" . $GLOBALS['config_vars']['Forums_Header_forum'] . "</a>");
			$GLOBALS['tmpl']->assign("post_count", $num);
			$GLOBALS['tmpl']->assign("matches", $matches);
			$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_ShowOwnPosts']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/showpost.tpl')); 
		}
		break;
		
		case 'topicsempty':
			$limit = getLim(15);
			
			$query = "
				SELECT
					p.id,
					p.title,
					p.topic_id,
					p.datum,
					p.use_bbcode,
					p.use_smilies,
					p.use_sig,
					p.message,
					p.attachment,
					f.id AS forum_id,
					f.title AS forum_title,
					t.title AS topic_title
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_f_forum AS f
				WHERE 
					t.replies < 2 AND 
					t.id = p.topic_id AND
					t.forum_id = f.id AND
					f.active = '1'
				ORDER BY datum DESC
			";
			
			$result = $GLOBALS['db']->Query($query);
			$num = $result->numrows();
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$query2 = "
				SELECT
					p.id,
					p.title,
					p.uid AS Autor,
					p.topic_id,
					p.datum,
					p.use_bbcode,
					p.use_smilies,
					p.use_sig,
					p.message,
					p.attachment,
					f.id AS forum_id,
					f.title AS forum_title,
					t.title AS topic_title 
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_f_forum AS f 
				WHERE 
					t.replies < 2 AND 
					t.id = p.topic_id AND
					t.forum_id = f.id AND
					f.active = '1' 
				ORDER BY
					datum DESC
					LIMIT $a,$limit
			";
			$result = $GLOBALS['db']->Query($query2);
			$matches = array();
			
			while ($post = $result->fetchrow())
			{
				$post->topic_title_raw = $post->topic_title;
				$post->Uname = getUsername($post->Autor);
				$post->topic_title_raw = $post->topic_title;
				$post->datum = strtotime($post->datum);
				$permissions = getForumPermissionsByUser($post->forum_id, UID);
				if (@$permissions[FORUM_PERMISSION_CAN_SEE_TOPIC] == 1)
				{
					// soll bbcode verwendet werden
					if ($post->use_bbcode == 1) 
					{
						$post->message = kcodes($post->message);
					} else {
						$post->message = nl2br($post->message);
					}
					
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$matches[] = $post;
				} else {
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$post->message = "denied";
					$matches[] = $post;
				}
				
			}
			
			if ($limit < $num)
			{
				$id = (!is_numeric($_REQUEST['id'])) ? 1 : $_REQUEST['id'];
				$GLOBALS['tmpl']->assign('pages', pagenav($seiten, $GLOBALS['config_vars']['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=forum&amp;action=print&amp;what=topicsempty&amp;page={s}&amp;pp=$limit\">{t}</a> "));
			}
			
			$GLOBALS['tmpl']->assign("navigation", "<a href='index.php?p=showforums'>" . $GLOBALS['config_vars']['Forums_Header_forum'] . "</a>");
			$GLOBALS['tmpl']->assign("post_count", $num);
			$GLOBALS['tmpl']->assign("matches", $matches);
			$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_ThreadsEmpty']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/showpost_without_replies.tpl')); 
		break;
		
		
		
		case 'lastposts':
			$limit = 25;
			$result = $GLOBALS['db']->Query("
				SELECT
					p.id,
					p.title,
					p.uid AS Autor,
					p.topic_id,
					p.datum,
					p.use_bbcode,
					p.use_smilies,
					p.use_sig,
					p.message,
					p.attachment,
					f.id AS forum_id,
					f.title AS forum_title,
					t.title AS topic_title 
				FROM
					" . PREFIX . "_f_post AS p,
					" . PREFIX . "_f_topic AS t,
					" . PREFIX . "_f_forum AS f 
				WHERE
					t.id = p.topic_id AND
					t.forum_id = f.id AND
					f.active = 1
				ORDER BY
					datum DESC
					LIMIT $limit");
			
			$matches = array();
			
			while ($post = $result->fetchrow())
			{
				$post->topic_title_raw = $post->topic_title;
				$post->Uname = getUsername($post->Autor);
				$post->datum = strtotime($post->datum);
				$permissions = getForumPermissionsByUser($post->forum_id, UID);
				if (@$permissions[FORUM_PERMISSION_CAN_SEE_TOPIC] == 1)
				{
					// soll bbcode verwendet werden
					if ($post->use_bbcode == 1) 
					{
						$post->message = kcodes($post->message);
					} else {
						$post->message = nl2br($post->message);
					}
					
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$matches[] = $post;
				} else {
					$post->topic_title = ($post->title!='') ? $post->title :  substr(strip_tags($post->message),0,80) . '...';
					$post->message = "denied";
					$matches[] = $post;
				}
				
			}
			
			
			$GLOBALS['tmpl']->assign("navigation", "<a href='index.php?p=showforums'>" . $GLOBALS['config_vars']['Forums_LastPostsT'] . "</a>");
			$GLOBALS['tmpl']->assign("post_count", $num);
			$GLOBALS['tmpl']->assign("matches", $matches);
			$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Forums_LastPostsT']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/lastposts_forum.tpl')); 
		break;
	}
	
	break;
	
	//=================================================================
	// Passwort geschuetztes Forum
	//=================================================================
	case 'login':
		$r_pass = $GLOBALS['db']->Query("SELECT password, title FROM " . PREFIX . "_f_forum WHERE id = '" . $_REQUEST['fid'] . "'");
		$pass = $r_pass->fetchrow();
		
		if (md5($_REQUEST['pass']) == $pass->password)
		{	
			// cookie setzen
			@setcookie('f_pass_id_' . $_REQUEST['fid'] , md5($_REQUEST['pass']) , 0);
			refer("index.php?p=showforum&fid=$_REQUEST[fid]");
		} else {
			$stitle = $GLOBALS['config_vars']['Forums_ForumTitle_login'];
			$sname = $stitle;
			$GLOBALS['tmpl']->assign('navigation', getNavigation($fid, 'forum') . $GLOBALS['config_vars']['Forums_Splitter'] . $pass->title);
			$GLOBALS['tmpl']->assign('fid', $_REQUEST['fid']);
			$GLOBALS['tmpl']->assign('error', $GLOBALS['config_vars']['Forums_Locked_WrongPass']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forums/forumlogin.tpl')); 
		}
	break;
	
	//=================================================================
	// Anlagen-Download
	//=================================================================
	case 'getfile':
		@ob_start();
		// berechtigung ueberpruefen
		$permissions = getForumPermissionsByUser($_REQUEST['f_id'], UID);
		
		if (($permissions[FORUM_PERMISSION_CAN_DOWNLOAD_ATTACHMENT] != 1) ) //&& (UGROUP != 1)
		{
			koobi_message('Global_NoPermission','Global_NoPermission_t', HOME_PATH . '/index.php?p=showtopic&amp;toid=' . $_REQUEST['t_id']);
		} else {
			$result = $GLOBALS['db']->Query("SELECT filename, orig_name FROM " . PREFIX . "_f_attachment WHERE id = '" .$_REQUEST['id'] . "'");
			$file = $result->fetchrow();
			
			$update = $GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_attachment set hits=hits+1 WHERE id='$_REQUEST[id]'");
			@ob_end_flush();
			@ob_end_clean();
			header("Cache-control: private");
			header("Content-type: application/octet-stream"); 
			header("Content-disposition:attachment; filename=" . str_replace(' ','',$file->orig_name));
			header("Content-Length: " . filesize("uploads/attachment/" . $file->filename));
			readfile("uploads/attachment/" . $file->filename);
			exit();
		}
	break;
	
	//=================================================================
	//=================================================================
	case 'markread':
		switch ($_REQUEST['what'])
		{
			case 'forum':
				if ($_REQUEST['id'] != "")
				{
					setForumAsRead(escs($_REQUEST['id']));
					refer("index.php?p=showforum&fid=" . $_REQUEST['id']);
				} else {
					setForumAsRead();
					refer("index.php?p=showforums");
				}
			break;
		}
	break;
}
?>