<?php
include("require.php");
if(!isset($_GET['action']))
  jump("index.php");
submitfilter($_GET['bid']);
submitfilter($_GET['belong']);
//初始准备
$_POST['theme']=mb_convert_encoding($_POST['theme'], "gbk", "UTF-8");
$_POST['content']=mb_convert_encoding($_POST['content'], "gbk", "UTF-8");
$_POST['content']=nl2br(autolink($_POST['content']));
$_POST['content']="<phpolor验证，稍后自动去除>".$_POST['content']."<phpolor验证，稍后自动去除>";
$preg = '/\@(.*)[\s+\&\<]/U';
$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
//发贴后状态
$state=1;
if($core_postaudit=="2" || ($core_postaudit==1 && !isset($_COOKIE["core_user_name"])))
  $state=3;
//获取用户名
$author=$_SERVER['REMOTE_ADDR'];
if(isset($_COOKIE["core_user_name"]) && $_POST['namexs']=="1")
  $author = $_COOKIE["core_user_name"];
elseif($core_visitorurl=="0")
{
  //游客不许超链接
  if(strpos($_POST['content'],"[url]"))
    die("12");
}
//获取禁用词语
$phpcolor->table="filter";
$phpcolor->field="content";
$phpcolor->where="type='1' and (fanwei='0' or fanwei='2')";
$rs=$phpcolor->assignment();
for ($count=0;$count<count($rs);$count++)
{
  if(strpos($_POST['theme'],$rs[$count]['content']) || strpos($_POST['content'],$rs[$count]['content']))
    die("1");
}
//获取审核词语
$phpcolor->where="type='3' and (fanwei='0' or fanwei='2')";
$rs=$phpcolor->assignment();
for ($count=0;$count<count($rs);$count++)
{
  if(strpos($_POST['theme'],$rs[$count]['content']) || strpos($_POST['content'],$rs[$count]['content']))
    $state=3;
}
$_POST['content']=str_replace('<phpolor验证，稍后自动去除>','',$_POST['content']);
//禁止发言
$phpcolor->table="block";
$phpcolor->field="*";
$phpcolor->where="(username='".$author."' or username='".$_SERVER['REMOTE_ADDR']."') and (removetime>'".$time."' or removetime='0000-00-00 00:00:00') and (barid ='".$_GET['bid']."' or barid='0')";
if($phpcolor->exist(2))
  die("11");
//主题超过字数限制
if((strlen($_POST['theme'])<$core_themeshort || strlen($_POST['theme'])>$core_themelong) && $_GET['action']=="list")
  die("2");
