<?php

define('THIS_SCRIPT', 'query');
define('QUERY_DEBUG',false);
require_once 'global.php';
include_once INC_PATH.'DV_Encoding.class.php';
$objenc =& DV_Encoding::GetEncoding('gbk');
$lang = load_lang($lang, 'query' );
$is_more_correlation = false;
$ft_settings = explode('|', $fsetting['ft_settings']);


PageLoad();
footer();

function PageLoad()
{
	global $arrNavMenu,$boardid,$SearchMaxPageList,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype,$is_more_correlation,$ft_settings,$issavvy;
	empty($issavvy) || !is_numeric($issavvy) || $issavvy>1 && $issavvy=0;
	$stats = $lang['HeaderTitle1'];
	$arrNavMenu = array(
		 0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>"query.php?boardid={$boardid}")
		,1=>array('linktitle'=>$lang['HeaderTitle2'])
	);
	//没有论坛搜索权限
	if( $groupsetting[14] == 0 && $GLOBALS['isweb'] != 2 ){
		head(1, 0, 0, $arrNavMenu);
		$get_REQUEST_URI = $_SERVER[REQUEST_URI];
		$rUrl = ($pos = strpos($get_REQUEST_URI,'&keyword',1)) ? (substr($get_REQUEST_URI, 0, $pos) ):$get_REQUEST_URI;
		$s = trim($GLOBALS['keyword']);
		$QuerySetUrl = $rUrl.'&keyword='.$s ;	
		if (!empty($s))
		{
			if ( strpos($get_REQUEST_URI,'keyword') === false )
			{
				header("Location:".$QuerySetUrl);
			}
		}
		showmsg($lang['Error.strings60']);
		exit;
	}

	$action = trim($GLOBALS['action']);
	if( !$master && $action != 'isweb' ){
		$action = '';
	}
	if( $fsetting['searchlimited_time'] == 0 ){
		$fsetting['searchlimited_time'] = 120;
	}
	$stype = intval($stype);
	if( $stype == 0 ){
		if( $action == '' ){
			if( $boardid == 0 ){
				head(1,0,0,$arrNavMenu);
			} else{
				$arrNavMenu[1]['linktitle'] = '';
				head(1,intval(get_cookie('depth')),0,$arrNavMenu);
			}
			Queryform();
		} elseif( $action == 'batch' ){
			$stats = $lang['template.strings22'];
			if( $boardid == 0 ){
				head(1,0,0,$arrNavMenu);
			} else{
				$arrNavMenu[1]['linktitle'] = '';
				head(1,intval(get_cookie('depth')),0,$arrNavMenu);
			}
			Dobatch();
		} elseif( $action == 'isweb' ){
			isWeb_Query();
		}
	} else{
		$stats = $lang['template.strings1'];
		if( CheckRequest() == true ){
			global $nsearch,$psearch,$selsearch;
			//搜索返回结果数控制
			$SearchMaxPageList = 50;
			if ($fsetting['otherpagesize'] < 1) {
				$fsetting['otherpagesize'] = 20;
			}
			if( $fsetting['searchlimited_num'] != 0 && is_numeric($fsetting['searchlimited_num']) ){
				$SearchMaxPageList = ceil( (float)$fsetting['searchlimited_num'] / (float)$fsetting['otherpagesize'] );
			}
			if( !is_numeric($GLOBALS['psearch']) ){
				$psearch = 1;
			} else{
				$psearch = intval($GLOBALS['psearch']);
			}
			if( !is_numeric($GLOBALS['nsearch']) ){
				$nsearch = 1;
			} else{
				$nsearch = intval($GLOBALS['nsearch']);
			}
			$selsearch = trim($GLOBALS['selsearch']);
			if( $selsearch == '' ) $selsearch = 'Page';
			$stats = $issavvy==1?$lang['template.strings4.1']:$lang['template.strings4'];
			//执行搜索
			if ((2 == $stype OR 7 == $stype) AND 1 == $ft_settings[0]){//全文检索
				global $SQL_CACHE;
				$SQL_CACHE = ' /*!40010 SQL_CACHE */ ';
				if( $stype == 2 ){//按主题全文检索
					if (0 == $ft_settings[2] OR 2 == $ft_settings[2]) {
						SearchType2();
					} else {
						ftSearch2();
					}
				} elseif( $stype == 7 ){//按主题和内容全文检索
					if (0 == $ft_settings[2] OR 1 == $ft_settings[2]) {
						SearchType7();
					} else {
						ftSearch7();
					}
				}
			} elseif ( $stype == 1) {//搜索作者
				SearchType1();
			} elseif( $stype == 2 ){//搜索主题
				SearchType2();
			} elseif ($stype >=3 AND $stype <= 6) { // 最新50帖、用户热帖、我的主题和被回复的主题、版面或用户精华帖
				SearchType3456();
			} elseif( $stype == 7 ){//搜索内容
				SearchType7();
			} else{
				head(1,0,0,$arrNavMenu);
				showmsg($lang['Error.strings61']);
			}
		}
	}
}//end function PageLoad

