<?php
include("require.php");
if($_GET['action']=="gobar")
{
  $_POST['searchname']=encode(bn_replace($_POST['searchname']));
  $phpcolor->table="bar";
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_POST['searchname']."'";
  $bar=$phpcolor->exist(1);
  if(isset($bar['id']))
    jump("list.php?bid=".$bar['id']); 
  echo $_POST['searchname']."吧不存在 <a href='action.php?action=create&barname=".$_POST['searchname']."'>创建本吧</a>&nbsp;&nbsp;<a href='index.php'>返回首页</a>".$core_wflow;
  exit();
}
if($_GET['action']=="create")
{
  $_GET['barname']=bn_replace($_GET['barname']);
  $phpcolor->table="bar";
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_GET['barname']."'";
  $bar=$phpcolor->exist(1);
  if(isset($bar['id']))
    jump("list.php?bid=".$bar['id']); 
  if($_GET['barname']=="")
  {
    echo "对不起，吧名不能为空 <a href='index.php'>返回首页</a>".$core_wflow;
    exit();
  }
  if(strlen($_POST['barname'])>30)
  {
    echo "对不起，吧名不能超过30个字符 <a href='index.php'>返回首页</a>".$core_wflow;
    exit();
  }
  if($core_createopen==0)
  {
    echo "对不起，目前系统禁止创建新吧 <a href='index.php'>返回首页</a>".$core_wflow;
    exit();
  }
  if($core_createopen==1 && !isset($_COOKIE["corew_user_name"]))
  {
    echo "对不起，目前仅允许登录会员创建新吧 <a href='index.php'>返回首页</a>".$core_wflow;
    exit();
  }
  $state=1;
  if($core_createaudit==1)
    $state=2;
  $phpcolor->field="barname, usernc, state, usergroup, usergroupf";
  $phpcolor->where="'".$_GET['barname']."', '会员', '".$state."','初级粉丝|中级粉丝|高级粉丝|正式会员|正式会员|核心会员|核心会员|铁杆会员|铁杆会员|知名人士|知名人士|人气楷模|人气楷模|意见领袖|意见领袖|进阶元老|资深元老|荣耀元老','0|5|15|30|50|100|200|500|1000|2000|3000|6000|10000|18000|30000|60000|100000|300000'";
  $phpcolor->write();
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_GET['barname']."'";
  $barid=$phpcolor->exist(1);
  jump("list.php?bid=".$bar['id']); 
}
//发主题
if($_GET['action']=="post")
{
  submitfilter($_GET['belong']);
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  //基础判断
  if((strlen($_POST['theme'])<$core_themeshort || strlen($_POST['theme'])>$core_themelong))
  {
    echo "对不起，主题长度不符合要求 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
	exit();
  }
  if(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
  {
    echo "对不起，内容长度不符合要求 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
    exit();
  }
  if(!preg_match("/[\x7f-\xff]|[a-z0-9]/", $_POST['theme']))
  {
    echo "对不起，您的贴子不含有意义文字 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
    exit();
  }
  //作者&是否是会员/具有管理权限
  $author=$_SERVER['REMOTE_ADDR'];
  $isuser=0;
  $ismaster=0;
  if(isset($_COOKIE["corew_user_name"]))
  {
    $author=$_COOKIE["corew_user_name"];
	$isuser=$phpcolor->isuorm($userv['id'],$_GET['belong'],1);
	$ismaster=$phpcolor->isuorm($userv['id'],$_GET['belong'],3);
  }
  elseif($core_visitorurl=="0")
  {
    //游客不许超链接
    if(strpos($_POST['content'],"[url]"))
	{
      echo "对不起，您没有权限发布超链接 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
      exit();
	}
  }
  //读取吧信息
  $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)
  {
    echo "对不起，您不具备发贴权限 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
	exit();
  }
  //禁止发布
  $phpcolor->table="filter";
  $phpcolor->field="content";
  $phpcolor->where="type='1'";
  $rs=$phpcolor->assignment();
  for ($count=0;$count<count($rs);$count++)
  {
    if(strpos($_POST['theme'],$rs[$count]['theme']) || strpos($_POST['content'],$rs[$count]['content']))
	{
	  echo "对不起，贴子中包含系统禁用词语 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
	  exit();
	}
  }
  //禁止发贴用户
  $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['belong']."' or barid='0')";
  if($phpcolor->exist(2))
  {
    echo "对不起，您目前被禁止在本吧发贴 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
	exit();
  }
  $_POST['theme']=encode($_POST['theme']);
  $_POST['content']=encode(nl2br(autolink($_POST['content'])));
  //贴子状态
  $state=1;
  if($core_postaudit=="2" || ($core_postaudit==1 && !isset($_COOKIE["corew_user_name"])))
    $state=3;
  //写入主题
  $phpcolor->table="topic";
  $phpcolor->field="theme, content, author, ip, published, republished, pxtime, belong, state, remind, ismobile";
  $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$time."','".$time."','".$_GET['belong']."','".$state."','0','1'";
  $phpcolor->write();
  //吧排名
  if(isset($_COOKIE['core_user_name']))
  {
	//更新吧排名（会员发主题贴）
    if($isuser)  
      $phpcolor->upranking($int_fbztu,$_GET['belong']);
	//更新吧排名（非会员主题贴）
    else
	  $phpcolor->upranking($int_fbztd,$_GET['belong']);
  }
  //更新吧排名（游客发主题贴）
  else
	$phpcolor->upranking($int_fbzty,$_GET['belong']);
  jump("list.php?bid=".$_GET['belong']); 
}
//发回复
if($_GET['action']=="repost")
{
  submitfilter($_GET['belong']);
  $_POST['content']=encode(nl2br(autolink($_POST['content'])));
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  //基础判断
  if(strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
  {
    echo "对不起，内容长度不符合要求 <a href='read.php?id=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
    exit();
  }
  //回复状态
  $state=1;
  if($core_postaudit=="2" || ($core_postaudit==1 && !isset($_COOKIE["corew_user_name"])))
    $state=3;
  //主题信息
  $phpcolor->table="topic";
  $phpcolor->field="reply,belong,top,locks";
  $phpcolor->where="id = '".$_GET['belong']."'";
  $post=$phpcolor->exist(1);
  //作者&是否是会员/具有管理权限
  $author=$_SERVER['REMOTE_ADDR'];
  $isuser=0;
  $ismaster=0;
  if(isset($_COOKIE["corew_user_name"]))
  {
    $author=$_COOKIE["corew_user_name"];
	$isuser=$phpcolor->isuorm($userv['id'],$post['belong'],1);
	$ismaster=$phpcolor->isuorm($userv['id'],$post['belong'],3);
  }
  elseif($core_visitorurl=="0")
  {
    //游客不许超链接
    if(strpos($_POST['content'],"[url]"))
	{
      echo "对不起，您没有权限发布超链接 <a href='list.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
      exit();
	}
  }
  //读取吧信息
  $phpcolor->table="bar";
  $phpcolor->field="rankhf";
  $phpcolor->where="id = '".$post['belong']."'";
  $bar=$phpcolor->exist(1);
  //是否具备发贴权限
  $postopen=$phpcolor->rankpost($post['locks'],$ismaster,$core_visitoropen,$bar['rankhf'],$isuser);
  if($postopen!=1)
  {
    echo "对不起，您不具备发贴权限 <a href='read.php?id=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
	exit();
  }
  //禁止发贴用户
  $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 ='".$post['belong']."' or barid='0')";
  if($phpcolor->exist(2))
  {
    echo "对不起，您目前被禁止在本吧发贴 <a href='read.php?id=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
    exit();
  }
  //禁止发布
  $phpcolor->table="filter";
  $phpcolor->field="content";
  $phpcolor->where="type='1'";
  $rs=$phpcolor->assignment();
  for ($count=0;$count<count($rs);$count++)
  {
    if(strpos($_POST['content'],$rs[$count]['content']))
	{
	  echo "对不起，贴子中包含系统禁用词语 <a href='read.php?bid=".$_GET['belong']."'>返回".$core_bartype."</a>".$core_wflow;
      exit();
	}
  }
  //更新主题
  $phpcolor->table="topic";
  if($post['top']=="1")
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',click = click+1,reply = reply+1";
  else
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',pxtime = '".$time."',click = click+1,reply = reply+1";
  $phpcolor->where="id = '".$_GET['belong']."'";
  $phpcolor->upgrade();
  //写入回贴
  $phpcolor->table="repost";
  $phpcolor->field="content, author, ip, published, belong, floor, state, ismobile";
  $phpcolor->where="'".$_POST['content']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$_GET['belong']."','".($post['reply']+1)."','".$state."','1'";
  $phpcolor->write();
  //吧排名
  if(isset($_COOKIE['core_user_name']))
  {
	//更新吧排名（会员发回复贴）
    if($isuser)  
      $phpcolor->upranking($int_fbhfu,$post['belong']);
	//更新吧排名（非会员发回复贴）
    else
	  $phpcolor->upranking($int_fbhfd,$post['belong']);
  }
  //更新吧排名（游客发回复贴）
  else
	$phpcolor->upranking($int_fbhfy,$post['belong']);
  jump("read.php?id=".$_GET['belong']);
}
if($_GET['action']=="login")
{
  $_POST['username']=encode($_POST['username']);
  $phpcolor->table="user";
  $phpcolor->field="passwd";
  $phpcolor->where="username = '".$_POST['username']."' and state != '0' and state != '3'";
  if(!$userinfo=$phpcolor->exist(1))
  {
    echo "用户不存在或已被关闭 <a href='index.php'>返回首页</a>".$core_wflow;
	exit();
  }
  if(md5($_POST['passwd']) != $userinfo['passwd'])
  {
    echo "用户名或密码错误 <a href='index.php'>返回首页</a>".$core_wflow;
	exit();
  }
  setcookie("corew_user_name", $_POST['username'], time()+3600);
  setcookie("corew_user_passwd", md5($userinfo['passwd'].$core_scode), time()+3600);
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $phpcolor->field="logintime='".$time."', loginip='".$_SERVER['REMOTE_ADDR']."'";
  $phpcolor->where="username = '".$_POST['username']."'";
  $phpcolor->upgrade();
  jump("index.php"); 
}
if($_GET['action']=="quit")
{
  setcookie("corew_user_name", "", time()-3600);
  setcookie("corew_user_passwd", "", time()-3600);
  jump("index.php"); 
}
if($_GET['action']=="reg")
{
  if($core_useropen==0)
  {
    echo "目前关闭注册 <a href='index.php'>返回首页</a>".$core_wflow;
	exit();
  }
  $_POST['username']=encode($_POST['username']);
  $_POST['email']=encode($_POST['email']);
  $_POST['safemail']=encode($_POST['safemail']);
  $_POST['question']=encode($_POST['question']);
  $_POST['answer']=encode($_POST['answer']);
  $phpcolor->table="user";
  $phpcolor->field="*";
  $phpcolor->where="username = '".$_POST['username']."'";
  if(($core_filteruser!="" && eregi($core_filteruser,$_POST['username'])) || preg_match("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$_POST['username']))
  {
    echo "对不起，用户名包含系统禁止词语 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if($_POST['question']=="" or $_POST['answer']=="")
  {
    echo "对不起，请将信息填写完整 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if (strlen($_POST['username'])<$core_usershort or strlen($_POST['username'])>$core_userlong)
  {
    echo "对不起，用户名长度不符合要求 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if ($phpcolor->exist(1))
  {
    echo "对不起，用户名已存在 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if (strlen($_POST['passwd'])<$core_pwdshort or strlen($_POST['passwd'])>$core_pwdlong)
  {
    echo "对不起，密码长度不符合要求 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if ($_POST['passwd']!= $_POST['passwdy'])
  {
    echo "对不起，两次输入密码不一致 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  if(!ereg("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$_POST['email']) or !ereg("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$_POST['safemail']))
  {
    echo "对不起，请输入正确邮箱 <a href='reg.php'>返回注册</a>".$core_wflow;
	exit();
  }
  $state=1;
  if($core_useraudit=="1")
    $state=3;
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $user_reg_passwd = md5($_POST['passwd']);
  $phpcolor->field="username, passwd, email, safemail, question, answer, registered, logintime, loginip, state";
  $phpcolor->where="'".$_POST['username']."', '".$user_reg_passwd."', '".$_POST['email']."', '".$_POST['safemail']."', '".$_POST['question']."', '".$_POST['answer']."', '".$time."', '".$time."', '".$_SERVER['REMOTE_ADDR']."', '".$state."'";
  $phpcolor->write();
  if($core_useraudit=="0")
  {
    setcookie("corew_user_name", $_POST['username'], time()+3600);
    setcookie("corew_user_passwd", md5($user_reg_passwd.$core_scode), time()+3600);
  }
  jump("index.php");
}
?>