<?php

define('THIS_SCRIPT', 'indivgroup_postings');
require_once 'global.php';
require_once INC_PATH.'filehandle.misc.php';
require_once INC_PATH.'BoardCache.class.php';
$lang = load_lang($lang, 'indivgroup_postings' );
$stats = $lang['HeaderTitle'];
$gettitle = $lang['HeaderTitle'];
if($fsetting['indivgroup_show'] == 0)
{
	head();
	showmsg($lang['Error.strings15']);
	footer();
}
head();

if( $userid <= 0 ){
	showmsg($lang['Error.strings0']);
	exit;
}

if( !is_numeric($id) || $id <= 0 ){
	showmsg($lang['Error.strings2']);
	exit;
}

if( $action{0}.$action{1} != 'p_' ){
	showmsg(str_replace('{$action}',$action,$lang['Error.strings7']));
	exit;
}
if( !function_exists($action) ){
	showmsg(str_replace('{$action}',$action,$lang['Error.strings7']));
	exit;
}

if( ChkPost() === false ){
	showmsg($lang['Error.strings3']);
	exit;
}

global $db,$dv,$userid,$membername;
head();
//检查发贴权限start
$groupid= intval($_GET["groupid"]);
$groupboardid= intval($_GET["groupboardid"]);
$groupmaster= false;							//是否群组管理员
if($userid <= 0)
	showmsg($lang['Error.strings16']);
if($fsetting['indivgroup_show'] == '0')
	showmsg($lang['Error.strings17']);

if (($groupid==0) || ($groupboardid == 0)){
	showmsg($lang['Error.strings18']);
}else{
	$groupinfo= array();
	if($query=$db->query("select id,groupname,groupinfo,appuserid,appusername,usernum,stats,postnum,topicnum,todaynum,yesterdaynum,limituser,appdate,passdate,visidate,locked,viewflag from {$dv}group_name where id={$groupid}")){
		if ($arr=$db->fetch_array($query)){
			$groupinfo=$arr;
		}
	}
	$db->free_result($query);

	if(count($groupinfo)==0)
		showmsg($lang['Error.strings19']);
	else{
		$groupmasterStr= $arr["appusername"];
		if(($groupmasterStr == $membername) && ($membername != ""))
			$groupmaster= true;

		$userresult=$db->query("select username from {$dv}group_user where islock=2 and groupid=".$groupid);	
		while ($userarr=$db->fetch_array($userresult)){
			if((trim($userarr["username"]) != "") && (trim($userarr["username"]) != $arr["appusername"])){
				$groupmasterStr.= "|".trim($userarr["username"]);
				if(($userarr["username"] == $membername) && ($membername != ""))
					$groupmaster= true;
			}
		}
		$db->free_result($userresult);
	}

	if(!$master)
		if($groupinfo["stats"] == '0')			//检查群组状态
			showmsg($lang['Error.strings20']);

	if(!($master || $groupmaster)){
		if($groupinfo["stats"] == '3')
			showmsg($lang['Error.strings21']);
		if($groupinfo["stats"] == '2')
			showmsg($lang['Error.strings22']);
		if($groupinfo["viewflag"] == '1'){
			if($userid == '')
				showmsg($lang['Error.strings23']);
		}
		showmsg($lang['Error.strings28']);
	}

	$query=$db->query("select id boardid,rootid,boardname,boardname boardtype,boardinfo,indeximg,postnum,topicnum,todaynum,lastpost,boardstats,rules from {$dv}group_board where rootid={$groupid} and id={$groupboardid}");
	$showboardlist = 1;

	$groupboardnum= mysql_num_rows($query);
	if ($groupboardnum==0)
		showmsg($lang['Error.strings25']);
	else{
		$arr=$db->fetch_array($query);
		$groupboardinfo=$arr;
		if($groupboardinfo["boardstats"] == '0')
			showmsg($lang['Error.strings26']);
	}
	$db->free_result($query);
}
//检查发贴权限end

$_topicInfo = false;

$ActionInfo = $lang['Action.Info'][str_replace('do_','',$action)];
$LocalCanLockTopic = false;
$LocalCanDelTopic = false;
$LocalCanMoveTopic = false;
$LocalCanTopTopic = false;
$LocalCanBestTopic = false;
$LocalCanAwardTopic = false;
$LocalCanTopTopic_a = false;
$LocalCanTopTopic_m = false;
$LocalCanTopicMode = false;
$allmsg = $lang['template.strings4'];

if( $query = $db->query("SELECT topicid,title,boardid,pollid,locktopic,child,postusername,postuserid,dateandtime,hits,expression,lastpost,lastposttime,istop,isbest,topicmode,mode FROM {$dv}group_topic WHERE topicid='{$id}' AND boardid={$groupboardid}") ){
	$_topicInfo =& $db->fetch_array($query);
}
if(empty($_topicInfo)){
	showmsg($lang['Error.strings4'].'11111111');
	exit;
} else{
	$tblName = "{$dv}group_bbs1";//$db->escape_string($_topicInfo['posttable']);
}
$UpdateBoardID = $boardinfo['parentstr'].','.$boardid;

$S= explode(',',$UpdateBoardID);
$temp= "";
$i= 0;
while($i<count($S))
{
	if($temp == "")
		$temp= $S[$i];
	else		
		$temp.= ",".$S[$i];
	$i++;
}
$UpdateBoardID= $temp;

$nowTotalUseTable = "{$dv}group_bbs1";

