<?php
define('THIS_SCRIPT', 'admin_lockuser');
require_once 'global.php';
$lang = load_lang($lang, 'admin_lockuser' );
global $imgurl;
$stats = $lang['forum.info1'];
$arrNavMenu = array(
		 0=>array('linktitle'=>$stats)
);
head(1,0,0,$arrNavMenu);
$adminuserper = false;
if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[42] == 1){
	$adminuserper = true;
}
if ($usergroupid > 3 && $groupsetting[42] == 1){
	$adminuserper = true;
}
if ($founduserper == true && $groupsetting[42] == 1){
	$adminuserper = true;
}elseif ($founduserper == true && $groupsetting[42] == 0){
	$adminuserper = false;
}
if ($name == ''){
	showmsg($lang['err.info1']);
}
if ($toid != ''){
	if (!is_numeric($toid)){
		showmsg($lang['err.info2']);
	}else{
		$query=$db->query("Select userid,username From {$dv}user Where userid='$toid'");
		if ($uinfo = $db->fetch_array($query)){
			$toid = $uinfo['userid'];
			$name = $uinfo['username'];
		}else{
			showmsg($lang['err.info3']);
		}
	}
}else{
	$query=$db->query("Select userid,username From {$dv}user Where username='$name'");
	if ($uinfo = $db->fetch_array($query)){
		$toid = $uinfo['userid'];
		$name = $uinfo['username'];
	}else{
		showmsg($lang['err.info3']);
	}
}
if (ChkPost() === false){
	showmsg($lang['err.info4']);
}
switch($action){
	case 'power':
		poweruser();
		break;
	case 'deltopic':
		deltopic();
		break;
	case 'getpermission':
		boardlist();
		break;
	case 'userboardpermission':
		userboardpermission();
		break;
	case 'saveuserpermission':
		saveuserpermission();
		break;
	case 'deluserreply':
		deluserreply();
		break;
	default:
		lockuser();
		break;
}

function lockuser(){
	global $dv,$db,$action,$name,$toid,$membername,$usertrueip,$lang,$master,$boardmaster,$superboardmaster,$founduserper,$groupsetting,$usergroupid;
	$canlockuser = false;
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[28] == 1){
		$canlockuser = true;
	}
	if ($usergroupid > 3 && $groupsetting[28] == 1){
		$canlockuser = true;
	}
	if ($founduserper == true && $groupsetting[28] == 1){
		$canlockuser = true;
	}elseif ($founduserper == true && $groupsetting[28] == 0){
		$canlockuser = false;
	}
	if ($canlockuser == false){
		showmsg($lang['err.info5']);
	}
	switch($action){
		case 'lock_1':
			$locktype = $lang['forum.info2'].$lang['forum.info3'];
			$db->query("update {$dv}user set lockuser=1 where userid='$toid' and UserGroupID > 1");
			$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('$name','$membername','$locktype','$usertrueip',6,'".TIME_NOW."')");
			$locktype = $lang['forum.info3'];
			break;
		case 'lock_2':
			$locktype = $lang['forum.info2'].$lang['forum.info4'];
			$db->query("update {$dv}user set lockuser=2 where userid='$toid' and UserGroupID > 1");
			$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('$name','$membername','$locktype','$usertrueip',6,'".TIME_NOW."')");
			$locktype = $lang['forum.info4'];
			break;
		case 'lock_3':
			$locktype = $lang['forum.info2'].$lang['forum.info5'];
			$db->query("update {$dv}user set lockuser=0 where userid='$toid' and UserGroupID > 1");
			$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('$name','$membername','$locktype','$usertrueip',6,'".TIME_NOW."')");
			$locktype = $lang['forum.info5'];
			break;
		default:
			showmsg($lang['err.info6']);
			break;
	}
	$lang['suc.info1'] = str_replace('{$locktype}',$locktype,$lang['suc.info1']);
	showmsg($lang['suc.info1']);
}

