<?php 
//================================================================
// KOOBI
// C2002-2007 dream4
// Autor         : dream4 - http://www.dream4.de
// Koobi-Version : Standard + Pro
// Anmerkung     : -
// Geaendert am  : 13.12.2006
//================================================================
// 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
//================================================================
/*
require_once("init.php");
require_once("../inc/filetypes.php");
*/

if( (!perm('forums_cat_new')) && (!perm('forums_forum_new')) && (!perm('forums_cat_edit')) && (!perm('forums_forum_edit')) ) { echo error($langadmin['noperms']); exit(); }

#$tmpl = new Koobi("templates/" . ADMINTHEME . "/forum/");
#$GLOBALS['tmpl']->assign("theme", ADMINTHEME);
#$GLOBALS['tmpl']->assign("langadmin", $langadmin);
#$GLOBALS['tmpl']->assign("area", $area);
#$GLOBALS['tmpl']->assign("pref", $pref);
$GLOBALS['tmpl']->register_modifier("sslash", "stripslashes");
$GLOBALS['tmpl']->register_function("permission", "permission");

switch (@$_REQUEST['action']) {
	
	// ======================================================
	//  <<-- Attachments -->>
	// ======================================================
	case 'attachments':
	if(!perm("forums_forum_edit")) { echo error($langadmin['noperms']); exit(); }
	if($_REQUEST['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($_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 = ($_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($_REQUEST['q']!='') { 
		if($_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(BASEDIR . "uploads/attachment/" . $row->filename)/1024);
		array_push($attachments, $row);
	}
	
	if($num>$limit) $GLOBALS['tmpl']->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"forum.php?ext=$_REQUEST[ext]&amp;q=$_REQUEST[q]&amp;action=attachments&sort=$_REQUEST[sort]&pp=$limit&page={s}\">{t}</a> "));
	$GLOBALS['tmpl']->assign('attachments', $attachments);
	$tmpl->display('forum_attachments.tpl');
	break;
	// ======================================================
	//  <<-- Ansehen -->>
	// ======================================================
	case 'view':

	switch ($_GET['what'])
	{
		case 'overview':

		$categories = array();
		getCategoriesAdmin($_GET['id'], $categories, " - ");
		
		$GLOBALS['tmpl']->assign("categories", $categories);
		$tmpl->display("forums_overview.tpl");
		
		break;
		
		case 'del_topic':
		if(!perm('forums_del_topics')) { echo error($langadmin['noperms']); exit(); }
		$tmpl->display("delete_topic.tpl");
		break;
		
		case 'uploads':
		if(!perm('forums_allowed_files')) { echo error($langadmin['noperms']); exit(); }
		// erlaubte dateitypen holen
		$query = "SELECT id, filetype, filesize FROM " . PREFIX . "_f_allowed_files ORDER BY filetype";
		$result = $GLOBALS['db']->Query($query);
		
		$allowed_files = array();
		
		$allowed_mime_types = array();
		
		while ($allowed_file = $result->fetchrow())
		{
			$allowed_files[] = $allowed_file;
			$allowed_mime_types[] = $allowed_file->filetype;
		}
		
		// filtere bereits erlaubte dateitypen aus der liste
		// der moeglichen dateitypen aus
		$filtered_possible_files = array();
		foreach ($possible_files as $key => $value) {
			if (!@in_array($value, $allowed_mime_types)) {
				$filtered_possible_files[$key] = $value;
			}
		}
		
		$GLOBALS['tmpl']->assign("possible_files_keys", array_keys($filtered_possible_files));
		$GLOBALS['tmpl']->assign("possible_files", $filtered_possible_files);
		$GLOBALS['tmpl']->assign("allowed_files", $allowed_files);
		
		$tmpl->display("edit_uploads.tpl");
		break;
		
		case 'rank':
			$query = "SELECT id, title, count FROM " . PREFIX . "_f_rank";
			$result = $GLOBALS['db']->Query($query);
			$ranks = array();
			
			while ($entry = $result->fetchrow()) {
				$ranks[] = $entry;
			}
			
			$GLOBALS['tmpl']->assign("ranks", $ranks);
			$tmpl->display("rank_overview.tpl");
			
			break;
	}
	
	break;
	
	
	
	
	// ======================================================
	//  <<-- Bearbeiten -->>
	// ======================================================
	case 'edit':
	if(!perm('forums_cat_edit')) { echo error($langadmin['noperms']); exit(); }
	switch ($_GET['what']) {
		case 'category':
		$c_id = $_GET['id'];
		$query = "
		SELECT
		id,
		title,
		comment,
		position,
		group_id
		FROM
		" . PREFIX . "_f_category
		WHERE
		id = '$c_id'
		";
		
		$result = $GLOBALS['db']->Query($query);
		$category = $result->fetchrow();
		
		$category->group_id = @explode(",", $category->group_id);
		
		// gruppen
		$query = "
		SELECT
		ugroup,
		groupname
		FROM
		" . PREFIX . "_usergroup";
		
		$result = $GLOBALS['db']->Query($query);
		
		$groups = array();
		
		while ($group = $result->fetchrow()) {
			$groups[] = $group;
		}
		
		$GLOBALS['tmpl']->assign("groups", $groups);
		$GLOBALS['tmpl']->assign("category", $category);
		
		$tmpl->display("edit_category.tpl");
		
		break;
		
		case 'forum':
		$f_id = $_GET['id'];
		$query = "
		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
		";
		
		$result = $GLOBALS['db']->Query($query);
		$forum = $result->fetchrow();
		
		$forum->group_id = @explode(",", $forum->group_id);
		
		// gruppen
		$query = "
		SELECT
		ugroup,
		groupname
		FROM
		" . PREFIX . "_usergroup";
		
		$result = $GLOBALS['db']->Query($query);
		
		$groups = array();
		
		while ($group = $result->fetchrow()) {
			$groups[] = $group;
		}
		
		$GLOBALS['tmpl']->assign("forum", $forum);
		$GLOBALS['tmpl']->assign("groups", $groups);
		
		$tmpl->display("edit_forum.tpl");
		break;
		
		case 'permissions':
		$forum_id = $_GET['f_id'];
		$group_id = $_GET['g_id'];
		
		$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);
		
		$tmpl->display("edit_permissions.tpl");
		break;
		
		
		case 'mods':
		if(!perm('forums_mods')) { echo error($langadmin['noperms']); exit(); }
		$f_id = $_GET['id'];
		
		$query = "
		SELECT 
		u.uid,
		u.uname
		FROM
		" . PREFIX . "_user AS u,
		" . PREFIX . "_f_mods AS m
		WHERE
		u.uid = m.user_id AND
		m.forum_id = $f_id
		";
		
		$result = $GLOBALS['db']->Query($query);
		
		$mods = array();
		
		while ($mod = $result->fetchrow()) {
			$mods[] = $mod;
		}
		
		$GLOBALS['tmpl']->assign("mods", $mods);
		$tmpl->display("edit_mods.tpl");
		break;
	}
	break;
	
	
	
	
	// ======================================================
	//  <<-- Speichern -->>
	// ======================================================
	case 'save':
	switch ($_GET['what']) {
		case 'category':
		if(!perm('forums_cat_edit')) { echo error($langadmin['noperms']); exit(); }
		if (isset($_POST['title']) && $_POST['title'] == "") {
			echo error(str_replace("{0}", $langadmin['title'], $langadmin['forum_err_missing_field']));
			exit;
		}
		
		if (isset($_POST['position'])) {
			if ($_POST['position'] == "") {
				echo error(  str_replace("{0}", $langadmin['position'], $langadmin['forum_err_missing_field'])  );
				exit;
			} else if (!is_numeric($_POST['position'])) 
			{
				echo error(  str_replace("{0}", $langadmin['position'], $langadmin['forum_err_numeric_field'])  );
				exit;
			}
		}
		
		if ($_POST['c_id'] == "") 
		{
			$title = strip_tags($_POST['title']);
			$comment = ($_POST['comment'] != "") ? $_POST['comment'] : "";
			$position = $_POST['position'];
			$parent_id = ($_POST['f_id'] == "") ? 0 : $_POST['f_id'];
			
			$query = "
			INSERT INTO  " . PREFIX . "_f_category (
			id,
			title,
			position,
			comment,
			parent_id,
			group_id
			) VALUES (
			'', 
			'$title', 
			$position, 
			'$comment', 
			'$parent_id',
			'" . @implode(",", $_POST['group_id']) . "'
			)
			";
		} else {
			
			$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);
		
//      header("Location: forum.php?action=view&what=overview&id=0");
		echo '<script>window.opener.parent.frames[\'mainFrame\'].location.reload();window.close();</script>';
		break;
		
		case 'forum':
		if(!perm('forums_forum_edit')) { echo error($langadmin['noperms']); exit(); }
		if (isset($_POST['title']) && $_POST['title'] == "") {
			echo error(  str_replace("{0}", $langadmin['title'], $langadmin['forum_err_missing_field'])  );
			exit;
		}
		
		
		if ($_POST['f_id'] == "") {
			if(!perm('forums_forum_new')) { echo error($langadmin['noperms']); exit(); }
			// neues forum anlegen
			
			// 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 = ($_POST['moderated'] != "") ? 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 = $db->InsertId();
			
			// gruppen
			$q_groups = "SELECT ugroup FROM " . PREFIX . "_usergroup";
			$r_groups = $GLOBALS['db']->Query($q_groups);
			
			// standardmaske fuer die rechte einer gruppe
			include("perm_mask.php");
			
			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);
			}
		} else {
			if(!perm('forums_forum_edit')) { echo error($langadmin['noperms']); exit(); }
			// forum aktualisieren
			$password = ($_POST['password'] == "") ? '' : md5($_POST['password']);
			$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 = '$_POST[moderated]',
			moderated_posts = '$_POST[moderated_posts]',
			post_emails = '$_POST[post_emails]',
			topic_emails = '$_POST[topic_emails]'
			WHERE
			id = " . $_POST['f_id'];
			$result = $GLOBALS['db']->Query($query);
		}
		
		// Bug Fix 24 Aug 2007 by Olaf
		// saving the forum ID for future reference 
		// speicherung der Forum ID zur späteren Verwendung
			$forum_id = $db->InsertId();
		//}
		
		
		// Bug Fix 24 Aug 2007 by Olaf
		// using the right forum ID $forum_id instead of $db->InsertId()
		// which is wrong for new entries 
		// benutzung der richtigen Forum ID $forum_id da $db->InsertId() bei
		// neuen Einträgen die falsche ID wiedergibt 
		$f_id = ($_POST['f_id'] == "") ? $forum_id : $_POST['f_id'];
		
		// status aendern
		switchForumStatus($f_id, $_POST['status']);
		
		// header("Location: forum.php?action=view&what=overview&id=0");
		echo '<script>window.opener.parent.frames[\'mainFrame\'].location.reload();window.close();</script>';
		break;
		
		case 'permissions':
		if(!perm('forums_permissions')) { echo error($langadmin['noperms']); exit(); }
		$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'];
		// moderator- rechte
		//$permissions[FORUM_PERMISSIONS_CAN_MODERATE] = $_POST['can_moderate'];
		
		$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();
		?>
		<script>
		window.close();
		</script>
		
		<?php  
		// header("Location: forum.php?action=edit&what=permissions&g_id=" . $_POST['g_id'] . "&f_id=" . $_POST['f_id']);
		
		break;
		
		case 'position':
		if(!perm('forums_cat_edit')) { echo error($langadmin['noperms']); exit(); }
		// 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);
			}
		}
	if($_REQUEST['subaction']!='view')
		header("Location: forum.php?action=$_REQUEST[subaction]&what=$_REQUEST[what]&id=$_REQUEST[id]&area=$_REQUEST[area]");
	else
		header("Location: forum.php?action=view&what=overview&id=0");
		
		break;
		
		case "filesize":
		$keys = array_keys($_POST['filesize']);
		foreach ($keys as $key) {
			$query = "UPDATE " . PREFIX . "_f_allowed_files SET filesize = '" . $_POST['filesize'][$key] . "' WHERE id = $key";
			$result = $GLOBALS['db']->Query($query);
		}
		
		header("Location: forum.php?action=view&what=uploads");
		break;
		
		case 'rank':
			
			$rank_id = array_keys($_POST['count']);
			
			foreach ($rank_id as $id) {
				if (is_numeric($_POST['count'][$id])) {
					$query = "
						UPDATE
							" . PREFIX . "_f_rank
						SET
							count = '" . $_POST['count'][$id] . "',
							title = '" . $_POST['title'][$id] . "'
						WHERE
							id = '$id'";
					$result = $GLOBALS['db']->Query($query);
				}
			}
			
			header("Location: forum.php?action=view&what=rank");
		break;
	}
	
	break;
	
	case 'add' :
	
	switch ($_GET['what']) {
		case 'forum':
			if(!perm('forums_forum_new')) { echo error($langadmin['noperms']); exit(); }
			// gruppen
			$query = "
			SELECT
			ugroup,
			groupname
			FROM
			" . PREFIX . "_usergroup";
			
			$result = $GLOBALS['db']->Query($query);
			
			$groups = array();
			
			while ($group = $result->fetchrow()) {
				$groups[] = $group;
			}
			
			$GLOBALS['tmpl']->assign("groups", $groups);
			
			$tmpl->display('edit_forum.tpl');
			break;
			case 'category':
			if(!perm('forums_cat_new')) { echo error($langadmin['noperms']); exit(); }
			// gruppen
			$query = "
			SELECT
			ugroup,
			groupname
			FROM
			" . PREFIX . "_usergroup";
			
			$result = $GLOBALS['db']->Query($query);
			
			$groups = array();
			
			while ($group = $result->fetchrow()) {
				$groups[] = $group;
			}
			
			$GLOBALS['tmpl']->assign("groups", $groups);
			
			$tmpl->display('edit_category.tpl');
			
			break;
			
			case 'mods':
			if(!perm('forums_mods')) { echo error($langadmin['noperms']); exit(); }
			$query = "
			SELECT
			uid
			FROM
			" . PREFIX . "_user
			WHERE
			uname = '" . $_POST['user_name'] . "'
			";
			
			$result = $GLOBALS['db']->Query($query);
			$user = $result->fetchrow();
			
			if ($result->numrows() == 0) {
				header("Location: forum.php?action=edit&what=mods&id=" . $_POST['id'] . "&error=1");
			} else {
				$query = "
				INSERT INTO 
				" . PREFIX . "_f_mods (
				forum_id, 
				user_id
				) VALUES (
				" . $_POST['id'] . ", 
				" . $user->uid . "
				)";
				
				$result = $GLOBALS['db']->Query($query);
				header("Location: forum.php?action=edit&what=mods&id=" . $_POST['id']);
			}
		break;
		
		case 'filetype':
			if ($_POST['filesize'] == "") {
				header("Location: forum.php?action=view&what=uploads&error=1");
			}
			
			$query = "INSERT INTO " . PREFIX . "_f_allowed_files (id, filetype, filesize) VALUES ('', '" . $_POST['filetype'] . "', '" . $_POST['filesize'] . "')";
			$result = $GLOBALS['db']->Query($query);
			header("Location: forum.php?action=view&what=uploads");
		break;
		
		case 'rank':
			if ($_POST['title'] == "") {
				echo error(  str_replace("{0}", $langadmin['title'], $langadmin['forum_err_missing_field'])  );
				exit;
			}
			
			if ($_POST['count'] == "") {
				echo error(  str_replace("{0}", $langadmin['count'], $langadmin['forum_err_missing_field'])  );
				exit;
			}
			
			if (is_numeric($_POST['count'])) {
				$query = "INSERT INTO " . PREFIX . "_f_rank (id, title, count) VALUES ('', '" . $_POST['title'] . "', '" . $_POST['count'] . "')";
				$result = $GLOBALS['db']->Query($query);
			}
			
			header("Location: forum.php?action=view&what=rank");
		break;
		
	}
	
	break;
	
	
	
	case 'delete':
	
	
	switch ($_GET['what']) {
		
		case 'category':
		if(!perm('forums_cat_edit')) { echo error($langadmin['noperms']); exit(); }
		deleteCategory($_GET['id']);
		
		header("Location:forum.php?action=view&what=overview&id=0&area=$_REQUEST[area]");
		break;
		
		
		case 'forum':
		if(!perm('forums_forum_edit')) { echo error($langadmin['noperms']); exit(); }
		deleteForum($_GET['id']);
		header("Location:forum.php?action=view&what=overview&id=0&area=$_REQUEST[area]");
		break;
		
		case 'mods':
		if(!perm('forums_mods')) { echo error($langadmin['noperms']); exit(); }
		$forum_id = $_POST['id'];
		$user_id = $_POST['user_id'];
		
		$query = "DELETE FROM " . PREFIX . "_f_mods WHERE forum_id = $forum_id AND user_id = $user_id";
		$result = $GLOBALS['db']->Query($query);
		
		header("Location: forum.php?action=edit&what=mods&id=$forum_id");
		break;
		
		case 'topic':
		if(!perm('forums_del_topics')) { echo error($langadmin['noperms']); exit(); }
		$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[] = "((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(' OR ', $where_clausel);
		
		if (count($where_clausel) > 0) {
			// loesche die dazugehoerigen beitraege
			$result = $GLOBALS['db']->Query($query);
			
			while ($topic = $result->fetchrow()) {
				deleteTopic($topic->id);
			}
			
			header("Location: forum.php?action=view&what=overview&id=0");
		} else {
			$tmpl->display("delete_topic.tpl");
		}
		
		break;
		
		case 'filetype':
		$query = "DELETE FROM " . PREFIX . "_f_allowed_files WHERE id = " . $_GET['id'];
		$result = $GLOBALS['db']->Query($query);
		header("Location: forum.php?action=view&what=uploads");
		break;
		
		case 'rank':
			$query = "DELETE FROM " . PREFIX . "_f_rank WHERE id = '" . $_GET['id'] . "'";
			$result = $GLOBALS['db']->Query($query);
			header("Location: forum.php?action=view&what=rank");
		break;
	}
	
	break;
	
	
	case 'close':
	
	switch ($_GET['what']) {
		case 'forum':
		if(!perm('forums_forum_edit')) { echo error($langadmin['noperms']); exit(); }
		switchForumStatus($_GET['id'], FORUM_STATUS_CLOSED);
		header("Location: forum.php?action=view&what=overview&id=0");
		break;
	}
	
	break;
	
	
	case 'open':
	
	switch ($_GET['what']) {
		case 'forum':
		if(!perm('forums_forum_edit')) { echo error($langadmin['noperms']); exit(); }
		switchForumStatus($_GET['id'], FORUM_STATUS_OPEN);
		header("Location: forum.php?action=view&what=overview&id=0");
		break;
	}
	
	break;
}
?>