function CheckRequest(){
	global $_SESSION,$dv,$arrNavMenu,$lang,$boardid,$fsetting,$groupsetting,$_POST,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$stype,$isweb,$SearchMaxPageList,$is_more_correlation,$objenc;
	//更多相关主题处理-------------将更多相关主题映射到主题搜索，并在下边去除搜索关键字字符长度的判断
	$is_more_correlation = false;
	if( $stype === -1 ){
		$is_more_correlation = true;
		$stype = 2;
	}
	//isweb=0包含站外搜索,isweb=1仅站内,isweb=2仅站外
	if( !is_numeric($GLOBALS['isweb']) ){
		$isweb = 1;
	} else {
		$isweb = intval($GLOBALS['isweb']);
	}
	if( $stype == 3 || $stype == 5 ){
		$isweb = 1;
	} else{
		if( $isweb != 0 && $_POST['submitdBtn'] == 1 ){
			$isweb = 2;
			$stype = 8;
		}
		if( $isweb < 0 || $isweb > 2 ) $isweb = 1;
	}
	//搜索时间间隔判断
	if( is_array($_SESSION) ){
		if( empty($_SESSION['QueryLimited']) ){
			$_SESSION['QueryLimited'] = array($keyword,$stype,gmdate('Y-m-d H:i:s'));
		} else{
			if( $isweb != 2 ){
				if( !( $master || $boardmaster || $supermaster ) ){
					$arrTmp =& $_SESSION['QueryLimited'];
					if( count($arrTmp) == 3 ){
						if( $arrTmp[0] == $keyword ){
							if( $arrTmp[1] == $stype ){
								if( dateDiff('s',$arrTmp[2],gmdate("Y-m-d H:i:s")) < $fsetting['searchlimited_time'] ){
									head(1,0,0,$arrNavMenu);
									showmsg(str_replace('{$timelimited}',$fsetting['searchlimited_time'],$lang['template.strings20']));
									return false;
								}
							}
						}
					}
				}
			}
			$_SESSION['QueryLimited'] = array($keyword,$stype,gmdate('Y-m-d H:i:s'));
		}
	}

	if( $stype == 8 && ( $GLOBALS['submitdBtn'] == '' || $GLOBALS['submitdBtn'] == 0 ) ) $stype = 2;
	if( preg_match('/^[12345768]$/',$stype) === 0 ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings0']);
		return false;
	}
	$stable = trim($GLOBALS['stable']);
	if (empty($stable) OR strlen($stable) > 10) {
		$stable = $fsetting['nowtablename'];
	}
	$keyword = trim($GLOBALS['keyword']);
	if ($stype == 1 OR $stype == 2 OR $stype == 7) {
		if ($keyword === '') {
			head(1,0,0,$arrNavMenu);
			showmsg($lang['Error.strings35']);
			return false;
		} else {
			$keywordMBLength = $objenc->StrLength($keyword);
			//搜索不受长度限制的词
			$Foundmykeyword = false;
			if( !empty($fsetting['searchlimited_word']) ){//$fsetting['searchlimited_word'] like 'dvbbs|sql|aspsky|asp|php|cgi|jsp|htm'
				if( preg_match("/{$fsetting['searchlimited_word']}/i",$keyword) ){
					$Foundmykeyword = true;
				}
			}
			//搜索字串最小和最大长度
			if( $is_more_correlation === false && $Foundmykeyword == false ){
				if( !( $master || $boardmaster || $superboardmaster ) ){
					if( !empty($fsetting['searchlimited_length']) ){
						list($keywordminlimited,$keywordmaxlimited) = explode('|',$fsetting['searchlimited_length']);
						if( $keywordMBLength < $keywordminlimited ){
							head(1,0,0,$arrNavMenu);
							showmsg(str_replace('{$minlength}',$keywordminlimited,$lang['template.strings17']));
							return false;
						}
						if( $keywordMBLength > $keywordmaxlimited ){
							head(1,0,0,$arrNavMenu);
							showmsg(str_replace('{$maxlength}',$keywordmaxlimited,$lang['template.strings18']));
							return false;
						}
					}
				}
			}
		}
	}
	//搜索过滤字
	$FobWords =& $lang['FobWords1'];
	if( in_array($keyword,$FobWords) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings61']);
		return false;
	}
	$keyword = preg_replace('/!|\\[|\\]|\\(|\\)|'.preg_quote(chr(161).chr(161)).'|-|\\/|\\+|=|,/i',' ',$keyword);
	$FobWords =& $lang['FobWords2'];
	if( in_array($keyword,$FobWords) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings61']);
		return false;
	}
	return true;
}//end function CheckRequest

function Queryform(){
	global $arrNavMenu,$stats,$dv,$db,$fsetting,$lang;
	$keywordlimited = explode('|',$fsetting['searchlimited_length']);
	$count = count($keywordlimited);
	if( $count < 2 ){
		unset($keywordlimited);
		$keywordlimited = array(2,20);
	}
	$keywordminlen = $keywordlimited[0];
	$keywordmaxlen = $keywordlimited[1];
	$timelimited = $fsetting['searchlimited_time'];
	$_dbResults =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
	require printout('query.form');
}//end function Queryform

//搜索作者
function SearchType1(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb,$gettitle;
	$SearchUserID = $db->scalar("SELECT userid FROM {$dv}user WHERE username='{$keyword}'");
	if (false === $SearchUserID OR $SearchUserID <= 0){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['template.strings21']);
		exit;
	}
	switch($nsearch){
		case 1://主题作者
			$countStmt = "SELECT COUNT(1) FROM {$dv}topic WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." postuserid={$SearchUserID} ".(!$master?' AND hidename=0 ':'');
			$stmt = "SELECT boardid,topicid AS rootid,title AS topic,expression,postusername AS username,postuserid,dateandtime,isbest,locktopic,child,hits FROM {$dv}topic WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." postuserid={$SearchUserID} ".(!$master?' AND hidename=0 ':'').' ORDER BY topicid DESC ';
			$stats = $lang['template.strings7'];
			$isMoreInfo = 1;
			break;
		case 2://回复作者
			$countStmt = "SELECT COUNT(1) FROM {$stable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." parentid>0 AND postuserid={$SearchUserID} ".(!$master?' AND signflag<2 ':'');
			$stmt = "SELECT boardid,rootid,topic,expression,username,postuserid,dateandtime,isbest,locktopic,body,announceid FROM {$stable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." parentid>0 AND postuserid={$SearchUserID} ".(!$master?' AND signflag<2 ':'').' ORDER BY announceid DESC ';
			$stats = $lang['template.strings8'];
			break;
		case 3://主题和回复作者
			$countStmt = "SELECT COUNT(1) FROM {$stable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." postuserid={$SearchUserID} ".(!$master?' AND signflag<2 ':'');
			$stmt = "SELECT boardid,rootid,topic,expression,username,postuserid,dateandtime,isbest,locktopic,body,announceid FROM {$stable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." postuserid={$SearchUserID} ".(!$master?' AND signflag<2 ':'').' ORDER BY announceid DESC ';
			$stats = $lang['template.strings9'];
			break;
	}
	$Record_Count = $db->scalar($countStmt);
	$stats .= $keyword;
	if( $boardid != 0 ){
		$gettitle = $stats;
		head();
	} else{
		$arrNavMenu[1]['linktitle'] = $stats;
		head(1,1,0,$arrNavMenu);
	}
	if ($Record_Count <= 0) {
		showmsg($lang['str_42'].$lang['str_43']);
		exit;
	}
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$page = intval($GLOBALS['page']);
	$pagesize = $fsetting['otherpagesize'];
	$RecordRowLimited = $fsetting['otherpagesize'] * $SearchMaxPageList;
	if ($Record_Count > $RecordRowLimited) $Record_Count = $RecordRowLimited;
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	if( $page > $PCount ) $page = $PCount;
	if( $page < 1 ) $page = 1;
	$__dbResults =& $db->getResultSet($stmt, array('absolutePage'=>$page,'pageSize'=>$pagesize));
	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);
	$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
	$pagecount = $Record_Count;
	$loginhidden = $groupsetting[37];
	$action = $GLOBALS['action'];
	global $master,$superboardmaster;
	if( $master || $superboardmaster ){
		$Master = 1;
	} else{
		$Master = 0;
	}
	if( !empty($__dbResults) ){
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
		include printout('query.result');
	}
}//end function SearchType1

