145 lines
7.1 KiB
PHP
145 lines
7.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once __DIR__ . '/m_services/MailService.php';
|
|
|
|
if (isLoggedIn()) {
|
|
redirect('index.php');
|
|
}
|
|
|
|
$error = '';
|
|
$success = '';
|
|
$step = 'request'; // 'request' or 'reset'
|
|
|
|
// Check if we are in reset mode (token in URL)
|
|
$token = $_GET['token'] ?? '';
|
|
if ($token) {
|
|
$stmt = db()->prepare("SELECT * FROM users WHERE reset_token = ? AND reset_token_expiry > NOW()");
|
|
$stmt->execute([$token]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user) {
|
|
$step = 'reset';
|
|
} else {
|
|
$error = 'رابط استعادة كلمة المرور غير صالح أو منتهي الصلاحية.';
|
|
$step = 'request';
|
|
}
|
|
}
|
|
|
|
// Handle POST requests
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['request_reset'])) {
|
|
$email = trim($_POST['email'] ?? '');
|
|
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$stmt = db()->prepare("SELECT id, full_name FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user) {
|
|
$newToken = bin2hex(random_bytes(32));
|
|
$expiry = date('Y-m-d H:i:s', strtotime('+1 hour'));
|
|
|
|
$update = db()->prepare("UPDATE users SET reset_token = ?, reset_token_expiry = ? WHERE id = ?");
|
|
$update->execute([$newToken, $expiry, $user['id']]);
|
|
|
|
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
$resetLink = "$protocol://$host/forgot_password.php?token=$newToken";
|
|
|
|
$subject = "استعادة كلمة المرور - " . ($charity_info['charity_name'] ?? 'النظام');
|
|
$html = "
|
|
<div dir='rtl' style='font-family: Arial, sans-serif; text-align: right;'>
|
|
<h3>مرحباً {$user['full_name']}</h3>
|
|
<p>لقد طلبت استعادة كلمة المرور الخاصة بك. يرجى الضغط على الرابط أدناه لإعادة تعيينها:</p>
|
|
<p><a href='$resetLink' style='background: #000; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>إعادة تعيين كلمة المرور</a></p>
|
|
<p>هذا الرابط صالح لمدة ساعة واحدة فقط.</p>
|
|
<p>إذا لم تطلب هذا، يرجى تجاهل هذه الرسالة.</p>
|
|
</div>
|
|
";
|
|
|
|
$res = MailService::sendMail($email, $subject, $html);
|
|
if ($res['success']) {
|
|
$success = 'تم إرسال رابط استعادة كلمة المرور إلى بريدك الإلكتروني.';
|
|
} else {
|
|
$error = 'فشل إرسال البريد الإلكتروني. يرجى المحاولة لاحقاً أو التواصل مع الإدارة.';
|
|
}
|
|
} else {
|
|
$error = 'البريد الإلكتروني غير مسجل لدينا.';
|
|
}
|
|
} else {
|
|
$error = 'يرجى إدخال بريد إلكتروني صحيح.';
|
|
}
|
|
} elseif (isset($_POST['reset_password'])) {
|
|
$password = $_POST['password'] ?? '';
|
|
$confirm = $_POST['confirm_password'] ?? '';
|
|
|
|
if (strlen($password) < 6) {
|
|
$error = 'كلمة المرور يجب أن تكون 6 أحرف على الأقل.';
|
|
} elseif ($password !== $confirm) {
|
|
$error = 'كلمات المرور غير متطابقة.';
|
|
} else {
|
|
$hashed = password_hash($password, PASSWORD_DEFAULT);
|
|
$update = db()->prepare("UPDATE users SET password = ?, reset_token = NULL, reset_token_expiry = NULL WHERE id = ?");
|
|
$update->execute([$hashed, $user['id']]);
|
|
$success = 'تم تغيير كلمة المرور بنجاح. يمكنك الآن <a href="login.php">تسجيل الدخول</a>.';
|
|
$step = 'completed';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="row justify-content-center align-items-center" style="min-height: 80vh;">
|
|
<div class="col-md-4 col-lg-3">
|
|
<div class="card p-4 shadow-sm border-0 text-center">
|
|
<div class="mb-4">
|
|
<?php if (!empty($charity_info['charity_logo'])): ?>
|
|
<img src="<?= htmlspecialchars($charity_info['charity_logo']) ?>" alt="Logo" class="mb-3" style="max-height: 80px;">
|
|
<?php endif; ?>
|
|
<h4 class="fw-bold">استعادة كلمة المرور</h4>
|
|
<p class="text-muted small">بريد <?= htmlspecialchars($charity_info['charity_name'] ?? 'النظام') ?></p>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?= $error ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?= $success ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($step === 'request'): ?>
|
|
<form method="POST">
|
|
<div class="mb-3 text-start">
|
|
<label class="form-label">البريد الإلكتروني</label>
|
|
<input type="email" name="email" class="form-control" placeholder="example@domain.com" required>
|
|
<div class="form-text">أدخل البريد الإلكتروني المرتبط بحسابك.</div>
|
|
</div>
|
|
<div class="d-grid mt-4">
|
|
<button type="submit" name="request_reset" class="btn btn-dark">إرسال رابط الاستعادة</button>
|
|
</div>
|
|
<div class="mt-3">
|
|
<a href="login.php" class="text-decoration-none small text-secondary">العودة لتسجيل الدخول</a>
|
|
</div>
|
|
</form>
|
|
<?php elseif ($step === 'reset'): ?>
|
|
<form method="POST">
|
|
<div class="mb-3 text-start">
|
|
<label class="form-label">كلمة المرور الجديدة</label>
|
|
<input type="password" name="password" class="form-control" required minlength="6">
|
|
</div>
|
|
<div class="mb-3 text-start">
|
|
<label class="form-label">تأكيد كلمة المرور</label>
|
|
<input type="password" name="confirm_password" class="form-control" required minlength="6">
|
|
</div>
|
|
<div class="d-grid mt-4">
|
|
<button type="submit" name="reset_password" class="btn btn-primary">تغيير كلمة المرور</button>
|
|
</div>
|
|
</form>
|
|
<?php elseif ($step === 'completed'): ?>
|
|
<div class="mt-3">
|
|
<a href="login.php" class="btn btn-outline-dark">الذهاب لصفحة الدخول</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|