<?php

/**
 *--------------------------------------
 * 登录
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-3
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class LoginCtrlr extends Ctrlr
{
	public function __construct()
	{
		parent::__construct();

		/* 检查入口 */
		session_start();
		if(!isset($_SESSION['admin_enter']) or 1 != $_SESSION['admin_enter'])
		{
			redirect(Url::U('home@index/index'));
			exit();
		}
	}

	public function index()
	{
		if(ASession::get('admin_id'))
		{
			redirect(Url::U('index/index'));
		}

		$_LANG = list_dir(LANG_PATH);
		$this->assign('_LANG', $_LANG);
		$hal = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5));
		$this->assign('hal', $hal);

		$this->display('admin/login');
	}

	public function login_do()
	{
		check_interaction('login');

		if(!AFilter::is_userid(ARequest::get('userid')))
		{
			$this->error(L('VERIFY_FAILED'), AServer::get_preUrl());
		}

		$where['__MEMBER__.m_userid'] = array('EQ', ARequest::get('userid'));
		$where['__MEMBER__.m_password'] = array('EQ', md5(ARequest::get('userid').md5(ARequest::get('password'))));

		$_MI = M('Member')->join('__ADMIN__ AS a ON a.member_id = __MEMBER__.member_id')
			->join('__ADMIN_ROLE__ AS ar ON ar.admin_role_id = a.admin_role_id')
			->where($where)->find();
		if(empty($_MI['admin_role_id']))
		{
			/* 写入管理员日志信息 */
			M('AdminLog')->add_log(ARequest::get('userid'), L('LOGIN'), 0);
			$this->error(L('VERIFY_FAILED'), Url::U('login/index'));
		}

		/* 更新登录信息 */
		$data['admin_id'] = $_MI['admin_id'];
		$data['a_login_time'] = time();
		$data['a_login_ip'] = AServer::get_ip();
		M('Admin')->update($data);

		/*设置*/
		ASession::set('admin_id', $_MI['admin_id']);
		ASession::set('member_id', $_MI['member_id']);
		ASession::set('m_userid', $_MI['m_userid']);
		ASession::set('m_username', $_MI['m_username']);
		ASession::set('m_status', $_MI['m_status']);
		ASession::set('ar_name', $_MI['ar_name']);
		ASession::set('admin_role_id', $_MI['admin_role_id']);

		ACookie::set('lang', ARequest::get(C('VAR.LANG')));

		/* 写入管理员日志信息 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('LOGIN'));

		$this->success(L('LOGIN_SUCCESS'), AServer::get_preUrl());
	}

	public function logout_do()
	{
		ASession::clear();
		$this->success(L('LOGOUT_SUCCESS'), Url::U('login/index'));
	}
}

?>