<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[10]!=1)
  jump("jump.php?id=25&jump=main.php");
if(isset($_GET['action']) and $_GET['action'] == "delnav")
{
  $phpcolor->table="bar_nav";
  $phpcolor->where="id = '".$_GET['nid']."'";
  $phpcolor->delete(); 
  jump("jump.php?id=24&jump=bar.php?action=edit=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and $_GET['action'] == "naveditdo")
{
  $phpcolor->table="bar_nav";
  for($i=1;$i<=count($_POST['id']);$i++)
  {
    if($_POST['name'][$i]!="" && $_POST['link'][$i]!="" && $_POST['queue'][$i]!="" && $_POST['colors'][$i]!="")
	{
      $phpcolor->field="name = '".$_POST['name'][$i]."',link = '".$_POST['link'][$i]."',queue = '".$_POST['queue'][$i]."',type = '".$_POST['type'][$i]."',colors = '".$_POST['colors'][$i]."'";
      $phpcolor->where="id='".$_POST['id'][$i]."'";
      $phpcolor->upgrade();
    }
  }
  jump("jump.php?id=24&jump=bar.php?action=edit=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and $_GET['action'] == "navadddo")
{
  if($_POST['name']=="" || $_POST['link']=="" || $_POST['queue']=="" || $_POST['colors']=="")
    jump("jump.php?id=2&jump=bar.php?action=edit=and=id".$_GET['id']);
  $phpcolor->table="bar_nav";
  $phpcolor->field="name, link, queue, belong, type, colors";
  $phpcolor->where="'".$_POST['name']."', '".$_POST['link']."', '".$_POST['queue']."', '".$_GET['id']."', '".$_POST['type']."', '".$_POST['colors']."'";
  $phpcolor->write();
  jump("jump.php?id=24&jump=bar.php?action=edit=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) && isset($_GET['id']) && $_GET['action'] == "editdo")
{
  $_POST['barname']=bn_replace($_POST['barname']);
  $phpcolor->table="bar";
  $phpcolor->where="barname = '".$_POST['barname']."'";
  if($phpcolor->exist(2) && $_POST['barname']!=$_GET['bn'])
    jump("jump.php?id=4&jump=bar.php?action=edit=and=id=".$_GET['id']);
  if($_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();
    }
  }
  $phpcolor->field="*";
  if($_POST['friendbar']!="")
  {
    $friendbar = split("\|", $_POST['friendbar']);
    for($count=0;$count<count($friendbar);$count++)
    {
      $phpcolor->where="barname = '".$friendbar[$count]."'";
      if(!$phpcolor->exist(2))
        jump("jump.php?id=7&jump=bar.php?action=edit=and=id=".$_GET['id']);
      if($friendbar[$count]==$_POST['barname'])
        jump("jump.php?id=21&jump=bar.php?action=edit=and=id=".$_GET['id']);
	  for($i=$count+1;$i<count($friendbar);$i++)
	  {
	    if($friendbar[$count]==$friendbar[$i])
		  jump("jump.php?id=20&jump=bar.php?action=edit=and=id=".$_GET['id']);
	  }
	}
  }
  $_POST['introduction']=autolink($_POST['introduction']);
  $_POST['barts']=autolink($_POST['barts']);
  $_POST['picture']=bn_replace($_POST['picture']);
  $phpcolor->field="barname='".$_POST['barname']."', introduction='".$_POST['introduction']."', picture='".$_POST['picture']."', friendbar='".$_POST['friendbar']."', barclass='".$_POST['barclass']."', verify='".$_POST['verify']."', rankfw='".$_POST['rankfw']."', rankzt='".$_POST['rankzt']."', rankhf='".$_POST['rankhf']."', usernc='".$_POST['usernc']."', apply='".$_POST['apply']."', state='".$_POST['state']."', recommend='".$_POST['recommend']."', merge='".$_POST['merge']."', style='".$_POST['style']."',rankingq='".$_POST['rankingq']."',rankings='".$_POST['rankings']."',ranking='".$_POST['ranking']."',barts='".$_POST['barts']."',listcon='".$_POST['listcon']."'";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->table="bar";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $bar=$phpcolor->exist(1);
  //吧分类
  $phpcolor->table="class";
  $phpcolor->field="id,classname,base";
  $phpcolor->where="belong = 0 order by queue";
  $bigsort=$phpcolor->assignment();
  for ($count=0;$count<count($bigsort);$count++)
  {
    $phpcolor->table="class";
    $phpcolor->field="id,classname";
    $phpcolor->where="belong = ".$bigsort[$count]['id']." order by queue";
    $smallsort[$count]=$phpcolor->assignment();
  }
  $phpcolor->table="style";
  $phpcolor->field="*";
  $phpcolor->where="1=1";
  $style=$phpcolor->assignment();
  $phpcolor->table="bar_user";
  $phpcolor->field="uid,accesstime";
  $phpcolor->where="bid = '".$bar['id']."' and rank=2";
  $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);
    $master[$count]['username'] = $rsm['username'];
	$master[$count]['uid'] = $rs[$count]['uid'];
	$master[$count]['riqi'] = substr($rs[$count]['accesstime'],0,10);
  }
  $phpcolor->table="bar_user";
  $phpcolor->field="uid,accesstime";
  $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'];
	$smaster[$count]['riqi'] = substr($rs[$count]['accesstime'],0,10);
  }
  $phpcolor->table="bar_domain";
  $phpcolor->field="id,domain";
  $phpcolor->where="bid = '".$bar['id']."'";
  $bardomain=$phpcolor->assignment();
  $phpcolor->table="bar_nav";
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$bar['id']."' order by queue";
  $barnav=$phpcolor->assignment();
  $tpl -> assign("barnav", $barnav);
  $tpl -> assign("smaster", $smaster);
  $tpl -> assign("master", $master);
  $tpl -> assign("style", $style);
  $tpl -> assign("bigsort", $bigsort);
  $tpl -> assign("smallsort", $smallsort);
  $tpl -> assign("bar", $bar);
  $tpl -> assign("bardomain", $bardomain);
  $tpl -> display("bar_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "domain")
{
  $phpcolor->table="bar_domain";
  $phpcolor->field="id";
  $phpcolor->where="domain = '".$_POST['domain']."'";
  if($domain=$phpcolor->exist(1))
    jump("jump.php?id=4&jump=bar.php?action=edit=and=id=".$_GET['id']);
  $phpcolor->field="bid , domain";
  $phpcolor->where="'".$_GET['id']."', '".$_POST['domain']."'";
  $phpcolor->write();
  jump("jump.php?id=24&jump=bar.php?action=edit=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['did']) and $_GET['action'] == "deldomain")
{
  $phpcolor->table="bar_domain";
  $phpcolor->field="rank=0";
  $phpcolor->where="id = '".$_GET['did']."'";
  $phpcolor->delete();
  jump("jump.php?id=24&jump=bar.php?action=edit=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['uid']) and $_GET['action'] == "cmaster")
{
  $phpcolor->table="bar_user";
  $phpcolor->field="rank=0";
  $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['id']."'";
  $phpcolor->upgrade();
  if($_GET['jump']==1)
    jump("jump.php?id=24&jump=user.php?action=edit=and=id=".$_GET['uid']);
  jump("jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "del")
{
  if(!isset($_GET['qr']))
  {
    echo "<html><head><link rel=stylesheet type='text/css' href='../admin/images/css.css'></head><body>";
    echo "<table width='100%' height='500' border='0'><tr>";
    echo "<td align='center' valign='middle'>";
	echo "<table class='table' cellspacing='1' cellpadding='2' width='50%' height='120' align='center' border='0'>";
    echo "<tr><td height='30' class='bg_tr'>系统提示</td></tr>";
    echo "<tr><td class='td_bg' align='center'>您正在尝试删除本吧，删除后无法恢复，请确认您的操作<br><a href='bar.php?action=del&id=".$_GET['id']."&qr=1'>确认删除</a>&nbsp;&nbsp;&nbsp;<a href='bar.php?action=edit&id=".$_GET['id']."'>放弃删除</a><br></td></tr>";
    echo "</table></td></tr></table></body></html>";
	exit;
  }
  $phpcolor->table="bar";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="bar_user";
  $phpcolor->where="bid = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="barmaudit";
  $phpcolor->where="bid = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="bar_domain";
  $phpcolor->where="bid = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="bar_nav";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="topic";
  $phpcolor->field="id";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $rs = $phpcolor->assignment();
  $phpcolor->delete();
  $phpcolor->table="repost";
  for($count=0;$count<count($rs);$count++)
  {
    $phpcolor->where="belong = '".$rs[$count]['id']."'";
    $phpcolor->delete();
  }
 jump("jump.php?id=24&jump=bar.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "user")
{
  if(isset($_POST['username']))
    $_GET['username']=$_POST['username'];
  $phpcolor->table="bar";
  $phpcolor->field="id,barname";
  $phpcolor->where="id = '".$_GET['id']."'";
  $bar=$phpcolor->exist(1);
  if(isset($_GET['username']))
  {
    $phpcolor->table="user";
    $phpcolor->field="id";
	$phpcolor->where="username = '".$_GET['username']."'";
	$rs=$phpcolor->exist(1);
	$phpcolor->table="bar_user";
    $phpcolor->field="*";
    $phpcolor->where="bid = '".$bar['id']."' and uid = '".$rs['id']."'";
  }
  else
  {
    $phpcolor->table="bar_user";
    $phpcolor->field="*";
    $phpcolor->where="bid = '".$bar['id']."'";
    $count = $phpcolor->exist(2);
	$page=pages($count,20,$_GET['page'],'bar','&action=user&id=',$_GET['id'],'0');
    $phpcolor->where="bid = '".$bar['id']."' limit ".($_GET['page']*50).",50";
  }
  $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);
  $tpl -> assign("bar", $bar);
  $tpl -> assign("page", $page);
  $tpl -> display("bar_user.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['uid']) and $_GET['action'] == "deluser")
{
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['id']."'";
  $phpcolor->delete();
  jump("jump.php?id=24&jump=bar.php?action=user=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_POST['username']) and $_GET['action'] == "adduser")
{
  $phpcolor->table="user";
  $phpcolor->field="id";
  $phpcolor->where="username = '".$_POST['username']."'";
  $userid=$phpcolor->exist(1);
  if(!$userid)
    jump("jump.php?id=1&jump=bar.php?action=user=and=id=".$_GET['id']);
  $phpcolor->table="bar_user";
  $phpcolor->field="*";
  $phpcolor->where="bid = '".$_GET['id']."' and uid='".$userid['id']."'";
  if($phpcolor->exist(2))
    jump("jump.php?id=4&jump=bar.php?action=user=and=id=".$_GET['id']);
  $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['id']."', '0', '".$time."'";
  $phpcolor->write();
  jump("jump.php?id=24&jump=bar.php?action=user=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['uid']) and $_GET['action'] == "rmaster")
{
  $phpcolor->table="bar_user";
  $phpcolor->field="rank=2";
  $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['id']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=bar.php?action=user=and=id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and isset($_GET['uid']) and $_GET['action'] == "rsmaster")
{
  $phpcolor->table="bar_user";
  $phpcolor->field="rank=1";
  $phpcolor->where="uid = '".$_GET['uid']."' and bid = '".$_GET['id']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=bar.php?action=user=and=id=".$_GET['id']);
}
else
{
  $phpcolor->table="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,barclass,ranking,rankings,rankingq,state";
    $phpcolor->where="state != '2' and barname like '%".$_GET['barname']."%' order by id desc limit ".($_GET['page']*35).",35";
    $page=pages($count,35,$_GET['page'],'bar','&barname=',$_GET['barname'],'0');
  }
  elseif(isset($_GET['type']) && $_GET['type']=="limit")
  {
    $phpcolor->where="state = '3'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barclass,ranking,rankings,rankingq,state";
    $phpcolor->where="state = '3' order by id desc limit ".($_GET['page']*35).",35";
    $page=pages($count,35,$_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,barclass,ranking,rankings,rankingq,state";
    $phpcolor->where="state = '0' order by id desc limit ".($_GET['page']*35).",35";
    $page=pages($count,35,$_GET['page'],'bar','&type=','off','0');
  }
  else
  {
    $phpcolor->where="1=1";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,barname,barclass,ranking,rankings,rankingq,state";
    $phpcolor->where="1=1 order by id desc limit ".($_GET['page']*35).",35";
    $page=pages($count,35,$_GET['page'],'bar','','','0');
  }
  $bar=$phpcolor->assignment();
  $barrs=count($bar);
  $bars=5-$barrs%5;
  for($count=0;$count<$barrs;$count++)
  {
    if($bar[$count]["rankings"]==0)
    {
      $bar[$count]["upordown"]="1";
	  $bar[$count]["baifen"]=ceil($bar[$count]["ranking"]*100);
    }
    elseif($bar[$count]["ranking"]>$bar[$count]["rankings"])
    {
      $bar[$count]["upordown"]="1";
	  $bar[$count]["baifen"]=ceil(($bar[$count]["ranking"]-$bar[$count]["rankings"])/$bar[$count]["rankings"]*100);
    }
    else
    {
      $bar[$count]["upordown"]="2";
      $bar[$count]["baifen"]=ceil(($bar[$count]["rankings"]-$bar[$count]["ranking"])/$bar[$count]["rankings"]*100);
    }
	if($bar[$count]["state"]=="0")
	  $bar[$count]["state"]="<span style='color:#FF0000'>关闭</span>";
	elseif($bar[$count]["state"]=="2")
	  $bar[$count]["state"]="<span style='color:#FF0000'>未审核</span>";
	elseif($bar[$count]["state"]=="3")
	  $bar[$count]["state"]="<span style='color:#FFCC00'>限制</span>";
	else
	  $bar[$count]["state"]="<span style='color:#00FF00'>开放</span>";
	if($bar[$count]["barclass"]!="0")
	{
	  $phpcolor->table="class";
      $phpcolor->field="classname";
      $phpcolor->where="id = '".$bar[$count]['barclass']."'";
      $sort[$count] = $phpcolor->exist(1);
	}
	$phpcolor->table="topic";
	$phpcolor->field="*";
    $phpcolor->where="belong='".$bar[$count]['id']."' and state = '1'";
    $bar[$count]["topic"] = $phpcolor->exist(2);
	$phpcolor->table="bar_user";
    $phpcolor->where="bid = '".$bar[$count]['id']."'";
    $bar[$count]["user"]=$phpcolor->exist(2);
  }
  $tpl -> assign("sort", $sort);
  $tpl -> assign("bar", $bar);
  $tpl -> assign("bars", $bars);
  $tpl -> assign("page", $page);
  $tpl -> assign("type", $_GET['type']);
  $tpl -> display("bar.htm");
}
?> 