<?php
/*
	Dvbbs.php  - Version: 1.0.0
	Web: http://www.dvbbs.net
	Copyright: 2000-2006 Dvbbs.Net
	script file:accesstopic.php
	Script Written by 
	Function of the Page:论坛审核
	*/
define('THIS_SCRIPT', 'accesstopic');
require_once 'global.php';
require_once INC_PATH.'dv_fsettings.global.php';
include_once CACHE_PATH.'board/board_all.php';

$lang = load_lang($lang, THIS_SCRIPT);
$stats = $fsetting['forumname'].' - '.$lang['tpl.pagetitle'];
$arrNavMenu = array(
	0=>array('linktitle'=>$lang['tpl.pagetitle'],'linkurl'=>'')
);


$adminlocktopic = false;
if ((true === $master OR true === $superboardmaster OR true === $boardmaster) AND (1 == $groupsetting[36])) {
	$adminlocktopic = true;
}

if (1 == $groupsetting[36] AND $usergroupid > 3) {
	$adminlocktopic = true;
}
if (true === $founduserper AND 1 == $groupsetting[36]) {
	$adminlocktopic = true;
} elseif (true === $founduserper AND 0 == $groupsetting[36]) {
	$adminlocktopic = false;
}
if (false === $adminlocktopic) {
	head(1,0,0,$arrNavMenu);
	showmsg($lang['tpl.error0']);
	exit;
}

$action = strtolower($action);

if (!ChkPost()) {
	header('Location: index.php');
	exit;
}

