<?php
/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide.login.php,v2 19:47 2008-09-19 Lay $
*/
require '../../include/loader.php';
html_start("用户登录 VeryIDE");

/////////////////////////////////

$jump = getgpc('jump');
$action = getgpc('action');

//Cookie名称
$cookie = 'admin';
		
//连接数据库
System :: connect();

switch ($action){
	
	//登录
	case "login":
		
		$username = getgpc("username");
		$password = getgpc('password');
		
		$result = System :: admin_login( $username, $password );
		
		/*
		if( $Global["captcha"] == "on" && ( !$_COOKIE["VCODE"] || $_POST["captcha"] != $_COOKIE["VCODE"] ) ){
	
			$Project["message"]	="验证码输入错误!";
			$Project["jump"]	="";
			$Project["class"]	="info";			

		}
		*/
		
		switch( $result ){
			
			case -1:			
				$_G['project']['message']	="登录信息不能为空!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="info";			
			break;
						
			case -2:			
				$_G['project']['message']	="验证码输入错误!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="info";			
			break;
						
			case -3:			
				$_G['project']['message']	="密码或随机验证输入错误!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="info";			
			break;
						
			case -4:			
				$_G['project']['message']	="当前IP不在授权的范围!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="wrong";
			break;
						
			case -5:			
				$_G['project']['message']	="用户名或密码错误!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="wrong";			
			break;
						
			case -6:			
				$_G['project']['message']	="该用户已经在登录状态!";
				$_G['project']['jump']	="";
				$_G['project']['class']	="wrong";			
			break;
			
			default:			
				//写入Cookie
				if( $_G['setting']['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.replace("'.VI_BASE.'");</script>';
				exit;
			break;			
			
		}
		
		//清空验证码
		$_SESSION['captcha'] = '';
		
	break;

	//退出
	case "exit":
	
		System :: admin_logout();
					
		//重载菜单
		echo '<script type="text/javascript">parent.location.href="'.str_replace("//","/",VI_BASE.'/?T='.date("Y-m-d H:i:s")).'";</script>';		
		exit();
		
	break;

	//未登录
	default:
	
		$username = urldecode($_COOKIE[$cookie]);
		
	break;
}

//关闭数据库
System :: connect();

$_SESSION['RNDCODE'] = rand_string(16);			

echo loader_script(array(VI_BASE."js/mo.hash.js"),"utf-8",$_G['product']['version']);

?>
<script type="text/javascript">

Mo.reader(function(){
	
	//绑定表单事件
	Mo("#wrapper form").bind( 'submit', function( index , e ){

		var result = Mo.Form( this, function(x){
			Mo.Message("wrong",'<div class="s"></div><div class="c">'+x+'</div><div class="e"></div>', 3, { "unique" : "message", "center" : true });
		});
		
		//将密码加密
		return result && Mo('#password').value( Mo.md5( Mo('#password').value() ) ) && Mo( "input[type=submit]", this ).disabled() && Mo.Message("info",'<div class="s"></div><div class="c">正在验证，请稍后…</div><div class="e"></div>', 10, { "unique" : "message", "center" : true });

	});

});

////////////////////////////////

function showCode(){
	
	var user = Mo("#username").value();
	var pass = Mo("#password").value();
	
	if( "<?php echo $_G['setting']['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/get.captcha.php?rnd="+Math.random()} );
	
	}
}

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

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

</script>

    
    <p>&nbsp;</p>
    
    <div id="licence">
        <p><a href="http://www.veryide.com/" target="_blank"><img src="<?php echo VI_BASE;?>image/model/<?php echo $_G['licence']['type'];?>.gif" alt="<?php echo $_G['version'][$_G['licence']['type']]['name'];?>" /></a></p>
        <p class="text-key">VeryIDE <?php echo $_G['version'][$_G['licence']['type']]['name'];?></p>
        <p><?php echo $_G['product']['powered'];?></p>
    </div>
    
    <form action="?" method="post" style="float:left;">
    
    <table cellpadding="0" cellspacing="0" class="form">
        
        <tr>
            <td width="50"><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();" fix_name="用户账号" fix_null="yes" /></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();" fix_name="用户密码" fix_null="yes" />
            </td>
        </tr>
        
        <?php
        
        if( $_G['setting']['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="50" maxlength="4" tabindex="3" autocomplete="off" fix_name="验证字符" fix_null="yes" />
                <img id="Getcaptcha" onclick="rndCode(this);" style="cursor:pointer;" align="absmiddle" />
            </td>
        </tr>
        
        <?php
        }
        ?>
    
        <tr class="nobr">
            <td><strong></strong></td>
            <td>
                <input type="hidden" name="action" id="action" value="login" />
                <input type="hidden" name="jump" id="jump" value="<?php echo urlencode($jump);?>" />
                <input type="hidden" name="rndcode" value="<?php echo $_SESSION['RNDCODE'];?>" />
                <input type="submit" name="Submit" value="立即登录" class="submit" />
            </td>
        </tr>
    
        <tr class="nobr">
            <td><strong></strong></td>
            <td>
                
				<?php                
                //错误信息
                if ( $_G['project']['message'] ){
                    
                    echo '<div class="text-no">'.$_G['project']['message'].'</div>';
                    
                    $_G['project']['message'] = "";
                    
                }else{
					echo '<div><img src="'.VI_BASE.'image/icon/lock.png" align="absmiddle" />  已使用安全方式登录</div>';
				}
                ?>
                
            </td>
        </tr>
    
    </table>

    </form>

	</div>
	
<?php html_close();?>