function poweruser(){
	global $dv,$db,$action,$name,$toid,$membername,$usertrueip,$lang,$master,$boardmaster,$superboardmaster,$founduserper,$groupsetting,$checked;
	global $dowealth,$douserep,$dousercp,$douserpower,$title,$content,$imgurl,$usergroupid;
	$canlockuser = false;
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[43] == 1){
		$canlockuser = true;
	}
	if ($usergroupid > 3 && $groupsetting[43] == 1){
		$canlockuser = true;
	}
	if ($founduserper == true && $groupsetting[43] == 1){
		$canlockuser = true;
	}elseif ($founduserper == true && $groupsetting[43] == 0){
		$canlockuser = false;
	}
	if ($canlockuser == false){
		showmsg($lang['err.info5']);
	}
	if ($checked == 'yes'){
		if (!is_numeric($dowealth)){
			$dowealth = 0;
			$dowealthmsg = '';
		}else{
			if ($dowealth > 50 || $dowealth < -50){
				$dowealth = 0;
				$dowealthmsg = '';
			}else{
				$dowealthmsg = $lang['forum.info6'].$dowealth;
			}
		}
		if (!is_numeric($douserep)){
			$douserep = 0;
			$douserepmsg = '';
		}else{
			if ($douserep > 50 || $douserep < -50){
				$douserep = 0;
				$douserepmsg = '';
			}else{
				$douserepmsg = $lang['forum.info7'].$douserep;
			}
		}
		if (!is_numeric($dousercp)){
			$dousercp = 0;
			$dousercpmsg = '';
		}else{
			if ($dousercp > 50 || $dousercp < -50){
				$dousercp = 0;
				$dousercpmsg = '';
			}else{
				$dousercpmsg = $lang['forum.info8'].$dousercp;
			}
		}
		if (!is_numeric($douserpower)){
			$douserpower = 0;
			$douserpowermsg = '';
		}else{
			if ($douserpower > 5 || $douserpower < -5){
				$douserpower = 0;
				$douserpowermsg = '';
			}else{
				$douserpowermsg = $lang['forum.info9'].$douserpower;
			}
		}
		if ($dowealthmsg == '' && $douserepmsg == '' && $dousercpmsg == '' && $douserpowermsg == ''){
			$allmsg = $lang['forum.info10'];
		}else{
			$allmsg = $lang['forum.info2'].$dowealthmsg.$douserepmsg.$dousercpmsg.$douserpowermsg;
		}
		if ($title == '' && $content == ''){
			showmsg($lang['err.info7']);
		}
		$content = $lang['forum.info2'].$lang['forum.info11'].$title.$content. $lang['lang11'] .$allmsg;
		$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type,l_addtime) values ('$name','$membername','$content','$usertrueip',5,'".TIME_NOW."')");
		$db->query("update {$dv}user set userwealth=userwealth+{$dowealth},usercp=usercp+{$dousercp},userep=userep+{$douserep},userpower=userpower+{$douserpower} where userid='$toid'");
		$lang['suc.info1'] = str_replace('{$locktype}',$lang['suc.info2'],$lang['suc.info1']);
		$lang['suc.info1'] = $allmsg. '|||' .$lang['suc.info1'];
		showmsg($lang['suc.info1']);
	}else{
		require_once printout('admin_lockuser');
	}
}

