<?php
include("require.php");
submitfilter($_GET['id']);
$phpcolor->table="filter";
$phpcolor->field="content";
$phpcolor->where="type='2' and (fanwei='0' or fanwei='2')";
$filter=$phpcolor->assignment();
$phpcolor->table="topic";
$phpcolor->field="*";
$phpcolor->where="id = '".$_GET['id']."'";
$topic = $phpcolor->exist(1);
$topic['content']=post_replace($topic['content']);
$topic['content']=$topic['content']." ";
$preg = '/\@(.*)[\s+\&\<]/U';
preg_match_all($preg, $topic['content'], $atuser);
if($core_atnum=="all" or $core_atnum>count($atuser[1]))
  $num=count($atuser[1]);
else
  $num=$core_atnum;
for ($count=0;$count<$num;$count++)
{
  $phpcolor->table="user";
  $phpcolor->field="*";
  $phpcolor->where="username='".$atuser[1][$count]."'";
  if($phpcolor->exist(2))
	 $topic['content']=str_replace("@".$atuser[1][$count],"<a href='space.php?pages=info&un=".$atuser[1][$count]."'>@".$atuser[1][$count]."</a>",$topic['content']);
}
$phpcolor->table="bar";
$phpcolor->where="id = '".$topic['belong']."'";
$bar = $phpcolor->exist(1);
$bar['url']=htmlurl($core_url,"list",$bar['id'],0,$core_rewrite);
$phpcolor->table="bar_user";
$phpcolor->field="uid";
$phpcolor->where="bid = '".$bar['id']."' and rank=2";
$rs=$phpcolor->assignment();
for($count=0;$count<count($rs);$count++)
{
  $phpcolor->table="user";
  $phpcolor->field="username";
  $phpcolor->where="id = '".$rs[$count]['uid']."'";
  $rsm=$phpcolor->exist(1);
  $master[$count]['username'] = $rsm['username'];
  $master[$count]['uid'] = $rs[$count]['uid'];
}
$ismaster=0;
$isuser=0;
if(isset($_COOKIE["core_user_name"]))
{
  //是否拥有管理权
  $ismaster=$phpcolor->isuorm($userv['id'],$bar['id'],3);
  if($_COOKIE["core_user_rank"] != '0')
    $ismaster=1;
  //是否是吧会员
  $isuser=$phpcolor->isuorm($userv['id'],$bar['id'],1);
}
$phpcolor->field="*";
if($bar['verify']==0)
  $core_vrepost=0;
elseif(($bar['verify']==1 && !isset($_COOKIE["core_user_name"])) || $bar['verify']==2 || ($bar['verify']==3 && $isuser==0))
  $core_vrepost=1;
elseif(isset($_COOKIE["core_user_name"]))
  $core_vrepost=0;
if($topic['vote']=="1")
{
  $phpcolor->table="vote";
  $phpcolor->field="*";
  $phpcolor->where="tid = '".$_GET['id']."'";
  $vote = $phpcolor->exist(1);
  $voteo = split("\|", $vote['options']);
  $voter = split("\|", $vote['result']);
  $votec = count($voter);
  $rs=1;
  for($i=0;$i<$votec;$i++)
  {
    $rs+=$voter[$i];
  }
  for($i=0;$i<$votec;$i++)
  {
    $voters[$i]=number_format($voter[$i]/$rs,2)*50;
  }
  $vote['is']="1";
  $vote['iso']="0";
  if(($vote['state']=="1" && $vote['already']!="" && eregi($vote['already'],$_COOKIE["core_user_name"])) || ($vote['state']=="0" && $vote['already']!="" && eregi($vote['already'],$_SERVER['REMOTE_ADDR'])) || (isset($_GET['votec']) && $ismaster=="1"))
    $vote['iso']="1";
}
for ($count=0;$count<count($filter);$count++)
{
  $topic['content']=str_replace($filter[$count]['content'],"【禁用词语】",$topic['content']);
}
if($topic['file']!="")
  $topic['content'].=$phpcolor->fileshow($topic['file']);
