<?php
/**
 *	Generated by miceCMS
 *	http://www.miceCMS.com/
 */
class loginAction extends security_normal {
	
	function action(){
		$miceSession = new session();
		if($miceSession->get('loginname')){
			header("Location: ".MICE_URL."admin.php/index");
			exit;
		}

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

				if(!$result){
					$jsstring='Dialog.alert(\'登录失败!<br>用户名、密码或验证码错误.\',function(){window.location.href="'.MICE_URL.'admin.php/login";window.event.returnValue = false;},300,100,"返回登录页面");';
					MessageBox($jsstring);
					exit;
					$loginsuccess = 0;
				}

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

				//判断密码，验证码，
				if($pwd == $result->getPassword() and (strtolower($_POST['checkcode']) == $miceSession->get('checkcode') or MICE_CCIMG == 0)){

					$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(\'登录失败!<br>您的帐号已经到期或被禁用.\',function(){window.location.href="'.MICE_URL.'admin.php/login";window.event.returnValue = false;},300,100,"返回登录页面");';
						MessageBox($jsstring);
						exit;
						$loginsuccess = 0;
					}elseif( strlen($usergroups->getMenu())<3 ){
						$jsstring='Dialog.alert(\'登录失败!<br>用户名、密码或验证码错误.\',function(){window.location.href="'.MICE_URL.'admin.php/login";window.event.returnValue = false;},300,100,"返回登录页面");';
						MessageBox($jsstring);
						exit;
						$loginsuccess = 0;
					}else{
						//index模块使用
						$miceSession->set('mu__loginname', $result->getLoginname());
						$miceSession->set('mu__userid', $result->getUserId());
						$miceSession->set('mu__groupid', $result->getUsergroupId());
						$miceSession->set('mu__grouplevel', $usergroups->getLevel());
						
						//admin模块使用
						$miceSession->set('loginname', $result->getLoginname());
						$miceSession->set('userid', $result->getUserId());
						$miceSession->set('usergroupid', $result->getUsergroupId());
						$miceSession->set('usergrouplevel', $usergroups->getLevel());
						$miceSession->set('usergroupmenu', $usergroups->getMenu());
						$miceSession->set('usergroupmodule', $usergroups->getModule());
						$user->setLastlogindate(time());
						$user->setLastloginip(GetRealIp());
						$user->doUpdate();
						header("Location: ".MICE_URL."admin.php/index");
						exit;
						
						$loginsuccess = 1;
					}
				}
			}
			
			if($loginsuccess == 0){
				//die("11");
				$jsstring='Dialog.alert(\'登录失败!<br>用户名、密码或验证码错误。\',function(){window.location.href="'.MICE_URL.'admin.php/login";window.event.returnValue = false;},300,100,"返回登录页面");';
				MessageBox($jsstring);
				exit;
			}
		}

		

		$smarty = new Smarty();
		$smarty->load_filter('pre','micePreCompileAdmin');
		$smarty->assign("title","登录管理");
		$smarty->assign("checkcode",MICE_URL.'admin.php/checkcode');
		$filename = new getTPLFileName(__CLASS__);
		$smarty->display($filename->TPLFileName);
	}

}
?>