<?php
!function_exists('adminmsg') && exit('Forbidden');
$basename = "$admin_file?adminjob=setforum";

include_once(D_P.'data/bbscache/forumcache.php');
require_once(R_P.'require/updateforum.php');

list($hidefid,$hideforum) = GetHiddenForum();
//if (If_manager) {
	$forumcache .= $hideforum;
//}
if (empty($action)) {

	$catedb = $forumdb = $subdb1 = $subdb2 = array();
	$space  = '<span class="fourm-two"></span>';

	$query = $db->query("SELECT fid,fup,type,name,vieworder,forumadmin,f_type,cms FROM pw_forums WHERE cms!='1' ORDER BY vieworder");
	while ($forums = $db->fetch_array($query)) {
		$forums['name'] = Quot_cv($forums['name']);
		$forums['forumadmin'] = trim($forums['forumadmin'],',');
		if ($forums['type'] == 'category') {
			$catedb[] = $forums;
		} elseif ($forums['type'] == 'forum') {
			$forumdb[] = $forums;
		} elseif ($forums['type'] == 'sub') {
			$subdb1[] = $forums;
		} else {
			$subdb2[] = $forums;
		}
	}
	$threaddb = array();
	foreach ($catedb as $cate) {
		$threaddb[$cate['fid']] = array();
		foreach ($forumdb as $key2 => $forumss) {
			if ($forumss['fup'] == $cate['fid']) {
				$threaddb[$cate['fid']][] = $forumss;
				unset($forumdb[$key2]);
				foreach ($subdb1 as $key3 => $sub1) {
					if ($sub1['fup'] == $forumss['fid']) {
						$threaddb[$cate['fid']][] = $sub1;
						unset($subdb1[$key3]);
						foreach ($subdb2 as $key4 => $sub2) {
							if ($sub2['fup'] == $sub1['fid']) {
								$threaddb[$cate['fid']][] = $sub2;
								unset($subdb2[$key4]);
							}
						}
					}
				}
			}
		}
	}
	$forum_L = array();
	if ($forumdb) {
		foreach ($forumdb as $value) {
			$forum_L[] = $value;
		}
	}
	if ($subdb1) {
		foreach ($subdb1 as $value) {
			$forum_L[] = $value;
		}
	}
	if ($subdb2) {
		foreach ($subdb2 as $value) {
			$forum_L[] = $value;
		}
	}
	$ajaxurl = EncodeUrl($basename);
	include PrintEot('setforum');exit;

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

	InitGP(array('fup','forumnum','ifsave'));
	InitGP(array('name'),'P',0);
	if (empty($_POST['step'])) {
		if (!empty($name)) {
			$db->update("INSERT INTO pw_forums SET " . pwSqlSingle(array(
				'fup'	=> 0,
				'type'	=> 'category',
				'name'	=> $name,
				'f_type'=> 'forum',
				'cms'	=> 0,
				'ifhide'=> 1,
				'allowtype'=> 3
			)));
			$fid = $db->insert_id();
			$db->update("INSERT INTO pw_forumdata SET fid=".pwEscape($fid));
			P_unlink(D_P.'data/bbscache/c_cache.php');
			updatecache_f();
			ObHeader("$basename&action=addforum&fup=$fid");
		} elseif (!empty($fup)) {
			if(empty($forum[$fup]) || $forum[$fup]['type'] == 'sub2' ) {
				adminmsg('fup_empty');
			}
			$checked = $ifsave == 1 ? 'checked' : '';
			empty($forumnum) && $forumnum = 5;
			require_once(R_P."require/forum.php");
			$setfid_style = getstyles($style);
		}
		include PrintEot('setforum');exit;
	} else {
		InitGP(array('vieworder','forumadmin','style','keywords','descrip','logo','ifsave','addtype'));
		InitGP(array('descrip'),'P',0);
		$forumtype = $forum[$fup]['type'] == 'category' ? 'forum' : ($forum[$fup]['type'] == 'forum' ? 'sub' : 'sub2');
		if ($forum[$fup]['type'] != 'category') {
			$fupset = $db->get_one("SELECT f.allowhide,f.allowsell,f.allowtype,f.copyctrl,f.viewsub,f.allowvisit,f.allowpost,f.allowrp,f.allowdownload,f.allowupload,f.f_type,f.f_check,f.cms,f.ifhide,fe.creditset,fe.forumset FROM pw_forums f LEFT JOIN pw_forumsextra fe USING(fid) WHERE f.fid=".pwEscape($fup));
			Add_S($fupset);
			@extract($fupset,EXTR_OVERWRITE);
		}
		foreach($name as $key => $value) {
			if(empty($value)) continue;
			$value     = str_replace('<iframe','&lt;iframe',$value);
			$descrip[$key]  = str_replace('<iframe','&lt;iframe',$descrip[$key]);
			$keywords[$key] = Char_cv($keywords[$key]);
			strlen($descrip[$key])>250 && adminmsg('descrip_long');
			$newadmin= array();
			$str_admin = '';
			$admin_a  = explode(",",$forumadmin[$key]);
			foreach ($admin_a as $aid=>$avalue) {
				$avalue = trim($avalue);
				if ($avalue && !in_array($avalue,$newadmin)) {
					$mb = $db->get_one("SELECT uid FROM pw_members WHERE username='$avalue'");
					if ($mb) {
						$newadmin[] = $avalue;
					} else {
						$errorname .= $avalue.',';
					}
				}
			}
			if ($newadmin) {
				$newadmin = implode(',',$newadmin);
				$str_admin = ','.$newadmin.',';
			} else {
				$str_admin = '';
			}
			if($forum[$fup]['type'] != 'category' && $ifsave[$key] == 1) {
				$db->update("INSERT INTO pw_forums SET " . pwSqlSingle(array(
					'fup'			=> $fup,
					'type'			=> $forumtype,
					'logo'			=> $logo[$key],
					'name'			=> $value,
					'descrip'		=> $descrip[$key],
					'keywords'		=> $keywords[$key],
					'vieworder'		=> $vieworder[$key],
					'forumadmin'    => $str_admin,
					'style'			=> $style[$key],
					'allowhide'		=> $allowhide,
					'allowsell'		=> $allowsell,
					'allowtype'		=> $allowtype,
					'copyctrl'		=> $copyctrl,
					'viewsub'		=> $viewsub,
					'allowvisit'	=> $allowvisit,
					'allowpost'		=> $allowpost,
					'allowrp'		=> $allowrp,
					'allowdownload'	=> $allowdownload,
					'allowupload'	=> $allowupload,
					'f_type'		=> $f_type,
					'f_check'		=> $f_check,
					'cms'			=> $cms,
					'ifhide'		=> $ifhide
				)));
				$fid = $db->insert_id();
				if ($creditset || $forumset) {
					$db->update("INSERT INTO pw_forumsextra SET " . pwSqlSingle(array(
						'fid'		=> $fid,
						'creditset'	=> $creditset,
						'forumset'	=> $forumset
					)));
				}
			} else {
				$f_type = $forum[$fup]['f_type'] == 'hidden' ? 'hidden' : 'forum';
				$db->update("INSERT INTO pw_forums SET " . pwSqlSingle(array(
					'fup'			=> $fup,
					'type'			=> $forumtype,
					'logo'			=> $logo[$key],
					'name'			=> $value,
					'descrip'		=> $descrip[$key],
					'keywords'		=> $keywords[$key],
					'vieworder'		=> $vieworder[$key],
					'forumadmin'    => $str_admin,
					'style'			=> $style[$key],
					'f_type'		=> $f_type,
					'cms'			=> 0,
					'ifhide'		=> 1,
					'allowtype'		=> 3
				)));
				$fid = $db->insert_id();
			}
			$db->update("INSERT INTO pw_forumdata SET fid=".pwEscape($fid));
		}
		P_unlink(D_P.'data/bbscache/c_cache.php');
		updatecache_f();
		$forumtype != 'category' && updatetop();
		if($addtype == 1){
			ObHeader("$basename&action=edit&fid=$fid");
		}else{
			adminmsg('operate_success');
		}
	}
} elseif ($_POST['action'] == 'editforum') {

	InitGP(array('forumadmin'), 'P', 0);
	InitGP(array('order'), 'P', 2);
	$errorname = array();
	$forumdb = $db->query("SELECT fid,forumadmin,vieworder FROM pw_forums WHERE cms!='1'");
	while ($foruminfo = $db->fetch_array($forumdb)) {
		$pwSQL = $admin_a = $admin_n = $admin_d = array();
		if ($foruminfo['forumadmin'] != $forumadmin[$foruminfo['fid']] && $foruminfo['forumadmin'] != ','.$forumadmin[$foruminfo['fid']].',') {
			$admin_a = explode(',',$forumadmin[$foruminfo['fid']]);
			if ($admin_a) {
				$query = $db->query("SELECT username FROM pw_members WHERE username IN (".pwImplode($admin_a).")");
				while ($member = $db->fetch_array($query)) {
					$admin_d[] = $member['username'];
				}
				foreach ($admin_a as $value) {
					if (CkInArray($value,$admin_d)) {
						$admin_n[] = $value;
					}
				}
			}
			if ($admin_n) {
				$pwSQL['forumadmin'] = ','.implode(',',$admin_n).',';
			} else {
				$pwSQL['forumadmin'] = '';
			}
			$errorname = array_merge($errorname,array_diff($admin_a,$admin_n));
		}

		if ($order[$foruminfo['fid']] != $foruminfo['vieworder']) {
			$pwSQL['vieworder'] = $order[$foruminfo['fid']];
		}
		if ($pwSQL) {
			$db->update("UPDATE pw_forums SET".pwSqlSingle($pwSQL)."WHERE fid=".pwEscape($foruminfo['fid'],false));
		}
	}
	updatecache_f();
	updateadmin();
	$errorname && $errorname = implode(',',$errorname);
	adminmsg($errorname ? 'user_not_exists' : 'operate_success');

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

	InitGP(array('fid'));

	if (empty($_POST['step'])) {

		include PrintEot('setforum');exit;

	} else {

		$count = $db->get_value("SELECT COUNT(*) AS count FROM pw_forums WHERE fup=".pwEscape($fid)." AND type<>'category'");
		if ($count) {
			adminmsg('forum_havesub');
		}
		delforum($fid);
		P_unlink(D_P.'data/bbscache/c_cache.php');
		updatecache_f();
		adminmsg('operate_success');
	}
} elseif ($action == 'credit') {

	InitGP(array('fid'));
	$basename = "$admin_file?adminjob=setforum&action=credit&fid=$fid";
	$rt = $db->get_one("SELECT fid FROM pw_forums WHERE fid=".pwEscape($fid)." AND type!='category'");
	if (!$rt) {
		adminmsg('forum_not_exists');
	}
	if (empty($_POST['step'])) {

		require_once(R_P.'require/credit.php');
		$creditset = $db->get_value("SELECT creditset FROM pw_forumsextra WHERE fid=".pwEscape($fid));
		$creditset = $creditset ? unserialize($creditset) : array();
		include PrintEot('setforum');exit;

	} else {

		InitGP(array('creditdb','otherfid','otherforum'),'P');

		foreach ($creditdb as $key => $value) {
			foreach ($value as $k => $v) {
				if (is_numeric($v)) {
					$creditdb[$key][$k] = round($v,$k == 'rvrc' ? 1 : 0);
				} else {
					$creditdb[$key][$k] = '';
				}
			}
		}
		$creditset = $creditdb ? serialize($creditdb) : '';

		$db->pw_update(
			"SELECT fid FROM pw_forumsextra WHERE fid=".pwEscape($fid),
			"UPDATE pw_forumsextra SET creditset=".pwEscape($creditset,false).'WHERE fid='.pwEscape($fid),
			"INSERT INTO pw_forumsextra SET creditset=".pwEscape($creditset,false).',fid='.pwEscape($fid)
		);
		if (is_array($otherfid) && is_array($otherforum)) {
			foreach ($otherfid as $key => $selfid) {
				if (!$selfid || !is_numeric($selfid) || $selfid == $fid || $forum[$selfid]['type'] == 'category') {
					continue;
				}
				$rt = $db->get_one("SELECT fid,creditset FROM pw_forumsextra WHERE fid=".pwEscape($selfid));
				if ($rt['fid']) {
					if ($rt['creditset']) {
						$newcreditset = (array)unserialize($rt['creditset']);
						$newcreditset = unserialize($rt['creditset']);
						foreach ($newcreditset as $key => $value) {
							foreach ($value as $k => $val) {
								if ($otherforum[$key][$k]) {
									$newcreditset[$key][$k] = $creditdb[$key][$k];
								}
							}
						}
						$newcreditset = serialize($newcreditset);
						$db->update("UPDATE pw_forumsextra SET creditset=".pwEscape($newcreditset,false).'WHERE fid='.pwEscape($selfid));
					} else {
						$newcreditset = array();
						foreach ($creditdb as $key => $value) {
							foreach ($value as $k => $val) {
								if ($otherforum[$key][$k]) {
									$newcreditset[$key][$k] = $creditdb[$key][$k];
								} else {
									$newcreditset[$key][$k] = '';
								}
							}
						}
						$newcreditset = serialize($newcreditset);
						$db->update("UPDATE pw_forumsextra SET creditset=".pwEscape($newcreditset,false).'WHERE fid='.pwEscape($selfid));
					}
				} else {
					$newcreditset = array();
					foreach ($creditdb as $key => $value) {
						foreach ($value as $k => $val) {
							if ($otherforum[$key][$k]) {
								$newcreditset[$key][$k] = $creditdb[$key][$k];
							} else {
								$newcreditset[$key][$k] = '';
							}
						}
					}
					$newcreditset = serialize($newcreditset);
					$db->update("INSERT INTO pw_forumsextra SET creditset=".pwEscape($newcreditset,false).',fid='.pwEscape($selfid));
				}
			}
		}
		adminmsg('operate_success');
	}
} elseif ($action == 'edit') {

	InitGP(array('fid'),'GP',2);

	if (empty($_POST['step'])) {

		@extract($db->get_one("SELECT f.*,fe.forumset FROM pw_forums f LEFT JOIN pw_forumsextra fe USING(fid) WHERE f.fid=" . pwEscape($fid)));
		$forumset = unserialize($forumset);
		$forumset['newtime']  /= 60;
		$forumset['rvrcneed'] /= 10;
		$forumset['addtpctype'] ? $addtpctype_Y = 'checked' : $addtpctype_N = 'checked';
		
		$name = str_replace("<","&lt;",$name);
		$name = str_replace(">","&gt;",$name);

		require_once(R_P."require/forum.php");
		$setfid_style = getstyles($style);

		if ($type <> 'category') {
			require_once(R_P.'require/credit.php');
			list($uploadcredit,$uplodmoney,$downloadmoney,$downloadimg) = explode("\t",$forumset['uploadset']);
			ifcheck($downloadimg,'img');
			ifcheck($forumset['dig'],'dig');
			ifcheck($forumset['inspect'],'inspect');
			ifcheck($forumset['watermark'],'watermark');
			ifcheck($forumset['allowencode'],'allowencode');
			ifcheck($forumset['anonymous'],'anonymous');
			ifcheck($forumset['commend'],'commend');
			list($rw_time,$rw_b_val,$rw_a_val,$rw_credit) = explode("\t",$forumset['rewarddb']);
			$rw_credit = explode(',',$rw_credit);

			for ($i = 0;$i < 6;$i++) {
				${'allowtype_'.pow(2,$i)} = $allowtype & pow(2,$i) ? 'checked' : '';
			}
			for ($i = 1;$i < 4;$i++) {
				${'recycle_'.pow(2,$i)} = $forumset['recycle'] & pow(2,$i) ? 'checked' : '';
			}
			${'autocommend_'.$forumset['autocommend']}='selected';
			${'sel_'.$forumset['orderway']}='selected';
			${'sel_'.$forumset['asc']}='selected';
			$name	 = str_replace(array('<','>','"',"'"),array("&lt;","&gt;","&quot;","&#39;"),$name);
			$descrip = str_replace(array('<','>'),array("&lt;","&gt;"),$descrip);

			ifcheck($viewsub,'viewsub');
			ifcheck($allowhide,'allowhide');
			ifcheck($allowsell,'allowsell');
			ifcheck($copyctrl,'copyctrl');
			ifcheck($showsub,'showsub');
			ifcheck($ifhide,'ifhide');
			
			$logotype = !empty($logo) && strpos($logo,'http://') === false && file_exists($attachdir.'/'.$logo) ? 'upload' : 'url';
			$logotype == 'upload' ? $logotype_upload = 'checked' : $logotype_url = 'checked';
			$logotype == 'upload' && $logo = '';
			${'check_'.$f_check} = 'checked';
			$ftype[$f_type] = 'selected';
			$forumcache = str_replace("<option value=\"$fup\">","<option value=\"$fup\" selected>",$forumcache);
			$usergroup  = "<table cellspacing='0' cellpadding='0' border='0' width='100%' align='center'><tr>";
			foreach ($ltitle as $key => $value) {
				if ($key == 1 || $key == 2) continue;
				$htm_tr='';$num++;$num%5==0?$htm_tr='</tr><tr>':'';
				$usergroup.="<td><input type='checkbox' name='permit[]' value='$key' _{$key}_>$value</td>$htm_tr";
			}
			$usergroup  .= "</tr></table>";
			$viewvisit	 = str_replace('permit','allowvisit',$usergroup);
			$viewread	 = str_replace('permit','allowread',$usergroup);
			$viewpost    = str_replace('permit','allowpost',$usergroup);
			$viewrp      = str_replace('permit','allowrp',$usergroup);
			$viewupload  = str_replace('permit','allowupload',$usergroup);
			$viewdownload= str_replace('permit','allowdownload',$usergroup);
			$visitper = explode(",",$allowvisit);
			$readper  = explode(",",$allowread);
			$postper  = explode(",",$allowpost);
			$rpper	  = explode(",",$allowrp);
			$uploadper= explode(",",$allowupload);
			$downper  = explode(",",$allowdownload);
			$t_typedb = explode("\t",$t_type);/*主題分類*/
			$t_typedbnum = count($t_typedb);
			$t_typedb[0] = (int)$t_typedb[0];
			${'t_type_'.$t_typedb[0]}='checked';
			foreach ($visitper as $value)
				$viewvisit = str_replace("_{$value}_",'checked',$viewvisit);
			foreach ($readper as $value)
				$viewread  = str_replace("_{$value}_",'checked',$viewread);
			foreach ($postper as $value)
				$viewpost  = str_replace("_{$value}_",'checked',$viewpost);
			foreach ($rpper as $value)
				$viewrp = str_replace("_{$value}_",'checked',$viewrp);
			foreach ($uploadper as $value)
				$viewupload = str_replace("_{$value}_",'checked',$viewupload);
			foreach ($downper as $value)
				$viewdownload = str_replace("_{$value}_",'checked',$viewdownload);
		}
		include PrintEot('setforum');exit;

	} elseif ($_POST['step'] == 2) {

		$forum = $db->get_one("SELECT type,fup,forumadmin,logo FROM pw_forums WHERE fid=".pwEscape($fid));
		InitGP(array('name','descrip'),'P',0);
		InitGP(array('vieworder','dirname','style','across','keywords'),'P');
		$name     = str_replace('<iframe','&lt;iframe',$name);
		$descrip  = str_replace('<iframe','&lt;iframe',$descrip);
		$keywords = Char_cv($keywords);
		strlen($descrip)>250 && adminmsg('descrip_long');

		if ($forum['type'] == 'category') {
			$db->update("UPDATE pw_forums SET " . pwSqlSingle(array(
				'name'		=> $name,
				'vieworder'	=> $vieworder,
				'dirname'	=> $dirname,
				'style'		=> $style,
				'across'	=> $across,
				'cms'		=> $cms
			)) . " WHERE fid=".pwEscape($fid));
		} else {

			InitGP(array('forumsetdb','uploadset','rewarddb','cfup','ffup','showsub','ifhide', 'viewsub','allowhide','allowsell','copyctrl','f_check','password','allowvisit','allowread', 'allowpost','allowrp','allowupload','allowdownload','otherfid','otherforum','allowtime','allowtype', 'recycle','forumsell','sdate','cprice','rprice','logotype','logo_upload','logo_url','ifdellogo'),'P');
			InitGP(array('t_db','new_t_db','f_type'),'P',0);
			$forumsetdb['newtime'] *= 60;
			foreach ($forumsetdb as $key => $value) {
				if ($key == 'link') {
					$forumsetdb['link'] = str_replace(array('"',"'",'\\'),array('','',''),$value);
				} elseif ($key == 'recycle') {
					$forumsetdb['recycle'] = array_sum($value);
				} elseif (!in_array($key,array('orderway','asc','commendlist','chat'))) {
					$forumsetdb[$key] = (int)$value;
				}
			}
			$sellprice = array();
			foreach ($sdate as $key => $value) {
				if ($value && ($cprice[$key] || $rprice[$key])) {
					$sellprice[$value] = array('cprice' => $cprice[$key], 'rprice' => $rprice[$key]);
				}
			}
			ksort($sellprice);
			$forumsetdb['sellprice'] = $sellprice;
			$forumsetdb['uploadset'] = implode("\t",$uploadset);
			$forumsetdb['rvrcneed'] *= 10;
			$rewarddb[3] = implode(',',$rewarddb[3]);
			$forumsetdb['rewarddb']  = implode("\t",$rewarddb);
			$forumsetdb['allowtime'] = $allowtime ? ",".implode(",",$allowtime)."," : '';
			$forumsextradb = serialize($forumsetdb);
			$db->pw_update(
				"SELECT fid FROM pw_forumsextra WHERE fid=".pwEscape($fid),
				"UPDATE pw_forumsextra SET forumset=".pwEscape($forumsextradb,false)."WHERE fid=".pwEscape($fid),
				"INSERT INTO pw_forumsextra SET forumset=".pwEscape($forumsextradb,false).',fid='.pwEscape($fid)
			);

			$fup = $cms == '1' ? $cfup : $ffup;
			$fup == $fid && adminmsg('setforum_fupsame');
			if (!$fup || !is_numeric($fup)) {
				$fupfid = $db->get_one("SELECT fid FROM pw_forums WHERE type='category' ORDER BY fid LIMIT 1");
				$fup = $fupfid['fid'];
			}
			if (!empty($password) && strlen($password) != 32) {
				$password = md5($password);
			}
			$allowvisit		&& $allowvisit		= ','.implode(",",$allowvisit).',';
			$allowread		&& $allowread		= ','.implode(",",$allowread).',';
			$allowpost		&& $allowpost		= ','.implode(",",$allowpost).',';
			$allowrp		&& $allowrp			= ','.implode(",",$allowrp).',';
			$allowupload	&& $allowupload		= ','.implode(",",$allowupload).',';
			$allowdownload	&& $allowdownload	= ','.implode(",",$allowdownload).',';
			$allowtype = array_sum($allowtype);

			$rt = $db->get_one("SELECT type,cms FROM pw_forums WHERE fid=".pwEscape($fup));
			if ($rt['type'] == 'category') {
				$type = 'forum';
			} elseif ($rt['type'] == 'forum') {
				if (($rt['cms'] && !$cms) || (!$rt['cms'] && $cms)) {
					adminmsg('setforum_cms');
				}
				$type = 'sub';
			} elseif ($rt['type'] == 'sub') {
				$type = 'sub2';
			}
			$newtdb = array();
			foreach ($new_t_db as $key => $value){
				if ($value) $newtdb[] = $value;
			}
			$t_db = array_merge($t_db,$newtdb);
			$t_type = implode("\t",$t_db);/*主題分類*/
			$t_type = Quot_cv($t_type);
			if (strlen($t_type) > 255) {
				adminmsg('beyond_sql_max_size');
			}
			/*if ($forum['fup'] != $fup) {
				$rt = $db->get_one("SELECT fid FROM pw_forums WHERE fup=".pwEscape($fid));
				if ($rt && $rt['fid'] != $fid) {
					adminmsg('forum_havesub');
				}
			}*/
			if ($f_type == 'hidden' && $allowvisit == '') {
				$basename = "$admin_file?adminjob=setforum&action=edit&fid=$fid";
				adminmsg('forum_hidden');
			}
			$db_uploadfiletype = !empty($db_uploadfiletype) ? unserialize($db_uploadfiletype) : array();
			if ($logotype == 'upload') {
				if ($ifdellogo == 1) {
					pwDelatt($forum['logo'],$db_ifftp);
					$db->update("UPDATE pw_forums SET logo='' WHERE fid=".pwEscape($fid));
					$forum['logo'] = '';
				}
				require_once(R_P.'require/postfunc.php');
				$uploaddb = UploadFile($winduid,'forumlogo');
				$logo = !empty($uploaddb) ? $uploaddb[0]['attachurl'] : $forum['logo'];
			} elseif ($logotype == 'url') {
				$logo = $logo_url;
			}
			
			$db->update("UPDATE pw_forums SET " . pwSqlSingle(array(
				'fup'		=> $fup,			'type'		=> $type,
				'name'		=> $name,			'vieworder'	=> $vieworder,
				'logo'		=> $logo,			'keywords'	=> $keywords,
				'descrip'	=> $descrip,		'style'		=> $style,
				'across'	=> $across,			'allowhide'	=> $allowhide,
				'allowsell'	=> $allowsell,		'allowtype'	=> $allowtype,
				'copyctrl'	=> $copyctrl,		'password'	=> $password,
				'viewsub'	=> $viewsub,		'allowvisit'=> $allowvisit,
				'allowread'	=> $allowread,		'allowpost'	=> $allowpost,
				'allowrp'	=> $allowrp,		'allowdownload'=> $allowdownload,
				'allowupload' => $allowupload,	'f_type'	=> $f_type,
				'f_check'	=> $f_check,		't_type'	=> $t_type,
				'forumsell'	=> $forumsell,		'cms'		=> $cms,
				'ifhide'	=> $ifhide,			'showsub'	=> $showsub
			)) . " WHERE fid=".pwEscape($fid));
			updateforum($fup);
			updateforum($forum['fup']);
		}
		P_unlink(D_P.'data/bbscache/c_cache.php');

		$othersql = $otherfids = array();
		$update_f = '';
		if (is_array($otherfid)) {
			$otherfids = pwImplode($otherfid);
		}
		if (is_array($otherforum)) {
			foreach ($otherforum as $key => $value) {
				if ($key === 'forumsetdb') {
					$update_f = 1;
					continue;
				}
				$othersql[$key] = $$key;
			}
		}
		if ($othersql && $otherfids) {
			$db->update("UPDATE pw_forums SET".pwSqlSingle($othersql)."WHERE fid IN($otherfids)");
		}
		if ($otherfids && $update_f) {
			include(D_P.'data/bbscache/forum_cache.php');
			foreach ($otherfid as $key => $selfid) {
				if (!$selfid || !is_numeric($selfid) || $selfid == $fid || $forum[$selfid]['type'] == 'category') {
					continue;
				}
				$rt = $db->get_one("SELECT fid,forumset FROM pw_forumsextra WHERE fid=".pwEscape($selfid));
				if ($rt['fid']) {
					$newforumset = unserialize($rt['forumset']);
					foreach ($forumsetdb as $key => $value) {
						if ($otherforum['forumsetdb'][$key]) {
							$newforumset[$key] = $value;
						} elseif (!isset($newforumset[$key])) {
							$newforumset[$key] = 0;
						}
					}
					$forumset = serialize($newforumset);
					$db->update("UPDATE pw_forumsextra SET forumset=".pwEscape($forumset,false)."WHERE fid=".pwEscape($selfid));
				} else {
					$newforumset = array();
					foreach ($forumsetdb as $key => $value) {
						if ($otherforum['forumsetdb'][$key]) {
							$newforumset[$key] = $value;
						} else {
							$newforumset[$key] = 0;
						}
					}
					$forumset = serialize($newforumset);
					$db->update("INSERT INTO pw_forumsextra SET forumset=".pwEscape($forumset).',fid='.pwEscape($selfid));
				}
			}
		}
		updatecache_f();
		$basename = "$admin_file?adminjob=setforum&action=edit&fid=$fid";
		adminmsg('operate_success');
	}
} elseif ($action == 'changename') {

	$fid = (int)GetGP('fid');
	InitGP(array('fname'),'P',0);
	$fname     = str_replace('<iframe','&lt;iframe',$fname);
	$fname	 = str_replace(array('<iframe','"',"'"),array("&lt;iframe","",""),$fname);
	$db->update("UPDATE pw_forums SET name=" . pwEscape($fname)." WHERE fid=".pwEscape($fid));
	updatecache_f();
	$msg = getLangInfo('cpmsg','operate_success');
	echo $msg;
	ajax_footer();
}

