<?php
session_start();
session_destroy();

$x_dbhost='mysql:host=localhost;dbname=supercryptex';
$x_dbuser='root';
$x_dbpass='';
?>
<!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, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title>IPFlare | Login</title>

  <!-- Custom fonts for this template-->
  <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

  <!-- Custom styles for this template-->
  <link href="./css/login_register.css" rel="stylesheet">
  <link rel="stylesheet" href="css/style.css"/>
  <!---<link href="css/sb-admin-2.min.css" rel="stylesheet">--->

</head>

<body class="" style="background-color: #0a183d;">

  <div class="container">

    <!-- Outer Row -->
    <div class="row justify-content-center">

      <div class="col-xl-10 col-lg-12 col-md-9">

        <div class="card o-hidden border-0 shadow-lg my-5">
          <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
             <div class="col-lg-6 d-none d-lg-block bg-login-image"></div>
             <div class="col-lg-6">
              <div class="p-5">
                <div class="text-center">
                  <h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
                </div>
                <form class="user" method="post">
                  <div class="form-group">
                    <input type="text" class="form-control form-control-user" style="text-transform: lowercase;" aria-describedby="emailHelp" placeholder="Username" name="nm" id="nm" required>
                  </div>
                  <div class="form-group">
                    <input type="password" class="form-control form-control-user" placeholder="Password" name="pw" id="pw" required>
                  </div>
                  <div class="form-group">
                    <div class="custom-control custom-checkbox small">
                      <input type="checkbox" class="custom-control-input" id="customCheck">
                      <label class="custom-control-label" for="customCheck">Remember Me</label>
                    </div>
                  </div>
                  <!---<a href="index.html" class="btn btn-primary btn-user btn-block">Login</a>--->
                  <button type="submit" class="btn btn-primary btn-user btn-block">Login</button>
                  <hr>
                  <a href="" class="btn btn-google btn-user btn-block">
                    <i class="fab fa-google fa-fw"></i> Login with Google
                  </a>
                  <a href="" class="btn btn-facebook btn-user btn-block">
                    <i class="fab fa-facebook-f fa-fw"></i> Login with Facebook
                  </a>
                </form>

                <?php

                $is_login=false;

                // Login Attempt Code
                $ip = $_SERVER["REMOTE_ADDR"];
                $mysqli = new mysqli("localhost","root","","supercryptex");
                $attempt_result = mysqli_query($mysqli, "SELECT COUNT(*) FROM `ip_attempt` WHERE `ip` LIKE '$ip' AND `time` > (now() - interval 10 minute)");
                $attempt_count = mysqli_fetch_array($attempt_result, MYSQLI_NUM);

                if (isset($_POST["nm"]) and isset($_POST["pw"])) {
                  if ($attempt_count[0] >= 3) {
                    echo "<script>alert('You\'ve Entered the Wrong Login Credentials More Than 3 Times! Try Again in 10 Minutes')</script>";
                  }
                  else {

                    $name=strtolower(strip_tags($_POST["nm"]));
                    $password=strip_tags($_POST["pw"]);
                    $pdo=new PDO($x_dbhost, $x_dbuser, $x_dbpass);
                    $stmt = $pdo->prepare("SELECT * FROM users WHERE name=:nm and password=PASSWORD(:pw)");
                    $stmt->execute(array(':nm' => $name, ':pw' => $password));
                    $result = $stmt->fetch();

                    if ($result) {
                      session_start();
                      $_SESSION["user_id"] = $result["user_id"];
                      $_SESSION["name"] = $result["name"];
                      $_SESSION["fullname"] = $result["fullname"];
                      $_SESSION["email"] = $result["email"];
                      $_SESSION["birthday"] = $result["birthday"];
                      $_SESSION["phone"] = $result["phone"];
                      $_SESSION["address"] = $result["address"];
                      $_SESSION["country"] = $result["country"];
                      $_SESSION["image"] = $result["image"];
                      $_SESSION["password"] = $result["password"];
                      $_SESSION["host_private"] = $result['host_private'];
                      $_SESSION["host_public"] = $result['host_public'];
                      $_SESSION["client_private"] = $result['client_private'];
                      $_SESSION["client_public"] = $result['client_public'];

                      $_SESSION["dbhost"]=$x_dbhost;
                      $_SESSION["dbuser"]=$x_dbuser;
                      $_SESSION["dbpass"]=$x_dbpass;

                      echo '<div class="text-center"><p style="color: green;">Login Success!<br></p></div>';
                      header("Location: ./main.php");
                      $is_login=true;
                    }
                    else {
                      // Login Attempt Code
                      mysqli_query($mysqli, "INSERT INTO `ip_attempt` (`ip`, `time`) VALUES ('$ip', CURRENT_TIMESTAMP)");

                      echo '<div class="text-center"><p style="color: red;">Login Failed!<br></p></div>';
                      $is_login=false;
                    }
                  }
                }
                ?>
                
                <hr>
                <div class="text-center">
                  <a class="small" href="./forgot-password.php">Forgot Password?</a>
                </div>
                <div class="text-center">
                  <a class="small" href="./register.php">Create an Account!</a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>

  </div>

</div>

<!--====== Javascripts & Jquery ======-->
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.slicknav.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/mixitup.min.js"></script>
<script src="js/main.js"></script>

<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>

</body>

</html>
