<?php
 	include_once('includes/db_connect.php');
	include_once('includes/global_functions.php');

	if($_SERVER['REQUEST_METHOD'] == "POST"){
		//Get username and password
		$username  = safeString($_POST['username']);
		$password = safeString($_POST['password']);
		$remember = isset($_POST['remember']) ? $_POST['remember'] : 0;

		//Check if user exists
		if($stmt1 = $mysqli -> prepare("SELECT uid, password FROM login WHERE username=?")){
			$stmt1 -> bind_param('s', $username);
			$stmt1 -> execute();
			$stmt1 -> bind_result($id, $password_hash);
			$stmt1 -> fetch();
			$stmt1 -> close();
		}	
		if($password_hash == null){
			logout("nouser");
		}else{
			//Check if password is correct
			if(!password_verify($password, $password_hash)){
				logout("nouser");
			}
			else
			{
				//Get user ID, IP
				$ip = $_SERVER['REMOTE_ADDR'];

				//Generate Session ID
				$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				$session_id = '';
				for ($i = 0; $i < 60; $i++) {
					$session_id .= $characters[rand(0, strlen($characters) - 1)];
				}
				//Update Session ID, Last Login, IP in database
				if($stmt1 = $mysqli -> prepare("UPDATE login SET session_id=?, ip=?, `last_login`=`current_login`, `current_login`=(NOW() + INTERVAL 7 HOUR), `remember`=? WHERE `uid`=?")) {
						$stmt1 -> bind_param('sssi', $session_id, $ip, $remember, $id);
						$stmt1 -> execute();
						$stmt1 -> close();
				}	
				
				//Set Session ID
				setSession($session_id);

				//Reroute user to account page
					die(header("Location: index.php"));
			}
		}			
	}
?>
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Salty radio</title>

    <link href="includes/css/bootstrap.min.css" rel="stylesheet">
    <link href="includes/font-awesome/css/font-awesome.css" rel="stylesheet">

    <!-- Morris -->
    <link href="includes/css/plugins/morris/morris-0.4.3.min.css" rel="stylesheet">

    <link href="includes/css/animate.css" rel="stylesheet">
    <link href="includes/css/style.css" rel="stylesheet">
    
	<link href="includes/js/plugins/colorpicker/bootstrap-colorpicker.min.css" rel="stylesheet">
</head>

<body class="fixed-sidebar gray-bg">
    <div class="middle-box text-center loginscreen animated fadeInDown">
        <div>
            <div>
                <h1 class="logo-name">S®</h1>
            </div>
            <h3>SaltyRadio</h3>
            <p>Inlog pagina voor onze alarm</p>
            <?php if(isset($_GET['error'])){ ?>
                <div class="alert alert-danger">
                Er is iets fout gegaan.
                </div>
            <?php } ?>

            <?php if(isset($_GET['nosession'])){ ?>
                <div class="alert alert-info">
                Je bent niet ingelogd.
                </div>
            <?php } ?>

            <?php if(isset($_GET['session'])){ ?>
                <div class="alert alert-danger">
					De login sessie is afgelopen.
                </div>
            <?php } ?>

            <?php if(isset($_GET['nouser'])){ ?>
                <div class="alert alert-warning">
					Deze gebruikersnaam en wachtwoord combinatie is niet bekend. 
                </div>
            <?php } ?>

            <?php if(isset($_GET['ip'])){ ?>
                <div class="alert alert-danger">
					Ergens anders ingelogd. Log opnieuw in om hier in te loggen.
                </div>
            <?php } ?>

            <?php if(isset($_GET['logout'])){ ?>
                <div class="alert alert-success">
                Succesvol uitgelogd.
                </div>
            <?php } ?>

            <?php if(isset($_GET['verify'])){ ?>
                <div class="alert alert-success">
                Email address has been verified.
                </div>
            <?php } ?>

            <?php if(isset($_GET['expired'])){ ?>
                <div class="alert alert-warning">
                This code is no longer usable.
                </div>
            <?php } ?>

            <?php if(isset($_GET['register'])){ ?>
                <div class="alert alert-success">
                    Succesvol geregistreerd. U kunt nu inloggen.
                </div>
            <?php } ?>
            <form class="m-t" role="form" method="POST" action="">
                <div class="form-group">
                    <input type="text" class="form-control" name="username" placeholder="username" required="">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" name="password" placeholder="password" required="">
                </div>
                <div class="form-group">
                    <div class="checkbox i-checks"><label> <input value="1" name="remember" type="checkbox"><i></i> Onthoud mij</label></div>
                </div>
                <button type="submit" class="btn btn-primary block full-width m-b">Login</button>

                <a href="#"><small>Wachtwoord  vergeten? Kut voor je.</small></a>
                <p class="text-muted text-center"><small>Geen account?</small></p>
                <a class="btn btn-sm btn-white btn-block" href="register.php">Maak een account!</a>
            </form>
            <p class="m-t"> <small>StreamIT &copy; 2016</small> </p>
        </div>
    </div>
<?php 	include_once("includes/footer.php"); ?>