$topic['authorf']=$topic['author'];
if(!($topic['authorip'] = is_ip($topic['author'])))
{
  $phpcolor->table="user";
  $phpcolor->where="username = '".$topic['author']."'";
  $topicuser=$phpcolor->exist(1);
  $phpcolor->table="attest";
  $phpcolor->where="uid='".$topicuser['id']."' and type='1'";
  $topic['authorv']="0";
  if($rs=$phpcolor->exist(1))
  {
    $topic['authorv']="1";
	$topic['authorvxx']=$rs['attest'];
  }
  //是否是VIP会员
  $topic['authorvip']=$phpcolor->isvord($topicuser['id'],1);
  //是否是达人
  $topic['authordr']=$phpcolor->isvord($topicuser['id'],2);
  //一举‘橙’名
  //签到
  $qdqt=date('Y-m-d',strtotime(date('Y-m-d'))+$core_time*3600-86400*$core_yjcm);
  $phpcolor->table="qiandao";
  $phpcolor->where="belong='".$topicuser['id']."' and type='1' and published>'".$qdqt."'";
  $topic['authorc']="0";
  if($phpcolor->exist(2)>=$core_yjcm)
    $topic['authorc']="1";
  //打卡
  if($topic['authorc']=="0")
  {
    $qdqt=date('Y-m-d',strtotime(date('Y-m-d'))+$core_time*3600-86400*$core_yjcmdb);
    $phpcolor->table="punch";
    $phpcolor->where="uid='".$topicuser['id']."' and bid='".$topic['belong']."' and published>'".$qdqt."'";
	if($phpcolor->exist(2)>=$core_yjcmdb)
      $topic['authorc']="1";
  }
  $phpcolor->table="bar_user";
  $phpcolor->where="uid = '".$topicuser['id']."' and bid = '".$topic['belong']."'";
  $topicusergroup=$phpcolor->exist(1);
  $topicusergroup['dj']=$phpcolor->djpd($bar['usergroup'],$bar['usergroupf'],$topicusergroup['integral']);
  $topicuser["signature"]=preg_replace("/\[img\](.*)\[\/img\]/","<img src=\"\\1\" border=0 onload=\"javascript:if(this.width>500)this.width=200\">",$topicuser["signature"]);
  $topicuser['signature']=str_replace("&lt;br /&gt;","<br />",$topicuser['signature']);
}
else
  $topic['author']=ip_three($topic['author']);
if($topic['id']=="" or $topic['state']=="0" or $topic['state']=="3")
  jump("error.php?id=11");
if($bar['state']=="0" and $_COOKIE["core_user_rank"]!="2")
  jump("error.php?id=5");
if($bar['state']=="2" && $_COOKIE["core_user_rank"]!="2" && $bar['ausername']!=$_SERVER['REMOTE_ADDR'] && $bar['ausername']!=$_COOKIE['core_user_name'])
  jump("error.php?id=6");
if($bar['rankfw']>0 && !isset($_COOKIE["core_user_name"]))
  jump("login.php?action=login&last=read.php?id=".$topic['id']);
if($bar['rankfw']>1 && $isuser==0 && ($_COOKIE["core_user_rank"]=="0" || !isset($_COOKIE["core_user_rank"])))
  jump("bar_user.php?bid=".$bar['id']);
if($bar['rankfw']>2 && $ismaster==0 && ($_COOKIE["core_user_rank"]=="0" || !isset($_COOKIE["core_user_rank"])))
  jump("error.php?id=5");
//是否具备发贴权限
$postopen=$phpcolor->rankpost($topic['locks'],$ismaster,$core_visitoropen,$bar['rankhf'],$isuser);
$phpcolor->table="topic";
$phpcolor->where="id = '".$_GET['id']."'";
$topic['click']=$topic['click']+1;
$phpcolor->field="click = '".$topic['click']."'";
$phpcolor->upgrade();
if(!isset($_GET['page']))
  $_GET['page']=0;
$phpcolor->table="repost";
$phpcolor->field="*";
if(isset($_GET['onlyone']))
  $phpcolor->where="belong = '".$_GET['id']."' and author = '".$topic['author']."' and state = '1'";
else
  $phpcolor->where="belong = '".$_GET['id']."' and state = '1'";
