<?php
session_start();

require_once './include/Constants.php';
require_once "./vendor/autoload.php";
require_once "./include/DatabaseConn.php";
require_once './models/Settings.php';
require_once "./models/User.php";



if (isset($_POST['token'])) {
    $token = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    if ($token == null || $token == FALSE) {
        $error = "Invalid code";
        die($error);
    } else {
        $token = trim($token);
        $db = new DatabaseConn();
        $link = $db->connect();
        $stmt = mysqli_prepare($link, "select `user` from token where token = ?");
        $stmt->bind_param("s", $token);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 1) {
            $stmt->bind_result($user);
            $stmt->fetch();
            $stmt = mysqli_prepare($link, "update users set status = ? where email = ?");
            $newUser = NEW_USER;
            $stmt->bind_param("ss", $newUser, $user);
            $stmt->execute();
            $stmt->store_result();
            User::notifyAdmin(EMAIL_SUPPORT, $user);
            $_SESSION[USER] = $user;
            
            die("OK");
        } else {
            $error = "Invalid code";
            die($error);
        }
    }
}

$title = 'Confirm Account ' . APP_NAME;
?>