prepare("INSERT INTO otp_codes (email, code_hash, expires_at) VALUES (?, ?, ?)");
$stmt->execute([$email, $code_hash, $expires_at]);
// Send the plain code to the user's email
$subject = "کد ورود شما به فروشگاه آتیمه";
$body = "
کد تایید شما
برای ورود یا ثبتنام در وبسایت آتیمه، از کد زیر استفاده کنید:
{$otp_code}
این کد تا ۱۰ دقیقه دیگر معتبر است.
";
$mail_result = MailService::sendMail($email, $subject, $body);
if (!$mail_result['success']) {
error_log('OTP Mail Error: ' . ($mail_result['error'] ?? 'Unknown error'));
flash_message('danger', 'خطایی در ارسال ایمیل رخ داد. لطفاً مطمئن شوید ایمیل را درست وارد کردهاید.', 'login.php');
}
// Store email in session to use on the verification page
$_SESSION['otp_email'] = $email;
header('Location: verify.php');
exit;
} catch (Exception $e) {
error_log('OTP Generation Error: ' . $e->getMessage());
flash_message('danger', 'خطای سرور. لطفاً لحظاتی دیگر دوباره تلاش کنید.', 'login.php');
}
}
function handle_verify_otp() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: login.php');
exit;
}
$email = filter_var(trim($_POST['email'] ?? ''), FILTER_VALIDATE_EMAIL);
$otp_code = trim($_POST['otp_code'] ?? '');
if (!$email || !$otp_code) {
flash_message('danger', 'ایمیل یا کد تایید نامعتبر است.', 'login.php');
}
try {
$pdo = db();
// Find the latest, unused OTP for this email that has not expired
$stmt = $pdo->prepare("SELECT * FROM otp_codes WHERE email = ? AND is_used = 0 AND expires_at > NOW() ORDER BY created_at DESC LIMIT 1");
$stmt->execute([$email]);
$otp_row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($otp_row && password_verify($otp_code, $otp_row['code_hash'])) {
// Mark OTP as used
$stmt_update = $pdo->prepare("UPDATE otp_codes SET is_used = 1 WHERE id = ?");
$stmt_update->execute([$otp_row['id']]);
// Check if user exists
$stmt_user = $pdo->prepare("SELECT * FROM users WHERE email = ?");
$stmt_user->execute([$email]);
$user = $stmt_user->fetch(PDO::FETCH_ASSOC);
$user_id = null;
if ($user) {
// User exists, log them in
$user_id = $user['id'];
$_SESSION['user_name'] = $user['first_name']; // Might be null, that's ok
} else {
// User does not exist, create a new one
$stmt_create = $pdo->prepare("INSERT INTO users (email, is_admin) VALUES (?, 0)");
$stmt_create->execute([$email]);
$user_id = $pdo->lastInsertId();
$_SESSION['user_name'] = null; // New user has no name yet
}
// Set session variables for login
$_SESSION['user_id'] = $user_id;
unset($_SESSION['otp_email']); // Clean up session
// Redirect to homepage with success
flash_message('success', 'شما با موفقیت وارد شدید!', 'index.php');
} else {
// Invalid or expired OTP
flash_message('danger', 'کد وارد شده اشتباه یا منقضی شده است.', 'verify.php');
}
} catch (Exception $e) {
error_log('OTP Verification Error: ' . $e->getMessage());
flash_message('danger', 'خطای سرور. لطفاً لحظاتی دیگر دوباره تلاش کنید.', 'verify.php');
}
}
function handle_logout() {
session_unset();
session_destroy();
header('Location: index.php');
exit;
}
function flash_message($type, $message, $location) {
// Ensure email is carried over to verify page on error
if ($location === 'verify.php' && isset($_POST['email'])) {
$_SESSION['otp_email'] = $_POST['email'];
}
$_SESSION['flash_message'] = ['type' => $type, 'message' => $message];
header("Location: $location");
exit;
}