<?php
if(!defined('IN_TM')) 
{
	exit('Access Denied');
}
if($_SESSION["admin"]!="Silence")
{
	include template('login.htm');
    exit;
}
//¹ÜÀíÔ±²Ù×÷
$a = getgpc('do');
if($a == 'list')
{
	$page = getgpc('page');
	if(isset($page)){ $page = intval($page); }else{ $page = 1; }
	$page_size = 15; 
	$sql = "SELECT * FROM `{$tablepre}admin`";
	$row = $db->num_rows($db->query($sql));
	$mpurl =$PHP_SELF.'?action=admin&do=list&';
	$dispage =  page($row,$page_size,$page,$mpurl);
	$page_get_start = page_get_start($page,$page_size,$row);
	$sql = "SELECT * FROM {$tablepre}admin LIMIT $page_get_start, $page_size";
	$userlist = $db->fetch_all($sql);
	$a = 'Y-m-d H:i';
	$tplid = 1;
	include template('admin.htm');
	$db->close();
	exit;
}
elseif ($a == 'add')
{
	if (empty($_POST['Submit']))
	{
		$id = getgpc('uid');
		$userlist = $db->fetch_all("SELECT * FROM {$tablepre}admin WHERE id = '{$id}'");
		$tplid = 2;
		include template('admin.htm');
		exit;
	}
	else
	{
		$username = getgpc('adminuser');
		$password = getgpc('adminpwd');
		$pwd = getgpc('adminpwd2');
		if(empty($username))
		{
			$messageid = 1;
			include template('message.htm');
			exit;
		}
		if(empty($pwd) or empty($password) )
		{
			$messageid = 2;
			include template('message.htm');
			exit;
		}
		$password = md5($password);
		$pwd = md5($pwd);
		if($password != $pwd)
		{
			$messageid = 7;
			include template('message.htm');
			exit;
		}
		
		$row_num = $db->num_rows($db->query("SELECT * FROM {$tablepre}admin where username='{$username}'"));
		if($row_num > 0)
		{
 			$messageid = 20;
			include template('message.htm');
 			return;
 		}
 		else
 		{
			$db->query("INSERT INTO {$tablepre}admin (username,password,oltime,lastip) VALUES ('$username','$password','$timestamp','$onlineip')");
			$messageid = 19;
			include template('message.htm');
			exit;
		}
	}
}
elseif ($a == 'del')
{
	$uids = timplode(getgpc('delete'));
	$rs = $db->query("DELETE FROM {$tablepre}admin WHERE id IN ($uids) ");
	if (!$rs) 
	{
		$messageid = 22;
		include template('message.htm');
		exit;
	}
	$messageid = 21;
	include template('message.htm');
	$db->close();
	exit;
}
elseif ($a == 'edit')
{
	if (empty($_POST['Submit']))
	{
		$id = getgpc('uid');
		$sql = "SELECT * FROM {$tablepre}admin WHERE id = '{$id}'";
		$userlist = $db->fetch_all($sql);
		$tplid = 3;
		include template('admin.htm');
		exit;
	}
	else
	{
		$id = getgpc('id');
		$username = getgpc('username');
		$pwd = getgpc('adminpwd');
		$pwd2 = getgpc('adminpwd2');
		if(empty($pwd) or empty($pwd2))
		{
			$messageid = 2;
			include template('message.htm');
			exit;
		}
		$pwd = md5($pwd);
		$pwd2 = md5($pwd2);
		if($pwd2 != $pwd)
		{
			$messageid = 7;
			include template('message.htm');
			exit;
		}
		$sql = "UPDATE {$tablepre}admin SET username = '{$username}',password = '{$pwd}' WHERE id ={$id};";
		$db->query($sql);
		$messageid = 18;
		include template('message.htm');
		exit;
    }
}
?>