<?
define('ADMIN',true);
define('AJAX',true);
define('ROOT','../../..');
header('Content-Type: text/javascript; charset=utf-8');
require_once(ROOT.'/.config.php');
require_once(LIBS_PATH.'/system/sys_start.php');
require_once(LIBS_PATH.'/system/load_libs.php');
///////////////////////////

//if($_SESSION['backend']['login_procedure']==1) {
	$X->STR->post_filter();
	$login=mysql_real_escape_string($_POST['login']);
	$password=mysql_real_escape_string($_POST['password']);
	$encoded_password=md5('d:'.$password.'<>'.$password.':h');


	if(!empty($login) && !empty($password) && count($_POST)==2) $user=$X->DB->fast(' SELECT * FROM _admin_users WHERE login="'.$login.'" AND password="'.$encoded_password.'" LIMIT 1 ');

	if($user['id']!=1 && $user['active']!=1) unset($user);


	if(!empty($user['id'])) {
		$checksum=md5('dh::'.$password.'@'.$X->STR->outt($user['login']).'<<'.(int)$user['id']);
		if($checksum==$user['auth_checksum']) {
			$_SESSION['backend']['user_id']=$user['id'];
			$_SESSION['backend']['css_color']=$user['css_color'];
			$_SESSION['backend']['last_activity']=$user['last_activity'];
			$_SESSION['backend']['last_ip']=$user['last_ip'];
			if($user['id']==1) $_SESSION['backend']['is_root']=1;
			unset($_SESSION['backend']['login_procedure']);

			echo '
				setTimeout(function(){ window.location=window.location; },200);
				$("#top_logo, #login_box").fadeOut(200);
			';
		} else {
			$_SESSION['backend']['xCHECK']['login']=true;
			$X->ADMIN->disable_interface($user,true);
			$head='
				$("#sync_check_img").remove();
				$("body").append(\''.ADDON_BODY.'\');
				setTimeout(function(){ $("#sync_check_img").remove(); },2000);
			';
			unset($user);
		}
	}
//}


if(empty($user['id'])) {
	$t=40;
	$px=-60;
	$easing='easeOutBack';

	echo '
		L=$.trim($("#input_login").val());
		if(L.length==0) {
			$("#input_login").val("").focus();
			$("#input_password").val("");
		} else {
			$("#input_password").val("").focus();
		}

		F=$("#form_cont");

		$(F).stop().animate({"margin-left":"'.$px.'px"}, '.$t.', "'.$easing.'", function(){
			$(F).animate({"margin-left":"0px"}, '.$t.', "'.$easing.'", function(){
				$(F).animate({"margin-left":"'.$px.'px"}, '.$t.', "'.$easing.'", function(){
					$(F).animate({"margin-left":"0px"}, '.$t.', "'.$easing.'", function(){
						$(F).animate({"margin-left":"'.$px.'px"}, '.$t.', "'.$easing.'", function(){
							$(F).stop().animate({"margin-left":"0px"}, '.$t.', "'.$easing.'", function(){
								send_pending=0;
								$("#input_submit").removeClass("dis");
							})
						})
					})
				})
			})
		});
		'.$head.'
	';
}


if(!empty($login) || !empty($password)) {
	if(empty($user['id'])) {
		$login_fail=1;
	} else {
		$password=$encoded_password;
		$login_fail=0;
	}

	$X->DB->q(' INSERT INTO _admin_users_login (session_id,user_id,login,password,date,ip,host,browser,user_agent,login_fail) VALUES ("'.SID.'","'.(int)$user['id'].'","'.$login.'","'.$password.'","'.date('Y-m-d H:i:s').'","'.$_SERVER['REMOTE_ADDR'].'","'.$_SERVER['REMOTE_HOST'].'","'.$CONF->BROWSER['full_name'].' '.$CONF->BROWSER['version'].$CONF->BROWSER['special'].'","'.$CONF->BROWSER['agent'].'","'.$login_fail.'") ');
	if($login_fail==0) {
		$X->DB->q(' UPDATE _admin_users SET last_login="'.DATE.'", last_ip="'.IP.'" WHERE id="'.$user['id'].'" ');
		$_SESSION['backend']['login_id']=$X->DB->fast(' SELECT id FROM _admin_users_login WHERE session_id="'.SID.'" AND user_id="'.$user['id'].'" ORDER BY id DESC LIMIT 1 ');
	} else {
		unset($_SESSION['backend']['login_procedure']);
	}
}
?>