<?php
if (isset($_GET['ajax'])) {
	define('AJAX','1');
}
require_once('global.php');
$groupid == 'guest' && Showmsg('not_login');

InitGP(array('action'),'GP',0);
InitGP(array('atc_content'),'P');

if (!in_array($action,array('banuser','delatc','shield','remind','commend','check','inspect','pingcp'))) {
	Showmsg('undefined_action');
}

require_once(R_P.'require/forum.php');
require_once(R_P.'require/msg.php');
require_once(R_P.'require/writelog.php');
require_once(R_P.'require/updateforum.php');
include_once(D_P.'data/bbscache/forum_cache.php');

$foruminfo = $db->get_one("SELECT * FROM pw_forums f LEFT JOIN pw_forumsextra fe USING(fid) WHERE f.fid=".pwEscape($fid)." AND f.type<>'category'");
!$foruminfo && Showmsg('data_error');
wind_forumcheck($foruminfo);

$isGM = CkInArray($windid,$manager);
$isBM = admincheck($foruminfo['forumadmin'],$foruminfo['fupadmin'],$windid);
if (!$isGM) {
	switch ($action) {
		case 'delatc' :
			$admincheck = pwRights($isBM,'modother');
			break;
		case 'check' :
			$admincheck = pwRights($isBM,'tpccheck');
			break;
		case 'commend' :
			$admincheck = $isBM;
			break;
		case 'banuser' :
			$admincheck = pwRights($isBM,'banuser');
			break;
		case 'shield' :
			$admincheck = pwRights($isBM,'shield');
			break;
		case 'remind' :
			$admincheck = pwRights($isBM,'remind');
			break;
		case 'inspect' :
			$admincheck = pwRights($isBM,'inspect');
			break;
		case 'pingcp' :
			$admincheck = pwRights($isBM,'pingcp');
			break;
		default :
			$admincheck = false;
	}
	!$admincheck && Showmsg('mawhole_right');
}

$secondurl = "thread.php?fid=$fid";
$template = 'ajax_masingle';

if (empty($_POST['step']) && !defined('AJAX')) {
	require_once(R_P.'require/header.php');
	$template = 'masingle';
} elseif ($_POST['step'] == '3') {
	if ($db_enterreason && !$atc_content) {
		Showmsg('enterreason');
	}
}

