<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  Header("Location: error.php?id=2");
else
{
  submitfilter(1,$_GET['bid']);
  if(!isset($_GET['action']))
    $_GET['action']="basic";
  $ismaster=0;
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['bid']."'";
  $bar=$phpcolor->exist(1);
  $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($ismaster==1 || $_COOKIE["core_user_rank"]!= '0')
  {
    if($_GET['action']=="basic")
	{
	  $sbarmaster = split("\|", $bar['sbarmaster']);
      $friendbar = split("\|", $bar['friendbar']);
	  for ($count=0;$count<count($friendbar);$count++)
	  {
	    $phpcolor->field="id";
        $phpcolor->where="barname = '".$friendbar[$count]."'";
	    $friendbarid[$count]=$phpcolor->exist(1);
	  }
	  $phpcolor->table=$core_mysql_prefix."barmaudit";
	  $phpcolor->field="*";
	  $phpcolor->where="bid='".$_GET['bid']."'";
      $yxg = $phpcolor->exist(2);
	  $tpl -> assign("yxg", $yxg);
	  $tpl -> assign("core_opendomain", $core_opendomain);
	  $tpl -> assign("core_erdomain", $core_erdomain);
	  $tpl -> assign("core_barmgl", $core_barmgl);
	  $tpl -> assign("barsmaster", $sbarmaster);
	  $tpl -> assign("friendbar", $friendbar);
	  $tpl -> assign("friendbarid", $friendbarid);
    }
	elseif($_GET['action']=="block")
	{
	  $blocktime = date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
	  $phpcolor->table=$core_mysql_prefix."block";
      $phpcolor->where="barid = '".$_GET['bid']."' and removetime < '".$blocktime."'";
      $phpcolor->delete();
      $phpcolor->where="barid = '".$_GET['bid']."'";
      $block=$phpcolor->assignment();
	  $tpl -> assign("blockinfo", $block);
	}
	elseif($_GET['action']=="user")
	{
	  $phpcolor->table=$core_mysql_prefix."bar_user";
      $phpcolor->where="bid = '".$bar['id']."'";
      $baruser=$phpcolor->assignment();
	  for($count=0;$count<count($baruser);$count++)
	  {
	    $phpcolor->table=$core_mysql_prefix."user";
	    $phpcolor->field="username";
        $phpcolor->where="id = '".$baruser[$count]['uid']."'";
	    $rs=$phpcolor->exist(1);
		$baruser[$count]['username']=$rs['username'];
	  }
	  $tpl -> assign("baruser", $baruser);
	}
	elseif($_GET['action']=="userdel")
	{
	  $_GET['uid']=submitfilter(1,$_GET['uid']);
	  $phpcolor->table=$core_mysql_prefix."bar_user";
      $phpcolor->field="*";
      $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['bid']."'";
      $phpcolor->delete();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=user");
	}
	elseif($_GET['action']=="userapply")
	{
	  $phpcolor->table=$core_mysql_prefix."apply_user";
      $phpcolor->where="object = '".$bar['barname']."' and type = '1'";
      $userapply=$phpcolor->assignment();
	  $tpl -> assign("userapply", $userapply);
	}
	elseif($_GET['action']=="userok")
	{
	  submitfilter(1,$_GET['i']);
	  $phpcolor->table=$core_mysql_prefix."apply_user";
      $phpcolor->where="id = '".$_GET['i']."'";
      $userapply=$phpcolor->exist(1);
	  $phpcolor->delete();
	  $phpcolor->table=$core_mysql_prefix."user";
	  $phpcolor->field="id";
      $phpcolor->where="username = '".$userapply['username']."'";
      $userid=$phpcolor->exist(1);
	  $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="'".$userid['id']."', '".$_GET['bid']."', '2', '".$time."'";
      $phpcolor->write();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=userapply");
	}
	elseif($_GET['action']=="userno")
	{
	  submitfilter(1,$_GET['i']);
	  $phpcolor->table=$core_mysql_prefix."apply_user";
      $phpcolor->where="id = '".$_GET['i']."'";
      $phpcolor->delete();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=userapply");
	}
	elseif($_GET['action']=="sort")
	{
	  $phpcolor->table=$core_mysql_prefix."class";
      $phpcolor->field="id,classname";
      $phpcolor->where="base = '1' order by queue";
      $sort=$phpcolor->assignment();
	  $tpl -> assign("sort", $sort);
	}
    elseif($_GET['action']=="editpi")
    {
	  if($core_opendomain==1 && $_POST['domain']!="")
	  {
	    $_POST['domain']=bn_replace($_POST['domain']);;
	    $isok = 1;
	    $domain = split("\|", $core_filterdomain);
	    for($i=0;$i<count($domain);$i++)
	    {
	      if($domain[$i]==$_POST['domain'])
	        $isok = 0;
	    }
	    $phpcolor->table=$core_mysql_prefix."bar";
        $phpcolor->field="id";
        $phpcolor->where="domain = '".$_POST['domain']."'";
        if($bardomain=$phpcolor->exist(1))
	      $isok = 0;
		if (preg_match("/[\x7f-\xff]/", $_POST['domain']))
          $isok = 0;
	    if($isok == 0)
	    {
	      Header("Location: error.php?id=18&last=bar_manage.php?bid=".$bar['id']);
	      exit();
	    }
		$phpcolor->where="id = '".$_GET['bid']."'";
	  }
	  $_POST['introduction']=html_replace(autolink($_POST['introduction']));
	  $_POST['picture']=bn_replace($_POST['picture']);
	  $_POST['keyword']=bn_replace($_POST['keyword']);
	  $_POST['description']=bn_replace($_POST['description']);
	  $_POST['baruserp']=bn_replace($_POST['baruserp']);
	  $_POST['apply']=bn_replace($_POST['apply']);
	  $_POST['visitor']=bn_replace($_POST['visitor']);
	  $_POST['verify']=bn_replace($_POST['verify']);
	  $_POST['usernc']=bn_replace($_POST['usernc']);
      if($core_barmgl=="0")
	  {
        $phpcolor->field="domain='".$_POST['domain']."',introduction='".$_POST['introduction']."',picture='".$_POST['picture']."',keyword='".$_POST['keyword']."',description='".$_POST['description']."',usernc='".$_POST['usernc']."',baruserp='".$_POST['baruserp']."',apply='".$_POST['apply']."',visitor='".$_POST['visitor']."',verify='".$_POST['verify']."'";
        $phpcolor->upgrade();
	  }
	  else
	  {
	    $phpcolor->table=$core_mysql_prefix."barmaudit";
		$phpcolor->field="bid, domain, keyword, description, introduction, picture, usernc, baruserp, apply, visitor, verify, way";
		$phpcolor->where="'".$_GET['bid']."', '".$_POST['domain']."','".$_POST['keyword']."','".$_POST['description']."' , '".$_POST['introduction']."', '".$_POST['picture']."', '".$_POST['usernc']."', '".$_POST['baruserp']."', '".$_POST['apply']."', '".$_POST['visitor']."', '".$_POST['verify']."', '".$_POST['way']."'";
        $phpcolor->write();
	  }
	  $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."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'更新贴吧设置，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'";
      $phpcolor->write();
      Header("Location: bar_manage.php?bid=".$bar['id']."&action=basic");
    }
	elseif($_GET['action']=="deldomain")
    {
	  $phpcolor->field="domain=''";
      $phpcolor->upgrade();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=basic");
    }
    elseif($_GET['action']=="addsm")
    {
	  $_POST['addsm']=bn_replace($_POST['addsm']);
	  $phpcolor->table=$core_mysql_prefix."user";
      $phpcolor->where="username = '".$_POST['addsm']."'";
	  if(!$phpcolor->exist(2))
        die("0");
	  else
	  {
	    $smaster = split("\|", $bar['sbarmaster']);
        $count = count($smaster);
        if($bar['sbarmaster']=="")
          $count=0;
        for($i=0;$i<$count;$i++)
        {
          if($smaster[$i]==$_POST['addsm'])
            die("1");
        }
		if($count>=$core_barsmastercount)
		  die("3");
	    if($bar['sbarmaster']!="")
          $bar['sbarmaster']=$bar['sbarmaster']."|".$_POST['addsm'];
	    else
	      $bar['sbarmaster']=$_POST['addsm'];
	    $phpcolor->table=$core_mysql_prefix."bar";
	    $phpcolor->where="id = '".$_GET['bid']."'";
        $phpcolor->field="sbarmaster='".$bar['sbarmaster']."'";
	    $phpcolor->upgrade();
		$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."log";
        $phpcolor->field="operate, username, operatetime, ip, type";
        $phpcolor->where="'增加".$_POST['addsm']."为小吧主，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
        $phpcolor->write();
		die("2");
	  }
    }
    elseif($_GET['action']=="addfb")
    {
	  $_POST['addfb']=bn_replace($_POST['addfb']);
      $phpcolor->where="barname = '".$_POST['addfb']."' and state = '1'";
      if(!$phpcolor->exist(2))
        die("0");
	  elseif($_POST['addfb']==$bar['barname'])
	    die("1");
	  elseif($bar['friendbar']!="" and eregi($bar['friendbar'],$_POST['addfb']))
	    die("2");
	  else
	  {
	    if($bar['friendbar']!="")
          $bar['friendbar']=$bar['friendbar']."|".$_POST['addfb'];
	    else
	      $bar['friendbar']=$_POST['addfb'];
	    $phpcolor->where="id = '".$_GET['bid']."'";
        $phpcolor->field="friendbar='".$bar['friendbar']."'";
	    $phpcolor->upgrade();
		$phpcolor->table=$core_mysql_prefix."log";
        $phpcolor->field="operate, username, operatetime, ip, type";
        $phpcolor->where="'增加".$_POST['addfb']."为友情贴吧，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
        $phpcolor->write();
		die("3");
	  }
    }
    elseif($_GET['action']=="addpc")
    {
	  if($_POST['addpc']=="")
        die("0");
      else
	  {
		$_POST['addpc']=bn_replace($_POST['addpc']);
	    $phpcolor->table=$core_mysql_prefix."postclass";
	    $phpcolor->field="name, belong, queue";
        $phpcolor->where="'".$_POST['addpc']."', '".$_GET['bid']."', '0'";
        $phpcolor->write();
	    $phpcolor->table=$core_mysql_prefix."log";
        $phpcolor->field="operate, username, operatetime, ip, type";
        $phpcolor->where="'增加".$_POST['addpc']."为精品区分类，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
        $phpcolor->write();
	    die("1");
	  }
    }
	elseif($_GET['action']=="pcdel")
    {
	  $phpcolor->table=$core_mysql_prefix."postclass";
      $phpcolor->where="id='".$_GET['id']."'";
      $phpcolor->delete();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=postclass");
    }
    elseif($_GET['action']=="delsm")
    {
	  $_GET['n']=submitfilter(2,$_GET['n']);
	  $bar['sbarmaster']=replacemandf($bar['sbarmaster'],$_GET['n']);
      $phpcolor->field="sbarmaster='".$bar['sbarmaster']."'";
	  $phpcolor->upgrade();
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'删除小吧主".$_GET['n']."，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
      $phpcolor->write();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=basic");
    }
    elseif($_GET['action']=="delfb")
    {
	  $_GET['n']=submitfilter(2,$_GET['n']);
	  $bar['friendbar']=replacemandf($bar['friendbar'],$_GET['n']);
	  $phpcolor->field="friendbar='".$bar['friendbar']."'";
	  $phpcolor->upgrade();
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'删除友情贴吧".$_GET['n']."，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
      $phpcolor->write();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=basic");
    }
	elseif($_GET['action']=="unblock")
    {
	  submitfilter(1,$_GET['i']);
	  $phpcolor->table=$core_mysql_prefix."block";
      $phpcolor->where="id='".$_GET['i']."'";
	  $block=$phpcolor->exist(1);
      $phpcolor->delete();
	  $phpcolor->table=$core_mysql_prefix."log";
      $phpcolor->field="operate, username, operatetime, ip, type";
      $phpcolor->where="'解除由".$block['operate']."封锁的".$block['username']."，贴吧名：".$bar['barname']."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '2'"; 
      $phpcolor->write();
	  Header("Location: bar_manage.php?bid=".$bar['id']."&action=block");
    }
	elseif($_GET['action']=="applys")
    {
      if($_POST['reason']=="" || $_POST['email']=="")
        Header("Location: error.php?id=1&last=bar_manage.php?bid=".$_GET['bid']."&action=apply"); 
	  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']."', '".$bar['barname']."', '".$_POST['reason']."', '".$time."', '".$_POST['email']."', 2";
        $phpcolor->write();
	    Header("Location: bar_manage.php?bid=".$bar['id']."&action=apply");
	  }
    }
	elseif($_GET['action']=="sorts")
    {
      if($_POST['reason']=="")
        Header("Location: error.php?id=1&last=bar_manage.php?bid=".$_GET['bid']."&action=sort"); 
	  else
	  {
	    $_POST['reason']=html_replace($_POST['reason']);
		$_POST['sorts']=html_replace($_POST['sorts']);
	    $phpcolor->table=$core_mysql_prefix."apply_sort";
        $phpcolor->field="username, barname, why, sort";
        $phpcolor->where="'".$_COOKIE['core_user_name']."', '".$bar['barname']."', '".$_POST['reason']."', '".$_POST['sorts']."'";
        $phpcolor->write();
	    Header("Location: bar_manage.php?bid=".$bar['id']."&action=sort");
	  }
    }
	elseif($_GET['action']=="postclass")
    {
      $phpcolor->table=$core_mysql_prefix."postclass";
	  $phpcolor->field="id, name";
	  $phpcolor->where="belong = '".$_GET['bid']."'";
	  $postclass=$phpcolor->assignment();
	  $tpl -> assign("postclass", $postclass);
    }
	$tpl -> assign("bar", $bar);
    $tpl -> assign("action", $_GET['action']);
	$tpl -> assign("core_header_title", $bar['barname']."吧吧务管理 - ");
    $tpl -> display("$core_style/bar_manage.htm");
  }
  else
    Header("Location: error.php?id=2");
}
?>