File "send_otp.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/qp_setter/send_otp.php
File size: 1.56 KiB (1598 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../email/send_message.php';

function redirect($url){ header('Location: '.$url); exit; }

try {
    $token = $_GET['token'] ?? '';
    if ($token === '') { throw new Exception('Invalid request'); }

    $st = $pdo->prepare('SELECT t.*, s.email, s.first_name, s.last_name FROM qp_setter_login_tokens t JOIN staff s ON t.staff_id = s.staff_id WHERE t.token = ? AND t.used = 0 AND t.expires_at > NOW()');
    $st->execute([$token]);
    $tok = $st->fetch(PDO::FETCH_ASSOC);
    if (!$tok) { throw new Exception('Invalid or expired link'); }

    $otp = str_pad((string)random_int(0, 999999), 6, '0', STR_PAD_LEFT);
    // Invalidate previous unused OTPs for this staff to avoid confusion
    $pdo->prepare('UPDATE qp_setter_otps SET is_used = 1 WHERE staff_id = ? AND is_used = 0')->execute([$tok['staff_id']]);
    $expiresAt = date('Y-m-d H:i:s', time() + 600);
    $pdo->prepare('INSERT INTO qp_setter_otps (staff_id, otp_code, expires_at) VALUES (?,?,?)')->execute([$tok['staff_id'], $otp, $expiresAt]);

    $email = new EmailSender($pdo);
    $subject = 'Your OTP Code';
    $message = "Dear ".trim(($tok['first_name']??'').' '.($tok['last_name']??'')).",\n\nYour OTP is: \n\n".$otp."\n\nThis code expires in 10 minutes.\n\nRegards,\nCOE";
    $email->sendMessage($tok['email'], $subject, nl2br($message), null, null);

    redirect(BASE_URL . 'qp-setter-login.php?token='.urlencode($token));
} catch (Throwable $e) {
    echo 'Error: '.htmlspecialchars($e->getMessage());
}
?>


PHP File Manager