37048-vm/verify.php
2025-12-10 00:53:11 +00:00

198 lines
8.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/session_config.php';
session_start();
// Redirect if identifier is not in session
if (!isset($_SESSION['otp_identifier'])) {
header('Location: login.php');
exit;
}
$identifier_for_display = htmlspecialchars($_SESSION['otp_identifier']);
$page_title = "تایید کد یکبار مصرف";
// For debugging phone OTPs without an SMS service
$debug_otp = $_SESSION['show_otp_for_debugging'] ?? null;
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $page_title; ?> - آتیمه</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
<!-- Remixicon -->
<link href="https://cdn.jsdelivr.net/npm/remixicon@4.2.0/fonts/remixicon.css" rel="stylesheet" />
<!-- Main CSS -->
<link rel="stylesheet" href="assets/css/theme.css?v=<?= time(); ?>">
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time(); ?>">
<style>
/* Using styles from the modern login page for consistency */
body {
background-color: var(--color-dark-bg);
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
</style>
</head>
<body class="login-page-modern">
<main class="login-container">
<div class="login-form-wrapper">
<div class="login-header text-center mb-4">
<a href="index.php" class="logo-link"><h1 class="logo-title h2">آتیمه</h1></a>
<p class="tagline">فقط یک قدم دیگر...</p>
</div>
<h2 class="form-title text-center mb-4">تایید کد</h2>
<p class="text-center text-secondary mb-4">کد ۶ رقمی ارسال شده به <strong class="d-block mt-1"><?= $identifier_for_display; ?></strong> را وارد کنید.</p>
<?php if(isset($_SESSION['flash_message'])): ?>
<div class="alert alert-<?= htmlspecialchars($_SESSION['flash_message']['type']); ?> alert-dismissible fade show my-3" role="alert">
<?= htmlspecialchars($_SESSION['flash_message']['message']); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['flash_message']); ?>
<?php endif; ?>
<div id="otpDisplayContainer">
<?php if ($debug_otp): ?>
<div class="alert alert-warning">
<strong>حالت آزمایشی:</strong> سرویس پیامک فعال نیست. کد شما: <strong id="otpCode"><?= htmlspecialchars($debug_otp); ?></strong>
</div>
<?php endif; ?>
</div>
<form action="auth_handler.php?action=verify_otp" method="POST" class="needs-validation" novalidate>
<input type="hidden" name="identifier" value="<?= $identifier_for_display; ?>">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="otp_code" name="otp_code" placeholder="کد تایید" required pattern="\d{6}" maxlength="6" autocomplete="one-time-code" style="text-align: center; font-size: 1.5rem; letter-spacing: 0.5rem;">
<label for="otp_code">کد تایید</label>
<div class="invalid-feedback">
کد تایید باید یک عدد ۶ رقمی باشد.
</div>
</div>
<div class="d-grid mt-4">
<button type="submit" class="btn btn-primary btn-lg">تایید و ورود</button>
</div>
</form>
<div class="resend-container text-center mt-4">
<button id="resendBtn" class="btn btn-link text-decoration-none" disabled>ارسال مجدد کد</button>
<div id="timerContainer" class="timer-container text-secondary mt-2">
<span id="timer_message">ارسال مجدد تا <span id="timer" class="fw-bold">30</span> ثانیه دیگر</span>
</div>
</div>
<div class="auth-footer text-center mt-3">
<p><a href="login.php" class="text-secondary"><i class="ri-arrow-right-line align-middle"></i> بازگشت و اصلاح</a></p>
</div>
</div>
</main>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const resendBtn = document.getElementById('resendBtn');
const timerEl = document.getElementById('timer');
const timerContainer = document.getElementById('timerContainer');
let countdownInterval;
function startTimer() {
let countdown = 30;
resendBtn.disabled = true;
timerContainer.style.display = 'block';
timerEl.textContent = countdown;
countdownInterval = setInterval(() => {
countdown--;
if (countdown > 0) {
timerEl.textContent = countdown;
} else {
clearInterval(countdownInterval);
timerContainer.style.display = 'none';
resendBtn.disabled = false;
}
}, 1000);
}
resendBtn.addEventListener('click', function() {
clearInterval(countdownInterval);
startTimer();
const originalBtnText = resendBtn.textContent;
resendBtn.textContent = 'در حال ارسال...';
resendBtn.disabled = true;
fetch('auth_handler.php?action=resend_otp', {
method: 'GET',
headers: {
'Accept': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Resend request completed:', data);
resendBtn.textContent = originalBtnText;
if (data.success && data.otp) {
let otpDisplayContainer = document.getElementById('otpDisplayContainer');
let otpCodeEl = document.getElementById('otpCode');
if (otpCodeEl) {
// If the element exists, just update the code
otpCodeEl.textContent = data.otp;
} else {
// If the element doesn't exist, create and inject it
const newOtpDisplay = document.createElement('div');
newOtpDisplay.className = 'alert alert-warning';
newOtpDisplay.innerHTML = `<strong>حالت آزمایشی:</strong> سرویس پیامک فعال نیست. کد شما: <strong id="otpCode">${data.otp}</strong>`;
// Find a good place to insert it, e.g., before the form
const form = document.querySelector('.needs-validation');
if (form) {
otpDisplayContainer.appendChild(newOtpDisplay);
}
}
// You can add a small visual confirmation, like a flash
const otpDisplayDiv = document.querySelector('#otpDisplayContainer .alert');
if(otpDisplayDiv) {
otpDisplayDiv.style.transition = 'none';
otpDisplayDiv.style.backgroundColor = '#fff3cd'; // Bootstrap's warning yellow
setTimeout(() => {
otpDisplayDiv.style.transition = 'background-color 0.5s ease';
otpDisplayDiv.style.backgroundColor = '';
}, 100);
}
} else {
// Handle server-side errors
alert(data.message || 'خطایی در ارسال مجدد کد رخ داد.');
}
})
.catch(error => {
console.error('Error resending OTP:', error);
resendBtn.textContent = originalBtnText;
alert('یک خطای ارتباطی رخ داد. لطفاً اتصال اینترنت خود را بررسی کنید.');
});
});
// Start the timer when the page loads
startTimer();
});
</script>
</body>
</html>