<?php
class loginAction extends security_normal {
	
	function action(){
		$miceSession = new session();
		if($miceSession->get('mu__userid')){
			$jsstring='Dialog.alert(\'您已成功登录\',function(){window.location.href="'.MICE_URL.'index.php/user";window.event.returnValue = false;},300,100,"提示信息");';
			MessageBox($jsstring);
			exit;
		}

		global $conn;
		if($_POST){
			if($_POST['checkcode']!=$miceSession->get('checkcode')){
				$jsstring='Dialog.alert("验证码输入错误",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			if($_POST['loginname']==''){
				$jsstring='Dialog.alert("请填写登录名",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			if($_POST['password']==''){
				$jsstring='Dialog.alert("密码不能为空",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			$loginsuccess = 0;
			$loginname = strtolower($_POST['loginname']);
			if($loginname){
				$user=new model_User();
				$user->setSubsection("WHERE loginname='".$loginname."'");
				$result=$user->doSelectOne();

				if(!$result){
					$jsstring='Dialog.alert(\'用户名、密码或验证码错误\',function(){window.location.href="'.MICE_URL.'";window.event.returnValue = false;},300,100,"提示信息");';
					MessageBox($jsstring);
					exit;
				}

				$pwd = sha1($result->getSalt().$_POST['password']);		

				//判断密码，验证码，
				if($pwd == $result->getPassword()){

					$usergroup=new model_Usergroup();
					$usergroup->setSubsection('WHERE usergroup_id='.$result->getUsergroupId());
					$usergroups=$usergroup->doSelectOne();

					//判断到期时间和状态
					if(($result->getStopdate()!=0 and $result->getStopdate()<time()) or $result->getStatus()!=0){
						$jsstring='Dialog.alert(\'您的帐号已经到期或被禁用\',function(){window.location.href="'.MICE_URL.'";window.event.returnValue = false;},300,100,"提示信息");';
						MessageBox($jsstring);
						exit;
					}else{
						$miceSession->set('mu__loginname', $result->getLoginname());
						$miceSession->set('mu__userid', $result->getUserId());
						$miceSession->set('mu__groupid', $result->getUsergroupId());
						$miceSession->set('mu__grouplevel', $usergroups->getLevel());
						$miceSession->set('usergroupmenu', $usergroups->getMenu());
						$miceSession->set('usergroupmodule', $usergroups->getModule());
						$user->setLastlogindate(time());
						$user->setLastloginip(GetRealIp());
						$user->doUpdate();
						$jsstring='Dialog.alert("登录成功",function(){history.back(-1);});';
						MessageBox($jsstring);
						exit;
					}
				}
			}
			
			if($loginsuccess == 0){
				$jsstring='Dialog.alert("用户名、密码或验证码错误,请重试",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}
		}

		$smarty = new Smarty();
		$smarty->load_filter('pre','micePreCompileIndex');
		$smarty->assign("name",$name);
		$smarty->assign("checkcode",MICE_URL.'index.php/checkcode');

		$filename = new getTPLFileName(__CLASS__);
		$smarty->display($filename->TPLFileName);
	}

}
?>