$isnav = 1;
switch ($_GET['action']) {
	case 'manage'://批量审核
		if (!empty($arrayid) AND is_array($arrayid)) {
			$manageboardid = array();
			foreach ($arrayid AS $index => $topicid) {
				if (is_numeric($topicid) AND $topicid > 0) {
					$replyid = $arrayreplyid[$index];
					$passed = ${"pass_{$topicid}_{$replyid}"};
					if (!is_numeric($replyid) OR $replyid <= 0 ) {
						$replyid = $topicid;
					}
					//检查主题表是否有记录，并且取得其主帖状态
					$query = $db->query("SELECT boardid,locktopic,posttable,isvote,pollid,lastposttime FROM {$dv}topic WHERE topicid='{$topicid}'");
					if ($query AND $db->num_rows($query) > 0) {
						$today = 0;
						$topicinfo = $db->fetch_array($query);
						$db->free_result($query);
						$posttable = $db->escape_string(trim($topicinfo['posttable']));

						//删除缓存
						if($topicinfo['boardid'] == '444' || $topicinfo['boardid'] == '777')
							del_static_topic($topicinfo['locktopic'],$topicid);
						else
							del_static_topic($topicinfo['boardid'],$topicid);

						if ('1' === $passed) {//通过审核
							$postinfo = false;
							if ($replyid == $topicid) {
								if ($query = $db->query("SELECT dateandtime,username,postuserid,locktopic,username,announceid,topic,rootid,boardid,signflag,SUBSTRING(body, 1, 20) AS body FROM {$posttable} /*!32312 USE INDEX(dispbbs)*/ WHERE boardid=777 AND rootid='{$topicid}' AND parentid=0 and forumtype=0", array('type' => 'SILENT'))) {
									if (false !== ($postinfo = $db->fetch_array($query))) {
										$db->free_result($query);
									}
								}
								if (false !== $postinfo) {
									$locktopic = $postinfo['locktopic'];

									if($postinfo['topic'] == '')
										$tempTopic= $postinfo['body'];
									else
										$tempTopic= $postinfo['topic'];
									if( $postinfo['signflag'] == 2 ){
										$lastpost = $lang['tpl.string82'].'$'.$postinfo['announceid'].'$'.time().'$'.strip_tags(str_replace('$','&#36;',$tempTopic)).'$$$'.$postinfo['rootid'].'$'.$locktopic;
									}
									else{
										$lastpost = str_replace('$','&#36;',$postinfo['username']).'$'.$postinfo['announceid'].'$'.time().'$'.strip_tags(str_replace('$','&#36;',$tempTopic)).'$$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									}
									$boardinfo= $_DCACHE['all_forums'][$postinfo['boardid']];
									if( intval($boardinfo['checkout']) == 1 ){
										$LastPost_1 = $lang['tpl.string83'].$postinfo['announceid'].'$'.time().$lang['tpl.string84'].'$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									} elseif( $signflag == 2 ){
										$LastPost_1 = $lang['tpl.string82'].'$'.$postinfo['announceid'].'$'.time().'$'.$tempTopic.'$$'.' $'.$postinfo['rootid'].'$'.$locktopic;
									} else{
										$LastPost_1 = str_replace('$','',$postinfo['username']).'$'.$postinfo['announceid'].'$'.time().'$'.$tempTopic.'$$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									}
									$LastPost_1 = ChkBadWords(reUBBCode(str_replace("'",'',$LastPost_1)));

									$today = (0 == dateDiff('d', $topicinfo['dateandtime'], TIME_NOW) ? 1 : 0);
									$manageresult[$index] = array('topic' => 1, 'child' => 0, 'today' => $today, 'boardid' => $locktopic, 'stats' => $lang['tpl.str5']);
									$manageboardid["{$locktopic}"][] =& $manageresult[$index];
									$db->query("UPDATE {$posttable} /*!32312 USE INDEX(dispbbs)*/ SET boardid='{$locktopic}', locktopic=0 WHERE boardid=777 AND rootid='{$topicid}' AND parentid=0 and forumtype=0", array('type' => 'SILENT'));
									$db->query("UPDATE {$dv}topic /*!32312 USE INDEX(PRIMARY)*/ SET boardid='{$locktopic}', locktopic=0,lastpost='{$lastpost}',lastposttime=".time()." WHERE topicid='{$topicid}' AND boardid=777", array('type' => 'SILENT'));//edit 2007-4-10
									updatePostUser($postinfo['postuserid'], $locktopic, 1);
									
									if (0 != $postinfo['postuserid']) {
										sendMessenger($postinfo['postuserid'], $lang['tpl.sendmsg.str1'], str_replace(array('{$TPL_boardid}', '{$TPL_id}'), array($locktopic, $topicid), $lang['tpl.sendmsg.str2']));
									}
								} else {
									$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.error2']);
								}
							} else {
								if ($query = $db->query("SELECT dateandtime,postuserid,parentid,locktopic,username,announceid,topic,rootid,boardid,signflag,SUBSTRING(body, 1, 20) AS body FROM {$posttable} /*!32312 USE INDEX(PRIMARY) */ WHERE announceid='{$replyid}' AND rootid='{$topicid}' AND boardid=777", array('type' => 'SILENT'))) {
									if (false !== ($postinfo = $db->fetch_array($query))) {
										$db->free_result($query);
									}
								}
								if (false !== $postinfo) {
									$locktopic = $postinfo['locktopic'];

									if($postinfo['topic'] == '')
										$tempTopic= $postinfo['body'];
									else
										$tempTopic= $postinfo['topic'];
									if( $postinfo['signflag'] == 2 ){
										$lastpost = $lang['tpl.string82'].'$'.$postinfo['announceid'].'$'.time().'$'.strip_tags(str_replace('$','&#36;',$tempTopic)).'$$$'.$postinfo['rootid'].'$'.$locktopic;
									}
									else{
										$lastpost = str_replace('$','&#36;',$postinfo['username']).'$'.$postinfo['announceid'].'$'.time().'$'.strip_tags(str_replace('$','&#36;',$tempTopic)).'$$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									}
									$boardinfo= $_DCACHE['all_forums'][$postinfo['boardid']];
									if( intval($boardinfo['checkout']) == 1 ){
										$LastPost_1 = $lang['tpl.string83'].$postinfo['announceid'].'$'.time().$lang['tpl.string84'].'$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									} elseif( $signflag == 2 ){
										$LastPost_1 = $lang['tpl.string82'].'$'.$postinfo['announceid'].'$'.time().'$'.$tempTopic.'$$'.' $'.$postinfo['rootid'].'$'.$locktopic;
									} else{
										$LastPost_1 = str_replace('$','',$postinfo['username']).'$'.$postinfo['announceid'].'$'.time().'$'.$tempTopic.'$$'.$postinfo['postuserid'].'$'.$postinfo['rootid'].'$'.$locktopic;
									}
									$LastPost_1 = ChkBadWords(reUBBCode(str_replace("'",'',$LastPost_1)));

									$today = (0 == dateDiff('d', $topicinfo['dateandtime'], TIME_NOW) ? 1 : 0);
									$manageresult[$index] = array('topic' => 1, 'child' => 0, 'today' => $today, 'boardid' => $locktopic, 'stats' => $lang['tpl.str5']);
									$manageboardid["{$locktopic}"][] =& $manageresult[$index];
									if (0 == $postinfo['parentid']) {
										$db->query("UPDATE {$posttable} /*!32312 USE INDEX(dispbbs) */ SET boardid='{$locktopic}', locktopic=0 WHERE boardid=777 AND rootid='{$topicid}' AND parentid=0 and forumtype=0");
										$db->query("UPDATE {$dv}topic /*!32312 USE INDEX(PRIMARY)*/ SET boardid='{$locktopic}', locktopic=0,lastpost='{$lastpost}',lastposttime=".time()." WHERE topicid='{$topicid}'");//edit 2007-4-10
										updatePostUser($postinfo['postuserid'], $locktopic, 1);
										
										if ('' != $postinfo['username']) {
											sendMessenger($postinfo['username'], $lang['tpl.sendmsg.str1'], str_replace(array('{$TPL_boardid}', '{$TPL_id}'), array($locktopic, $topicid), $lang['tpl.sendmsg.str2']));
										}
									} else {
										$db->query("UPDATE {$posttable} /*!32312 USE INDEX(PRIMARY) */ SET boardid='{$locktopic}', locktopic=0 WHERE announceid='{$replyid}' AND rootid='{$topicid}'");
										$db->query("UPDATE {$dv}topic /*!32312 USE INDEX(PRIMARY)*/ SET boardid='{$locktopic}', locktopic=0, child=(child+1),lastpost='{$lastpost}',lastposttime=".time()." WHERE topicid='{$topicid}'");//edit 2007-4-10
										updatePostUser($postinfo['postuserid'], $locktopic, 0);
										
										if ('' != $postinfo['username']) {
											sendMessenger($postinfo['username'], $lang['tpl.sendmsg.str1'], str_replace(array('{$TPL_boardid}', '{$TPL_id}'), array($locktopic, $topicid), $lang['tpl.sendmsg.str2']));
										}
									}
								} else {
									$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.error2']);
								}
							}
						} elseif ('0' === $passed) {//删除
							if ($replyid == $topicid) {
								$postuserid = $db->scalar("SELECT username FROM {$posttable} WHERE rootid='{$topicid}'");
								if ($postuserid != '') {
									sendMessenger($postuserid, $lang['tpl.sendmsg.str1'], $lang['tpl.sendmsg.str3']);
								}
								if (1 == $topicinfo['isvote']) {
									$db->query("DELETE FROM {$dv}vote WHERE voteid='{$topicinfo['pollid']}'");
								}
								$db->query("DELETE FROM {$posttable} WHERE rootid='{$topicid}' and forumtype=0");
								$db->query("DELETE FROM {$dv}topic WHERE topicid='{$topicid}'");
							} else {
								$postinfo = false;
								if ($query = $db->query("SELECT parentid,username, postuserid FROM {$posttable} /*!32312 USE INDEX(PRIMARY)*/ WHERE announceid='{$replyid}' AND rootid='{$topicid}'", array('type' => 'SILENT'))) {
									if ($postinfo = $db->fetch_array($query)) {
										$db->free_result($query);
									}
								}
								if (false !== $postinfo) {
									if ($postinfo['username'] != '') {
										sendMessenger($postinfo['username'], $lang['tpl.sendmsg.str1'], $lang['tpl.sendmsg.str3']);
									}
									if ($postinfo['parentid'] != 0) {
										$db->query("DELETE FROM {$posttable} WHERE announceid='{$replyid}' AND rootid='{$topicid}'");
									} else {
										$db->query("DELETE FROM {$posttable} WHERE rootid='{$topicid}' and forumtype=0");
										$db->query("DELETE FROM {$dv}topic WHERE topicid='{$topicid}'");
										if (1 == $topicinfo['isvote']) {
											$db->query("DELETE FROM {$dv}vote WHERE voteid='{$topicinfo['pollid']}'");
										}
									}
								} else {
									//for other
								}
							}
							//清除上传附件
							$db->query("UPDATE {$dv}upfile SET f_flag=4 WHERE f_announceid='{$topicid}|{$replyid}'");
							$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.str6']);
							require_once INC_PATH.'BoardCache.class.php';
							Create_index_forumnum();//20070523
						} else {
							$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.str7']);
						}
					} else {
						$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.error3']);
					}
					$manageresult[$index]['rootid'] = $topicid;
					$manageresult[$index]['announceid'] = $replyid;
				} else {
					$manageresult[$index] = array('topic' => 0, 'child' => 0, 'today' => 0, 'stats' => $lang['tpl.error4']);
				}
			}
		}
		$allpost = 0;
		$alltopic = 0;
		$alltoday = 0;
		require_once CACHE_PATH.'board/board_all.php';
		if (!empty($_DCACHE['all_forums']) AND is_array($_DCACHE['all_forums'])) {
			foreach ($_DCACHE['all_forums'] AS $currboardid => $val) {
				if (!empty($manageboardid["{$currboardid}"]) AND is_array($manageboardid["{$currboardid}"])) {
					$topic = $child = $today = 0;
					foreach ($manageboardid["{$currboardid}"] AS $mgmresult) {
						$topic += $mgmresult['topic'];
						$child += $mgmresult['child'];
						$today += $mgmresult['today'];
					}
					if ($topic + $child > 0) {
						$alltopic += $topic;
						$allpost += $topic + $child;
						$alltoday += $today;
						updateBoardInfoAndCache($currboardid, $topic, $child, $today, $LastPost_1);
					}
				}
			}
		}
		if ($allpost > 0 OR $alltopic > 0 OR $alltoday > 0) {
			$db->query("UPDATE {$dv}setup SET forum_postnum=forum_postnum+'{$allpost}', forum_topicnum=forum_topicnum+'{$alltopic}', forum_todaynum=forum_todaynum+'{$alltoday}'");
		//Start 重新生成论坛总设置缓存文件
			require_once CONFIG_PATH.'BoardCache.class.php';
			$boardcache =& new BoardCache($db);
			$boardcache->createForumSetting();
			unset($boardcache);
		//End 重新生成论坛总设置缓存文件
		}
		tolog($lang['tpl.str4']);
		break;
	case 'view'://查看单个审核帖
		if (!is_numeric($id) OR $id <= 0) {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['tpl.error5']);
			exit;
		} else {
			$topicid = trim($id);
		}
		if (!is_numeric($replyid) OR $replyid <= 0) {
			$replyid = 0;
		}
		$topicinfo = false;
		if ($query = $db->query("SELECT posttable,boardid FROM {$dv}topic WHERE topicid='{$topicid}'")) {
			$topicinfo = $db->fetch_array($query);
		}
		if (false === $topicinfo) {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['tpl.error6']);
			exit;
		} else {
			$db->free_result($query);
			$posttable = $db->escape_string($topicinfo['posttable']);
			if (0 == $replyid) {
				$stmt = "SELECT announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype FROM {$posttable} /*!32312 USE INDEX(dispbbs) */ WHERE boardid=777 AND rootid='{$topicid}' AND parentid=0 and forumtype=0";
			} else {
				$stmt = "SELECT announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype FROM {$posttable} /*!32312 USE INDEX(PRIMARY) */ WHERE announceid='{$replyid}' AND boardid=777 AND rootid='{$topicid}'";
			}
			$postinfo = false;
			if ($query = $db->query($stmt, array('pageSize' => 1))) {
				$postinfo = $db->fetch_array($query);
			}
			if (false === $postinfo) {
				head(1,0,0,$arrNavMenu);
				showmsg($lang['tpl.error7']);
				exit;
			}
		}
		unset($arrNavMenu);
		$arrNavMenu = null;
		$arrNavMenu[0]['linktitle'] = $lang['tpl.pagetitle3'];
		break;
	case 'info'://查看当前审核规则
		unset($arrNavMenu);
		$arrNavMenu = null;
		$arrNavMenu[0]['linktitle'] = $lang['tpl.pagetitle1'];
		break;
	case 'add_exempt_user'://豁免审核用户  addnocheck
		unset($arrNavMenu);
		$arrNavMenu = null;
		$arrNavMenu[0]['linktitle'] = $lang['tpl.pagetitle2'];
		$exemptusername = trim($_GET['username']);
	//Start 添加用户豁免列表
		if (!empty($exemptusername)) {
			if ($destboardid > 0 AND ($usergrpid = getUserGroupIdByName($exemptusername)) > 0) {
				if ($settingsid = $db->scalar("SELECT audit_settings_id FROM {$dv}board WHERE boardid='{$destboardid}'")) {
					if ($settings =& getSettingsById($settingsid)) {
						$settings['exempt_user_list'][settingsHashCode($exemptusername)] = $exemptusername;
						updateSettingsById($settingsid,$settings);
					}
				}
			}
		}
	//End 添加用户豁免列表
	//Start 从豁免用户列表中删除usergroupid为零的用户
		if ($allsettings =& getAllAccessSettings()) {
			foreach ($allsettings AS $dbinfo) {
				if ($settings = unserialize($dbinfo['setting'])) {
					if (!empty($settings['exempt_user_list'])) {
						foreach ($settings['exempt_user_list'] AS $key => $currusername) {
							$usergrpid = getUserGroupIdByName($currusername);
							if ($usergrpid <= 0) {
								unset($settings['exempt_user_list'][$key]);
								updateSettingsById($dbinfo['id'],$settings);
							}
						}
					}
				}
			}
		}
	//End 从豁免用户列表中删除usergroupid为零的用户
		$db->query("INSERT INTO {$dv}log(l_AnnounceID,l_BoardID,l_touser,l_username,l_content,l_ip,l_type) VALUES(0, '{$boardid}', '".$db->escape_string($lang['tpl.str8'])."', '{$membername}', '".$db->escape_string($lang['tpl.str9']).$exemptusername."', '{$usertrueip}', 3)");
		require_once INC_PATH.'BoardCache.class.php';
		$objcache = new BoardCache($db);
		$objcache->createAll();
		break;
	case 'unlock'://解除固封
	case 'unlock_topic':
		unset($arrNavMenu);
		$arrNavMenu = null;
		$arrNavMenu[0]['linktitle'] = $lang['tpl.pagetitle4'];
		if (!is_numeric($id) OR $id <= 0) {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['tpl.error5']);
			exit;
		} else {
			$topicid = $id;
		}
		if (!is_numeric($replyid) OR $replyid <= 0) {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['tpl.error5']);
			exit;
		}
		if (false === ($posttable = $db->scalar("SELECT posttable FROM {$dv}topic WHERE topicid='{$topicid}'"))) {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['tpl.error6']);
			exit;
		} else {
			$posttable = $db->escape_string($posttable);
		}
		$db->query("UPDATE {$posttable} /*!32312 USE INDEX(PRIMARY)*/ SET locktopic=0 WHERE announceid='{$replyid}' AND rootid='{$topicid}' AND locktopic=3");
		require_once INC_PATH.'BoardCache.class.php';
		Create_index_forumnum();//20070523
		$db->query("INSERT INTO {$dv}log(l_AnnounceID,l_BoardID,l_touser,l_username,l_content,l_ip,l_type) VALUES(0, '{$boardid}', '".$db->escape_string($lang['tpl.pagetitle4'])."', '{$membername}', '".$db->escape_string(str_replace(array('{$TPL_topicid}', '{$TPL_announceid}'), array($topicid, $replyid), $lang['tpl.str10']))."', '{$usertrueip}', 3)");

		//删除缓存
		$delboardid = $db->scalar("SELECT boardid FROM {$dv}topic WHERE topicid='{$topicid}'");
		del_static_topic($delboardid,$topicid);
		break;
	default://帖子审核
		if ($boardid > 0) {
			unset($arrNavMenu);
			$arrNavMenu = null;
			$gettitle = $lang['tpl.str0'];
			$isnav = 0;
			$substmt = " AND locktopic='{$boardid}'";
		}
		break;
}
head($isnav,0,0,$arrNavMenu);