function boardtodays($rootid,$tablename){//今日帖子数;
	global $lang,$dv,$db,$imgurl,$usergroupid;

	$time1 = strtotime(date('Y-m-d 00:00:00'));
	$time2 = $time1 + (24 * 3600 - 1);
	if(strpos($tablename,'argue')!==false){
		$today_num=$db->scalar("SELECT count(1) FROM ".$tablename." WHERE rootid='".$rootid."' and boardid!=444 and boardid!=777 AND (creation_date between ".$time1." and ".$time2.")");
	}
	else{
		$today_num=$db->scalar("SELECT count(1) FROM ".$tablename." WHERE rootid='".$rootid."' and boardid!=444 and boardid!=777 AND (dateandtime between ".$time1." and ".$time2.")");	
	}

	return ($today_num);
}
function deltopic(){
	global $dv,$db,$action,$name,$toid,$membername,$usertrueip,$lang,$master,$boardmaster,$superboardmaster,$founduserper,$usergroupid;
	global $deltopicdate,$_DCACHE,$groupsetting,$imgurl;
	$canlockuser = false;
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[29] == 1){
		$canlockuser = true;
	}
	if ($usergroupid > 3 && $groupsetting[29] == 1){
		$canlockuser = true;
	}
	if ($founduserper == true && $groupsetting[29] == 1){
		$canlockuser = true;
	}elseif ($founduserper == true && $groupsetting[29] == 0){
		$canlockuser = false;
	}
	if ($canlockuser == false){
		showmsg($lang['err.info5']);
	}
	if (!is_numeric($deltopicdate)){
		showmsg($lang['err.info6']);
	}
	$nowtime = TIME_NOW - ($deltopicdate * 24 * 3600);
	if( empty($_DCACHE['all_forums']) ){
		include_once CACHE_PATH.'board/board_all.php';
	}
	$ii = 0;
	$query=$db->query("SELECT topicid, child, boardid, posttable, dateandtime FROM {$dv}topic WHERE boardid NOT IN (444, 777) AND postuserid={$toid} AND dateandtime>'$nowtime' ORDER BY topicid");
	while($topics = $db->fetch_array($query)) {
		$todaynum =boardtodays($topics['topicid'],$topics['posttable']);
		$totalusetable = $topics['posttable'];
		$db->query("UPDATE {$dv}user SET userpost=(CASE WHEN (userpost-{$topics[child]})>1 THEN (userpost-{$topics[child]}-1) ELSE 0 END),usertopic=(CASE WHEN usertopic>0 THEN (usertopic-1) ELSE 0 END), userdel=(userdel+{$topics[child]}+1) WHERE userid={$toid}");
		$db->query("UPDATE {$dv}topic SET locktopic = boardid, boardid = 444 WHERE topicid={$topics[topicid]}");
		$db->query("UPDATE ".$db->escape_string($topics['posttable'])." SET boardid=444,locktopic='".$topics['boardid']."' WHERE rootid='".$topics['topicid']."' and forumtype=0");
		if ($_DCACHE['all_forums'][$topics['boardid']]['parentid'] == '0'){
			$updateboardid = $topics['boardid'];
		}else{
			$updateboardid = $_DCACHE['all_forums'][$topics['boardid']]['parentstr']. ',' .$topics['boardid'];
		}

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

		$db->query("update {$dv}board set postnum=(CASE WHEN postnum>({$topics[child]}+1) THEN (postnum-{$topics[child]}-1) ELSE 0 END),topicnum=(CASE WHEN topicnum>1 THEN (topicnum-1) ELSE 0 END),todaynum=(CASE WHEN todaynum>{$todaynum} THEN (todaynum-{$todaynum}) ELSE 0 END) where boardid in ({$updateboardid})");

		$db->query("update {$dv}setup set forum_todaynum=(CASE WHEN forum_todaynum>{$todaynum} THEN (forum_todaynum-{$todaynum}) ELSE 0 END),forum_postnum=(CASE WHEN forum_postnum>({$topics[child]}+1) THEN (forum_postnum-{$topics[child]}-1) ELSE 0 END),forum_topicnum=(CASE WHEN forum_topicnum>1 THEN (forum_topicnum-1) ELSE 0 END)");
		$ii++;

		//删除缓存
		del_static_topic($topics['boardid'],$topics['topicid']);
		del_static_boards($updateboardid);
	}
	if ($ii > 0){
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createAll();
		$mycache->createForumSetting();
		Create_index_forumnum();//20070523
	}
	$lang['suc.info3'] = str_replace('{$delnum}',$ii,$lang['suc.info3']);
	showmsg($lang['suc.info3']);
	exit;
}
function deluserreply(){
	global $dv,$db,$action,$name,$toid,$membername,$usertrueip,$lang,$master,$boardmaster,$superboardmaster,$founduserper;
	global $deltopicdate,$delbbs,$_DCACHE,$fsetting,$groupsetting,$imgurl,$usergroupid;
	$canlockuser = false;
	if (($master == true || $boardmaster == true || $superboardmaster == true) && $groupsetting[29] == 1){
		$canlockuser = true;
	}
	if ($usergroupid > 3 && $groupsetting[29] == 1){
		$canlockuser = true;
	}
	if ($founduserper == true && $groupsetting[29] == 1){
		$canlockuser = true;
	}elseif ($founduserper == true && $groupsetting[29] == 0){
		$canlockuser = false;
	}
	if ($canlockuser == false){
		showmsg($lang['err.info5']);
	}
	if (!is_numeric($deltopicdate)){
		showmsg($lang['err.info6']);
	}
	$nowtime = TIME_NOW - ($deltopicdate * 24 * 3600);
	if( empty($_DCACHE['all_forums']) ){
		include_once CACHE_PATH.'board/board_all.php';
	}
	$delbbs = substr($delbbs,0,8);
	$forum_user = explode(',',$fsetting['userpoint']);
	$ii = 0;
	$query=$db->query("SELECT announceid, rootid, boardid, dateandtime FROM {$delbbs} WHERE boardid NOT IN (444, 777) AND PostUserID = {$toid} AND NOT ParentID = 0 AND dateandtime>'".$nowtime."'");
	while($posts = $db->fetch_array($query)) {
		$db->query("UPDATE {$dv}user SET userpost = (CASE WHEN userpost>0 THEN userpost-1 ELSE 0 END), userdel = userdel + 1, userwealth = userwealth - {$forum_user[3]}, userep = userep - {$forum_user[8]}, usercp = usercp - {$forum_user[13]} WHERE userid = {$toid}");
		$db->query("UPDATE {$dv}topic SET child = (CASE WHEN child>0 THEN child-1 ELSE 0 END) WHERE topicid = {$posts[rootid]}");
		$db->query("UPDATE {$delbbs} SET locktopic =".intval($posts['boardid']).", boardid = 444 WHERE announceid={$posts[announceid]}");

		IsEndReply($posts['rootid'],$posts['announceid'],1);
		if (dateDiff('D',$posts['dateandtime'],$nowtime) == -1){
			$todaynum = 1;
		}else{
			$todaynum = 0;
		}
		if ($_DCACHE['all_forums'][$posts['boardid']]['parentid'] == '0'){
			$updateboardid = $posts['boardid'];
		}else{
			$updateboardid = $_DCACHE['all_forums'][$posts['boardid']]['parentstr']. ',' .$posts['boardid'];
		}
		$S= explode(',',$updateboardid);
		$updateboardid= "";
		$i= 0;
		while($i < count($S))
		{
			if($updateboardid == "")
				$updateboardid= "'".$S[$i]."'";
			else
				$updateboardid.= ",'".$S[$i]."'";
			$i++;
		}
		$db->query("UPDATE {$dv}board SET postnum = (CASE WHEN postnum > 1 THEN postnum - 1 ELSE 0 END), todaynum = (CASE WHEN todaynum >= {$todaynum} THEN todaynum - {$todaynum} ELSE 0 END) WHERE boardid IN ({$updateboardid})");
		$db->query("UPDATE {$dv}setup SET forum_todaynum = (CASE WHEN forum_todaynum >= {$todaynum} THEN forum_todaynum - {$todaynum} ELSE 0 END), forum_postnum = (CASE WHEN forum_postnum >= 1 THEN forum_postnum - 1 ELSE 0 END)");
		LastCount($posts['boardid'],$posts['rootid'],$posts['announceid'],2,$updateboardid);
		$ii++;

		//删除缓存
		del_static_topic($posts['boardid'],$posts['rootid']);
		del_static_boards($updateboardid);
	}
	if ($ii > 0){
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createAll();
		$mycache->createForumSetting();
		Create_index_forumnum();//20070523
	}
	$lang['suc.info3'] = str_replace('{$delnum}',$ii,$lang['suc.info3']);
	showmsg($lang['suc.info3']);
	exit;
}

