<?php
include("require.php");
if(!isset($_GET['action']))
  Header("Location: index.php");
else
{
  $_POST['theme']=html_replace($_POST['theme']);
  $_POST['content']=nl2br(html_replace(autolink($_POST['content'])));
  $preg = '/\@(.*)[\s+\&\<]/U';
  $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"];
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
  $phpcolor->table=$core_mysql_prefix."filter";
  $phpcolor->field="content";
  $phpcolor->where="type='1'";
  $rs=$phpcolor->assignment();
  for ($count=0;$count<count($rs);$count++)
  {
    if($filter=="")
      $filter=$rs[$count]['content'];
    else
	  $filter=$filter."|".$rs[$count]['content'];
  }
  $phpcolor->where="type='3'";
  $rs=$phpcolor->assignment();
  for ($count=0;$count<count($rs);$count++)
  {
    if($filter=="")
      $filters=$rs[$count]['content'];
    else
	  $filters=$filters."|".$rs[$count]['content'];
  }
  if($filters!="" && (eregi($filters,$_POST['theme']) || eregi($filters,$_POST['content'])))
    $state=3;
  $phpcolor->table=$core_mysql_prefix."block";
  $phpcolor->field="*";
  submitfilter(1,$_GET['bid']);
  $phpcolor->where="username='".$author."' and (removetime>'".$time."' or removetime='0000-00-00 00:00:00') and (barid ='".$_GET['bid']."' or barid='0')";
  if($phpcolor->exist(2))
    die("0");
  elseif($filter!="" && (eregi($filter,$_POST['theme']) || eregi($filter,$_POST['content'])))
    die("1");
  elseif((strlen($_POST['theme'])<$core_themeshort || strlen($_POST['theme'])>$core_themelong) && $_GET['action']=="list")
    die("2");
  elseif(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
    die("3");
  elseif($_POST['verifyopen']!=0 && $_POST['verify']!=$_SESSION['auth'])
    die("4");
  elseif($_GET['action']=="read")
  {
    if(isset($_COOKIE["core_user_name"]) && $_POST['namexs']=="1")
    {
	  $phpcolor->table=$core_mysql_prefix."user";
      $phpcolor->field="reply=reply+1";
      $phpcolor->where="username = '".$_COOKIE["core_user_name"]."'";
      $phpcolor->upgrade();
    }
    $phpcolor->table=$core_mysql_prefix."topic";
    $phpcolor->field="id,reply";
	submitfilter(1,$_GET['belong']);
    $phpcolor->where="id = '".$_GET['belong']."'";
    $post=$phpcolor->exist(1);
	$post['reply']=$post['reply']+1;
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',reply = '".$post['reply']."'";
    $phpcolor->upgrade();
	$phpcolor->table=$core_mysql_prefix."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']."','".$state."'";
	$phpcolor->write();
	if($_POST['file']!="")
	{
	  $files = split("\|", $_POST['file']);
	  $phpcolor->table=$core_mysql_prefix."upload";
	  $phpcolor->field="type='1'";
	  for($i=0;$i<count($files);$i++)
	  {
	    $phpcolor->where="id = '".$files[$i]."'";
		$phpcolor->upgrade();
	  }
	}
	$_POST['content']=$_POST['content']." ";
    preg_match_all($preg, $_POST['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))
	  {
        $phpcolor->table=$core_mysql_prefix."message";
        $phpcolor->field="theme, content, addresser, addressee, sent, type";
        $phpcolor->where="'".$author."在回贴中@了您', '用户".$author."在Ta的回复中@了您。查看：[url]".$core_url."read.php?id=".$post['id']."#".$post['reply']."[/url]', '".$email_addresser."', '".$atuser[1][$count]."', '".$time."', '2'";
        $phpcolor->write();
	  }
    }
	if($state==1)
      die("5");
	else
	  die("7");
  }
  elseif($_GET['action']=="list")
  {
    if(isset($_COOKIE["core_user_name"]) && $_POST['namexs']=="1")
    {
	  $phpcolor->table=$core_mysql_prefix."user";
      $phpcolor->field="topic=topic+1";
      $phpcolor->where="username = '".$_COOKIE["core_user_name"]."'";
      $phpcolor->upgrade();
    }
	submitfilter(1,$_GET['belong']);
	$phpcolor->table=$core_mysql_prefix."topic";
    $phpcolor->field="theme, content, file, author, ip, published, republished, belong, state";
    $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','".$_POST['file']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$time."','".$_GET['belong']."','".$state."'";
	$phpcolor->write();
	if($_POST['file']!="")
	{
	  $files = split("\|", $_POST['file']);
	  $phpcolor->table=$core_mysql_prefix."upload";
	  $phpcolor->field="type='1'";
	  for($i=0;$i<count($files);$i++)
	  {
	    $phpcolor->where="id = '".$files[$i]."'";
		$phpcolor->upgrade();
	  }
	}
	$phpcolor->table=$core_mysql_prefix."topic";
	$phpcolor->field="id";
    $phpcolor->where="author='".$author."' and published='".$time."'";
    $rs=$phpcolor->exist(1);
	$_POST['content']=$_POST['content']." ";
    preg_match_all($preg, $_POST['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))
	  {
        $phpcolor->table=$core_mysql_prefix."message";
        $phpcolor->field="theme, content, addresser, addressee, sent, type";
        $phpcolor->where="'".$author."在贴子中@了您', '用户".$author."在Ta的主题中@了您。查看：[url]".$core_url."read.php?id=".$rs['id']."[/url]', '".$email_addresser."', '".$atuser[1][$count]."', '".$time."', '2'";
        $phpcolor->write();
	  }
    }
	if($state==1)
      die("6");
	else
	  die("8");
  }
}
?>