<?php
/**
 *  Copyright (C) 2021  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

$retrieved = $_GET['key'];

if (($retrieved != '') && (strlen($retrieved) == 50)) {

  define('ACCESSCONST', TRUE);

  require('db-connect.php');

    // Find the user who has a token identical with the one retrieved from the link that has been clicked
    $registereduser = '0';
    $enabled = 1;
    $query0 = $mysqli->prepare("SELECT id, registered, token, enabled FROM app_users WHERE registered=? AND token=? AND enabled=?");
    $query0->bind_param("ssi", $registereduser, $retrieved, $enabled);
    $query0->execute();
    $fetchdata = $query0->get_result()->fetch_assoc();

    if (!$fetchdata) {
        $query0->close();
        exit("Error !");
    } else {
        $ID = $fetchdata['id'];
        $query1 = $mysqli->query("UPDATE app_users SET registered = '1', token = ''  WHERE id = '$ID'");
        header("Location: registration-success.php");
    }
}

?>