<?php
define('LOGIN', true);
require('global.php');
$second = 2;
if ($_GET['action'] == 'logout'){
	checkAdminReferer();
	@unlink(C_P."cache/purview/admin/".$admin_info['uid'].".php");
	adminOut();
	qbase::showmsg('成功退出登陆', 'default', 'index.php');
}

if ($_POST['action'] == 'checkpass')
{
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$code = strtolower(trim($_POST['code']));
	$admin_verify_code = strtolower(trim($_POST['admin_verify_code']));
	$url = "login.php?action=login";
	if (!$username || !$password || !$code || !$admin_verify_code) qbase::showmsg("请填写完整登陆信息", "default", $url );
	if (strtolower($code) != strtolower($_SESSION['code'])) qbase::showmsg("验证码错误", "default",  $url);
	if ($admin_verify_code != $_SET['admin_verify_code'])  qbase::showmsg("安全码错误", "default",  $url);
	$user = qdb::QueryFirst("SELECT `uid`, `username` ,`groupid` FROM `".TABLEPRE."admin` WHERE `username` = '" . $username . "' AND `password` = '" . md5($password) . "'  LIMIT 1");

	if ($user['uid'] < 1) qbase::showmsg("输入了错误信息", 'default',  $url);

	$timestamp =$_SERVER['REQUEST_TIME'];
	$ip = qbase::get_ip();
	$checkString = qbase::random(6) ;
	$session = md5($checkString . $ip . $user['groupid']);
	
	qdb::Query("update ".TABLEPRE."admin set lastloginip='$ip',lastlogintime='$timestamp',active_time = '{$_SERVER['REQUEST_TIME']}',checkString='$checkString',`session`='$session' where username='$username' limit 1");
	$admin_info =array(
		'u' => $user['uid'],
		's'=> $session
	);
	$admin_info_string = qbase::authcode(qbase::array2string($admin_info), "ENCODE");
	qbase::phpsetcookie("admin_auth", $admin_info_string);
	cachePurview();
	qbase::showmsg('登陆成功,欢迎你回来', 'default', 'index.php');
}

if ($_GET[action] == 'login' or $_GET['action'] == ''){
	if ($admin_info['uid']){
		qbase::showmsg("您已经登陆", "default", "index.php");
	}
}

include qbase::template($path . 'login');

?>
