<?php
require('./load.php');

$pagetitle = SYS_NAME.'v'.SYS_VERSION;
$fileurl = 'login.php';
$tempfile = 'login.html';
$table = $DB->table('admin');

if ($_POST['act'] == 'login') {
	$username = trim($_POST['username']);
	$userpass = trim($_POST['userpass']);
	
	if (empty($username)) {
		msgbox('请输入登陆用户名！', $fileurl);
	}
	if (empty($userpass)) {
		msgbox('请输入登陆密码！', $fileurl);
	}
	
	$newpass = md5($userpass);
	
	$admin = $DB->fetch_one("SELECT admin_id, admin_pass, login_time, login_ip, login_count FROM $table WHERE admin_name='$username'");
	if ($admin['admin_id'] && $newpass == $admin['admin_pass']) {
		$login_count = $admin['login_count'] + 1;
		$data = array(
			'login_time' => time(),
			'login_ip' => get_client_ip(),
			'login_count' => $login_count,
		);
		$where = array('admin_id' => $admin['admin_id']);
		$DB->update($table, $data, $where);
		$admin_auth = authcode("$admin[admin_id]@$newpass@$login_count");
		setcookie('admin_auth', $admin_auth);
		
		redirect('admin.php');
	} else {
		msgbox('用户名或密码错误，请重试！', $fileurl);
	}
}

if ($_GET['act'] == 'logout') {
	setcookie('admin_auth', '');
	redirect('../');
}

smarty_output($tempfile);
?>