<?php
require_once('global.php');
require_once(R_P.'require/forum.php');
include_once(D_P.'data/bbscache/forum_cache.php');
$groupid == 'guest' && Showmsg('not_login');

$fiddb = array();
InitGP(array('action'));

$isGM = CkInArray($windid,$manager);
if ($action) {
	!$fid && Showmsg('data_error');
	$forums = $db->get_one("SELECT f.name,f.forumadmin,f.fupadmin,fe.forumset FROM pw_forums f LEFT JOIN pw_forumsextra fe USING(fid) WHERE f.fid=".pwEscape($fid)." AND f.type!='category'");
	!$forums && Showmsg('data_error');
	$isBM = admincheck($forums['forumadmin'],$forums['fupadmin'],$windid);
	if (!in_array($groupid,array('3','4')) && !$isBM) {
		Showmsg('not_forumadmin');
	}
	$forumset = unserialize($forums['forumset']);
	$first_admin = $db_adminset && strpos($forums['forumadmin'],','.$windid.',')===0 ? 1 : 0;
} else {
	$query = $db->query("SELECT fid,forumadmin,fupadmin FROM pw_forums WHERE cms=0 AND type!='category'");
	while ($rt = $db->fetch_array($query)) {
		if (in_array($groupid,array('3','4')) || admincheck($rt['forumadmin'],$rt['fupadmin'],$windid)) {
			$fiddb[] = $rt['fid'];
		}
	}
	!$fiddb && Showmsg('not_forumadmin');
}
require_once(R_P.'require/header.php');

