124 lines
6.4 KiB
PHP
124 lines
6.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'mail/MailService.php';
|
|
|
|
$project_name = $_SERVER['PROJECT_NAME'] ?? 'LPA Online';
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_POST['email'] ?? '');
|
|
|
|
if (empty($email)) {
|
|
$error = 'Please enter your email address.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error = 'Please enter a valid email address.';
|
|
} else {
|
|
$stmt = db()->prepare('SELECT id, name FROM users WHERE email = ?');
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user) {
|
|
$token = bin2hex(random_bytes(32));
|
|
$expiry = date('Y-m-d H:i:s', strtotime('+1 hour'));
|
|
|
|
$update = db()->prepare('UPDATE users SET reset_token = ?, reset_expires_at = ? WHERE id = ?');
|
|
$update->execute([$token, $expiry, $user['id']]);
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
|
|
$reset_link = "$protocol://$host/reset-password.php?token=$token";
|
|
|
|
$subject = 'Reset Your Password';
|
|
$name = htmlspecialchars($user['name'] ?? 'User');
|
|
$html = "
|
|
<div style='font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #eee; border-radius: 10px;'>
|
|
<h2 style='color: #0d6efd;'>Password Reset Request</h2>
|
|
<p>Hello $name,</p>
|
|
<p>You recently requested to reset your password for your <strong>$project_name</strong> account. Click the button below to reset it. This link is valid for 1 hour.</p>
|
|
<div style='text-align: center; margin: 30px 0;'>
|
|
<a href='$reset_link' style='background-color: #0d6efd; color: white; padding: 12px 25px; text-decoration: none; border-radius: 50px; font-weight: bold; display: inline-block;'>Reset Password</a>
|
|
</div>
|
|
<p style='font-size: 0.9em; color: #666;'>If you did not request a password reset, please ignore this email or contact support if you have concerns.</p>
|
|
<hr style='border: 0; border-top: 1px solid #eee; margin: 20px 0;'>
|
|
<p style='font-size: 0.8em; color: #999;'>© " . date('Y') . " $project_name. All rights reserved.</p>
|
|
</div>
|
|
";
|
|
$text = "Hello $name,\n\nYou recently requested to reset your password for your $project_name account. Copy and paste the link below into your browser to reset it. This link is valid for 1 hour.\n\n$reset_link\n\nIf you did not request a password reset, please ignore this email.";
|
|
|
|
$res = MailService::sendMail($email, $subject, $html, $text);
|
|
|
|
if (!empty($res['success'])) {
|
|
$success = 'If an account exists for that email, you will receive a password reset link shortly.';
|
|
} else {
|
|
$error = 'Failed to send the reset email. Please try again later.';
|
|
}
|
|
} else {
|
|
// For security, don't reveal if the email exists or not
|
|
$success = 'If an account exists for that email, you will receive a password reset link shortly.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Forgot Password — <?php echo htmlspecialchars($project_name); ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link href="assets/css/custom.css" rel="stylesheet">
|
|
</head>
|
|
<body class="bg-light d-flex flex-column min-vh-100">
|
|
<nav class="navbar navbar-expand-lg bg-white border-bottom shadow-sm">
|
|
<div class="container justify-content-center">
|
|
<a class="navbar-brand d-flex align-items-center m-0" href="/">
|
|
<img src="assets/pasted-20260228-235417-eedda424.png" alt="<?php echo htmlspecialchars($project_name); ?>" height="40">
|
|
</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container py-5 flex-grow-1 d-flex align-items-center">
|
|
<div class="row justify-content-center w-100 m-0">
|
|
<div class="col-md-5 col-lg-4">
|
|
<div class="card shadow-lg border-0 p-4 p-md-5 rounded-4">
|
|
<div class="text-center mb-4">
|
|
<h1 class="h3 fw-bold">Reset Password</h1>
|
|
<p class="text-muted small">Enter your email and we'll send you a reset link.</p>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger small py-2"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success small py-2"><?php echo htmlspecialchars($success); ?></div>
|
|
<div class="text-center mt-3">
|
|
<a href="login.php" class="btn btn-outline-primary btn-sm rounded-pill px-4">Back to Login</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="forgot-password.php" method="POST">
|
|
<div class="mb-4">
|
|
<label for="email" class="form-label small fw-bold">Email Address</label>
|
|
<input type="email" class="form-control form-control-lg" id="email" name="email" placeholder="name@company.com" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary btn-lg w-100 rounded-pill">Send Reset Link</button>
|
|
</form>
|
|
<div class="text-center mt-4 pt-2 border-top">
|
|
<p class="text-muted small mb-0">Remembered your password? <a href="login.php" class="text-primary text-decoration-none fw-semibold">Sign In</a></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="py-4 text-center text-muted mt-auto">
|
|
<div class="container">
|
|
<p class="small mb-0">© <?php echo date('Y'); ?> <?php echo htmlspecialchars($project_name); ?>. All rights reserved.</p>
|
|
</div>
|
|
</footer>
|
|
</body>
|
|
</html>
|