require printout(THIS_SCRIPT);

footer();

function &getTableList($opts = array())
{
	global $dv, $db, $boardid;
	static $tablelist = array();
	if (!is_array($opts)) {
		$opts = array();
	}
	if (!isset($opts['getcount'])) {
		$opts['getcount'] = false;
	}
	if (!isset($opts['reload'])) {
		$opts['reload'] = false;
	}
	if (true === $opts['reload']) {
		unset($tablelist);
		$tablelist = array();
	}
	if (empty($tablelist)) {
		if ($query = $db->query("SELECT id, tablename, tabletype FROM {$dv}tablelist")) {
			while ($rs = $db->fetch_array($query)) {
				$rs['RecordCount'] = 0;
				$tablelist['TableList_IdIndex']["{$rs['id']}"] = $rs;
				$tablelist['TableList_NameIndex']["{$rs['tablename']}"] =& $tablelist['TableList_IdIndex']["{$rs['id']}"];
			}
		}
		if (true === $opts['getcount'] AND !empty($tablelist)) {
			if ($boardid > 0 ) {
				$substmt = " AND locktopic='{$boardid}'";
			} else {
				$substmt = '';
			}
			$tablelist['count'] = 0;
			$tablelist['boardid'] = $boardid;
			$tablelist['action'] = $GLOBALS['action'];
			$tablelist['master'] = $GLOBALS['master'];
			$tablelist['Topic_RecordCount'] = $db->scalar("SELECT COUNT(1) FROM {$dv}topic /*!32312 USE INDEX(audit_list)*/ WHERE boardid=777 {$substmt}");
			$tablelist['count'] += $tablelist['Topic_RecordCount'];
			foreach ($tablelist['TableList_NameIndex'] AS $tablename => $table) {
				$tablelist['TableList_NameIndex'][$tablename]['RecordCount'] = $db->scalar("SELECT COUNT(1) FROM ".$db->escape_string($tablename)." /*!32312 USE INDEX(audit_list)*/ WHERE boardid=777 {$substmt}");
				$tablelist['count'] += $tablelist['TableList_NameIndex'][$tablename]['RecordCount'];
			}
		}
	}
	return $tablelist;
}