$count=$phpcolor->exist(2);
$count++;
$page=pages($count,$core_readpage,$_GET['page'],'read','&id=',$_GET['id'],$core_rewrite);
if($_GET['go']=="goend")
{
  if($core_readpage>=$count)
    jump("read.php?id=".$_GET['id']."#post");
  $count=ceil($count/$core_readpage)-1;
  jump("read.php?id=".$_GET['id']."&page=".$count."#post");
}
if(isset($_GET['onlyone']))
{
  if($_GET['page']==0)
    $phpcolor->where="belong = '".$_GET['id']."' and author = '".$topic['author']."' and state = '1' order by published limit ".($core_readpage-1)."";
  else
    $phpcolor->where="belong = '".$_GET['id']."' and author = '".$topic['author']."' and state = '1' order by published limit ".($_GET['page']*$core_readpage-1).",".$core_readpage."";
}
else
{
  if($_GET['page']==0)
    $phpcolor->where="belong = '".$_GET['id']."' and state = '1' order by published limit ".($core_readpage-1)."";
  else
    $phpcolor->where="belong = '".$_GET['id']."' and state = '1' order by published limit ".($_GET['page']*$core_readpage-1).",".$core_readpage."";
}
$post=$phpcolor->assignment();
for($count=0;$count<count($post);$count++)
{
  $post[$count]['content']=post_replace($post[$count]['content']);
  $post[$count]['content']=$post[$count]['content']." ";
  preg_match_all($preg, $post[$count]['content'], $atuser);
  if($core_atnum=="all" or $core_atnum>count($atuser[1]))
    $num=count($atuser[1]);
  else
    $num=$core_atnum;
  for ($count1=0;$count1<$num;$count1++)
  {
    $phpcolor->table="user";
    $phpcolor->field="*";
    $phpcolor->where="username='".$atuser[1][$count1]."'";
    if($phpcolor->exist(2))
      $post[$count]['content']=str_replace("@".$atuser[1][$count1],"<a href='space.php?pages=info&un=".$atuser[1][$count1]."'>@".$atuser[1][$count1]."</a>",$post[$count]['content']);
  }
  for($count1=0;$count1<count($filter);$count1++)
  {
    $post[$count]['content']=str_replace($filter[$count1]['content'],"【禁用词语】",$post[$count]['content']);
  }
  if($post[$count]['file']!="")
    $post[$count]['content'].=$phpcolor->fileshow($post[$count]['file']);
  $post[$count]['authorf']=$post[$count]['author'];
  if(!($post[$count]['authorip']=is_ip($post[$count]['author'])))
  {
    $phpcolor->table="user";
    $phpcolor->where="username = '".$post[$count]['author']."'";
    $user[$count]=$phpcolor->exist(1);
    $phpcolor->table="attest";
    $phpcolor->where="uid='".$user[$count]['id']."' and type='1'";
    $post[$count]['authorv']="0";
    if($rs=$phpcolor->exist(1))
    {
      $post[$count]['authorv']="1";
      $post[$count]['authorvxx']=$rs['attest'];
    }
    //是否是VIP会员
    $post[$count]['authorvip']=$phpcolor->isvord($user[$count]['id'],1);
    //是否是达人
    $post[$count]['authordr']=$phpcolor->isvord($user[$count]['id'],2);
    //一举‘橙’名
	//签到
    $qdqt=date('Y-m-d',strtotime(date('Y-m-d'))+$core_time*3600-86400*$core_yjcm);
    $phpcolor->table="qiandao";
    $phpcolor->where="belong='".$user[$count]['id']."' and type='1' and published>'".$qdqt."'";
    $post[$count]['authorc']="0";
    if($phpcolor->exist(2)>=$core_yjcm)
      $post[$count]['authorc']="1";
	//打卡
	if($post[$count]['authorc']=="0")
    {
      $qdqt=date('Y-m-d',strtotime(date('Y-m-d'))+$core_time*3600-86400*$core_yjcmdb);
      $phpcolor->table="punch";
      $phpcolor->where="uid='".$user[$count]['id']."' and bid='".$topic['belong']."' and published>'".$qdqt."'";
	  if($phpcolor->exist(2)>=$core_yjcmdb)
        $post[$count]['authorc']="1";
    }
    $phpcolor->table="bar_user";
    $phpcolor->where="uid = '".$user[$count]['id']."' and bid = '".$topic['belong']."'";
    $usergroup[$count]=$phpcolor->exist(1);
    $usergroup[$count]['dj']=$phpcolor->djpd($bar['usergroup'],$bar['usergroupf'],$usergroup[$count]['integral']);
    $user[$count]["signature"]=preg_replace("/\[img\](.*)\[\/img\]/","<img src=\"\\1\" border=0 onload=\"javascript:if(this.width>500)this.width=200\">",$user[$count]["signature"]);
    $user[$count]['signature'] = str_replace("&lt;br /&gt;","<br />",$user[$count]['signature']);
  }
  else
    $post[$count]['author']=ip_three($post[$count]['author']);
}
if($_GET['page']==0)
{
  for ($count=1;$count<=count($post);$count++)
  { 
    $post[count($post)-$count+1]=$post[count($post)-$count];
    $user[count($post)-$count+1]=$user[count($post)-$count];
    $usergroup[count($post)-$count+1]=$usergroup[count($post)-$count];
  }
  $post[0]=$topic;
  $user[0]=$topicuser;
  $usergroup[0]=$topicusergroup;
}
$fileopen=0;
if($core_file==2 || ($core_file==1 && isset($_COOKIE["core_user_name"])))
  $fileopen=1;