if (!$action) {

	$forum_name = '';
	$fids		= pwImplode($fiddb);
	$froumdb	= array();
	$query = $db->query("SELECT * FROM pw_forums f LEFT JOIN pw_forumdata fd USING(fid) WHERE f.fid IN($fids)");
	while ($rt = $db->fetch_array($query)) {
		$forumdb[] = $rt;
	}
	$i = count($forumdb);
	if ($i > 4) {
		$j_sum = 4;
		$j_wid = '25%';
	} else {
		$j_sum = $i;
		$j_wid = (100/$i).'%';
	}
	require_once(PrintEot('forumcp'));footer();

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

	$forum_name = $forums['name'];
	InitGP(array('type'));
	!$type && $type = 'notice';

	if ($type == 'notice') {

		$annoucedb = array();
		$pages = ''; $page = $_GET['page']; (int)$page<1 && $page = 1;
		$query = $db->query('SELECT aid,ifopen,vieworder,author,subject,startdate,enddate FROM pw_announce WHERE fid='.pwEscape($fid).' ORDER BY fid,vieworder,startdate DESC '.pwLimit(($page-1)*$db_perpage,$db_perpage));
		while ($rt = $db->fetch_array($query)) {
			$rt['subject'] = substrs($rt['subject'],30);
			$rt['starttime'] = $rt['startdate'] ? get_date($rt['startdate'],'Y-m-d H:i') : '--';
			$rt['endtime'] = $rt['enddate'] ? get_date($rt['enddate'],'Y-m-d H:i') : '--';
			$annoucedb[] = $rt;
		}
		$db->free_result($query);
		$count = $db->get_value('SELECT COUNT(*) FROM pw_announce WHERE fid='.pwEscape($fid));
		if ($count > $db_perpage) {
			require_once(R_P.'require/forum.php');
			$pages = numofpage($count,$page,ceil($count/$db_perpage), "forumcp.php?action=edit&fid=$fid&type=$type&");
		}
		require_once(PrintEot('forumcp'));footer();

	} elseif ($type == 'n_del') {

		PostCheck();
		$aid = (int)$_GET['aid'];
		$rt = $db->get_one('SELECT aid,fid,ifopen FROM pw_announce WHERE aid='.pwEscape($aid));
		(!$rt['aid'] || $rt['fid']!=$fid) && Showmsg('data_error');
		$db->update('DELETE FROM pw_announce WHERE aid='.pwEscape($aid));
		if ($rt['ifopen']) {
			require_once(R_P.'require/updatenotice.php');
			updatecache_i($fid);
		}
		refreshto("forumcp.php?action=edit&fid=$fid",'operate_success');

	} elseif ($type == 'n_order') {

		PostCheck();
		!is_array($vieworder = $_POST['vieworder']) && $vieworder = array();
		$updatedb = array();
		foreach ($vieworder as $key => $value) {
			if (is_numeric($key)) {
				$value = (int)$value;
				$updatedb[$value] .= ",'$key'";
			}
		}
		foreach ($updatedb as $key => $value) {
			$value && $db->update("UPDATE pw_announce SET vieworder='$key' WHERE aid IN (".substr($value,1).')');
		}
		require_once(R_P.'require/updatenotice.php');
		updatecache_i($fid);
		refreshto("forumcp.php?action=edit&fid=$fid",'operate_success');

	} elseif ($type == 'add' || $type == 'edit') {
		InitGP(array('aid'),'GP',2);

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

			$ifopen_Y = 'CHECKED'; $vieworder = (int)$vieworder;
			$ifopen_N = $subject = $atc_content = $enddate = '';
			$startdate = get_date($timestamp,'Y-m-d H:i');
			if ($type == 'edit') {
				$db_redundancy = 0;
				$rt = $db->get_one('SELECT aid,fid,ifopen,vieworder,startdate,enddate,subject,content FROM pw_announce WHERE aid='.pwEscape($aid));
				!$rt['aid'] && Showmsg('data_error');
				extract($rt,EXTR_OVERWRITE);
				if (!$ifopen) {
					$ifopen_Y = '';
					$ifopen_N = 'CHECKED';
				}
				$startdate && $startdate = get_date($startdate,'Y-m-d H:i'); $enddate && $enddate = get_date($enddate,'Y-m-d H:i');
				$atc_content = $content;
			}
			require_once(PrintEot('forumcp'));footer();

		} else {

			PostCheck();
			!$fid && Showmsg('annouce_fid');
			InitGP(array('startdate','enddate','atc_title'),'P');
			$startdate = $startdate ? PwStrtoTime($startdate) : $timestamp;
			$enddate = $enddate ? PwStrtoTime($enddate) : '';
			$enddate && $enddate<=$startdate && Showmsg('annouce_time');
			InitGP(array('ifopen','vieworder'),'P',2);
			$atc_content = trim(Char_cv($_POST['atc_content']));
			if ($type == 'add') {
				!$atc_title && Showmsg('annouce_title');
				!$atc_content && Showmsg('annouce_content');

				$pwSQL = pwSqlSingle(array(
					'fid'		=> $fid,
					'ifopen'	=> $ifopen,
					'vieworder'	=> $vieworder,
					'author'	=> $windid,
					'startdate'	=> $startdate,
					'enddate'	=> $enddate,
					'url'		=> $url,
					'subject'	=> $atc_title,
					'content'	=> $atc_content
				));
				$db->update("INSERT INTO pw_announce SET $pwSQL");
				if ($ifopen && (!$enddate || $enddate>=$timestamp)) {
					require_once(R_P.'require/updatenotice.php');
					updatecache_i($fid);
				}
			} else {
				$rt = $db->get_one('SELECT aid,fid,content FROM pw_announce WHERE aid='.pwEscape($aid));
				!$atc_title && Showmsg('annouce_title');
				!$atc_content && Showmsg('annouce_content');

				(!$rt['aid'] || $rt['fid']!=$fid) && Showmsg('data_error');
				$pwSQL = pwSqlSingle(array(
					'ifopen'	=> $ifopen,
					'vieworder'	=> $vieworder,
					'startdate'	=> $startdate,
					'enddate'	=> $enddate,
					'url'		=> $url,
					'subject'	=> $atc_title,
					'content'	=> $atc_content
				));
				$db->update("UPDATE pw_announce SET $pwSQL WHERE aid=".pwEscape($aid));
				require_once(R_P.'require/updatenotice.php');
				updatecache_i($fid);
			}
			refreshto("forumcp.php?action=edit&fid=$fid",'operate_success');
		}
	} elseif ($type == 'report') {

		InitGP(array('page'),'GP',2);
		$page < 1 && $page = 1;
		$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);

		$rt = $db->get_one('SELECT COUNT(*) AS count FROM pw_report r LEFT JOIN pw_threads t ON t.tid=r.tid WHERE t.fid='.pwEscape($fid));
		$sum = $rt['count'];
		$numofpage = ceil($sum/$db_perpage);
		$pages = numofpage($sum,$page,$numofpage,"forumcp.php?action=edit&type=report&fid=$fid&");

		$query = $db->query('SELECT r.*,m.username,t.fid FROM pw_report r LEFT JOIN pw_members m ON m.uid=r.uid LEFT JOIN pw_threads t ON t.tid=r.tid WHERE t.fid='.pwEscape($fid).' ORDER BY id '.$limit);
		while ($rt = $db->fetch_array($query)) {
			$rt['fname'] = $forum[$rt['fid']]['name'];
			$reportdb[] = $rt;
		}
		require_once(PrintEot('forumcp'));footer();

	} elseif ($type == 'f_type') {

		$rt = $db->get_one('SELECT f.t_type,fe.fid,fe.forumset FROM pw_forums f LEFT JOIN pw_forumsextra fe USING(fid) WHERE f.fid='.pwEscape($fid));
		$forumset = unserialize($rt['forumset']);

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

			$forumset['addtpctype'] ? $addtpctype_Y='checked' : $addtpctype_N='checked';
			$t_typedb=explode("\t",$rt['t_type']);/*主题分类*/
			$t_typedb[0] = (int)$t_typedb[0];
			${'t_type_'.$t_typedb[0]}='checked';
			require_once(PrintEot('forumcp'));footer();

		} else {

			PostCheck();
			Add_S($forumset);
			InitGP(array('t_db','addtpctype','new_t_db'),'P');
			$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 = str_replace('"','&quot;',$t_type);
			if(strlen($t_type) > 255){
				Showmsg('beyond_sql_max_size');
			}
			if ($t_type != $rt['t_type']) {
				$db->update('UPDATE pw_forums SET t_type='.pwEscape($t_type).' WHERE fid='.pwEscape($fid));
			}
			if ($addtpctype != $forumset['addtpctype']) {
				$forumset['addtpctype'] = $addtpctype;
				$forumset = serialize($forumset);
				if ($rt['fid']) {
					$db->update('UPDATE pw_forumsextra SET forumset='.pwEscape($forumset).' WHERE fid='.pwEscape($fid));
				} else {
					$db->update('INSERT INTO pw_forumsextra SET '.pwSqlSingle(array('fid'=>$fid,'forumset'=>$forumset)));
				}
			}
			refreshto("forumcp.php?action=edit&type=f_type&fid=$fid",'operate_success');
		}
	} elseif ($type == 'reward') {

		InitGP(array('starttime','endtime','username'));
		InitGP(array('page'),'GP',2);
		$page < 1 && $page=1;
		$limit = "LIMIT ".($page-1)*$db_perpage.",$db_perpage";

		$sql = $url_a = '';
		$_POST['starttime'] && $starttime= PwStrtoTime($starttime);
		$_POST['endtime']   && $endtime  = PwStrtoTime($endtime);
		if ($username) {
			$sql.=' AND t.author='.pwEscape($username);
			$url_a.="username=".rawurlencode($username)."&";
		}
		if ($starttime) {
			$sql.=' AND t.postdate>'.pwEscape($starttime);
			$url_a.="starttime=$starttime&";
		}
		if ($endtime) {
			$sql.=' AND t.postdate<'.pwEscape($endtime);
			$url_a.="endtime=$endtime&";
		}

		$rt = $db->get_one("SELECT COUNT(*) AS count FROM pw_threads t LEFT JOIN pw_reward r USING(tid) WHERE t.fid=".pwEscape($fid)." AND t.special='3' AND t.state='0' AND r.timelimit<".pwEscape($timestamp).$sql);
		$sum = $rt['count'];
		$numofpage = ceil($sum/$db_perpage);
		$pages = numofpage($sum,$page,$numofpage,"forumcp.php?action=edit&type=reward&fid=$fid&$url_a");

		$threaddb = array();
		$query = $db->query("SELECT t.tid,t.fid,t.subject,t.author,t.authorid,t.postdate,r.cbtype,r.cbval,r.catype,r.caval FROM pw_threads t LEFT JOIN pw_reward r USING(tid) WHERE t.fid=".pwEscape($fid)." AND t.special='3' AND t.state='0' AND r.timelimit<".pwEscape($timestamp).$sql." ORDER BY t.postdate $limit");

		while ($rt = $db->fetch_array($query)) {
			$rt['postdate'] = get_date($rt['postdate'],'Y-m-d');
			$rt['cbtype'] = is_numeric($rt['cbtype']) ? $_CREDITDB[$rt['cbtype']][0] : ${'db_'.$rt['cbtype'].'name'};
			$rt['catype'] = is_numeric($rt['catype']) ? $_CREDITDB[$rt['catype']][0] : ${'db_'.$rt['catype'].'name'};
			$rt['binfo'] = $rt['cbval']."&nbsp;".$rt['cbtype'];
			$rt['ainfo'] = $rt['caval']."&nbsp;".$rt['catype'];
			$threaddb[]  = $rt;
		}
		require_once(PrintEot('forumcp'));footer();

	} elseif ($type == 'thread') {

		InitGP(array('starttime','endtime','username','t_type'));
		InitGP(array('page'),'GP',2);
		$page < 1 && $page=1;
		$limit="LIMIT ".($page-1)*$db_perpage.",$db_perpage";
		$sql = $url_a = '';
		$_POST['starttime'] && $starttime= PwStrtoTime($starttime);
		$_POST['endtime']   && $endtime  = PwStrtoTime($endtime);
		if ($username) {
			$sql.=' AND author='.pwEscape($username);
			$url_a.="username=".rawurlencode($username)."&";
		}
		if ($starttime) {
			$sql.=' AND postdate>'.pwEscape($starttime);
			$url_a.="starttime=$starttime&";
		}
		if ($endtime) {
			$sql.=' AND postdate<'.pwEscape($endtime);
			$url_a.="endtime=$endtime&";
		}
		if ($t_type) {
			switch($t_type) {
				case 'digest':
					$sql.=" AND digest>'0'";
					break;
				case 'active':
					$sql.=" AND special='2'";
					break;
				case 'reward':
					$sql.=" AND special='3'";
					break;
				case 'sale':
					$sql.=" AND special='4'";
					break;
				default :
					$sql.=" AND digest>'0'";
			}
			$url_a.="t_type=$t_type&";
		}
		if ($sql) {
			$rt = $db->get_one("SELECT COUNT(*) AS sum FROM pw_threads WHERE fid=".pwEscape($fid)." AND ifcheck=1 $sql");
		} else {
			$rt = $db->get_one('SELECT topic AS sum FROM pw_forumdata WHERE fid='.pwEscape($fid));
		}
		$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "forumcp.php?action=edit&type=thread&fid=$fid&$url_a");
		$query = $db->query("SELECT tid,subject,author,authorid,postdate,titlefont,topped,digest FROM pw_threads WHERE fid=".pwEscape($fid)." AND ifcheck='1' $sql ORDER BY topped DESC,lastpost DESC $limit");
		$threaddb = array();
		while ($rt = $db->fetch_array($query)) {
			$rt['subject'] = substrs($rt['subject'],35);
			if ($rt['titlefont']) {
				$titledetail = explode("~",$rt['titlefont']);
				if ($titledetail[0])$rt['subject'] = "<font color=\"$titledetail[0]\">$rt[subject]</font>";
				if ($titledetail[1])$rt['subject'] = "<b>$rt[subject]</b>";
				if ($titledetail[2])$rt['subject'] = "<i>$rt[subject]</i>";
				if ($titledetail[3])$rt['subject'] = "<u>$rt[subject]</u>";
			}
			$rt['postdate'] = get_date($rt['postdate']);
			$threaddb[] = $rt;
		}
		require_once PrintEot('forumcp');footer();

	} elseif ($type == 'tcheck') {
		if(!$isGM && !pwRights($isBM,'viewcheck')) Showmsg('not_forumadmin');
		if (empty($_POST['step'])) {

			InitGP(array('starttime','endtime','username'));
			InitGP(array('page'),'GP',2);
			$page < 1 && $page=1;
			$limit = "LIMIT ".($page-1)*$db_perpage.",$db_perpage";
			$sql = $url_a = '';
			$_POST['starttime'] && $starttime= PwStrtoTime($starttime);
			$_POST['endtime']   && $endtime  = PwStrtoTime($endtime);
			if ($username) {
				$sql.=' AND author='.pwEscape($username);
				$url_a.="username=".rawurlencode($username)."&";
			}
			if ($starttime) {
				$sql.=' AND postdate>'.pwEscape($starttime);
				$url_a.="starttime=$starttime&";
			}
			if ($endtime) {
				$sql.=' AND postdate<'.pwEscape($endtime);
				$url_a.="endtime=$endtime&";
			}
			$rt = $db->get_one("SELECT COUNT(*) AS sum FROM pw_threads WHERE fid=".pwEscape($fid)." AND ifcheck='0' $sql");
			$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "forumcp.php?action=edit&type=tcheck&fid=$fid&$url_a");

			$threaddb = $ttable_a = array();
			$query = $db->query("SELECT tid,subject,author,authorid,postdate FROM pw_threads WHERE fid=".pwEscape($fid)." AND ifcheck='0' $sql ORDER BY topped DESC,lastpost DESC $limit");
			while ($rt = $db->fetch_array($query)) {
				$rt['subject']	= substrs($rt['subject'],35);
				$rt['postdate']	= get_date($rt['postdate']);
				$threaddb[$rt['tid']] = $rt;
				$ttable_a[GetTtable($rt['tid'])][] = $rt['tid'];
			}
			include_once(D_P.'data/bbscache/wordsfb.php');
			foreach ($ttable_a as $pw_tmsgs=>$value) {
				$value = pwImplode($value);
				$query = $db->query("SELECT tid,content FROM $pw_tmsgs WHERE tid IN($value)");
				while ($rt = $db->fetch_array($query)) {
					$rt['content'] = str_replace("\n","<br>",$rt['content']);
					foreach ($alarm as $key=>$value) {
						$rt['content'] = str_replace($key,'<span style="background-color:#ffff66">'.$key.'</span>',$rt['content']);
					}
					$threaddb[$rt['tid']]['content'] = $rt['content'];
				}
			}
			require_once PrintEot('forumcp');footer();

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

			PostCheck();
			InitGP(array('selid'));
			$tids = '';
			foreach ($selid as $key=>$value) {
				is_numeric($value) && $tids .= ($tids ? ',' : '').$value;
			}
			!$tids && Showmsg('id_error');
			$db->update("UPDATE pw_threads SET ifcheck='1' WHERE tid IN($tids) AND fid=".pwEscape($fid));
			require_once(R_P.'require/updateforum.php');
			updateforum($fid);
			refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');

		} else {

			PostCheck();
			InitGP(array('selid'));
			$delids   = '';
			$ttable_a = $attachdb = array();
			foreach ($selid as $key => $value) {
				if (is_numeric($value)) {
					$delids .= $value.',';
					$ttable_a[GetTtable($value)] .= $value.',';
				}
			}
			!$delids && Showmsg('mawhole_nodata');
			$delids = substr($delids,0,-1);

			require_once(R_P.'require/updateforum.php');
			foreach ($ttable_a as $pw_tmsgs => $value) {
				$value = substr($value,0,-1);
				$query = $db->query("SELECT t.tid,t.fid,tm.aid FROM pw_threads t LEFT JOIN $pw_tmsgs tm ON tm.tid=t.tid WHERE t.tid IN($value)");
				while ($read = $db->fetch_array($query)) {
					$read['fid'] != $fid && Showmsg('admin_forum_right');
					if ($read['aid']) $attachdb[] = $read['aid'];
				}
			}
			$db->update("DELETE FROM pw_threads WHERE tid IN($delids) AND fid=".pwEscape($fid)." AND ifcheck='0'");
			foreach ($ttable_a as $pw_tmsgs => $val) {
				$val = substr($val,0,-1);
				$db->update("DELETE FROM $pw_tmsgs WHERE tid IN($val)");
			}
			delete_tag($delids);

			if ($attachdb) {
				$ftp = null;
				if ($db_ifftp) {
					require_once(R_P.'require/ftp.php');
					$ftp = new FTP($ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir);
				}
				delete_att($attachdb);
				if ($ftp) {
					$ftp->close(); unset($ftp);
				}
			}
			refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');
		}
	} elseif ($type == 'pcheck') {
		if(!$isGM && !pwRights($isBM,'viewcheck')) Showmsg('not_forumadmin');
		if (empty($_POST['step'])) {

			InitGP(array('starttime','endtime','username','ptable'));
			InitGP(array('page'),'GP',2);
			$page < 1 && $page=1;
			$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
			$sql = $url_a = '';
			$_POST['starttime'] && $starttime= PwStrtoTime($starttime);
			$_POST['endtime']   && $endtime  = PwStrtoTime($endtime);
			if ($username) {
				$sql.=' AND author='.pwEscape($username);
				$url_a.="username=".rawurlencode($username)."&";
			}
			if ($starttime) {
				$sql.=' AND postdate>'.pwEscape($starttime);
				$url_a.="starttime=$starttime&";
			}
			if ($endtime) {
				$sql.=' AND postdate<'.pwEscape($endtime);
				$url_a.="endtime=$endtime&";
			}
			if ($db_plist) {
				!is_numeric($ptable) && $ptable = $db_ptable;
				$p_table = "<option value=\"0\">post</option>";
				$p_list  = explode(',',$db_plist);
				foreach ($p_list as $key=>$val) {
					$p_table .= "<option value=\"$val\">post$val</option>";
				}
				$p_table  = str_replace("<option value=\"$ptable\">","<option value=\"$ptable\" selected>",$p_table);
				$url_a	 .= "ptable=$ptable&";
				$pw_posts = GetPtable($ptable);
			} else {
				$pw_posts = 'pw_posts';
			}
			$rt = $db->get_one("SELECT COUNT(*) AS sum FROM $pw_posts WHERE fid=".pwEscape($fid)." AND ifcheck='0' $sql");
			$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "forumcp.php?action=edit&type=$type&fid=$fid&$url_a");

			$postdb = array();
			include_once(D_P.'data/bbscache/wordsfb.php');
			$query = $db->query("SELECT pid,tid,subject,author,authorid,postdate,content FROM $pw_posts WHERE fid=".pwEscape($fid)." AND ifcheck='0' $sql $limit");
			while ($rt = $db->fetch_array($query)) {
				if ($rt['subject']) {
					$rt['subject'] = substrs($rt['subject'],35);
				} else {
					$rt['subject'] = substrs($rt['content'],35);
				}
				$rt['postdate'] = get_date($rt['postdate']);
				$rt['content']  = str_replace("\n","<br>",$rt['content']);
				foreach ($alarm as $key => $value) {
					$rt['content'] = str_replace($key,'<span style="background-color:#ffff66">'.$key.'</span>',$rt['content']);
				}
				$postdb[] = $rt;
			}
			require_once PrintEot('forumcp');footer();

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

			PostCheck();
			InitGP(array('selid','ptable'));
			$tids = '';
			foreach ($selid as $key => $value) {
				is_numeric($value) && $tids .= ($tids ? ',' : '').$value;
			}
			!$tids && Showmsg('id_error');
			$pw_posts = GetPtable($ptable);
			$db->update("UPDATE $pw_posts SET ifcheck='1' WHERE pid IN($tids) AND fid='$fid'");
			require_once(R_P.'require/updateforum.php');
			updateforum($fid);
			refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');

		} else {

			PostCheck();
			InitGP(array('selid','ptable'));
			$tids = '';
			$attachdb = array();
			foreach ($selid as $key => $value) {
				is_numeric($value) && $tids .= ($tids ? ',' : '').$value;
			}
			!$tids && Showmsg('id_error');
			$pw_posts = GetPtable($ptable);
			$query = $db->query("SELECT fid,aid FROM $pw_posts WHERE pid IN($tids)");
			while ($rt = $db->fetch_array($query)) {
				$rt['fid'] != $fid && Showmsg('admin_forum_right');
				if ($rt['aid']) $attachdb[] = $rt['aid'];
			}
			$db->update("DELETE FROM $pw_posts WHERE pid IN($tids)");
			if ($attachdb) {
				$ftp = null;
				if ($db_ifftp) {
					require_once(R_P.'require/ftp.php');
					$ftp = new FTP($ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir);
				}
				require_once(R_P.'require/updateforum.php');
				delete_att($attachdb);
				if ($ftp) {
					$ftp->close(); unset($ftp);
				}
			}
			refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');
		}
	} elseif ($type == 'commend') {

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

			$commend = array();
			if ($forumset['commendlist']) {
				$query = $db->query("SELECT tid,authorid,author,postdate,subject FROM pw_threads WHERE tid IN($forumset[commendlist])");
				while ($rt=$db->fetch_array($query)) {
					$rt['postdate'] = get_date($rt['postdate']);
					$commend[] = $rt;
				}
			}
			require_once PrintEot('forumcp');footer();

		} else {

			PostCheck();
			InitGP(array('selid'));
			foreach ($selid as $key => $value) {
				if (is_numeric($value)) {
					$forumset['commendlist'] = substr(str_replace(",$value,",",",",$forumset[commendlist],"),1,-1);
				}
			}
			updatecommend($fid,$forumset);
			refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');
		}
	} elseif ($type == 'adminset') {

		!$first_admin && Showmsg('undefined_action');
		$admin_a = explode(',',trim($forums['forumadmin'],','));
		$firstadmin = $admin_a[0];
		$firstadmin != $windid && Showmsg('undefined_action');
		if (empty($_POST['step'])) {
			$s_admin = trim(str_replace(",$firstadmin,",',',$forums['forumadmin']),',');
			require_once(PrintEot('forumcp'));footer();

		} else {

			PostCheck();
			InitGP(array('forumadmin'),'P');
			$errorname = '';
			if ($forums['forumadmin'] != stripslashes(",".$windid.",$forumadmin,")) {
				$newadmin = array('0'=>$windid);
				$newadmin_a = array_unique(explode(",",$forumadmin));
				foreach ($newadmin_a as $aid => $value) {
					$value = trim($value);
					if ($value && !in_array($value,$newadmin)) {
						$mb = $db->get_one('SELECT uid,groupid,groups FROM pw_members WHERE username='.pwEscape($value));
						if ($mb) {
							$newadmin[] = $value;
							if ($mb['groupid'] == -1) {
								$db->update("UPDATE pw_members SET groupid='5' WHERE uid=".pwEscape($mb['uid']));
								$pwSQL = pwSqlSingle(array(
									'uid'		=> $mb['uid'],
									'username'	=> $value,
									'groupid'	=> 5,
									'groups'	=> $mb['groups']
								));
								$db->update("REPLACE INTO pw_administrators SET $pwSQL");
							} elseif ($mb['groupid'] <> 5 && strpos($mb['groups'],',5,')===false) {
								$mb['groups'] = $mb['groups'] ? $mb['groups'].'5,' : ",5,";
								$db->update('UPDATE pw_members SET groups='.pwEscape($mb['groups']).' WHERE uid='.pwEscape($mb['uid']));
								$pwSQL = pwSqlSingle(array(
									'uid'		=> $mb['uid'],
									'username'	=> $value,
									'groupid'	=> $mb['groupid'],
									'groups'	=> $mb['groups']
								));
								$db->update("REPLACE INTO pw_administrators SET $pwSQL");
							}
						} else {
							$errorname .= ','.$value;
						}
					}
				}
				$oldfadmin = explode(',',trim($forums['forumadmin'],','));
				if ($oldfadmin) {
					$f_admin = array();
					$query = $db->query("SELECT forumadmin FROM pw_forums WHERE fid<>".pwEscape($fid)." AND forumadmin<>''");
					while ($rt = $db->fetch_array($query)) {
						foreach (explode(",",$rt['forumadmin']) as $key=>$value) {
							if ($value = trim($value)) {
								$f_admin[] = $value;
							}
						}
					}
					$f_admin = array_unique($f_admin);

					$oldfadmin = pwImplode($oldfadmin,false);
					$query = $db->query("SELECT uid,username,groupid,groups FROM pw_members WHERE username IN($oldfadmin)");

					while ($rt = $db->fetch_array($query)) {
						if (!in_array($rt['username'],$newadmin) && !in_array($rt['username'],$f_admin)) {
							if ($rt['groupid']=='5') {
								$db->update("UPDATE pw_members SET groupid='-1' WHERE uid=".pwEscape($rt['uid'],false));
								$rt['groupid'] = -1;
							} else {
								$rt['groups'] = str_replace(',5,',',',$rt['groups']);
								$rt['groups']==',' && $rt['groups'] = '';
								$db->update("UPDATE pw_members SET groups=".pwEscape($rt['groups'],false)."WHERE uid=".pwEscape($rt['uid'],false));
							}
							if (in_array($rt['groupid'],array('-1','6','7')) && $rt['groups']=='') {
								$db->update("DELETE FROM pw_administrators WHERE uid=".pwEscape($rt['uid'],false));
							} else {
								$db->update("REPLACE INTO pw_administrators SET".pwSqlSingle(array(
									'uid'		=> $rt['uid'],
									'username'	=> $rt['username'],
									'groupid'	=> $rt['groupid'],
									'groups'	=> $rt['groups']
								),false));
							}
						}
					}
				}
				$newadmin = addslashes(implode(',',$newadmin));
				$db->update("UPDATE pw_forums SET forumadmin=',$newadmin,' WHERE fid=".pwEscape($fid));
				updatecache_fd();
			}
			if ($errorname) {
				$errorname = Char_cv(substr($errorname,1));
				Showmsg('user_not_exists');
			} else {
				refreshto("forumcp.php?action=edit&type=$type&fid=$fid",'operate_success');
			}
		}
	} elseif ($type == 'trecycle' && $db_recycle) {

		require_once(R_P.'require/updateforum.php');
		require_once(R_P.'require/writelog.php');
		InitGP(array('page','step'),'GP',2);

		if (empty($step)) {

			InitGP(array('username','starttime','endtime','t_type'));
			$page<1 && $page = 1;
			$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);

			$sql = $url_a = '';
			$starttime && $starttime= PwStrtoTime($starttime);
			$endtime   && $endtime  = PwStrtoTime($endtime);
			if ($username) {
				$sql.=' AND author='.pwEscape($username);
				$url_a.="username=".rawurlencode($username)."&";
			}
			if ($starttime) {
				$sql.=' AND postdate>'.pwEscape($starttime);
				$url_a.="starttime=$starttime&";
			}
			if ($endtime) {
				$sql.=' AND postdate<'.pwEscape($endtime);
				$url_a.="endtime=$endtime&";
			}
			if ($t_type) {
				switch($t_type) {
					case 'digest':
						$sql.=" AND t.digest>'0'";
						break;
					case 'active':
						$sql.=" AND t.special='2'";
						break;
					case 'reward':
						$sql.=" AND t.special='3'";
						break;
					case 'sale':
						$sql.=" AND t.special='4'";
						break;
					default :
				}
				${'t_type_'.$t_type} = 'selected';
				$url_a.="t_type=$t_type&";
			}
			$rt = $db->get_one("SELECT COUNT(*) AS sum FROM pw_recycle r LEFT JOIN pw_threads t USING(tid) WHERE r.pid='0' AND r.fid=".pwEscape($fid).$sql);
			$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "forumcp.php?action=edit&type=trecycle&fid=$fid&$url_a");

			$ttable_a = array();
			$query = $db->query("SELECT r.*,t.subject,t.author,t.authorid FROM pw_recycle r LEFT JOIN pw_threads t USING(tid) WHERE r.pid='0' AND r.fid=".pwEscape($fid).$sql." ORDER BY deltime DESC $limit");
			while ($rt = $db->fetch_array($query)) {
				$rt['deltime'] = get_date($rt['deltime']);
				$rt['subject'] = substrs($rt['subject'],50);
				$rt['fname']   = $forum[$rt['fid']]['name'];
				$recycledb[$rt['tid']] = $rt;
				$ttable_a[GetTtable($rt['tid'])][] = $rt['tid'];
			}
			foreach ($ttable_a as $pw_tmsgs => $value) {
				$value = pwImplode($value);
				$query = $db->query("SELECT tid,content FROM $pw_tmsgs WHERE tid IN($value)");
				while ($rt = $db->fetch_array($query)) {
					$rt['content'] = str_replace("\n","<br>",$rt['content']);
					$recycledb[$rt['tid']]['content'] = $rt['content'];
				}
			}
			require_once(PrintEot('forumcp'));footer();

		} elseif ($_POST['step'] == '1' && $forumset['recycle'] & 2) {

			PostCheck();
			InitGP(array('ids'),'P');
			count($ids) > 500 && Showmsg('forumcp_recycle_maxcount');
			recycle($ids);
			$logdb = array(
				'type'      => 'recycle',
				'username1' => '',
				'username2' => $windid,
				'field1'    => $fid,
				'field2'    => '',
				'field3'    => '',
				'descrip'   => 'recycle_topic_delete',
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'affect'    => '',
				'forum'		=> $forum[$fid]['name'],
				'reason'	=> ''
			);
			writelog($logdb);
			refreshto("forumcp.php?action=edit&type=trecycle&fid=$fid",'operate_success');

		} elseif ($_POST['step'] == '2' && $forumset['recycle'] & 4) {

			PostCheck();
			InitGP(array('ids'),'P');
			count($ids) > 500 && Showmsg('forumcp_recycle_maxcount');
			$reids = $logdb = $ptable_a = array();
			foreach ($ids as $key => $value) {
				if (is_numeric($value)) {
					$reids[] = $value;
				}
			}
			$reids && $reids = pwImplode($reids);
			!$reids && Showmsg('forumcp_recycle_nodata');
			$query = $db->query("SELECT r.*,t.ptable FROM pw_recycle r LEFT JOIN pw_threads t ON r.tid=t.tid WHERE r.pid='0' AND r.tid IN ($reids)");
			$reids = $ptable_a = array();
			while ($read = $db->fetch_array($query)) {
				$read['fid'] != $fid && Showmsg('admin_forum_right');
				$ptable_a[$read['ptable']] = 1;
				$reids[] = $read['tid'];
			}
			if ($reids) {
				$reids = pwImplode($reids);
				$db->update("UPDATE pw_threads SET ".pwSqlSingle(array('fid'=>$fid,'ifshield'=>0))."WHERE tid IN($reids)");
				$db->update("UPDATE pw_attachs SET fid=".pwEscape($fid)."WHERE tid IN($reids)");
				$db->update("DELETE FROM pw_recycle WHERE tid IN ($reids)");
				foreach ($ptable_a as $key => $val) {
					$pw_posts = GetPtable($key);
					$db->update("UPDATE $pw_posts SET fid=".pwEscape($fid)."WHERE tid IN($reids)");
				}
			}
			updateforum($fid);
			$logdb = array(
				'type'      => 'recycle',
				'username1' => '',
				'username2' => $windid,
				'field1'    => $fid,
				'field2'    => '',
				'field3'    => '',
				'descrip'   => 'recycle_topic_restore',
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'affect'    => '',
				'forum'		=> $forum[$fid]['name'],
				'reason'	=> ''
			);
			writelog($logdb);
			refreshto("forumcp.php?action=edit&type=trecycle&fid=$fid",'operate_success');

		} elseif ($step == '3' && $forumset['recycle'] & 8) {

			PostCheck();
			$ids = array();
			$flag = false;
			$query = $db->query("SELECT * FROM pw_recycle WHERE fid=".pwEscape($fid)." AND pid='0' LIMIT 100");
			while ($rt = $db->fetch_array($query)) {
				$flag || $flag = true;
				$ids[] = $rt['tid'];
			}
			if ($flag) {
				recycle($ids);
				refreshto("forumcp.php?action=edit&type=trecycle&fid=$fid&step=3&verify=$verifyhash", 'delete_recycle');
			} else {
				$logdb = array(
					'type'      => 'recycle',
					'username1' => '',
					'username2' => $windid,
					'field1'    => $fid,
					'field2'    => '',
					'field3'    => '',
					'descrip'   => 'recycle_topic_empty',
					'timestamp' => $timestamp,
					'ip'        => $onlineip,
					'affect'    => '',
					'forum'		=> $forum[$fid]['name'],
					'reason'	=> ''
				);
				writelog($logdb);
				refreshto("forumcp.php?action=edit&type=trecycle&fid=$fid",'operate_success');
			}
		}
	} elseif ($type == 'precycle' && $db_recycle) {

		require_once(R_P.'require/updateforum.php');
		require_once(R_P.'require/writelog.php');
		InitGP(array('ptable'));
		InitGP(array('step','page'),'GP',2);
		$db_perpage = 5;

		if (empty($step)) {

			InitGP(array('username','starttime','endtime','t_type'));
			$sql = $url_a = '';
			if ($db_plist) {
				!is_numeric($ptable) && $ptable = $db_ptable;
				$p_table = "<option value=\"0\">post</option>";
				$p_list  = explode(',',$db_plist);
				foreach ($p_list as $key=>$val) {
					$p_table .= "<option value=\"$val\">post$val</option>";
				}
				$p_table  = str_replace("<option value=\"$ptable\">","<option value=\"$ptable\" selected>",$p_table);
				$url_a	 .= "ptable=$ptable&";
				$pw_posts = GetPtable($ptable);
			} else {
				$pw_posts = 'pw_posts';
			}
			$starttime && $starttime= PwStrtoTime($starttime);
			$endtime   && $endtime  = PwStrtoTime($endtime);
			if ($username) {
				$sql.=' AND author='.pwEscape($username);
				$url_a.="username=".rawurlencode($username)."&";
			}
			if ($starttime) {
				$sql.=' AND postdate>'.pwEscape($starttime);
				$url_a.="starttime=$starttime&";
			}
			if ($endtime) {
				$sql.=' AND postdate<'.pwEscape($endtime);
				$url_a.="endtime=$endtime&";
			}
			if ($t_type) {
				switch($t_type) {
					case 'digest':
						$sql.=" AND t.digest>'0'";
						break;
					case 'active':
						$sql.=" AND t.special='2'";
						break;
					case 'reward':
						$sql.=" AND t.special='3'";
						break;
					case 'sale':
						$sql.=" AND t.special='4'";
						break;
					default :
						$sql.="";
				}
				${'t_type_'.$t_type} = 'selected';
				$url_a.="t_type=$t_type&";
			}

			(!is_numeric($page) || $page<1) && $page = 1;
			$limit = "LIMIT ".($page-1)*$db_perpage.",$db_perpage";
			$rt    = $db->get_one("SELECT COUNT(*) AS sum FROM pw_recycle r LEFT JOIN $pw_posts p USING(pid) LEFT JOIN pw_threads t ON r.tid=t.tid WHERE r.fid=".pwEscape($fid)." AND r.pid>'0' AND p.fid='0' $sql");
			$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "forumcp.php?action=edit&type=precycle&fid=$fid&$url_a");

			$query = $db->query("SELECT r.*,p.author,p.authorid,p.content,t.subject FROM pw_recycle r LEFT JOIN $pw_posts p ON r.pid=p.pid LEFT JOIN pw_threads t ON r.tid=t.tid WHERE r.fid=".pwEscape($fid)." AND r.pid>'0' AND p.fid='0' $sql ORDER BY r.deltime DESC $limit");
			while ($rt = $db->fetch_array($query)) {
				$rt['deltime'] = get_date($rt['deltime']);
				$rt['subject'] = substrs($rt['subject'],50);
				$rt['content'] = str_replace("\n","<br>",$rt['content']);
				$rt['fname']   = $forum[$rt['fid']]['name'];
				$recycledb[]   = $rt;
			}
			require_once(PrintEot('forumcp'));footer();

		} elseif ($_POST['step'] == '1' && $forumset['recycle'] & 2) {

			PostCheck();
			InitGP(array('ids'),'P');
			count($ids) > 500 && Showmsg('forumcp_recycle_maxcount');
			$delids = array();
			foreach ($ids as $key => $value) {
				if (is_numeric($value)) {
					$delids[] = $value;
				}
			}
			$delids && $delids = pwImplode($delids);
			!$delids && Showmsg('forumcp_recycle_nodata');
			!is_numeric($ptable) && $ptable = $db_ptable;
			$pw_posts = GetPtable($ptable);

			$ids = $attachdb = array();
			$query = $db->query("SELECT r.*,p.aid FROM pw_recycle r LEFT JOIN $pw_posts p ON r.pid=p.pid WHERE r.pid IN($delids)");
			while ($read = $db->fetch_array($query)) {
				$read['fid'] != $fid && Showmsg('admin_forum_right');
				$read['aid'] && $attachdb[] = $read['aid'];
				$ids[] = $read['pid'];
			}
			if ($ids) {
				$ids = pwImplode($ids);
				$db->update("DELETE FROM $pw_posts WHERE pid IN ($ids)");
			}
			if ($attachdb) {
				$ftp = null;
				if ($db_ifftp) {
					require_once(R_P.'require/ftp.php');
					$ftp = new FTP($ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir);
				}
				delete_att($attachdb);
				if ($ftp) {
					$ftp->close(); unset($ftp);
				}
			}
			$logdb = array(
				'type'      => 'recycle',
				'username1' => '',
				'username2' => $windid,
				'field1'    => $fid,
				'field2'    => '',
				'field3'    => '',
				'descrip'   => 'recycle_reply_delete',
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'affect'    => '',
				'forum'		=> $forum[$fid]['name'],
				'reason'	=> ''
			);
			writelog($logdb);
			refreshto("forumcp.php?action=edit&type=precycle&fid=$fid&ptable=$ptable",'operate_success');

		} elseif ($_POST['step'] == '2' && $forumset['recycle'] & 4) {

			PostCheck();
			InitGP(array('ids'),'P');
			count($ids) > 500 && Showmsg('forumcp_recycle_maxcount');
			$reids = array();
			foreach ($ids as $key => $value) {
				if (is_numeric($value)) {
					$reids[] = $value;
				}
			}
			$reids && $reids = pwImplode($reids);
			!$reids && Showmsg('forumcp_recycle_nodata');
			!is_numeric($ptable) && $ptable = $db_ptable;
			$pw_posts = GetPtable($ptable);
			$repliesnum = $ids = $delids = $rids = array();
			$articlenum = 0;
			$query = $db->query("SELECT r.*,t.fid as tfid FROM pw_recycle r LEFT JOIN pw_threads t ON r.tid=t.tid WHERE r.pid IN($reids)");
			while ($read = $db->fetch_array($query)) {
				$read['fid'] != $fid && Showmsg('admin_forum_right');
				if ($read['tfid']) {
					$ids[] = $read['pid'];
					$articlenum++;
				} else {
					$delids[] = $read['pid'];
				}
				$rids[] = $read['pid'];
				$repliesnum[$read['tid']]++;
			}
			if ($ids) {
				$ids = pwImplode($ids);
				$db->update("UPDATE $pw_posts p LEFT JOIN pw_recycle r ON p.pid=r.pid SET p.tid=r.tid,p.fid=r.fid WHERE p.pid IN ($ids)");
				$db->update("UPDATE pw_attachs SET fid='$fid' WHERE pid IN($reids)");
			}
			if ($delids) {
				$delids = pwImplode($delids);
				$db->update("UPDATE $pw_posts p LEFT JOIN pw_recycle r ON p.pid=r.pid SET p.tid=r.tid,p.fid='0' WHERE p.pid IN ($delids)");
			}
			if ($rids) {
				$rids = pwImplode($rids);
				$db->update("DELETE FROM pw_recycle WHERE pid IN ($ids)");
			}

			foreach ($repliesnum as $key => $val) {
				$db->update("UPDATE pw_threads SET replies=replies+".pwEscape($val)." WHERE tid=".pwEscape($key));
			}
			$articlenum && $db->update("UPDATE pw_forumdata SET article=article+".pwEscape($articlenum)." WHERE fid=".pwEscape($fid));
			$logdb = array(
				'type'      => 'recycle',
				'username1' => '',
				'username2' => $windid,
				'field1'    => $fid,
				'field2'    => '',
				'field3'    => '',
				'descrip'   => 'recycle_reply_restore',
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'affect'    => '',
				'forum'		=> $forum[$fid]['name'],
				'reason'	=> ''
			);
			writelog($logdb);
			refreshto("forumcp.php?action=edit&type=precycle&fid=$fid&ptable=$ptable",'operate_success');

		} elseif($step == '3' && $forumset['recycle'] & 8){

			PostCheck();
			$ids = $attachdb = array();
			$flag = false;
			!is_numeric($ptable) && $ptable = $db_ptable;
			$pw_posts = GetPtable($ptable);
			$query = $db->query("SELECT r.*,p.aid FROM pw_recycle r LEFT JOIN $pw_posts p ON r.pid=p.pid WHERE r.fid=".pwEscape($fid)." AND r.pid>'0' LIMIT 100");
			while ($rt = $db->fetch_array($query)) {
				$flag || $flag = true;
				$ids[] = $rt['pid'];
				$attachdb[] = $rt['aid'];
			}
			if ($flag) {
				if ($ids) {
					$ids = pwImplode($ids);
					$db->update("DELETE FROM $pw_posts WHERE pid IN ($ids)");
					$db->update("DELETE FROM pw_recycle WHERE pid IN ($ids)");
				}
				if ($attachdb) {
					$ftp = null;
					if ($db_ifftp) {
						require_once(R_P.'require/ftp.php');
						$ftp = new FTP($ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir);
					}
					delete_att($attachdb);
					if ($ftp) {
						$ftp->close(); unset($ftp);
					}
				}
				refreshto("forumcp.php?action=edit&type=$type&fid=$fid&step=3&ptable=$ptable&verify=$verifyhash", 'delete_recycle');
			} else {
				$logdb = array(
					'type'      => 'recycle',
					'username1' => '',
					'username2' => $windid,
					'field1'    => $fid,
					'field2'    => '',
					'field3'    => '',
					'descrip'   => 'recycle_reply_empty',
					'timestamp' => $timestamp,
					'ip'        => $onlineip,
					'affect'    => '',
					'forum'		=> $forum[$fid]['name'],
					'reason'	=> ''
				);
				writelog($logdb);
				refreshto("forumcp.php?action=edit&type=precycle&fid=$fid",'operate_success');
			}
		}
	}
} elseif ($action == 'del') {

	PostCheck();
	InitGP(array('selid','type'));
	$selids = array();
	foreach ($selid as $key => $value) {
		is_numeric($value) && $selids[] = $value;
	}
	if ($selids) {
		$selids = pwImplode($selids);
	} else {
		Showmsg('id_error');
	}
	if ($type == 'report') {
		$db->update("DELETE FROM pw_report WHERE id IN ($selids)");
		refreshto("forumcp.php?action=edit&type=report&fid=$fid",'operate_success');
	}
}

