<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[2]!=1)
  jump("jump.php?id=25&jump=main.php");
if(isset($_GET['action']) and $_GET['action'] == "del" and isset($_GET['id']))
{
  $phpcolor->table="user";
  $phpcolor->field="rank='0'";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->upgrade();
  $phpcolor->table="admin";
  $phpcolor->where="uid = '".$_GET['id']."'";
  $phpcolor->delete();
  jump("jump.php?id=24&jump=admin.php");
}
if(isset($_GET['action']) and $_GET['action'] == "adddo")
{
  $phpcolor->table="user";
  $phpcolor->field="id";
  $phpcolor->where="username = '".$_POST['username']."' and rank = 0";
  if(!$user=$phpcolor->exist(1))
    jump("jump.php?id=1&jump=admin.php?action=add");
  if($_POST['rank']==2)
  {
    $purview=$_POST['purview'][0];
    for($i=1;$i<count($_POST['purview']);$i++)
    {
      $purview.="|".$_POST['purview'][$i];
    }
    $phpcolor->table="admin";
    $phpcolor->field="uid, purview";
    $phpcolor->where="'".$user['id']."', '".$purview."'";
    $phpcolor->write();
  }
  $phpcolor->table="user";
  $phpcolor->where="username = '".$_POST['username']."'";
  $phpcolor->field="rank='".$_POST['rank']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=admin.php");
}
if(isset($_GET['action']) and $_GET['action'] == "editdo" and isset($_GET['un']))
{
  $phpcolor->table="user";
  $phpcolor->field="rank='".$_POST['rank']."'";
  $phpcolor->where="username = '".$_GET['un']."'";
  $phpcolor->upgrade();
  if($_POST['rank']==2)
  {
    $phpcolor->field="id,rank";
    $phpcolor->where="username = '".$_GET['un']."'";
    $user=$phpcolor->exist(1);
	$purview=$_POST['purview'][0];
	for($i=1;$i<count($_POST['purview']);$i++)
	{
	  $purview.="|".$_POST['purview'][$i];
	}
    if($user['rank']==1)
	{
	  $phpcolor->table="admin";
	  $phpcolor->field="uid, purview";
      $phpcolor->where="'".$user['id']."', '".$purview."'";
	  $phpcolor->write();
	}
	else
	{
	  $phpcolor->table="admin";
	  $phpcolor->field="purview='".$purview."'";
      $phpcolor->where="uid = '".$user['id']."'";
      $phpcolor->upgrade();
	}
  }
  jump("jump.php?id=24&jump=admin.php");
}
if(isset($_GET['action']) and $_GET['action'] == "add")
  $tpl -> display("admin_add.htm");
elseif(isset($_GET['action']) and $_GET['action'] == "edit" and isset($_GET['un']))
{
  $phpcolor->table="user";
  $phpcolor->field="id, rank";
  $phpcolor->where="username = '".$_GET['un']."'";
  $user=$phpcolor->exist(1);
  $phpcolor->table="admin";
  $phpcolor->field="purview";
  $phpcolor->where="uid = '".$user['id']."'";
  $rs=$phpcolor->exist(1);
  $rs1 = split("\|", $rs['purview']);
  for($i=0;$i<count($rs1);$i++)
  {
    $purview[$rs1[$i]]="1";
  }
  $tpl -> assign("username", $_GET['un']);
  $tpl -> assign("user", $user);
  $tpl -> assign("purview", $purview);
  $tpl -> display("admin_edit.htm");
}
else
{
  $phpcolor->table="user";
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if(isset($_GET['username']))
  {
    $phpcolor->where="rank != '0' and username like '%".$_GET['username']."%'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,username,email,logintime,loginip,rank";
    $phpcolor->where="rank != '0' and username like '%".$_GET['username']."%' order by registered desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'user','&username=',$_GET['username'],'0');
  }
  else
  {
    $phpcolor->where="rank != '0'";
    $count = $phpcolor->exist(2);
    $phpcolor->field="id,username,email,logintime,loginip,rank";
    $phpcolor->where="rank != '0' order by registered desc limit ".($_GET['page']*20).",20";
    $page=pages($count,20,$_GET['page'],'user','','','0');
  }
  $user=$phpcolor->assignment();
  for($count=0;$count<count($user);$count++)
  {
    if($user[$count]['rank']==1)
      $user[$count]['rank']="前台管理员";
    else
      $user[$count]['rank']="后台管理员";
  }
  $tpl -> assign("user", $user);
  $tpl -> assign("page", $page);
  $tpl -> display("admin.htm");
}
?>