<?
function error($text){
	global $mod;
	if ($_SERVER['REQUEST_METHOD']=="POST" && $_POST['login'] && $_POST['pass'] && $mod!="login"){
		echo '<?xml version="1.0" encoding="utf-8"?>
			<response>
				<result>300</result>
				<comment>'.$text.'</comment>
			</response>';	
	} else {
		echo json_encode(array('success'=>false,'errors'=>$text));
	}
	
	die();
}

function success($data=''){
	if ($_SERVER['REQUEST_METHOD']=="POST" || ($_POST['login'] && $_POST['pass'])){
		echo json_encode(array('success'=>true,'data'=>$data));
	}
}

function login(){
	global $_SESSION,$DB;
	
	$login=preg_replace("/[^a-zA-Z0-9_]/","",$_POST["login"]);
	$pass =$_POST["pass"];
	
	if(!$login||!$_POST["pass"]||($login!=$_POST["login"])) error("Введите в формате [a-zA-Z0-9_]!"); 
	
		
	$user=$DB->selectRow("SELECT * from users where login=? and pass=?",$login,md5($pass));

	if (!$user[id]){
			error("Неправильный логин или пароль!");
		} else {
			$_SESSION["logined"]=true;
			$_SESSION["id"]=$user[id];
			$_SESSION["UserName"]=$login;
			$_SESSION["User_Name"]=$user[full_name];

			success();
		}
	

}
?>