function &getAuditList()
{
	global $dv, $db, $boardid, $fsetting;
	static $auditlist = array();
	if (!empty($auditlist)) {
		return $auditlist;
	}
	$pagesize = 30;
	if (!is_numeric($GLOBALS['tableid']) OR ($GLOBALS['tableid'] < 0)) {
		$tableid = 0;
		$posttable = $fsetting['nowtablename'];
	} else {
		$tableid = intval($GLOBALS['tableid']);
		$posttable = "{$dv}topic";
	}
	$keyword = trim($GLOBALS['keyword']);
	$tablelist =& getTableList(array('getcount' => true));
	if (0 !== $tableid) {
		if (isset($tablelist['TableList_IdIndex']["{$tableid}"])) {
			$posttable = $tablelist['TableList_IdIndex']["{$tableid}"]['tablename'];
		}
	} else {
		if (isset($tablelist['TableList_NameIndex']["{$posttable}"])) {
			$tableid = $tablelist['TableList_NameIndex']["{$posttable}"]['id'];
		}
	}
	$tablelist['tableid'] = $tableid;
	$tablelist['keyword'] = $keyword;
	$tablelist['pagesize'] = $pagesize;
	$tablelist['posttable'] = $posttable;

	$page = (!is_numeric($GLOBALS['page']) OR $GLOBALS['page'] < 1 ? 1 : $GLOBALS['page']);
	$tablelist['page'] = $page;
	$auditlist = array('PageSize' => $pagesize, 'AbsolutePage' => $page, 'RecordCount' => 0, 'PageCount' => 0, 'ResultSet' => false);

	$wherestmt = ' WHERE boardid=777 '.($boardid > 0 ? " AND locktopic='{$boardid}'" : '');
	$orderbystmt = '';
	if (0 == $tableid) {
		$posttable = "{$dv}topic";	//	强制指定 $posttable 为 dv_topic
		$fldlist = ' topicid AS id, title AS topic, locktopic AS bid, postusername AS username, postuserid AS userid, posttable, dateandtime, 0 AS replyid ';
		if ('' !== $keyword) {
			$wherestmt .= " AND (title LIKE '%{$keyword}%' OR postusername='{$keyword}')";
		}
		$orderbystmt = ' ORDER BY topicid ';
	} else {
		$fldlist = ' rootid AS id, topic, SUBSTRING(body, 1, 50) AS body, locktopic AS bid, username, postuserid AS userid, announceid AS replyid, dateandtime, parentid ';
		if ('' !== $keyword) {
			$wherestmt .= " AND (topic LIKE '%{$keyword}%' OR username='{$keyword}')";
		}
		$orderbystmt = ' ORDER BY announceid ';
	}
	$countstmt = "SELECT COUNT(1) FROM ".$db->escape_string($posttable)." {$wherestmt}";
	$auditlist['RecordCount'] = $db->scalar($countstmt);
	if ($auditlist['RecordCount'] > 0) {
		$auditlist['PageCount'] = ceil((float)$auditlist['RecordCount'] / (float)$auditlist['PageSize']);
		if ($auditlist['AbsolutePage'] > $auditlist['PageCount']) {
			$auditlist['AbsolutePage'] = $auditlist['PageCount'];
			$tablelist['page'] = $auditlist['AbsolutePage'];
		}
		$stmt = "SELECT {$fldlist} FROM ".$db->escape_string($posttable)." {$wherestmt} {$orderbystmt}";
		$auditlist['ResultSet'] =& $db->getResultSet($stmt, array('absolutePage' => $auditlist['AbsolutePage'], 'pageSize' => $auditlist['PageSize']));
	}
	return $auditlist;
}