//广告
$ad[1]["code"]=$phpcolor->adshow(1,$bar['id']);
$ad[2]["code"]=$phpcolor->adshow(2,$bar['id']);
$ad[5]["code"]=$phpcolor->adshow(5,$bar['id']);
$ad[8]["code"]=$phpcolor->adshow(8,$bar['id']);
$ad[9]["code"]=$phpcolor->adshow(9,$bar['id']);
$phpcolor->table="postclass";
$phpcolor->field="id, name";
$phpcolor->where="belong = '".$bar['id']."'";
$postclass=$phpcolor->assignment();
if($isuser!=0)
{
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="uid = '".$userv['id']."' and bid = '".$bar['id']."'";
  $muser=$phpcolor->exist(1);
  $muser['dj']=$phpcolor->djpd($bar['usergroup'],$bar['usergroupf'],$muser['integral']);
  $yztime=date('Y-m-d',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $phpcolor->where="accesstime like '%".$yztime."%' and uid='".$userv['id']."' and bid='".$bar['id']."'";
  $pd=$phpcolor->exist(2);
  if(!$pd)
  {
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
    $phpcolor->field="integral='".($muser['integral']+$int_fwb)."', accesstime='".$time."'";
    $phpcolor->where="uid='".$userv['id']."' and bid='".$bar['id']."'";
    $phpcolor->upgrade();
    //更新吧排名（会员首次访问）
	$phpcolor->upranking($int_hyfws,$bar['bid']);
  }
  //更新吧排名（会员首次访问）
  else
	$phpcolor->upranking($int_hyfw,$bar['bid']);
}
//更新吧排名（登录用户访问）
elseif(isset($_COOKIE["core_user_name"]))
  $phpcolor->upranking($int_zcfw,$bar['bid']);
//更新吧排名（游客访问）
else
  $phpcolor->upranking($int_ykfw,$bar['bid']);
$isauthor="0";
if(isset($_COOKIE["core_user_name"]) && $_COOKIE["core_user_name"]==$topic['author'])
  $isauthor="1";
$zztime=0;
if($core_rmtime>0)
  $zztime=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600-$core_rmtime*86400);
$phpcolor->table="topic";
$phpcolor->field="id, theme";
//吧友热议
$phpcolor->where="belong = '".$bar['id']."' and state = '1' and published>'".$zztime."' order by reply desc limit 6";
$postreyi=$phpcolor->assignment();
for ($count=0;$count<count($postreyi);$count++)
{  
  $postreyi[$count]['url']=htmlurl($core_url,"read",$postreyi[$count]['id'],0,$core_rewrite);
}
//热点关注
$phpcolor->where="belong = '".$bar['id']."' and state = '1' and published>'".$zztime."' order by click desc limit 6";
$postredian=$phpcolor->assignment();
for ($count=0;$count<count($postredian);$count++)
{  
  $postredian[$count]['url']=htmlurl($core_url,"read",$postredian[$count]['id'],0,$core_rewrite);
}
$tpl -> assign("ad", $ad);
$tpl -> assign("postreyi", $postreyi);
$tpl -> assign("postredian", $postredian);
$tpl -> assign("postopen", $postopen);
$tpl -> assign("core_vrepost", $core_vrepost);
$tpl -> assign("fileopen", $fileopen);
$tpl -> assign("master", $master);
$tpl -> assign("ismaster", $ismaster);
$tpl -> assign("page", $page);
$tpl -> assign("post", $post);
$tpl -> assign("topic", $topic);
$tpl -> assign("bar", $bar);
$tpl -> assign("vote", $vote);
$tpl -> assign("voteo", $voteo);
$tpl -> assign("voter", $voter);
$tpl -> assign("votec", $votec);
$tpl -> assign("voters", $voters);
$tpl -> assign("user", $user);
$tpl -> assign("isauthor", $isauthor);
$tpl -> assign("isuser", $isuser);
$tpl -> assign("muser", $muser); 
$tpl -> assign("usergroup", $usergroup);
$tpl -> assign("postclass", $postclass);
$tpl -> assign("post_page", "read");
$tpl -> assign("post_belong", $_GET['id']);
$tpl -> assign("core_header_title", $topic['theme']." 第".($_GET['page']+1)."页 - ".$bar['barname']."吧 - ");
$tpl -> display("$core_style/read.htm");
?>