<?php
$configfile = 'config.php';
if (!file_exists($configfile)) {
    echo '<meta http-equiv="refresh" content="0; url=install" />';
    exit();
}

include "config.php";

session_start();

if (isset($_SESSION['sec-username'])) {
    $uname = $_SESSION['sec-username'];
    $table = $prefix . 'users';
    $suser = mysqli_query($connect, "SELECT * FROM `$table` WHERE username='$uname'");
    $count = mysqli_num_rows($suser);
    if ($count > 0) {
        echo '<meta http-equiv="refresh" content="0; url=dashboard.php" />';
        exit;
    }
}
    
$_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
    
$error = 0;
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
        <title>Project SECURITY &rsaquo; Admin Panel</title>

        <!-- CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/all.css">

        <!-- Favicon -->
        <link rel="shortcut icon" href="assets/img/favicon.png">
		
		<style>
html,
body {
  height: 100%;
}

body {
  display: -ms-flexbox;
  display: -webkit-box;
  display: flex;
  -ms-flex-align: center;
  -ms-flex-pack: center;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  padding-top: 0px;
  padding-bottom: 140px;
  background-color: #f5f5f5;
}

.form-signin {
  width: 100%;
  max-width: 380px;
  padding: 15px;
  margin: 0 auto;
}

.form-signin .form-control {
  position: relative;
  box-sizing: border-box;
  height: auto;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
		</style>
    </head>

    <body class="text-center">
	    <form class="form-signin" action="" method="post">
		
		<h1 class="h3 mb-3 font-weight-normal"><i class="fab fa-get-pocket"></i> Project <strong>SECURITY</strong></h1>
<?php
    if (isset($_POST['signin'])) {
        $username = mysqli_real_escape_string($connect, $_POST['username']);
        $password = hash('sha256', $_POST['password']);
        $table    = $prefix . "users";
        $check    = mysqli_query($connect, "SELECT username, password FROM `$table` WHERE `username`='$username' AND password='$password'");
        if (mysqli_num_rows($check) > 0) {
            $_SESSION['sec-username'] = $username;
            echo '<meta http-equiv="refresh" content="0;url=dashboard.php">';
        } else {
            echo '<br />
		<div class="alert alert-danger">
              <i class="fas fa-exclamation-circle"></i> The entered <strong>Username</strong> or <strong>Password</strong> is incorrect.
        </div>';
            $error = 1;
        }
    }
?> 
			<div class="form-group has-feedback <?php
    if ($error == 1) {
        echo 'has-danger';
    }
?>">
            <div class="input-group">
                    <div class="input-group-prepend">
                    <div class="input-group-text"><em class="fa fa-fw fa-user"></em></div>
				    </div>
				<input type="username" name="username" class="form-control <?php
    if ($error == 1) {
        echo 'is-invalid';
    }
?>" placeholder="Username" <?php
    if ($error == 1) {
        echo 'autofocus';
    }
?> required>
            </div>
			</div>
            <div class="form-group has-feedback">
			    <div class="input-group">
                    <div class="input-group-prepend">
                    <div class="input-group-text"><em class="fa fa-fw fa-key"></em></div>
				    </div>
                <input type="password" name="password" class="form-control" placeholder="Password" required>
				</div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <button type="submit" name="signin" class="btn btn-lg btn-primary btn-block"><i class="fas fa-sign-in-alt"></i>
&nbsp;Sign In</button>
                </div>
            </div>
        </form> 

        <!-- Javascript -->
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js"></script>

    </body>
</html>