function updatePostUser($arguserid, $argpostboardid, $argistopic)//更新用户发帖数,积分
{
	global $dv, $db;
	static $boardinfo = array();
	if (!isset($boardinfo["{$argpostboardid}"])) {
		global $_DCACHE;
		include_once CACHE_PATH.'board/board_all.php';
		$boardinfo["{$argpostboardid}"] =& $_DCACHE['all_forums']["{$postboardid}"];
	}
	$forumuser = explode(',', $boardinfo["{$argpostboardid}"]['board_user']);
	if (1 == $argistopic) {
		$db->query("UPDATE {$dv}user SET userpost=userpost+1, usertopic=usertopic+1, userwealth=userwealth+'{$forumuser[1]}', userep=userep+'{$forumuser[6]}', usercp=usercp+'{$forumuser[11]}' WHERE userid='{$arguserid}'");
	} else {
		$db->query("UPDATE {$dv}user SET userpost=userpost+1, userwealth=userwealth+'{$forumuser[2]}', userep=userep+'{$forumuser[7]}', usercp=usercp+'{$forumuser[12]}' WHERE userid='{$arguserid}'");
	}
}

function sendMessenger($argtouserid, $argsenduser, $argmessangertext)
{
	global $dv, $db;
	$db->query("INSERT INTO {$dv}message(sender, incept, delr, issend, title, content) VALUES('".$db->escape_string($argsenduser)."', '".$argtouserid."', 0, 1, '".$db->escape_string($argsenduser)."', '".$db->escape_string($argmessangertext)."')");
}

