<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  Header("Location: login.php?action=login&last=".$last);
else
{
  if(isset($_GET['bn']) && isset($_GET['action']) && $_GET['action'] == "submit")
  {
    submitfilter(1,$_GET['bid']);
    $_GET['bn']=submitfilter(2,$_GET['bn']);
    if($_POST['reason']=="" || $_POST['email']=="")
      Header("Location: error.php?id=1&last=apply.php?bid=".$_GET['bid']); 
    else
    {
      $_POST['reason']=html_replace(autolink($_POST['reason']));
	  $_POST['email']=html_replace($_POST['email']);
	  $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."apply_master";
      $phpcolor->field="username, barname, why, time, email, type";
      $phpcolor->where="'".$_COOKIE['core_user_name']."', '".$_GET['bn']."', '".$_POST['reason']."', '".$time."', '".$_POST['email']."', 1";
      $phpcolor->write();
	  if($core_rewrite=="1")
	    Header("Location: list-".$_GET['bid']."-0.html");
	  else
	    Header("Location: list.php?bid=".$_GET['bid']);
    }
  }
  else
  {
    submitfilter(1,$_GET['bid']);
    $phpcolor->table=$core_mysql_prefix."bar";
    $phpcolor->field="barname,barmaster";
    $phpcolor->where="id = '".$_GET['bid']."'";
    if($bar=$phpcolor->exist(1))
    {
	  $phpcolor->table=$core_mysql_prefix."apply_master";
	  $phpcolor->field="*";
      $phpcolor->where="username='".$_COOKIE['core_user_name']."' and barname='".$bar['barname']."'";
      if($phpcolor->exist(1))
	  {
	    if($core_rewrite=="1")
		  Header("Location: error.php?id=9&last=list-".$_GET['bid']."-0.html");
	    else
		  Header("Location: error.php?id=9&last=list.php?bid=".$_GET['bid']); 
		exit();
      }
	  $master = split("\|", $bar['barmaster']);
	  $count = count($master);
	  if($bar['barmaster']=="")
	    $count=0;
	  for($i=0;$i<$count;$i++)
	  {
	    if($master[$i]==$_COOKIE['core_user_name'])
	    {
		  if($core_rewrite=="1")
	        Header("Location: list-".$_GET['bid']."-0.html");
	      else
	        Header("Location: list.php?bid=".$_GET['bid']);
	      exit();
	    }
	  }
      if($bar['barmaster']!="" and ($count >= $core_barmastercount))
	  {
	    if($core_rewrite=="1")
	      Header("Location: list-".$_GET['bid']."-0.html");
	    else
	      Header("Location: list.php?bid=".$_GET['bid']);
	  }
	  else
	  {
	    $tpl -> assign("barid", $_GET['bid']);
        $tpl -> assign("barname", $bar['barname']);
        $tpl -> assign("core_header_title", $bar['barname']."吧申请吧主 - ");
        $tpl -> display("$core_style/apply.htm");
	  }
    }
    else
      Header("Location: index.php");
  }
}
?>