function boardlist(){
	global $dv,$db,$action,$lang,$toid,$name,$membername,$master,$superboardmaster,$boardmaster,$adminuserper,$_DCACHE,$imgurl,$usergroupid;
	if ($adminuserper == false){
		showmsg($lang['err.info5']);
	}
	if( empty($_DCACHE['all_forums']) ){
		if( !@include_once CACHE_PATH.'board/board_all.php' ){
			return null;
		}
	}
	$FBoardMaster = $dispboard = false;
	$query=$db->query("select boardid,boardtype,parentid,parentstr,child,depth,boardmaster,boardid as fboardmaster,boardid as dispboard,boardid as isuserper from {$dv}board order by rootid,orders");
	while($bs = $db->fetch_array($query)){
		$i = 0;
		$boardsetid = $_DCACHE['all_forums']["{$bs['boardid']}"]['setting_id'];
		$reboard_setting = $_DCACHE['forum_settings']["{$boardsetid}"]['setting'];
		if ($bs['parentid'] > 0 && $bs['depth'] > 0){
			$query_1=$db->query("select boardmaster from {$dv}board where boardid in ({$bs[parentstr]}) order by orders");
			while ($tbs = $db->fetch_array($query_1)){
				if ($reboard_setting[2] == 1 && $FBoardMaster == false){
					if (strpos('|'.$tbs['boardmaster'].'|','|'.$membername.'|') !== false){
						$FBoardMaster = true;
					}
				}
				$i++;
				if($FBoardMaster !== false){break;}
				if($i > 9){break;}
			}
		}
		if ($boardmaster !== false && strpos('|'.$bs['boardmaster'].'|','|'.$membername.'|') == false){$dispboard = false;}
		if ($FBoardMaster !== false){$dispboard = true;}
		if ($master !== false || $superboardmaster !== false){$dispboard = true;}
		if ($dispboard !== false){
			$query_2=$db->query("select uc_userid from {$dv}useraccess where uc_boardid={$bs[boardid]} and uc_userid={$toid}");
			if ($us = $db->fetch_array($query_2)){
				$bs['isuserper'] = 1;
			}else{
				$bs['isuserper'] = 0;
			}
		}
		$bs['fboardmaster'] = $FBoardMaster;
		$bs['dispboard'] = $dispboard;
		$_bs[] = $bs;
		$FBoardMaster = false;
		$dispboard = false;
	}
	require_once printout('admin_lockuser');
}

