<?php
ob_start();
$title = 'Авторизация';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
?>
<body>
    <div class="container">
      <form class="form-signin" action="" method="POST">

        <h2 class="form-signin-heading">Авторизация</h2>
        <?php
		if(isset($_POST['reg'])){
			$_POST['email'] = input($_POST['email']);
			$_POST['inputPassword'] = input($_POST['inputPassword']);
			if(empty($_POST['email'])){
				showAlertDanger('Введите E-Mail');
			}elseif(empty($_POST['inputPassword'])){
				showAlertDanger('Ведите пароль');
			}elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
				showAlertDanger('Введите E-Mail корректно');
			}elseif($db->query("SELECT `id` FROM `users` WHERE `email`='".$_POST['email']."'")->num_rows==0){
				showAlertDanger('Пользователя с таким E-Mail не существует');
			}elseif($db->query("SELECT `id` FROM `users` WHERE `email`='".$_POST['email']."' AND `password`='".md5($_POST['inputPassword'])."'")->num_rows==0){
				showAlertDanger('Неверный пароль');
			}else{
				$sess = md5(time());
				$us = $db->query("SELECT * FROM `users` WHERE `email`='".$_POST['email']."' AND `password`='".md5($_POST['inputPassword'])."'")->fetch_assoc();
				$db->query("UPDATE `users` SET `sess`='".$sess."' WHERE `id`='".$us['id']."'");
				setcookie("id", $us['id'], time() + 60 * 60 * 24 * 30 * 12, "/");
				setcookie("sess", $sess, time() + 60 * 60 * 24 * 30 * 12, "/");
				header('location:/');
			}
		}
        ?>
        <label for="inputEmail" class="sr-only">E-Mail адрес</label>
        <input type="email" name="email" id="inputEmail" class="form-control" placeholder="E-Mail адрес" required autofocus>
        <div class="inputPasswordAuth">
	        <label for="inputPasswordAuth" class="sr-only">Пароль</label>
	        <input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Пароль" required>
	    </div>
	    <script src="//ulogin.ru/js/ulogin.js"></script>
	    <div class="ulogin">
			<div id="uLogin" data-ulogin="display=panel;theme=flat;fields=email,first_name,last_name;providers=vkontakte,odnoklassniki,twitter,facebook;hidden=;redirect_uri=http%3A%2F%2Fmagaz.ru%2Fphp%2Fmodules%2Flogin%2Fsoc_reg.php;mobilebuttons=0;"></div>
		</div>
        <button class="btn btn-lg btn-primary btn-block" type="submit" name="reg">Войти</button>
      </form>
    </div>