function tolog($arginfo)
{
	global $dv, $db, $boardid, $lang, $membername, $usertrueip;
	$db->query("INSERT INTO {$dv}log(l_AnnounceID,l_BoardID,l_touser,l_username,l_content,l_ip,l_type) VALUES(0, '{$boardid}', '{$lang['tpl.str3']}', '{$membername}', '".$db->escape_string($arginfo)."', '{$usertrueip}', 3)");
}



function updateBoardInfoAndCache($argboardid,$argtopic,$argchild,$argtoday,$LastPost_1)
{
	global $dv, $db, $_DCACHE;
	$parentstr = $_DCACHE['all_forums']["{$argboardid}"]['parentstr'];
	if ($parentstr != 0) {
		$updateboardid = "{$parentstr},{$argboardid}";
	} else {
		$updateboardid = $argboardid;
	}
	$db->query("UPDATE {$dv}board SET postnum=postnum+'".($argtopic + $argchild)."', topicnum=topicnum+'{$argtopic}', todaynum=todaynum+'{$argtoday}',lastpost='{$LastPost_1}' WHERE boardid IN({$updateboardid})");
	//删除缓存
	del_static_boards($updateboardid);
}


function getUserGroupIdByName($argusername, $useescape = true)
{
	global $dv, $db;
	static $groupid = array();
	if (empty($groupid["{$argusername}"])) {
		if (!$useescape) {
			$username = $db->escape_string($argusername);
		} else {
			$username = $argusername;
		}
		$groupid["{$argusername}"] = $db->scalar("SELECT usergroupid FROM {$dv}user WHERE username='{$username}'");
		if (empty($groupid["{$argusername}"])) {
			$groupid["{$argusername}"] = 0;
		}
	}
	return $groupid["{$argusername}"];
}

