<?
class login extends action{
	function	form()
	{
		global $glo_site_version;
		$this->init_tpl();
		$this->tpl->out['glo_site_version']	=	$glo_site_version;
		$this->tpl->out['is_checkcode']		=	$this->db->getNumber("select value from {$this->db->db_pre}site_config WHERE name='is_checkcode' LIMIT 1");
		$this->tpl->display('login.form.html');
	}

	function save_login()
	{
		$user_name	=	$_POST['username'];
		$password	=	$_POST['password'];
		$checkcode	=	$_POST['checkcode'];
		
		global $_ADMIN_LANG;
		$result_user_name		=	$this->db->getRecord("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_name' AND value='".$this->db->escape($user_name)."'");
		$result_password		=	$this->db->getRecord("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_pass' AND value='".encrypt_admin_pass($password)."'");
		if(!$result_user_name||!$result_password){
			$this->system_message($_ADMIN_LANG['user_login_info_error'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));
			exit;
		}
		$is_checkcode			=	$this->db->getNumber("SELECT value FROM {$this->db->db_pre}site_config where   name='is_checkcode' LIMIT 1");
		if($is_checkcode){
			if(md5($checkcode)!=$_SESSION['checkcode']['mdfivevalue']){
				$this->system_message($_ADMIN_LANG['verification_code_error'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));
				exit;
			}
		}
	
		$_SESSION['user']	=	array(
			'user_name'		=>	$user_name,
			'password'		=>	$password,
			'login_time'	=>	time(),
		);
		header('Location:'.$this->domain_directory.'admin');
		exit;
	}

	function forget_pass()
	{
		global $_ADMIN_LANG;
		$admin_name		=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_name' LIMIT 1");
		$admin_email	=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_email' LIMIT 1");
		$email_setting	=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='email_setting' LIMIT 1");
		$email_setting	=	storage_dencode($email_setting);

		$get_pass_tmp_password	=	mt_rand(100000,999999);
		$get_pass_link_code		=	md5($get_pass_tmp_password);	
		
		$this->db->query("INSERT INTO {$this->db->db_pre}site_config SET name='get_pass_tmp_password',value='".encrypt_admin_pass($get_pass_tmp_password)."',update_time='".time()."' ON DUPLICATE KEY UPDATE value='".encrypt_admin_pass($get_pass_tmp_password)."'");

		$this->db->query("INSERT INTO {$this->db->db_pre}site_config SET name='get_pass_link_code',value='{$get_pass_link_code}',update_time='".time()."' ON DUPLICATE KEY UPDATE value='{$get_pass_link_code}'");

		$desc	=<<<html
		&nbsp;&nbsp;{$_ADMIN_LANG['system_name']} {$_ADMIN_LANG['system_admin_password_reset']}<br>
		&nbsp;&nbsp;{$_ADMIN_LANG['your_account_is']}&nbsp;:&nbsp;		{$admin_name}<br>
		&nbsp;&nbsp;{$_ADMIN_LANG['temporary_password_is']}&nbsp;:&nbsp;		{$get_pass_tmp_password}<br>
		&nbsp;&nbsp;{$_ADMIN_LANG['password_recovery_link_is']}&nbsp;:&nbsp;<a href='{$this->domain_name}{$this->domain_directory}admin/index.php?act=login.new_set_pass&link_code={$get_pass_link_code}'>{$this->domain_name}{$this->domain_directory}admin/index.php?act=login.new_set_pass&link_code={$get_pass_link_code}</a>
html;
		if($email_setting['type']=='smtp'){
			$smtp_arr	=	array(
				'host'	=>$email_setting['smtp']['host'],
				'user'	=>$email_setting['smtp']['user'],
				'pass'	=>$email_setting['smtp']['pass'],
			);
		}else{
			$smtp_arr	=	array();
		}
		if(!$email_setting['reply']){
			$email_setting['reply']		=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_email'");
		}
		if(!$email_setting['from_name']){
			$email_setting['from_name']		=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_name'");
		}
		$send_email_result				=	sendmail($admin_email,str_replace('$1',$_ADMIN_LANG['system_name'],$_ADMIN_LANG['background_password_of_system']),$desc,$email_setting['reply'],$email_setting['from_name'],$email_setting['port'],$smtp_arr,$email_setting['reply']);
		if($send_email_result){
			$this->system_message($_ADMIN_LANG['mail_reset_delivery_success'],'succeed',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));	
			exit;
		}else{
			$this->system_message($_ADMIN_LANG['mail_delivery_failure'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));	
			exit;
		}	
	}

	function new_set_pass()
	{
		global $glo_site_version;
		$link_code	=	$_GET['link_code'];
		$result		=	$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='get_pass_link_code' AND value='".$this->db->escape($link_code)."'");
		if(!$result){
			exit;
		}
		$this->init_tpl();
		$this->tpl->out['glo_site_version']		=	$glo_site_version;
		$this->tpl->display("login.new_set_pass.html");

	}

	function save_new_set_pass()
	{
		global $_ADMIN_LANG;
		$admin_name	=	$_POST['admin_name'];
		$result		=	$this->db->getRecord("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_name' AND value='".$this->db->escape($admin_name)."'");
		if(!$result){
			$this->system_message($_ADMIN_LANG['admin_name_error'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));	
			exit;
		}	
		
		$tmp_pass	=	$_POST['tmp_pass'];
		$result		=	$this->db->getRecord("SELECT value FROM {$this->db->db_pre}site_config WHERE name='get_pass_tmp_password' AND value='".encrypt_admin_pass($this->db->escape($tmp_pass))."'");
		if(!$result){
			$this->system_message($_ADMIN_LANG['temporary_password_error'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));	
			exit;
		}
		$new_pass	=	$_POST['new_pass'];
		if($new_pass){
			$result	=	$this->db->updateRecord("{$this->db->db_pre}site_config"," name='admin_pass' ",array('value'=>encrypt_admin_pass($new_pass)));
			if($result){
				$this->system_message($_ADMIN_LANG['password_reset_success'],'succeed',array($_ADMIN_LANG['back_to_the_log_in_page']=>'index.php?act=login.form'));	
				exit;
			}
		}
	
	}
	function logout()
	{
		unset($_SESSION['user']);
		global $_ADMIN_LANG;
		$this->system_message($_ADMIN_LANG['successfully_logout'],'succeed',array($_ADMIN_LANG['back_to_the_front_end']=>$this->domain_directory,$_ADMIN_LANG['log_in_again']=>$this->domain_directory.'admin'));	
	}
}
	
