if(substr($_SERVER[HTTP_USER_AGENT],0,4)=='Wget') header("location: http://news.sina.com.cn");
#error_reporting(7);
$templatelist="forum_home";
require "global.php";
include $rootdir."faces/index.php";
global $faces,$aline;
if($_GET[action]=='' and $_POST[action]!='') $_GET[action]=$_POST[action];
if($_GET[action]=='') $_GET[action]='list';
$timenow=date('Y-m-d H:i:s');
if($_SESSION[pic_check]=='') $_SESSION[pic_check]=substr(md5($_SERVER['SERVER_NAME'].time()),0,5);
$boards='';
$rx=$DB->query("SELECT `id`,`title` FROM `${db_prefix}forumname` ");
while($rxa=$DB->fetch_assoc($rx))
{
	$boards.="$rxa[title] ";
}
switch($_GET[action])
{
	case 'fix_users_posts':
		{
			$at=time();
			$userid=intval($_GET[userid]);//过滤Id,Neeao
			if($_GET[userid]>0) $cont="where `userid`>'$userid'";
			else $cont='';
			$re=$DB->query("SELECT userid from `${db_prefix}user` $cont order by userid");
			while($r=$DB->fetch_assoc($re))
			{
				$posts=$DB->fetch_one_assoc("SELECT count(*) as total  from `${db_prefix}forum` where `create_id`='$r[userid]'");
				$postsg=$DB->fetch_one_assoc("SELECT count(*) as total  from `${db_prefix}forum` where `create_id`='$r[userid]' and `jinghua`='1'");
				$DB->query("update `${db_prefix}user` set `forum_posts`='$posts[total]',`forum_posts_good`='$postsg[total]' where `userid`='$r[userid]'");
				if($at+20fetch_one_assoc("select id,bid,jinghua,create_id from `${db_prefix}forum` where `id`='$id'");
			deltopcache($r[top_id]);
			$DB->query("update `${db_prefix}forum` set lasttime=now() where `top_id`='$r[top_id]' and bid='$r[bid]'");
			$DB->query("update `${db_prefix}forumname` set lasttime=now() where `id`='$r[bid]'");
			if($r[id]=='') 	{
				redirect("$phparticleurl/forum.php?action=view&id=$id","哦?!");
				break;
			}
			if(GetForumQuanXian($r[bid])!=1) {
				gotourl("$phparticleurl/forum.php?action=view&id=$id");
				break;
			}
			if($r[jinghua]==1) $r[jinghua]=0;
			else $r[jinghua]=1;
			$DB->query("update `${db_prefix}forum` set `jinghua` = '$r[jinghua]' where `id`='$id'");
			if($r[jinghua]==1)
				$DB->query("update `${db_prefix}user` set `forum_posts_good` =`forum_posts_good`+1  where `userid`='$r[create_id]' limit 1");
			else  $DB->query("update `${db_prefix}user` set `forum_posts_good` =`forum_posts_good`-1  where `userid`='$r[create_id]' and `forum_posts_good`>0 limit 1");
			$DB->query("update `${db_prefix}forumname` set `lasttime`=now() where id='rr[bid]'");
			redirect("$phparticleurl/forum.php?action=view&id=$id","已设为精华贴!");
		}
	case 'go_other_board':
		{	
			$id=intval($_GET[id]);//Neeao 
			$_GET[bid]=intval($_GET[bid]);
			$r=$DB->fetch_one_assoc("SELECT * from `${db_prefix}forum` where `id`='$id'");
			deltopcache($r[top_id]);
			if(GetForumQuanXian($r[bid])!=1) {
				redirect("$phparticleurl/forum.php?action=view&id=$_GET[id]","哦?!");
				break;
			}
			$DB->query("update `${db_prefix}forum` set `bid`='$_GET[bid]' where  `top_id`='$r[top_id]'");
			$DB->query("DELETE FROM `${db_prefix}cache` WHERE `name` = 'ListCache!'");
			$DB->query("update `${db_prefix}forumname` set lasttime=now() where `id`='$_GET[bid]' or `id`='$r[bid]'");
			redirect("$phparticleurl/forum.php?action=view&id=$id","转移完成!");
			break;
		}
	case 'fix1':
		{
			$id=intval($_GET[id]);//Neeao 
			if($id>0) $cont=" and `id`>'$id'"; else $cont='';
			$re=$DB->query("SELECT * from `${db_prefix}forum` where `rid`='0' $cont");
			$at=time();
			while($r=$DB->fetch_assoc($re)) {
				fix($r[id]);
				if($at+20fetch_one_assoc("SELECT * FROM `${db_prefix}forumname` WHERE `id`='$id' limit 1");
			eval('$forum_css= "'.gettemplate('forum_css').'";');
			eval('echo "'.gettemplate('forum_post').'";');
			break;
		}
	case 'SetTop':
		{
			$id = intval($_GET[id]);//过滤ID,By:Neeao
			$r=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forum` WHERE `id`='$id' limit 1");
			$DB->query("update `${db_prefix}forum` set lasttime=now() where `top_id`='$r[top_id]' and bid='$r[bid]'");
			if(GetForumQuanXian($r[bid])==0) break;
			$DB->QUERY("UPDATE `${db_prefix}forum` SET `top` = IF(`top`='0','1','0') where `id`='$r[id]' limit 1");
			$DB->Query("DELETE from `${db_prefix}cache` where `name`='ListCache!' and `d1`='$r[bid]'");
			$DB->query("update `${db_prefix}forumname` set `lasttime`=now() where `id`='$r[bid]'");
			redirect("$phparticleurl/forum.php?action=list&id=$r[bid]","forum_set_top_ok");
			break;
		}
	case 'sp':
		{//将1分支升级为正贴
			//$_GET[id]
			$id = intval($_GET[id]);//过滤ID,By:Neeao
			$r=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forum` where `id`='$id' and `top_id`!=`id` limit 1");
			$DB->query("update `${db_prefix}forum` set lasttime=now() where `top_id`='$r[top_id]' and bid='$r[bid]'");
			if($r[id]!='') {
				$r[title]=str_replace('Re:','',$r[title]);
				$DB->query("update `${db_prefix}forum` set `top_id`='$r[id]',`rid`='0',`title`='$r[title]',`last_relay`=`createtime`,`last_relay_id`=`id` where `id`='$r[id]' limit 1");
				fix($r[id]);
				fix($r[top_id]);
				deltopcache($r[top_id]);
				$row=$DB->fetch_one_assoc("SELECT moditime as last_relay,id as last_relay_id from ${db_prefix}forum where `top_id`='$r[top_id]' order by `moditime` desc limit 1");
				$DB->query("UPDATE ${db_prefix}forum set `last_relay`='$row[last_relay]',`last_relay_id`='$row[last_relay_id]' where `id`='$r[top_id]' limit 1");
				$DB->query("DELETE FROM `${db_prefix}cache` WHERE `name` = 'ListCache!' and `d1`='$r[bid]'");
				$DB->query("update `${db_prefix}forumname` set `lasttime`=now() where `id`='$r[bid]'");
			}
			fix($r[id]);
			fix($r[top_id]);
			deltopcache($r[id]);
			redirect("$phparticleurl/forum.php?action=view&id=$r[id]","forum_split_ok");
			break;
		}
	case 'deletebypassword':
		{
			//通过一个随机密码删除,随机密码在发贴时生成.
			$id = intval($_GET[id]);//过滤ID,By:Neeao
			$r=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forum` WHERE `id`='$id' limit 1");
			if($r[id]=='') die('ok!'); 
				//redirect("$phparticleurl/forum.php","ok");
			if($r[password]==$_GET[password])
			{ 
			        $_SESSION[delete]='1';
				delcache($r[id]);
				deltopcache($r[top_id]);
				$DB->query("delete from `${db_prefix}forum` where `id`='$id' limit 1 ");
				if($r[top_id]==$r[id])
				{
					$DB->query("DELETE   FROM `${db_prefix}forum` where `top_id`='$r[id]'");//删除所有的跟帖
				}
				else  $DB->query("UPDATE `${db_prefix}forum` SET `rid`='$r[rid]' WHERE `rid`='$r[id]' and `type`='b'");//调整下级贴子的首贴号
				$rowa=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum` where `bid`='$r[bid]' and `type`='b' ");
				$row=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum` where `bid`='$r[bid]' and `id`=`top_id` and `type`='b'");
				$DB->query("UPDATE `${db_prefix}forumname` set `lasttime`=now(),`total` = '$row[total]',`total_all`='$rowa[total]' where `id`='$r[bid]' limit 1");
				$DB->query("UPDATE `${db_prefix}user` SET `forum_posts`=`forum_posts`-1 WHERE `userid`='$r[create_id]' and `forum_posts`>0 limit 1");
				setre($r[rid]); //从统计此帖上面的贴子数 //递归
				$lastrelay=$DB->fetch_one_assoc("SELECT moditime,id from `${db_prefix}forum` where `top_id`='$r[top_id]' order by `id` desc limit 1");
				$DB->query("update `${db_prefix}forum` set `last_relay`='$lastrelay[moditime]',`last_relay_id`='$lastrelay[id]' where `top_id`='$r[top_id]' limit 1");
				$row=$DB->fetch_one_assoc("SELECT moditime as last_relay,id as last_relay_id from ${db_prefix}forum where `top_id`='$r[top_id]' order by `moditime` desc limit 1");
				$DB->query("UPDATE ${db_prefix}forum set `last_relay`='$row[last_relay]',`last_relay_id`='$row[last_relay_id]' where `id`='$r[top_id]' limit 1");
				$DB->query("DELETE FROM `${db_prefix}cache` WHERE `name` = 'ListCache!' and `d1`='$r[bid]'");
				$DB->query("update `${db_prefix}forum` set lasttime=now() where `top_id`='$r[top_id]' and bid='$r[bid]'");
			$posts=$DB->fetch_one_assoc("SELECT count(*) as count from ${db_prefix}forum where `create_id`='$r[create_id]' and `bid`='$r[bid]'");
			if($r[id]!=$r[top_id]) {
				fix($r[top_id]);
			}
			gotourl("$phparticleurl/forum.php?action=dels&name_id=$r[create_id]&bid=$r[bid]&title=$r[title]&ip=$r[ip]");
			}
			die('no!');
			break;
		}
	case 'delete':
		{	
			$id = intval($_GET[id]);//过滤ID,By:Neeao
			$r=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forum` WHERE `id`='$id' limit 1");
			if( $_SESSION[delete]!=1 and GetForumQuanXian($r[bid])==0) die('no!');
			$DB->query("update `${db_prefix}forum` set lasttime=now() where `top_id`='$r[top_id]' and bid='$r[bid]'");
			$DB->query("DELETE FROM `${db_prefix}forum` WHERE `id`='$_GET[id]' limit 1");//删
			delcache($r[id]);
			deltopcache($r[top_id]);
			if($r[top_id]==$r[id])
				$DB->query("DELETE   FROM `${db_prefix}forum` where `top_id`='$r[id]'");//删除所有的跟帖
			else  $DB->query("UPDATE `${db_prefix}forum` SET `rid`='$r[rid]' WHERE `rid`='$r[id]' and `type`='b'");//调整下级贴子的首贴号
			$rowa=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum` where `bid`='$r[bid]' and `type`='b' ");
			$row=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum` where `bid`='$r[bid]' and `id`=`top_id` and `type`='b'");
			$DB->query("UPDATE `${db_prefix}forumname` set `lasttime`=now(), `total` = '$row[total]',`total_all`='$rowa[total]' where `id`='$r[bid]' limit 1");
			$DB->query("UPDATE `${db_prefix}user` SET `forum_posts`=`forum_posts`-1 WHERE `userid`='$r[create_id]' and `forum_posts` > 0");
			setre($r[rid]); //从统计此帖上面的贴子数 //递归
			$row=$DB->fetch_one_assoc("SELECT moditime as last_relay,id as last_relay_id from ${db_prefix}forum where `top_id`='$r[top_id]' order by `moditime` desc limit 1");
			$DB->query("UPDATE ${db_prefix}forum set `last_relay`='$row[last_relay]',`last_relay_id`='$row[last_relay_id]' where `id`='$r[top_id]' limit 1");
			$DB->query("DELETE FROM `${db_prefix}cache` WHERE `name` = 'ListCache!' and `d1`='$r[bid]'");
			$DB->query("update `${db_prefix}forum` set `lasttime`=now() where `top_id`='$r[top_id]'");
			if($r[id]!=$r[top_id]) {
				fix($r[top_id]);
				gotourl("$phparticleurl/forum.php?action=view&id=$r[top_id]");
			}else gotourl("$phparticleurl/forum.php?action=list&id=$r[bid]");
			break;
		}
	case 'post2':
		{
if(get_str($_POST[check])!=$_SESSION[pic_check])
{
				redirect("$phparticleurl/forum.php?action=view&id=".$id,"forum_post_ok");
break;
}
			if($_SESSION[pauserinfo][usergroupid]==4) eval('$userinfo="'.gettemplate('forum_register').'";');
			else $userinfo=$_SESSION[pauserinfo][username];
			$name=htmlspecialchars(trim($_POST[name]));
			$password=htmlspecialchars(trim($_POST[usrpass]));
			$email=htmlspecialchars(trim($_POST[email]));
			$_POST[face]=htmlspecialchars(trim($_POST[face]));//将特殊字符转成HTML格式,Neeao
			$_POST[forumid]=intval($_POST[forumid]);
			$_POST[rid]=intval($_POST[rid]);
			if($_SESSION[logined]!=1)
			{
				if(empty($_POST[name]))
				{
					$msg="名字不能为空!";
					eval('echo "'.gettemplate('forum_repost').'";');
					exit();
				}
			}
			if(!empty($_POST[name]))
			{
				$pass=login($password, $name);
				if($pass==0)
				{
					if(adduser($name,$password,$email))
						$pass=login($password, $name);
					else
					{
						$msg="密码不对!'=>'',''=>'','
'=>'');
				$_POST[subject]=htmlspecialchars(trim(strtr($_POST[subject],$trans)));//将特殊字符转成HTML格式,Neeao
				if($_POST[subject]=='')
				{
					$msg="标题不能为空!\nContent-Type: text/plain;\n  charset=\"UTF-8\"\nX-Mailer: PHP"); //!
				}
				if(empty($_POST[rid])) {
					$DB->query("UPDATE `${db_prefix}forumname` SET lasttime=now(),`total_all`=`total_all`+1,`total`=`total`+1 WHERE `id`='$_POST[forumid]' limit 1");
					$DB->query("UPDATE `${db_prefix}forum` SET lasttime=now(),`top_id`=`id` where `id`='$id' limit 1");
					deltopcache($id);
				}
				else  {
					$rad=$DB->fetch_one_assoc("SELECT top_id FROM `${db_prefix}forum` where `id`='$_POST[rid]'");
					$DB->query("UPDATE `${db_prefix}forumname` SET lasttime=now(),`total_all`=`total_all`+1 WHERE `id`='$_POST[forumid]' limit 1");
					$DB->query("UPDATE `${db_prefix}forum` SET `top_id`='$rad[top_id]' where `id`='$id' limit 1");
					$DB->query("update `${db_prefix}forum` set `lasttime`=now() where `top_id`='$rad[top_id]'");
					deltopcache($rad[top_id]);
				}
				$DB->query("UPDATE `${db_prefix}forum` SET `last_relay_id`='$id',`last_relay`='".date('Y-m-d H:i:s')."' WHERE `id`='$rad[top_id]' limit 1");
				//!回帖email
				//!回复加1
				$rid=$_POST[rid];
				$aa=0;
				unset($ids);
				unset($cc);
				unset($to);
				do{
					$aa++;
					if($aa>10) break; //最多10级
					if($rid==0) break;
					$rad=$DB->fetch_one_assoc("SELECT `title`, `top_id`, `create_id`,`rid`,`mail_me`,`createtime` FROM `${db_prefix}forum` where `id`='$rid' limit 1");
					$DB->query("UPDATE `${db_prefix}forum` SET `relaycount`=`relaycount`+1 where `id`='$rid' limit 1");
					if($rad[mail_me]==1)
					{
						$user=$DB->fetch_one_assoc("SELECT username,password,`email`,`userid` FROM `${db_prefix}user` WHERE `userid`='$rad[create_id]' and `mailsend`='0' and `userid`!='$_SESSION[pauserinfo][userid]' limit 1");
						//		if($email!='') {
						//		$DB->query("update `${db_prefix}user` set `email`='$email' where `userid`='$_SESSION[pauserinfo][userid]' limit 1");
						//		$user[email]=$email;
						//		$pauserinfo[email]=$email;
						//		$_SESSION[pauserinfo][email]=$email;
						//		}
						if($user[email]!='' and $user[userid]!='')
						{
							$timea=time();
							$DB->query("update `${db_prefix}user` set `mailsend`='1',`sendtime`='".date('Y-m-d H:i:s',$timea)."' where `userid`='$user[userid]' limit 1");
							$mailmsg="您的标题:$rad[title] $rad[createtime]
								http://$_SERVER[SERVER_NAME]$phparticleurl/forum.php?action=mailview&key=$timea&id=$rid
								回复标题:$_POST[subject] ".date('Y-m-d H:i:s')."
								http://$_SERVER[SERVER_NAME]$phparticleurl/forum.php?action=mvr&key=$timea&id=$rid&relayid=$id 
								在您通过上面的地址看帖子之前,将不会再发提醒邮件给您
								点击这里取消帖子的订阅
								http://$_SERVER[SERVER_NAME]$phparticleurl/forum.php?action=cmm&id=$rid&key=$user[password]
								";
							mail1('=?utf-8?B?'.base64_encode($user[username]).'?=<'.$user[email].'>',"您在[$rf[title]]的贴子有回复了!",$mailmsg,"安恒","forum@anheng.com.cn"); //!
						}
					}
					if($rid==$rad[rid]) {echo "论坛id循环![$rid]$r[title]($r[total]) del ";
				if($r[create_userid]) $userid=$r[create_userid];
				if($forum_line!='') eval('$forum_line.="'.gettemplate('forum_line_hr').'";');
				eval('$forum_line.="'.gettemplate('forum_line').'";');
			}
			if($userid!=''){
				$pagenav="[$user[username] 注册时间:".date('Y-m-d',$user[joindate])."]发布的帖子:$pagenav";
				$_GET[key]=$userid;
			}
			eval('$forum_css="'.gettemplate('forum_css').'";');
			eval('echo "'.gettemplate('forum_list').'";');
			break;
		}
	case 'user_posts':
		{
			if($_GET[bid]=='') $_GET[bid]=intval($_GET[id]);
			if($_GET[name_id]=='') break;
			$_GET[name_id]=intval($_GET[name_id]);
			$isadmin=GetForumQuanXian($_GET[bid]);
			$userfind=" and `create_id`='$_GET[name_id]'";
			$user=$DB->fetch_one_assoc("SELECT * from `${db_prefix}user` where `userid`='$_GET[name_id]'");
			$_GET[bid]=intval($_GET[bid]);
			$foruminfo=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forumname` where `id`='$_GET[bid]'");
			$r=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum`
					where `bid`='$_GET[bid]' and `act`='1' $userfind ");
			pageft($r[total],20);
			$re=$DB->query("SELECT * FROM `${db_prefix}forum` WHERE `bid`='$_GET[bid]' and `act`='1' $userfind order by `id` DESC limit $firstcount,$displaypg");
			$forum_line='';
			while($r=$DB->fetch_assoc($re))
			{
				if($isadmin==1 or $_SESSION[delete]==1) $del="del ";
				if($r[create_userid]) $userid=$r[create_userid];
				if($forum_line!='') eval('$forum_line.="'.gettemplate('forum_line_hr').'";');
				eval('$forum_line.="'.gettemplate('forum_line').'";');
			}
			if($userid!=''){
				$pagenav="[$user[username] 注册时间:".date('Y-m-d',$user[joindate])."]发布的帖子:$pagenav";
				$_GET[key]=$userid;
			}
			eval('$forum_css="'.gettemplate('forum_css').'";');
			eval('echo "'.gettemplate('forum_list').'";');
			break;
		}
	case 'listbytime':
		{
			$foruminfo=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forumname` where `id`='$_GET[id]'");
			pageft($foruminfo[total_all],20);
			$re=$DB->query("SELECT * FROM `${db_prefix}forum` WHERE `act`='1' and `bid`='$_GET[id]'
					order by `id` DESC limit $firstcount,$displaypg");
			$forum_line='';
			while($r=$DB->fetch_assoc($re))
			{
				for($a=0;$a"$key ");
					$r[msg]=strtr($r[msg],$trans);
					$r[create_userid]=strtr($r[create_userid],$trans);
					$r[title]=strtr($r[title],$trans);
				}
				if($forum_line!='') eval('$forum_line.="'.gettemplate('forum_line_hr').'";');
				eval('$forum_line.="'.gettemplate('forum_line').'";');
			}
			eval('$forum_css="'.gettemplate('forum_css').'";');
			eval('echo "'.gettemplate('forum_list').'";');
			break;
		}
	case 'find':
		{	if($_GET[key]=='') break;
			$akey=explode(" ",ucmd($_GET[key]));
			for($a=0;$a0) $findkey.="and  (`create_userid`='$key' or `title` like '%$key%' or `msg` like '%$key%')";
			}
			//echo $findkey;
			$foruminfo=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forumname` where `id`='$_GET[bid]'");
			$r=$DB->fetch_one_assoc("SELECT count(id) as total FROM `${db_prefix}forum`
					where `bid`='$_GET[bid]' and `act`='1' $findkey ");
			pageft($r[total],20);
			$pagenav='【搜索结果】:'.$pagenav;
			$re=$DB->query("SELECT * FROM `${db_prefix}forum` WHERE `act`='1' and `bid`='$_GET[bid]' $findkey
					order by `id` DESC limit $firstcount,$displaypg");
			$forum_line='';
			while($r=$DB->fetch_assoc($re))
			{
				for($a=0;$a"$key ");
					$r[msg]=strtr($r[msg],$trans);
					$r[create_userid]=strtr($r[create_userid],$trans);
					$r[title]=strtr($r[title],$trans);
				}
				if($forum_line!='') eval('$forum_line.="'.gettemplate('forum_line_hr').'";');
				eval('$forum_line.="'.gettemplate('forum_line').'";');
			}
			eval('$forum_css="'.gettemplate('forum_css').'";');
			eval('echo "'.gettemplate('forum_list').'";');
			break;
		}
	default:
		{
			break;
		}
}
function GetFaceList()
{
	GLOBAL $rootdir,$phparticleurl,$faces,$aline;
	$aline--;
	$a=0;
	while($r=each($faces))
	{
		if($a%8==0) $FaceAll.='';
		$a++;
		eval('$FaceAll.="'.gettemplate('forum_view_post_face').'";');
	}
	return $FaceAll;
}
function fix($id, $top_id = '')
{
	global $DB,$db_prefix;
	if($id==0) return;
	if($top_id=='')
	{
		$fixlast=1; //最后要fix最后1贴
		$top=$DB->fetch_one_assoc("SELECT `top_id` from `${db_prefix}forum` where  `id`='$id' limit 1");
		if($top[top_id]>0)
			$top_id=$top[top_id];
		else $top_id=$id;
		unset($top);
	}
	$r=$DB->fetch_one_assoc("SELECT sum(relaycount) as total from `${db_prefix}forum` WHERE `rid`='$id'");
	$DB->query("UPDATE `${db_prefix}forum` SET `relaycount`='$r[total]' where `id`='$id' limit 1");
	$DB->query("UPDATE `${db_prefix}forum` SET `top_id`='$top_id' where `rid`='$id'"); //更新下级的top_id
	$re=$DB->query("SELECT `id` from `${db_prefix}forum` where `rid`='$id'");
	if($DB->num_rows($re)>0)
		while($row=$DB->fetch_assoc($re))
		{
			fix($row[id],$top_id);
		}
	if($fixlast==1)
	{
		$r=$DB->fetch_one_assoc("SELECT `createtime`,`top_id`,`id` from `${db_prefix}forum` where `top_id`='$top_id' order by id desc limit 1");
		$DB->query("UPDATE `${db_prefix}forum` set `last_relay`='$r[createtime]',`last_relay_id`='$r[id]' where `top_id`='$r[top_id]' and `last_relay`<'$r[createtime]' limit 1");
	}
	return;
}
function setre($id, $top_id = '')
{
//向上统计relaycount
    global $DB,$db_prefix;
    $r=$DB->fetch_one_assoc("SELECT sum(relaycount) as total from `${db_prefix}forum` WHERE `rid`='$id'");
    $DB->query("UPDATE `${db_prefix}forum` SET lasttime=now(),`relaycount`='$r[total]' where `id`='$id' limit 1");
    $r=$DB->fetch_one_assoc("SELECT `id`,`rid` from `${db_prefix}forum` WHERE `id`='$id' limit 1");
    if ($r['id']==$r['rid']) {
        return; //循环
    }
    if ($r['rid']!==0) {
        setre($r['rid'], $top_id);
    }
    return;
}
function GetForumQuanXian($id = '')
{
    //获得当前用户在论坛$bid里面的权限 放进$_SESSION['forumqx'];
    global $db_prefix,$DB,$pauserinfo;
    if ($_SESSION['pauserinfo']['username']=='admin') {
        return 1;
    }
    if ($_SESSION['pauserinfo']['username']=='anheng') {
        return 1;
    }
#if($_SESSION['pauserinfo']['username']=='刘世伟') return 1;
    $r=$DB->fetch_one_assoc("select * from `${db_prefix}manager` where `sortid`='$id' and `type`='board' and `userid`='".$_SESSION['pauserinfo']['userid']."' limit 1");
    if ($r['userid']!='') {
        return 1;
    }
    return 0;
}
function GetATieZi($id)
{
    global $DB,$boards,$cachepath,$db_prefix,$pauserinfo,$phparticleurl,$style,$faces,$rootdir;
    $cachepatha="$cachepath/forum/".intval($id/3000);
    $DB->query("UPDATE `${db_prefix}forum` SET `viewcount`=`viewcount`+1 where `id`='$id'");
    if ($pauserinfo['usergroupid']<3 and  file_exists("$cachepatha/$id.html")) {
        cachetime(filemtime("$cachepatha/$id.html"), 72000);
        return file_get_contents("$cachepatha/$id.html");
        exit();
    }
    $r=$DB->fetch_one_assoc("SELECT * FROM `${db_prefix}forum` WHERE `id`='$id' limit 1");
    if ($r['id']=='') {
        gotourl("http://www.anheng.com");
    }
#$trans=array("\n"=>"';
	if($r[face]!='' and $r[face]!='0') 
	{
		$facesd=$faces[$r[face]];
		eval('$face="'.gettemplate(forum_view_face).'";');
	}
	if($r[id]==$local_id and $local_id!=$r[top_id]) eval('$forum_line.="'.gettemplate('forum_line_local').'";');
	else eval('$forum_line.="'.gettemplate('forum_line').'";');
	$re=$DB->query("SELECT `id`  FROM `${db_prefix}forum` WHERE `rid`='$top_id' $jinghua and `act`='1' order by 'id' DESC $count");
	if($DB->num_rows($re)==0)
	{
		return $forum_line.' ';
	}
	$jibie++;
	if($jibie>0 and $jibie<10) $forum_line.='';
	while($r=$DB->fetch_assoc($re))
	{
		$forum_line.=GetListTree($r[id],$local_id,$jibie,$jh);
	}
	if($jibie>0 and $jibie<10) $forum_line.=' ';
	return $forum_line.'';
}
function ucmd($get)
{
    return(addslashes(htmlspecialchars(trim($get))));
}
function mail1($email, $submit, $msg, $from = '论坛', $fromemail = 'noname@nodomain.com')
{
	mail($email,'=?utf-8?B?'.base64_encode($submit).'?=',$msg,"From: =?utf-8?B?".base64_encode($from)."?=<$fromemail>\r\nContent-Transfer-Encoding: 8bit\r\nContent-Type: text/plain; charset=UTF-8"); //!
}
function delcache($id)
{
	global $rootdir,$cachepath;
	$cachepatha="$cachepath/forum/".intval($id/3000);
	$file=$cachepatha.'/'.$id.'.html';
	if(file_exists($file)) unlink($file);
}
function deltopcache($top_id)
{
	global $DB,$db_prefix;
	$top_id=intval($top_id);
	$re=$DB->query("select id from ${db_prefix}forum where top_id='$top_id'");
	while($a=$DB->fetch_assoc($re)) delcache($a[id]);
}
?>