function delforum($fid) {
	global $db,$db_guestdir,$db_guestthread,$db_guestread;
	$foruminfo = $db->get_one("SELECT fid,fup,forumadmin FROM pw_forums WHERE fid=".pwEscape($fid));
	$db->update("DELETE FROM pw_forums WHERE fid=".pwEscape($fid));
	$db->update("DELETE FROM pw_forumdata WHERE fid=".pwEscape($fid));
	$db->update("DELETE FROM pw_forumsextra WHERE fid=".pwEscape($fid));
	$db->update("DELETE FROM pw_permission WHERE fid>'0' AND fid=".pwEscape($fid));
	if ($foruminfo['forumadmin']) {
		$forumadmin = explode(",",$foruminfo['forumadmin']);
		foreach ($forumadmin as $key => $value) {
			if ($value) {
				$gid = $db->get_one("SELECT uid,groupid FROM pw_members WHERE username=".pwEscape($value,false));
				if ($gid['groupid'] == 5 && !ifadmin($value)) {
					$db->update("UPDATE pw_members SET groupid='-1' WHERE username=".pwEscape($value,false));
					admincheck($gid['uid'],$value,$gid['groupid'],'','delete');
				}
			}
		}
	}
	if ($db_guestthread || $db_guestread) {
		require_once(R_P.'require/guestfunc.php');
		$db_guestthread && deldir(D_P."$db_guestdir/T_{$fid}");
	}
	$ttable_a = $ptable_a = array();
	$query = $db->query("SELECT tid,replies,ptable FROM pw_threads WHERE fid=".pwEscape($fid));
	while ($tpc = $db->fetch_array($query)) {
		$tid = $tpc['tid'];
		$ttable_a[GetTtable($tid)][] = $tid;
		$ptable_a[$tpc['ptable']] = 1;
		$db_guestread && clearguestcache($tid,$tpc['replies']);
	}
	foreach ($ttable_a as $pw_tmsgs => $val) {
		$val = pwImplode($val,false);
		$db->update("DELETE FROM $pw_tmsgs WHERE tid IN($val)");
	}
	$db->update("DELETE FROM pw_threads WHERE fid=".pwEscape($fid));
	foreach ($ptable_a as $key => $val) {
		$pw_posts = GetPtable($key);
		$db->update("DELETE FROM $pw_posts WHERE fid=".pwEscape($fid));
	}
	updateforum($foruminfo['fup']);
}