function updatecache_fd() {
	global $db;
	$db->update("UPDATE pw_forums SET childid='0',fupadmin=''");
	$query = $db->query("SELECT fid,forumadmin FROM pw_forums WHERE type='category' ORDER BY vieworder");
	while ($cate = $db->fetch_array($query)) {
		Add_S($cate);
		$query2 = $db->query("SELECT fid,forumadmin FROM pw_forums WHERE type='forum' AND fup=".pwEscape($cate['fid']));
		if ($db->num_rows($query2)) {
			$havechild[] = $cate['fid'];
			while ($forum = $db->fetch_array($query2)) {
				Add_S($forum);
				$fupadmin = trim($cate['forumadmin']);
				if ($fupadmin) {
					$db->update("UPDATE pw_forums SET fupadmin=".pwEscape($fupadmin)." WHERE fid=".pwEscape($forum['fid']));
				}
				if (trim($forum['forumadmin'])) {
					$fupadmin .= $fupadmin ? substr($forum['forumadmin'],1) : $forum['forumadmin']; //is
				}
				$query3 = $db->query("SELECT fid,forumadmin FROM pw_forums WHERE type='sub' AND fup=".pwEscape($forum['fid']));
				if ($db->num_rows($query3)) {
					$havechild[] = $forum['fid'];
					while ($sub1 = $db->fetch_array($query3)) {
						Add_S($sub1);
						$fupadmin1 = $fupadmin;
						if ($fupadmin1) {
							$db->update("UPDATE pw_forums SET fupadmin=".pwEscape($fupadmin1)." WHERE fid=".pwEscape($sub1['fid']));
						}
						if (trim($sub1['forumadmin'])) {
							$fupadmin1 .= $fupadmin1 ? substr($sub1['forumadmin'],1) : $sub1['forumadmin'];
						}
						$query4 = $db->query("SELECT fid,forumadmin FROM pw_forums WHERE type='sub' AND fup=".pwEscape($sub1['fid']));
						if ($db->num_rows($query4)) {
							$havechild[] = $sub1['fid'];
							while ($sub2 = $db->fetch_array($query4)) {
								Add_S($sub2);
								$fupadmin2 = $fupadmin1;
								if ($fupadmin2) {
									$db->update("UPDATE pw_forums SET fupadmin=".pwEscape($fupadmin2)." WHERE fid=".pwEscape($sub2['fid']));
								}
							}
						}
					}
				}
			}
		}
	}
	if ($havechild) {
		$havechilds = pwImplode($havechild);
		$db->update("UPDATE pw_forums SET childid='1' WHERE fid IN($havechilds)");
	}
}

