<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: login.php 525 2009-12-31 20:09:03Z genghonghao $
 */

define('IN_GEN', true);
define('SERVERAPP_START', true);
include './includes/common.inc.php';

// 登录处理
if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
    $username = trim($_REQUEST['username']);
    $password = md5(trim($_REQUEST['password']));
    $cur_time = time();
    $userinfo = $db->result("SELECT admin_id, username FROM " . $db->get_table(TB_ADMIN) . " WHERE username='{$username}' AND password='{$password}'");
    if (isset($userinfo) && is_array($userinfo) && count($userinfo) == 1) {
        $db->query("UPDATE " . $db->get_table(TB_ADMIN) . " SET last_login_ip='$client_ip', last_login_time='$cur_time' WHERE admin_id='{$userinfo[0]['admin_id']}'");
        $_SESSION['user']['islogin'] = true;
        $_SESSION['user']['userid'] = $userinfo[0]['admin_id'];
        $_SESSION['user']['username'] = $userinfo[0]['username'];
        include GEN_ROOT . 'includes/libs/phprpc/phprpc_client.php';
        include GEN_ROOT . 'includes/correspond.php';
        page_redirect('Success', $_LANG['user_login_success'], $base_url . '/admin');
    }
    page_redirect('Failure', $_LANG['user_login_failure'], $base_url . '/login.php');
}
// 如果用户已登录则跳转至后台
if (true === verify_user()) {
    header("Location: $base_url/admin");
}
$tpl->display('login.html');
?>
