<?php

include_once '../AutoLoader.php';
AutoLoader::registerDirectory('../src/classes');
require "config.php";
require "MailFiles/PHPMailerAutoload.php";
$fp = new ForgotPassword();
if (!empty($_POST)) {
    // Check if the email is recognized.
    $fp->checkEmail($_POST['email'], $db);
    // If the email was recognized, generate a new password and send an email.
    if (empty($fp->noEmail) && !empty($_POST['challenge_question_answer'])) {
        if ($fp->checkAnswer(htmlspecialchars($_POST['challenge_question_answer']))) {
            $newPassword = PasswordUtils::generateNewPassword();
            if ($fp->sendNewPassword($newPassword)) {
                $fp->success = "An email has been sent to the address that you provided. " . "Use the password included in the email to log in.";
                // Hash the new password and update the tables.
                $newSalt = PasswordUtils::generatePasswordSalt();
                $newPassword = PasswordUtils::hashPassword($newPassword, $newSalt);
                $fp->updateTables($newPassword, $newSalt, $db);
            } else {
                $fp->registrationFailure = "Verification email could not be sent. Please try again later.";
            }
        }
    }
}
?>

<!doctype html>
<html lang="en">
<head>