prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$email]); $user = $stmt->fetch(); if ($user) { $newToken = bin2hex(random_bytes(32)); $expires = date('Y-m-d H:i:s', strtotime('+1 hour')); $update = db()->prepare("UPDATE users SET reset_token = ?, reset_expires = ? WHERE id = ?"); $update->execute([$newToken, $expires, $user['id']]); $resetUrl = app_url('reset_password.php', ['action' => 'reset', 'token' => $newToken]); $fullResetUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . ltrim($resetUrl, '/'); $htmlBody = current_lang() === 'ar' ? "

لقد طلبت إعادة تعيين كلمة المرور. انقر على الرابط أدناه لإعادة تعيينها:

{$fullResetUrl}

ينتهي الرابط خلال ساعة واحدة.

" : "

You requested a password reset. Click the link below to reset it:

{$fullResetUrl}

Link expires in 1 hour.

"; MailService::sendMail($email, "Password Reset", $htmlBody); } $success = t('If that email is in our system, you will receive a password reset link shortly.', 'إذا كان البريد الإلكتروني مسجلاً لدينا، ستتلقى رابطاً لإعادة تعيين كلمة المرور قريباً.'); } } elseif ($action === 'reset' && $token) { $password = $_POST['password'] ?? ''; $password_confirm = $_POST['password_confirm'] ?? ''; $stmt = db()->prepare("SELECT id FROM users WHERE reset_token = ? AND reset_expires > NOW()"); $stmt->execute([$token]); $user = $stmt->fetch(); if (!$user) { $error = t('Invalid or expired token.', 'رمز غير صالح أو منتهي الصلاحية.'); } elseif ($password !== $password_confirm) { $error = t('Passwords do not match.', 'كلمتا المرور غير متطابقتين.'); } else { $hash = password_hash($password, PASSWORD_DEFAULT); $update = db()->prepare("UPDATE users SET password = ?, reset_token = NULL, reset_expires = NULL WHERE id = ?"); $update->execute([$hash, $user['id']]); $success = t('Password updated successfully. You can now log in.', 'تم تحديث كلمة المرور بنجاح. يمكنك الآن تسجيل الدخول.'); $action = 'done'; } } } render_head(t('Reset Password', 'إعادة تعيين كلمة المرور')); render_nav('login.php'); ?>