<?php
include("require.php");
submitfilter(1,$_GET['bid']);
$phpcolor->table=$core_mysql_prefix."bar";
$phpcolor->field="*";
$phpcolor->where="id = '".$_GET['bid']."'";
if(!($bar=$phpcolor->exist(1)))
  Header("Location: index.php");
elseif($bar['merge']!="")
  Header("Location: create.php?barname=".$bar['merge']);
elseif($bar['state']=="0")
  Header("Location: error.php?id=5");
elseif($bar['state']=="2")
  Header("Location: error.php?id=6");
elseif(!isset($_COOKIE["core_user_name"]))
  Header("Location: login.php?action=login&last=".$last);
else
{
  $ismaster = 0;
  $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'])
      $ismaster = 1;
  }
  if($_COOKIE["core_user_rank"]=="2")
    $ismaster = 1;
  $phpcolor->table=$core_mysql_prefix."bar_user";
  $phpcolor->field="*";
  $phpcolor->where="uid = '".$userv['id']."' and bid = '".$_GET['bid']."'";
  $isuser=$phpcolor->exist(2);
  if(isset($_GET['action']) && $_GET['action']=='join' && $bar['apply']=="0")
  {
    if($isuser)
      Header("Location: error.php?id=15&last=list.php?bid=".$_GET['bid']);
    else
    {
      $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."bar_user";
      $phpcolor->field="uid, bid , integral, accesstime";
	  $phpcolor->where="'".$userv['id']."', '".$_GET['bid']."', '2', '".$time."'";
      $phpcolor->write();
      Header("Location: list.php?bid=".$_GET['bid']);
    }
  }
  elseif(isset($_GET['action']) && $_GET['action']=='applysubmit' && isset($_GET['bn']))
  {
    $_GET['bn']=submitfilter(2,$_GET['bn']);
    $phpcolor->table=$core_mysql_prefix."apply_user";
    $phpcolor->field="*";
    $phpcolor->where="username='".$_COOKIE['core_user_name']."' and object='".$bar['barname']."' and type='1'";
    if($phpcolor->exist(1))
    {
      Header("Location: error.php?id=9"); 
	  exit();
    }
	$_POST['reason']=html_replace($_POST['reason']);
	$phpcolor->table=$core_mysql_prefix."apply_user";
    $phpcolor->field="username, object, why, type";
    $phpcolor->where="'".$_COOKIE["core_user_name"]."', '".$_GET['bn']."', '".$_POST['reason']."', '1'";
    $phpcolor->write();
    if($bar['baruserp']=="1")
      Header("Location: index.php");
    else
      Header("Location: list.php?bid=".$_GET['bid']);
  }
  elseif(isset($_GET['action']) && $_GET['action']=='quit')
  {
    if(!isset($_COOKIE["core_user_name"]))
      Header("Location: login.php?action=login&last=".$last);
    else
    {
      $phpcolor->table=$core_mysql_prefix."bar_user";
      $phpcolor->field="*";
      $phpcolor->where="uid = '".$userv['id']."' and bid = '".$_GET['bid']."'";
      $phpcolor->delete();
      Header("Location: index.php");
    }
  }
  else
  {
    $phpcolor->table=$core_mysql_prefix."bar";
    $friendbar = split("\|", $bar['friendbar']);
    for ($count=0;$count<count($friendbar);$count++)
    {
      $phpcolor->field="id";
      $phpcolor->where="barname = '".$friendbar[$count]."'";
      $friendbarid[$count]=$phpcolor->exist(1);
    }
    $sort = 0;
    if($bar['barclass']!=0)
    {
      $phpcolor->table=$core_mysql_prefix."class";
      $phpcolor->field="classname";
      $phpcolor->where="id = '".$bar['barclass']."'";
      $sort = $phpcolor->exist(1);
    }
    if($bar['apply']=="1" && !$isuser)
      $audit=1;
    elseif($bar['apply']=="0" && !$isuser)
      Header("Location: bar_user.php?action=join&bid=".$bar['id']);
    else
    {
      $audit=0;
      if(!isset($_GET['page']))
        $_GET['page']=0;
	  $phpcolor->table=$core_mysql_prefix."bar_user";
	  $phpcolor->field="*";
      $phpcolor->where="bid = '".$_GET['bid']."'";
	  $barusercount=$phpcolor->exist(2);
	  $phpcolor->where="bid = '".$_GET['bid']."' limit ".($_GET['page']*50).",50";
      $baruser=$phpcolor->assignment();
	  $page=pages($barusercount,50,$_GET['page'],'bar_user','&bid=',$bar['id'],"0");
	  for ($count=0;$count<count($baruser);$count++)
	  {
	    $phpcolor->table=$core_mysql_prefix."user";
	    $phpcolor->field="username, picture";
        $phpcolor->where="id = '".$baruser[$count]['uid']."'";
	    $rs=$phpcolor->exist(1);
		$baruser[$count]['username']=$rs['username'];
		$baruser[$count]['picture']=$rs['picture'];
	  }
      $tpl -> assign("page", $page);
	  $tpl -> assign("baruser", $baruser);
	}
	$master = split("\|", $bar['barmaster']);
	$tpl -> assign("bar", $bar);
    $tpl -> assign("friendbar", $friendbar);
    $tpl -> assign("friendbarid", $friendbarid);
    $tpl -> assign("sort", $sort['classname']);
    $tpl -> assign("master", $master);
    $tpl -> assign("audit", $audit);
    $tpl -> assign("ismaster", $ismaster);
    $tpl -> assign("core_header_title", $bar['barname']."吧吧内会员 - ");
    $tpl -> display("$core_style/bar_user.htm");
  }
}
?>