if( is_numeric($dowealth) && $dowealth != 0 ){
	$doWealthMsg = $lang['template.strings5'].$dowealth.$lang['template.strings3'];
} else{
	$dowealth = 0;
}
if( is_numeric($douserep) && $douserep != 0 ){
	$douserEPMsg = $lang['template.strings6'].$douserep.$lang['template.strings3'];
} else{
	$douserep = 0;
}
if( is_numeric($dousercp) && $dousercp != 0 ){
	$douserCPMsg = $lang['template.strings7'].$dousercp.$lang['template.strings3'];
} else{
	$dousercp = 0;
}
if( $doWealthMsg != '' || $douserEPMsg != '' || $douserCPMsg != '' ){
	$allmsg = $lang['template.strings8'].$doWealthMsg.$douserEPMsg.$douserCPMsg;
}

if( $userinfo['usertoday'] == '' ){
	$arrUserToday = array(0,0,0,0,0);
} else{
	$arrUserToday = explode('|',$userinfo['usertoday']);
}

if( !isset($delupfile) ){
	$delupfile = 0;
}

global $__REMOVED_FILE_ID_LIST;

isset($replyID) AND !isset($replyid) AND ($replyid = $replyID);
$action($_GET['funcargs']);
function_exists('_dv_muf_adminpostings_callback') AND _dv_muf_adminpostings_callback($action, $id, $replyid, $__REMOVED_FILE_ID_LIST);

footer();



/**
 ******************************************************
 *				以下函数仅从外部调用
 ******************************************************
 */

function p_do_fix_topic(){//提交修复帖子
	global $db, $dv, $groupboardid, $id, $sucmsg, $lang, $nowTotalUseTable,$checkbox;
	$LogType = 3;
	$sucmsg = $lang['template.strings21'];
	$rs =& $db->getResultSet("SELECT COUNT(1) AS c, MAX(dateandtime) AS m FROM ".$db->escape_string($nowTotalUseTable)." WHERE boardid='{$groupboardid}' AND rootid='{$id}'", array('pageSize'=>1));
	if (!empty($rs)) {
		$db->query("UPDATE {$dv}group_topic SET child=".($rs[0]['c']-1).",lastposttime='".$db->escape_string($rs[0]['m'])."' WHERE topicid='{$id}'");
	}
	FixLastPost();
	Insert_Forum_Log();
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_fix_topic

function FixLastPost()
{
	global $db, $dv, $groupboardid, $id, $sucmsg, $lang, $nowTotalUseTable;
	$rs =& $db->getResultSet("SELECT topic, body, announceid, dateandtime, username, postuserid, rootid, boardid, signflag, SUBSTRING(REPLACE(body, '$', ''), 1, 20) AS subbody FROM ".$db->escape_string($nowTotalUseTable)." WHERE boardid='{$groupboardid}' AND rootid='{$id}' ORDER BY announceid DESC", array('pageSize'=>1));
	if (!empty($rs)) {
		$bbsinfo =& $rs[0];
		$lastrootid = $bbsinfo['announceid'];
		$lastposttime = $bbsinfo['datandtime'];
		if (2 == $bbsinfo['signflag']) {	//	匿名用户
			$lastpostuser = $db->escape_string($lang['template.strings22']);
		} else {
			$lastpostuser = $db->escape_string(str_replace('$', '', $bbsinfo['username']));
		}
		$body = $db->escape_string($bbsinfo['body']);
		$lasttopic = htmlspecialchars($db->escape_string($bbsinfo['subbody']));
		$lastpostuserid = $bbsinfo['postuserid'];
		$lastid = $bbsinfo['rootid'];
		$boardid = $bbsinfo['boardid'];
	} else {
		$lastpostuser = $lasttopic = $db->scalar("SELECT SUBSTRING('".$db->escape_string(htmlspecialchars($lang['template.strings14']))."', 1, 20)");
		$lastrootid = 0;
		$lastposttime = TIME_NOW;
		$lastpostuserid = 0;
		$body = '';
		$lastid = 0;
		$boardid = 0;
	}
	$lastposttime=TIME_NOW;
	$lastpost = $lastpostuser.'$'.$lastrootid.'$'.$lastposttime.'$'.$lasttopic.'$'.$Uploadpic_n.'$'.$lastposruserid.'$'.$lastid.'$'.$boardid;
	$db->query("UPDATE {$dv}group_topic SET lastpost='{$lastpost}' WHERE topicid='{$id}'");
}//end function FixLastPost

function p_do_lock_topic(){//提交帖子锁定
	global $LogType,$db,$dv,$id,$groupboardid,$_topicInfo,$lang,$checkbox;
	$LogType = 5;
	$db->query("UPDATE {$dv}group_topic SET locktopic=1 WHERE boardid={$groupboardid} AND topicid='{$id}'");
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($sucmsg));
}//end function p_lock_topic