function recycle($ids){
	global $db,$fid,$attachdir,$ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir,$db_ifftp;
	$delids = array();
	foreach ($ids as $key => $value) {
		if (is_numeric($value)) {
			$delids[] = $value;
		}
	}
	if ($delids) {
		$delids = pwImplode($delids);
	} else {
		Showmsg('forumcp_recycle_nodata');
	}
	$ftp = null;
	if ($db_ifftp) {
		require_once(R_P.'require/ftp.php');
		$ftp = new FTP($ftp_server,$ftp_port,$ftp_user,$ftp_pass,$ftp_dir);
	}
	$query  = $db->query("SELECT r.*,t.special,t.ifshield,t.ifupload,t.ptable,t.replies,t.fid AS ckfid FROM pw_recycle r LEFT JOIN pw_threads t ON r.tid=t.tid WHERE r.tid IN ($delids) AND r.pid='0' AND r.fid=".pwEscape($fid));
	$taid_a = $ttable_a = $ptable_a = array();
	$delids = $pollids = $actids = $delaids = $rewids = $ids = array();
	while (@extract($db->fetch_array($query))) {
		$ids[] = $tid;
		($ifshield != '2' || $replies == '0' || $ckfid == '0')  && $delids[] = $tid;
		$special == 1 && $pollids[] = $tid;
		$special == 2 && $actids[] = $tid;
		$special == 3 && $rewids[] = $tid;
		if ($ifshield != '2' || $replies == '0' || $ckfid == '0') {
			$ptable_a[$ptable] = 1;
			$ttable_a[GetTtable($tid)][] = $tid;
		}
		if ($ifupload) {
			$taid_a[GetTtable($tid)][] = $tid;
			if ($ifshield != '2' || $replies == '0' || $ckfid == '0') {
				$pw_posts = GetPtable($ptable);
				$query2 = $db->query("SELECT aid FROM $pw_posts WHERE tid=".pwEscape($tid)." AND aid!=''");
				while (@extract($db->fetch_array($query2))) {
					if (!$aid) continue;
					$attachs = unserialize(stripslashes($aid));
					foreach ($attachs as $key => $value) {
						is_numeric($key) && $delaids[] = $key;
						if ($ftp && !file_exists($attachdir."/".$value['attachurl'])) {
							$ftp->delete($value['attachurl']);
							$value['ifthumb'] && $ftp->delete("thumb/$value[attachurl]");
						} else {
							P_unlink("$attachdir/$value[attachurl]");
							$value['ifthumb'] && P_unlink("$attachdir/thumb/$value[attachurl]");
						}
					}
				}
			}
		}
	}
	foreach ($taid_a as $pw_tmsgs => $value) {
		$value = pwImplode($value);
		$query = $db->query("SELECT aid FROM $pw_tmsgs WHERE tid IN($value) AND aid!=''");
		while (@extract($db->fetch_array($query))) {
			if (!$aid) continue;
			$attachs = unserialize(stripslashes($aid));
			foreach ($attachs as $key => $value) {
				is_numeric($key) && $delaids[] = $key;
				if ($ftp && !file_exists($attachdir."/".$value['attachurl'])) {
					$ftp->delete($value['attachurl']);
					$value['ifthumb'] && $ftp->delete("thumb/$value[attachurl]");
				} else {
					P_unlink("$attachdir/$value[attachurl]");
					$value['ifthumb'] && P_unlink("$attachdir/thumb/$value[attachurl]");
				}
			}
		}
	}
	if ($pollids) {
		$pollids = pwImplode($pollids);
		$db->update("DELETE FROM pw_polls WHERE tid IN($pollids)");
	}
	if ($actids) {
		$actids = pwImplode($actids);
		$db->update("DELETE FROM pw_activity WHERE tid IN($actids)");
		$db->update("DELETE FROM pw_actmember WHERE actid IN($actids)");
	}
	if ($rewids) {
		$rewids = pwImplode($rewids);
		$db->update("DELETE FROM pw_reward WHERE tid IN($rewids)");
	}
	if ($delaids) {
		$delaids  = pwImplode($delaids);
		$db->update("DELETE FROM pw_attachs WHERE aid IN($delaids)");
	}
	$delids  = pwImplode($delids);
	if ($delids) {
		$db->update("DELETE FROM pw_threads	WHERE tid IN($delids)");
	}
	foreach ($ttable_a as $pw_tmsgs => $val) {
		$val = pwImplode($val);
		$db->update("DELETE FROM $pw_tmsgs WHERE tid IN($val)");
	}
	foreach ($ptable_a as $key => $val) {
		$pw_posts = GetPtable($key);
		$db->update("DELETE FROM $pw_posts WHERE tid IN($delids)");
	}
	delete_tag($delids);
	if ($ids) {
		$ids = pwImplode($ids);
		$db->update("DELETE FROM pw_recycle WHERE tid IN ($ids)");
	}
	if ($ftp) {
		$ftp->close(); unset($ftp);
	}
}
?>