function userboardpermission(){
	global $dv,$db,$action,$lang,$toid,$boardid,$name,$membername,$master,$superboardmaster,$boardmaster,$adminuserper,$fsetting,$imgurl,$usergroupid;
	if ($adminuserper == false){
		showmsg($lang['err.info5']);
	}
	$query=$db->query("select u.usergroupid,ug.title,u.username from {$dv}user u inner join {$dv}usergroups ug on u.usergroupid=ug.usergroupid where u.userid={$toid}");
	if ($us = $db->fetch_array($query)){
		$usergroupid = $us['usergroupid'];
		$usertitle = $us['title'];
		$membername = $us['username'];
	}else{
		showmsg($lang['err.info3']);
	}
	$query=$db->query("select boardtype from {$dv}board where boardid={$boardid}");
	if ($bs = $db->fetch_array($query)){
		$boardtype = $bs['boardtype'];
	}else{
		$boardtype = $lang['forum.info12'];
		$boardid = 0;
	}
	$foundgroup = $founduserpermission = $foundgrouppermission = false;
	$query=$db->query("select uc_setting from {$dv}useraccess where uc_boardid={$boardid} and uc_userid={$toid}");
	if ($gs = $db->fetch_array($query)){
		$regroupsetting = $gs['uc_setting'];
		$foundgroup = $founduserpermission = true;
	}
	if ($foundgroup !== true){
		$query=$db->query("select psetting from {$dv}boardpermission where boardid={$boardid} and groupid={$usergroupid}");
		if ($gs = $db->fetch_array($query)){
			$regroupsetting = $gs['psetting'];
			$foundgroup = $foundgrouppermission = true;
		}
	}
	if ($foundgroup !== true){
		$query=$db->query("select groupsetting from {$dv}usergroups where usergroupid={$usergroupid}");
		if ($gs = $db->fetch_array($query)){
			$regroupsetting = $gs['groupsetting'];
			$foundgroup = $foundgrouppermission = true;
		}else{
			showmsg($lang['err.info8']);
		}
	}
	$lang = load_lang($lang, 'grouppermission' );
	require_once printout('grouppermission');
	require_once printout('admin_lockuser');
}

