Your IP : 216.73.216.86


Current Path : /home/emeraadmin/www/4d695/
Upload File :
Current File : /home/emeraadmin/www/4d695/send_reset_link.php.tar

home/emeraadmin/public_html/pages/send_reset_link.php000064400000003556151676734200017104 0ustar00<?php

//load .env file
require_once '../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

//set base url
$base_url = $_ENV['BASE_URL'];

require_once 'emailUtils.php';
require_once '../Classes/Database.php';

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

    $db = new Database();

    // Check if email exists in the database
    $db->query('SELECT id, email FROM users WHERE email = :email');
    $db->bind(':email', $email);
    $user = $db->single();

    if ($user) {
        // Generate unique token
        $token = bin2hex(random_bytes(50));
        $expiry = date("Y-m-d H:i:s", strtotime('+1 hour'));

        // Save token and expiry in the database
        $db->query('INSERT INTO password_resets (user_id, token, expires_at) VALUES (:user_id, :token, :expires_at)');
        $db->bind(':user_id', $user->id);
        $db->bind(':token', $token);
        $db->bind(':expires_at', $expiry);
        $db->execute();

        // Prepare email details
        $resetLink = $base_url."/pages/reset-password.php?token=" . $token;
        $template = 'email_templates/password_reset_template.html';
        $placeholders = [
            "Recipient's Name" => $user->email,
            'Reset Password Link' => $resetLink,
            'expiration time, e.g., 24 hours' => '24 hours',
            'Your Name' => 'Your Name',
            'Your Position' => 'Your Position',
            'Your Company' => 'Your Company',
            'Your Contact Information' => 'Your Contact Information'
        ];

        // Call the sendEmail function
        
        $result = sendEmail($email, 'Reset Your Password', '', true, $template, $placeholders);
        echo $result;
    } else {
        echo "Email not found";
    }
} else {
    echo "Invalid request";
}
?>