function SearchType3456() // 最新50帖、用户热帖、我的主题和被回复的主题、版面或用户精华帖
{
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb;
	if ($boardid <= 0) {
		$boardid = 0;
	}
	$Record_Count = 0;
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$page = intval($GLOBALS['page']);
	if ($page < 1) {
		$page = 1;
	}
	$pagesize = $fsetting['otherpagesize'];
	$RecordRowLimited = $pagesize * $SearchMaxPageList;
	if ($stype == 3) { // 最新50帖
		$arrNavMenu[1]['linktitle'] = $lang['template.strings12'];
		if ($boardid > 0) {
			$stmt = "SELECT boardid, rootid, topic, expression, username, postuserid, dateandtime, isbest, locktopic, body, announceid, signflag FROM ".$db->escape_string($stable)." WHERE boardid='{$boardid}' ORDER BY announceid DESC";
		} else {
			$stmt = "SELECT boardid, rootid, topic, expression, username, postuserid, dateandtime, isbest, locktopic, body, announceid, signflag FROM ".$db->escape_string($stable)." ORDER BY announceid DESC";
		}
		$Record_Count = 500;
		if (($page * $pagesize) > $Record_Count) {
			$page = ceil($Record_Count / $pagesize);
			$pagesize = ($page * $pagesize) - $Record_Count;
		}
		if ($query = $db->query($stmt, array('absolutePage'=>$page, 'pageSize'=>$pagesize))) {
			if ($numrows = $db->num_rows($query)) {
				while ($dbinfo =& $db->fetch_array($query)) {
					$__dbResults[] =& $dbinfo;
				}//end while
			} else {
				$Record_Count = 0;
			}
		} else {
			$Record_Count = 0;
		}
	} elseif ($stype == 4) { // 用户热帖
		$arrNavMenu[1]['linktitle'] = $lang['template.strings28'];
		if ($keyword != '') {
			$SearchUserID = $db->scalar("SELECT userid FROM {$dv}user WHERE username='{$keyword}'");			
			if (false === $SearchUserID OR $SearchUserID <= 0){
				head(1,0,0,$arrNavMenu);
				showmsg($lang['template.strings21']);
				exit;
			}
		}
		$HotTopicDay = 10;
		$HotTopicView = 200;
		if ($fsetting['searchlimited_hot'] != 0) { // 搜索热门帖子条件中对应的搜索天数和浏览次数标准
			$MyHotTopic = explode('|', $fsetting['searchlimited_hot']);
			if (!empty($MyHotTopic) AND count($MyHotTopic) == 2) {
				$HotTopicDay = $MyHotTopic[0];
				$HotTopicView = $MyHotTopic[1];
			}
		}
		$stats = str_replace(array('{$daylimited}', '{$viewlimited}'), array($HotTopicDay, $HotTopicView), $lang['template.strings13']);
		$searchday = " dateandtime>'".dateAdd('d', -$HotTopicDay, gmdate("Y-m-d 23:59:59"), '')."' ";
		$searchkeyword = '';
		if ($keyword != '') {
			$searchkeyword = " AND postuserid='{$SearchUserID}' ";
			if ($master === false OR $superboardmaster === false) {
				$searchkeyword .= " AND hidename=0 ";
			}
		}
		$countStmt = "SELECT COUNT(1) FROM {$dv}topic WHERE {$searchday} AND hits>'{$HotTopicView}' {$searchkeyword}";
		$stmt = "SELECT boardid, topicid AS rootid, title AS topic, expression, postusername AS username, postuserid, dateandtime, isbest, locktopic, child, hits, hidename FROM {$dv}topic WHERE {$searchday} AND hits>'{$HotTopicView}' {$searchkeyword} ORDER BY topicid DESC";
		$isMoreInfo = 1;
	} elseif ($stype == 5) { // 我的主题和被回复的主题
		$nsearch = 0;
		$s = trim($GLOBALS['s']);
		if ($s <= 0) {
			$s = 1;
		}
		if ($SearchUserID = $userid) {
			if ($s == 1) {
				$arrNavMenu[1]['linktitle'] = $lang['template.strings14'];
				$countStmt = "SELECT COUNT(1) FROM {$dv}topic AS t INNER JOIN ".$db->escape_string($stable)." AS bbs ON t.topicid=bbs.rootid AND bbs.parentid>0 AND bbs.postuserid='{$userid}'";
				$stmt = "SELECT t.boardid, t.topicid AS rootid, t.title AS topic, t.expression, t.postusername AS username, t.postuserid, t.dateandtime, t.isbest, t.locktopic, t.child, t.hits FROM {$dv}topic AS t INNER JOIN ".$db->escape_string($stable)." AS bbs ON t.topicid=bbs.rootid AND bbs.parentid>0 AND bbs.postuserid='{$userid}' WHERE t.boardid<>444 ORDER BY t.topicid DESC";
			} else {
				$arrNavMenu[1]['linktitle'] = $lang['template.strings15'];
				$countStmt = "SELECT COUNT(1) FROM {$dv}topic WHERE boardid<>444 AND postuserid='{$userid}'";
				$stmt = "SELECT boardid, topicid AS rootid, title AS topic, expression, postusername AS username, postuserid, dateandtime, isbest, locktopic, child, hits FROM {$dv}topic WHERE boardid<>444 AND postuserid='{$userid}' ORDER BY topicid DESC";
			}
			$isMoreInfo = 1;
		}
	} elseif ($stype == 6) { // 版面或用户精华帖
		$arrNavMenu[1]['linktitle'] = $lang['template.strings29'];
		if ($keyword != '') {
			$SearchUserID = $db->scalar("SELECT userid FROM {$dv}user WHERE username='{$keyword}'");			
			if (false === $SearchUserID OR $SearchUserID <= 0){
				head(1,0,0,$arrNavMenu);
				showmsg($lang['template.strings21']);
				exit;
			}
		}
		if (trim($searchboard) === '') {
			if ($keyword != '') {
				$searchkeyword = " AND postuserid='{$SearchUserID}'";
			} else {
				$searchkeyword = '';
				$isweb = 0;
			}
			
			$whereStmt = " WHERE ".($boardid > 0 ? " boardid='{$boardid}' " : " 1=1 ")." {$searchkeyword} ";
			$countStmt = "SELECT COUNT(1) FROM {$dv}besttopic {$whereStmt} ";
			$stmt = "SELECT boardid, rootid, title AS topic, expression, postusername AS username, postuserid, dateandtime, postuserid AS issbest, postuserid AS locktopic FROM {$dv}besttopic {$whereStmt} ORDER BY id DESC";
		} else {
			$searchkeyword = '';
			if ($keyword != '') {
				$searchkeyword = " AND postuserid='{$SearchUserID}'";
			}
			$whereStmt = " WHERE ".($boardid > 0 ? " boardid='{$boardid}' " : " 1=1 ")." {$searchkeyword} ";
			$countStmt = "SELECT COUNT(1) FROM {$dv}besttopic {$whereStmt}";
			$stmt = "SELECT boardid, rootid, title AS topic, expression, postusername AS username, postuserid, dateandtime, postuserid AS issbest, postuserid AS locktopic FROM {$dv}besttopic {$whereStmt} ORDER BY id DESC";
		}
	}
	if( $boardid == 0 ){
		head(1,0,0,$arrNavMenu);
	} else{
		head(1,1,0,$arrNavMenu);
	}
	if (isset($countStmt)) {
		$Record_Count = $db->scalar($countStmt);
	}
	if ($Record_Count <= 0) {
		head(1,0,0,$arrNavMenu);
		showmsg($lang['str_42'].$lang['str_43']);
		exit;
	}
	if ($Record_Count > $RecordRowLimited) $Record_Count = $RecordRowLimited;
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	if( $page > $PCount ) $page = $PCount;
	if( $page < 1 ) $page = 1;
	$pagecount = $Record_Count;
	if (empty($__dbResults) AND !empty($stmt)) {
		$__dbResults =& $db->getResultSet($stmt, array('absolutePage'=>$page, 'pageSize'=>$pagesize));
	}
	if( !empty($__dbResults) ){

	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);

		$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
		$loginhidden = $groupsetting[37];
		$action = $GLOBALS['action'];
		global $master,$superboardmaster;
		if( $master || $superboardmaster ){
			$Master = 1;
		} else{
			$Master = 0;
		}
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist", array('absolutePage'=>$page,'pageSize'=>$pagesize));
		include printout('query.result');
	}
}//end function 