function saveuserpermission(){
	global $dv,$db,$action,$lang,$toid,$boardid,$name,$membername,$master,$superboardmaster,$boardmaster,$founduserper,$adminuserper,$fsetting;
	global $isdefault,$usertrueip,$_DCACHE,$usergroupid;
	if ($adminuserper == false){
		showmsg($lang['err.info5']);
	}
	if( empty($_DCACHE['all_forums']) ){
		if( !@include_once CACHE_PATH.'board/board_all.php' ){
			return null;
		}
	}
	$fboardmaster = false;
	require_once printout('grouppermission');
	$myGroupSetting = getgrouppermission();
	if ($boardmaster == true && $founduserper !== true && $master !== true && $superboardmaster !== true){
		$query=$db->query("select boardmaster,parentid,depth,parentstr from {$dv}board where boardid={$boardid}");
		if ($bs = $db->fetch_array($query)){
			$boardsetid = $_DCACHE['all_forums']["{$boardid}"]['setting_id'];
			$reboard_setting = $_DCACHE['forum_settings']["{$boardsetid}"]['setting'];
			if ($bs['parentid'] > 0 && $bs['depth'] > 0){
				$query_1=$db->query("select boardmaster from {$dv}board where boardid in ({$bs['parentstr']}) order by orders");
				$i = 0;
				while($tbs = $db->fetch_array($query_1)){
					if ($reboard_setting[2] == 1 && $fboardmaster == false){
						if (strpos('|'.$tbs['boardmaster'].'|','|'.$membername.'|') !== false){
							$fboardmaster = true;
						}
					}
					$i++;
					if($fboardmaster !== false){break;}
					if($i > 9){break;}
				}
			}
			if ($fboardmaster !== true && strpos('|'.$bs['boardmaster'].'|','|'.$membername.'|') == false){showmsg($lang['err.info9']);}
		}else{
			showmsg($lang['err.info9']);
		}
	}
	if ($isdefault == 1){
		$db->query("delete from {$dv}useraccess where uc_boardid={$boardid} and uc_userid={$toid}");
		$foundsetting=$db->scalar("Select Count(*) from {$dv}useraccess where uc_boardid={$boardid} and uc_userid={$toid}");
		$query=$db->query("select isgroupsetting From {$dv}board Where boardid={$boardid}");
		$bs = $db->fetch_array($query);
		if ($bs['isgroupsetting'] == ''){
			$MyIsGroupSetting = '';
		}else{
			$IsGroupSetting = ',' .$bs['isgroupsetting']. ',';
			$IsGroupSetting = str_replace(',0,','',$IsGroupSetting);
			if ($foundsetting == '' || $foundsetting == 0){
				$IsGroupSetting = str_replace(',0_' .$toid,'',$IsGroupSetting);
			}
			$IsGroupSetting = str_replace(',,','',$IsGroupSetting);
			$IsGroupSetting = explode(',',$IsGroupSetting);
			for ($i=1; $i<count($IsGroupSetting)-1; $i++){
				if ($i == 1){
					$MyIsGroupSetting = $IsGroupSetting[$i];
				}else{
					$MyIsGroupSetting .= ',' .$IsGroupSetting[$i];
				}
			}
		}
		$db->query("update {$dv}board set isgroupsetting='{$MyIsGroupSetting}' Where boardid={$boardid}");
		//删除缓存
		del_static_board($boardid);
	}else{
		$query=$db->query("select uc_userid from {$dv}useraccess where uc_boardid={$boardid} and uc_userid={$toid}");
		if ($us = $db->fetch_array($query)){
			$db->query("update {$dv}useraccess set uc_setting='{$myGroupSetting}' where uc_boardid={$boardid} and uc_userid={$toid}");
		}else{
			$db->query("insert into {$dv}useraccess (uc_userid,uc_boardid,uc_setting) values ('{$toid}',{$boardid},'{$myGroupSetting}')");
		}
		$query=$db->query("select isgroupsetting From {$dv}board Where boardid={$boardid}");
		$bs = $db->fetch_array($query);
		if ($bs['isgroupsetting'] == ''){
			$MyIsGroupSetting = '0_' .$toid;
		}else{
			$IsGroupSetting = ',' .$bs['isgroupsetting']. ',';
			$IsGroupSetting = str_replace(',0,','',$IsGroupSetting);
			$IsGroupSetting = str_replace(',0_' .$toid,'',$IsGroupSetting);
			$IsGroupSetting .= ',0_' .$toid. ',';
			$IsGroupSetting = str_replace(',,','',$IsGroupSetting);
			$IsGroupSetting = explode(',',$IsGroupSetting);
			for ($i=1; $i<count($IsGroupSetting)-1; $i++){
				if ($i == 1){
					$MyIsGroupSetting = $IsGroupSetting[$i];
				}else{
					$MyIsGroupSetting .= ',' .$IsGroupSetting[$i];
				}
			}
		}
		$db->query("update {$dv}board set isgroupsetting='{$MyIsGroupSetting}' Where boardid={$boardid}");
		//删除缓存
		del_static_boards($boardid);
	}
	include_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createuserpermission();
	$db->query("insert into {$dv}log (l_touser,l_username,l_content,l_ip,l_type) values ('{$name}','{$membername}','{$lang[forum.info13]}','{$usertrueip}',5)");
	showmsg($lang['suc.info4']);
}

