<?php

/**
 * 后台 登录模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: login.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://www.t-mac.org；
 */
class login extends Model
{

    private $db;
    private $users_table;
    private $userstype_table;
    private $userslog_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->users_table = DB_PREFIX . 'admin';
        $this->userstype_table = DB_PREFIX . 'admintype';
        $this->userslog_table = DB_PREFIX . 'adminlog';
    }

    /**
     * 登录验证
     * @param <type> $admin_name
     * @param <type> $admin_psw
     * @param <type> $yzm
     * @param <type> $remember 
     */
    public function check($admin_name, $admin_psw, $yzm, $remember = 0)
    {
        //取config配置文件
        $cfg_mid = Tmac::config('configcache.config.cfg_mid', APP_WWW_NAME, '.inc.php');
        if (trim(md5($yzm)) != trim($_SESSION['valid'])) {
            $this->redirect("效验码输入错误!", PHP_SELF . "?m=login");
            exit();
        }
        if (trim($admin_name) == "") {
            $this->redirect("请输入用户名!");
            exit();
        }
        if (trim($admin_psw) == "") {
            $this->redirect("请输入密码!");
            exit();
        }
        $sql = "SELECT a.purviews, b.* FROM $this->userstype_table a LEFT JOIN $this->users_table b ON a.rank = b.rank WHERE b.username = '{$admin_name}' LIMIT 0, 1";
        $info = $this->db->getRow($sql);
        if ($info) {
            if (md5($admin_psw . '^' . $cfg_mid) == $info['password']) {
                if (!empty($_SESSION['admin'])) {
                    unset($_SESSION['admin']);
                }
                if (!empty($_SESSION['admin_uid'])) {
                    unset($_SESSION['admin_uid']);
                }
                if (!empty($_SESSION['admin_purviews'])) {
                    unset($_SESSION['admin_purviews']);
                }
                //注册session
                $_SESSION['admin'] = $info['username'];
                $_SESSION['admin_uid'] = $info['id'];
                $_SESSION['admin_purviews'] = $info['purviews'];

                $uid = $info['id'];
                $time = time();
                $logip = $_SERVER["REMOTE_ADDR"];
                //插adminlog 登录日志
                $log_info = array(
                    'rank' => $info['rank'],
                    'uid' => $uid,
                    'username' => $info['username'],
                    'logip' => $logip,
                    'logtime' => $time
                );
                $rs = $this->db->autoExecute($this->userslog_table, $log_info, 'INSERT');
                //更新用户上次登录时间，IP，登录次数
                $sql = "UPDATE $this->users_table SET login_ip='$logip', login_time='$time', logincount=logincount+1 WHERE uid = $uid";
                $rs = $this->db->execute($sql);

                if ($remember == 1) {//选中了 记住密码
                    HttpResponse::setcookie("cookie_remember", "yes", time() + 3600 * 24 * 30);
                    HttpResponse::setcookie("cookie_username", $admin_name, time() + 3600 * 24 * 30);
                    HttpResponse::setcookie("cookie_password", $admin_psw, time() + 3600 * 24 * 30);
                } else {
                    HttpResponse::setcookie("cookie_remember", '');
                    HttpResponse::setcookie("cookie_username", '');
                    HttpResponse::setcookie("cookie_password", '');
                }
                $this->redirect("成功登录，正在转向管理管理主页！", PHP_SELF);
                unset($_SESSION['valid']);
                exit();
            } else {
                $this->redirect("Access Denied! 密码错误", PHP_SELF . "?m=login");
                exit();
            }
        } else {
            $this->redirect("Access Denied! 没有此用户", PHP_SELF . "?m=login");
            exit();
        }
    }

    public function out()
    {
        if (phpversion() < '4.3.0') {
            session_unregister('admin');
            session_unregister('admin_uid');
            session_unregister('admin_purviews');
            session_unregister('valid');
        } else {
            unset($_SESSION['admin']);
            unset($_SESSION['admin_uid']);
            unset($_SESSION['admin_purviews']);
            unset($_SESSION['valid']);
        }
        $this->redirect("退出/注销成功!", PHP_SELF);
    }

}