<?
include("require.php");
if($_GET['action']=="create")
{
  $_GET['barname']=encode($_GET['barname']);
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_GET['barname']."'";
  $bar=$phpcolor->exist(1);
  if(isset($bar['id']))
    Header("Location: list.php?bid=".$bar['id']); 
  else
  {
    if($_GET['barname']=="")
	{
	  echo "<wml><card>\n";
      echo "<p>\n";
      echo "对不起，贴吧名称不能为空 <a href='index.php'>返回首页</a>\n";
      echo "</p>\n";
	  echo "</card></wml>";
	}
    elseif($core_createopen==0)
	{
	  echo "<wml><card>\n";
      echo "<p>\n";
      echo "对不起，目前系统禁止创建新贴吧 <a href='index.php'>返回首页</a>\n";
      echo "</p>\n";
	  echo "</card></wml>";
	}
	elseif($core_createopen==1 && !isset($_COOKIE["core_user_name"]))
    {
	  echo "<wml><card>\n";
      echo "<p>\n";
      echo "对不起，目前仅允许登录会员创建贴吧 <a href='index.php'>返回首页</a>\n";
      echo "</p>\n";
	  echo "</card></wml>";
	}
	else
	{
	  if($core_createaudit==1)
	    $state=2;
	  else
	    $state=1;
	  $_GET['barname']=bn_replace($_GET['barname']);
	  $phpcolor->field="barname, introduction, usernc, state";
      $phpcolor->where="'".$_GET['barname']."', '', '会员', '".$state."'";
      $phpcolor->write();
	  $phpcolor->field="id";
	  $phpcolor->where="barname = '".$_GET['barname']."'";
	  $barid=$phpcolor->exist(1);
	  Header("Location: list.php?bid=".$barid['id']); 
	}
  }
}
if($_POST['action']=="gobar")
{
  $_POST['barname']=encode($_POST['barname']);
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="id";
  $phpcolor->where="barname = '".$_POST['barname']."'";
  $bar=$phpcolor->exist(1);
  if(isset($bar['id']))
    Header("Location: list.php?bid=".$bar['id']); 
  else
  {
    echo "<wml><card>\n";
    echo "<p>\n";
    echo $_POST['barname']."吧不存在 <a href='post.php?action=create&amp;barname=".$_POST['barname']."'>创建本吧</a> <a href='index.php'>返回首页</a>\n";
    echo "</p>\n";
	echo "</card></wml>";
  }
}
if($_POST['action']=="list")
{
  $_POST['theme']=encode($_POST['theme']);
  $_POST['content']=encode($_POST['content']);
  if($_POST['theme']!="" || $_POST['content']!="")
  {
    if(isset($_COOKIE["core_user_name"]))
      $author=$_COOKIE["core_user_name"];
    else
      $author=$_SERVER['REMOTE_ADDR'];
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
    $_POST['theme']=html_replace($_POST['theme']);
    $_POST['content']=nl2br(html_replace(autolink($_POST['content'])));	
	$phpcolor->table=$core_mysql_prefix."topic";
    $phpcolor->field="theme, content, author, ip, published, republished, belong, ismobile";
    $phpcolor->where="'".$_POST['theme']."','".$_POST['content']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$time."','".$_POST['belong']."','1'";
	$phpcolor->write();
    Header("Location: list.php?bid=".$_POST['belong']);
  }
  else
  {
    echo "<wml><card>\n";
    echo "<p>\n";
    echo "主题和内容不能为空 <a href='index.php'>返回首页</a>\n";
    echo "</p>\n";
	echo "</card></wml>";
  }
}
if($_POST['action']=="read")
{
  $_POST['content']=encode($_POST['content']);
  if($_POST['content']!="")
  {
    $phpcolor->table=$core_mysql_prefix."topic";
    $phpcolor->field="click,reply";
    $phpcolor->where="id = '".$_POST['belong']."'";
    $post=$phpcolor->exist(1);
	if(isset($_COOKIE["core_user_name"]))
      $author=$_COOKIE["core_user_name"];
	else
	  $author=$_SERVER['REMOTE_ADDR'];
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
    $_POST['content']=autolink(nl2br(html_replace($_POST['content'])));
    $post['click']=$post['click']+1;
    $post['reply']=$post['reply']+1;
    $phpcolor->field="reauthor='".$author."',republished = '".$time."',click = '".$post['click']."',reply = '".$post['reply']."'";
    $phpcolor->where="id = '".$_POST['belong']."'";
    $phpcolor->upgrade();
	$phpcolor->table=$core_mysql_prefix."repost";
    $phpcolor->field="content, author, ip, published, belong, floor, ismobile";
    $phpcolor->where="'".$_POST['content']."','".$author."','".$_SERVER['REMOTE_ADDR']."','".$time."','".$_POST['belong']."','".$post['reply']."','1'";
	$phpcolor->write();
    Header("Location: read.php?id=".$_POST['belong']);
  }
  else
  {
    echo "<wml><card>\n";
    echo "<p>\n";
    echo "内容不能为空 <a href='index.php'>返回首页</a>\n";
    echo "</p>\n";
	echo "</card></wml>";
  }
}
if($_POST['action']=="login")
{
  $_POST['username']=encode($_POST['username']);
  $phpcolor->table=$core_mysql_prefix."user";
  $phpcolor->field="passwd";
  $phpcolor->where="username = '".$_POST['username']."'";
  $userinfo=$phpcolor->exist(1);
  if(md5($_POST['pwd']) != $userinfo['passwd'])
  {
    echo "<wml><card>\n";
    echo "<p>\n";
    echo "用户名或密码错误 <a href='index.php'>返回首页</a>\n";
    echo "</p>\n";
	echo "</card></wml>";
  }
  else
  {
    setcookie("core_user_name", $_POST['username'], time()+3600);
    setcookie("core_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*60*60);
    $phpcolor->field="logintime='".$time."'";
    $phpcolor->where="username = '".$_POST['username']."'";
    $phpcolor->upgrade();
	Header("Location: index.php"); 
  }
}
if($_GET['action']=="quit")
{
  setcookie("core_user_name", "", time()-3600);
  setcookie("core_user_passwd", "", time()-3600);
  Header("Location: index.php"); 
}
?>