<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[10]!=1)
{
  Header("Location: jump.php?id=25&jump=main.php");
  exit();
}
if(isset($_GET['action']) && isset($_GET['id']) && $_GET['action'] == "editdo")
{
  $phpcolor->table=$core_mysql_prefix."user";
  $phpcolor->field="*";
  if($_POST['barmaster']!="")
  {
    $master = split("\|", $_POST['barmaster']);
    for($count=0;$count<count($master);$count++)
    {
      $phpcolor->where="username = '".$master[$count]."'";
      if(!$phpcolor->exist(2))
      {
        Header("Location: jump.php?id=1&jump=bar.php?action=edit=and=id".$_GET['id']);
        exit();
      }
      else
      {
        for($i=$count+1;$i<count($master);$i++)
        {
          if($master[$count]==$master[$i])
          {
            Header("Location: jump.php?id=6&jump=bar.php?action=edit=and=id".$_GET['id']);
            exit();
		  }
		}
	  }
    }
  }
  if($_POST['sbarmaster']!="")
  {
    $smaster = split("\|", $_POST['sbarmaster']);
    for($count=0;$count<count($smaster);$count++)
    {
      $phpcolor->where="username = '".$smaster[$count]."'";
      if(!$phpcolor->exist(2))
      {
        Header("Location: jump.php?id=1&jump=bar.php?action=edit=and=id".$_GET['id']);
        exit();
      }
      else
      {
        for($i=$count+1;$i<count($smaster);$i++)
        {
          if($smaster[$count]==$smaster[$i])
          {
	        Header("Location: jump.php?id=6&jump=bar.php?action=edit=and=id".$_GET['id']);
            exit();
          }
	    }
	  }
    }
  }
  $_POST['barname']=bn_replace($_POST['barname']);
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->where="barname = '".$_POST['barname']."'";
  if($phpcolor->exist(2) && $_POST['barname']!=$_GET['bn'])
  {
    Header("Location: jump.php?id=4&jump=bar.php?action=edit=and=id".$_GET['id']);
    exit();
  }
  elseif($_POST['barname']!=$_GET['bn'])
  {
    $phpcolor->where="friendbar like '%".$_GET['bn']."%'";
    $friendbar=$phpcolor->assignment();
    for($i=0;$i<count($friendbar);$i++)
    {
      $friendbar[$i]['friendbar']=replacemandf($friendbar[$i]['friendbar'],$_GET['bn']);
      $phpcolor->field="friendbar='".$friendbar[$i]['friendbar']."'";
      $phpcolor->where="id = '".$friendbar[$i]['id']."'";
      $phpcolor->upgrade();
    }
  }
  if($_POST['friendbar']!="")
  {
    $friendbar = split("\|", $_POST['friendbar']);
    for($count=0;$count<count($friendbar);$count++)
    {
      $phpcolor->where="barname = '".$friendbar[$count]."'";
      if(!$phpcolor->exist(2))
      {
        Header("Location: jump.php?id=7&jump=bar.php?action=edit=and=id".$_GET['id']);
        exit();
      }
      elseif($friendbar[$count]==$_POST['barname'])
      {
        Header("Location: jump.php?id=21&jump=bar.php?action=edit=and=id".$_GET['id']);
        exit();
	  }
	  else
	  {
	    for($i=$count+1;$i<count($friendbar);$i++)
		{
	      if($friendbar[$count]==$friendbar[$i])
		  {
		    Header("Location: jump.php?id=20&jump=bar.php?action=edit=and=id".$_GET['id']);
            exit();
	      }
	    }
	  }
    }
  }
  $_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['merge']=bn_replace($_POST['merge']);
  $phpcolor->field="barname='".$_POST['barname']."',keyword='".$_POST['keyword']."', description='".$_POST['description']."', introduction='".$_POST['introduction']."', picture='".$_POST['picture']."', barmaster='".$_POST['barmaster']."', sbarmaster='".$_POST['sbarmaster']."', friendbar='".$_POST['friendbar']."', barclass='".$_POST['barclass']."', visitor='".$_POST['visitor']."', verify='".$_POST['verify']."', baruserp='".$_POST['baruserp']."', apply='".$_POST['apply']."', state='".$_POST['state']."', recommend='".$_POST['recommend']."', merge='".$_POST['merge']."'";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->upgrade();
  Header("Location: jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $bar=$phpcolor->exist(1);
  $phpcolor->table=$core_mysql_prefix."class";
  $phpcolor->field="id,classname";
  $phpcolor->where="base = 1  order by belong";
  $sort=$phpcolor->assignment();
  $tpl -> assign("sort", $sort);
  $tpl -> assign("bar", $bar);
  $tpl -> display("bar_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['bn']) and $_GET['action'] == "del")
{
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->where="barname = '".$_GET['bn']."'";
  $phpcolor->delete();
  $phpcolor->table=$core_mysql_prefix."topic";
  $phpcolor->field="id";
  $phpcolor->where="belong = '".$_GET['bn']."'";
  $phpcolor->delete();
  $rs = $phpcolor->assignment();
  $phpcolor->table=$core_mysql_prefix."repost";
  for($count=0;$count<count($rs);$count++)
  {
    $phpcolor->where="belong = '".$rs[$count]['id']."'";
    $phpcolor->delete();
  }
  Header("Location: jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "user")
{
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="id,barname";
  $phpcolor->where="id = '".$_GET['id']."'";
  $bar=$phpcolor->exist(1);
  $phpcolor->table=$core_mysql_prefix."bar_user";
  $phpcolor->field="*";
  $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);
  $tpl -> assign("bar", $bar);
  $tpl -> display("bar_user.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['uid']) and $_GET['action'] == "deluser")
{
  $phpcolor->table=$core_mysql_prefix."bar_user";
  $phpcolor->field="*";
  $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['id']."'";
  $phpcolor->delete();
  Header("Location: jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_POST['username']) and $_GET['action'] == "adduser")
{
  $phpcolor->table=$core_mysql_prefix."user";
  $phpcolor->field="id";
  $phpcolor->where="username = '".$_POST['username']."'";
  $userid=$phpcolor->exist(1);
  if(!$userid)
    Header("Location: jump.php?id=1&jump=bar.php?action=user=and=id=".$_GET['id']);
  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="'".$userid['id']."', '".$_GET['id']."', '0', '".$time."'";
    $phpcolor->write();
    Header("Location: jump.php?id=24&jump=bar.php");
  }
}
else
{
  $phpcolor->table=$core_mysql_prefix."bar";
  $phpcolor->field="*";
  if(!isset($_GET['page']))
    $_GET['page']=0;
  if(isset($_GET['barname']))
  {
    $phpcolor->where="state != '2' and barname like '%".$_GET['barname']."%'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barmaster,barclass,state";
    $phpcolor->where="state != '2' and barname like '%".$_GET['barname']."%' order by id desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'bar','&barname=',$_GET['barname'],'0');
  }
  elseif(isset($_GET['type']) && $_GET['type']=="limit")
  {
    $phpcolor->where="baruserp != '0'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barmaster,barclass,state";
    $phpcolor->where="baruserp != '0' order by id desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'bar','&type=','limit','0');
  }
  elseif(isset($_GET['type']) && $_GET['type']=="off")
  {
    $phpcolor->where="state = '0'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barmaster,barclass,state";
    $phpcolor->where="state = '0' order by id desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'bar','&type=','off','0');
  }
  else
  {
    $phpcolor->where="state != '2'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barmaster,barclass,state";
    $phpcolor->where="state != '2' order by id desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'bar','','','0');
  }
  $bar=$phpcolor->assignment();
  $phpcolor->table=$core_mysql_prefix."class";
  $phpcolor->field="classname";
  for($count=0;$count<count($bar);$count++)
  {
    $phpcolor->where="id = '".$bar[$count]['barclass']."'";
    $sort[$count] = $phpcolor->exist(1);
    $master[$count] = split("\|", $bar[$count]['barmaster']);
  }
  $tpl -> assign("bar", $bar);
  $tpl -> assign("sort", $sort);
  $tpl -> assign("master", $master);
  $tpl -> assign("page", $page);
  $tpl -> assign("type", $_GET['type']);
  $tpl -> display("bar.htm");
}
?>