//内容超过字数限制
if(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
  die("3");
//验证码不正确
if($_POST['verifyopen']!=0 && $_POST['verify']!=$_SESSION['auth'])
  die("4");
//空贴
if(!preg_match("/[\x7f-\xff]|[a-z0-9]/", $_POST['theme']) and  $_GET['action']=="list")
  die("10");
//发表回贴
if($_GET['action']=="read")
{
  //读取主题信息
  $phpcolor->table="topic";
  $phpcolor->field="id,theme,author,belong,top,remind,locks,reply,state";
  $phpcolor->where="id = '".$_GET['belong']."'";
  $post=$phpcolor->exist(1);
  //读取吧信息
  $phpcolor->table="bar";
  $phpcolor->field="rankhf";
  $phpcolor->where="id = '".$post['belong']."'";
  $bar=$phpcolor->exist(1);
  //是否是吧会员/具有管理权限
  $isuser=0;
  $ismaster=0;
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
    $isuser=$phpcolor->isuorm($userv['id'],$post['belong'],1);
	$ismaster=$phpcolor->isuorm($userv['id'],$post['belong'],3);
  }
  if(isset($_COOKIE['core_user_name']) && $_COOKIE["core_user_rank"] != '0')
    $ismaster=1;
  //是否具备发贴权限
  $postopen=$phpcolor->rankpost($post['locks'],$ismaster,$core_visitoropen,$bar['rankhf'],$isuser);
  if($postopen!=1)
    die("11");
  //会员处理
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
    if($isuser)
    {
	  //回贴奖励
      $phpcolor->upbaruser($int_fbh,$post['belong'],$userv['id']);  
	  //更新吧排名（会员发回复贴）
      $phpcolor->upranking($int_fbhfu,$post['belong']);
    }
	//更新吧排名（非会员发回复贴）
    else
	  $phpcolor->upranking($int_fbhfd,$post['belong']);
  }
  //更新吧排名（游客发回复贴）
  else
	$phpcolor->upranking($int_fbhfy,$post['belong']);
  //更新主题信息
  $phpcolor->table="topic";
  if($post['top']=="1" or $post['state']=="2")
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',reply = reply+1";
  else
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',pxtime = '".$time."',reply = reply+1";
  $phpcolor->where="id = '".$_GET['belong']."'";
  $phpcolor->upgrade();
  //回复楼主奖励
  $phpcolor->table="repost";
  $phpcolor->field="*";
  $phpcolor->where="author='".$author."' and belong='".$post['id']."'";
  if(!$phpcolor->exist(2) and !is_ip($post['author']) and $post['author']!=$author)
  {
    $phpcolor->table="user";
    $phpcolor->field="id";
    $phpcolor->where="username = '".$post['author']."'";
    $rs=$phpcolor->exist(1);
    $phpcolor->table="bar_user";
    $phpcolor->field="*";
    $phpcolor->where="uid = '".$rs['id']."' and bid = '".$post['belong']."'";
	if($phpcolor->exist(2))
	  $phpcolor->upbaruser($int_fbf,$post['belong'],$rs['id']);
	if($int_ufbf>0)
      $phpcolor->upintegral($post['author'],"",$int_ufbf,14,$time);
  }
  //写入回复
  $phpcolor->table="repost";
  $phpcolor->field="content, file, author, ip, published, belong, floor, state";
  $phpcolor->where="'".$_POST['content']."','".$_POST['file']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$_GET['belong']."','".($post['reply']+1)."','".$state."'";
  $phpcolor->write();
  $rsid=mysql_insert_id();
  //新鲜事
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
	if($int_ufbh>0)
      $phpcolor->upintegral($_COOKIE['core_user_name'],"",$int_ufbh,13,$time);
    $phpcolor->newswrite($rsid,$time,$userv['id'],6);
  }
  //回贴提醒
  if($post['remind']=="1")
  {
	$neirong=$author."回复了您发布的《".$post['theme']."》。查看：[url]".$core_url."read.php?id=".$post['id']."#".$post['reply']."[/url]";
    $phpcolor->message($neirong,$email_addresser,$post['author'],$time,3);
  }
  //附件处理
  if($_POST['file']!="")
  {
    $files = split("\|", $_POST['file']);
	$fsj = split("\|", $_POST['fsj']);
	$jflx = split("\|", $_POST['jflx']);
    $phpcolor->table="upload";
    for($i=0;$i<count($files);$i++)
    {
      if(!eregi("^[0-9]+$",$fsj[$i]))
        $fsj[$i]="0";
	  if(!(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1"))
        $fsj[$i]=0;
	  $phpcolor->field="filetype=filetype+1,type='".$rsid."',pricetype='".$jflx[$i]."',price='".$fsj[$i]."'";
      $phpcolor->where="id = '".$files[$i]."'";
      $phpcolor->upgrade();
    }
  }
  //at处理
  $_POST['content']=$_POST['content']." ";
  $num = $core_atnum;
  preg_match_all($preg, $_POST['content'], $atuser);
  if($core_atnum=="all" or $core_atnum>count($atuser[1]))
    $num=count($atuser[1]);
  for ($count=0;$count<$num;$count++)
  {
    $phpcolor->table="user";
    $phpcolor->field="*";
    $phpcolor->where="username='".$atuser[1][$count]."'";
    if($phpcolor->exist(2))
    {
	  $neirong="用户".$author."在Ta的回复中@了您。查看：[url]".$core_url."read.php?id=".$post['id']."#".$post['reply']."[/url]";
      $phpcolor->message($neirong,$email_addresser,$atuser[1][$count],$time,2);
	}
  }
  //结束
  if($state==1)
    die("5");
  die("7");
}
//发布主题
if($_GET['action']=="list")
{
  //禁止重复发贴
  if($_POST['theme']==$_COOKIE['otheme'])
	die("9");
  //是否是吧会员/具有管理权限
  $isuser=0;
  $ismaster=0;
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
    $isuser=$phpcolor->isuorm($userv['id'],$_GET['belong'],1);
	$ismaster=$phpcolor->isuorm($userv['id'],$_GET['belong'],3);
  }
  if(isset($_COOKIE['core_user_name']) && $_COOKIE["core_user_rank"] != '0')
    $ismaster=1;
  //读取吧信息
  $phpcolor->table="bar";
  $phpcolor->field="rankzt";
  $phpcolor->where="id = '".$_GET['belong']."'";
  $bar=$phpcolor->exist(1);
  //是否具备发贴权限
  $postopen=$phpcolor->rankpost(0,$ismaster,$core_visitoropen,$bar['rankzt'],$isuser);
  if($postopen!=1)
    die("11");
  //会员处理
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
    if($isuser)
	{
	  //发贴奖励
      $phpcolor->upbaruser($int_fbz,$_GET['belong'],$userv['id']);
	  //更新吧排名（会员发主题贴）
	  $phpcolor->upranking($int_fbztu,$_GET['belong']);
    }
	//更新吧排名（非会员发主题贴）
	else
	  $phpcolor->upranking($int_fbztd,$_GET['belong']);
  }
  //更新吧排名（游客发主题贴）
  else
	$phpcolor->upranking($int_fbzty,$_GET['belong']);
  //回贴提醒
  if(!isset($_COOKIE["core_user_name"]) || $_POST['namexs']!="1")
    $_POST['remind'] = "0";
  //写入主题
  $phpcolor->table="topic";
  $phpcolor->field="theme, content, file, author, ip, published, republished, pxtime, belong, state, remind";
  $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','".$_POST['file']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$time."','".$time."','".$_GET['belong']."','".$state."','".$_POST['remind']."'";
  $phpcolor->write();
  $rsid=mysql_insert_id();
  if(isset($_COOKIE['core_user_name']) && $_POST['namexs']=="1")
  {
	//用户积分
	if($int_ufb>0)
      $phpcolor->upintegral($_COOKIE['core_user_name'],"",$int_ufb,12,$time);
	//新鲜事
	$phpcolor->newswrite($rsid,$time,$userv['id'],5);
  }
  setcookie("otheme", $_POST['theme'], time()+$core_time*3600+3600);
  //附件处理
  if($_POST['file']!="")
  {
    $files = split("\|", $_POST['file']);
	$fsj = split("\|", $_POST['fsj']);
	$jflx = split("\|", $_POST['jflx']);
    $phpcolor->table="upload";
    for($i=0;$i<count($files);$i++)
    {
      if(!eregi("^[0-9]+$",$fsj[$i]))
        $fsj[$i]="0";
	  $phpcolor->field="type='".$rsid."',pricetype='".$jflx[$i]."',price='".$fsj[$i]."'";
      $phpcolor->where="id = '".$files[$i]."'";
      $phpcolor->upgrade();
	}
  }
  //at处理
  $_POST['content']=$_POST['content']." ";
  $num=$core_atnum;
  preg_match_all($preg, $_POST['content'], $atuser);
  if($core_atnum=="all" or $core_atnum>count($atuser[1]))
    $num=count($atuser[1]);
  for ($count=0;$count<$num;$count++)
  {
    $phpcolor->table="user";
    $phpcolor->field="*";
    $phpcolor->where="username='".$atuser[1][$count]."'";
    if($phpcolor->exist(2))
    {
      $neirong="用户".$author."在Ta的主题中@了您。查看：[url]".$core_url."read.php?id=".$rs['id']."[/url]";
      $phpcolor->message($neirong,$email_addresser,$atuser[1][$count],$time,2);
	}
  }
  //结束
  if($state==1)
    die("6");
  die("8");
}
?>