<?php
define('THIS_SCRIPT', 'update');
include_once 'global.php';
include_once INC_PATH.'dv_code.php';
$lang = load_lang($lang, 'update' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'12,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$action,$reaction,$doaction,$allaction,$fsetting,$boardid,$url,$num,$tablelist,$pic_info1,$pic_info2,$pic_width,$pic_height,$userid,$countid,$result_info,$index,$str,$pen_space,$all_forum,$clear_online,$linkaction;

	switch ($allaction){// 计论坛数据和修复;
		case "upall_forum":
			up_allforum(0);//更新总论坛数据;
		break;
		case "upboard_data":
			up_boarddata();//更新分版面数据;
		break;
		case "upforum_coll":
			up_forumcoll();//更新论坛收藏夹;
		break;
		case "repair_forum":
			up_repair_forum();//修复帖子;
		break;
		case "re_repair_forum":
			up_repair_forum();//继续修复帖子;
		break;
		case "correct":
			correct_forum();//修正帖子UBB标签
		break;
		case "re_correct":
			correct_forum();//继续修正帖子UBB标签
			$pic_info1=$lang['str.info22'];
			$pic_width="1%";
			$pic_info2="0%";
			$pic_height="20";
		break;
	}
	switch ($doaction){//重计用户各项数据;
		case "put":
			count_put();
		break;
		case "reput":
			count_put();
			$pic_info1=$lang['str.info22'];
			$pic_width="1%";
			$pic_info2="0%";
			$pic_height="20";
		break;
		case "grade":
			user_grade();
		break;
		case "regrade":
			user_grade();
			$pic_info1=$lang['str.info22'];
			$pic_width="1%";
			$pic_info2="0%";
			$pic_height="20";
		break;
		case "money":
			user_money();
		break;
		case "remoney":
			user_money();
			$pic_info1=$lang['str.info22'];
			$pic_width="1%";
			$pic_info2="0%";
			$pic_height="20";
		break;
	}
	switch ($index) {
		case "all_forum_data":
			up_allforum(1);
			up_boarddata();
			break;
		case "pen_space_data":
			up_boarddata();
			break;
		case "clear_online_data":
			clear_online_ren();
			break;
	}
include printout('update');
}
function up_boarddata(){//更新分版面数据;
	global $lang,$dv,$db,$u1,$u2,$u3,$u4,$u5,$u6,$u7,$boardid,$fsetting,$pic_info1,$pic_width,$pic_info2,$pic_height,$countid,$allarticle,$alltopic,$alltoday,$b_tablename,$result_info,$forum_nowusebbs;
	$u1=intval($_POST['u1']);
	$u2=intval($_POST['u2']);
	$u3=intval($_POST['u3']);
	$u4=intval($_POST['u4']);
	$u5=intval($_POST['u5']);
	$u6=intval($_POST['u6']);
	$u7=intval($_POST['u7']);
	$boardid=intval($boardid);
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if(intval($boardid)!=0 or intval($boardid)!=""){//'获得要更新表的总数;
		$countid=$db->scalar("SELECT count(1) FROM {$dv}board WHERE boardid=".$boardid."");
	}
	else{
		$countid=$db->scalar("SELECT count(1) FROM {$dv}board");
	}
	if(intval($boardid)!=0 or intval($boardid)!=""){
		$sql_val=$db->query("SELECT boardid,boardtype,child,parentstr,rootid,board_setting,boardtopstr,isgroupsetting FROM {$dv}board WHERE boardid=".$boardid." order by depth desc");
	}
	else{
		 board_child();//统计下属论坛函数
		$sql_val=$db->query("SELECT boardid,boardtype,child,parentstr,rootid,board_setting,boardtopstr,isgroupsetting FROM {$dv}board order by depth desc");
	}
	$allboard=0;
	$nowtime = time();
	while($val=$db->fetch_array($sql_val)){
		$prt_set=explode(",",$val['board_setting']);//'所有主题和帖子;
		$counts=$db->query("SELECT count(1),sum(child) FROM {$dv}topic WHERE boardid=".$val['boardid']."");
		$fcount = $db->fetch_row($counts);
		$count = $fcount[0];
		$sum = $fcount[1];
		if(is_null($count)){
			$count=0;
		}
		if(is_null($sum)){
			$sum=0;
		}
		$alltopic=$count;
		$allarticle=$count+$sum;
		$time1 = strtotime(date('Y-m-d 00:00:00'));
		$time2 = $time1 + (24 * 3600 - 1);
		$alltoday=$db->scalar("SELECT count(1) FROM {$forum_nowusebbs} WHERE boardid=".$val['boardid']." and (dateandtime between ".$time1." and ".$time2.")");//'所有今日帖strtotime(dateAdd('d',-1,gmdate('Y-m-d H:i:s')))
		$get_lasttime="SELECT lastpost FROM {$dv}topic WHERE boardid=".$val['boardid']." order by lastposttime Desc";
		$result=$db->query($get_lasttime,array('pageSize'=>1));//'最后回复信息
		$set=$db->fetch_array($result);
		$lastpost=addslashes(htmlspecialchars($set['lastpost'].""));
		$lpset=explode("$",$lastpost);//'更新当前版面数据
		if(count($lpset)<7){
			$lastpost.="$";//还要进行脏话的过滤;
		}
		$db->query("UPDATE {$dv}board Set postnum=".$allarticle.",topicnum=".$alltopic.",todaynum=".$alltoday.",lastpost='".$lastpost."' WHERE boardid=".$val['boardid']."");
		if($val['child']>0){//'如果当前版面有下属论坛，则更新其数据为下属论坛数据
			//'帖子总数，主题总数，今日帖总数，下属版面数
			if($val['parentstr']==0){
				$parentstr=$val['boardid'];
			}
			else{
				$parentstr=$val['parentstr'].",".$val['boardid'];
			}
			$sql=$db->query("SELECT sum(postnum) as psum,sum(topicnum) as tsum,sum(todaynum) as tosum,count(1) as col FROM {$dv}board WHERE parentid=".$val['boardid']);
			while($list=$db->fetch_array($sql)){//'如果该版面允许发帖，则帖子数应该是该版面帖数+下属版面帖子数
				$allarticle=$allarticle;
				$alltopic=$alltopic;
				$alltoday=$alltoday;
				$allboard=$allboard;
				$allarticle+= $list['psum'];
				$alltopic+= $list['tsum'];
				$alltoday+= $list['tosum'];
				$allboard+= $list['col'];
			}
			$parentstr=$val['boardid'];//'下属版块ID
			$sql=$db->query("SELECT boardid FROM {$dv}board WHERE parentid=".$val['boardid']."");
			while($setlist=$db->fetch_array($sql)){
				$parentstr.=",".$setlist['boardid'];
			}
			$stmt="SELECT lastpost FROM {$dv}topic WHERE boardid in (".$parentstr.") order by lastposttime Desc";
			$result=$db->query($stmt,array('pageSize'=>1));//'最后回复信息
			$arr=$db->fetch_array($result);
			$lastpost=addslashes(htmlspecialchars($arr['lastpost'].""));
			$lpset=explode("$",$lastpost);//'更新版面数据
			if(count($lpset)<7){
				$lastpost.="$";//还要进行脏话的过滤;
			}
			$db->query("UPDATE {$dv}board SET postnum=".$allarticle.",topicnum=".$alltopic.",todaynum=".$alltoday.",lastpost='".$lastpost."' Where boardid=".$val['boardid']."");
		}
		$group_setting=$val['isgroupsetting'];//'更新IsGroupSetting
		$user_count=$db->scalar("SELECT count(1) FROM {$dv}useraccess WHERE uc_boardid=".$val['boardid']."");
		$useraccess_count=$user_count;
		if(is_null($useraccess_count) or $useraccess_count==""){
			$useraccess_count=0;
		}
		if($useraccess_count>0){
			$upgroup_setting="0";
		}
		$sql=$db->query("SELECT groupid FROM {$dv}boardpermission WHERE boardid=".$val['boardid']."");
		while($arr=$db->fetch_array($sql)){
			if($upgroup_setting==""){
				$upgroup_setting=$arr['groupid'];
			}
			else{
				$upgroup_setting.=",".$arr['groupid'];
			}
		}
		if(trim($val['boardtopstr'])!==''){//'更新和清理固顶帖数据(固顶和区域固顶)
			if(substr($val['boardtopstr'], -1, 1) == ',')
				$val['boardtopstr']= substr($val['boardtopstr'], 0, -1);

			$sql=$db->query("SELECT topicid,boardid,istop FROM {$dv}topic WHERE topicid in (".$val['boardtopstr'].")");
			while($set=$db->fetch_array($sql)){
				if($set['boardid']!=444 && $set['boardid']!=777 && $set['istop']>0 && $set['istop']!=3){
					if($boardtopstr==""){
						$boardtopstr=$set['topicid'];
					}
					else{
						$boardtopstr.=",".$set['topicid'];
					}
				}
			}
		}
		$db->query("UPDATE {$dv}board SET boardtopstr='".$boardtopstr."',isgroupsetting='".$upgroup_setting."' WHERE boardid=".$val['boardid']."");
		$useraccess_count="";
		$group_setting="";
		$upgroup_setting="";
		$boardtopstr="";
		//加载取得基本设置数据loadSetup(),LoadBoardList();
		$b_tablename=$val['boardtype'];//得到表的类型;
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
		$result_info.=$val['boardtype'].",".$allarticle.",".$alltopic.",".$alltoday."br";
	}
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
	require_once INC_PATH.'BoardCache.class.php';
	Create_index_forumnum();//20070523
	Create_index_forumnum_group();
}
function board_child(){//统计下属论坛函数
	global $lang,$dv,$db;
	$b_upsql=$db->query("UPDATE {$dv}board SET child=0");
	$b_stmt=$db->query("SELECT boardid, rootid, parentid, depth, child, parentstr FROM {$dv}board ORDER BY boardid DESC");
	$b_num=$db->scalar("SELECT count(1) FROM {$dv}board");
	while($b_arr=$db->fetch_array($b_stmt)){
		if(is_null($b_arr['child']) and intval($b_arr['depth'])>0){
			$upstmt=$db->query("UPDATE {$dv}board SET child=0 WHERE boardid=".$b_arr['boardid']."");
		}
		if(intval($arr['parentid'])==0 and intval($b_arr['depth'])==0){
			$count=$db->scalar("SELECT COUNT(1) FROM {$dv}board WHERE rootid=".$b_arr['rootid']."");
			$count-=1;
			if(is_null($count) or $count<0){
				$count=0;
			}
			if(!empty($b_arr['boardid']) and is_numeric($b_arr['boardid'])){
				$stmt=$db->query("UPDATE {$dv}board SET child=".$count." WHERE boardid=".$b_arr['boardid']."");
			}
		}
		elseif(intval($b_arr['depth'])>1){
			$prt_list=explode(",",$b_arr['parentstr']);
			for($i=0;$i<count($prt_list);$i++){
				$stmt=$db->query("UPDATE {$dv}board SET child=".($b_arr['child']+1)." WHERE boardid=".$prt_list[$i]."");
			}
		}
	}
}
function up_forumcoll(){//更新论坛收藏夹;
	global $lang,$dv,$db,$pic_info1,$pic_width,$pic_info2,$pic_height,$countid;
	$stmt=$db->query("SELECT id,url,username FROM {$dv}bookmark ORDER BY id");
	while($arr=$db->fetch_array($stmt)){
		$sql=$db->query("SELECT username FROM {$dv}user WHERE username='".$arr['username']."'");
		while($set=$db->fetch_array($sql)){
			if(empty($set)){
				$delsql=$db->query("DELETE FROM {$dv}bookmark WHERE id=".intval($arr['id'])."");
			}
			else{
				$rootid =explode("=",explode("&",$arr['url'],1),1);
				if(is_numeric($rootid)){
					$rootid =intval($rootid);					
				}
				else{
					$rootid=0;
				}
				$sel_sql=$db->query("SELECT boardid,topicid,title FROM {$dv}topic WHERE boardid!=444 and boardid!=777 AND topicid=".$rootid."");
				while($list=$db->fetch_array($sel_sql)){
					if(empty($list)){
						$del_sql=$db->query("DELETE FROM {$dv}bookmark WHERE id=".intval($arr['id'])."");
					}
					else{
						$topic =substr(trim($list['title']),-100);
						$url="dispbbs.php?boardid=".intval($list['boardid'])."&id=".intval($list['topicid']);
						$upsql=$db->query("UPDATE {$dv}bookmark SET url='".$url."',topic='".$topic."' WHERE id=".intval($arr['id'])."");
					}
				}
			}
		}
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
	}
	$countid=count($arr);
}
function up_repair_forum(){//修复帖子;
	global $lang,$dv,$db,$upstartid,$upendid,$num,$pic_info1,$pic_width,$pic_info2,$pic_height,$allaction;
	$upstartid=intval($_POST['upstartid']);
	$upendid=intval($_POST['upendid']);
	$maxid= $db->scalar("select max(topicid) from {$dv}topic");
	if($upstartid<=0 || $upendid<=0 || $maxid ==0){
		show_msg($lang['sys.info'],$lang['str.info19'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($upstartid>=$upendid){
		show_msg($lang['sys.info'],$lang['str.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($upendid < $maxid)
		$allaction= 'repair_forum';
	else
		$allaction= 're_repair_forum';
	$stmt=$db->query("SELECT topicid,posttable,isvote FROM {$dv}topic WHERE topicid>=".$upstartid." and topicid<=".$upendid."");
	$updateTotal= 0;
	$updateTopic= 0;
	while($arr=$db->fetch_array($stmt)){
		if(intval($arr['isvote'])==-1){
			continue;
		}
		$sql="SELECT username,dateandtime,topic,announceid,postuserid,rootid,body,boardid FROM ".$arr['posttable']." WHERE rootid=".intval($arr['topicid'])." and forumtype=0 order by announceid desc";
		$result=$db->query($sql,array('pageSize'=>1));
		while($list=$db->fetch_array($result)){
			$topic=str_replace("$","",substr($list['body'],-20));			$mem=$list['username']."$".$list['announceid']."$".$list['dateandtime']."$".$topic."$$".$list['postuserid']."$".$list['rootid']."$".$list['boardid']."$";
			$upsql=$db->query("UPDATE {$dv}topic SET lastpost='".str_replace("'","",$mem)."' WHERE topicid=".intval($arr['topicid'])."");
		}
		$count=$db->scalar("SELECT count(1) FROM ".$arr['posttable']." WHERE rootid=".$arr['topicid']." AND boardid!=444 AND boardid!=777 and forumtype=0");
		$up_sql=$db->query("UPDATE {$dv}topic SET child=".($count-1)." WHERE topicid=".$arr['topicid']."");
		$updateTotal++;
		if($up_sql)
			$updateTopic++;
	}
	$num= $updateTotal;
	$pic_info1="";
	$pic_width="100%";
	if($updateTotal == 0)
		$pic_info2= "100%";
	else
		$pic_info2= intval(($updateTopic/$updateTotal)*100)."%";
	$pic_height="1";
}
function up_allforum($index){//更新总论坛数据
	global $lang,$dv,$db,$u1,$u2,$u3,$u4,$u5,$u6,$u7,$str;
	$u1=intval($_POST['u1']);
	$u2=intval($_POST['u2']);
	$u3=intval($_POST['u3']);
	$u4=intval($_POST['u4']);
	$u5=intval($_POST['u5']);
	$u6=intval($_POST['u6']);
	$u7=intval($_POST['u7']);
	if($u1==1 or $index==1){
		$topicnum=gettopicnum();//主题数;
		if($topicnum==''){
			$topicnum=0;
		}
		if($strsql==""){
			$strsql= "forum_topicnum=".$topicnum."";
		}
		else{
			$strsql.=",forum_topicnum=".$topicnum."";
		}
		if($str==""){
			$str=$lang['tmp.info47'].$topicnum.$lang['tmp.info48'];
		}
		else{
			$str.=",".$topicnum.$lang['tmp.info48'];
		}
	}
	if($u2==1 or $index==1){
		$postnum=announcenum();//帖子数;
		if($postnum==''){
			$postnum=0;
		}
		if($strsql==""){
			$strsql="forum_postnum=".$postnum."";
		}
		else{
			$strsql.=",forum_postnum=".$postnum."";
		}
		if($str==""){
			$str=$lang['tmp.info47'].$postnum.$lang['tmp.info49'];
		}
		else{
			$str.=",".$postnum.$lang['tmp.info49'];
		}
	}
	if($u3==1 or $index==1){
		$usernum=allusers();//用户数;
		if($usernum==''){
			$usernum=0;
		}
		if($strsql==""){
			$strsql="forum_usernum=".$usernum."";
		}
		else{
			$strsql.=",forum_usernum=".$usernum."";
		}
		if($str==""){
			$str=$lang['tmp.info47'].$usernum.$lang['tmp.info50'];
		}
		else{
			$str.=",".$usernum.$lang['tmp.info50'];
		}
	}
	if($u4==1 or $index==1){
		$todaynum=alltodays();//今日帖子数;
		if($todaynum==''){
			$todaynum=0;
		}
		if($strsql==""){
			$strsql="forum_todaynum=".$todaynum."";
		}
		else{
			$strsql.=",forum_todaynum=".$todaynum."";
		}
		if($str==""){
			$str=$lang['tmp.info47'].$todaynum.$lang['tmp.info51'];
		}
		else{
			$str.=",".$todaynum.$lang['tmp.info51'];
		}
	}
	if($u5==1 or $index==1){
		$yesterdaynum=allyesterdays();//昨天帖子数;
		if($yesterdaynum==''){
			$yesterdaynum=0;
		}
		if($strsql==""){
			$strsql="forum_yesterdaynum=".$yesterdaynum."";
		}
		else{
			$strsql.=",forum_yesterdaynum=".$yesterdaynum."";
		}
		if($str==""){
			$str=$lang['tmp.info47'].$yesterdaynum.$lang['tmp.info52'];
		}
		else{
			$str.=",".$yesterdaynum.$lang['tmp.info52'];
		}
	}
	if($u6==1 or $index==1){
		$alltopnum=forum_alltopnum();//总固顶;
		if($alltopnum==''){
			$alltopnum=0;
		}
		if($strsql==""){
			$strsql="forum_alltopnum='".$alltopnum."'";
		}
		else{
			$strsql.=",forum_alltopnum='".$alltopnum."'";
		}
		if($str==""){
			if($alltopnum==0){
				$str= $lang['tmp.info47'].$alltopnum.$lang['tmp.info53'];
			}
			else{			
				$str= $lang['tmp.info47'].(count(explode(",",$alltopnum))).$lang['tmp.info53'];
			}
		}
		else{
			if($alltopnum==0){
				$str.=",".$alltopnum.$lang['tmp.info53'];
			}
			else{
				$str.=",".(count(explode(",",$alltopnum))).$lang['tmp.info53'];				
			}
		}
	}
	if($u7==1 or $index==1){
		$lastuser=newuser();//最后注册;
		if($strsql==""){
			$strsql="forum_lastuser='".htmlspecialchars($lastuser)."'";
		}
		else{
			$strsql.=",forum_lastuser='".htmlspecialchars($lastuser)."'";
		}
		if($str==""){
			$str=$lang['tmp.info54'].$lastuser."";
		}
		else{
			$str.=",".$lang['tmp.info54'].$lastuser."";
		}
	}
	if($strsql==""){
		show_msg($lang['sys.info'],$lang['tmp.info55'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$upsql=$db->query("UPDATE {$dv}setup SET ".$strsql);
		require_once INC_PATH.'BoardCache.class.php';
		$mycache =& new BoardCache($db);
		$mycache->createForumSetting();
		Create_index_forumnum();//20070523
		Create_index_forumnum_group();
		unset($mycache);
		if($upsql){
			show_msg($lang['sys.info'],$lang['tmp.info57'].$str,1,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
		else{
			show_msg($lang['sys.info'],$lang['tmp.info56'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
	}
	//加载基本设置;loadSetup()
}
function gettopicnum(){
	global $lang,$dv,$db;
	$topicnum= $allusers=$db->scalar("select sum(topicnum) from {$dv}board where parentid=0");
	return $topicnum;
}
function announcenum(){
	global $lang,$dv,$db;
	$forumnum= $allusers=$db->scalar("select sum(postnum) from {$dv}board where parentid=0");
	return $forumnum;
}
function allusers(){//用户数;
	global $lang,$dv,$db;
	$allusers=$db->scalar("SELECT count(1) FROM {$dv}user");
	return ($allusers);
}
function alltodays(){//今日帖子数;
	global $lang,$dv,$db;
	$t_sql=$db->query("select tablename from {$dv}tablelist");
	while($t_arr=$db->fetch_array($t_sql)){
		//$time1 = strtotime(date('Y-m-d 00:00:00')) + date('Z') - getUserTimeZoneOffset();
		$time1 = strtotime(date('Y-m-d 00:00:00'));
		$time2 = $time1 + (24 * 3600 - 1);
		$today_num=$db->scalar("SELECT count(1) FROM ".$t_arr['tablename']." WHERE boardid!=444 and boardid!=777 AND (dateandtime between ".$time1." and ".$time2.")");
		$to_count+=$today_num;
	}
	return ($to_count);
}
function allyesterdays(){//昨天帖子数;
	global $lang,$dv,$db;
	$t_sql=$db->query("select tablename from {$dv}tablelist");
	while($t_arr=$db->fetch_array($t_sql)){
		$time1 = strtotime(date('Y-m-d 00:00:00'));
		$time2 = $time1 - 1;
		$time1 = $time1 - (24 * 3600);
		$yesday_num=$db->scalar("SELECT count(1) FROM ".$t_arr['tablename']." WHERE boardid!=444 and boardid!=777 AND (dateandtime between ".$time1." and ".$time2.")");
		$yes_count+=$yesday_num;
	}
	return ($yes_count);
}
function forum_alltopnum(){//总固顶;
	global $lang,$dv,$db;
	$sql=$db->query("SELECT topicid FROM {$dv}topic WHERE boardid!=444 AND boardid!=777 AND istop=3");
	while($arr=$db->fetch_array($sql)){
		if(empty($arr['topicid'])){
			$forum_alltop_num="";
		}
		else{
			if($forum_alltop_num==""){
				$forum_alltop_num=$arr['topicid'];
			}
			else{
				$forum_alltop_num.=",".$arr['topicid'];
			}
		}
	}
	return ($forum_alltop_num);
}
function newuser(){//最后注册;
	global $lang,$dv,$db;
	$sql="SELECT username FROM {$dv}user ORDER BY userid desc";
	$result=$db->query($sql,array('pageSize'=>1));
	while($arr=$db->fetch_array($result)){
		if(empty($arr)){
			$newuser=$lang['tmp.info59'];
		}
		else{
   			$newuser=$arr['username'];
		}
	}
	return ($newuser);
}
function correct_forum(){//修正帖子UBB标签
	global $lang,$dv,$db,$modstartid,$modendid,$sign_date,$num,$htmlsign,$pic_info1,$pic_info2,$pic_width,$pic_height;
	$modstartid=intval($_POST['modstartid']);
	$modendid=intval($_POST['modendid']);
	$sign_date=$_POST['sign_date'];
	$htmlsign=intval($_POST['htmlsign']);
	if($modstartid<=0 or $modendid<=0){
		show_msg($lang['sys.info'],$lang['str.info19'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($modstartid>$modendid){
		show_msg($lang['sys.info'],$lang['str.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($sign_date!=""){
		if(!ereg("([0-9]{4})-([0-2]{1,2})-([0-3]{1,2})",$sign_date)){
			show_msg($lang['sys.info'],$lang['tmp.info58'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
	}
	$num=$modendid-$modstartid;
	$sql=$db->query("SELECT max(topicid) FROM {$dv}topic");
	$arr=$db->fetch_array($sql);
	$maxid=$arr['max(topicid)'];
	$stmt=$db->query("SELECT topicid,posttable,isvote FROM {$dv}topic WHERE topicid>=".$modstartid." AND topicid<=".$modendid." ORDER BY topicid");
	while($set=$db->fetch_array($stmt)){
		if(intval($set['isvote'])==-1){
			continue;
		}
		$sql=$db->query("SELECT body,isagree,ubblist,topic,dateandtime,announceid FROM ".$set['posttable']." WHERE rootid=".intval($set['topicid'])." and forumtype=0");
		while($list=$db->fetch_array($sql)){
			$vbody=$list['body'];
			$isagree=$list['isagree'];
			if(is_null($isagree)){
				$isagree="";
			}
			if($isagree!=""){
				$isagree=str_replace("[isubb]","",$isagree);
			}
			else{
				$isagree=$list['isagree'];
			}
			if($htmlsign=="Y"){
				$list['body']=$vbody;
			}
			$list['isagree']=$isagree."";
			if(ereg("([0-9]{4})-([0-2]{1,2})-([0-3]{1,2})",$sign_date)){
				$sign_date=time()+date('Z')-getUserTimeZoneOffset()-($sign_date*24-3600);
				if($sign_date>$list['dateandtime']){
					$list['ubblist']=Ubblist($vbody);
				}
				else{
					if($htmlsign=="Y"){
						$vbody=str_replace("[br]","",$vbody);
						$vbody=Ubblist($vbody);
						$list['ubblist']=str_replace("39,","",$vbody);
					}
				}
			}
			else{
				$list['ubblist']=str_replace("39,","",$vbody);
			}
		}
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
	}
}
function count_put(){//重新计算用户发帖;
	global $dv,$db,$lang,$put_startid,$put_endid,$url,$num,$pic_info1,$pic_info2,$pic_width,$pic_height;
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	$put_startid=intval($_POST['put_startid']);
	$put_endid=intval($_POST['put_endid']);
	if($put_startid=="" or $put_startid==0){
		show_msg($lang['sys.info'],$lang['str.info19'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($put_endid==0 or $put_endid==""){
		show_msg($lang['sys.info'],$lang['str.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($put_startid>=$put_endid){
		show_msg($lang['sys.info'],$lang['str.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=intval($put_endid-$put_startid);
	$stmt=$db->query("select userid,username from {$dv}user where userid>={$put_startid} and userid<={$put_endid}");
	while ($arr=$db->fetch_array($stmt)){
		$user_all_topic=user_alltopic($arr['userid']);
		$user_all_forum=user_allforum($arr['userid']);
		$sql=$db->query("update {$dv}user set userpost=".$user_all_forum.",usertopic=".$user_all_topic." where userid='".$arr['userid']."'");
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
	}
$db->free_result($stmt);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function user_alltopic($userid){
	global $dv,$db,$lang;
	$userid_count=$db->scalar("select count(1) from {$dv}topic where boardid<>444 and boardid<>777 and postuserid='".$userid."'");
	return ($userid_count);
$db->free_result($userid_count);
}
function user_allforum($userid){
	global $dv,$db,$lang;
	$stmt=$db->query("select tablename from {$dv}tablelist");
	while($arr=$db->fetch_array($stmt)){
		$userforum_count=$db->scalar("select count(1) from ".$arr['tablename']." where boardid<>444 and boardid<>777 and postuserid='".$userid."'");
		$userforum_num+=$userforum_count;
	}
	return ($userforum_num);
$db->free_result($stmt);
$db->free_result($userforum_count);
}
function user_grade(){//更新用户等级
	global $dv,$db,$lang,$grade_startid,$grade_endid,$num,$pic_info1,$pic_info2,$pic_width,$pic_height;
	$grade_startid=intval($_POST['grade_startid']);
	$grade_endid=intval($_POST['grade_endid']);
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($grade_startid=="" or $grade_startid==0){
		show_msg($lang['sys.info'],$lang['str.info19'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($grade_endid=="" or $grade_endid==0){
		show_msg($lang['sys.info'],$lang['str.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($grade_startid>=$grade_endid){
		show_msg($lang['sys.info'],$lang['str.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=intval($grade_endid-$grade_startid);
	$str_sql=$db->query("select usergroupid from {$dv}usergroups where parentgid not in (0,3)");
	while ($user_idlist=$db->fetch_array($str_sql)){
		$getrow_str.=$user_idlist['usergroupid'].",";
	}
	$getrow_str.="1";
	$sql=$db->query("select usertitle,grouppic,usergroupid,minarticle from {$dv}usergroups where parentgid=3 order by minarticle desc");
	$oldminarticle=0;
	while ($arr=$db->fetch_array($sql)){
		$update_sql=$db->query("update {$dv}user set userclass='".$arr['usertitle']."',titlepic='".$arr['grouppic']."',usergroupid=".$arr['usergroupid']." where usergroupid not in (".$getrow_str.") and userid>={$grade_startid} and userid<={$grade_endid} and userpost<".$oldminarticle." and userpost>=".$arr['minarticle']."");
		$oldminarticle=$arr['minarticle'];
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
	}
$db->free_result($str_sql);
$db->free_result($sql);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function user_money(){//更新用户金钱/经验/魅力
	global $dv,$db,$lang,$money_startid,$money_endid,$num,$pic_info1,$pic_info2,$pic_width,$pic_height,$forum_user;
	$money_startid=$_POST['money_startid'];
	$money_endid=$_POST['money_endid'];
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($money_startid=="" or $money_startid==0){
		show_msg($lang['sys.info'],$lang['str.info19'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($money_endid=="" or $money_endid==0){
		show_msg($lang['sys.info'],$lang['str.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($money_startid>=$money_endid){
		show_msg($lang['sys.info'],$lang['str.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=intval($money_endid-$money_startid);
	$stmt=$db->query("select userlogins,userid,userpost,usertopic,username from {$dv}user where userid>={$money_startid} and userid<={$money_endid}");
	while ($arr=$db->fetch_array($stmt)){
		$userwealth=intval($arr['userlogins'])*$forum_user[4]+intval($arr['usertopic'])*$forum_user[1]+(intval($arr['userpost'])-intval($arr['usertopic']))*$forum_user[2];
		$userep=intval($arr['userlogins'])*$forum_user[9]+intval($arr['usertopic'])*$forum_user[6]+(intval($arr['userpost'])-intval($arr['usertopic']))*$forum_user[7];
		$usercp=intval($arr['userlogins'])*$forum_user[14]+intval($arr['usertopic'])*$forum_user[11]+(intval($arr['userpost'])-intval($arr['usertopic']))*$forum_user[12];
		if(empty($userwealth) or !is_numeric($userwealth)){
			$userwealth=0;
		}
		if (empty($userep) or !is_numeric($userep)){
			$userep=0;
		}
		if (empty($usercp) or !is_numeric($usercp)){
			$usercp=0;
		}
		$sql=$db->query("update {$dv}user set userwealth='".$userwealth."',userep='".$userep."',usercp='".$usercp."' where userid=".intval($arr['userid']));
		$pic_info1="";
		$pic_width="100%";
		$pic_info2="100%";
		$pic_height="1";
	}
$db->free_result($stmt);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function clear_online_ren() {
	global $lang,$dv,$db;
	if($sql=$db->query("delete from {$dv}online")){
		show_msg($lang['sys.info'],$lang['tmp.info63'],1,'','',$lang['url'],$lang['cmdback']);
	exit;		
	}
}
?>