<?php
include("require.php");
submitfilter(1,$_GET['id']);
$phpcolor->table=$core_mysql_prefix."filter";
$phpcolor->field="content";
$phpcolor->where="type='2'";
$filter=$phpcolor->assignment();
$phpcolor->table=$core_mysql_prefix."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=$core_mysql_prefix."user";
  $phpcolor->field="*";
  $phpcolor->where="username='".$atuser[1][$count]."'";
  if($phpcolor->exist(2))
  {
	 $topic['content']=str_replace("@".$atuser[1][$count],"<a href='info.php?un=".$atuser[1][$count]."'>@".$atuser[1][$count]."</a>",$topic['content']);
  }
}
$phpcolor->table=$core_mysql_prefix."bar";
$phpcolor->where="id = '".$topic['belong']."'";
$bar = $phpcolor->exist(1);
$master = split("\|", $bar['barmaster']);
$ismaster=0;
$isuser=0;
$phpcolor->table=$core_mysql_prefix."bar_user";
$phpcolor->field="*";
$phpcolor->where="bid = '".$bar['id']."'";
$barusercount=$phpcolor->exist(2);
if(isset($_COOKIE["core_user_name"]))
{
  $count = count($master);
  if($bar['barmaster']=="")
    $count=0;
  for($i=0;$i<$count;$i++)
  {
    if($master[$i]==$_COOKIE['core_user_name'])
      $ismaster=1;
  }
  $smaster = split("\|", $bar['sbarmaster']);
  $count = count($smaster);
  if($bar['sbarmaster']=="")
    $count=0;
  for($i=0;$i<$count;$i++)
  {
    if($smaster[$i]==$_COOKIE['core_user_name'])
      $ismaster=1;
  }
  if($_COOKIE["core_user_rank"] != '0')
    $ismaster=1;
  $phpcolor->where="uid = '".$userv['id']."' and bid = '".$bar['id']."'";
  $isuser=$phpcolor->exist(2);
}
if($bar['verify']==0)
  $core_verifyopen=0;
elseif(($bar['verify']==1 && !isset($_COOKIE["core_user_name"])) || $bar['verify']==2 || ($bar['verify']==3 && $isuser==0))
  $core_verifyopen=1;
elseif(isset($_COOKIE["core_user_name"]))
  $core_verifyopen=0;