function reUBBCode(&$strContent){
	if ('' == $strContent) {
		return '';
	}
	$strContent = str_replace('&nbsp;', '', $strContent);
	static $st_arr_pattern, $st_arr_repl;
	if (!isset($st_arr_pattern)) {
		$st_arr_pattern = array(0=>'/\[QUOTE\].*\[\/QUOTE\]/is', 1=>'/\[point\s*=\s*[0-9]*\].*\[\/point\]/is', 2=>'/\[post\s*=\s*[0-9]*\].*\[\/post\]/is', 3=>'/\[power\s*=\s*[0-9]*\].*\[\/power\]/is', 4=>'/\[usercp\s*=\s*[0-9]*\].*\[\/usercp\]/is', 5=>'/\[money\s*=\s*[0-9]*\].*\[\/money\]/is', 6=>'/\[replyview\].*\[\/replyview\]/is', 7=>'/\[usemoney\s*=\s*[0-9]*\].*\[\/usemoney\]/is', 8=>'/<I><\/I>/i');
		$st_arr_repl = array(0=>'', 1=>'&nbsp;', 2=>'&nbsp;', 3=>'&nbsp;', 4=>'&nbsp;', 5=>'&nbsp;', 6=>'&nbsp;', 7=>'&nbsp;', 8=>'');
	}
	return str_replace(array("\r", "\n", chr(32), chr(9), '<'), array('', '', ' ', '&lt;'), strip_tags(preg_replace($st_arr_pattern, $st_arr_repl, $strContent)));
}//end function reUBBCode
?>