//搜索主题
function SearchType2(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype,$issavvy;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb;
	$RecordRowLimited = $fsetting['otherpagesize'] * $SearchMaxPageList;
	empty($issavvy) || !is_numeric($issavvy) || $issavvy>1 && $issavvy=0;
    $searchtable=$dv.($issavvy==1?"savvy_topic":"topic");
	//$now = gmdate('Y-m-d H:i:s');
	$now = TIME_NOW;
	$dateCompare1 = dateAdd('s',-$fsetting['searchlimited_time'], TIME_NOW, '');
	$dateCompare2 = dateAdd('s',-$fsetting['searchlimited_time'], TIME_NOW, '');
	$Record_Count = 0;
	$qidList = '';
	//获取缓存
	if( $query = $db->query("SELECT id_count,qid_list,@qidList:=NULL FROM {$dv}querycache WHERE  boardid={$boardid} AND stype='{$stype}' AND creation_datetime>'{$dateCompare2}' AND keyword='{$keyword}'") ){
		if( $result =& $db->fetch_row($query) ){
			$Record_Count = $result[0];
			$qidList = $result[1];
			$db->free_result($query);
			$db->query("REPLACE INTO {$dv}querycache SET creation_datetime='{$now}',boardid={$boardid},keyword='{$keyword}',stype='{$stype}'");
		}
	}
	
	if( $Record_Count <= 0 ){//创建缓存
		//删除过期缓存
		$db->query("DELETE FROM {$dv}querycache WHERE boardid={$boardid} AND keyword='{$keyword}' AND stype='{$stype}' AND creation_datetime<'{$dateCompare1}'");
		if( $query = $db->query("SELECT @qidList:=COALESCE(CONCAT(@qidList,',',topicid),topicid) AS qidList FROM {$searchtable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." title LIKE '%{$keyword}%' ORDER BY topicid DESC ",array('pageSize'=>$RecordRowLimited)) ){
			$Record_Count = $db->num_rows($query);

			if( $Record_Count <= 0 ){
				head(1,0,0,$arrNavMenu);
				showmsg($lang['str_42'].$lang['str_43']);
				exit;
			} else{
				if( $Record_Count > 1 ) $db->data_seek($query,$Record_Count-1);
				$result =& $db->fetch_row($query);
				$db->free_result($query);
				$qidList = $result[0];
				unset($result);
				$db->query("REPLACE INTO {$dv}querycache(boardid,keyword,stype,creation_datetime,qid_list,id_count) VALUES({$boardid},'{$keyword}','{$stype}','{$now}',@qidList,{$Record_Count})");
			}
		}
	}
	$stmt = "SELECT boardid,topicid AS rootid,title AS topic,expression,postusername AS username,postuserid,dateandtime,isbest,locktopic,child,hits,hidename,@qidList:=NULL FROM {$searchtable} WHERE ".($boardid>0?" boardid={$boardid} AND ":'')."  topicid IN({$qidList}) ORDER BY topicid DESC ";
	unset($qidList);
	$isMoreInfo = 1;
	$page = intval($GLOBALS['page']);
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$pagesize = $fsetting['otherpagesize'];
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	if( $page > $PCount ) $page = $PCount;
	if( $page < 1 ) $page = 1;
	$__dbResults =& $db->getResultSet($stmt,array('absolutePage'=>$page,'pageSize'=>$pagesize));
	$stats = $lang['template.strings10'];
	$arrNavMenu[1]['linktitle'] = $issavvy==1?$lang['template.strings4.1']:$lang['template.strings4'];
	$arrNavMenu[1]['linkurl'] =  $issavvy==1?'forum_savvy.php':'?boardid=0';
	if( $boardid == 0 ){
		head(1,0,0,$arrNavMenu);
	} else{
		head(1,1,0,$arrNavMenu);
	}
	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);
	$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
	$pagecount = $Record_Count;
	$loginhidden = $groupsetting[37];
	$action = $GLOBALS['action'];
	global $master,$superboardmaster;
	if( $master || $superboardmaster ){
		$Master = 1;
	} else{
		$Master = 0;
	}
	if( !empty($__dbResults) ){
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
		include printout('query.result');
	}
}//end function SearchType2

