<?php 
    session_start();
	if ( !empty($_SESSION['login']) ) {
        header( 'Location: /' );
        die;
	}

    include 'include/header.php';    
?>

    <div class="wrap">
<?php
    include 'include/database.php';
    
    if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
        if ( !empty($_POST['user']) && !empty($_POST['pass']) ) {
            $connection = connect($database);
            login($connection, $_POST['user'], $_POST['pass']);
            $connection = null;
        } else {
            echo '<strong class="warning center block">Missing username or password!</strong>';
        }
    }
?>
    
        <h1 class="pad-top">Login</h1>
        
        <form id="login" method="post" action="">
            <div>
                <label class="block" for="user">username:</label>
                <input id="user" type="text" name="user" value="<?php if ( !empty($_POST['user']) ) { echo htmlspecialchars($_POST['user']); } ?>" />
            </div>
            
            <div class="pad">
                <label class="block" for="pass">password:</label>
                <input id="pass" type="password" name="pass" autocomplete="off" />
            </div>
            
            <input type="submit" value="login" />
        </form>
        
        <div class="center">
            <div class="pad-bottom"><a href="newUser.php">New user?</a></div>
            <div class="pad-bottom"><a href="resetPassword.php">Forgot your password?</a></div>
            <div><a href="//avstew.com/bb/">USM Forums</a></div>
        </div>
        
    </div>

<?php include 'include/footer.php'; ?>