if($topic['vote']=="1")
{
  $phpcolor->table=$core_mysql_prefix."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']!="")
{
  $files = split("\|", $topic['file']);
  for($i=0;$i<count($files);$i++)
  {
    $phpcolor->table=$core_mysql_prefix."upload";
    $phpcolor->where="id = '".$files[$i]."'";
    $file[$i]=$phpcolor->exist(1);
    if($file[$i]['filetype']==1)
      $topic['content'].="<br /><br />图片附件:<br /><a href='url.php?file=".$file[$i]['filename']."' target=_blank><img src='url.php?file=".$file[$i]['filename']."' border=0 onload='javascript:if(this.width>600)this.width=600' title='点击这里用新窗口浏览图片'></a>";
    else
      $topic['content'].="<br /><br />下载附件: <a href='url.php?file=".$file[$i]['filename']."'>".$file[$i]['oldfilename']."</a> (".number_format($file[$i]['filesize']/1024,0)."KB)";
  }
}
$topic['authorf']=$topic['author'];
if(!($topic['authorip'] = is_ip($topic['author'])))
{
  $phpcolor->table=$core_mysql_prefix."user";
  $phpcolor->where="username = '".$topic['author']."'";
  $topicuser=$phpcolor->exist(1);
  $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")
  Header("Location: error.php?id=11");
elseif($bar['state']=="0")
  Header("Location: error.php?id=5");
elseif($bar['state']=="2")
  Header("Location: error.php?id=6");
else
{
  if($bar['baruserp']=="1" && !isset($_COOKIE["core_user_name"]))
  {
    Header("Location: bar_user.php?bid=".$bar['id']);
    exit();
  }
  elseif($bar['baruserp']=="1")
  {
    $phpcolor->table=$core_mysql_prefix."bar_user";
    $phpcolor->field="*";
    $phpcolor->where="uid = '".$userv['id']."' and bid = '".$bar['id']."'";
    if(!$phpcolor->exist(2))
    {
      Header("Location: bar_user.php?bid=".$bar['id']);
      exit();
    }
  }
  $visitoropen=1;
  if(($bar['visitor']==0 && !isset($_COOKIE["core_user_name"])) || ($bar['visitor']!=1 && $core_visitoropen==0 && !isset($_COOKIE["core_user_name"])))
    $visitoropen=0;
  elseif($bar['baruserp']==2 && (!isset($_COOKIE["core_user_name"]) || $baruser['baruser']=="" || !eregi($baruser['baruser'],$_COOKIE["core_user_name"])))
    $visitoropen=2;
  $phpcolor->table=$core_mysql_prefix."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=$core_mysql_prefix."repost";
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$_GET['id']."' and state = '1' order by published";
  $count=$phpcolor->exist(2);
  $count++;
  $page=pages($count,$core_readpage,$_GET['page'],'read','&id=',$_GET['id'],$core_rewrite);
  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=$core_mysql_prefix."user";
      $phpcolor->field="*";
      $phpcolor->where="username='".$atuser[1][$count1]."'";
      if($phpcolor->exist(2))
      {
	    $post[$count]['content']=str_replace("@".$atuser[1][$count1],"<a href='info.php?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']!="")
    {
      $files = split("\|", $post[$count]['file']);
      for($i=0;$i<count($files);$i++)
      {
        $phpcolor->table=$core_mysql_prefix."upload";
        $phpcolor->where="id = '".$files[$i]."'";
        $file[$i]=$phpcolor->exist(1);
        if($file[$i]['filetype']==1)
          $post[$count]['content'].="<br /><br />图片附件:<br /><a href='url.php?file=".$file[$i]['filename']."' target=_blank><img src='url.php?file=".$file[$i]['filename']."' border=0 onload='javascript:if(this.width>500)this.width=500' title='点击这里用新窗口浏览图片'></a>";
        else
          $post[$count]['content'].="<br /><br />下载附件: <a href='url.php?file=".$file[$i]['filename']."'>".$file[$i]['oldfilename']."</a> (".number_format($file[$i]['filesize']/1024,0)."KB)";
      }
    }
    $post[$count]['authorf']=$post[$count]['author'];
    if(!($post[$count]['authorip']=is_ip($post[$count]['author'])))
    {
      $phpcolor->table=$core_mysql_prefix."user";
      $phpcolor->where="username = '".$post[$count]['author']."'";
      $user[$count]=$phpcolor->exist(1);
      $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];
    }
	$post[0]=$topic;
	$user[0]=$topicuser;
  }
  $fileopen=0;
  if($core_file==2 || ($core_file==1 && isset($_COOKIE["core_user_name"])))
    $fileopen=1;
  $phpcolor->table=$core_mysql_prefix."ad";
  $phpcolor->field="name,site,type,theme,picture,link,code";
  $phpcolor->where="site = 5 order by rand()";
  $rs[5]=$phpcolor->assignment();
  for($count=0;$count<count($rs[5]);$count++)
  {
    if($rs[5][$count]['type']==1)
      $ad[5][$count]="<a href=".$rs[5][$count]['link']." target='_blank'><img src=".$rs[5][$count]['picture']." width='120' height='240' style='border:0px;'></a>";
    elseif($rs[5][$count]['type']==2)
      $ad[5][$count]="<a href=".$rs[5][$count]['link']." target='_blank'>".$rs[5][$count]['theme']."</a>";
    else
      $ad[5][$count]=$rs[5][$count]['code'];
  }
  $phpcolor->where="site = 8 order by rand()";
  $rs[8]=$phpcolor->assignment();
  for ($count=0;$count<count($rs[8]);$count++)
  {
    if($rs[8][$count]['type']==1)
      $ad[8][$count]="<a href=".$rs[8][$count]['link']." target='_blank'><img src=".$rs[8][$count]['picture']." width='200' height='200' style='border:0px;'></a>";
    elseif($rs[8][$count]['type']==2)
      $ad[8][$count]="<a href=".$rs[8][$count]['link']." target='_blank'>".$rs[8][$count]['theme']."</a>";
    else
      $ad[8][$count]=$rs[8][$count]['code'];
  }
  $phpcolor->where="site = 9 order by rand()";
  $rs[9]=$phpcolor->assignment();
  for ($count=0;$count<count($rs[9]);$count++)
  {
    if($rs[9][$count]['type']==1)
      $ad[9][$count]="<a href=".$rs[9][$count]['link']." target='_blank'><img src=".$rs[9][$count]['picture']." width='200' height='200' style='border:0px;'></a>";
    elseif($rs[9][$count]['type']==2)
      $ad[9][$count]="<a href=".$rs[9][$count]['link']." target='_blank'>".$rs[9][$count]['theme']."</a>";
    else
      $ad[9][$count]=$rs[9][$count]['code'];
  }
  $phpcolor->table=$core_mysql_prefix."postclass";
  $phpcolor->field="id, name";
  $phpcolor->where="belong = '".$bar['id']."'";
  $postclass=$phpcolor->assignment();
  $tpl -> assign("ad", $ad);
  $tpl -> assign("visitoropen", $visitoropen);
  $tpl -> assign("core_verifyopen", $core_verifyopen);
  $tpl -> assign("core_opendomain", $core_opendomain);
  $tpl -> assign("core_erdomain", $core_erdomain);
  $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("isuser", $isuser);
  $tpl -> assign("barusercount", $barusercount);
  $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");
}
?>