//按主题全文检索
function ftSearch2(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb,$SQL_CACHE,$boardinfo,$issavvy;
	if(empty($issavvy) || !is_numeric($issavvy) || $issavvy>1)$issavvy=0;
	global $is_more_correlation;
	$RecordRowLimited = $fsetting['otherpagesize'] * $SearchMaxPageList;
	$now = TIME_NOW;
	$dateCompare1 = dateAdd('s',-$fsetting['searchlimited_time'], TIME_NOW, '');
	$dateCompare2 = dateAdd('s',-$fsetting['searchlimited_time'], TIME_NOW, '');
	$Record_Count = 0;
	$arrFTKeyWord =& DV_ChineseWordSegment($keyword);
	$ftKeyWords = ''.implode(' ',$arrFTKeyWord);
	$Record_Count = $db->scalar("SELECT {$SQL_CACHE} COUNT(1) FROM {$dv}topic_ft AS ft WHERE MATCH(ft.topicindex) AGAINST('{$ftKeyWords}' IN BOOLEAN MODE) ".($boardid>0?" AND ft.boardid='{$boardid}'":'')." AND issavvytopic={$issavvy} AND ft.posttable='{$stable}'");

	if( $Record_Count <= 0 ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['str_42'].$lang['str_43']);
		exit;
	}
	$page = intval($GLOBALS['page']);
	$pagesize = $fsetting['otherpagesize'];
	if( $pagesize < 1 ){
		$pagesize = 20;
	}
	if( $page < 1 ) $page = 1;
	$stmt = "SELECT {$SQL_CACHE} /*SQL_CALC_FOUND_ROWS*/ ft.topicid FROM {$dv}topic_ft AS ft WHERE MATCH(ft.topicindex) AGAINST('{$ftKeyWords}' IN BOOLEAN MODE) ".($boardid>0?" AND ft.boardid='{$boardid}'":'')." AND issavvytopic={$issavvy} AND ft.posttable='{$stable}'";
	$TopicIDList = '0';
	if( $query = $db->query($stmt,array('absolutePage'=>$page,'pageSize'=>$pagesize,'debug'=>QUERY_DEBUG)) ){
		if( $Record_Count <= 0 ){
			head(1,0,0,$arrNavMenu);
			showmsg($lang['str_42'].$lang['str_43']);
			exit;
		}
		while( $tmpResult =& $db->fetch_row($query) ){
			$TopicIDList .= ",{$tmpResult[0]}";
		}
	}
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	$__dbResults =& $db->getResultSet("SELECT {$SQL_CACHE} t.boardid,t.topicid AS rootid,t.title AS topic,t.expression,t.postusername AS username,t.postuserid,t.dateandtime,t.isbest,t.locktopic,t.child,t.hits,t.hidename FROM {$dv}topic AS t WHERE t.topicid IN({$TopicIDList}) ORDER BY t.topicid DESC");
	if( $is_more_correlation === false ){
		if ( $boardid == 0 ){
			$arrNavMenu[1]['linktitle'] = $issavvy===1?$lang['template.strings4.1']:$lang['template.strings4'];
		}else{
			$arrNavMenu[1]['linktitle'] = $boardinfo['boardtype'].$lang['template.strings1'];
		}
	} else{
		$arrNavMenu[1]['linktitle'] = $lang['template.strings5'];
		$stype = -1;
	}
	$stats = $arrNavMenu[1]['linktitle'];
	head(1,1,0,$arrNavMenu);
	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);
	$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
	$pagecount = $Record_Count;
	$loginhidden = $groupsetting[37];
	$isMoreInfo = 1;
	$action = $GLOBALS['action'];
	global $master,$superboardmaster;
	if( $master || $superboardmaster ){
		$Master = 1;
	} else{
		$Master = 0;
	}
	if( !empty($__dbResults) ){
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
		include printout('query.result');
	}
}//end function ftSearch2()

//搜索内容
function SearchType7(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb;
	$stmt = "SELECT T1.boardid,T1.rootid,T1.topic,T1.expression,T1.username,T1.postuserid,T1.dateandtime,T1.isbest,T1.locktopic,T1.body,T1.announceid,T1.signflag FROM {$stable} AS T1 WHERE ".($boardid>0?" boardid={$boardid} AND ":'')." body LIKE '%{$keyword}%' ORDER BY announceid DESC ";
	$Record_Count = 0;
	$RecordRowLimited = $fsetting['otherpagesize'] * $SearchMaxPageList;
	if( $query = $db->query($stmt,array('pageSize'=>$RecordRowLimited)) ){
		$Record_Count = $db->num_rows($query);
	}
	if( $Record_Count <= 0 ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['str_42'].$lang['str_43']);
		exit;
	}
	$page = intval($GLOBALS['page']);
	if( $Record_Count > $RecordRowLimited ) $Record_Count = $RecordRowLimited;
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$pagesize = $fsetting['otherpagesize'];
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	if( $page > $PCount ) $page = $PCount;
	if( $page < 1 ) $page = 1;
	if( $page > 1 ) $db->data_seek($query,($page-1)*$pagesize);
	for( $i = 0 ; $i < $pagesize ; $i++ ){
		$__dbResults[$i] =& $db->fetch_array($query);
		if( $__dbResults[$i] === false ){
			unset($__dbResults[$i]);
			break;
		}
	}
	$db->free_result($query);
	$stats = $lang['template.strings11'];
	$arrNavMenu[1]['linktitle'] = $issavvy==1?$lang['template.strings4.1']:$lang['template.strings4'];
	$arrNavMenu[1]['linkurl'] = '?boardid=0';
	if( $boardid == 0 ){
		head(1,0,0,$arrNavMenu);
	} else{
		head(1,1,0,$arrNavMenu);
	}
	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);
	$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
	$pagecount = $Record_Count;
	$loginhidden = $groupsetting[37];
	$action = $GLOBALS['action'];
	global $master,$superboardmaster;
	if( $master || $superboardmaster ){
		$Master = 1;
	} else{
		$Master = 0;
	}
	if( !empty($__dbResults) ){
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
		include printout('query.result');
	}
}//end function SearchType7

