<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  jump("error.php?id=2");
submitfilter($_GET['bid']);
if(!isset($_GET['action']))
  $_GET['action']="basic";
$ismaster=0;
$phpcolor->table="bar";
$phpcolor->field="*";
$phpcolor->where="id = '".$_GET['bid']."'";
$bar=$phpcolor->exist(1);
$phpcolor->table="bar_user";
$phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$userv['id']."' and rank = 2";
if($phpcolor->exist(2))
  $ismaster=1;
if($ismaster==1 || $_COOKIE["core_user_rank"]!= '0')
{
  if($bar['style']!="0")
  {
    $phpcolor->table="style";
    $phpcolor->field="link,phpfile";
    $phpcolor->where="id=".$bar['style'];
    if($style=$phpcolor->exist(1))
      $core_style=$style['link'];
  }
  $phpcolor->table="bar";
  //贴吧设置
  if($_GET['action']=="basic")
  {
    $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="bar_user";
    $phpcolor->field="uid";
    $phpcolor->where="bid = '".$bar['id']."' and rank=1";
    $rs=$phpcolor->assignment();
    for($count=0;$count<count($rs);$count++)
    {
      $phpcolor->table="user";
      $phpcolor->field="username";
      $phpcolor->where="id = '".$rs[$count]['uid']."'";
      $rsm=$phpcolor->exist(1);
      $smaster[$count]['username'] = $rsm['username'];
      $smaster[$count]['uid'] = $rs[$count]['uid'];
    }
    $phpcolor->table="barmaudit";
    $phpcolor->field="*";
    $phpcolor->where="bid='".$_GET['bid']."'";
    $yxg = $phpcolor->exist(2);
    $phpcolor->table="style";
    $phpcolor->field="*";
    $phpcolor->where="id=".$bar['style']." and type=3";
    if(!$yzs=$phpcolor->exist(1))
    {
      $phpcolor->where="type!=3";
      $style=$phpcolor->assignment();
    }
    $tpl -> assign("yzs", $yzs);
    $tpl -> assign("style", $style);
    $tpl -> assign("yxg", $yxg);
    $tpl -> assign("core_barmgl", $core_barmgl);
    $tpl -> assign("smaster", $smaster);
    $tpl -> assign("friendbar", $friendbar);
    $tpl -> assign("friendbarid", $friendbarid);
  }
  //封锁管理
  if($_GET['action']=="block")
  {
    $blocktime = date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
    $phpcolor->table="block";
    $phpcolor->where="barid = '".$_GET['bid']."' and removetime < '".$blocktime."'";
    $phpcolor->delete();
    $phpcolor->where="barid = '".$_GET['bid']."'";
    $block=$phpcolor->assignment();
    $tpl -> assign("blockinfo", $block);
  }
  //会员管理
  if($_GET['action']=="user")
  {
    $phpcolor->table="bar_user";
    $phpcolor->where="bid = '".$bar['id']."'";
    $baruser=$phpcolor->assignment();
    for($count=0;$count<count($baruser);$count++)
    {
      $phpcolor->table="user";
      $phpcolor->field="username";
      $phpcolor->where="id = '".$baruser[$count]['uid']."'";
      $rs=$phpcolor->exist(1);
      $baruser[$count]['username']=$rs['username'];
	}
    $tpl -> assign("baruser", $baruser);
  }
  //删除贴吧会员
  if($_GET['action']=="userdel")
  {
    submitfilter($_GET['uid']);
    $phpcolor->table="bar_user";
    $phpcolor->field="*";
    $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['bid']."' and rank=2";
    if($phpcolor->exist(2))
      jump("error.php?id=2&last=bar_manage.php?bid=".$bar['id']."=and=action=user");
	$phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['bid']."'";
    $phpcolor->delete();
	//更新吧排名（会员删除）
	$phpcolor->upranking($int_hytc,$_GET['bid']);
	jump("bar_manage.php?bid=".$bar['id']."&action=user");
  }
  //会员申请
  if($_GET['action']=="userapply")
  {
    $phpcolor->table="apply_user";
    $phpcolor->where="object = '".$bar['barname']."' and type = '1'";
    $userapply=$phpcolor->assignment();
    $tpl -> assign("userapply", $userapply);
  }
  //会员申请通过
  if($_GET['action']=="userok")
  {
    submitfilter($_GET['i']);
    $phpcolor->table="apply_user";
    $phpcolor->where="id = '".$_GET['i']."'";
    $userapply=$phpcolor->exist(1);
    $phpcolor->delete();
    $phpcolor->table="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*3600);
    $phpcolor->table="bar_user";
    $phpcolor->field="uid, bid , integral, accesstime";
    $phpcolor->where="'".$userid['id']."', '".$_GET['bid']."', '2', '".$time."'";
    $phpcolor->write();
    //更新吧排名（会员加入）
    $phpcolor->upranking($int_hyjr,$_GET['bid']);
    jump("bar_manage.php?bid=".$bar['id']."&action=userapply");
  }
  //会员申请拒绝
  if($_GET['action']=="userno")
  {
    submitfilter($_GET['i']);
    $phpcolor->table="apply_user";
    $phpcolor->where="id = '".$_GET['i']."'";
    $phpcolor->delete();
    jump("bar_manage.php?bid=".$bar['id']."&action=userapply");
  }
  //会员等级名称
  if($_GET['action']=="userdj")
  {
    $rs=split("\|", $bar['usergroup']);
    $rs1=split("\|", $bar['usergroupf']);
    for($count=0;$count<9;$count++)
    {
      $usergroupl[$count]['dj']=$rs[$count];
      $usergroupl[$count]['djjf']=$rs1[$count];
	}
    for($count=0;$count<9;$count++)
    {
      $usergroupr[$count]['dj']=$rs[($count+9)];
      $usergroupr[$count]['djjf']=$rs1[($count+9)];
	}
    $tpl -> assign("usergroupl", $usergroupl);
    $tpl -> assign("usergroupr", $usergroupr);
  }
  //会员等级名称设置
  if($_GET['action']=="userdjsz")
  {
	$_POST['djjf'][1]="0";
	for ($count=1;$count<=count($_POST['dj']);$count++)
    {  
      $_POST['dj'][$count]=bn_replace($_POST['dj'][$count]);
	  if(strlen($_POST['dj'][$count])>8 || $_POST['dj'][$count]=="" || strlen($_POST['djjf'][$count])>8 || $_POST['djjf'][$count]=="" || !eregi("^[0-9]+$",$_POST['djjf'][$count]))
	    jump("error.php?id=1&last=bar_manage.php?bid=".$bar['id']."=and=action=userdj");
	  if ($count==1)
	  {
		$usergroup=$_POST['dj'][$count];
		$usergroupf=$_POST['djjf'][$count];
	  }
	  else
	  {
		$usergroup.="|".$_POST['dj'][$count];
		$usergroupf.="|".$_POST['djjf'][$count];
	  }
    }
    $phpcolor->field="usergroup='".$usergroup."',usergroupf='".$usergroupf."'";
    $phpcolor->upgrade();
    jump("bar_manage.php?bid=".$bar['id']."&action=userdj");
  }
  //分类申请
  if($_GET['action']=="sort")
  {
    $phpcolor->table="class";
    $phpcolor->field="id,classname";
    $phpcolor->where="base = '1' order by belong, queue";
    $sort=$phpcolor->assignment();
    $tpl -> assign("sort", $sort);
  }
  //贴吧图片设置
  if($_GET['action']=="pic")
  {
    submitfilter($_GET['fs']);
    if($_GET['fs']=="1")
      $picture=bn_replace($_POST['picture']);
    else
    {
      $oldname=$_FILES["file"]["name"];
      $fileextensionname=strtolower(eregi_replace(".+(\..+)","\\1",$oldname));
      $filename = $bar["id"].$fileextensionname;
      if(!preg_match( '/^image\//i ',$_FILES["file"]["type"]) || ($_FILES["file"]["size"]/1024)>2048)
        jump("error.php?id=16");
	  if(file_exists("upload/barpic/".$filename))
        unlink("upload/barpic/".$filename);
	  move_uploaded_file($_FILES["file"]["tmp_name"],"upload/barpic/".$filename);
	  $picture=$core_url."upload/barpic/".$filename;
	}
	$phpcolor->table="bar";
	$phpcolor->field="picture='".$picture."'";
	$phpcolor->where="id = '".$_GET['bid']."'";
    $phpcolor->upgrade();
	jump("bar_manage.php?bid=".$bar['id']."&action=basic");
  }
  //贴吧设置提交
  if($_GET['action']=="editpi")
  {
	submitfilter($_POST['apply']);
	submitfilter($_POST['rankfw']);
	submitfilter($_POST['rankzt']);
	submitfilter($_POST['rankhf']);
	submitfilter($_POST['verify']);
	$_POST['introduction']=autolink(encode($_POST['introduction']));
	$_POST['usernc']=encode($_POST['usernc']);
	if($core_barmgl=="3")
	{
      if($_POST['apply']!=$bar['apply'] || $_POST['rankfw']!=$bar['rankfw'] || $_POST['rankzt']!=$bar['rankzt'] || $_POST['rankhf']!=$bar['rankhf'])
	    $core_barmgl=="1";
      else
	    $core_barmgl=="0";
    }
    if($core_barmgl=="0")
	{
      $phpcolor->field="introduction='".$_POST['introduction']."',usernc='".$_POST['usernc']."',apply='".$_POST['apply']."',rankfw='".$_POST['rankfw']."',rankzt='".$_POST['rankzt']."',rankhf='".$_POST['rankhf']."',verify='".$_POST['verify']."',style='".$_POST['style']."'";
      $phpcolor->upgrade();
	}
	else
	{
	  $_POST['way']=bn_replace(encode($_POST['way']));
	  $phpcolor->table="barmaudit";
	  $phpcolor->field="bid, introduction, usernc, apply, rankfw, rankzt, rankhf, verify, way, style";
	  $phpcolor->where="'".$_GET['bid']."', '".$_POST['introduction']."', '".$_POST['usernc']."', '".$_POST['apply']."', '".$_POST['rankfw']."', '".$_POST['rankzt']."', '".$_POST['rankhf']."', '".$_POST['verify']."', '".$_POST['way']."','".$_POST['style']."'";
      $phpcolor->write();
	}
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="更新吧设置，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
    jump("bar_manage.php?bid=".$bar['id']."&action=basic");
  }
  //添加小吧主
  if($_GET['action']=="addsm")
  {
	$_POST['addsm']=iconv("UTF-8", "gbk", $_POST['addsm']);
	$_POST['addsm']=bn_replace($_POST['addsm']);
	$phpcolor->table="user";
    $phpcolor->where="username = '".$_POST['addsm']."'";
	if(!$rs=$phpcolor->exist(1))
      die("1");
    $phpcolor->table="bar_user";
	$phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$rs['id']."' and rank = 1";
    if($phpcolor->exist(2))
      die("2");
	$phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$rs['id']."'";
	if(!$phpcolor->exist(2))
      die("4");
	$phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$rs['id']."' and rank = 2";
	if($phpcolor->exist(2))
      die("5");
	if($count>=$core_barsmastercount)
	  die("3");
    $phpcolor->field="rank = 1";
	$phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$rs['id']."'";
    $phpcolor->upgrade();
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="增加".$_POST['addsm']."为小吧主，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
	die("6");
  }
  //添加友情贴吧
  if($_GET['action']=="addfb")
  {
    $_POST['addfb']=iconv("UTF-8", "gbk", $_POST['addfb']);
    $_POST['addfb']=bn_replace($_POST['addfb']);
    $phpcolor->where="barname = '".$_POST['addfb']."' and state = '1'";
    if(!$phpcolor->exist(2))
      die("4");
    if($_POST['addfb']==$bar['barname'])
	  die("1");
	if($bar['friendbar']!="" and eregi($bar['friendbar'],$_POST['addfb']))
	  die("2");
	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();
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="增加".$_POST['addfb']."为友情".$core_bartype."，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
	die("3");
  }
  //添加贴子分类
  if($_GET['action']=="addpc")
  {
    if($_POST['addpc']=="")
      die("0");
	$_POST['addpc']=iconv("UTF-8", "gbk", $_POST['addpc']);
	$_POST['addpc']=bn_replace($_POST['addpc']);
	$phpcolor->table="postclass";
    $phpcolor->field="name, belong, queue";
    $phpcolor->where="'".$_POST['addpc']."', '".$_GET['bid']."', '0'";
    $phpcolor->write();
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="增加".$_POST['addpc']."为精品区分类，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
    die("1");
  }
  //删除贴子分类
  if($_GET['action']=="pcdel")
  {
	submitfilter($_GET['id']);
    $phpcolor->table="postclass";
    $phpcolor->where="id='".$_GET['id']."'";
    $phpcolor->delete();
    jump("bar_manage.php?bid=".$bar['id']."&action=postclass");
  }
  //删除小吧主
  if($_GET['action']=="delsm")
  {
    submitfilter($_GET['id']);
    $phpcolor->table="bar_user";
    $phpcolor->field="rank=0";
    $phpcolor->where="bid = '".$_GET['bid']."' and uid = '".$_GET['id']."'";
    $phpcolor->upgrade();
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="删除小吧主".$_GET['n']."，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
    jump("bar_manage.php?bid=".$bar['id']."&action=basic");
  }
  //删除友情贴吧
  if($_GET['action']=="delfb")
  {
    $bar['friendbar']=replacemandf($bar['friendbar'],$_GET['n']);
    $phpcolor->field="friendbar='".$bar['friendbar']."'";
    $phpcolor->upgrade();
    $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="删除友情".$core_bartype."：".$_GET['n']."，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
    jump("bar_manage.php?bid=".$bar['id']."&action=basic");
  }
  //解除封锁
  if($_GET['action']=="unblock")
  {
	submitfilter($_GET['i']);
	$phpcolor->table="block";
    $phpcolor->where="id='".$_GET['i']."'";
	$block=$phpcolor->exist(1);
    $phpcolor->delete();
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$neirong="解除由".$block['operate']."封锁的".$block['username']."，吧名：".$bar['barname'];
    $phpcolor->logwrite($neirong,$_COOKIE["core_user_name"],$time,2);
	jump("bar_manage.php?bid=".$bar['id']."&action=block");
  }
  if($_GET['action']=="applys")
  {
    if($_POST['reason']=="" || $_POST['email']=="")
      jump("error.php?id=1&last=bar_manage.php?bid=".$_GET['bid']."&action=apply"); 
	$_POST['reason']=autolink($_POST['reason']);
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
	$phpcolor->table="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();
    jump("bar_manage.php?bid=".$bar['id']."&action=apply");
  }
  if($_GET['action']=="sorts")
  {
    if($_POST['reason']=="")
      jump("error.php?id=1&last=bar_manage.php?bid=".$_GET['bid']."&action=sort"); 
    $phpcolor->table="apply_sort";
    $phpcolor->field="username, barname, why, sort";
    $phpcolor->where="'".$_COOKIE['core_user_name']."', '".$bar['barname']."', '".$_POST['reason']."', '".$_POST['sorts']."'";
    $phpcolor->write();
    jump("bar_manage.php?bid=".$bar['id']."&action=sort");
  }
  if($_GET['action']=="postclass")
  {
    $phpcolor->table="postclass";
    $phpcolor->field="id, name";
    $phpcolor->where="belong = '".$_GET['bid']."'";
    $postclass=$phpcolor->assignment();
    $tpl -> assign("postclass", $postclass);
  }
  $tpl -> assign("bar", $bar);
  $tpl -> assign("core_style", $core_style);
  $tpl -> assign("action", $_GET['action']);
  $tpl -> assign("core_header_title", $bar['barname']."吧吧务管理 - ");
  $tpl -> display("$core_style/bar_manage.htm");
}
else
  jump("error.php?id=2");
?>