function updateadmin() {
	global $db;
	$f_admin = array();
	$query = $db->query("SELECT forumadmin FROM pw_forums");
	while ($forum = $db->fetch_array($query)) {
		$adminarray = explode(",",$forum['forumadmin']);
		foreach ($adminarray as $key => $value) {
			$value = trim($value);
			if ($value) {
				$f_admin[] = $value;
			}
		}
	}
	$f_admin = array_unique($f_admin);

	$query = $db->query("SELECT uid,username,groupid,groups FROM pw_administrators WHERE groupid=5 OR groups LIKE '%,5,%'");
	while ($rt = $db->fetch_array($query)) {
		if (!in_array($rt['username'],$f_admin)) {
			if ($rt['groupid'] == '5') {
				$db->update("UPDATE pw_members SET groupid='-1' WHERE uid='$rt[uid]'");
				$rt['groupid'] = -1;
			} else {
				$rt['groups'] = str_replace(',5,',',',$rt['groups']);
				$rt['groups'] == ',' && $rt['groups'] = '';
				$db->update("UPDATE pw_members SET groups='$rt[groups]' WHERE uid='$rt[uid]'");
			}
			if ($rt['groupid'] == '-1' && $rt['groups'] == '') {
				admincheck($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups'],'delete');
			} else {
				admincheck($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups'],'update');
			}
		}
	}
	if ($f_admin) {
		$usernames = pwImplode($f_admin);
		$pwSQL = array();
		$query = $db->query("SELECT m.uid,m.username,m.groupid,m.groups,a.groupid AS gid,a.groups AS gps FROM pw_members m LEFT JOIN pw_administrators a ON m.uid=a.uid WHERE m.username IN($usernames)");
		while ($rt = $db->fetch_array($query)) {
			if ($rt['groupid'] == '-1') {
				$rt['groups'] = str_replace(',5,',',',$rt['groups']);
				$rt['groups'] == ',' && $rt['groups'] = '';
				$db->update("UPDATE pw_members SET groupid='5',groups=" . pwEscape($rt['groups']) . ' WHERE uid='.pwEscape($rt['uid']));
				$rt['groupid'] = 5;
			} elseif ($rt['groupid'] != '5' && strpos($rt['groups'],',5,') === false) {
				$rt['groups'] = $rt['groups'] ? $rt['groups'].'5,' : ",5,";
				$db->update("UPDATE pw_members SET groups=" . pwEscape($rt['groups']) . ' WHERE uid='.pwEscape($rt['uid']));
			}
			if ($rt['groupid'] <> $rt['gid'] || $rt['groups'] <> $rt['gps']) {
				$pwSQL[] = array($rt['uid'],$rt['username'],$rt['groupid'],$rt['groups']);
			}
		}
		if ($pwSQL) {
			$db->update("REPLACE INTO pw_administrators (uid,username,groupid,groups) VALUES ".pwSqlMulti($pwSQL));
		}
	}
}
?>