<?php

/*
	[Discuz!] (C)2001-2007 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: forums.inc.php,v $
	$Revision: 1.95.2.6 $
	$Date: 2007/04/30 13:03:56 $
*/

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
        exit('Access Denied');
}

cpheader();

if($action == 'forumadd')  {

	if((!submitcheck('catsubmit') && !submitcheck('forumsubmit'))) {
		$addforumtype = '';
		$groupselect = $forumselect = "<select name=\"fup\">\n";
		$query = $db->query("SELECT fid, name, type, fup FROM {$tablepre}forums WHERE type<>'sub' ORDER BY displayorder");
		while($fup = $db->fetch_array($query)) {
			$fups[] = $fup;
		}
		if(is_array($fups)) {
			foreach($fups as $forum1) {
				if($forum1['type'] == 'group') {
					if(isset($fupid) && $fupid == $forum1['fid']) {
						$fupselected = 'selected';
						$addforumtype = $forum1['type'];
					} else {
						$fupselected = '';
					}

					$groupselect .= "<option value=\"$forum1[fid]\" $fupselected>$forum1[name]</option>\n";
					$forumselect .= "<optgroup label=\"$forum1[name]\">\n";
					foreach($fups as $forum2) {
						if($forum2['type'] == 'forum' && $forum2['fup'] == $forum1['fid']) {
							if(isset($fupid) && $fupid == $forum2['fid']) {
								$fupselected = 'selected';
								$addforumtype = $forum2['type'];
							} else {
								$fupselected = '';
							}

							$forumselect .= "<option value=\"$forum2[fid]\" $fupselected>&nbsp; &gt; $forum2[name]</option>\n";
						}
					}
				}
			}
			foreach($fups as $forum0) {
				if($forum0['type'] == 'forum' && $forum0['fup'] == 0) {
					if(isset($fupid) && $fupid == $forum0['fid']) {
						$fupselected = 'selected';
						$addforumtype = $forum0['type'];
					} else {
						$fupselected = '';
					}
					if($forum0['type'] == 'group') {
						$groupselect .= "<option value=\"$forum0[fid]\" $fupselected>$forum0[name]</option>\n";
					} else {
						$forumselect .= "<option value=\"$forum0[fid]\" $fupselected>$forum0[name]</option>\n";
					}
				}
			}
		}
		$groupselect .= '</select>';
		$forumselect .= '</select>';

		$projectselect = "<select name=\"projectid\"><option value=\"0\" selected=\"selected\">".$lang['none']."</option>";
		$query = $db->query("SELECT id, name FROM {$tablepre}projects WHERE type='forum'");
		while($project = $db->fetch_array($query)) {
			$projectselect .= "<option value=\"$project[id]\">$project[name]</option>\n";
		}
		$projectselect .= '</select>';

		shownav('menu_forums_add');
		showtips('forums_add_tips');

		if(empty($addforumtype)) {

?>
<br><form method="post" action="admincp.php?action=forumadd&add=category">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="3"><?=$lang['forums_add_category']?></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['name']?>:</td>
<td class="altbg2" width="85%"><input type="text" name="newcat" value="<?=$lang['forums_add_category_name']?>" size="20"></td>
</table><br><center>
<input class="button" type="submit" name="catsubmit" value="<?=$lang['submit']?>"></center></form>
<?

		}

		if(empty($addforumtype) || $addforumtype == 'group') {

?>
<br><form method="post" action="admincp.php?action=forumadd&add=forum">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="2"><?=$lang['forums_add_forum']?></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['name']?>:</td>
<td class="altbg2" width="85%"><input type="text" name="newforum" value="<?=$lang['forums_add_forum_name']?>" size="20"></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['forums_add_parent_category']?>:</td>
<td class="altbg2" width="85%"><?=$groupselect?></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['forums_scheme']?></td>
<td class="altbg2" width="85%"><?=$projectselect?></td></tr></table><br>
<center><input class="button" type="submit" name="forumsubmit" value="<?=$lang['submit']?>"></center></form>
<?

		}

		if(empty($addforumtype) || $addforumtype == 'forum') {

?>
<br><form method="post" action="admincp.php?action=forumadd&add=forum">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="3"><?=$lang['forums_add_sub']?></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['name']?>:</td>
<td class="altbg2" width="28%"><input type="text" name="newforum" value="<?=$lang['forums_add_sub_name']?>" size="20"></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['forums_add_parent_forum']?>:</td>
<td class="altbg2" width="27%"><?=$forumselect?></td></tr>
<tr align="center"><td class="altbg1" width="15%"><?=$lang['forums_scheme']?></td>
<td class="altbg2" width="85%"><?=$projectselect?></td></tr></table><br>
<center><input class="button" type="submit" name="forumsubmit" value="<?=$lang['submit']?>"></center>
</form><br>
<?

		}

	} elseif(submitcheck('catsubmit')) {

		if(strlen($newcat) > 50) {
			cpmsg('forums_name_toolong');
		}

		$db->query("INSERT INTO {$tablepre}forums (type, name, status)
			VALUES ('group', '$newcat', '1')");
		$fid = $db->insert_id();

		$db->query("INSERT INTO {$tablepre}forumfields (fid)
			VALUES ('$fid')");

		updatecache('forums');
		cpmsg('forums_add_category_succeed', 'admincp.php?action=forumsedit');

	} elseif(submitcheck('forumsubmit')) {

		if(strlen($newforum) > 50) {
			cpmsg('forums_name_toolong');
		}

		if(!$fup) {
			cpmsg('forums_noparent');
		}

		$modarray = $forumfields = array();
		$table_forum_columns = array('fup', 'type', 'name', 'status', 'styleid', 'allowsmilies', 'allowhtml', 'allowbbcode', 'allowimgcode', 'allowanonymous', 'allowshare', 'allowpostspecial', 'alloweditrules', 'allowpaytoauthor', 'alloweditpost', 'modnewposts', 'recyclebin', 'jammer', 'forumcolumns', 'threadcaches', 'disablewatermark', 'autoclose', 'simple');
		$table_forumfield_columns = array('fid', 'attachextensions', 'threadtypes', 'postcredits', 'replycredits', 'digestcredits', 'postattachcredits', 'getattachcredits', 'viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');

		$query = $db->query("SELECT * FROM {$tablepre}forums WHERE fid='$fup'");
		$forum = $db->fetch_array($query);
		if(!empty($projectid)) {
			$query = $db->query("SELECT value FROM {$tablepre}projects WHERE id='$projectid'");
			$project = unserialize($db->result($query, 0));

			foreach($table_forum_columns as $field) {
				$forumfields[$field] = $project[$field];
			}

			foreach($table_forumfield_columns as $field) {
				$forumfields[$field] = $project[$field];
			}

		} else {
			$forumfields['allowsmilies'] = $forumfields['allowbbcode'] = $forumfields['allowimgcode'] = $forumfields['allowshare'] = 1;
			$forumfields['allowpostspecial'] = 15;
		}

		$forumfields['fup'] = $forum ? $fup : 0;
		$forumfields['type'] = $forum['type'] == 'forum' ? 'sub' : 'forum';
		$forumfields['name'] = $newforum;
		$forumfields['status'] = 1;

		$sql1 = $sql2 = $comma = '';
		foreach($table_forum_columns as $field) {
			if(isset($forumfields[$field])) {
				$sql1 .= "$comma$field";
				$sql2 .= "$comma'{$forumfields[$field]}'";
				$comma = ', ';
			}
		}

		$db->query("INSERT INTO {$tablepre}forums ($sql1) VALUES ($sql2)");
		$forumfields['fid'] = $fid = $db->insert_id();

		$sql1 = $sql2 = $comma = '';
		foreach($table_forumfield_columns as $field) {
			if(isset($forumfields[$field])) {
				$sql1 .= "$comma$field";
				$sql2 .= "$comma'{$forumfields[$field]}'";
				$comma = ', ';
			}
		}

		$db->query("INSERT INTO {$tablepre}forumfields ($sql1) VALUES ($sql2)");

		$query = $db->query("SELECT uid, inherited FROM {$tablepre}moderators WHERE fid='$fup'");
		while($mod = $db->fetch_array($query)) {
			if($mod['inherited'] || $forum['inheritedmod']) {
				$db->query("REPLACE INTO {$tablepre}moderators (uid, fid, inherited)
					VALUES ('$mod[uid]', '$fid', '1')");
			}
		}

		updatecache('forums');
		cpmsg('forums_add_forum_succeed', 'admincp.php?action=forumsedit');

	}

} elseif($action == 'forumsedit') {

	if(!submitcheck('editsubmit')) {
		shownav('menu_forums_edit');
		showtips('forums_tips');

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td><?=$lang['forums_edit']?></td></tr>
<tr><td class="altbg1"><br>
<form method="post" action="admincp.php?action=forumsedit">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<?

		$forums = $showedforums = array();
		$query = $db->query("SELECT f.fid, f.type, f.status, f.name, f.fup, f.displayorder, f.inheritedmod, ff.moderators
			FROM {$tablepre}forums f LEFT JOIN {$tablepre}forumfields ff USING(fid)
			ORDER BY f.type<>'group', f.displayorder");

		while($forum = $db->fetch_array($query)) {
			$forums[] = $forum;
		}
		for($i = 0; $i < count($forums); $i++) {
			if($forums[$i]['type'] == 'group') {
				echo '<ul>'.showforum($i, 'group');
				for($j = 0; $j < count($forums); $j++) {
					if($forums[$j]['fup'] == $forums[$i]['fid'] && $forums[$j]['type'] == 'forum') {
						echo '<ul>'.showforum($j);
						for($k = 0; $k < count($forums); $k++) {
							if($forums[$k]['fup'] == $forums[$j]['fid'] && $forums[$k]['type'] == 'sub') {
								echo '<ul>'.showforum($k, 'sub').'</ul>';
							}
						}
						echo '</ul>';
					}
				}
				echo '</ul>';
			} elseif(!$forums[$i]['fup'] && $forums[$i]['type'] == 'forum') {
				echo '<ul>'.showforum($i);
				for($j = 0; $j < count($forums); $j++) {
					if($forums[$j]['fup'] == $forums[$i]['fid'] && $forums[$j]['type'] == 'sub') {
						echo '<ul>'.showforum($j, 'sub').'</ul>';
					}
				}
				echo '</ul>';
			}
		}

		foreach($forums as $key => $forum) {
			if(!in_array($key, $showedforums)) {
				$db->query("UPDATE {$tablepre}forums SET fup='0', type='forum' WHERE fid='$forum[fid]'");
				echo '<ul>'.showforum($key).'</ul>';
			}
		}

		echo "<br><center><input class=\"button\" type=\"submit\" name=\"editsubmit\" value=\"$lang[submit]\"></center><br></form></td></tr></table>\n";

	} else {

		// read from groups
		$usergroups = array();
		$query = $db->query("SELECT groupid, type, creditshigher, creditslower FROM {$tablepre}usergroups");
		while($group = $db->fetch_array($query)) {
			$usergroups[$group['groupid']] = $group;
		}

		if(is_array($order)) {
			foreach($order as $fid => $value) {
				$db->query("UPDATE {$tablepre}forums SET displayorder='$order[$fid]' WHERE fid='$fid'");
			}
		}

		updatecache('forums');

		cpmsg('forums_update_succeed', 'admincp.php?action=forumsedit');
	}

} elseif($action == 'moderators' && $fid) {

	if(!submitcheck('modsubmit')) {

		$moderators = '';
		$query = $db->query("SELECT m.username, mo.* FROM {$tablepre}members m, {$tablepre}moderators mo WHERE mo.fid='$fid' AND m.uid=mo.uid ORDER BY mo.inherited, mo.displayorder");
		while($mod = $db->fetch_array($query)) {

			$moderators .= "<tr align=\"center\"><td class=\"altbg1\"><input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$mod[uid]\" ".($mod['inherited'] ? 'disabled' : '').">\n".
				"<td class=\"altbg2\"><a href=\"viewpro.php?uid=$mod[uid]\" target=\"_blank\">$mod[username]</a></td>\n".
				"<td class=\"altbg1\"><input type=\"text\" name=\"displayordernew[$mod[uid]]\" value=\"$mod[displayorder]\" size=\"2\"></td>\n".
				"<td class=\"altbg2\">".($mod['inherited'] ? '<b>'.$lang['yes'].'</b>' : $lang['no'])."</td></tr>\n";
		}

		if($forum['type'] == 'group' || $forum['type'] == 'sub') {
			$checked = $forum['type'] == 'group' ? 'checked' : '';
			$disabled = 'disabled';
		} else {
			$checked = $forum['inheritedmod'] ? 'checked' : '';
			$disabled = '';
		}
		shownav('forums_moderators_edit');
		showtips('forums_moderators_tips');
?>
<form method="post" action="admincp.php?action=moderators&fid=<?=$fid?>">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="4"><?=$lang['forums_moderators_edit']?> - <?=$forum['name']?></td></tr>
<tr align="center" class="category"><td><?=$lang['del']?></td><td><?=$lang['username']?></td><td><?=$lang['display_order']?></td><td><?=$lang['forums_moderators_inherited']?></td></tr>
<?=$moderators?>
<tr align="center"><td class="altbg1"><?=$lang['add_new']?></td><td class="altbg2"><input type='text' name="newmoderator" size="20"></td><td class="altbg1"><input type="text" name="newdisplayorder" size="2" value="0"></td><td class="altbg2">&nbsp;</td></tr>
<tr><td colspan="4" class="altbg2"><input class="checkbox" type="checkbox" name="inheritedmodnew" value="1" <?=$checked?> <?=$disabled?>> <?=$lang['forums_moderators_inherit']?></td></tr>
</table><br>
<center><input class="button" type="submit" name="modsubmit" value="<?=$lang['submit']?>"></center>
</form>
<?

	} else {

		if($forum['type'] == 'group') {
			$inheritedmodnew = 1;
		} elseif($forum['type'] == 'sub') {
			$inheritedmodnew = 0;
		}

		if(!empty($delete) || $newmoderator || (bool)$forum['inheritedmod'] != (bool)$inheritedmodnew) {

			$fidarray = $newmodarray = $origmodarray = array();

			if($forum['type'] == 'group') {
				$query = $db->query("SELECT fid FROM {$tablepre}forums WHERE type='forum' AND fup='$fid'");
				while($sub = $db->fetch_array($query)) {
					$fidarray[] = $sub['fid'];
				}
				$query = $db->query("SELECT fid FROM {$tablepre}forums WHERE type='sub' AND fup IN ('".implode('\',\'', $fidarray)."')");
				while($sub = $db->fetch_array($query)) {
					$fidarray[] = $sub['fid'];
				}
			} elseif($forum['type'] == 'forum') {
				$query = $db->query("SELECT fid FROM {$tablepre}forums WHERE type='sub' AND fup='$fid'");
				while($sub = $db->fetch_array($query)) {
					$fidarray[] = $sub['fid'];
				}
			}

			if(is_array($delete)) {
				foreach($delete as $uid) {
					$db->query("DELETE FROM {$tablepre}moderators WHERE uid='$uid' AND ((fid='$fid' AND inherited='0') OR (fid IN ('".implode('\',\'', $fidarray)."') AND inherited='1'))");
				}

				$excludeuids = 0;
				$deleteuids = '\''.implode('\',\'', $delete).'\'';
				$query = $db->query("SELECT uid FROM {$tablepre}moderators WHERE uid IN ($deleteuids)");
				while($mod = $db->fetch_array($query)) {
					$excludeuids .= ','.$mod['uid'];
				}

				$usergroups = array();
				$query = $db->query("SELECT groupid, type, radminid, creditshigher, creditslower FROM {$tablepre}usergroups");
				while($group = $db->fetch_array($query)) {
					$usergroups[$group['groupid']] = $group;
				}

				$query = $db->query("SELECT uid, groupid, credits FROM {$tablepre}members WHERE uid IN ($deleteuids) AND uid NOT IN ($excludeuids) AND adminid NOT IN (1,2)");
				while($member = $db->fetch_array($query)) {
					if($usergroups[$member['groupid']]['type'] == 'special' && $usergroups[$member['groupid']]['radminid'] != 3) {
						$adminidnew = -1;
						$groupidnew = $member['groupid'];
					} else {
						$adminidnew = 0;
						foreach($usergroups as $group) {
							if($group['type'] == 'member' && $member['credits'] >= $group['creditshigher'] && $member['credits'] < $group['creditslower']) {
								$groupidnew = $group['groupid'];
								break;
							}
						}
					}
					$db->query("UPDATE {$tablepre}members SET adminid='$adminidnew', groupid='$groupidnew' WHERE uid='$member[uid]'");
				}
			}

			if((bool)$forum['inheritedmod'] != (bool)$inheritedmodnew) {
				$query = $db->query("SELECT uid FROM {$tablepre}moderators WHERE fid='$fid' AND inherited='0'");
				while($mod = $db->fetch_array($query)) {
					$origmodarray[] = $mod['uid'];
					if(!$forum['inheritedmod'] && $inheritedmodnew) {
						$newmodarray[] = $mod['uid'];
					}
				}
				if($forum['inheritedmod'] && !$inheritedmodnew) {
					$db->query("DELETE FROM {$tablepre}moderators WHERE uid IN ('".implode('\',\'', $origmodarray)."') AND fid IN ('".implode('\',\'', $fidarray)."') AND inherited='1'");
				}
			}

			if($newmoderator) {
				$query = $db->query("SELECT uid FROM {$tablepre}members WHERE username='$newmoderator'");
				if(!$member = $db->fetch_array($query)) {
					cpmsg('members_edit_nonexistence');
				} else {
					$newmodarray[] = $member['uid'];
					$db->query("UPDATE {$tablepre}members SET groupid='3' WHERE uid='$member[uid]' AND adminid NOT IN (1,2,3,4,5,6,7,8,-1)");
					$db->query("UPDATE {$tablepre}members SET adminid='3' WHERE uid='$member[uid]' AND adminid NOT IN (1,2)");
					$db->query("REPLACE INTO {$tablepre}moderators (uid, fid, displayorder, inherited)
						VALUES ('$member[uid]', '$fid', '$newdisplayorder', '0')");
				}
			}

			foreach($newmodarray as $uid) {
				$db->query("REPLACE INTO {$tablepre}moderators (uid, fid, displayorder, inherited)
					VALUES ('$uid', '$fid', '$newdisplayorder', '0')");

				if($inheritedmodnew) {
					foreach($fidarray as $ifid) {
						$db->query("REPLACE INTO {$tablepre}moderators (uid, fid, inherited)
							VALUES ('$uid', '$ifid', '1')");
					}
				}
			}

			if($forum['type'] == 'group') {
				$inheritedmodnew = 1;
			} elseif($forum['type'] == 'sub') {
				$inheritedmodnew = 0;
			}
			$db->query("UPDATE {$tablepre}forums SET inheritedmod='$inheritedmodnew' WHERE fid='$fid'");

		}

		if(is_array($displayordernew)) {
			foreach($displayordernew as $uid => $order) {
				$db->query("UPDATE {$tablepre}moderators SET displayorder='$order' WHERE fid='$fid' AND uid='$uid'");
			}
		}

		$moderators = $tab = '';
		$query = $db->query("SELECT m.username FROM {$tablepre}members m, {$tablepre}moderators mo WHERE mo.fid='$fid' AND mo.inherited='0' AND m.uid=mo.uid ORDER BY mo.displayorder");
		while($mod = $db->fetch_array($query)) {
			$moderators .= $tab.addslashes($mod['username']);
			$tab = "\t";
		}
		$db->query("UPDATE {$tablepre}forumfields SET moderators='$moderators' WHERE fid='$fid'");

		cpmsg('forums_moderators_update_succeed', "admincp.php?action=moderators&fid=$fid");

	}

} elseif($action == 'forumsmerge') {

	if(!submitcheck('mergesubmit') || $source == $target) {

		require_once DISCUZ_ROOT.'./include/forum.func.php';
		require_once DISCUZ_ROOT.'./forumdata/cache/cache_forums.php';

		$forumselect = "<select name=\"%s\">\n<option value=\"\">&nbsp;&nbsp;> $lang[select]</option><option value=\"\">&nbsp;</option>".str_replace('%', '%%', forumselect()).'</select>';
		shownav('menu_forums_merge');

?>
<form method="post" action="admincp.php?action=forumsmerge">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="3"><?=$lang['forums_merge']?></td></tr>
<tr align="center"><td class="altbg1" width="40%"><?=$lang['forums_merge_source']?>:</td>
<td class="altbg2" width="60%"><?=sprintf($forumselect, "source")?></td></tr>
<tr align="center"><td class="altbg1" width="40%"><?=$lang['forums_merge_target']?>:</td>
<td class="altbg2" width="60%"><?=sprintf($forumselect, "target")?></td></tr>
</table><br><center><input class="button" type="submit" name="mergesubmit" value="<?=$lang['submit']?>"></center></form>
<?

	} else {

		$query = $db->query("SELECT COUNT(*) FROM {$tablepre}forums WHERE fid IN ('$source', '$target') AND type<>'group'");
		if(($db->result($query, 0)) != 2) {
			cpmsg('forums_nonexistence');
		}

		$query = $db->query("SELECT COUNT(*) FROM {$tablepre}forums WHERE fup='$source'");
		if($db->result($query, 0)) {
			cpmsg('forums_merge_source_sub_notnull');
		}

		$db->query("UPDATE {$tablepre}threads SET fid='$target' WHERE fid='$source'");
		$db->query("UPDATE {$tablepre}posts SET fid='$target' WHERE fid='$source'");

		$query = $db->query("SELECT threads, posts FROM {$tablepre}forums WHERE fid='$source'");
		$sourceforum = $db->fetch_array($query);

		$db->query("UPDATE {$tablepre}forums SET threads=threads+$sourceforum[threads], posts=posts+$sourceforum[posts] WHERE fid='$target'");
		$db->query("DELETE FROM {$tablepre}forums WHERE fid='$source'");
		$db->query("DELETE FROM {$tablepre}forumfields WHERE fid='$source'");
		$db->query("DELETE FROM {$tablepre}moderators WHERE fid='$source'");

		$query = $db->query("SELECT * FROM {$tablepre}access WHERE fid='$source'");
		while($access = $db->fetch_array($query)) {
			$db->query("INSERT INTO {$tablepre}access (uid, fid, allowview, allowpost, allowreply, allowgetattach)
				VALUES ('$access[uid]', '$target', '$access[allowview]', '$access[allowpost]', '$access[allowreply]', '$access[allowgetattach]')", 'SILENT');
		}
		$db->query("DELETE FROM {$tablepre}access WHERE fid='$source'");

		updatecache('forums');

		cpmsg('forums_merge_succeed', 'admincp.php?action=forumsedit');
	}

} elseif($action == 'forumdetail') {

	$perms = array('viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');

	$query = $db->query("SELECT *, f.fid AS fid FROM {$tablepre}forums f
		LEFT JOIN {$tablepre}forumfields ff USING (fid)
		WHERE f.fid='$fid'");

	if(!$forum = $db->fetch_array($query)) {
		cpmsg('forums_nonexistence');
	}

	$query = $db->query("SELECT disabledactions FROM {$tablepre}adminactions WHERE admingid='$groupid'");
	$dactionarray = ($dactionarray = unserialize($db->result($query, 0))) ? $dactionarray : array();
	$allowthreadtypes = !in_array('threadtypes', $dactionarray);

	if(!empty($projectid)) {
		$query = $db->query("SELECT value FROM {$tablepre}projects WHERE id='$projectid'");
		$forum = @array_merge($forum, unserialize($db->result($query, 0)));
	}

	if(!submitcheck('detailsubmit') && !submitcheck('saveconfigsubmit')) {
		shownav('menu_forums_detail');

?>
<script language="JavaScript">
var typenum = 1;
function addtype() {
	if(typenum > 9) return;
	$('type_' + typenum).style.display = '';
	typenum++;
}
</script>
<form method="post" action="admincp.php?action=forumdetail&fid=<?=$fid?>&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<input type="hidden" name="type" value="<?=$forum['type']?>">
<input type="hidden" name="detailsubmit" value="submit">
<?

		if($forum['type'] == 'group') {

			showtype("$lang[forums_cat_detail] - $forum[name]", 'top');
			showsetting('forums_cat_name', 'namenew', $forum['name'], 'text');
			showsetting('forums_sub_horizontal', 'forumcolumnsnew', $forum['forumcolumns'], 'text');
			showtype('', 'bottom');

		} else {
			showtips('forums_edit_tips');
			$projectselect = "<select name=\"projectid\" onchange=\"window.location='admincp.php?action=forumdetail&fid=$fid&projectid='+this.options[this.options.selectedIndex].value\"><option value=\"0\" selected=\"selected\">".$lang['none']."</option>";
			$query = $db->query("SELECT id, name FROM {$tablepre}projects WHERE type='forum'");
			while($project = $db->fetch_array($query)) {
				$projectselect .= "<option value=\"$project[id]\" ".($project['id'] == $projectid ? 'selected="selected"' : NULL).">$project[name]</option>\n";
			}
			$projectselect .= '</select>';

			$fupselect = "<select name=\"fupnew\">\n";
			$query = $db->query("SELECT fid, type, name, fup FROM {$tablepre}forums WHERE fid<>'$fid' AND type<>'sub' ORDER BY displayorder");
			while($fup = $db->fetch_array($query)) {
				$fups[] = $fup;
			}
			if(is_array($fups)) {
				foreach($fups as $forum1) {
					if($forum1['type'] == 'group') {
						$selected = $forum1['fid'] == $forum['fup'] ? "selected=\"selected\"" : NULL;
						$fupselect .= "<option value=\"$forum1[fid]\" $selected>$forum1[name]</option>\n";
						foreach($fups as $forum2) {
							if($forum2['type'] == 'forum' && $forum2['fup'] == $forum1['fid']) {
								$selected = $forum2['fid'] == $forum['fup'] ? "selected=\"selected\"" : NULL;
								$fupselect .= "<option value=\"$forum2[fid]\" $selected>&nbsp; &gt; $forum2[name]</option>\n";
							}
						}
					}
				}
				foreach($fups as $forum0) {
					if($forum0['type'] == 'forum' && $forum0['fup'] == 0) {
						$selected = $forum0['fid'] == $forum['fup'] ? "selected=\"selected\"" : NULL;
						$fupselect .= "<option value=\"$forum0[fid]\" $selected>$forum0[name]</option>\n";
					}
				}
			}
			$fupselect .= '</select>';

			$groups = array();
			$query = $db->query("SELECT groupid, grouptitle FROM {$tablepre}usergroups");
			while($group = $db->fetch_array($query)) {
				$groups[] = $group;
			}

			$styleselect = "<select name=\"styleidnew\"><option value=\"0\">$lang[use_default]</option>";
			$query = $db->query("SELECT styleid, name FROM {$tablepre}styles");
			while($style = $db->fetch_array($query)) {
				$styleselect .= "<option value=\"$style[styleid]\" ".
					($style['styleid'] == $forum['styleid'] ? 'selected="selected"' : NULL).
					">$style[name]</option>\n";
			}
			$styleselect .= '</select>';

			if($forum['autoclose']) {
				$acoption = $forum['autoclose'] / abs($forum['autoclose']);
				$forum['autoclose'] = abs($forum['autoclose']);
			} else {
				$acoption = 0;
			}
			$checkac = array($acoption => 'checked');

			$checkpostspecial = array();
			$forum['allowpostspecial'] = sprintf('%04b', $forum['allowpostspecial']);
			for($i = 1; $i <= 4; $i++) {
				$checkpostspecial[$i] = $forum['allowpostspecial'][4 - $i] ? 'checked' : '';
			}

			$checkmod = array($forum['modnewposts'] => 'checked');
			$checkrules = array($forum['alloweditrules'] => 'checked');

			$viewaccess = $postaccess = $replyaccess = $getattachaccess = $postattachaccess = '';

			$query = $db->query("SELECT m.username, a.* FROM {$tablepre}access a LEFT JOIN {$tablepre}members m USING (uid) WHERE fid='$fid'");
			while($access = $db->fetch_array($query)) {
				$member = ", <a href=\"admincp.php?action=access&uid=$access[uid]\" target=\"_blank\">$access[username]</a>";
				$viewaccess .= $access['allowview'] ? $member : NULL;
				$postaccess .= $access['allowpost'] ? $member : NULL;
				$replyaccess .= $access['allowreply'] ? $member : NULL;
				$getattachaccess .= $access['allowgetattach'] ? $member : NULL;
				$postattachaccess .= $access['allowpostattach'] ? $member : NULL;
			}
			unset($member);


                        if($forum['status'] != 2) {
        			$typeids = $comma = '';
        			if($forum['threadtypes']) {
        				$forum['threadtypes'] = unserialize($forum['threadtypes']);
        				$forum['threadtypes']['status'] = 1;
        				$flatarray = is_array($forum['threadtypes']['flat']) ? $forum['threadtypes']['flat'] : array();
        				$selectboxarray = is_array($forum['threadtypes']['selectbox']) ? $forum['threadtypes']['selectbox'] : array();
        				$typearray = array_merge($flatarray, $selectboxarray);

        				if(is_array($forum['threadtypes'][types]) && $forum['threadtypes'][types]) {
        					foreach($forum['threadtypes'][types] AS $typeid => $typename) {
        						$typeids .= $comma.$typeid;
        						$comma = ',';
        					}
        				}
        			} else {
        				$forum['threadtypes'] = array('status' => 0, 'required' => 0, 'listable' => 0, 'prefix' => 0, 'options' => array());
        			}

        			$typeselect = '';

        			$query = $db->query("SELECT * FROM {$tablepre}threadtypes ORDER BY displayorder");
        			while($type = $db->fetch_array($query)) {

        				$typeselected = array();
        				if(isset($forum['threadtypes']['flat'][$type['typeid']])) {
        					$typeselected[1] = 'checked';
        				} elseif(isset($forum['threadtypes']['selectbox'][$type['typeid']])) {
        					$typeselected[2] = 'checked';
        				} else {
        					$typeselected[0] = 'checked';
        				}
        				$typeselect .= "<tr align=\"center\"><td class=\"altbg1\">$type[name]</td>".
        					"<td class=\"altbg2\">$type[description]</td>".
        					"<td class=\"altbg1\"><input class=\"radio\" type=\"radio\" name=\"threadtypesnew[options][{$type[typeid]}]\" value=\"0\" $typeselected[0]></td>".
        					"<td class=\"altbg2\"><input class=\"radio\" type=\"radio\" name=\"threadtypesnew[options][{$type[typeid]}]\" value=\"1\" $typeselected[1]></td>".
        					"<td class=\"altbg1\"><input class=\"radio\" type=\"radio\" name=\"threadtypesnew[options][{$type[typeid]}]\" value=\"2\" $typeselected[2]></td>".
        					"</tr>";
        			}

        			$typeselect = $typeselect ? $typeselect : '<tr><td class="altbg1" colspan="5">'.$lang['forums_edit_threadtypes_options_null'].'</td></tr>';
                        }

			$forum['postcredits'] = $forum['postcredits']? unserialize($forum['postcredits']) : array();
			$forum['replycredits'] = $forum['replycredits']? unserialize($forum['replycredits']) : array();
			$forum['digestcredits'] = $forum['digestcredits']? unserialize($forum['digestcredits']) : array();
			$forum['postattachcredits'] = $forum['postattachcredits']? unserialize($forum['postattachcredits']) : array();
			$forum['getattachcredits'] = $forum['getattachcredits']? unserialize($forum['getattachcredits']) : array();
			$forum['circle'] = $forum['status'] == 2 ? 1 : 0;
			showtype('menu_forums_detail', 'top');
			showsetting('forums_scheme', '', '', $projectselect);

			showtype("forums_basic_settings");

			showsetting('forums_edit_name', 'namenew', $forum['name'], 'text');
			showsetting('forums_edit_display', 'statusnew', $forum['status'], 'radio');
			showsetting('forums_edit_perm_passwd', 'passwordnew', $forum['password'], 'text');
			showsetting('forums_edit_up', '', '', $fupselect);
			showsetting('forums_edit_redirect', 'redirectnew', $forum['redirect'], 'text');
			showsetting('forums_edit_icon', 'iconnew', $forum['icon'], 'text');
			showsetting('forums_edit_description', 'descriptionnew', $forum['description'], 'textarea');
			showsetting('forums_edit_rules', 'rulesnew', $forum['rules'], 'textarea');

			showtype('forums_extend_conf');
			showsetting('forums_edit_style', '', '', $styleselect);
			showsetting('forums_sub_horizontal', 'forumcolumnsnew', $forum['forumcolumns'], 'text');
			showsetting('forums_threadcache', 'threadcachesnew', $forum['threadcaches'], 'text');
			showsetting('forums_edit_simple', 'simplenew', $forum['simple'], 'radio');
			showsetting('forums_edit_edit_rules', '', '', '<input class="radio" type="radio" name="alloweditrulesnew" value="0" '.$checkrules[0].'> '.$lang['forums_edit_edit_rules_html_none'].'<br><input class="radio" type="radio" name="alloweditrulesnew" value="1" '.$checkrules[1].'> '.$lang['forums_edit_edit_rules_html_no'].'<br><input class="radio" type="radio" name="alloweditrulesnew" value="2" '.$checkrules[2].'> '.$lang['forums_edit_edit_rules_html_yes']);
			if($supe['status']) {
			        if($supe['circlestatus']) {
			        	showsetting('circle_forum', 'circlenew', $forum['circle'], 'radio');
			        }
				$forum['supe_pushsetting'] = unserialize($forum['supe_pushsetting']);
				$forum['supe_pushsetting']['status'] = intval($forum['supe_pushsetting']['status']);
				$forum['supe_pushsetting']['filter']['digest'] = intval($forum['supe_pushsetting']['filter']['digest']);
				$forum['supe_pushsetting']['filter']['displayorder'] = intval($forum['supe_pushsetting']['filter']['displayorder']);
				$supe_pushsetchecked = array($forum['supe_pushsetting']['status'] => ' checked');
				$supe_pushsetdigestselected = array($forum['supe_pushsetting']['filter']['digest'] => ' selected');
				$supe_pushsetdisplayorderselected = array($forum['supe_pushsetting']['filter']['displayorder'] => ' selected');
				echo '<tr><td class="altbg1"><b>'.$lang['supe_collection_mode'].'</b><br>'.$lang['supe_collection_mode_comment'].'</td>';
				echo '<td class="altbg2"><input class="radio" type="radio" name="supe_pushsetting[status]" value="0" '.$supe_pushsetchecked[0].' onclick="$(\'supe_pushsetting_filter_div\').style.display=\'none\'">'.$lang['supe_pushsetting_status_0'].'<br><input class="radio" type="radio" name="supe_pushsetting[status]" value="1" '.$supe_pushsetchecked[1].' onclick="$(\'supe_pushsetting_filter_div\').style.display=\'none\'">'.$lang['supe_pushsetting_status_1'].'<br><input class="radio" type="radio" name="supe_pushsetting[status]" value="2" '.$supe_pushsetchecked[2].' onclick="$(\'supe_pushsetting_filter_div\').style.display=\'none\'">'.$lang['supe_pushsetting_status_2'].'<br><input class="radio" type="radio" name="supe_pushsetting[status]" value="3" '.$supe_pushsetchecked[3].' onclick="$(\'supe_pushsetting_filter_div\').style.display=\'block\'">'.$lang['supe_pushsetting_status_3'];
				echo '<div id="supe_pushsetting_filter_div" style="display:'.($forum['supe_pushsetting']['status'] == 3 ? 'block' : 'none').'">';
					echo '<br>'.$lang['supe_pushsetting_views'].' >= <input type="input" name="supe_pushsetting[filter][views]" value="'.$forum['supe_pushsetting']['filter']['views'].'" size="8">';
					echo '<br>'.$lang['supe_pushsetting_replys'].' >= <input type="input" name="supe_pushsetting[filter][replies]" value="'.$forum['supe_pushsetting']['filter']['replies'].'" size="8">';
					echo '<br>'.$lang['supe_pushsetting_digest'].' >= <select name="supe_pushsetting[filter][digest]"><option value="0"'.$supe_pushsetdigestselected[0].'></option><option value="1"'.$supe_pushsetdigestselected[1].'>'.$lang['forums_digest_one'].'</option><option value="2"'.$supe_pushsetdigestselected[2].'>'.$lang['forums_digest_two'].'</option><option value="3"'.$supe_pushsetdigestselected[3].'>'.$lang['forums_digest_three'].'</option></select>';
					echo '<br>'.$lang['supe_pushsetting_stick'].' >= <select name="supe_pushsetting[filter][displayorder]"><option value="0"'.$supe_pushsetdisplayorderselected[0].'></option><option value="1"'.$supe_pushsetdisplayorderselected[1].'>'.$lang['forums_stick_one'].'</option><option value="2"'.$supe_pushsetdisplayorderselected[2].'>'.$lang['forums_stick_two'].'</option><option value="3"'.$supe_pushsetdisplayorderselected[3].'>'.$lang['forums_stick_three'].'</option></select>';
				echo '</div></td></tr>';
				unset($supe_pushsetchecked, $supe_pushsetdigestselected, $supe_pushsetdisplayorderselected);
			}

			showtype('forums_edit_options');
			showsetting('forums_edit_modposts', '', '', '<input class="radio" type="radio" name="modnewpostsnew" value="0" '.$checkmod[0].'> '.$lang['none'].'<br><input class="radio" type="radio" name="modnewpostsnew" value="1" '.$checkmod[1].'> '.$lang['forums_edit_modposts_threads'].'<br><input class="radio" type="radio" name="modnewpostsnew" value="2" '.$checkmod[2].'> '.$lang['forums_edit_modposts_posts']);
			showsetting('forums_edit_alloweditpost', 'alloweditpostnew', $forum['alloweditpost'], 'radio');
			showsetting('forums_edit_recyclebin', 'recyclebinnew', $forum['recyclebin'], 'radio');
			showsetting('forums_edit_share', 'allowsharenew', $forum['allowshare'], 'radio');
			showsetting('forums_edit_html', 'allowhtmlnew', $forum['allowhtml'], 'radio');
			showsetting('forums_edit_bbcode', 'allowbbcodenew', $forum['allowbbcode'], 'radio');
			showsetting('forums_edit_imgcode', 'allowimgcodenew', $forum['allowimgcode'], 'radio');
			showsetting('forums_edit_smilies', 'allowsmiliesnew', $forum['allowsmilies'], 'radio');
			showsetting('forums_edit_jammer', 'jammernew', $forum['jammer'], 'radio');
			showsetting('forums_edit_anonymous', 'allowanonymousnew', $forum['allowanonymous'], 'radio');
			showsetting('forums_edit_disablewatermark', 'disablewatermarknew', $forum['disablewatermark'], 'radio');
			showsetting('forums_edit_trade_payto', 'allowpaytoauthornew', $forum['allowpaytoauthor'], 'radio');
			showsetting('forums_edit_allowpostspecial', '', '', '<input class="checkbox" type="checkbox" name="allowpostspecialnew[1]" value="1" '.$checkpostspecial[1].'>'.$lang['forums_thread_poll'].'<br><input class="checkbox" type="checkbox" name="allowpostspecialnew[2]" value="1" '.$checkpostspecial[2].'>'.$lang['forums_thread_trade'].'<br><input class="checkbox" type="checkbox" name="allowpostspecialnew[3]" value="1" '.$checkpostspecial[3].'>'.$lang['forums_thread_reward'].'<br><input class="checkbox" type="checkbox" name="allowpostspecialnew[4]" value="1" '.$checkpostspecial[4].'>'.$lang['forums_thread_activity']);
			showsetting('forums_edit_allowspecialonly', 'allowspecialonlynew', $forum['allowspecialonly'], 'radio');
			showsetting('forums_edit_autoclose', '', '', '<input class="radio" type="radio" name="autoclosenew" value="0" '.$checkac[0].' onclick="this.form.autoclosetimenew.disabled=true;"> '.$lang['forums_edit_autoclose_none'].'<br><input class="radio" type="radio" name="autoclosenew" value="1" '.$checkac[1].' onclick="this.form.autoclosetimenew.disabled=false;"> '.$lang['forums_edit_autoclose_dateline'].'<br><input class="radio" type="radio" name="autoclosenew" value="-1" '.$checkac[-1].' onclick="this.form.autoclosetimenew.disabled=false;"> '.$lang['forums_edit_autoclose_lastpost']);
			showsetting('forums_edit_autoclose_time', '', '', '<input type="text" size="30" value="'.$forum['autoclose'].'" name="autoclosetimenew" '.($acoption ? '' : 'disabled').'>');
			showsetting('forums_edit_attach_ext', 'attachextensionsnew', $forum['attachextensions'], 'text');

			showtype('forums_edit_credits');
			showsetting('forums_edit_postcredits', 'postcreditsstatus', $forum['postcredits'], 'radio');
			showsetting('forums_edit_replycredits', 'replycreditsstatus', $forum['replycredits'], 'radio');
			showsetting('settings_digest_add_credits', 'digestcreditsstatus', $forum['digestcredits'], 'radio');
			showsetting('settings_postattachment_add_cridits', 'postattachcreditsstatus', $forum['postattachcredits'], 'radio');
			showsetting('settings_digest_cut_credits', 'getattachcreditsstatus', $forum['getattachcredits'], 'radio');
			echo '</table><br><table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">'.
				'<tr class="header"><td>'.$lang['credits_id'].'</td><td>'.$lang['credits_title'].'</td><td>'.$lang['forums_edit_postcredits_add'].'</td><td>'.$lang['forums_edit_replycredits_add'].'</td><td>'.$lang['settings_credits_policy_digest'].'</td><td>'.$lang['settings_credits_policy_post_attach'].'</td><td>'.$lang['settings_credits_policy_get_attach'].'</td></tr>';
			for($i = 1; $i <= 8; $i++) {
				echo "<tr align=\"center\" ".(isset($extcredits[$i]) ? '' : 'disabled')."><td class=\"altbg1\" width=\"10%\">extcredits$i</td>".
					"<td class=\"altbg2\" width=\"10%\">{$extcredits[$i]['title']}</td>".
					"<td class=\"altbg1\" width=\"12%\"><input type=\"text\" size=\"2\" name=\"postcreditsnew[$i]\" value=\"".(isset($forum['postcredits'][$i]) ? $forum['postcredits'][$i] : 0)."\"></td>".
					"<td class=\"altbg2\" width=\"12%\"><input type=\"text\" size=\"2\" name=\"replycreditsnew[$i]\" value=\"".(isset($forum['replycredits'][$i]) ? $forum['replycredits'][$i] : 0)."\"></td>".
					"<td class=\"altbg1\" width=\"12%\"><input type=\"text\" size=\"2\" name=\"digestcreditsnew[$i]\" value=\"".(isset($forum['digestcredits'][$i]) ? $forum['digestcredits'][$i] : 0)."\"></td>".
					"<td class=\"altbg2\" width=\"12%\"><input type=\"text\" size=\"2\" name=\"postattachcreditsnew[$i]\" value=\"".(isset($forum['postattachcredits'][$i]) ? $forum['postattachcredits'][$i] : 0)."\"></td>".
					"<td class=\"altbg1\" width=\"12%\"><input type=\"text\" size=\"2\" name=\"getattachcreditsnew[$i]\" value=\"".(isset($forum['getattachcredits'][$i]) ? $forum['getattachcredits'][$i] : 0)."\"></td></tr>";
			}

			if($allowthreadtypes && $forum['status'] != 2) {
				showtype('forums_edit_threadtypes');
				showsetting('forums_edit_threadtypes_status', 'threadtypesnew[status]', $forum['threadtypes']['status'], 'radio');
				showsetting('forums_edit_threadtypes_required', 'threadtypesnew[required]', $forum['threadtypes']['required'], 'radio');
				showsetting('forums_edit_threadtypes_listable', 'threadtypesnew[listable]', $forum['threadtypes']['listable'], 'radio');
				showsetting('forums_edit_threadtypes_prefix', 'threadtypesnew[prefix]', $forum['threadtypes']['prefix'], 'radio');
				echo '</table><br><table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">'.
					'<tr class="header"><td>'.$lang['forums_edit_cat_name'].'</td><td>'.$lang['forums_sort_note'].'</td><td width="20%">'.$lang['not_use'].'</td><td width="20%">'.$lang['forums_threadtypes_use_cols'].'</td><td width="20%">'.$lang['forums_threadtypes_use_choice'].'</td></tr>'.
					$typeselect.'<tr><td colspan=5>'.$lang['add_new'].'<a href="###" onclick="addtype()">[+]</a></td></tr><tbody id="type_0"><tr align="center"><td class="altbg1"><input type="text" name="newname[0]" size="15"></td><td class="altbg2"><input type="text" name="newdescription[0]" size="15"></td><td class="altbg1"><input class="radio" type="radio" name="newoptions[0]" value="0"></td><td class="altbg2"><input class="radio" type="radio" name="newoptions[0]" value="1" checked></td><td class="altbg1"><input class="radio" type="radio" name="newoptions[0]" value="2"></td></tr></tbody>';

				for($i = 1; $i < 10; $i++) {
					echo '<tbody id="type_'.$i.'" style="display: none"><tr align="center"><td class="altbg1"><input type="text" name="newname['.$i.']" size="15"></td><td class="altbg2"><input type="text" name="newdescription['.$i.']" size="15"></td><td class="altbg1"><input class="radio" type="radio" name="newoptions['.$i.']" value="0"></td><td class="altbg2"><input class="radio" type="radio" name="newoptions['.$i.']" value="1" checked></td><td class="altbg1"><input class="radio" type="radio" name="newoptions['.$i.']" value="2"></td></tr></tbody>';
				}

				echo '<tr><td colspan=5>'.$lang['settings_threadtypes_comment'].'</td></tr></table>';
			}

			echo '<br><table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">'.
				'<tr class="header"><td>'.$lang['forums_edit_perm'].'</td>'.
				'<td><input class="checkbox" type="checkbox" name="chkall1" onclick="checkall(this.form, \'viewperm\', \'chkall1\')"> '.$lang['forums_edit_perm_view'].'</td>'.
				'<td><input class="checkbox" type="checkbox" name="chkall2" onclick="checkall(this.form, \'postperm\', \'chkall2\')"> '.$lang['forums_edit_perm_post'].'</td>'.
				'<td><input class="checkbox" type="checkbox" name="chkall3" onclick="checkall(this.form, \'replyperm\', \'chkall3\')"> '.$lang['forums_edit_perm_reply'].'</td>'.
				'<td><input class="checkbox" type="checkbox" name="chkall4" onclick="checkall(this.form, \'getattachperm\', \'chkall4\')"> '.$lang['forums_edit_perm_get_attach'].'</td>'.
				'<td><input class="checkbox" type="checkbox" name="chkall5" onclick="checkall(this.form, \'postattachperm\', \'chkall5\')"> '.$lang['forums_edit_perm_post_attach'].'</td></tr>';
			foreach($groups as $group) {
				echo '<tr><td class="altbg1"><input class="checkbox" title="'.$lang['select_all'].'" type="checkbox" name="chkallv'.$group['groupid'].'" onclick="checkallvalue(this.form, '.$group['groupid'].', \'chkallv'.$group['groupid'].'\')"> '.$group[grouptitle].'</td>';
				$altbgthis = 'altbg1';
				foreach($perms as $perm) {
					$checked = strstr($forum[$perm], "\t$group[groupid]\t") ? 'checked' : NULL;
					$altbgthis = $altbgthis == 'altbg2' ? 'altbg1' : 'altbg2';
					echo '<td class="'.$altbgthis.'"><input class="checkbox" type="checkbox" name="'.$perm.'[]" value="'.$group['groupid'].'" '.$checked.'></td>';
				}
				echo '</tr>';
			}
			echo '</td></tr><tr><td colspan="6">'.$lang['forums_edit_perm_comment'].'</td></tr>';

			echo '<tr class="header"><td colspan="6"><b>'.$lang['forums_edit_access_mask'].'</b></td></tr>';
			echo '<tr><td class="altbg1">'.$lang['forums_edit_perm_view'].':</td><td class="altbg2" colspan="5">'.substr($viewaccess, 2).'</td></tr>';
			echo '<tr><td class="altbg1">'.$lang['forums_edit_perm_post'].':</td><td class="altbg2" colspan="5">'.substr($postaccess, 2).'</td></tr>';
			echo '<tr><td class="altbg1">'.$lang['forums_edit_perm_reply'].':</td><td class="altbg2" colspan="5">'.substr($replyaccess, 2).'</td></tr>';
			echo '<tr><td class="altbg1">'.$lang['forums_edit_perm_get_attach'].':</td><td class="altbg2" colspan="5">'.substr($getattachaccess, 2).'</td></tr>';
			echo '<tr><td class="altbg1">'.$lang['forums_edit_perm_post_attach'].':</td><td class="altbg2" colspan="5">'.substr($postattachaccess, 2).'</td></tr>';

			showtype('', 'bottom');

		}

		echo "<br><center><input class=\"button\" type=\"submit\" name=\"detailsubmit\" value=\"$lang[submit]\">";
		if($forum['type'] != 'group') {
			echo "&nbsp;&nbsp;&nbsp;<input class=\"button\" type=\"submit\" name=\"saveconfigsubmit\" value=\"".$lang['saveconf']."\"></form>";
		}

	} else {

		if(strlen($namenew) > 50) {
			cpmsg('forums_name_toolong');
		}

		if($type == 'group') {

			if($namenew) {
				$db->query("UPDATE {$tablepre}forums SET name='$namenew',forumcolumns='".intval($forumcolumnsnew)."' WHERE fid='$fid'");
				updatecache('forums');

				cpmsg('forums_edit_succeed', 'admincp.php?action=forumsedit');
			} else {
				cpmsg('forums_edit_name_invalid');
			}

		} else {

			$extensionarray = array();
			foreach(explode(',', $attachextensionsnew) as $extension) {
				if($extension = trim($extension)) {
					$extensionarray[] = $extension;
				}
			}
			$attachextensionsnew = implode(', ', $extensionarray);

			foreach($perms as $perm) {
				${$perm.'new'} = is_array($$perm) && !empty($$perm) ? "\t".implode("\t", $$perm)."\t" : '';
			}

			$fupadd = '';
			if($fupnew != $forum['fup']) {
				$query = $db->query("SELECT fid FROM {$tablepre}forums WHERE fup='$fid'");
				if($db->num_rows($query)) {
					cpmsg('forums_edit_sub_notnull');
				}

				$query = $db->query("SELECT fid, type, inheritedmod FROM {$tablepre}forums WHERE fid='$fupnew'");
				$fup = $db->fetch_array($query);

				$fupadd = ", type='".($fup['type'] == 'forum' ? 'sub' : 'forum')."', fup='$fup[fid]'";
				$db->query("DELETE FROM {$tablepre}moderators WHERE fid='$fid' AND inherited='1'");
				$query = $db->query("SELECT * FROM {$tablepre}moderators WHERE fid='$fupnew' ".($fup['inheritedmod'] ? '' : "AND inherited='1'"));
				while($mod = $db->fetch_array($query)) {
					$db->query("REPLACE INTO {$tablepre}moderators (uid, fid, displayorder, inherited)
						VALUES ('$mod[uid]', '$fid', '0', '1')");
				}

				$moderators = $tab = '';
				$query = $db->query("SELECT m.username FROM {$tablepre}members m, {$tablepre}moderators mo WHERE mo.fid='$fid' AND mo.inherited='0' AND m.uid=mo.uid ORDER BY mo.displayorder");
				while($mod = $db->fetch_array($query)) {
					$moderators .= $tab.addslashes($mod['username']);
					$tab = "\t";
				}
				$db->query("UPDATE {$tablepre}forumfields SET moderators='$moderators' WHERE fid='$fid'");
			}

                        $statusnew = $supe['status'] && $supe['circlestatus'] && $circlenew ? 2 : $statusnew;
                        $supe_pushsetting[status] = $statusnew == 2 && !$supe_pushsetting[status] ? 1 : $supe_pushsetting[status];
			$supe_pushsettingadd = $supe['status'] ? ", supe_pushsetting='".serialize($supe_pushsetting)."'" : '';

			if(!$allowsharenew && (bool)$allowsharenew != (bool)$forum['allowshare']) {
				$db->query("UPDATE {$tablepre}threads SET blog='0' WHERE fid='$fid'");
			}

			$allowpostspecialnew = bindec(intval($allowpostspecialnew[4]).intval($allowpostspecialnew[3]).intval($allowpostspecialnew[2]).intval($allowpostspecialnew[1]));
			$allowspecialonlynew = $allowpostspecialnew ? $allowspecialonlynew : 0;
			$forumcolumnsnew = intval($forumcolumnsnew);
			$threadcachesnew = max(0, min(100, intval($threadcachesnew)));

			$db->query("UPDATE {$tablepre}forums SET status='$statusnew', name='$namenew', styleid='$styleidnew', allowshare='$allowsharenew', alloweditpost='$alloweditpostnew',
				allowpostspecial='$allowpostspecialnew', allowspecialonly='$allowspecialonlynew', allowpaytoauthor='$allowpaytoauthornew', allowhtml='$allowhtmlnew', allowbbcode='$allowbbcodenew', allowimgcode='$allowimgcodenew',
				allowsmilies='$allowsmiliesnew', alloweditrules='$alloweditrulesnew', modnewposts='$modnewpostsnew',
				recyclebin='$recyclebinnew', jammer='$jammernew', allowanonymous='$allowanonymousnew', forumcolumns='$forumcolumnsnew', threadcaches='$threadcachesnew',
				simple='$simplenew', disablewatermark='$disablewatermarknew', autoclose='".intval($autoclosenew * $autoclosetimenew)."' $fupadd
				WHERE fid='$fid'");

			$query = $db->query("SELECT fid FROM {$tablepre}forumfields WHERE fid='$fid'");
			if(!($db->num_rows($query))) {
				$db->query("INSERT INTO {$tablepre}forumfields (fid)
					VALUES ('$fid')");
			}

			$postcreditsarray = $replycreditsarray = array();

			if($postcreditsstatus) {
				for($i = 1; $i <= 8; $i++) {
					if(isset($extcredits[$i]) && isset($postcreditsnew[$i])) {
						$postcreditsnew[$i]  = $postcreditsnew[$i] < -99 ? -99 : $postcreditsnew[$i];
						$postcreditsnew[$i]  = $postcreditsnew[$i] > 99 ? 99 : $postcreditsnew[$i];
						$postcreditsarray[$i] = intval($postcreditsnew[$i]);
					}
				}
			}
			if($replycreditsstatus) {
				for($i = 1; $i <= 8; $i++) {
					if(isset($extcredits[$i]) && isset($replycreditsnew[$i])) {
						$replycreditsnew[$i]  = $replycreditsnew[$i] < -99 ? -99 : $replycreditsnew[$i];
						$replycreditsnew[$i]  = $replycreditsnew[$i] > 99 ? 99 : $replycreditsnew[$i];
						$replycreditsarray[$i] = intval($replycreditsnew[$i]);
					}
				}
			}
			if($digestcreditsstatus) {
				for($i = 1; $i <= 8; $i++) {
					if(isset($extcredits[$i]) && isset($digestcreditsnew[$i])) {
						$digestcreditsnew[$i]  = $digestcreditsnew[$i] < -99 ? -99 : $digestcreditsnew[$i];
						$digestcreditsnew[$i]  = $digestcreditsnew[$i] > 99 ? 99 : $digestcreditsnew[$i];
						$digestcreditsarray[$i] = intval($digestcreditsnew[$i]);
					}
				}
			}
			if($postattachcreditsstatus) {
				for($i = 1; $i <= 8; $i++) {
					if(isset($extcredits[$i]) && isset($postattachcreditsnew[$i])) {
						$postattachcreditsnew[$i]  = $postattachcreditsnew[$i] < -99 ? -99 : $postattachcreditsnew[$i];
						$postattachcreditsnew[$i]  = $postattachcreditsnew[$i] > 99 ? 99 : $postattachcreditsnew[$i];
						$postattachcreditsarray[$i] = intval($postattachcreditsnew[$i]);
					}
				}
			}
			if($getattachcreditsstatus) {
				for($i = 1; $i <= 8; $i++) {
					if(isset($extcredits[$i]) && isset($getattachcreditsnew[$i])) {
						$getattachcreditsnew[$i]  = $getattachcreditsnew[$i] < -99 ? -99 : $getattachcreditsnew[$i];
						$getattachcreditsnew[$i]  = $getattachcreditsnew[$i] > 99 ? 99 : $getattachcreditsnew[$i];
						$getattachcreditsarray[$i] = intval($getattachcreditsnew[$i]);
					}
				}
			}
			$postcreditsnew = $postcreditsarray ? addslashes(serialize($postcreditsarray)) : '';
			$replycreditsnew = $replycreditsarray ? addslashes(serialize($replycreditsarray)) : '';
			$digestcreditsnew = $digestcreditsarray ? addslashes(serialize($digestcreditsarray)) : '';
			$postattachcreditsnew = $postattachcreditsarray ? addslashes(serialize($postattachcreditsarray)) : '';
			$getattachcreditsnew = $getattachcreditsarray ? addslashes(serialize($getattachcreditsarray)) : '';

			$threadtypesnew['types'] = $threadtypesnew['flat'] = $threadtypes['selectbox'] = array();

			if($allowthreadtypes && $forum['status'] != 2) {
				if(is_array($newname) && $newname) {
					$newname = array_unique($newname);
					if($newname) {
						foreach($newname AS $key => $val) {
							$val = trim($val);
							if($val) {
								$query = $db->query("SELECT typeid FROM {$tablepre}threadtypes WHERE name='$val'");
								$newtypeid = $db->result($query, 0);
								if(!$newtypeid) {
									$db->query("INSERT INTO	{$tablepre}threadtypes (name, description) VALUES
										('$val', '".dhtmlspecialchars(trim($newdescription[$key]))."')");
									$newtypeid = $db->insert_id();
								}
								if($newoptions[$key] == 1) {
									$threadtypesnew['types'][$newtypeid] = $threadtypesnew['flat'][$newtypeid] = $val;
								} elseif($newoptions[$key] == 2) {
									$threadtypesnew['types'][$newtypeid] = $threadtypesnew['selectbox'][$newtypeid] = $val;
								}
							}
						}
					}
					$threadtypesnew['status'] = 1;
				} else {
					$newname = array();
				}
				if($threadtypesnew['status']) {
					if(is_array($threadtypesnew['options']) && $threadtypesnew['options']) {

						$typeids = '0';
						foreach($threadtypesnew['options'] as $key => $val) {
							$typeids .= $val ? ', '.intval($key) : '';
						}

						$query = $db->query("SELECT * FROM {$tablepre}threadtypes WHERE typeid IN ($typeids) ORDER BY displayorder");
						while($type = $db->fetch_array($query)) {
							if($threadtypesnew['options'][$type['typeid']] == 1) {
								$threadtypesnew['types'][$type['typeid']] = $threadtypesnew['flat'][$type['typeid']] = $type['name'];
							} elseif($threadtypesnew['options'][$type['typeid']] == 2) {
								$threadtypesnew['types'][$type['typeid']] = $threadtypesnew['selectbox'][$type['typeid']] = $type['name'];
							}
						}
					}
					$threadtypesnew = $threadtypesnew['types'] ? addslashes(serialize(array
						(
						'required' => (bool)$threadtypesnew['required'],
						'listable' => (bool)$threadtypesnew['listable'],
						'prefix' => (bool)$threadtypesnew['prefix'],
						'types' => $threadtypesnew['types'],
						'selectbox' => $threadtypesnew['selectbox'],
						'flat' => $threadtypesnew['flat'],
						))) : '';
				} else {
					$threadtypesnew = '';
				}
				$threadtypesadd = "threadtypes='$threadtypesnew',";
			} else {
				$threadtypesadd = '';
			}

			$db->query("UPDATE {$tablepre}forumfields SET description='$descriptionnew', icon='$iconnew', password='$passwordnew', redirect='$redirectnew', rules='$rulesnew',
				attachextensions='$attachextensionsnew', $threadtypesadd postcredits='$postcreditsnew', replycredits='$replycreditsnew', digestcredits='$digestcreditsnew',
				postattachcredits='$postattachcreditsnew', getattachcredits='$getattachcreditsnew', viewperm='$viewpermnew', postperm='$postpermnew', replyperm='$replypermnew',
				getattachperm='$getattachpermnew', postattachperm='$postattachpermnew'$supe_pushsettingadd WHERE fid='$fid'");

			if($statusnew == 0) {
				$db->query("UPDATE {$tablepre}forums SET status='$statusnew' WHERE fup='$fid'", 'UNBUFFERED');
			}

			updatecache('forums');

			if(submitcheck('saveconfigsubmit') && $type != 'group') {
				$projectid = intval($projectid);
				dheader("Location: {$boardurl}admincp.php?action=projectadd&id=$fid&type=forum&projectid=$projectid");
			} else {
				cpmsg('forums_edit_succeed', 'admincp.php?action=forumsedit');
			}
		}

	}

} elseif($action == 'forumdelete') {

	if($ajax) {
		ob_end_clean();
		require_once DISCUZ_ROOT.'./include/post.func.php';
		$tids = 0;

		$total = intval($total);
		$pp = intval($pp);
		$currow = intval($currow);

		$query = $db->query("SELECT tid FROM {$tablepre}threads WHERE fid='$fid' LIMIT $pp");
		while($thread = $db->fetch_array($query)) {
			$tids .= ','.$thread['tid'];
		}

		if($tids) {
			$query = $db->query("SELECT attachment, thumb, remote FROM {$tablepre}attachments WHERE tid IN ($tids)");
			while($attach = $db->fetch_array($query)) {
				dunlink($attach['attachment'], $attach['thumb'], $attach['remote']);
			}

			foreach(array('threads', 'threadsmod', 'relatedthreads', 'posts', 'polls', 'polloptions', 'trades', 'activities', 'activityapplies', 'attachments', 'favorites', 'mythreads', 'myposts', 'subscriptions') as $value) {
				$db->query("DELETE FROM {$tablepre}$value WHERE tid IN ($tids)", 'UNBUFFERED');
			}
		}

		if($currow + $pp > $total) {
			$db->query("DELETE FROM {$tablepre}forums WHERE fid='$fid'");
			$db->query("DELETE FROM {$tablepre}forumfields WHERE fid='$fid'");
			$db->query("DELETE FROM {$tablepre}moderators WHERE fid='$fid'");
			$db->query("DELETE FROM {$tablepre}access WHERE fid='$fid'");
			echo 'TRUE';
			exit;
		}

		updatecache('forums');
		echo 'GO';
		exit;

	} else {

		if($finished) {

			cpmsg('forums_delete_succeed', 'admincp.php?action=forumsedit');

		}

		$query = $db->query("SELECT COUNT(*) FROM {$tablepre}forums WHERE fup='$fid'");
		if($db->result($query, 0)) {
			cpmsg('forums_delete_sub_notnull');
		}

		if(!$confirmed) {

			cpmsg('forums_delete_confirm', "admincp.php?action=forumdelete&fid=$fid", 'form');

		} else {

			$query = $db->query("SELECT COUNT(*) FROM {$tablepre}threads WHERE fid='$fid'");
			$threads = $db->result($query, 0);

			echo "
			<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"6\"><tr><td>
			<br><br><br><br><br><br>
			<table width=\"500\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" class=\"tableborder\">
			<tr class=\"header\"><td>".$lang['discuz_message']."</td></tr><tr><td class=\"altbg2\"><div align=\"center\">
			<form method=\"post\" action=\"admincp.php?action=forumdelete&fid=2\"><input type=\"hidden\" name=\"formhash\" value=\"6a47c68f\">
			<br><br><br>".$lang['forums_delete_alarm']."<br><br>
			<div class=\"process\" >
				<div id=\"percent\" class=\"processbg\">0%</div>
			</div>
			<br><br><br><br></div><br><br>
			</td></tr></table>
			<br><br><br>
			</td></tr></table>
			";

			echo "<div id=\"statusid\" style=\"display:none\"></div>";
			echo "<script src=\"include/javascript/ajax.js\"></script>";
			echo "

			<script>
				var xml_http_building_link = '".$lang['xml_http_building_link']."';
				var xml_http_sending = '".$lang['xml_http_sending']."';
				var xml_http_loading = '".$lang['xml_http_loading']."';
				var xml_http_load_failed = '".$lang['xml_http_load_failed']."';
				var xml_http_data_in_processed = '".$lang['xml_http_data_in_processed']."';
				function forumsdelete(url, total, pp, currow) {

					var x = new Ajax('HTML', 'statusid');
					x.get(url+'&ajax=1&pp='+pp+'&total='+total+'&currow='+currow, function(s) {
						if(s != 'GO') {
							location.href = 'admincp.php?action=forumdelete&finished=1';
						}

						currow += pp;
						var percent = ((currow / total) * 100).toFixed(0);
						percent = percent > 100 ? 100 : percent;
						document.getElementById('percent').innerHTML = percent+'%';
						document.getElementById('percent').style.backgroundPosition = '-'+percent+'%';

						if(currow < total) {
							forumsdelete(url, total, pp, currow);
						}
					});
				}
				forumsdelete('admincp.php?action=forumdelete&fid=$fid&confirmed=1', $threads, 2000, 0);
			</script>
			";
		}
	}

} elseif($action == 'threadtypes') {

	if(!submitcheck('typesubmit')) {

		$forumsarray = $fidsarray = array();
		$query = $db->query("SELECT f.fid, f.name, ff.threadtypes FROM {$tablepre}forums f , {$tablepre}forumfields ff WHERE ff.threadtypes<>'' AND f.fid=ff.fid");
		while($forum = $db->fetch_array($query)) {
			$forum['threadtypes'] = unserialize($forum['threadtypes']);
			if(is_array($forum['threadtypes']['types'])) {
				foreach($forum['threadtypes']['types'] as $typeid => $name) {
					$forumsarray[$typeid][] = '<a href="forumdisplay.php?fid='.$forum['fid'].'" target="_blank">'.$forum['name'].'</a> [<a href="admincp.php?action=forumdetail&fid='.$forum['fid'].'">'.$lang['edit'].'</a>]';
					$fidsarray[$typeid][] = $forum['fid'];
				}
			}
		}

		$threadtypes = '';
		$query = $db->query("SELECT * FROM {$tablepre}threadtypes ORDER BY displayorder");
		while($type = $db->fetch_array($query)) {
			$threadtypes .= "<tr align=\"center\"><td class=\"altbg1\"><input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$type[typeid]\"></td>\n".
				"<td class=\"altbg2\"><input type=\"text\" size=\"15\" name=\"namenew[$type[typeid]]\" value=\"".dhtmlspecialchars($type['name'])."\"></td>\n".
				"<td class=\"altbg1\"><input type=\"text\" size=\"2\" name=\"displayordernew[$type[typeid]]\" value=\"$type[displayorder]\"></td>\n".
				"<td class=\"altbg2\"><input type=\"text\" size=\"30\" name=\"descriptionnew[$type[typeid]]\" value=\"$type[description]\"></td>\n".
				"<td class=\"altbg1\">".(is_array($forumsarray[$type['typeid']]) ? implode(', ', $forumsarray[$type['typeid']])."<input type=\"hidden\" name=\"fids[$type[typeid]]\" value=\"".implode(', ', $fidsarray[$type['typeid']])."\">" : '')."</td></tr>\n";
		}
		shownav('menu_forums_threadtypes');
		showtips('forums_threadtypes_tips');

?>
<form method="post" action="admincp.php?action=threadtypes">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="5"><?=$lang['forums_threadtypes']?></td></tr>
<tr align="center" class="category"><td><input class="checkbox" type="checkbox" name="chkall" class="category" onclick="checkall(this.form)"><?=$lang['del']?></td><td><?=$lang['forums_threadtypes']?></td><td><?=$lang['display_order']?></td><td><?=$lang['description']?></td><td><?=$lang['forums_threadtypes_forums']?></td></tr>
<?=$threadtypes?>
<tr align="center" class="altbg1"><td><?=$lang['add_new']?></td><td><input type='text' name="newname" size="15"></td><td><input type="text" name="newdisplayorder" size="2" value="0"></td><td><input type="text" name="newdescription" size="30" value=""></td><td>&nbsp;</td></tr>
</table><br>
<center><input class="button" type="submit" name="typesubmit" value="<?=$lang['submit']?>"></center>
</form>
<?

	} else {

		$modifiedtypes = array();

		if(is_array($delete)) {
			$modifiedtypes = $delete;
			$deleteids = '\''.implode('\',\'', $delete).'\'';
			$db->query("DELETE FROM {$tablepre}threadtypes WHERE typeid IN ($deleteids)");
			if($db->affected_rows()) {
				$db->query("UPDATE {$tablepre}threads SET typeid='0' WHERE typeid IN ($deleteids)");
				foreach($delete AS $id) {
					if(!empty($fids[$id])) {
						$query = $db->query("SELECT fid, threadtypes FROM {$tablepre}forumfields WHERE fid IN ($fids[$id])");
						while($forum = $db->fetch_array($query)) {
							$forum['threadtypes'] = unserialize($forum['threadtypes']);
							unset($forum['threadtypes']['types'][$id], $forum['threadtypes']['flat'][$id], $forum['threadtypes']['selectbox'][$id]);
							$threadtypesnew = addslashes(serialize(array
								(
								'required' => (bool)$forum['threadtypes']['required'],
								'listable' => (bool)$forum['threadtypes']['listable'],
								'prefix' => (bool)$forum['threadtypes']['prefix'],
								'types' => $forum['threadtypes']['types'],
								'selectbox' => $forum['threadtypes']['selectbox'],
								'flat' => $forum['threadtypes']['flat'],
								)));
							$db->query("UPDATE {$tablepre}forumfields SET threadtypes='$threadtypesnew' WHERE fid='$forum[fid]'");
						}
					}
				}
			}
		}

		if(is_array($namenew)) {
			foreach($namenew as $typeid => $val) {
				$db->query("UPDATE {$tablepre}threadtypes SET name='".trim($namenew[$typeid])."', description='".dhtmlspecialchars(trim($descriptionnew[$typeid]))."', displayorder='$displayordernew[$typeid]' WHERE typeid='$typeid'");
				if($db->affected_rows()) {
					$modifiedtypes[] = $typeid;
				}
			}

			if($modifiedtypes = array_unique($modifiedtypes)) {
				$query = $db->query("SELECT f.fid, ff.threadtypes FROM {$tablepre}forums f, {$tablepre}forumfields ff WHERE ff.threadtypes<>'' AND f.fid=ff.fid");
				while($forum = $db->fetch_array($query)) {
					$forum['threadtypes'] = unserialize($forum['threadtypes']);
					foreach($modifiedtypes as $typeid) {
						if(isset($forum['threadtypes']['types'][$typeid])) {
							$db->query("SELECT * FROM {$tablepre}threadtypes WHERE typeid IN (".implode(',', array_keys($forum['threadtypes']['types'])).") ORDER BY displayorder");
							$forum['threadtypes']['types'] = array();
							while($type = $db->fetch_array($query)) {
								$forum['threadtypes']['types'][$type['typeid']] = $type['name'];
							}
							$db->query("UPDATE {$tablepre}forumfields SET threadtypes='".addslashes(serialize($forum['threadtypes']))."' WHERE fid='$fid'");
							break;
						}
					}
				}
			}
		}

		if($newname != '') {
			$newname = trim($newname);
			$query = $db->query("SELECT typeid FROM {$tablepre}threadtypes WHERE name='$newname'");
			if($db->num_rows($query)) {
				cpmsg('forums_threadtypes_duplicate');
			}
			$db->query("INSERT INTO	{$tablepre}threadtypes (name, description, displayorder) VALUES
					('$newname', '".dhtmlspecialchars(trim($newdescription))."', '$newdisplayorder')");
		}

		cpmsg('forums_threadtypes_succeed', 'admincp.php?action=threadtypes');

	}

} elseif($action == 'forumrules') {

	if(empty($fid)) {

		$forums = '';

		if($adminid == 2) {
			$query = $db->query("SELECT fid, name FROM {$tablepre}forums
				WHERE alloweditrules>'0' AND type IN ('forum', 'sub')");
		} else {
			$query = $db->query("SELECT f.fid, f.name, m.uid FROM {$tablepre}forums f
				LEFT JOIN {$tablepre}moderators m ON m.uid='$discuz_uid' AND m.fid=f.fid
				WHERE alloweditrules>'0' AND f.type IN ('forum', 'sub')");
		}

		while($forum = $db->fetch_array($query)) {
			if($forum['uid'] || $adminid == 2) {
				$forums .= "<option value=\"$forum[fid]\">".strip_tags($forum['name'])."</option>";
			}
		}

		if($forums) {
			$forums = '<select name="fid">'.$forums.'</select>';
		} else {
			cpmsg('forums_rules_nopermission');
		}
		shownav('menu_forums_rules');

?>
<form method="post" action="admincp.php?action=forumrules">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="2"><?=$lang['forums_edit']?></td></tr>
<tr class="altbg2">
<td><?=$lang['forum']?>:</td><td><?=$forums?></td></tr>
</table><br><center>
<input class="button" type="submit" value="<?=$lang['submit']?>">
</center></form><br>
<?

	} else {

		$access = 0;
		if($adminid == 2) {
			$access = 1;
		} elseif($adminid == 3) {
			$query = $db->query("SELECT uid FROM {$tablepre}moderators WHERE uid='$discuz_uid' AND fid='$fid'");
			$access = $db->num_rows($query) ? 1 : 0;
		}

		$query = $db->query("SELECT f.fid, f.name, f.alloweditrules, ff.rules FROM {$tablepre}forums f
			LEFT JOIN {$tablepre}forumfields ff USING (fid)
			WHERE f.fid='$fid' AND alloweditrules>'0' AND type IN ('forum', 'sub')");

		if(!$access || !($forum = $db->fetch_array($query))) {
			cpmsg('forums_rules_nopermission');
		}

		if(!submitcheck('rulessubmit')) {

			$comment = $lang[($forum['alloweditrules'] == 1 ? 'forums_edit_edit_rules_html_no' : 'forums_edit_edit_rules_html_yes')];

?>
<br><br><form method="post" action="admincp.php?action=forumrules&fid=<?=$fid?>">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="2"><?=$lang['forums_edit']?> - <?=$forum['name']?></td></tr>
<tr class="altbg2"><td valign="top"><span class="bold"><?=$lang['forums_edit_rules']?></span><br><?=$comment?></td>
<td><textarea name="rulesnew" rows="5" cols="60"><?=dhtmlspecialchars($forum['rules'])?></textarea></td></tr>
</table><br><center>
<input class="button" type="submit" name="rulessubmit" value="<?=$lang['submit']?>">
</center></form><br>
<?

		} else {

			if($forum['alloweditrules'] != 2) {
				$rulesnew = dhtmlspecialchars($rulesnew);
			}

			$db->query("UPDATE {$tablepre}forumfields SET rules='$rulesnew' WHERE fid='$fid'");

			cpmsg('forums_rules_succeed');

		}

	}

} elseif($action == 'forumcopy') {

	require_once DISCUZ_ROOT.'./forumdata/cache/cache_forums.php';

	$source = intval($source);
	$sourceforum = $_DCACHE['forums'][$source];

	if(empty($sourceforum) || $sourceforum['type'] == 'group') {
		cpmsg('forums_copy_source_invalid');
	}

	$optgroups = array
		(
		'normal'	=> array('modnewposts', 'recyclebin', 'allowshare', 'allowhtml', 'allowbbcode', 'allowimgcode', 'allowsmilies', 'jammer', 'allowanonymous' ,'disablewatermark' ,'allowpostspecial'),
		'credits'	=> array('postcredits', 'replycredits'),
		'access'	=> array('password', 'viewperm', 'postperm', 'replyperm', 'getattachperm' ,'postattachperm'),
		'misc'		=> array('threadtypes', 'attachextensions')
		);

	if(!submitcheck('copysubmit')) {

		require_once DISCUZ_ROOT.'./include/forum.func.php';

		$forumselect = '<select name="target[]" size="10" multiple="multiple" style="width: 80%">'.forumselect().'</select>';
		$optselect = '<select name="options[]" size="10" multiple="multiple" style="width: 80%">';

		foreach($optgroups as $optgroup => $options) {
			$optselect .= '<optgroup label="'.$lang['forums_copy_optgroups_'.$optgroup]."\">\n";
			foreach($options as $option) {
				$optselect .= "<option value=\"$option\">".$lang['forums_copy_options_'.$option]."</option>\n";
			}
		}
		$optselect .= '</select>';
		showtips('forums_copy_tips');

?>
<form method="post" action="admincp.php?action=forumcopy">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<input type="hidden" name="source" value="<?=$source?>">
<?

		showtype($lang['forums_copy'].' - '.$lang['forums_copy_source'].' - '.$sourceforum['name'], 'top');
		showsetting('forums_copy_target', '', '', $forumselect);
		showsetting('forums_copy_options', '', '', $optselect);
		showtype('', 'bottom');
		echo "<br><br><center><input class=\"button\" type=\"submit\" name=\"copysubmit\" value=\"$lang[submit]\"></form>";

	} else {

		$fids = $comma = '';
		if(is_array($target) && count($target)) {
			foreach($target as $fid) {
				if(($fid = intval($fid)) && $fid != $source ) {
					$fids .= $comma.$fid;
					$comma = ',';
				}
			}
		}
		if(empty($fids)) {
			cpmsg('forums_copy_target_invalid');
		}

		$forumoptions = array();
		if(is_array($options) && !empty($options)) {
			foreach($options as $option) {
				if($option = trim($option)) {
					if(in_array($option, $optgroups['normal'])) {
						$forumoptions['forums'][] = $option;
					} elseif(in_array($option, $optgroups['misc']) || in_array($option, $optgroups['credits']) || in_array($option, $optgroups['access'])) {
						$forumoptions['forumfields'][] = $option;
					}
				}
			}
		}

		if(empty($forumoptions)) {
			cpmsg('forums_copy_options_invalid');
		}

		foreach(array('forums', 'forumfields') as $table) {
			if(is_array($forumoptions[$table]) && !empty($forumoptions[$table])) {
				$query = $db->query("SELECT ".implode($forumoptions[$table],',')." FROM {$tablepre}$table WHERE fid='$source'");
				if(!$sourceforum = $db->fetch_array($query)) {
					cpmsg('forums_copy_source_invalid');
				}

				$updatequery = 'fid=fid';
				foreach($sourceforum as $key => $val) {
					$updatequery .= ", $key='".addslashes($val)."'";
				}
				$db->query("UPDATE {$tablepre}$table SET $updatequery WHERE fid IN ($fids)");
			}
		}

		updatecache('forums');
		cpmsg('forums_copy_succeed', 'admincp.php?action=forumsedit');

	}
}

?>