<?php

/*

		用户主题设置

*/

//载入全局配置和函数包
require_once(dirname(__FILE__)."/../include/global.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $Project["charset"];?>" />
<title>更换主题 - Powered By VeryIDE</title>

<?php

echo loader_style(array(VI_BASE."style/dialog.css"),$Project["charset"],$Project["version"]);

echo loader_script(array(VI_BASE."js/mo.js",VI_BASE."js/mo.hash.js"),$Project["charset"],$Project["version"]);

?>
<script>

Mo.reader(function(){
	
	//绑定表单事件
	Mo("#wrapper form").bind( 'submit', function( index , e ){
		
		//将密码加密
		Mo('#password').value( Mo.md5( Mo('#password').value() ) );

	});

});

</script>
</head>

<body>

	<div id="wrapper">
	
	<?php

	$jump = GetQuery("jump");
	$action = GetQuery("action");
	
	//Cookie名称
	$cookie = $Global["fixed"].'__UN';
	
	//单点登录标识
	$sso = true;
			
	//mysql open
	reader_mysql();		
	
	switch ($action){
		
		//登录
		case "login":
				
			//mysql open
			$error = reader_mysql();
			
			$username = GetQuery("username");
			$password = GetQuery("password");
			
			if( !$password || !$username ){
				
				$Project["message"]	="登录信息不能为空!";
				$Project["jump"]	="";
				$Project["class"]	="info";			
			
			}elseif( $Global["captcha"] == "on" && ( !$_COOKIE["VCODE"] || $_POST["captcha"] != $_COOKIE["VCODE"] ) ){
				
				$Project["message"]	="验证码输入错误!";
				$Project["jump"]	="";
				$Project["class"]	="info";			
			
			}elseif( strlen($password) != 32 || !$_SESSION['RNDCODE'] || $_SESSION['RNDCODE'] != $_POST["rndcode"] ){
				
				$Project["message"]	="用户密码输入错误!";
				$Project["jump"]	="";
				$Project["class"]	="info";
						
				//写入安全日志
				event_insert($mysql,"login",time(),time(),"尝试登录系统时使用密码不符合规则：".$password);
				
			}elseif( $Global["ipzone"] && !checkIP( explode("\n",$Global["ipzone"]) ,GetIP()) ){
				
				$Project["message"]	="当前IP不在授权的范围!";
				$Project["jump"]	="";
				$Project["class"]	="wrong";
						
				//写入安全日志
				event_insert($mysql,"login",time(),time(),"尝试登录系统时使用的IP不在允许的范围内：".GetIP());
				
			}else{
	
				$sql = str_replace("[account]",$username,$Project["sql"]["login"]);
				$sql = str_replace("[password]",$password,$sql);
				
				//exit($sql);
				
				//查询数据库(Manager)
				$result = $db -> query($sql);
				
				if($result==1){
					
					$row=mysql_fetch_assoc($db -> _result);
					
					//同一用户只能登录一次
					if ( $Global["sso"]=="on" && $row["last_active"] && time() - $row["last_active"] <= $Global["interval"] ){
						
						$Project["message"]	="该用户已经在登录状态!";
						$Project["jump"]	="";
						$Project["class"]	="wrong";
						
						$sso = false;
						
					}else{					
					
						//设置Session
						reader_session($row);
						
						//time
						$time = time();
						
						$sql = str_replace("[aid]",$_SESSION["Manager"]["id"],$Project["sql"]["update"]);
						$sql = str_replace("[setting]","stat_login=stat_login+1,last_ip='".GetIP()."',last_login='".$time."'",$sql);
						$db -> query($sql);
						
						//删除日志
						$sql = "select * from ".VI_DBMANPRE."event where event='login' and aid=".$_SESSION["Manager"]["id"];
						$rows = $db -> query($sql);
						
						if($rows > $Global["logsize"]-1){
						
							//删除日志_不支持子查询
							$sql = "delete from ".VI_DBMANPRE."event where dateline<".(time()-604800)." and aid=".$_SESSION["Manager"]["id"];
		
							$db -> execute($sql);
						}
						
						//写入日志
						event_insert($mysql,"login",$time,$time);
						
						//写入Cookie
						if( $Global["remember"] == "on" ){
							setcookie($cookie,urlencode($_SESSION["Manager"]["account"]),time()+60*60*24*30,VI_BASE);
						}else{
							setcookie($cookie,'',0,VI_BASE);
						}
						
						//重载菜单
						echo '<script type="text/javascript">parent.location.reload();</script>';
						
						exit();
					
					}
					
				}else{
					
					$Project["message"]	="用户名或密码错误!";
					$Project["jump"]	="";
					$Project["class"]	="wrong";
				
				}
			}
	
			//mysql close
			reader_mysql(true);			
			
		break;
	
		//退出
		case "exit":
		
			if( $_SESSION["Manager"]["id"] ){
						
				//写入日志
				event_insert($mysql,"exit",time(),time());					
			}
						
			session_unset();
			session_destroy();
						
			//重载菜单
			echo '<script type="text/javascript">parent.location.href="'.str_replace("//","/",VI_BASE.'/?T='.date("Y-m-d H:i:s")).'";</script>';
			
			exit();
			
			$username = urldecode($_COOKIE[$cookie]);
			
		break;
	
		//未登录
		default:
		
			$username = urldecode($_COOKIE[$cookie]);
			
		break;
	}

	//mysql close
	reader_mysql(true);

	//生成随机验证字符串
	$_SESSION['RNDCODE'] = rand_string(16);			
	
	?>

<script type="text/javascript">

function showCode(){
	var user = Mo("#username").value();
	var pass = Mo("#password").value();
	
	if( "<?php echo $Global["captcha"];?>" == "off" ) return false;
	
	if( user && pass ){
	
		Mo("#captcha_box").show();
		
		if( !Mo("#Getcaptcha").attr("src") ) Mo("#Getcaptcha").attr( {"src":"<?php echo VI_BASE;?>api/captcha.php?rnd="+Math.random()} );
	
	}
	
	/*
	
	
	if(user && $("captcha_box").style.display=="none"){
	
		Mo("#captcha_box").show();
		Mo("#Getcaptcha").attr("src","<?php echo VI_BASE;?>api/captcha.php");
		
		if( user && pass ){
			Mo("#captcha").focus();
		}
		
		var intl=window.setInterval(function(){
			
			if( !$('captcha').value ) $("Getcaptcha").src="<?php echo VI_BASE;?>api/captcha.php?rnd="+Math.random();
			
		},60000)
	}else{
		if(!user){
			$("captcha_box").style.display="none";
			clearInterval(intl);
		}
	}
	
	*/
}

function rndCode(o){
	o.src=o.src+'?'+Math.random();
	Mo('#captcha').value('');
	Mo('#captcha').focus();
}

function  detectCapsLock(event){
	var e = event||window.event;
	var o = e.target||e.srcElement;
	
	/*按键的keyCode*/
    var keyCode  =  e.keyCode||e.which; 
	
	/*shift键是否按住*/
    var isShift  =  e.shiftKey ||(keyCode  ==   16 ) || false ;
     if (
	 
	 /*Caps Lock 打开，且没有按住shift键*/
	 ((keyCode >=   65   &&  keyCode  <=   90 )  &&   !isShift)
	 
	 /*Caps Lock 打开，且按住shift键*/
     || ((keyCode >=   97   &&  keyCode  <=   122 )  &&  isShift)
	 ){
		 Mo.Tips(null,o,"bubble","大写锁定键被按下，请注意大小写<br />请按一下键盘上 <span class='text-key'>Caps Lock</span> 键",document,"click",o.offsetWidth+10,-5);
		 
	 }else{
		 Mo("#bubble").remove();
	} 
}

Mo.reader(function(){
	
	if( Mo("#username").value() ){
		Mo("#password").focus();
	}else{
		Mo("#username").focus();
	}
	
	Mo('#password').bind( 'keypress' , detectCapsLock );					  
});

</script>
    
		<form action="?" method="post">
        
        <table cellpadding="0" cellspacing="0" class="form">
            
        	<tr>
            	<td width="60"><strong>用 户：</strong></td>
                <td><input name="username" id="username" type="text" size="20" tabindex="1" maxlength="20" class="text login" value="<?php echo $username;?>" onChange="showCode();" /></td>
            </tr>
        
        	<tr>
            	<td><strong>密 码：</strong></td>
                <td>
                <input name="password" id="password" type="password" size="22" tabindex="2" maxlength="32" class="text login" value="<?php echo "";?>" onChange="showCode();" onFocus="showCode();" onkeyup="showCode();" onkeydown="showCode();" />
                </td>
            </tr>
            
            <?php
			
			if( $Global["captcha"] == "on" ){			
			?>
        
        	<tr id="captcha_box" style="display:none;">
            	<td><strong>验 证：</strong></td>
                <td><input name="captcha" type="text" id="captcha" class="text captcha" size="10" maxlength="4" tabindex="3" autocomplete="off" />
					<img id="Getcaptcha" onclick="rndCode(this);" style="cursor:pointer;" align="absmiddle" />
        		</td>
            </tr>
            
            <?php
			}
			?>
        
        	<tr class="nobr">
            	<td><strong></strong></td>
                <td>
                    <input name="jump" id="jump" type="hidden" value="<?php echo $jump;?>" />
                    <input name="rndcode" type="hidden" value="<?php echo $_SESSION['RNDCODE'];?>" />

                	<input type="submit" name="Submit" value="立即登录" class="submit" />
					<input type="hidden" name="action" id="action" value="login" />
                </td>
            </tr>
        
        	<tr>
            	<td></td>
                <td><img src="<?php echo VI_BASE;?>image/icon/lock.png" align="absmiddle" />  已使用安全方式登录</td>
            </tr>
        
        </table>

		</form>

	</div>
	
</body>
</html>