function p_do_unlock_topic(){//提交解除锁定
	global $LogType,$db,$dv,$id,$groupboardid,$lang,$fsetting,$userinfo,$arrUserToday,$_topicInfo,$lang,$checkbox;
	$LogType = 3;
	$db->query("UPDATE {$dv}group_topic SET locktopic=0 WHERE boardid={$groupboardid} AND topicid='{$id}'");
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_unlock_topic

function p_follow_topic_manage(){//跟帖管理操作界面
	global $LogType,$db,$dv,$id,$boardid,$lang,$fsetting,$userinfo,$arrUserToday,$tblName;
	global $dowealth,$douserep,$dousercp,$board_user;
	$dowealth = -$board_user[3];
	$douserep = -$board_user[8];
	$dousercp = -$board_user[13];
	Check_topicInfo();
	//判断用户是否有删除帖子权限
	if( CanDelTopic() === false ){
		showmsg($lang['Error.strings5']);
		exit;
	}
	global $star,$_topicInfo,$board_settings;
	if( !is_numeric($star) || $star <= 0 ){
		$star = 1;
	}
	$pageSize = $board_settings[61];
	$RecordCount = $db->scalar("SELECT COUNT(1) FROM {$tblName} WHERE boardid='{$boardid}' AND rootid='{$id}' AND boardid<>777 AND boardid<>444");
	$arrTopicInfo =& $db->getResultSet("SELECT  announceid,parentid,boardid,username,postuserid,topic,dateandtime,length,rootid,layer,orders,expression,body  FROM {$tblName} WHERE boardid='{$boardid}' AND rootid='{$id}' AND boardid<>777 AND boardid<>444 ORDER BY parentid ASC,rootid DESC,orders",array('absolutePage'=>$star,'pageSize'=>$pageSize));
	$reaction = 'p_do_follow_topic_manage';
	require printout('admin_postings.gtgl');
}//end function p_follow_topic_manage
function p_do_follow_topic_manage(){//提交跟帖管理——批量删除跟帖
	global $lang,$dv,$db,$delid,$lang,$_topicInfo,$replyid;
	if( empty($delid) ){
		showmsg($lang['Error.strings12']);
		exit;
	}
	//判断用户是否有删除帖子权限
	if( CanDelTopic() === false ){
		showmsg($lang['Error.strings5']);
		exit;
	}
	$DelIDList = implode(',',$delid);
	if( checkIDList($DelIDList) === false ){
		showmsg($lang['Error.strings12']);
		exit;
	}
	Check_topicInfo();
	$index = 0;
	$replyTitle = $lang['template.strings15'];
	$dateNow = TIME_NOW;
	foreach( $delid as $index => $replyid ){
		commonDelete(0);
	}
	Create_index_forumnum_group();//20070523
	showmsg(SucMsgInfo(str_replace('{$num}',$index+1,$lang['template.strings18'])));
}//end function p_do_follow_topic_manage

//判断是否为帖子最后回复
function isLastPost(){
	global $dv,$db,$boardid,$id,$lang,$_topicInfo,$replyid,$tblName;
	$returnVal = false;
	//取得当前主题最后回复ID
		//{$dv}topic.topicid是唯一的，因此，放弃查询，改用$_topicInfo
	if( $_topicInfo['lastpost'].'' !== '' ){
		$arrTmp = explode('$',$_topicInfo['lastpost']);
		if( $arrTmp[1] == $replyid ){
			$returnVal = true;
		}
	}
	if( $returnVal === true ){
		if (-1 == $_topicInfo['isvote']) {	//	判断是否为辩论帖
			$tmpRs =& $db->getResultSet("SELECT (CASE WHEN LENGTH(title)>0 THEN title WHEN LENGTH(content)>0 THEN SUBSTRING(content, 1, 20) ELSE '' END) AS Topic, argueid AS AnnounceID, creation_date AS CreationDate, postusername AS PostUserName, postuserid AS PostUserID, rootid, boardid FROM {$dv}argue WHERE rootid='{$id}' AND arguetype IN('0', '1', '2', '3') AND parentid>0 AND parentid<>argueid AND boardid='{$boardid}' ORDER BY argueid DESC", array('pageSize'=>1));
		} else {
			$tmpRs =& $db->getResultSet("SELECT (CASE WHEN LENGTH(topic)>0 THEN topic WHEN LENGTH(body)>0 THEN SUBSTRING(body, 1, 20) ELSE '' END) AS Topic, announceid AS AnnounceID, dateandtime AS CreationDate, username AS PostUserName, postuserid AS PostUserID, rootid, boardid FROM {$tblName} WHERE rootid='{$id}' AND boardid='{$boardid}' ORDER BY announceid DESC",array('pageSize'=>1));
		}
		if( $tmpRs !== false ){
			$LastRootID = $tmpRs[0]['AnnounceID'];
			$LastPostTime = $tmpRs[0]['CreationDate'];
			$LastPostUser = str_replace('$','',$tmpRs[0]['PostUserName']);
			$tmpRs[0]['Topic'] = trim($tmpRs[0]['Topic']);
			if ('' === $tmpRs[0]['Topic']) {
				$LastTopic = $lang['template.strings14'];
			} else {
				$LastTopic = $tmpRs[0]['Topic'];
			}
			$LastPostUserID = $tmpRs[0]['PostUserID'];
			$LastID = $tmpRs[0]['rootid'];
			$boardid = $tmpRs[0]['boardid'];
		} else{
			$LastRootID = 0;
			$LastPostTime = TIME_NOW;
			$LastPostUser = $lang['template.strings14'];
			$LastTopic = $lang['template.strings14'];
			$LastPostUserID = 0;
			$LastID = 0;
			$boardid = 0;
		}
		$LastTopic = strip_tags($LastTopic);
		$LastPost = $LastPostUser.'$'.$LastRootID.'$'.$LastPostTime.'$'.htmlspecialchars($LastTopic).'$'.$uploadpic_n.'$'.$LastPostUserID.'$'.$LastID.'$'.$boardid;
		$db->query("UPDATE {$dv}topic SET lastpost='".$db->escape_string($LastPost)."' WHERE topicid='{$id}'");
	}
	return $returnVal;
}//end function isLastPost

function p_do_delete_topic(){//提交删除帖子
	global $checkbox,$lang;
	global $_topicInfo,$lang,$db,$dv,$groupid,$groupboardid,$userid,$membername,$id,$tblName;
	if( $_topicInfo['title'] == '' || is_null($_topicInfo['title']) ){
		$_topicInfo['title'] = $lang['template.strings15'];
	}
	$LogType = 3;

	$time1 = strtotime(date('Y-m-d 00:00:00'));
	$time2 = $time1 + (24 * 3600 - 1);

	$TodayNum= 0;
	$PostNum= 0;
	$query= $db->query("Select dateandtime,parentid From {$dv}group_bbs1 Where groupid={$groupid} And boardid={$groupboardid} And rootid='{$id}'");
	while ($arr=$db->fetch_array($query)){
		if(($arr['dateandtime']>=$time1) && ($arr['dateandtime']<=$time2))
			$TodayNum++;
		$PostNum++;
	}
	$db->query("UPDATE {$dv}group_board SET postnum=(postnum-{$PostNum}),topicnum=(topicnum-1),todaynum=(todaynum-{$TodayNum}) WHERE id={$groupboardid}");
	$db->query("UPDATE {$dv}group_name SET postnum=(postnum-{$PostNum}),topicnum=(topicnum-1),todaynum=(todaynum-{$TodayNum}) WHERE id={$groupid}");

	$db->query("UPDATE {$tblName} SET boardid=(-1),locktopic='{$groupboardid}' WHERE rootid='{$id}'");
	$db->query("UPDATE {$dv}group_topic SET boardid=(-1),locktopic='{$groupboardid}',isbest='0' WHERE topicid='{$id}'");
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	lastpost();
	Insert_Forum_Log();
	require_once INC_PATH.'BoardCache.class.php';
	Create_index_forumnum_group();
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_delete_topic

function ReloadBoardInfo($lboardid) {
	global $lang,$dv,$db;
	$boardidlist=explode(",",$lboardid);
	foreach ($boardidlist as $bid){
		if($bid<>0 and $bid<>""){
			$bid=intval(trim($bid));			
		}
	}

}
function LoadBoardinformation($bid) {
	global $lang,$dv,$db;
}
function Get_RequestInfo() {
	global $lang,$dv,$db,$ActionInfo,$allmsg,$topic,$id;
	$sucmsg="";
	$title=htmlspecialchars($_POST['title']);
	$content=htmlspecialchars($_POST['content']);
	$content=$lang['template.strings0'].$title.$content;
	if($_POST['title']=='' and $_POST['content']==''){
		showmsg($lang['Error.strings6']);
		exit;				
	}
	$topic=$db->scalar("select title from {$dv}topic where topicid=".intval($id)."");
	$sucmsg=$ActionInfo.$lang['template.strings1'].htmlspecialchars($topic).$lang['template.strings2'].htmlspecialchars($content).$lang['template.strings3'].$allmsg."" ;
	return $sucmsg;
}

function p_do_shield_single_topic(){//提交单帖屏敝
	global $LogType,$db,$dv,$id,$groupboardid,$replyid,$lang,$fsetting,$userinfo,$arrUserToday,$LogType,$tblName,$_topicInfo,$checkbox;
	$LogType = 5;
	$db->query("UPDATE {$tblName} SET locktopic=2 WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	GetUserID();
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_shield_single_topic

function GetUserID(){
	global $dv,$db,$groupboardid,$tblName,$replyid,$_topicInfo;

	$tmpRs =& $db->getResultSet("SELECT postuserid,username FROM {$tblName} WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");

	if( $tmpRs !== false ){
		$_topicInfo['postuserid'] = $tmpRs[0]['postuerid'];
		$_topicInfo['postusername'] = $tmpRs[0]['username'];
	}
}//end function GetUserID

function p_do_unshield_single_topic(){//提交解除单帖屏敝
	global $LogType,$db,$dv,$rootid,$groupboardid,$id,$replyid,$lang,$fsetting,$userinfo,$arrUserToday,$LogType,$tblName,$checkbox;
	$LogType = 3;
	$db->query("UPDATE {$tblName} SET locktopic=0 WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	GetUserID();
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_unshield_single_topic

function p_do_placed_top_topic(){//提交固顶帖子
	global $checkbox;
	global $LogType,$db,$dv,$id,$groupboardid,$newboardid,$lang,$fsetting,$userinfo,$arrUserToday,$_POST;
	global $UpdateBoardID_1,$_topicInfo,$membername,$newboardid,$boardinfo,$replyid,$usergroupid,$getboard;
	$LogType = 4;
	//	当前版面固顶
	$db->query("UPDATE {$dv}group_topic SET istop=1 WHERE boardid='{$groupboardid}' AND topicid='{$id}'");
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	Insert_Forum_Log();
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_placed_top_topic
function p_do_unplaced_top_topic(){//提交解除固顶帖子
	global $checkbox;
	global $LogType,$db,$dv,$id,$groupboardid,$newboardid,$lang,$fsetting,$userinfo,$arrUserToday,$_POST;
	global $UpdateBoardID_1,$_topicInfo,$membername,$newboardid,$boardinfo,$replyid,$usergroupid,$getboard;
	$LogType = 4;
	//	当前版面固顶
	$db->query("UPDATE {$dv}group_topic SET istop=0 WHERE boardid='{$groupboardid}' AND topicid='{$id}'");
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	Insert_Forum_Log();
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_placed_top_topic

function p_do_set_best_topic(){//提交加为精华
	global $dv,$db,$lang,$groupboardid,$replyid,$id,$tblName,$_topicInfo,$checkbox;
	$LogType = 3;
	$tmpRs =& $db->getResultSet("SELECT announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,isagree,ubblist FROM {$tblName} WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	if( $tmpRs === false ){
		showmsg($lang['Error.strings4']);
		exit;
	}
	$_topicInfo['title'] = $tmpRs[0]['topic'];
	$_topicInfo['postusername'] = $tmpRs[0]['username'];
	$_topicInfo['postuserid'] = $tmpRs[0]['postuserid'];
	if( $_topicInfo['title'] == '' ){
		$_topicInfo['title'] = substr(str_replace(chr(10),',',$tmpRs[0]['body']),0,26);
	}
	$strDateTime = TIME_NOW;
	$db->query("UPDATE {$tblName} SET isbest='1' WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	$db->query("UPDATE {$dv}group_topic SET isbest='1' WHERE boardid='{$groupboardid}' AND topicid='{$id}'");
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($sucmsg));
}//end function p_do_set_best_topic

function p_do_unset_best_topic(){//提交解除精华
	global $dv,$db,$lang,$groupboardid,$replyid,$id,$tblName,$_topicInfo,$checkbox;
	$LogType = 3;
	$tmpRs =& $db->getResultSet("SELECT announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,isagree,ubblist FROM {$tblName} WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	if( $tmpRs === false ){
		showmsg($lang['Error.strings4']);
		exit;
	}
	$_topicInfo['title'] = $tmpRs[0]['topic'];
	$_topicInfo['postusername'] = $tmpRs[0]['username'];
	$_topicInfo['postuserid'] = $tmpRs[0]['postuserid'];
	if( $_topicInfo['title'] == '' ){
		$_topicInfo['title'] = substr(str_replace(chr(10),',',$tmpRs[0]['body']),0,26);
	}
	$strDateTime = TIME_NOW;
	$db->query("UPDATE {$tblName} SET isbest='0' WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	$db->query("UPDATE {$dv}group_topic SET isbest='0' WHERE boardid='{$groupboardid}' AND topicid='{$id}'");
	Insert_Forum_Log();
	global $sucmsg,$action;
	$sucmsg = $lang['Action.title'][str_replace('do_','',$action)];
	showmsg(SucMsgInfo($GLOBALS['sucmsg']));
}//end function p_do_unset_best_topic


/**
 ******************************************************
 *				以下函数仅从内部调用
 ******************************************************
 */

function CanLockTopic(){
	global $master,$boardmaster,$superboardmaster,$groupsetting,$LocalCanLockTopic,$usergroupid,$_topicInfo,$membername,$founduserper;
	if( ( $master===true || $boardmaster === true || $superboardmaster === true ) && $groupsetting[20] == 1 ){
		$LocalCanLockTopic = true;
	}
	if( $usergroupid > 3 && $groupsetting[20] == 1 ){
		$LocalCanLockTopic = true;
	}
	if( $groupsetting[13] == 1 && $_topicInfo['postusername'] == $membername ){
		$LocalCanLockTopic = true;
	}
	if( $founduserper === true && $groupsetting[13] == 1 && $_topicInfo['postusername'] == $membername ){
		$LocalCanLockTopic = true;
	} elseif( $founduserper === true && $groupsetting[13] == 0 && $_topicInfo['postusername'] == $membername ){
		$LocalCanLockTopic = false;
	}
	if( $founduserper === true && $groupsetting[20] == 1 && $_topicInfo['postusername'] != $membername ){
		$LocalCanLockTopic = true;
	} elseif( $founduserper === true && $groupsetting[20] == 0 && $_topicInfo['postusername'] != $membername ){
		$LocalCanLockTopic = false;
	}
	return $LocalCanLockTopic;
}//end function CanLockTopic

function InitRequestInfo(){
	global $sucmsg,$title,$content,$lang,$ActionInfo,$_topicInfo,$allmsg;
	$sucmsg = '';
	if( $title == '' && $content == '' ){
		showmsg($lang['Error.strings6']);
		exit;
	}
	$title = htmlspecialchars($title);
	$content = htmlspecialchars($content);
	$content = $lang['template.strings0'].$title.$content;
	$sucmsg = $ActionInfo.$lang['template.strings1'].htmlspecialchars($_topicInfo['title']).$lang['template.strings2'].$content.$lang['template.strings3'].$allmsg;
}

function Insert_Forum_Log(){
	global $dv,$db;
	$db->query("INSERT INTO {$dv}log (l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type,l_addtime) VALUES('{$GLOBALS['id']}',{$GLOBALS['groupboardid']},'".$db->escape_string($GLOBALS['topicUserName'])."','{$GLOBALS['membername']}','".$db->escape_string($GLOBALS['sucmsg'])."','{$GLOBALS['usertrueip']}','{$GLOBALS['LogType']}','".TIME_NOW."')");
}//end function Insert_Forum_Log

//判断用户是否有奖励/惩罚帖子权限
function CanAwardTopic(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanAwardTopic,$usergroupid;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	}
	if( $usergroupid > 13 && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	}
	if( $founduserper === true && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	} elseif( $founduserper === true && $groupsetting[22] == 0 ){
		$LocalCanAwardTopic = false;
	}
	return $LocalCanAwardTopic;
}//end function CanAwardTopic

//判断用户是否有删除帖子权限
function CanDelTopic(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanDelTopic,$usergroupid,$_topicInfo,$membername;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[18] == 1 ){
		$LocalCanDelTopic = true;
	}
	if( $usergroupid > 3 && $groupsetting[18] == 1 ){
		$LocalCanDelTopic = true;
	}
	if( $groupsetting[11] == 1 && $_topicInfo['postusername'] == $membername ){
		$LocalCanDelTopic = true;
	}
	if( $founduserper === true && $groupsetting[11] == 1 && $_topicInfo['postusername'] == $membername ){
		$LocalCanDelTopic = true;
	} elseif( $founduserper === true && $groupsetting[11] == 0 && $_topicInfo['postusername'] == $membername ){
		$LocalCanDelTopic = false;
	}
	if( $founduserper === true && $groupsetting[18] == 1){
		$LocalCanDelTopic = true;
	} elseif( $founduserper === true && $groupsetting[18] == 0){
		$LocalCanDelTopic = false;
	}
	return $LocalCanDelTopic;
}//end function CanDelTopic

//判断用户是否有奖励/惩罚帖子权限
function CanAwardTopic_backup(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanAwardTopic,$usergroupid,$_topicInfo,$membername;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	}
	if( $usergroupid > 3 && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	}
	if( $founduserper === true && $groupsetting[22] == 1 ){
		$LocalCanAwardTopic = true;
	} elseif( $founduserper === true && $groupsetting[22] == 0 ){
		$LocalCanAwardTopic = false;
	}
	return $LocalCanAwardTopic;
}//end function CanAwardTopic_backup

//判断用户是否有固顶/解除固顶帖子权限
function CanTopTopic(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanTopTopic,$usergroupid,$_topicInfo,$membername;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[21] == 1 ){
		$LocalCanTopTopic = true;
	}
	if( $usergroupid > 3 && $groupsetting[21] == 1 ){
		$LocalCanTopTopic = true;
	}
	if( $founduserper === true && $groupsetting[21] == 1 ){
		$LocalCanTopTopic = true;
	} elseif( $founduserper === true && $groupsetting[21] == 0 ){
		$LocalCanTopTopic = false;
	}
	return $LocalCanTopTopic;
}//end function CanTopTopic

//判断用户是否有总固顶帖子权限
function CanTopTopic_a(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanTopTopic_a,$usergroupid,$_topicInfo,$membername;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[38] == 1 ){
		$LocalCanTopTopic_a = true;
	}
	if( $usergroupid > 3 && $groupsetting[38] == 1 ){
		$LocalCanTopTopic_a = true;
	}
	if( $founduserper === true && $groupsetting[38] == 1 ){
		$LocalCanTopTopic_a = true;
	} elseif( $founduserper === true && $groupsetting[38] == 0 ){
		$LocalCanTopTopic_a = false;
	}
	return $LocalCanTopTopic_a;
}//end function CanTopTopic_a

//判断用户是否有区域固顶帖子权限
function CanTopTopic_m(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanTopTopic_m,$usergroupid,$_topicInfo,$membername,$getboard;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[54] == 1 ){
		$LocalCanTopTopic_m = true;
	}
	if( $usergroupid > 3 && $groupsetting[54] == 1 ){
		$LocalCanTopTopic_m = true;
	}
	if( $founduserper === true && $groupsetting[54] == 1 ){
		$LocalCanTopTopic_m = true;
	} elseif( $founduserper === true && $groupsetting[54] == 0 ){
		$LocalCanTopTopic_m = false;
	}
	if (is_array($getboard) AND count($getboard) <= 1) {
		$LocalCanTopTopic_m = false;
	}
	return $LocalCanTopTopic_m;
}//end function CanTopTopic_m

//判断用户是否有加入/解除精华帖子权限
function CanBestTopic(){
	global $master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$LocalCanBestTopic,$usergroupid,$_topicInfo,$membername;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[24] == 1 ){
		$LocalCanBestTopic = true;
	}
	if( $usergroupid > 3 && $groupsetting[24] == 1 ){
		$LocalCanBestTopic = true;
	}
	if( $founduserper === true && $groupsetting[24] == 1 ){
		$LocalCanBestTopic = true;
	} elseif( $founduserper === true && $groupsetting[24] == 0 ){
		$LocalCanBestTopic = false;
	}
	return $LocalCanBestTopic;
}//end function CanBestTopic

//版主奖惩帖子
function ChkRewardMoney(){
	global $lang,$master,$superboardmaster,$boardmaster,$groupsetting,$founduserper,$usergroupid,$_topicInfo,$fsetting,$arrUserToday;
	$CanRewardMoney = false;
	if( ( $master === true || $superboardmaster === true || $boardmaster === true ) && $groupsetting[22] == 1 ){
		$CanRewardMoney = true;
	}
	if( $usergroupid > 3 && $groupsetting[22] == 1 ){
		$CanRewardMoney = true;
	}
	if( $founduserper === true && $groupsetting[22] == 1 ){
		$CanRewardMoney = true;
	} elseif( $founduserper === true && $groupsetting[22] == 0 ){
		$CanRewardMoney = false;
	}
	if( $master === false ){
		if( $arrUserToday[4] >= $fsetting['boardmaster_moneylimited'] ){
			$CanRewardMoney = false;
		}
	}
	//return $CanRewardMoney;
	if( $CanRewardMoney === false ){
		showmsg($lang['Error.strings5']);
		exit;
	}
}//end function ChkRewardMoney

function SucMsgInfo($GetMsg){
	global $lang,$groupid,$groupboardid,$id,$replyid,$rootid,$_topicInfo;
	$Topic = htmlspecialchars($_topicInfo['title']);

	$TPL_returntopic = 'indivgroup_dispbbs.php';

	if ($groupboardid <= 0) {
		$groupboardid = ($_GET['groupboardid'] > 0 ? $_GET['groupboardid'] : $_POST['groupboardid']);
	}
	if ($groupid <= 0) {
		$groupid = ($_GET['groupid'] > 0 ? $_GET['groupid'] : $_POST['groupid']);
	}
	eval('$returnVal="'.addslashes($lang['SucMsgInfo']).'";');
	return $returnVal;
}//end function SucMsgInfo

//更新指定论坛信息
function LastCount($boardID){
	global $dv,$db,$UpdateBoardID,$boardid,$lang,$Lasttopic,$LastRootid,$LastPostUser,$LastPost,$nowTotalUseTable;
	$tmpRs =& $db->getResultSet("SELECT T.title,b.announceid,b.dateandtime,b.username,b.postuserid,b.rootid FROM ".$db->escape_string($nowTotalUseTable)." b INNER JOIN {$dv}topic T ON b.rootid=T.topicid WHERE b.boardid={$boardid} ORDER BY b.announceid DESC",array('pageSize'=>1));
	if( $tmpRs !== false ){
		$Lasttopic = str_replace('$','',substr(preg_replace('/<(.[^>]*)>/i','',$tmpRs['title']),0,15));
		$LastRootid = $tmpRs['announceid'];
		$LastPostTime = $tmpRs['dateandtime'];
		$LastPostUser = $tmpRs['username'];
		$LastPostUserid = $tmpRs['postuserid'];
		$Lastid = $tmpRs['rootid'];
	} else{
		$Lasttopic = $lang['template.strings14'];
		$LastRootid = 0;
		$LastPostTime = TIME_NOW;
		$LastPostUser = $lang['template.strings14'];
		$LastPostUserid = 0;
		$Lastid = 0;
	}
	$LastPost = $LastPostUser.'$'.$LastRootid.'$'.$LastPostTime.'$'.$uploadpic_n.'$'.$LastPostUserid.'$'.$Lastid.'$'.$boardid;
	if( checkIDList($UpdateBoardID) ){
		$db->query("UPDATE {$dv}board SET lastpost='{$LastPost}' WHERE boardid IN('{$UpdateBoardID}')");
	}
}//end function LastCount

//版面发帖数减少
function BoardNumSub($boardID,$topicNum,$postNum,$todayNum){
	global $dv,$db,$UpdateBoardID,$boardid,$lang,$Lasttopic,$LastRootid,$LastPostUser,$LastPost,$nowTotalUseTable;
	$db->query("UPDATE {$dv}board SET postnum=postnum-'{$postNum}',topicnum=topicnum-'{$topicNum}',todaynum=todaynum-'{$todayNum}' WHERE boardid IN({$UpdateBoardID})");
	$tmpRs =& $db->getResultSet("SELECT T.title,b.announceid,b.dateandtime,b.username,b.postuserid,b.rootid FROM ".$db->escape_string($nowTotalUseTable)." b INNER JOIN {$dv}topic T ON b.rootid=T.topicid WHERE b.boardid={$boardid} ORDER BY b.announceid DESC",array('pageSize'=>1));
	if( $tmpRs !== false ){
		$Lasttopic = str_replace('$','',substr(preg_replace('/<(.[^>]*)>/i','',$tmpRs['title']),0,15));
		$LastRootid = $tmpRs['announceid'];
		$LastPostTime = $tmpRs['dateandtime'];
		$LastPostUser = $tmpRs['username'];
		$LastPostUserid = $tmpRs['postuserid'];
		$Lastid = $tmpRs['rootid'];
	} else{
		$Lasttopic = $lang['template.strings14'];
		$LastRootid = 0;
		$LastPostTime = TIME_NOW;
		$LastPostUser = $lang['template.strings14'];
		$LastPostUserid = 0;
		$Lastid = 0;
	}
	$LastPost = $LastPostUser.'$'.$LastRootid.'$'.$LastPostTime.'$'.$uploadpic_n.'$'.$LastPostUserid.'$'.$Lastid.'$'.$boardid;
}//end function BoardNumSub

//所有论坛发帖数减少
function AllboardNumSub($todayNum,$postNum,$topicNum){
	global $db,$dv;
	$db->query("UPDATE {$dv}setup SET forum_todaynum=forum_todaynum-{$todayNum},forum_postnum=forum_postnum-{$postNum},forum_topicnum=forum_topicnum-{$topicNum}");
}//end function AllboardNumSub
//版面发帖数增加
function BoardNumAdd($boardID,$topicNum,$postNum,$todayNum){
	global $dv,$db,$UpdateBoardID;
	$db->query("UPDATE {$dv}board SET postnum=postnum+{$postNum},topicnum=topicnum+{$topicNum},todaynum=todaynum+{$todayNum} WHERE boardid IN ({$UpdateBoardID})");
}//end function BoardNumAdd

//所有论坛发帖数增加
function AllboardNumAdd($todayNum,$postNum,$topicNum){
	global $dv,$db;
	$db->query("UPDATE {$dv}setup SET forum_todaynum=forum_todaynum+{$todayNum},forum_postnum=forum_postnum+{$postNum},forum_topicnum=forum_topicnum+{$topicNum}");
}//end function AllboardNumAdd

function Check_topicInfo(){
	global $db,$dv,$boardid,$id,$_topicInfo,$tblName,$lang;
	$tmpRs =& $db->getResultSet("SELECT topic,username,postuserid FROM {$tblName} WHERE parentid=0 AND boardid='{$boardid}' AND rootid='{$id}'");
	if( $tmpRs === false ){
		showmsg($lang['Error.strings4']);
		exit;
	}
	$_topicInfo['title'] = $tmpRs[0]['topic'];
	$_topicInfo['postusername'] = $tmpRs[0]['username'];
	$_topicInfo['postuserid'] = $tmpRs[0]['postuserid'];
}//end function Check_topicInfo

function Check_AnnounceInfo(){
	global $lang,$dv,$db,$groupboardid,$replyid,$tblName,$_topicInfo;
	$tmpRs =& $db->getResultSet("SELECT topic,username,postuserid FROM {$tblName} WHERE boardid='{$groupboardid}' AND announceid='{$replyid}'");
	if( $tmpRs === false ){
		showmsg($lang['Error.strings4']);
		exit;
	}
	$_topicInfo['title'] = $tmpRs[0]['topic'];
	$_topicInfo['postusername'] = $tmpRs[0]['username'];
	$_topicInfo['postuserid'] = $tmpRs[0]['postuserid'];
}//end function Check_AnnounceInfo

//截取指定字符
function cutStr($str,$strlen){
	//去掉所有HTML标记
	$str = preg_replace( '/<([^<>]*?)>/i' ,'' ,$str);
	$len = strlen($str);
	$t = 0;
	$returnVal = '';
	for( $i = 0 ; $i < $len ; $i++ ){
		$c = abs(ord($str{$i}));
		if( $c > 255 ){
			$t += 2;
		} else{
			$t++;
		}
		if( $t > $len ){
			$returnVal = substr($str,0,$i+1).'...';
			break;
		} else{
			$returnVal = $str;
		}
	}
	return str_replace("\r",'',str_replace("\n",'',$returnVal));
}//end function cutStr

//过滤不必要UBB,和HTML
function reUBBCode(&$strContent){
	if( $strContent == '' ){
		return '';
	}
	return preg_replace(
			 array(
			  '/&nbsp;/i'
			 ,'/(\[QUOTE\])(.|\n)*(\[\/QUOTE\])/i'
			 ,'/(\[point=*([0-9]*)\])(.|\n)*(\[\/point\])/i'
			 ,'/(\[post=*([0-9]*)\])(.|\n)*(\[\/post\])/i'
			 ,'/(\[power=*([0-9]*)\])(.|\n)*(\[\/power\])/i'
			 ,'/(\[usercp=*([0-9]*)\])(.|\n)*(\[\/usercp\])/i'
			 ,'/(\[money=*([0-9]*)\])(.|\n)*(\[\/money\])/i'
			 ,'/(\[replyview\])(.|\n)*(\[\/replyview\])/i'
			 ,'/(\[usemoney=*([0-9]*)\])(.|\n)*(\[\/usemoney\])/i'
			 ,'/<I><\/I>/i'
			 ,'/\n|\r/i'
			 ,'/'.chr(32).'/i'
			 ,'/'.chr(9).'/i'
			 ,'/<(.[^>]*)>/i'
			 )
			,array(
			 ''
			,''
			,'&nbsp;'
			,'&nbsp;'
			,'&nbsp;'
			,'&nbsp;'
			,'&nbsp;'
			,'&nbsp;'
			,'&nbsp;'
			,''
			,''
			,' '
			,' '
			,''
			)
			,$strContent
		   );
}//end function reUBBCode

function delupfiles($F_BoardID,$F_announceID) // 删除附件
{
	global $dv, $db, $__REMOVED_FILE_ID_LIST;
	$documentroot = realpath(getenv('DOCUMENT_ROOT'));
	if ($query = $db->query("SELECT f_id, f_filename, f_viewname FROM {$dv}upfile WHERE f_boardid='{$F_BoardID}' AND f_announceid LIKE '{$F_announceID}%' AND f_flag=0")) {
		$fidlist = '';
		$comma = '';
		while ($dbinfo =& $db->fetch_array($query)) {
			$fidlist .= "{$comma}{$dbinfo['f_id']}";
			$comma = ',';
			if (!empty($dbinfo['f_filename'])) {
				if ($filepath = realpath($documentroot.'/'.$dbinfo['f_filename'])) {
					FileHandle::unlink($filepath);
				}
			}
			if (!empty($dbinfo['f_filename'])) {
				if ($viewfilepath = realpath($documentroot.'/'.$dbinfo['f_viewname'])) {
					FileHandle::unlink($viewfilepath);
				}
			}
		}
		if ($fidlist !== '') {
			$__REMOVED_FILE_ID_LIST = $fidlist;
			$db->query("DELETE FROM {$dv}upfile WHERE f_id IN({$fidlist})");
		}
	}
}

//删除帖子后更新“最后发帖” Add By xiaoD
function lastpost() {
	global $db, $dv, $groupboardid;

	$query = $db->query("SELECT username, announceid, dateandtime, parentid, topic, postuserid, rootid FROM {$dv}group_bbs1 WHERE boardid<>-1 AND boardid='$groupboardid' ORDER BY dateandtime DESC");

	$rs = $db->fetch_array($query);

	$username = $rs['username'];
	$announceid = $rs['announceid'];
	$dateandtime = $rs['dateandtime'];
	$topic = $rs['topic'];
	$postuserid = $rs['postuserid'];
	$topicid = $rs['rootid'];

	$parentid = $rs['parentid'];

	if($parentid) {
		$topic = $db->scalar("SELECT topic FROM {$dv}group_bbs1 WHERE announceid='$parentid'");
	}

	$lastpost = $username.'$'.$announceid.'$'.$dateandtime.'$'.$topic.'$$'.$postuserid.'$'.$topicid.'$'.$boardid;

	$db->query("UPDATE {$dv}group_board SET lastpost='$lastpost' WHERE id='$groupboardid'");
}

?>