<?php
session_start();

require("../includes/db_connect.php");

$email = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    if ((!empty($_POST['email']))) {

        if ($conn) {

            try {

                $email = $_POST['email'];

                $usr_sql = "SELECT email FROM users WHERE email =? LIMIT 1";
                $usr_sth = $conn->prepare($usr_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
                $usr_sth->execute([$email]);
                $users = $usr_sth->fetchAll();

                if (count($users) > 0) {

                    $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789!$*';
                    $token = str_shuffle($token);
                    $token = substr($token, 0, 100);

                    $rstpd_sql = "SELECT * FROM password_reset WHERE email =? LIMIT 1";
                    $rstpd_sth = $conn->prepare($rstpd_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
                    $rstpd_sth->execute([$email]);
                    $password_reset = $rstpd_sth->fetchAll();

                    if (count($password_reset) > 0) {

                        $pswdrst_sql = "UPDATE password_reset SET token=?, email_confirmation=? WHERE email=?";

                        $update_pswdrst = $conn->prepare($pswdrst_sql)->execute([$token, 0, $email]);

                        if ($update_pswdrst) {

                            send_vcode();
                        } else {
                            die("An unexpected error occurred. Please try again.");
                        }
                    } else {

                        $pswdrst_sql = "INSERT INTO password_reset (email, token, email_confirmation) VALUES (?,?,?)";
                        $create_pswdrst = $conn->prepare($pswdrst_sql)->execute([$email, $token, 0]);

                        if ($create_pswdrst) {
                            send_vcode();
                        } else {
                            die("An unexpected error occurred. Please try again.");
                        }
                    }
                } else {
                    die("success");
                }
            } catch (Exception $e) {
                die('An unexpected error occurred. Connection failed');
            }
        } else {
            die('An unexpected error occurred. Connection failed.');
        }
    } else {
        die('Connection failed.');
    }
} else {
    die('Connection failed.');
}


function send_vcode()
{

    global $email, $token;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "email=$email&token=$token");
    curl_setopt($ch, CURLOPT_URL, 'https://www.rocketta.io/email/forgot_mail');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_exec($ch);
    curl_close($ch);
    die("success");
}