<?php
class Forums
{

	//=================================================================
	// Hilfe-Seite loeschen
	//=================================================================
	function DelHelpPage($id)
	{
		if(perm('forum_helppages'))
		{
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_hilfetext WHERE Id = '$id'");
			header('Location:index.php?do=forums&sub=forumshelp');
			exit;
		}
	}
	
	//=================================================================
	// Hilfe-Kategorie loeschen
	//=================================================================
	function DelHelpCateg($categ)
	{
		if(perm('forum_helppages'))
		{
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_hilfe WHERE Id = '$categ'");
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_hilfetext WHERE Kategorie = '$categ'");
			header('Location:index.php?do=forums&sub=forumshelp');
			exit;
		}
	}
	
	//=================================================================
	// Hilfe-Kategorie anlegen
	//=================================================================
	function ForumsHelpNewCateg()
	{
		if(perm('forum_helppages'))
		{
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1 && !empty($_POST['Name_1']))
			{
				$n1 = $_POST['Name_1'];
				$n2 = (empty($_POST['Name_2'])) ? $n1 : $_POST['Name_2'];
				$n3 = (empty($_POST['Name_3'])) ? $n1 : $_POST['Name_3'];
				
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_f_hilfe
				(
					Id,
					Name_1,
					Name_2,
					Name_3,
					Position,
					Aktiv
				) VALUES (
					'',
					'$n1',
					'$n2',
					'$n3',
					'1',
					'1'
				)");
				
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Help_newcateg']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/helpcateg_edit.tpl'));
		}
	}
	
	//=================================================================
	// Neue Frage + Antwortseite
	//=================================================================
	function HelpPageNew($categ)
	{
		if(perm('forum_helppages'))
		{
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				$base = substr($_SERVER['PHP_SELF'],0,-15);
				
				$n1 = $_POST['Name_1'];
				$n2 = (empty($_POST['Name_2'])) ? $n1 : $_POST['Name_2'];
				$n3 = (empty($_POST['Name_3'])) ? $n1 : $_POST['Name_3'];
				
				$t1 = $_POST['Text_1'];
				$t2 = (empty($_POST['Text_2'])) ? $n1 : $_POST['Text_2'];
				$t3 = (empty($_POST['Text_3'])) ? $n1 : $_POST['Text_3'];
				
				$text_1 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t1);
				$text_2 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t2);
				$text_3 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t3);
				
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_f_hilfetext
				(
					Id,
					Kategorie, 	
					Name_1, 	
					Name_2, 	
					Name_3, 	
					Text_1, 	
					Text_2, 	
					Text_3, 	
					Position, 	
					Klicks, 	
					Aktiv
				) VALUES (
					'',
					'$categ',
					'$n1',
					'$n2',
					'$n3',
					'$text_1',
					'$text_2',
					'$text_3',
					'1',
					'',
					'1'
				)");
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$GLOBALS['tmpl']->assign('Text_1', fckInit('', "Text_1", 150, 'Basic'));
			$GLOBALS['tmpl']->assign('Text_2', fckInit('', "Text_2", 150, 'Basic'));
			$GLOBALS['tmpl']->assign('Text_3', fckInit('', "Text_3", 150, 'Basic'));
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Help_addhelp']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/helppage_edit_new.tpl'));
		}
	}
	
	//=================================================================
	// Frage + Antwortseite bearbeiten
	//=================================================================
	function ForumsHelpPageEdit($id)
	{
		if(perm('forum_helppages'))
		{
			$base = substr($_SERVER['PHP_SELF'],0,-15);
			
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1 && !empty($_POST['Text_1']))
			{
				$n1 = $_POST['Name_1'];
				$n2 = (empty($_POST['Name_2'])) ? $n1 : $_POST['Name_2'];
				$n3 = (empty($_POST['Name_3'])) ? $n1 : $_POST['Name_3'];
				
				$t1 = $_POST['Text_1'];
				$t2 = (empty($_POST['Text_2'])) ? $n1 : $_POST['Text_2'];
				$t3 = (empty($_POST['Text_3'])) ? $n1 : $_POST['Text_3'];
				
				$text_1 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t1);
				$text_2 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t2);
				$text_3 = str_replace('src=\"' . $base, 'src=\"%%ECRUOS_GMI_IBOOK%%', $t3);
				
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_hilfetext 
				SET
					Name_1 = '$n1',
					Name_2 = '$n2',
					Name_3 = '$n3',
					Text_1 = '$text_1',
					Text_2 = '$text_2',
					Text_3 = '$text_3'
				WHERE Id = '$id'");
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_f_hilfetext WHERE Id = '$id'");
			
			$GLOBALS['tmpl']->assign('Text_1', fckInit(str_replace('src="%%ECRUOS_GMI_IBOOK%%', 'src="' . $base, $res->Text_1), "Text_1", 150, 'Basic'));
			$GLOBALS['tmpl']->assign('Text_2', fckInit(str_replace('src="%%ECRUOS_GMI_IBOOK%%', 'src="' . $base, $res->Text_2), "Text_2", 150, 'Basic'));
			$GLOBALS['tmpl']->assign('Text_3', fckInit(str_replace('src="%%ECRUOS_GMI_IBOOK%%', 'src="' . $base, $res->Text_3), "Text_3", 150, 'Basic'));
			
			$GLOBALS['tmpl']->assign('res', $res);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Help_editcateg']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/helppage_edit_new.tpl'));
		}
	}
	
	//=================================================================
	// Kategorie bearbeiten
	//=================================================================
	function HelpPageEdit($id)
	{
		if(perm('forum_helppages'))
		{
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1 && !empty($_POST['Name_1']))
			{
				$n1 = $_POST['Name_1'];
				$n2 = (empty($_POST['Name_2'])) ? $n1 : $_POST['Name_2'];
				$n3 = (empty($_POST['Name_3'])) ? $n1 : $_POST['Name_3'];
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_hilfe 
				SET
					Name_1 = '$n1',
					Name_2 = '$n2',
					Name_3 = '$n3'
				WHERE Id = '$id'");
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_f_hilfe WHERE Id = '$id'");
			$GLOBALS['tmpl']->assign('res', $res);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Help_editcateg']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/helpcateg_edit.tpl'));
		}
	}
	
	function HelpPages()
	{
		if(perm('forum_helppages'))
		{
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				// Kategorien speichern
				foreach ($_POST['Fid'] as $id) 
				{
					$query = "UPDATE " . PREFIX . "_f_hilfe 
					SET 
						Position = '" . $_POST['Position'][$id] . "',
						Aktiv = '" . $_POST['Aktiv'][$id] . "'
					WHERE Id = '$id'";
					$result = $GLOBALS['db']->Query($query);
					
					// Fragen deaktivieren?
					if($_POST['Aktiv'][$id] != 1) $GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_hilfetext SET Aktiv='0' WHERE Kategorie='$id'");
				}
				
				// Fragen speichern
				foreach ($_POST['SFid'] as $id) 
				{
					$query = "UPDATE " . PREFIX . "_f_hilfetext
					SET 
						Position = '" . $_POST['SubPosition'][$id] . "',
						Aktiv = '" . $_POST['SubAktiv'][$id] . "'
					WHERE Id = '$id'";
					$result = $GLOBALS['db']->Query($query);
				}
			}
			
			$topics = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_f_hilfe ORDER BY Position ASC");
			while($row = $sql->fetchrow())
			{
				$subtopics = array();
				$sql_sub = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_f_hilfetext WHERE Kategorie = '$row->Id' ORDER BY Position ASC");
				while($row_sub = $sql_sub->fetchrow())
				{
					array_push($subtopics, $row_sub);
				}
				$row->subtopics = $subtopics;
				array_push($topics, $row);
			}
			
			$GLOBALS['tmpl']->assign('topics', $topics); 
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Help']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/helppages.tpl'));
		}
	}
	
	//=================================================================
	// Uebersicht
	//=================================================================
	function Overview()
	{
		if(!perm('forum'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				// position der kategorien speichern
				foreach ($_POST['c_id'] as $c_id) {
					$position = $_POST['c_position'][$c_id];
					if (is_numeric($position)) {
						$query = "UPDATE " . PREFIX . "_f_category SET position = $position WHERE id = '$c_id'";
						$result = $GLOBALS['db']->Query($query);
					}
				}
				
				// position der foren speichern
				foreach ($_POST['f_id'] as $f_id) {
					$position = $_POST['f_position'][$f_id];
					if (is_numeric($position)) {
						$query = "UPDATE " . PREFIX . "_f_forum SET position = $position WHERE id = '$f_id'";
						$result = $GLOBALS['db']->Query($query);
					}
				}
			}
			
			$_GET['id'] = (isset($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : 0;
			$categories = array();
			$this->getCategoriesAdmin($_GET['id'], $categories, "  - ");
			
			$GLOBALS['tmpl']->assign('categories', $categories);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/forums_overview.tpl'));
		}
	}
	
	function DelTopics()
	{
		if(!perm('forum_deltopics'))
		{
			no_perm();
		} else {
			
			if(isset($_POST['delete']) && $_POST['delete']==1)
			{
				$match = 0;
				$query = "SELECT id FROM " . PREFIX . "_f_topic AS t WHERE ";
				$where_clausel = array();
				
				// loesche themen aelter als
				if (isset($_POST['date']) && $_POST['date'] > 0) {
					$where_clausel[] = " (status='0') AND (forum_id='".$_POST['fid']."') AND ((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(t.datum)) / 60 / 60 / 24) >= '" . $_POST['date'] . "'";
				}
				
				// loesche themen mit antworten weniger als
				if (isset($_POST['reply_count']) && $_POST['reply_count'] != "") {
					$where_clausel[] = "t.replies < '" . $_POST['reply_count'] . "'";
				}
				
				// loesche geschlossene themen
				if (isset($_POST['topic_closed'])) {
					$where_clausel[] = "t.status = " . FORUM_STATUS_CLOSED;
				}
				
				// loesche themen mit antworten weniger als
				if (isset($_POST['hits_count']) && $_POST['hits_count'] != "") {
					$where_clausel[] = "t.views < '" . $_POST['hits_count'] . "'";
				}
				
				$query .= @implode(' ' . $_POST['verkn'] . ' ', $where_clausel);
				
				if (count($where_clausel) > 0) 
				{
					// loesche die dazugehoerigen beitraege
					$result = $GLOBALS['db']->Query($query);
					while ($topic = $result->fetchrow()) 
					{
						$this->AdminDeleteTopic($topic->id);
						$match++;
					}
					
				}
				$GLOBALS['tmpl']->assign('match', $match);
			}
		
			$GLOBALS['tmpl']->assign('forums', $this->GetForums());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/delete_topic.tpl'));
		}
	}
	
	function GetForums()
	{ 
		$categs = array();
		$query = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_f_category ORDER BY position ASC");
		while($row = $query->fetchrow())
		{
			$forums = array();
			$query2 = $GLOBALS['db']->Query("SELECT id,title FROM " . PREFIX . "_f_forum WHERE category_id='$row->id'");
			while($row2 = $query2->fetchrow())
			{
				array_push($forums,$row2);
			}
			
			$row->forums = $forums;
			array_push($categs,$row);
		}
		return $categs;
	} 
	
	//=================================================================
	// Ajax-Modsuche
	//=================================================================
	function ModSearch($mod)
	{
		$q = eregi_replace('[^-A-Za-zA-ÖO-öo-y0-9_. ]', '', $mod) ;
		$q = strtolower($q);
		if (!$q) return;
		
		$res = array();
		$result = $GLOBALS['db']->Query("SELECT Id,Benutzername FROM " . PREFIX . "_benutzer WHERE Benutzername LIKE '" . $q . "%'");
		while($row = $result->fetchrow())
		{
			array_push($res, $row->Benutzername);
		}
		
		foreach ($res as $key=>$value) 
		{
			if (strpos(strtolower($value), $q) !== false) echo stripslashes($value) . "\n"; // $key;
		}
	}
	
	//=================================================================
	// Moderatoren
	//=================================================================
	function Mods($id)
	{
		if(!perm('forum_mods'))
		{
			no_perm();
		} else {
			// Moderator loeschen
			if(isset($_POST['delete']) && $_POST['delete']==1)
			{
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_mods WHERE forum_id = '" . $_POST['id'] . "' AND user_id = '" . $_POST['user_id'] . "'");
			}
			
			// Moderator hinzufuegen
			if(isset($_POST['new']) && $_POST['new']==1 && isset($_POST['user_name']) && !empty($_POST['user_name']))
			{
				$result = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_benutzer WHERE Benutzername = '" . $_POST['user_name'] . "'");
				$user = $result->fetchrow();
				if ($result->numrows() == 1 && (!@in_array($_POST['user_name'],@$_POST['mods'])) ) 
				{
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_f_mods (forum_id, user_id) VALUES (" . $_POST['id'] . ", " . $user->Id . ")");
				} else {
					$GLOBALS['tmpl']->assign('error', 1);
				}
			}
			
			$mods = array();
			$result = $GLOBALS['db']->Query("SELECT Id,Benutzername FROM " . PREFIX . "_benutzer AS u," . PREFIX . "_f_mods AS m WHERE Id = m.user_id AND m.forum_id = '" . $id . "'");
			while ($mod = $result->fetchrow()) $mods[] = $mod;
			
			$GLOBALS['tmpl']->assign('mods', $mods);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_mods']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/edit_mods.tpl'));
		}
	}
	
	//=================================================================
	// Gruppen listen
	//=================================================================
	function Groups()
	{
		$result = $GLOBALS['db']->Query("SELECT Id AS ugroup,Name AS groupname FROM  " . PREFIX . "_benutzer_gruppen");
		$groups = array();
		while ($group = $result->fetchrow()) $groups[] = $group;
		return $groups;
	}
	
	//=================================================================
	// Kategorie loeschen
	//=================================================================
	function DeleteCategory($id)
	{
		if(!perm('forum_delete'))
		{
			no_perm();
		} else {
			$this->AdminDeleteCategory($id);
			echo '<script>window.opener.location.reload();window.close();</script>';
			exit;
		}
	}
	
	//=================================================================
	// Forum loeschen
	//=================================================================
	function DeleteForum($id)
	{
		if(!perm('forum_delete'))
		{
			no_perm();
		} else {
			$this->AdminDeleteForum($id);
			echo '<script>window.opener.location.reload();window.close();</script>';
			exit;
		}
	}
	
	//=================================================================
	// Kategorie hinzufuegen
	//=================================================================
	function AddCategory($id)
	{
		if(!perm('forum_categoryedit'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				$title = strip_tags($_POST['title']);
				$comment = ($_POST['comment'] != "") ? $_POST['comment'] : "";
				$position = $_POST['position'];
				$parent_id = ($_POST['f_id'] == "") ? 0 : $_POST['f_id'];
				
				$GLOBALS['db']->Query("INSERT INTO  " . PREFIX . "_f_category 
				(
					id,
					title,
					position,
					comment,
					parent_id,
					group_id
				) VALUES (
					'', 
					'$title', 
					$position, 
					'$comment', 
					'$parent_id',
					'" . @implode(",", $_POST['group_id']) . "')");
		
				echo '<script>window.opener.location.reload();window.close();</script>';
				exit;
			}
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_addCateg']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/edit_category.tpl'));
		}
	}
	
	//=================================================================
	// Kategorie bearbeiten
	//=================================================================
	function CategoryEdit($id)
	{
		if(!perm('forum_categoryedit'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				$query = "
				UPDATE " . PREFIX . "_f_category
				SET
				position = " . $_POST['position'];
				
				$comment = ($_POST['comment'] != "") ? $_POST['comment'] : "";
				if (isset($_POST['title'])) $query .= ", title = '" . strip_tags($_POST['title']) . "'";
				if (isset($comment)) $query .= ", comment = '" . $comment . "'";
				if (isset($_POST['group_id'])) 
				{
					// update die foren der kategorie
					$groups = @implode(",", $_POST['group_id']);
					
					$q_forums = "UPDATE " . PREFIX . "_f_forum SET group_id = '$groups' WHERE category_id = " . $_POST['c_id'];
					$r_forums = $GLOBALS['db']->Query($q_forums);
					
					$query .= ", group_id = '$groups'";
				}
				
				$query .= " WHERE id = " . $_POST['c_id'];
				$result = $GLOBALS['db']->Query($query);
				
				echo '<script>window.opener.location.reload();window.close();</script>';
				exit;
			}
			
			$category = $GLOBALS['db']->RowQuery("SELECT id,title,comment,position,group_id FROM " . PREFIX . "_f_category WHERE id = '$id'");
			$category->group_id = @explode(",", $category->group_id);
			
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('category', $category);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_CategEdit']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/edit_category.tpl'));
		}
	}
	
	//=================================================================
	// Gruppen-Berechtigungen
	//=================================================================
	function UserPermissions($group,$forum)
	{
		if(!perm('forum_userperms'))
		{
			no_perm();
		} else {
			// Speichern
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				$permissions = array();
				$permissions[FORUM_PERMISSION_CAN_SEE] = $_POST['can_see'];
				$permissions[FORUM_PERMISSION_CAN_SEE_TOPIC] = $_POST['can_see_topic'];
				$permissions[FORUM_PERMISSION_CAN_SEE_DELETE_MESSAGE] = 1;
				$permissions[FORUM_PERMISSION_CAN_SEARCH_FORUM] = 1;
				$permissions[FORUM_PERMISSION_CAN_DOWNLOAD_ATTACHMENT] = $_POST['can_download_attachment'];
				$permissions[FORUM_PERMISSION_CAN_CREATE_TOPIC] = $_POST['can_create_topic'];
				$permissions[FORUM_PERMISSION_CAN_REPLY_OWN_TOPIC] = $_POST['can_reply_own_topic'];
				$permissions[FORUM_PERMISSION_CAN_REPLY_OTHER_TOPIC] = $_POST['can_reply_other_topic'];
				$permissions[FORUM_PERMISSION_CAN_UPLOAD_ATTACHMENT] = $_POST['can_upload_attachment'];
				$permissions[FORUM_PERMISSION_CAN_RATE_TOPIC] = $_POST['can_rate_topic'];
				$permissions[FORUM_PERMISSION_CAN_EDIT_OWN_POST] = $_POST['can_edit_own_post'];
				$permissions[FORUM_PERMISSION_CAN_DELETE_OWN_POST] = $_POST['can_delete_own_post'];
				$permissions[FORUM_PERMISSION_CAN_MOVE_OWN_TOPIC] = $_POST['can_move_own_topic'];
				$permissions[FORUM_PERMISSION_CAN_CLOSE_OPEN_OWN_TOPIC] = $_POST['can_close_open_own_topic'];
				$permissions[FORUM_PERMISSION_CAN_DELETE_OWN_TOPIC] = $_POST['can_delete_own_topic'];
				$permissions[FORUM_PERMISSION_CAN_DELETE_OTHER_POST] = $_POST['can_delete_other_post'];
				$permissions[FORUM_PERMISSION_CAN_EDIT_OTHER_POST] = $_POST['can_edit_other_post'];
				$permissions[FORUM_PERMISSIONS_CAN_OPEN_TOPIC] = $_POST['can_open_topic'];
				$permissions[FORUM_PERMISSIONS_CAN_CLOSE_TOPIC] = $_POST['can_close_topic'];
				$permissions[FORUM_PERMISSIONS_CAN_CHANGE_TOPICTYPE] = $_POST['can_change_topic_type'];
				$permissions[FORUM_PERMISSIONS_CAN_MOVE_TOPIC] = $_POST['can_move_topic'];
				$permissions[FORUM_PERMISSIONS_CAN_DELETE_TOPIC] = $_POST['can_delete_topic'];
				
				$permissions = @implode(",", $permissions);
		
				if($_REQUEST['settoall'] == 1)
				{
					$sql = $GLOBALS['db']->Query("SELECT forum_id FROM " . PREFIX . "_f_permissions");
					while($row = $sql->fetchrow())
					{
						$sql2 = $GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_permissions SET permissions = '$permissions' WHERE forum_id = '$row->forum_id' AND group_id = '$_POST[g_id]'");
					}
					
				} else {
					$sql_first = $GLOBALS['db']->Query("SELECT forum_id FROM " . PREFIX . "_f_permissions WHERE forum_id = " . $_POST['f_id'] . " AND group_id = '" . $_POST['g_id'] . "'");
					$num_first = $sql_first->numrows();
					
					
					if($num_first < 1)
					{
						// wenn gruppe noch nicht in den permissions existiert
						$query = $GLOBALS['db']->Query(
						"INSERT INTO " . PREFIX . "_f_permissions (
							forum_id,
							group_id,
							permissions
						) VALUES (
							'" . $_POST['f_id'] . "',
							'" . $_POST['g_id'] . "',
							'$permissions' 
							)"
						);
					} else {
						// sonst aktualisieren
						$query = "UPDATE " . PREFIX . "_f_permissions SET permissions = '$permissions' WHERE forum_id = '" . $_POST['f_id'] . "' AND group_id = '" . $_POST['g_id'] . "'";
						$result = $GLOBALS['db']->Query($query);
					}
				}
				$q_parent_id = "
					SELECT 
					c.parent_id 
				FROM 
					" . PREFIX . "_f_category AS c,
					" . PREFIX . "_f_forum AS f
				WHERE
					f.id = " . $_POST['f_id'] . " AND
					c.id = f.category_id";
				
				$r_parent_id = $GLOBALS['db']->Query($q_parent_id);
				$parent_id = $r_parent_id->fetchrow();
				echo '<script>window.close();</script>';
				exit;
				// speichern ende
			}
			
			$forum_id = $forum;
			$group_id = $group;
			
			$query = "SELECT permissions FROM " . PREFIX . "_f_permissions WHERE forum_id = $forum_id AND group_id = $group_id";
			$result = $GLOBALS['db']->Query($query);
			$forum = $result->fetchrow();
			$permissions = @explode(',', $forum->permissions);
			
			$GLOBALS['tmpl']->assign('permissions', $permissions);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_GroupPerms']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/edit_permissions.tpl'));
		}
	}
	
	//=================================================================
	// Forum bearbeiten
	//=================================================================
	function EditForum($id)
	{
		if(!perm('forum_edit'))
		{
			no_perm();
		} else {
			// Speichern
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				$password = ($_POST['password'] == "") ? '' : md5($_POST['password']);
				$moderated = (isset($_POST['moderated']) && $_POST['moderated']==1) ? 1 : 0;
				$moderated_posts = (isset($_POST['moderated_posts']) && $_POST['moderated_posts']==1) ? 1 : 0;
				
				$query = "
				UPDATE " . PREFIX . "_f_forum SET
					title = '" . strip_tags($_POST['title']) . "',
					comment = '" . strip_tags($_POST['comment']) . "',
					group_id = '" . @implode(",", $_POST['group_id']) . "',
					active = " . $_POST['active'] . ",
					password = '$password',
					password_raw = '$_POST[password]',
					moderated = '$moderated',
					moderated_posts = '$moderated_posts',
					post_emails = '$_POST[post_emails]',
					topic_emails = '$_POST[topic_emails]'
				WHERE
					id = " . $_POST['f_id'];
				$result = $GLOBALS['db']->Query($query);
				$forum_id = $GLOBALS['db']->InsertId();
				$f_id = ($_POST['f_id'] == "") ? $forum_id : $_POST['f_id'];
		
				// status aendern
				$this->AdminSwitchForumStatus($f_id, $_POST['status']);
				echo '<script>window.opener.location.reload();window.close();</script>';
				exit;
			}
			
			// Auslesen
			$f_id = $id;
			$forum = $GLOBALS['db']->RowQuery("SELECT
				f.id,
				f.title,
				f.comment,
				f.active,
				c.group_id,
				f.category_id,
				f.status,
				f.password_raw,
				f.moderated,
				f.moderated_posts,
				f.post_emails,
				f.topic_emails
			FROM
				" . PREFIX . "_f_forum AS f,
				" . PREFIX . "_f_category AS c
			WHERE
				f.id = $f_id AND
				f.category_id = c.id");
			
			$forum->group_id = @explode(',', $forum->group_id);
			
			$GLOBALS['tmpl']->assign('forum', $forum);
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_fEdit']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/forums_edit.tpl'));
		}
	}
	
	//=================================================================
	// Forum hinzufuegen
	//=================================================================
	function AddForum($id)
	{
		if(!perm('forum_add'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && !empty($_POST['save']) && $_POST['save']==1)
			{
				// passwort erstellen
				$password = ($_POST['password'] == "") ? '' : md5($_POST['password']);
				$password_raw = $_POST['password'];
				
				// gruppen der kategorie uebernehmen
				$q_groups = "SELECT group_id FROM " . PREFIX . "_f_category WHERE id = '" . $_POST['c_id'] . "'";
				$r_groups = $GLOBALS['db']->Query($q_groups);
				$c_groups = $r_groups->fetchrow();
				
				$title = strip_tags($_POST["title"]);
				$comment = ($_POST['comment'] != "") ? strip_tags($_POST['comment']) : "";
				
				$moderated = (isset($_POST['moderated']) && $_POST['moderated']==1) ? 1 : 0;
				$moderated_posts = (isset($_POST['moderated_posts']) && $_POST['moderated_posts']==1) ? 1 : 0;
				
				$query = "INSERT INTO " . PREFIX . "_f_forum (
					id, 
					title, 
					comment, 
					category_id, 
					active, 
					password, 
					password_raw,
					group_id,
					moderated,
					moderated_posts,
					post_emails,
					topic_emails
				) VALUES (
					'', 
					'$title', 
					'$comment', 
					'$_POST[c_id]', 
					'$_POST[active]', 
					'$password',
					'$password_raw',
					'$c_groups->group_id',
					'$moderated',
					'$moderated_posts',
					'$_POST[post_emails]',
					'$_POST[topic_emails]')";
				
				$result = $GLOBALS['db']->Query($query);
				$forum_id = $GLOBALS['db']->InsertId();
				
				// gruppen
				$q_groups = "SELECT Id AS ugroup FROM " . PREFIX . "_benutzer_gruppen";
				$r_groups = $GLOBALS['db']->Query($q_groups);
				
				// standardmaske fuer die rechte einer gruppe
				$default_mask = $this->defPermOut();
				
				while ($group = $r_groups->fetchrow()) 
				{
					// zugriffsrechte tabellen
					if($default_mask[$group->ugroup] == ""){
						$defmask = $default_mask['2'];
					} else {
						$defmask = $default_mask[$group->ugroup];
					}
					
					$query = "INSERT INTO " . PREFIX . "_f_permissions (forum_id, group_id, permissions) VALUES ('$forum_id', '" . $group->ugroup . "', '".$defmask."')";
					$result = $GLOBALS['db']->Query($query);
				}
				$forum_id = $GLOBALS['db']->InsertId();
				$f_id = ($_POST['f_id'] == "") ? $forum_id : $_POST['f_id'];
		
				// status aendern
				$this->AdminSwitchForumStatus($f_id, $_POST['status']);
				echo '<script>window.opener.location.reload();window.close();</script>';
				exit;
				
			}
			
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_new']);
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/forums_new.tpl'));
		}
	}
	
	function getCategoriesAdmin($id, &$categories, $prefix) 
	{
		// kategorien holen
		$r_cat = $GLOBALS['db']->Query($q_cat = "SELECT id, title, comment,position FROM " . PREFIX . "_f_category WHERE parent_id = $id ORDER BY position ASC");
		
		// kategorien durchgehen
		while ($cat = $r_cat->fetchrow()) {
			$cat->forums = array();
			// foren zur kategorie holen
		$result = $GLOBALS['db']->Query("SELECT 
				f.id, 
				f.comment,
				f.category_id, 
				f.title,
				f.active,
				f.group_id,
				f.position,
				f.status
			FROM 
				" . PREFIX . "_f_forum AS f
			WHERE 
				category_id = '" . $cat->id . "'
			ORDER BY
				f.position");
		
			
			// alle foren durchgehen
			while ($forum = $result->fetchrow()) 
			{
				$group_ids = @explode(",", $forum->group_id);
				$forum->visible_title = $prefix . $forum->title;
				$r_sub_cat = $GLOBALS['db']->Query("SELECT id, title, comment FROM " . PREFIX . "_f_category WHERE parent_id = '" . $forum->id . "'");
				
				$forum->categories = array();
				while ($sub_cat = $r_sub_cat->fetchrow()) 
				{
					$forum->categories[] = $sub_cat;
				}
				
				// mods holen
				$mods = $GLOBALS['db']->RowQuery("SELECT COUNT(m.forum_id) AS m_count FROM " . PREFIX . "_f_mods AS m WHERE m.forum_id = '" . $forum->id . "'");
				$forum->mods = $mods->m_count;
				$cat->forums[] = $forum;
			}
			
			$categories[] = $cat;
		}	
	}
	
	
	
	
	function Posticons()
	{
		if(perm('comment_emoticons'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach ($_POST['smilie'] as $id => $var)
				{
					if(!empty($_POST['path'][$id]))
					{
						$GLOBALS['db']->Query("UPDATE ".PREFIX."_posticons  SET 
							active='" . @$_POST['active'][$id] . "',
							path='" . @$_POST['path'][$id] . "',
							posi='" . @$_POST['posi'][$id] . "' 
						WHERE id = '$id'");
					}
				}
				
				// loeschen
				if(isset($_POST['del']) && $_POST['del']>=1)
				{
					foreach ($_POST['del'] as $id => $del) {
						if( $del != "")  {
							$result = $GLOBALS['db']->Query("DELETE FROM  ".PREFIX."_posticons WHERE id = '$id'");
						}
					}
				}
				header('Location:index.php?do=forums&sub=posticons');
				exit;
			}
			
			// Neu
			if(isset($_POST['new']) && $_POST['new']==1)
			{
				$smileys = array();
				for ($i = 0; $i < count($_POST['path']); $i++) 
				{
					if( ($_POST['path'][$i] != "" ) )
					{
						$GLOBALS['db']->Query("INSERT INTO ".PREFIX."_posticons (id,active,path,area) VALUES ('','1','".$_POST['path'][$i]."','" . $_SESSION['a_area'] . "')");
					}
				}
				header('Location:index.php?do=forums&sub=posticons');
				exit;
			}
			
			$smileys = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_posticons WHERE area='" . $_SESSION['a_area'] . "' ORDER BY posi ASC");
			while($row = $sql->fetchrow())
			{
				$installed[] = $row->path;
				$row->Icon = '<img src="../theme/' . getThemeByArea($_SESSION['a_area']) . '/images/posticons/' . $row->path . '" alt="" border="0" />';
				array_push($smileys, $row);
			}
			
			$d = BASE_DIR . '/theme/' . getThemeByArea($_SESSION['a_area']) . '/images/posticons/';
			$handle = @opendir($d); 
			$smi = array();
			while ($file = @readdir ($handle)){ 
				$endg = @explode('.', $file);
				if(@$file != '.' && @$file != '..') { 
					$f = '';
					if(is_file($d.'/'.$file) && !in_array($file,$installed)){ 
						
						$short = explode('.',$file);
						$f->Short = ':' . $short[0] . ':';
						$f->Name = $file;
						array_push($smi,$f);
					} 
					
				} 
			} 
			
			$GLOBALS['tmpl']->assign('smi_path', '../theme/' . getThemeByArea($_SESSION['a_area']) . '/images/posticons/');
			$GLOBALS['tmpl']->assign('smi', $smi);
			$GLOBALS['tmpl']->assign('icon_t_path', getThemeByArea($_SESSION['a_area']));
			$GLOBALS['tmpl']->assign('smileys', $smileys);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_TIcons_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/posticons.tpl'));
		}
	}
	
	//=================================================================
	// Smilies
	//=================================================================
	function Emoticons()
	{
		if(perm('comment_emoticons'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach ($_POST['smilie'] as $id => $var)
				{
					if(!empty($_POST['path'][$id]) && !empty($_POST['code'][$id]))
					{
						$GLOBALS['db']->Query("UPDATE ".PREFIX."_smileys  SET 
							active='" . @$_POST['active'][$id] . "',
							code='" . @$_POST['code'][$id] . "',
							path='" . @$_POST['path'][$id] . "',
							posi='" . @$_POST['posi'][$id] . "' 
						WHERE id = '$id'");
					}
				}
				
				// loeschen
				if(isset($_POST['del']) && $_POST['del']>=1)
				{
					foreach ($_POST['del'] as $id => $del) {
						if( $del != "")  {
							$result = $GLOBALS['db']->Query("DELETE FROM  ".PREFIX."_smileys WHERE id = '$id'");
						}
					}
				}
				header('Location:index.php?do=forums&sub=emoticons');
				exit;
			}
			
			// Neu
			if(isset($_POST['new']) && $_POST['new']==1)
			{
				$smileys = array();
				for ($i = 0; $i < count($_POST['code']); $i++) 
				{
					if( ($_POST['code'][$i] != "" ) && ($_POST['path'][$i] != "") )
					{
						$GLOBALS['db']->Query("INSERT INTO ".PREFIX."_smileys (id,active,code,path,area) VALUES ('','1','".$_POST['code'][$i]."','".$_POST['path'][$i]."','" . $_SESSION['a_area'] . "')");
					}
				}
				header('Location:index.php?do=forums&sub=emoticons');
				exit;
			}
			
			$smileys = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_smileys WHERE area='" . $_SESSION['a_area'] . "' ORDER BY posi ASC");
			while($row = $sql->fetchrow())
			{
				$installed[] = $row->path;
				$row->Icon = '<img src="../theme/' . getThemeByArea($_SESSION['a_area']) . '/images/smilies/' . $row->path . '" alt="" border="0" />';
				array_push($smileys, $row);
			}
			
			$d = BASE_DIR . '/theme/' . getThemeByArea($_SESSION['a_area']) . '/images/smilies/';
			$handle = @opendir($d); 
			$smi = array();
			while ($file = @readdir ($handle)){ 
				$endg = @explode('.', $file);
				if(@$file != '.' && @$file != '..') { 
					$f = '';
					if(is_file($d.'/'.$file) && !in_array($file,$installed)){ 
						
						$short = explode('.',$file);
						$f->Short = ':' . $short[0] . ':';
						$f->Name = $file;
						array_push($smi,$f);
					} 
					
				} 
			} 
			
			$GLOBALS['tmpl']->assign('smi_path', '../theme/' . getThemeByArea($_SESSION['a_area']) . '/images/smilies/');
			$GLOBALS['tmpl']->assign('smi', $smi);
			$GLOBALS['tmpl']->assign('smi_t_path', getThemeByArea($_SESSION['a_area']));
			$GLOBALS['tmpl']->assign('smileys', $smileys);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Smi_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/emoticons.tpl'));
		}
	}
	
	function UserRankings()
	{
		if(perm('forum_userrankings'))
		{
			// Loeschen
			if(isset($_GET['del']) && $_GET['del']==1)
			{
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_rank WHERE id = '" . $_GET['id'] . "'");
				header('Location:index.php?do=forums&sub=userrankings');
				exit;
			}
			
			// Neu
			if(isset($_POST['new']) && $_POST['new']==1 && ($_POST['count']>1) && (!empty($_POST['title'])))
			{
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_f_rank (id, title, count) VALUES ('', '" . $_POST['title'] . "', '" . $_POST['count'] . "')");
				header('Location:index.php?do=forums&sub=userrankings');
				exit;
			}
			
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$rank_id = array_keys($_POST['count']);
				foreach ($rank_id as $id) {
					if (is_numeric($_POST['count'][$id]) && !empty($_POST['title'][$id]) ) {
						$GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_rank SET count = '" . $_POST['count'][$id] . "',title = '" . $_POST['title'][$id] . "' WHERE id = '$id'");
					}
				}
				header('Location:index.php?do=forums&sub=userrankings');
				exit;
			}
			
			$result = $GLOBALS['db']->Query("SELECT id, title, count FROM " . PREFIX . "_f_rank");
			$ranks = array();
			
			while ($entry = $result->fetchrow()) {
				$ranks[] = $entry;
			}
			
			$GLOBALS['tmpl']->assign("ranks", $ranks);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_URank_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/user_ranks.tpl'));
		}
	}
	
	//=================================================================
	// Ajax-Modsuche
	//=================================================================
	function AttachmentSearch($name)
	{
		if(perm('forum_attachments'))
		{
			$q = eregi_replace('[^-A-Za-zA-ÖO-öo-y0-9_. ]', '', $name) ;
			$q = strtolower($q);
			if (!$q) return;
			
			$res = array();
			$result = $GLOBALS['db']->Query("SELECT id,orig_name FROM " . PREFIX . "_f_attachment WHERE orig_name LIKE '" . $q . "%'");
			while($row = $result->fetchrow())
			{
				array_push($res, $row->orig_name);
			}
			
			foreach ($res as $key=>$value) 
			{
				if (strpos(strtolower($value), $q) !== false) echo stripslashes($value) . "\n"; // $key;
			}
		}
	}
	
	function ShowAttachments()
	{
		$extra = '';
		$extra_ext = '';
		if(!perm('forum_attachments'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				if($_POST['del']>=1)
				{
					foreach($_POST['del'] as $filename => $del)
					{
						if( $del != "")
						{
							$result = $GLOBALS['db']->Query("DELETE FROM  ".PREFIX."_f_attachment WHERE filename = '$filename'");
							@unlink("../uploads/attachment/$filename");
							
						}
					}
				}
			}
		
			if(isset($_REQUEST['dl']) && $_REQUEST['dl']==1)
			{
				$query = "SELECT filename, orig_name FROM " . PREFIX . "_f_attachment WHERE id = '$_GET[id]'";
				$result = $GLOBALS['db']->Query($query);
				$file = $result->fetchrow();
				
				header("Cache-control: private");
				header("Content-type: application/octet-stream"); 
				header("Content-disposition:attachment; filename=" . $file->orig_name);
				header("Content-Length: " . filesize("../uploads/attachment/" . $file->filename));
				readfile("../uploads/attachment/" . $file->filename);
				exit();
			}
			//$limit = 20;
			$limit = (isset($_REQUEST['pp']) && $_REQUEST['pp']!='') ? $_REQUEST['pp'] : getlimit();
			$attachments = array();
			
			
			switch(@$_REQUEST['sort']){
				case "name":
				$ord = " ORDER BY  orig_name ASC";
				break;
				
				case "hits":
				$ord = "ORDER BY hits DESC";
				break;
				
				case "":
				$ord = "ORDER BY hits DESC";
				break;
				
			}
			
			if(isset($_REQUEST['q']) && $_REQUEST['q']!='') 
			{ 
				if(isset($_REQUEST['ext']) && $_REQUEST['ext']!='') $extra_ext = " AND ( (RIGHT(orig_name,3) = '$_REQUEST[ext]') OR (RIGHT(orig_name,4) = '$_REQUEST[ext]') OR (RIGHT(orig_name,5) = '$_REQUEST[ext]') )  ";
				$extra = "WHERE orig_name like '%$_REQUEST[q]%' $extra_ext ";
			}
			
			$query = "SELECT id FROM " . PREFIX . "_f_attachment $extra $ord";
			
			$result = $GLOBALS['db']->Query($query);
			$num = $result->numrows();
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$query = "SELECT * FROM " . PREFIX . "_f_attachment $extra $ord limit $a,$limit";
			$result = $GLOBALS['db']->Query($query);
			
			while($row = $result->fetchrow())
			{
				$row->sizes = @file_size(@filesize(BASE_DIR . "uploads/attachment/" . $row->filename)/1024);
				array_push($attachments, $row);
			}
			
			if($num>$limit) $GLOBALS['tmpl']->assign('navi', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?do=forums&amp;q=$_REQUEST[q]&amp;sub=showattachments&amp;sort=" . @$_REQUEST['sort'] . "&amp;pp=$limit&amp;page={s}\">{t}</a> "));
			$GLOBALS['tmpl']->assign('attachments', $attachments);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/attachments.tpl'));
		}
	}
	
	
	//=================================================================
	// Anlagen-Typen loeschen
	//=================================================================
	function DelAttachmentType($id)
	{
		if(!perm('forum_attachments'))
		{
			no_perm();
		} else {
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_allowed_files WHERE id = '$id'");
			header('Location:index.php?do=forums&sub=attachmentsettings');
			exit;
		}
	}
	
	//=================================================================
	// Anlagen-Berechtigungs-Manager
	//=================================================================
	function AttachmentSettings()
	{
		if(!perm('forum_attachments'))
		{
			no_perm();
		} else {
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_allowed_files SET Max_Groesse='".abs((int)$_POST['Max_Groesse'])."', Typen = '" . @implode('|', $_POST['Typen']) . "'");
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_f_allowed_files");
			$res->possibles = explode('|',$res->Typen);
			
			$possibles = explode('|',$res->TypenMoegliche);
			asort($possibles);
			
			$GLOBALS['tmpl']->assign('res',$res);
			$GLOBALS['tmpl']->assign('possibles',$possibles);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Forums_Att_title']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/forum/attachment_settings.tpl'));
		}
	}
	
	
	
	
	function defPermOut()
	{
		$default_mask['1'] = 
		"1, " . // kann forum sehen
		"1, " . // kann themen anderer sehen
		"1, " . // kann loeschkommentar sehen ( wird benutzt )
		"1, " . // kann forum durchsuchen
		"1, " . // kann dateianhaenge runterladen
		"1, " . // kann themen verfassen
		"1, " . // kann auf eigene themen antworten
		"1, " . // kann auf andere themen antworten
		"1, " . // kann anhaenge hochladen
		"1, " . // kann themen bewerten
		"1, " . // kann eigene beitraege bearbeiten
		"1, " . // kann eigene beitraege loeschen
		"1, " . // kann eigene themen verschieben
		"1, " . // kann eigene themen oeffnen/schliessen
		"1, " . // kann eigene themen loeschen
		"1, " . // kann fremde themen loeschen
		"1, " . // kann fremde themen bearbeiten
		"1, " . // kann themen oeffnen
		"1, " . // kann themen schliessen
		"1, " . // kann themen- typ aendern
		"1, " . // kann themen verschieben
		"1";    // kann themen 
		
		
		
		// Gaeste
		$default_mask['2'] = 
		"0, " . // kann forum sehen
		"0, " . // kann themen anderer sehen
		"0, " . // kann loeschkommentar sehen ( wird benutzt )
		"1, " . // kann forum durchsuchen
		"0, " . // kann dateianhaenge runterladen
		"0, " . // kann themen verfassen
		"0, " . // kann auf eigene themen antworten
		"0, " . // kann auf andere themen antworten
		"0, " . // kann anhaenge hochladen
		"0, " . // kann themen bewerten
		"0, " . // kann eigene beitraege bearbeiten
		"0, " . // kann eigene beitraege loeschen
		"0, " . // kann eigene themen verschieben
		"0, " . // kann eigene themen oeffnen/schliessen
		"0, " . // kann eigene themen loeschen
		"0, " . // kann fremde themen loeschen
		"0, " . // kann fremde themen bearbeiten
		"0, " . // kann themen oeffnen
		"0, " . // kann themen schliessen
		"0, " . // kann themen- typ aendern
		"0, " . // kann themen verschieben
		"0";    // kann themen 
		
		
		// Registrierte Benutzer
		$default_mask['3'] = 
		"1, " . // kann forum sehen
		"1, " . // kann themen anderer sehen
		"0, " . // kann loeschkommentar sehen ( wird benutzt )
		"1, " . // kann forum durchsuchen
		"1, " . // kann dateianhaenge runterladen
		"1, " . // kann themen verfassen
		"1, " . // kann auf eigene themen antworten
		"1, " . // kann auf andere themen antworten
		"1, " . // kann anhaenge hochladen
		"1, " . // kann themen bewerten
		"1, " . // kann eigene beitraege bearbeiten
		"0, " . // kann eigene beitraege loeschen
		"0, " . // kann eigene themen verschieben
		"0, " . // kann eigene themen oeffnen/schliessen
		"0, " . // kann eigene themen loeschen
		"0, " . // kann fremde themen loeschen
		"0, " . // kann fremde themen bearbeiten
		"0, " . // kann themen oeffnen
		"0, " . // kann themen schliessen
		"0, " . // kann themen- typ aendern
		"0, " . // kann themen verschieben
		"0";    // kann themen 
		
		
		
		// Moderatoren
		$default_mask['4'] = 
		"1, " . // kann forum sehen
		"1, " . // kann themen anderer sehen
		"0, " . // kann loeschkommentar sehen ( wird benutzt )
		"1, " . // kann forum durchsuchen
		"1, " . // kann dateianhaenge runterladen
		"1, " . // kann themen verfassen
		"1, " . // kann auf eigene themen antworten
		"1, " . // kann auf andere themen antworten
		"1, " . // kann anhaenge hochladen
		"1, " . // kann themen bewerten
		"1, " . // kann eigene beitraege bearbeiten
		"0, " . // kann eigene beitraege loeschen
		"1, " . // kann eigene themen verschieben
		"1, " . // kann eigene themen oeffnen/schliessen
		"0, " . // kann eigene themen loeschen
		"0, " . // kann fremde themen loeschen
		"1, " . // kann fremde themen bearbeiten
		"1, " . // kann themen oeffnen
		"1, " . // kann themen schliessen
		"1, " . // kann themen- typ aendern
		"1, " . // kann themen verschieben
		"0";    // kann themen 
		
		return $default_mask;
	}
	
	
	
	//============================================================
	// setzt den status des forums auf $status
	// auch die subforen werden beachtet
	//============================================================
	function AdminSwitchForumStatus($id, $status)
	{
		$q_close = "UPDATE " . PREFIX . "_f_forum SET status = $status WHERE id = $id";
		$r_close = $GLOBALS['db']->Query($q_close);
		$q_child = "
			SELECT f.id FROM 
			" . PREFIX . "_f_category AS c, 
			" . PREFIX . "_f_forum AS f 
			WHERE parent_id = $id AND f.category_id = c.id";
	
		$r_child = $GLOBALS['db']->Query($q_child);
		if ($r_child->numrows() == 0) return;
	
		while ($child = $r_child->fetchrow()) {
			$this->AdminSwitchForumStatus($child->id, $status);
		} 
		
		return;
		
	} 
	

	// ========================================================
	// FORUM KATEGORIE- LOESCHFUNKTION
	// 1. foren loeschen
	// 2. kategorie loeschen
	// ========================================================
	function AdminDeleteCategory($id)
	{ 
		// foren loeschen
		$result = $GLOBALS['db']->Query("SELECT id FROM " . PREFIX . "_f_forum WHERE category_id = '" . $id . "'");
	
		while ($forum = $result->fetchrow()) {
			$this->AdminDeleteForum($forum->id);
		} 
		// kategorie loeschen
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_category WHERE id = '" . $id . "'");
	} 
	
	
	// ========================================================
	// FORUM- LOESCHFUNKTION
	// 1. themen loeschen
	// 2. berechtigungen loeschen
	// 3. unterkategorien loeschen
	// 4. tforum loeschen 
	// ========================================================
	function AdminDeleteForum($id)
	{
		$result = $GLOBALS['db']->Query("SELECT id FROM " . PREFIX . "_f_topic WHERE forum_id = '" . $id . "'"); 
		// themen loeschen
		while ($topic = $result->fetchrow()) {
			$this->AdminDeleteTopic($topic->id);
		} 
		// berechtigungen loeschen
		$result = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_permissions WHERE forum_id = '" . $id . "'"); 
		// unterkategorien loeschen
		$result = $GLOBALS['db']->Query("SELECT id FROM " . PREFIX . "_f_category WHERE parent_id = '" . $id . "'");
	
		while ($category = $result->fetchrow()) {
			$this->AdminDeleteCategory($category->id);
		} 
		// forum loeschen
		$result = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_forum WHERE id = '" . $id . "'");
	} 
	
	
	// ========================================================
	// THEMEN- LOESCHFUNKTION
	// 1. alle beitraege zum thema loeschen
	// 2. das rating zum thema loeschen
	// 3. thema loeschen
	// ========================================================
	function AdminDeleteTopic($id)
	{
		$result = $GLOBALS['db']->Query("SELECT id FROM " . PREFIX . "_f_post WHERE topic_id = '" . $id . "'"); 
		// Beitraege loeschen
		while ($post = $result->fetchrow()) {
			$this->AdminDeletePost($post->id);
		} 
		// Rating loeschen
		$result = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_rating WHERE topic_id = '" . $id . "'"); 
		// Thema loeschen
		$result = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_topic WHERE id = '" . $id . "'");
	} 
	
	
	// ========================================================
	// BEITRAGS- LOESCHFUNKTION
	// 1. alle anhaenge loeschen
	// 2. anzahl der beitraege im thema reduzieren
	// 3. anzahl der beitraege des benutzers reduzieren
	// 4. beitrag loeschen
	// ========================================================
	function AdminDeletePost($id)
	{
		$post = $GLOBALS['db']->RowQuery("SELECT uid, attachment, topic_id FROM " . PREFIX . "_f_post WHERE id = '" . $id . "'");
		$attachments = @explode(";", $post->attachment); 
		// anhaenge loeschen
		foreach ($attachments as $attachment) {
			if ($attachment != "") {
				$file = $GLOBALS['db']->RowQuery("SELECT filename FROM " . PREFIX . "_f_attachment WHERE id = $attachment");
				// loesche aus dem dateisystem
				//  @unlink ("../uploads/attachment" . $file);
				// loesche aus der datenbank
				$result = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_attachment WHERE id = $attachment");
			} 
		} 
		// reduziere die anzahl der beitraege im thema
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_f_topic SET replies = replies - 1 WHERE id = " . $post->topic_id); 
		// reduziere die anzahl der beitraege des benutzers
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_benutzer SET Beitraege = Beitraege - 1 WHERE Id = " . $post->uid); 
		// loesche beitrag
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_f_post WHERE id = '" . $id . "'");
	} 

	
	function PossibleFiles()
	{
		$possible_files = array (
		"ZIP"  => "application/x-zip-compressed",
		"RAR"  => "application/x-rar-compressed",
		"PDF" => "application/pdf",
		"DOC" => "application/msword",
		"AI, EPS" => "application/postscript",
		"RTF" => "application/rtf",
		"PPS (Powerpoint)" => "application/vnd.ms-powerpoint",
		"RM (Audio &amp; Video)"  => "application/vnd.rn-realplayer",
		"HLP"  => "application/winhlp",
		"GTAR"  => "application/x-gtar",
		"GZ, GZIP, TGZ" => "application/x-gzip",
		"JS"  => "application/x-javascript",
		"LTX"  => "application/x-latex",
		"EXE"  => "application/x-msdownload",
		"SWF"  => "application/x-shockwafe-flash",
		"XML"  => "application/xml",
		// Audio
		"MIDI"  => "audio/midi",
		"MP3, MP2"  => "audio/mpeg",
		"WAV"  => "audio/wav",
		
		// Grafik
		"BMP" => "image/bmp",
		"GIF" => "image/gif",
		"JPEG, JPE, JPG" => "image/jpeg",
		"PNG" => "image/png",
		"TIFF, TIF" => "image/tiff",
		"PSD" => "image/x-photoshop",
		"ICO" => "image/x-icon",
		
		// Text
		"CSV" => "text/comma-separated-values",
		"CSS" => "text/css",
		"HTM, HTML, SHTML" => "text/html",
		"TEXT" => "text/plain",
		"WML" => "text/wml",
		"XHT, XHTM" => "text/xhtml",
		"XML" => "text/xml",
		
		
		// Video
		"MPEG, MPG, MPA, WMV" => "video/mpeg",
		"QT, MOV" => "video/quicktime",
		"AVI" => "video/x-msvideo",
		
		"XLS" => "application/vnd.ms-excel"
		);
		return $possible_files;
	}
	
	
	
	
	
	
	
}
?>