9622f9aeca
Six fixes from a codebase review: - Consolidate the ad hoc install.php + migrate_v2..v6.php chain into one canonical schema.sql (structure reference) plus a simplified install.php that creates all tables and seeds site_settings, the superadmin account, and the standard prayer library. The six migrate_v*.php scripts are deleted — their cumulative effect is now fully captured in schema.sql. - Delete the two root-level setup.php/novena_group.php files that existed only to redirect to their admin/ equivalents of the same name; confirmed unreferenced by any link or .htaccess rule. - Decouple includes/build_slides.php from data/prayers.php's implicit `global $opening, $mysteries, ...` contract. data/prayers.php now explicitly returns its arrays; build_slides.php loads them through a small memoized get_prayer_data() and destructures them by key. - Add CSRF protection (includes/csrf.php: csrf_token/csrf_field/csrf_verify) across every POST-handling endpoint — 10 form pages and 7 API endpoints — plus token wiring in the JS/inline scripts that call the FormData- and JSON-body API endpoints (builder.js, setup.js, and the inline scripts in admin/audio.php, admin/novena_group.php, and index.php). - Stop round-tripping the SMTP password in plaintext through the settings form: the field now renders blank with a "currently set" hint, and a blank submission leaves the stored password unchanged instead of clearing it. - Add login rate-limiting: users.failed_login_attempts / locked_until columns, is_locked_out()/record_login_failure()/record_login_success() helpers in includes/auth.php, and lockout handling in login.php (5 failed attempts locks the account for 15 minutes). README documents the one manual ALTER TABLE needed to add these columns to an existing production database. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config/db.php';
|
|
require_once __DIR__ . '/includes/csrf.php';
|
|
|
|
$site_name = get_setting('site_name', APP_NAME);
|
|
$token = trim($_GET['token'] ?? '');
|
|
$errors = [];
|
|
$success = false;
|
|
|
|
if ($token === '') {
|
|
header('Location: ' . BASE_URL . '/forgot-password');
|
|
exit;
|
|
}
|
|
|
|
$pdo = get_pdo();
|
|
$stmt = $pdo->prepare('SELECT id, username FROM users WHERE reset_token = ? AND reset_expires > NOW() LIMIT 1');
|
|
$stmt->execute([$token]);
|
|
$user = $stmt->fetch();
|
|
|
|
if (!$user) {
|
|
$token_invalid = true;
|
|
}
|
|
|
|
if (!isset($token_invalid) && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
csrf_verify();
|
|
$password = $_POST['password'] ?? '';
|
|
$password_confirm = $_POST['password_confirm'] ?? '';
|
|
|
|
if (strlen($password) < 8) {
|
|
$errors[] = 'Password must be at least 8 characters.';
|
|
}
|
|
if ($password !== $password_confirm) {
|
|
$errors[] = 'Passwords do not match.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$hash = password_hash($password, PASSWORD_BCRYPT);
|
|
$pdo->prepare('UPDATE users SET password_hash = ?, reset_token = NULL, reset_expires = NULL WHERE id = ?')
|
|
->execute([$hash, $user['id']]);
|
|
$success = true;
|
|
header('Location: ' . BASE_URL . '/login?reset=1');
|
|
exit;
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="icon" type="image/svg+xml" href="<?= BASE_URL ?>/favicon.svg">
|
|
<title>Reset Password — <?= htmlspecialchars($site_name) ?></title>
|
|
<link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/setup.css">
|
|
</head>
|
|
<body class="login-page">
|
|
<div class="login-box">
|
|
<h1>✝ <?= htmlspecialchars($site_name) ?></h1>
|
|
<h2>Reset Password</h2>
|
|
|
|
<?php if (isset($token_invalid)): ?>
|
|
<div class="alert alert-error">
|
|
This password reset link is invalid or has expired.
|
|
</div>
|
|
<div style="margin-top:16px;text-align:center">
|
|
<a href="<?= BASE_URL ?>/forgot-password" class="btn btn-primary">Request a new link</a>
|
|
</div>
|
|
<?php else: ?>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-error">
|
|
<?php foreach ($errors as $err): ?>
|
|
<div><?= htmlspecialchars($err) ?></div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" action="<?= BASE_URL ?>/reset-password?token=<?= urlencode($token) ?>">
|
|
<?= csrf_field() ?>
|
|
<div class="form-group">
|
|
<label for="password">New Password <span class="required">*</span></label>
|
|
<input type="password" id="password" name="password"
|
|
minlength="8" autocomplete="new-password" autofocus required>
|
|
<p class="help-text">At least 8 characters.</p>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password_confirm">Confirm New Password <span class="required">*</span></label>
|
|
<input type="password" id="password_confirm" name="password_confirm"
|
|
minlength="8" autocomplete="new-password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary btn-full">Set New Password</button>
|
|
</form>
|
|
|
|
<?php endif; ?>
|
|
</div>
|
|
</body>
|
|
</html>
|