//按主题和内容全文检索
function ftSearch7(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype,$boardinfo;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb,$SQL_CACHE,$is_more_correlation;
	$RecordRowLimited = $fsetting['otherpagesize'] * $SearchMaxPageList;
	$ftKeyWord = ''.implode(' ',DV_ChineseWordSegment($keyword));
	$Record_Count = $db->scalar("SELECT {$SQL_CACHE} COUNT(1) FROM {$dv}bbs_ft WHERE MATCH(topicindex,bodyindex) AGAINST('{$ftKeyWord}' IN BOOLEAN MODE) ".($boardid>0?" AND boardid='{$boardid}'":'')." AND posttable='{$stable}'");
	if( intval($Record_Count) <= 0 ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['str_42'].$lang['str_43']);
		exit;
	}
	$page = intval($GLOBALS['page']);
	$pagesize = $fsetting['otherpagesize'];
	if( $pagesize < 1 ){
		$pagesize = 20;
	}
	if( $page < 1 ) $page = 1;
	$stmt = "SELECT {$SQL_CACHE} /*SQL_CALC_FOUND_ROWS*/ announceid FROM {$dv}bbs_ft AS ft WHERE MATCH(ft.topicindex,ft.bodyindex) AGAINST('{$ftKeyWord}' IN BOOLEAN MODE) ".($boardid>0?" AND boardid='{$boardid}'":'');
	$AnnounceIDList = '0';
	if( $query = $db->query($stmt,array('absolutePage'=>$page,'pageSize'=>$pagesize,'debug'=>QUERY_DEBUG)) ){
		if( $Record_Count <= 0 ){
			head(1,0,0,$arrNavMenu);
			showmsg($lang['str_42'].$lang['str_43']);
			exit;
		}
		while( $tmpResult =& $db->fetch_row($query) ){
			$AnnounceIDList .= ",{$tmpResult[0]}";
		}
	}
	$PCount = ceil( (float)$Record_Count / (float)$pagesize );
	if( $AnnounceIDList !== '0' ){
		$stmt = "SELECT {$SQL_CACHE}  bbs.boardid,bbs.rootid,bbs.topic,bbs.expression,bbs.username,bbs.postuserid,bbs.dateandtime,bbs.isbest,bbs.locktopic,bbs.body,bbs.announceid,bbs.signflag FROM {$stable} AS bbs WHERE announceid IN({$AnnounceIDList}) ORDER BY bbs.announceid DESC";
		$__dbResults =& $db->getResultSet($stmt);
	}
	if ( $boardid == 0 ){
		$arrNavMenu[1]['linktitle'] = $issavvy==1?$lang['template.strings4.1']:$lang['template.strings4'];
	}else{
		$arrNavMenu[1]['linktitle'] = $boardinfo['boardtype'].$lang['template.strings1'];
	}
	$stats = $arrNavMenu[1]['linktitle'];
	head(1,1,0,$arrNavMenu);
	$boardid=(!empty($_POST['boardid'])?$_POST['boardid']:$boardid);
	$PageStr = $SearchStr = "stype={$GLOBALS['stype']}&psearch={$GLOBALS['psearch']}&nsearch={$GLOBALS['nsearch']}&boardid={$boardid}&searchdate={$GLOBALS['searchdate']}&keyword=".urlencode($GLOBALS['keyword'])."&s={$GLOBALS['s']}&isweb={$GLOBALS['isweb']}&stable={$GLOBALS['stable']}";
	$pagecount = $Record_Count;
	$loginhidden = $groupsetting[37];
	$action = $GLOBALS['action'];
	global $master,$superboardmaster;
	if( $master || $superboardmaster ){
		$Master = 1;
	} else{
		$Master = 0;
	}
	if( !empty($__dbResults) ){
		$__tableList =& $db->getResultSet("SELECT id,tablename,tabletype FROM {$dv}tablelist");
		include printout('query.result');
	}
}//end function ftSearch7()

//进行批量操作
function Dobatch(){
	global $lang,$master;
	if( !$master ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['template.strings27']);
		return false;
	}
	$maction = trim($GLOBALS['maction']);
	if( $maction == '' ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['template.strings24']);
		return false;
	} elseif( $maction == 'move' ){
		MoveBatch();
	} elseif( $maction == 'dele' ){
		DeleteBatch();
	} elseif( $maction == 'isbest' ){
		IsBestBatch();
	} elseif( $maction == 'lock' ){
		LockBatch();
	} else{
		head(1,0,0,$arrNavMenu);
		showmsg($lang['template.strings25']);
		return false;
	}
}//end function Dobatch

