<?php
session_start(); // user email should not be longer 40 characters
date_default_timezone_set('Africa/Lagos');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once './vendor/autoload.php';
require_once './models/User.php';
require_once './include/DatabaseConn.php';
require_once './include/Constants.php';
require_once './include/Helper.php';
require_once './models/LoginRecord.php';
require_once './models/USettings.php';
require_once './include/GoogleAuthenticator.php';
require_once './models/Settings.php';


if (isset($_SESSION[URL]) && isset($_SESSION[USERV])) {

} else {
    header("location: login");
    die();
}

if (isset($_POST['code'])) {
    $db = new DatabaseConn();
    $link = $db->connect();
    $user = User::getCurrentUser($link, $_SESSION[USERV]);
    $ga = new PHPGangsta_GoogleAuthenticator();
    $secret = $user->getSecret();
    $checkResult = $ga->verifyCode($secret, $_POST['code'], 2);

    if (!$checkResult) {
        $error = "Invalid code entered";
    } else {
        $_SESSION[USER] = $user->getEmail();
        header("location: " . $_SESSION[URL]);
        die();
    }
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <?php require_once './partials/head.php'; ?>
    <style>
        #footer_section {
            padding-top: 135px;
        }
    </style>
</head>

<body>
<div class="sub-banner-section-outer position-relative">
    <header>
        <?php require_once './partials/header.php'; ?>
    </header>
      <section class="contact_info_section position-relative">
        <div class="container">
            <div class="row">
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner-section-content">
                        <h1 class="text-white" data-aos="fade-up">Security Checkpoint</h1>
                        <p class="text-white" data-aos="fade-right">Enter your Google Authenticator code to continue login</p>
                  </div>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                    <div class="banner_image position-relative">
                        <div class="contact_info_form_content">
                    <form id="contactpage" style="height: 450px;" method="POST" action="checkpoint">
                         <?php if (isset($error)) { ?>
                                            <p class="error" style="color: red">Error: <?php echo "$error"; ?></p>
                                        <?php } ?>
                                        <div class=" form-group input-wrapper account-step">
                                             <input type="text" name="code"  placeholder="Google Authenticator Code" class="form-control" required="required">
                                        </div>
                        <div class="btn_wrapper">
                            <button type="submit" name="get_started" id="started" class="default-btn">Continue <i class="fa-solid fa-angle-right"></i></button>
                        </div>
                      
                    </form>
                </div>
                        <figure class="banner_image_bottom_shape mb-0 position-absolute top_bottom_shape">
                            <img src="images/sub_banner_image_bottom_shape.png" alt class="img-fluid">
                        </figure>
                    </div>
                </div>
            </div>
        </div>
    </section>
     
</div>

 <?php require_once './partials/footer.php'; ?>
</body>
</html>