//更新指定论坛信息，取主题和最后回复作者及时间
//flag=1为根据主题ID判断，2为根据回复ID判断是否版面最后回复
function LastCount($mboardid,$topicid,$ireplyid,$flag,$s_updateboardid){
	global $dv,$db,$fsetting,$usergroupid;
	$lastcount = false;
	$query=$db->query("select lastpost from {$dv}board where boardid={$mboardid}");
	if ($bs = $db->fetch_array($query)){
		$lastpostinfo = explode('$',$bs['lastpost']);
		if($bs['lastpost'] == ''){return false;}
		if(count($lastpostinfo) == 8){
			if($flag == 1){
				if($lastpostinfo[6] == $topicid){
					$lastcount = true;
				}
			}elseif($flag == 2){
				if($lastpostinfo[1] == $ireplyid){
					$lastcount = true;
				}
			}
		}
	}
	if ($lastcount == true){
		$query=$db->query("select T.title,b.announceid,b.dateandtime,b.username,b.postuserid,b.rootid from {$fsetting[nowtablename]} b inner join {$dv}topic T on b.rootid=T.topicid where b.boardid={$mboardid} order by b.announceid desc Limit 1");
		$ps = $db->fetch_array($query);
		if ($ps['announceid'] == ''){
			$lasttopic = 'none';
			$lastrootid = 0;
			$lastposttime = TIME_NOW;
			$lastpostuser = 'none';
			$lastpostuserid = 0;
			$lastid = 0;
		}else{
			$body = $ps['title'];
			$body = str_replace('$','',$body);
			$lastrootid = $ps['announceid'];
			$lastposttime = $ps['dateandtime'];
			$lastpostuser = str_replace('$','',$ps['username']);
			$lasttopic = substr($body,0,20);
			$lastpostuserid = $ps['postuserid'];
			$lastid = $ps['rootid'];
		}
		$lastpost = $lastpostuser. '$' .$lastrootid. '$' .$lastposttime. '$' .$lasttopic. '$' .$lastpostinfo[4]. '$' .$lastpostuserid. '$' .$lastid. '$' .$mboardid;
		$splitboardid = explode(',',$s_updateboardid);
		for ($i=0; $i<count($splitboardid); $i++){
			$query=$db->query("select lastpost from {$dv}board where boardid={$splitboardid[$i]}");
			$bs = $db->fetch_array($query);
			if ($bs['lastpost'] != ''){
				$splitlastpost = explode('$',$bs['lastpost']);
				if (count($splitlastpost) == 8 && $lastrootid != $splitlastpost[1]){
					$db->query("update {$dv}board set lastpost='$lastpost' where boardid={$splitboardid[$i]}");
				}
			}
		}
	}
}

