<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: /process/test.php
*
*/

ob_start();
session_start();
header('Content-type: application/json');
Header("Content-Type: text/html;charset=UTF-8");
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit();}
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/libmail.php');

if(isset($_POST['login']))
{
	$login = clean($_POST['login']);
	$password = mdHashPass($_POST['password']);
	
	if(empty($login))
	{
		$err++;
		$errMess[$err] = 'Поле логин не заполнено';
	}
	
	if(empty($_POST['password']))
	{
		$err++;
		$errMess[$err] = 'Поле пароль не заполнено';
	}
	
	
	$q = $mysql->prepare("SELECT * FROM db_users WHERE Login = ? AND Password = ?");
	$q->execute(array($login, $password));
	if($q->rowCount() == 0)
	{
		$err++;
		$errMess[$err] = 'Не верные данные';
	}
	
	if($err == 0)
	{
		$w = $q->fetch();
		$_SESSION['id'] = $w['Uid'];
		$_SESSION['login'] = $w['Login'];
		
		echo json_encode($result = array('fail' => '<script type="text/javascript">location.href="/account";</script>'));
		
	}
	else
	{
		foreach($errMess as $key => $value)
		{
			$tmp .= '<b>'.$value.'<b><br><br>';
		}
		echo json_encode($result = array('fail' => $tmp));
	}
}
else
{
	exit();
}


?>