if ($action == "banuser") {

	InitGP(array('pid'));
	InitGP(array('page'),'GP',2);
	(!$tid || !$fid || !$pid) && Showmsg('masingle_data_error');
	$sqlsel = $sqltab = $sqladd = '';
	if (is_numeric($pid)) {
		$pw_posts = GetPtable('N',$tid);
		$sqlsel = "t.anonymous,t.userip,";
		$sqltab = "$pw_posts t LEFT JOIN pw_members m ON t.authorid=m.uid";
		$sqladd = ' AND t.pid='.pwEscape($pid);
	} else {
		$pw_tmsgs = GetTtable($tid);
		$sqlsel = "t.anonymous,tm.userip,";
		$sqltab = "pw_threads t LEFT JOIN $pw_tmsgs tm ON t.tid=tm.tid LEFT JOIN pw_members m ON t.authorid=m.uid";
	}
	$userdb = $db->get_one("SELECT $sqlsel m.uid,m.username,m.groupid,m.userstatus FROM $sqltab WHERE t.tid=".pwEscape($tid)." AND t.fid=".pwEscape($fid).$sqladd);
	!$userdb && Showmsg('undefined_action');
	$username = $userdb['anonymous'] ? $db_anonymousname : $userdb['username'];

	if (isban($userdb,$fid)) {
		Showmsg('member_havebanned');
	} elseif ($userdb['groupid'] != '-1') {
		Showmsg('masigle_ban_fail');
	}
	$pwBanMax = pwRights($isBM,'banmax');
	if (empty($_POST['step'])) {
		require_once PrintEot($template);footer();

	} else {

		InitGP(array('range','ifmsg','banip'),'P');
		InitGP(array('limit','type'),'P',2);
		$range = $range ? '0' : intval($fid);

		if ($limit > $pwBanMax) {
			Showmsg('masigle_ban_limit');
		}
		if (!$isGM && $type == '2' && !pwRights($isBM,'bantype')) {
			Showmsg('masigle_ban_right');
		}
		if (!$isGM && ($range == '0' || $banip) && pwRights($isBM,'banuser') != '2') {
			Showmsg('masigle_ban_range');
		}
		$pwSQL = pwSqlSingle(array(
			'uid'		=> $userdb['uid'],
			'fid'		=> $range,
			'type'		=> $type,
			'startdate'	=> $timestamp,
			'days'		=> $limit,
			'admin'		=> $windid,
			'reason'	=> $atc_content
		));
		$db->update("REPLACE INTO pw_banuser SET $pwSQL");
		if ($range) {
			$db->update('UPDATE pw_members SET userstatus=userstatus|1 WHERE uid='.pwEscape($userdb['uid']));
		} else {
			$db->update("UPDATE pw_members SET groupid='6' WHERE uid=".pwEscape($userdb['uid']));
		}
		if ($ifmsg) {
			$message = array(
				'toUser'	=> $userdb['username'],
				'subject'	=> 'banuser_title',
				'content'	=> 'banuser_content_'.$type,
				'other'		=> array(
					'reason'	=> stripslashes($atc_content),
					'manager'	=> $windid,
					'limit'		=> $limit
				)
			);
			pwSendMsg($message);
		}
		if ($banip && $userdb['userip']) {
			require_once(R_P.'admin/cache.php');
			$rs = $db->get_one("SELECT db_name,db_value FROM pw_config WHERE db_name='db_ipban'");
			$rs['db_value'] .= ($rs['db_value'] ? ',' : '').$userdb['userip'];
			if ($rs) {
				$db->update("UPDATE pw_config SET db_value=".pwEscape($rs['db_value'])." WHERE db_name='db_ipban'");
			} else {
				$db->update("INSERT INTO pw_config(db_name,db_value) VALUES ('db_ipban',".pwEscape($rs['db_value']).")");
			}
			updatecache_c();
		}
		if ($foruminfo['allowhtm']) {
			require_once(R_P.'require/template.php');
		}
		if (defined('AJAX')) {
			Showmsg('banuser_success');
		} else {
			refreshto("read.php?tid=$tid&page=$page",'operate_success');
		}
	}
} elseif ($action == "shield") {

	InitGP(array('pid','page'));
	(!$tid || !$pid) && Showmsg('masingle_data_error');
	$sqlsel = $sqltab = $sqladd = '';
	if (is_numeric($pid)) {
		$pw_posts = GetPtable('N',$tid);
		$sqlsel = "t.fid,t.subject,t.content,t.postdate,t.ifshield,t.anonymous,";
		$sqltab = "$pw_posts t LEFT JOIN pw_members m ON t.authorid=m.uid";
		$sqladd = " AND t.pid='$pid'";
	} else {
		$sqlsel = "t.fid,t.subject,t.postdate,t.ifshield,t.anonymous,";
		$sqltab = "pw_threads t LEFT JOIN pw_members m ON t.authorid=m.uid";
	}
	$readdb = $db->get_one("SELECT $sqlsel m.uid,m.username,m.groupid FROM $sqltab WHERE t.tid=".pwEscape($tid).$sqladd);
	if (!$readdb || $readdb['fid'] <> $fid) {
		Showmsg('illegal_tid');
	}
	$readdb['ifshield'] == '2' && Showmsg('illegal_data');

	if ($groupid != 3 && $groupid != 4) {
		if ($readdb['groupid'] == 3 || $readdb['groupid'] == 4) {
			Showmsg('modify_admin');
		}
	}
	$readdb['subject'] = substrs($readdb['subject'],35);
	!$readb['subject'] && is_numeric($pid) && $readdb['subject'] = substrs($readdb['content'],35);

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

		$readdb['ifshield'] ? $check_N = 'checked' : $check_Y = 'checked';
		$readdb['postdate'] = get_date($readdb['postdate']);
		$reason_sel='';
		$reason_a=explode("\n",$db_adminreason);
		foreach($reason_a as $k=>$v){
			if($v=trim($v)){
				$reason_sel .= "<option value=\"$v\">$v</option>";
			} else{
				$reason_sel .= "<option value=\"\">-------</option>";
			}
		}
		require_once PrintEot($template);footer();

	} else {

		PostCheck();
		if ($_POST['step'] == 3) {
			$readdb['ifshield'] == 1 && Showmsg('read_shield');
			$ifshield = 1;
		} else {
			$readdb['ifshield'] == 0 && Showmsg('read_unshield');
			$ifshield = 0;
		}
		if (is_numeric($pid)) {
			$db->update("UPDATE $pw_posts SET ifshield=".pwEscape($ifshield).' WHERE pid='.pwEscape($pid).' AND tid='.pwEscape($tid));
		} else {
			$db->update('UPDATE pw_threads SET ifshield='.pwEscape($ifshield).' WHERE tid='.pwEscape($tid));
			if ($db_ifpwcache ^ 1) {
				$db->update("DELETE FROM pw_elements WHERE type !='usersort' AND id=".pwEscape($tid));
			}
		}
		if ($_POST['ifmsg']) {
			$msg = array(
				'toUser'	=> $readdb['username'],
				'subject'	=> 'shield_title_'.$ifshield,
				'content'	=> 'shield_content_'.$ifshield,
				'other'		=> array(
					'manager'	=> $windid,
					'fid'		=> $fid,
					'tid'		=> $tid,
					'subject'	=> $readdb['subject'],
					'postdate'	=> get_date($readdb['postdate']),
					'forum'		=> strip_tags($forum[$fid]['name']),
					'admindate'	=> get_date($timestamp),
					'reason'	=> stripslashes($atc_content)
				)
			);
			pwSendMsg($msg);
		}
		if ($_POST['step'] == 3) {
			$log = array(
				'type'      => 'shield',
				'username1' => $readdb['username'],
				'username2' => $windid,
				'field1'    => $fid,
				'field2'    => '',
				'field3'    => '',
				'descrip'   => 'shield_descrip',
				'timestamp' => $timestamp,
				'ip'        => $onlineip,
				'tid'		=> $tid,
				'forum'		=> $forum[$fid]['name'],
				'subject'	=> substrs($readdb['subject'],28),
				'reason'	=> stripslashes($atc_content)
			);
			writelog($log);
		}
		if ($foruminfo['allowhtm'] && $page==1) {
			require_once(R_P.'require/template.php');
		}
		refreshto("read.php?tid=$tid&page=$page",'operate_success');
	}
} elseif ($action == 'remind') {

	InitGP(array('pid','page'));
	(!$tid || !$pid) && Showmsg('masingle_data_error');
	$sqlsel = $sqltab = $sqladd = '';
	if (is_numeric($pid)) {
		$pw_posts = GetPtable('N',$tid);
		$sqlsel = "t.fid,t.subject,t.content,t.postdate,t.remindinfo,t.anonymous,";
		$sqltab = "$pw_posts t LEFT JOIN pw_members m ON t.authorid=m.uid";
		$sqladd = " AND t.pid='$pid'";
	} else {
		$pw_tmsgs = GetTtable($tid);
		$sqlsel = "t.fid,t.subject,t.postdate,t.anonymous,tm.remindinfo,";
		$sqltab = "pw_threads t LEFT JOIN $pw_tmsgs tm ON t.tid=tm.tid LEFT JOIN pw_members m ON t.authorid=m.uid";
	}
	$readdb = $db->get_one("SELECT $sqlsel m.uid,m.username,m.groupid FROM $sqltab WHERE t.tid=".pwEscape($tid).' AND t.fid='.pwEscape($fid).$sqladd);
	if (!$readdb || $readdb['fid'] <> $fid) {
		Showmsg('illegal_tid');
	}

	if ($groupid != 3 && $groupid != 4) {
		if ($readdb['groupid'] == 3 || $readdb['groupid'] == 4) {
			Showmsg('modify_admin');
		}
	}
	$readdb['subject'] = substrs($readdb['subject'],35);
	!$readb['subject'] && is_numeric($pid) && $readdb['subject']=substrs($readdb['content'],35);

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

		$readdb['remindinfo'] ? $check_N = 'checked' : $check_Y = 'checked';
		$readdb['postdate'] = get_date($readdb['postdate']);
		$reason_sel='';
		$reason_a=explode("\n",$db_adminreason);
		list($remindinfo)=explode("\t",$readdb['remindinfo']);
		foreach($reason_a as $k=>$v){
			if($v=trim($v)){
				$reason_sel .= "<option value=\"$v\">$v</option>";
			} else{
				$reason_sel .= "<option value=\"\">-------</option>";
			}
		}
		$remindinfo = str_replace('&nbsp;',' ',$remindinfo);
		require_once PrintEot($template);footer();

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

		PostCheck();
		!$atc_content && Showmsg('remind_data_empty');

		$remindinfo = $atc_content."\t".addslashes($windid)."\t".$timestamp;
		if (strlen($remindinfo)>150) Showmsg('remind_length');
		if (is_numeric($pid)) {
			$db->update("UPDATE $pw_posts SET remindinfo=".pwEscape($remindinfo).' WHERE pid='.pwEscape($pid).' AND tid='.pwEscape($tid));
		} else {
			$db->update("UPDATE $pw_tmsgs SET remindinfo=".pwEscape($remindinfo).' WHERE tid='.pwEscape($tid));
		}
		if ($_POST['ifmsg']) {
			$msg = array(
				'toUser'	=> $readdb['username'],
				'subject'	=> 'remind_title',
				'content'	=> 'remind_content',
				'other'		=> array(
					'manager'	=> $windid,
					'fid'		=> $fid,
					'tid'		=> $tid,
					'subject'	=> $readdb['subject'],
					'postdate'	=> get_date($readdb['postdate']),
					'forum'		=> strip_tags($forum[$fid]['name']),
					'admindate'	=> get_date($timestamp),
					'reason'	=> stripslashes($atc_content)
				)
			);
			pwSendMsg($msg);
		}
		$log = array(
			'type'      => 'remind',
			'username1' => $readdb['username'],
			'username2' => $windid,
			'field1'    => $fid,
			'field2'    => '',
			'field3'    => '',
			'descrip'   => 'remind_descrip',
			'timestamp' => $timestamp,
			'ip'        => $onlineip,
			'tid'		=> $tid,
			'forum'		=> $forum[$fid]['name'],
			'subject'	=> substrs($readdb['subject'],28),
			'reason'	=> stripslashes($atc_content)
		);
		writelog($log);
		if (defined('AJAX')) {
			echo "success\t".str_replace(array("\n","\t"),array('<br />',''),$atc_content)."\t$windid";ajax_footer();
		} else {
			refreshto("read.php?tid=$tid&page=$page",'operate_success');
		}
	} else {

		PostCheck();
		if (is_numeric($pid)) {
			$db->update("UPDATE $pw_posts SET remindinfo='' WHERE pid=".pwEscape($pid).' AND tid='.pwEscape($tid));
		} else {
			$db->update("UPDATE $pw_tmsgs SET remindinfo='' WHERE tid=".pwEscape($tid));
		}
		if (defined('AJAX')) {
			echo "cancle\t";ajax_footer();
		} else {
			refreshto("read.php?tid=$tid&page=$page",'operate_success');
		}
	}
} elseif ($action == "delatc") {

	InitGP(array('selid'));
	empty($selid) && Showmsg('mawhole_nodata');
	$pw_tmsgs = GetTtable($tid);
	$tpcdb = $db->get_one("SELECT t.tid,t.fid,t.author,t.authorid,t.postdate,t.subject,t.topped,t.anonymous,t.ifshield,t.ptable,tm.aid FROM pw_threads t LEFT JOIN $pw_tmsgs tm ON tm.tid=t.tid WHERE t.tid='$tid'");
	if (!$tpcdb || $tpcdb['fid'] != $fid) {
		Showmsg('undefined_action');
	}
	$pw_posts = GetPtable($tpcdb['ptable']);
	$deltpc = '';
	$pids = array();
	foreach ($selid as $k => $v) {
		if ($v == 'tpc') {
			$deltpc = '1';
		} elseif (is_numeric($v)) {
			$pids[] = $v;
		}
	}
	$threaddb = array();
	if ($deltpc) {
		$tpcdb['ifshield'] == '2' && Showmsg('illegal_data');
		if ($groupid != 3 && $groupid != 4) {
			$authordb = $db->get_one('SELECT groupid FROM pw_members WHERE uid='.pwEscape($tpcdb['authorid']));
			if ($authordb['groupid'] == 3 || $authordb['groupid'] == 4) {
				Showmsg('modify_admin');
			}
		}
		$tpcdb['pid'] = 'tpc';
		$tpcdb['postdate'] = get_date($tpcdb['postdate']);
		$threaddb[] = $tpcdb;
	}
	if ($pids) {
		$pids  = pwImplode($pids);
		$query = $db->query("SELECT pid,fid,tid,aid,author,authorid,postdate,subject,content,anonymous FROM $pw_posts WHERE tid='$tid' AND fid='$fid' AND pid IN($pids)");
		while ($rt = $db->fetch_array($query)) {
			if ($groupid != 3 && $groupid != 4) {
				$authordb = $db->get_one('SELECT groupid FROM pw_members WHERE uid='.pwEscape($rt['authorid']));
				if ($authordb['groupid'] == 3 || $authordb['groupid'] == 4) {
					Showmsg('modify_admin');
				}
			}
			if (!$rt['subject']) {
				$rt['subject'] = substrs($rt['content'],35);
			}
			$rt['postdate'] = get_date($rt['postdate']);
			$threaddb[] = $rt;
		}
	}
	if (empty($_POST['step'])) {

		$reason_sel = '';
		$reason_a	= explode("\n",$db_adminreason);
		foreach ($reason_a as $k=>$v) {
			if ($v = trim($v)) {
				$reason_sel .= "<option value=\"$v\">$v</option>";
			} else {
				$reason_sel .= "<option value=\"\">-------</option>";
			}
		}
		$count = count($threaddb);
		require_once PrintEot($template);footer();

	} else {

		PostCheck();
		$attachdb  = array();
		require_once(R_P.'require/credit.php');
		$creditset = $credit->creditset($foruminfo['creditset'],$db_creditset);

		foreach ($threaddb as $key => $val) {
			if ($val['pid'] == 'tpc') {
				$db->update("UPDATE pw_threads SET ifshield='2' WHERE tid='$tid'");
				if ($db_recycle) {
					$pwSQL = pwSqlSingle(array(
						'pid'		=> 0,
						'tid'		=> $tid,
						'fid'		=> $fid,
						'deltime'	=> $timestamp,
						'admin'		=> $windid
					));
					$db->update("REPLACE INTO pw_recycle SET $pwSQL");
				} else {
					delete_tag($tid);
				}
				$val['aid'] && $attachdb[] = $val['aid'];
				if ($_POST['ifdel']) {
					$credit->addLog('topic_Delete',$creditset['Delete'],array(
						'uid'		=> $val['authorid'],
						'username'	=> $val['author'],
						'ip'		=> $onlineip,
						'fname'		=> strip_tags($forum[$fid]['name']),
						'operator'	=> $windid
					));
					$credit->sets($val['authorid'],$creditset['Delete'],false);
					$msg_delrvrc  = abs($creditset['Delete']['rvrc']);
					$msg_delmoney = abs($creditset['Delete']['money']);
				} else {
					$msg_delrvrc  = $msg_delmoney = 0;
				}
				$credit->setMdata($val['authorid'],'postnum',-1);

				if ($_POST['ifmsg']) {
					$msg = array(
						'toUser'	=> $val['author'],
						'subject'	=> 'deltpc_title',
						'content'	=> 'deltpc_content',
						'other'		=> array(
							'manager'	=> $windid,
							'fid'		=> $fid,
							'tid'		=> $tid,
							'subject'	=> $val['subject'],
							'postdate'	=> $val['postdate'],
							'forum'		=> strip_tags($forum[$fid]['name']),
							'affect'	=> "{$db_rvrcname}：-{$msg_delrvrc}，{$db_moneyname}：-{$msg_delmoney}",
							'admindate'	=> get_date($timestamp),
							'reason'	=> stripslashes($atc_content)
						)
					);
					pwSendMsg($msg);
				}
				$log = array(
					'type'      => 'delete',
					'username1' => $val['author'],
					'username2' => $windid,
					'field1'    => $fid,
					'field2'    => '',
					'field3'    => '',
					'descrip'   => 'deltpc_descrip',
					'timestamp' => $timestamp,
					'ip'        => $onlineip,
					'tid'		=> $tid,
					'forum'		=> $forum[$fid]['name'],
					'subject'	=> substrs($val['subject'],28),
					'affect'	=> "{$db_rvrcname}：-{$msg_delrvrc}，{$db_moneyname}：-{$msg_delmoney}",
					'reason'	=> $atc_content
				);
				writelog($log);
			} else {
				!$val['subject'] && $val['subject'] = substrs($val['content'],28);
				if ($db_recycle) {
					$db->update("UPDATE $pw_posts SET tid='0',fid='0' WHERE pid=".pwEscape($val['pid']));
					$pwSQL = pwSqlSingle(array(
						'pid'		=> $val['pid'],
						'tid'		=> $tid,
						'fid'		=> $fid,
						'deltime'	=> $timestamp,
						'admin'		=> $windid
					));
					$db->update("REPLACE INTO pw_recycle SET $pwSQL");
				} else {
					$db->update("DELETE FROM $pw_posts WHERE pid=".pwEscape($val['pid']));
				}
				$val['aid'] && $attachdb[] = $val['aid'];
				if ($_POST['ifdel']) {
					$credit->addLog('topic_Deleterp',$creditset['Deleterp'],array(
						'uid'		=> $val['authorid'],
						'username'	=> $val['author'],
						'ip'		=> $onlineip,
						'fname'		=> strip_tags($forum[$fid]['name']),
						'operator'	=> $windid
					));
					$credit->sets($val['authorid'],$creditset['Deleterp'],false);
					$msg_delrvrc  = abs($creditset['Deleterp']['rvrc']);
					$msg_delmoney = abs($creditset['Deleterp']['money']);
				} else {
					$msg_delrvrc  = $msg_delmoney = 0;
				}
				$credit->setMdata($val['authorid'],'postnum',-1);

				if ($_POST['ifmsg']) {
					$msg = array(
						'toUser'	=> $val['author'],
						'subject'	=> 'delrp_title',
						'content'	=> 'delrp_content',
						'other'		=> array(
							'manager'	=> $windid,
							'fid'		=> $fid,
							'tid'		=> $tid,
							'subject'	=> $val['subject'],
							'postdate'	=> $val['postdate'],
							'forum'		=> strip_tags($forum[$fid]['name']),
							'affect'	=> "{$db_rvrcname}：-{$msg_delrvrc}，{$db_moneyname}：-{$msg_delmoney}",
							'admindate'	=> get_date($timestamp),
							'reason'	=> stripslashes($atc_content)
						)
					);
					pwSendMsg($msg);
				}
				$log = array(
					'type'      => 'delete',
					'username1' => $val['author'],
					'username2' => $windid,
					'field1'    => $fid,
					'field2'    => '',
					'field3'    => '',
					'descrip'   => 'delrp_descrip',
					'timestamp' => $timestamp,
					'ip'        => $onlineip,
					'tid'		=> $tid,
					'forum'		=> $forum[$fid]['name'],
					'subject'	=> $val['subject'],
					'affect'	=> "{$db_rvrcname}：-{$msg_delrprvrc}，{$db_moneyname}：-{$msg_delmoney}",
					'reason'	=> stripslashes($atc_content)
				);
				writelog($log);
			}
		}
		$credit->runsql();

		if ($attachdb) {
			delete_att($attachdb,!$db_recycle);
			pwFtpClose($ftp);
		}
		P_unlink(R_P."$db_htmdir/$fid/".date('ym',$tpcdb['postdate'])."/$tid.html");

		$rt = $db->get_one("SELECT count(*) AS replies FROM $pw_posts WHERE tid='$tid' AND ifcheck='1'");
		$replies = $rt['replies'];
		if ($db_guestread) {
			require_once(R_P.'require/guestfunc.php');
			clearguestcache($tid,$replies);
		}
		$refreshto = "read.php?tid=$tid";

		if (!$replies) {
			$tpcdb['anonymous'] && $tpcdb['author'] = $db_anonymousname;
			if ($deltpc) {
				if ($db_recycle) {
					$db->update("UPDATE pw_threads SET fid='0' WHERE tid='$tid'");
				} else {
					$db->update("DELETE FROM pw_threads WHERE tid='$tid'");
					$db->update("DELETE FROM $pw_tmsgs WHERE tid='$tid'");
				}
				$refreshto = "thread.php?fid=$fid";
			} else {
				$db->update("UPDATE pw_threads SET replies='0',lastpost=postdate,lastposter=".pwEscape($tpcdb['author'])." WHERE tid='$tid'");
			}
		} else {
			$pt = $db->get_one("SELECT postdate,author,anonymous FROM $pw_posts WHERE tid='$tid' ORDER BY postdate DESC LIMIT 1");
			$pt['anonymous'] && $pt['author'] = $db_anonymousname;
			$pwSQL = pwSqlSingle(array(
				'replies'	=> $replies,
				'lastpost'	=> $pt['postdate'],
				'lastposter'=> $pt['author']
			),false);
			$db->update("UPDATE pw_threads SET $pwSQL WHERE tid='$tid'");
		}
		updateforum($fid);

		if ($tpcdb['topped'] && $deltpc) {
			updatetop();
		}
		if ($foruminfo['allowhtm']) {
			require_once(R_P.'require/template.php');
		}
		if (defined('AJAX')) {
			Showmsg(($deltpc && !$replies) ? "ajax_operate_success" : "ajaxma_success");
		} else {
			refreshto($refreshto,'operate_success');
		}
	}
} elseif ($action == 'commend') {

	PostCheck();
	$forumset = unserialize($foruminfo['forumset']);
	if (!$forumset['commend']) {
		Showmsg('commend_close');
	}
	if (strpos(",$forumset[commendlist],",",$tid,")!==false) {
		Showmsg('commend_exists');
	}
	$count = count(explode(',',$forumset['commendlist']));

	if ($forumset['commendnum'] && $count >= $forumset['commendnum']) {
		Showmsg('commendnum_limit');
	}
	$forumset['commendlist'] .= ($forumset['commendlist'] ? ',' : '').$tid;

	updatecommend($fid,$forumset);
	Showmsg('operate_success');

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

	PostCheck();
	$db->update("UPDATE pw_threads SET ifcheck='1' WHERE tid=".pwEscape($tid)."AND fid=".pwEscape($fid)."AND ifcheck='0'");
	if ($db->affected_rows() > 0) {
		$rt = $db->get_one("SELECT tid,author,postdate,subject FROM pw_threads WHERE fid=".pwEscape($fid)."AND ifcheck='1' AND topped='0' ORDER BY lastpost DESC LIMIT 1");
		$lastpost = $rt['subject']."\t".$rt['author']."\t".$rt['postdate']."\t"."read.php?tid=$rt[tid]&page=e#a";
		$db->update("UPDATE pw_forumdata SET topic=topic+'1',article=article+'1',tpost=tpost+'1',lastpost=".pwEscape($lastpost,false)." WHERE fid='$fid'");
	}
	Showmsg('operate_success');

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

	PostCheck();
	$forumset = unserialize($foruminfo['forumset']);
	if (empty($forumset['inspect'])) {
		Showmsg('undefined_action');
	}
	InitGP(array('pid'));

	$rt = $db->get_one('SELECT inspect FROM pw_threads WHERE tid='.pwEscape($tid) . " AND fid=".pwEscape($fid));
	empty($rt) && Showmsg('undefined_action');
	list($lou) = explode("\t",$rt['inspect']);
	$pid > $lou && $lou = $pid;
	$inspect = $lou."\t".addslashes($windid);
	$db->update('UPDATE pw_threads SET inspect='.pwEscape($inspect).' WHERE tid='.pwEscape($tid));
	delfcache($fid,$db_fcachenum);
	Showmsg('operate_success');
} elseif ($action == 'pingcp') {

	InitGP(array('pid','page'));
	if (empty($_POST['step'])) {

		$sqlwhere = ' WHERE 1';
		if (!is_numeric($pid)) {
			$sqlwhere .= " AND pid=0 AND tid=".pwEscape($tid);
		} else {
			$sqlwhere .= " AND pid=".pwEscape($pid);
		}
		$query = $db->query("SELECT id,name,point,pinger,record,pingdate FROM pw_pinglog $sqlwhere ORDER BY pingdate DESC");
		while ($rt = $db->fetch_array($query)) {
			$rt['point'] = $rt['point'] > 0 ? '+'.$rt['point'] : $rt['point'];
			$rt['pingdate'] = get_date($rt['pingdate']);
			if (strpos($username,"'".$rt['pinger']."'") === false) {
				$username .= $username ? ",'".$rt['pinger']."'" : "'".$rt['pinger']."'" ;
			}			
			$pingdb[] = $rt;
		}
		if ($username) {
			$query = $db->query("SELECT groupid,username FROM pw_members WHERE username IN($username)");
			while ($rt = $db->fetch_array($query)) {	
				$userdb[$rt['username']] = $rt['groupid'];
			}
			foreach ($pingdb as $key => $val) {
				if ($groupid != 3 && $groupid != 4) {
					if ($userdb[$val['pinger']] == 3 || $userdb[$val['pinger']] == 4) {
						$pingdb[$key]['ifable'] = 1;
					}
				}
			}
		}
		!$pingdb && Showmsg('masigle_noping');
		require_once PrintEot($template);footer();
	} elseif ($_POST['step'] == 2) {
		PostCheck();
		InitGP(array('selid','record'));
		empty($selid) && Showmsg('masigle_nodata');

		foreach ($record as $key => $val) {
			$db->update("UPDATE pw_pinglog SET record=".pwEscape($val)." WHERE id=".pwEscape($key));
		}

		$pw_tmsgs = GetTtable($tid);
		$thread = $db->get_one("SELECT ptable FROM pw_threads WHERE tid=".pwEscape($tid));
		$pw_posts = GetPtable($thread['ptable']);
		
		$sqlwhere = $where = $ifmark = '';
		if (!is_numeric($pid)) {
			$sqlwhere .= " WHERE tid=".pwEscape($tid);
			$where .= " AND pid=0";
			$table = $pw_tmsgs;
		} else {
			$sqlwhere .= " WHERE pid=".pwEscape($pid);
			$table = $pw_posts;
		}
		
		$query = $db->query("SELECT name,point,pinger,record FROM pw_pinglog $sqlwhere $where ORDER BY pingdate ASC");
		while ($rt = $db->fetch_array($query)) {
			$newmark = $rt['name'].':'.($rt['point']>0 ? '+' : '').$rt['point'].'('.addslashes($rt['pinger']).") ".substrs($rt['record'],24);
			$ifmark	= $ifmark ? $newmark."\t".addslashes($ifmark) : $newmark;
			
			if (strlen($ifmark)>240) {
				$ifmark = substr($ifmark,0,240);
				$ifmark = substr($ifmark,0,strrpos($ifmark,"\t"));
			}

		}
		$db->update("UPDATE $table SET ifmark=".pwEscape($ifmark)."$sqlwhere");
		
		if (defined('AJAX')) {
			echo "success";
			echo "\t".$pid."\t";
			$ifmark = explode("\t",$ifmark);
			foreach ($ifmark as $key => $value) {
				echo "<li>$value</li>";
			}
			ajax_footer();
		} else {
			refreshto("read.php?tid=$tid&page=$page",'operate_success');
		}
	}
}
?>