//删除回复过程中判断该回复是否最后发言，如果是则更新主题LastPost数据并回复-1
//入口：
//TopicID--主题ID
//iReplyID--回复ID
//isUpdate--是否更新时间
//出口：isEndReply=true/false
function IsEndReply($topicid,$ireplyid,$isupdate){
	global $dv,$db,$usergroupid;
	$isendreply = false;
	$query=$db->query("select lastpost,posttable from {$dv}topic where topicid={$topicid}");
	if ($ts = $db->fetch_array($query)){
		$lastpostinfo = explode('$',$ts['lastpost']);
		$itotalusetable = $ts['posttable'];
		if($itotalusetable == ''){return false;}
		if(count($lastpostinfo) == 8){
			if($lastpostinfo[1] == $ireplyid){
				$isendreply = true;
			}
		}
	}
	if ($isendreply == true && $isupdate == 1){
		$query=$db->query("select topic,body,announceid,dateandtime,username,postuserid,rootid,boardid from {$itotalusetable} where (not boardid in (444,777)) and rootid={$topicid} order by announceid desc Limit 1");
		$ps = $db->fetch_array($query);
		if ($ps['announceid'] == ''){
			$lasttopic = 'none';
			$lastrootid = 0;
			$lastposttime = TIME_NOW;
			$lastpostuser = 'none';
			$lastpostuserid = 0;
			$lastid = 0;
			$iboardid = 0;
		}else{
			$body = $ps['body'];
			$body = str_replace('$','',$body);
			$lastrootid = $ps['announceid'];
			$lastposttime = $ps['dateandtime'];
			$lastpostuser = str_replace('$','',$ps['username']);
			$lasttopic = substr($body,0,20);
			$lastpostuserid = $ps['postuserid'];
			$lastid = $ps['rootid'];
			$iboardid = $ps['boardid'];
		}
		$lastpost = $lastpostuser. '$' .$lastrootid. '$' .$lastposttime. '$' .$lasttopic. '$' .$lastpostinfo[4]. '$' .$lastpostuserid. '$' .$lastid. '$' .$iboardid;
		$db->query("update {$dv}topic set lastpost='$lastpost',child=(CASE WHEN child>0 THEN child-1 ELSE 0 END),lastposttime='$lastposttime' where topicid={$topicid}");
	}elseif($isupdate == 1 && $isendreply == false){
		$db->query("update {$dv}topic set child=(CASE WHEN child>0 THEN child-1 ELSE 0 END) where topicid={$topicid}");
	}
	return $isendreply;
}

footer();
?>