//批量移动
function MoveBatch(){
	global $lang,$master,$dv,$db,$arrNavMenu,$lang,$boardid,$userid,$membername,$fsetting,$groupsetting,$usertrueip,$_SERVER;
	$newboard = intval($GLOBALS['newboard']);
	if( $newboard <= 0 ){//移动帖子,请设置目标版面
		head(1,0,0,$arrNavMenu);
		showmsg($lang['template.strings25']);
		return false;
	}
	$arrRootID =& $GLOBALS['arrrootid'];
	if( empty($arrRootID) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings3']);
		return false;
	}
	include_once CACHE_PATH.'board/board_all.php';
	$ReportText = $lang['ReportText'];
	$BordidList = ',';
	foreach( $arrRootID as $RootID ){

		$posttable = $db->scalar("SELECT posttable FROM {$dv}topic WHERE topicid={$RootID}");
		if( empty($posttable) ){ 
			$ReportText .= str_replace('{$ID}',$RootID,$lang['ReportText12']);
			continue;
		}
		$reqAnnounceID = intval($GLOBALS["announceid_{$RootID}"]);
		if( $reqAnnounceID <= 0 ){
			$tmpStmt = "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} WHERE rootid={$RootID} AND parentid=0  and forumtype=0";
		} else{
			$tmpStmt = "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} WHERE announceid={$reqAnnounceID}";
		}

		if( $query = $db->query($tmpStmt) ){
			if( $tmpResult =& $db->fetch_array($query,3) ) $db->free_result($query);
		}
		if( empty($tmpResult) ){
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		$currBoardID = intval($tmpResult['boardid']);
		$currRootID = intval($tmpResult['rootid']); 
		if( $newboard == $currBoardID ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= str_replace('{$ID}',$tmpResult[0],$lang['ReportText4']);
			continue;
		}
		if( $tmpResult['boardid'] == 444 || $tmpResult['boardid'] == 777 ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		$currAnnounceID = intval($tmpResult['announceid']);//postid
		if( $tmpResult['parentid'] == 0 ){
			$ReportText .= str_replace('{$ID}',$tmpResult[0],str_replace('{$Topic}',$tmpResult['topic'],$lang['ReportText3']));
			$db->query("UPDATE {$posttable} SET boardid={$newboard} WHERE rootid={$currRootID} AND boardid<>444 AND boardid<>777 and forumtype=0");
			$UpdateCount = intval($db->scalar("SELECT COUNT(1) FROM {$posttable} WHERE rootid={$currRootID} AND boardid<>444 AND boardid<>777 and forumtype=0"));
			$db->query("UPDATE {$dv}topic SET boardid={$newboard},mode=0 WHERE topicid={$currRootID}");
			$db->query("UPDATE {$dv}board SET topicnum=topicnum-1,postnum=postnum-{$UpdateCount} WHERE boardid={$currBoardID}");
			$db->query("UPDATE {$dv}board SET topicnum=topicnum+1,postnum=postnum+{$UpdateCount} WHERE boardid={$newboard}");
			$db->query("UPDATE {$dv}besttopic SET boardid={$newboard} WHERE rootid={$RootID}");

			if( strpos($BordidList,",{$currBoardID},") === false ){
				$BordidList .= "{$currBoardID},";
			}
			if( strpos($BordidList,",{$newboard},") === false ){
				$BordidList .= "{$newboard},";
			}
		} else{
			$ReportText .= str_replace('{$ID}',$tmpResult[0],$lang['ReportText2']);
		}
	}
	$db->query("INSERT INTO {$dv}log(l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type) VALUES(0,0,'More','{$membername}','{$GLOBALS['maction']}','{$usertrueip}',3)");
	if( strlen($BordidList) > 1 ){
		$BordidList{0} = '';
		$BordidList{strlen($BordidList)-1} = '';
	}
	if( strlen($BordidList) > 1 ){
	}
	showmsg($ReportText,1,$_SERVER['HTTP_REFERER']);
}//end function MoveBatch

//批量删除
function DeleteBatch(){
	global $lang,$master,$dv,$db,$arrNavMenu,$lang,$boardid,$userid,$membername,$fsetting,$groupsetting,$usertrueip,$_SERVER;
	$arrRootID =& $GLOBALS['arrrootid'];
	if( empty($arrRootID) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings3']);
		return false;
	}
	$Setupreload = false;
	$ReportText = $lang['ReportText'];
	$BordidList = ',';
	foreach( $arrRootID as $RootID ){
		$posttable = $db->scalar("SELECT posttable FROM {$dv}topic WHERE topicid={$RootID}");
		if( empty($posttable) ) continue;
		$reqAnnounceID = intval($GLOBALS["announceid_{$RootID}"]);
		if( $reqAnnounceID <= 0 ){
			$tmpStmt = "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} WHERE rootid={$RootID} AND parentid=0 and forumtype=0";
		} else{
			$tmpStmt = "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} WHERE announceid={$reqAnnounceID}";
		}
		if( $query = $db->query($tmpStmt) ){
			if( $tmpResult =& $db->fetch_array($query,3) ) $db->free_result($query);
		}
		if( empty($tmpResult) ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		if( $tmpResult['boardid'] == 444 || $tmpResult['boardid'] == 777 ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		$currBoardID = intval($tmpResult['boardid']);
		$currAnnounceID = intval($tmpResult['announceid']);//postid
		$tmpBoardData =& LoadBoardData($tmpResult['boardid'],false);
		if( !empty($tmpBoardData) ){
			$Forum_user = explode(',',$tmpBoardData['boarduser']);
		}
		if( $tmpResult['parentid'] == 0 ){
			$ReportText .= str_replace('{$ID}',$tmpResult[0],str_replace('{$Topic}',$tmpResult['topic'],$lang['ReportText5']));
			$isTop = intval($db->scalar("SELECT istop FROM {$dv}topic WHERE topicid={$RootID}"));
			if( $isTop == 0 ){
				if( $query = $db->query("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} WHERE rootid={$RootID} and forumtype=0 ORDER BY parentid") ){
					$UpdateCount = 0;
					while( $tmpResult2 =& $db->fetch_array($query) ){
						$UpdateCount++;
						if( intval($tmpResult2['isbest']) == 1 ){
							$tmpResult2['isbest'] = 0;
							$db->query("DELETE FROM {$dv}besttopic WHERE announceid={$tmpResult2['announceid']}");
							$db->query("UPDATE {$dv}user SET userwealth=userwealth-'{$Forum_user[15]}',usercp=usercp-'{$Forum_user[16]}',userep=userep-'{$Forum_user[17]}',userisbest=userisbest-1 WHERE userid={$tmpResult2['postuserid']}");
						}
						if( $tmpResult2['parentid'] == 0 ){
							$db->query("UPDATE {$dv}user SET userwealth=userwealth-'{$Forum_user[3]}',usercp=usercp-'{$Forum_user[8]}',userep=userep-'{$Forum_user[13]}',usertopic=usertopic-1 WHERE userid={$tmpResult2['postuserid']}");
						} else{
							$db->query("UPDATE {$dv}user SET userwealth=userwealth-'{$Forum_user[3]}',usercp=usercp-'{$Forum_user[8]}',userep=userep-'{$Forum_user[13]}',userpost=userpost-1 WHERE userid={$tmpResult2['postuserid']}");
						}
						$db->query("UPDATE {$posttable} SET boardid=444,locktopic={$currBoardID},isbest='{$tmpResult2['isbest']}' WHERE rootid={$RootID} and forumtype=0 ");
					}
					$db->query("UPDATE {$dv}topic SET isbest='0',boardid=444,locktopic={$boardid} WHERE topicid={$RootID}");
					$db->query("UPDATE {$dv}board SET topicnum=topicnum-1,postnum=postnum-{$UpdateCount} WHERE boardid={$currBoardID}");
					$db->query("UPDATE {$dv}setup SET forum_topicnum=forum_topicnum-1,forum_postnum=forum_postnum-{$UpdateCount}");
					$Setupreload = true;
					if( strpos($BordidList,",{$currBoardID},") === false ){
						$BordidList .= "{$currBoardID},";
					}
				}
			} else{
				$ReportText .= $lang['ReportText6'];
			}
		} else{
			if( intval($tmpResult['isbest']) == 1 ){
				$db->query("DELETE FROM {$dv}besttopic WHERE announceid={$tmpResult['announceid']}");
				$db->query("UPDATE {$dv}user SET userwealth=userwealth-'{$Forum_user[15]}',usercp=usercp-'{$Forum_user[8]}',userep=userep-'{$Forum_user[17]}',userisbest=userisbest-1 WHERE userid={$tmpResult['postuserid']}");
			}
			$db->query("UPDATE {$posttable} SET isbest='0',boardid=4444,locktopicid={$currBoardID} WHERE announceid={$currAnnounceID}");
			$UpdateCount = 1;
			$db->query("UPDATE {$dv}user SET userwealth=userwealth-'{$Forum_user[3]}',usercp=usercp-'{$Forum_user[8]}',userep=userep-'{$Forum_user[13]}',userpost=userpost-1 WHERE userid={$tmpResult['postuserid']}");
			$db->query("UPDATE {$dv}topic SET child=child-1 WHERE topicid={$RootID}");;
			$db->query("UPDATE {$dv}board SET topicnum=topicnum-1,postnum=postnum-{$UpdateCount} WHERE boardid={$currBoardID}");
			$db->query("UPDATE {$dv}setup SET forum_topicnum=forum_topicnum-1,forum_postnum-forum_postnum-{$UpdateCount}");
			$Setupreload = true;
			$ReportText .= str_replace('{$ID}',$tmpResult[0],$lang['ReportText7']);
			if( strpos($BordidList,",{$currBoardID},") === false ){
				$BordidList .= "{$currBoardID},";
			}
		}
	}
	$db->query("INSERT INTO {$dv}log(l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type) VALUES(0,0,'More','{$membername}','{$GLOBALS['maction']}','{$usertrueip}',3)");
	if( strlen($BordidList) > 1 ){
		$BordidList{0} = '';
		$BordidList{strlen($BordidList)-1} = '';
	}
	showmsg($ReportText,1,$_SERVER['HTTP_REFERER']);
}//end function MoveBatch

//批量精华
function IsBestBatch(){
	global $lang,$master,$dv,$db,$arrNavMenu,$lang,$boardid,$userid,$membername,$fsetting,$groupsetting,$usertrueip,$_SERVER;
	$arrRootID =& $GLOBALS['arrrootid'];
	if( empty($arrRootID) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings3']);
		return false;
	}
	$Setupreload = false;
	$ReportText = $lang['ReportText'];
	$BordidList = ',';
	foreach( $arrRootID as $RootID ){
		$posttable = $db->scalar("SELECT posttable FROM {$dv}topic WHERE topicid={$RootID}");
		if( empty($posttable) ) continue;
		$reqAnnounceID = intval($GLOBALS["announceid_{$RootID}"]);
		if( $reqAnnounceID <= 0 ){
			$tmpStmt = "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} WHERE rootid={$RootID} AND parentid=0 and forumtype=0";
		} else{
			$tmpStmt = "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} WHERE announceid={$reqAnnounceID}";
		}
		if( $query = $db->query($tmpStmt) ){
			if( $tmpResult =& $db->fetch_array($query,3) ) $db->free_result($query);
		}
		if( empty($tmpResult) ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		if( $tmpResult['boardid'] == 444 || $tmpResult['boardid'] == 777 ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		$currBoardID = intval($tmpResult['boardid']);
		$currAnnounceID = intval($tmpResult['announceid']);//postid
		$tmpBoardData =& LoadBoardData($tmpResult['boardid'],false);
		if( !empty($tmpBoardData) ){
			$Forum_user = explode(',',$tmpBoardData['boarduser']);
		}
		if( $tmpResult['parentid'] == 0 ){
			$ReportText .= str_replace('{$Topic}',$tmpResult['topic'],str_replace('{$ID}',$tmpResult[0],$lang['ReportText8']));
		} else{
			$ReportText .= str_replace('{$ID}',$tmpResult[0],$lang['ReportText9']);
		}
		$db->query("UPDATE {$posttable} SET isbest='1' WHERE announceid={$currAnnounceID}");
		$db->query("UPDATE {$dv}topic SET isbest='1' WHERE topicid={$RootID}");
		$topic = $tmpResult['topic'];
		$topicUserName = $tmpResult['username'];
		$topicUserID = $tmpResult['postuserid'];
		if( $topic == '' ){
			$topic = substr(str_replace(chr(10),',',$tmpResult['body']) ,0,26);
		}
		$dateTimeStr = $tmpResult['dateandtime'];
		$db->query("INSERT INTo {$dv}besttopic(title,boardid,announceid,rootid,postusername,postuserid,dateandtime,expression) VALUES('".addslashes($topic)."' ,{$tmpResult['boardid']} ,{$tmpResult['announceid']} ,{$tmpResult['rootid']} ,'".addslashes($topicUserName)."' ,{$tmpResult['postuserid']} ,'{$dateTimeStr}' ,'{$tmpResult['expression']}')");
		$db->query("UPDATE {$dv}user SET userwealth=userwealth+'{$Forum_user[15]}',usercp=usercp+'{$Forum_user[16]}',userep=userep+'{$Forum_user[17]}',userisbest=userisbest+1 WHERE userid={$topicUserID}");
	}
	$db->query("INSERT INTO {$dv}log(l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type) VALUES(0,0,'More','{$membername}','{$GLOBALS['maction']}','{$usertrueip}',3)");
	if( strlen($BordidList) > 1 ){
		$BordidList{0} = '';
		$BordidList{strlen($BordidList)-1} = '';
	}
	showmsg($ReportText,1,$_SERVER['HTTP_REFERER']);
}//end function MoveBatch



//批量锁定
function LockBatch(){
	global $lang,$master,$dv,$db,$arrNavMenu,$lang,$boardid,$userid,$membername,$fsetting,$groupsetting,$usertrueip,$_SERVER;
	$arrRootID =& $GLOBALS['arrrootid'];
	if( empty($arrRootID) ){
		head(1,0,0,$arrNavMenu);
		showmsg($lang['Error.strings3']);
		return false;
	}
	$Setupreload = false;
	$ReportText = $lang['ReportText'];
	$BordidList = ',';
	foreach( $arrRootID as $RootID ){
		$posttable = $db->scalar("SELECT posttable FROM {$dv}topic WHERE topicid={$RootID}");
		if( empty($posttable) ) continue;
		$reqAnnounceID = intval($GLOBALS["announceid_{$RootID}"]);
		if( $reqAnnounceID <= 0 ){
			$tmpStmt = "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} WHERE rootid={$RootID} AND parentid=0 and forumtype=0";
		} else{
			$tmpStmt = "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} WHERE announceid={$reqAnnounceID}";
		}
		if( $query = $db->query($tmpStmt) ){
			if( $tmpResult =& $db->fetch_array($query,3) ) $db->free_result($query);
		}
		if( empty($tmpResult) ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		if( $tmpResult['boardid'] == 444 || $tmpResult['boardid'] == 777 ){//帖子被删除或在待审核中，跳过操作
			$ReportText .= $lang['ReportText1'];
			continue;
		}
		if( $tmpResult['parentid'] == 0 ){
			$ReportText .= str_replace('{$Topic}',$tmpResult['topic'],str_replace('{$ID}',$tmpResult[0],$lang['ReportText10']));
			$db->query("UPDATE {$dv}topic SET locktopic=1 WHERE topicid={$RootID}");
		} else{
			$ReportText .= str_replace('{$ID}',$tmpResult[0],$lang['ReportText11']);
		}
	}
	$db->query("INSERT INTO {$dv}log(l_announceid,l_boardid,l_touser,l_username,l_content,l_ip,l_type) VALUES(0,0,'More','{$membername}','{$GLOBALS['maction']}','{$usertrueip}',3)");
	if( strlen($BordidList) > 1 ){
		$BordidList{0} = '';
		$BordidList{strlen($BordidList)-1} = '';
	}
	showmsg($ReportText,1,$_SERVER['HTTP_REFERER']);
}//end function MoveBatch

function isWeb_Query(){
	global $boardid,$arrNavMenu,$stats,$dv,$db,$fsetting,$groupsetting,$lang,$userid,$membername,$master,$boardmaster,$superboardmaster,$stype;
	global $selsearch,$SearchMaxPageList,$stable,$keyword,$keywordMBLength,$Foundmykeyword,$psearch,$nsearch,$isweb;
	require printout('query.result');
}//end function isWeb_Query

?>