Consolidate schema, add CSRF protection, and add login rate-limiting
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>
This commit is contained in:
+16
-3
@@ -5,6 +5,7 @@
|
||||
require_once __DIR__ . '/../config/db.php';
|
||||
require_once __DIR__ . '/../includes/auth.php';
|
||||
require_once __DIR__ . '/../includes/mailer.php';
|
||||
require_once __DIR__ . '/../includes/csrf.php';
|
||||
|
||||
require_role('superadmin');
|
||||
|
||||
@@ -15,16 +16,23 @@ $error = '';
|
||||
|
||||
// Save settings
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
csrf_verify();
|
||||
$action = $_POST['action'] ?? 'save';
|
||||
|
||||
if ($action === 'save') {
|
||||
$keys = ['site_name','site_url','smtp_host','smtp_port','smtp_user','smtp_pass','smtp_from','smtp_from_name',
|
||||
// smtp_pass is handled separately: the form always renders it blank
|
||||
// (see below), so a blank submission means "leave it unchanged",
|
||||
// not "clear it".
|
||||
$keys = ['site_name','site_url','smtp_host','smtp_port','smtp_user','smtp_from','smtp_from_name',
|
||||
'donate_enabled','donate_type','donate_handle','donate_label'];
|
||||
foreach ($keys as $k) {
|
||||
if (isset($_POST[$k])) {
|
||||
set_setting($k, trim($_POST[$k]));
|
||||
}
|
||||
}
|
||||
if (!empty($_POST['smtp_pass'])) {
|
||||
set_setting('smtp_pass', trim($_POST['smtp_pass']));
|
||||
}
|
||||
$message = 'Settings saved.';
|
||||
$site_name = get_setting('site_name', APP_NAME); // refresh
|
||||
}
|
||||
@@ -53,7 +61,7 @@ $settings = [
|
||||
'smtp_host' => get_setting('smtp_host'),
|
||||
'smtp_port' => get_setting('smtp_port', '587'),
|
||||
'smtp_user' => get_setting('smtp_user'),
|
||||
'smtp_pass' => get_setting('smtp_pass'),
|
||||
'smtp_pass_set' => get_setting('smtp_pass') !== '',
|
||||
'smtp_from' => get_setting('smtp_from'),
|
||||
'smtp_from_name' => get_setting('smtp_from_name', 'Rosary Presenter'),
|
||||
'donate_enabled' => get_setting('donate_enabled', '0'),
|
||||
@@ -105,6 +113,7 @@ $settings = [
|
||||
<h2 style="margin-bottom:24px">Site Settings</h2>
|
||||
|
||||
<form method="post">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="save">
|
||||
|
||||
<div class="settings-section">
|
||||
@@ -155,9 +164,12 @@ $settings = [
|
||||
<div class="pass-wrap">
|
||||
<input type="password" id="smtp_pass" name="smtp_pass"
|
||||
autocomplete="new-password"
|
||||
value="<?= htmlspecialchars($settings['smtp_pass']) ?>">
|
||||
placeholder="<?= $settings['smtp_pass_set'] ? '•••••••• (leave blank to keep current)' : '' ?>">
|
||||
<button type="button" class="pass-toggle" onclick="togglePass()">Show</button>
|
||||
</div>
|
||||
<p class="help-text">
|
||||
<?= $settings['smtp_pass_set'] ? '✓ A password is currently set. Leave blank to keep it.' : 'No password set.' ?>
|
||||
</p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="smtp_from">From Email</label>
|
||||
@@ -223,6 +235,7 @@ $settings = [
|
||||
<h3>Test Email</h3>
|
||||
<p class="help-text">Send a test email to <strong><?= htmlspecialchars($user['email']) ?></strong> to verify your SMTP settings.</p>
|
||||
<form method="post">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="test_email">
|
||||
<button type="submit" class="btn btn-secondary">Send Test Email</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user