LuminaWebsite/admin/audit.php
Maaxxs e4f23fcde4 Audit log, abuse protection, gallery comments with moderation, safe image uploads, 404 page
- Audit log: every admin_log entry is also stored permanently in the database (admin/audit.php, audit.view)
- Rate limits per account, login throttling per IP (not per account), emergency lock after mass deletions, force logout / lock in user management
- Comments under gallery pictures (picture page, report button, owner can disable them)
- Moderation: new members, links, reported content and a "moderate everything" switch go to the approval list (moderation.php)
- Every uploaded image is re-encoded (strips appended code and EXIF data)
- Custom 404 page, also shown for pages without permission

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 21:57:34 +02:00

167 lines
8.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Audit-Log: alle Verwaltungsaktionen und Sicherheitsereignisse, dauerhaft in der Datenbank.
* Nur lesen - Eintraege lassen sich ueber die Website weder aendern noch loeschen.
*/
require_once __DIR__ . '/../includes/bootstrap.php';
require_permission('audit.view');
$query = mb_substr(trim((string) ($_GET['q'] ?? '')), 0, 100);
$area = isset(AUDIT_AREAS[$_GET['area'] ?? '']) ? (string) $_GET['area'] : '';
$tone = in_array($_GET['tone'] ?? '', ['danger', 'warning'], true) ? (string) $_GET['tone'] : '';
$actorId = (int) ($_GET['user'] ?? 0);
$from = preg_match('/^\d{4}-\d{2}-\d{2}$/', (string) ($_GET['from'] ?? '')) ? (string) $_GET['from'] : '';
$to = preg_match('/^\d{4}-\d{2}-\d{2}$/', (string) ($_GET['to'] ?? '')) ? (string) $_GET['to'] : '';
$conditions = [];
$params = [];
if ($query !== '') {
$like = '%' . addcslashes($query, '%_\\') . '%';
$conditions[] = '(a.title LIKE ? OR a.details LIKE ? OR a.actor_name LIKE ?)';
array_push($params, $like, $like, $like);
}
if ($area !== '') {
$conditions[] = 'a.area = ?';
$params[] = $area;
}
if ($tone !== '') {
$conditions[] = $tone === 'warning' ? "a.tone IN ('warning', 'danger')" : "a.tone = 'danger'";
}
if ($actorId > 0) {
$conditions[] = 'a.user_id = ?';
$params[] = $actorId;
}
if ($from !== '') {
$conditions[] = 'a.created_at >= ?';
$params[] = $from . ' 00:00:00';
}
if ($to !== '') {
$conditions[] = 'a.created_at <= ?';
$params[] = $to . ' 23:59:59';
}
$where = $conditions ? 'WHERE ' . implode(' AND ', $conditions) : '';
$countStmt = db()->prepare("SELECT COUNT(*) FROM audit_log a $where");
$countStmt->execute($params);
$total = (int) $countStmt->fetchColumn();
$perPage = 50;
$pageCount = max(1, (int) ceil($total / $perPage));
$page = min($pageCount, max(1, (int) ($_GET['page'] ?? 1)));
$stmt = db()->prepare(
"SELECT a.*, u.discord_id FROM audit_log a LEFT JOIN users u ON u.id = a.user_id
$where ORDER BY a.created_at DESC, a.id DESC LIMIT $perPage OFFSET " . (($page - 1) * $perPage)
);
$stmt->execute($params);
$entries = $stmt->fetchAll();
// Anzahl je Bereich (fuer die Chips) und der Name der gefilterten Person
$areaCounts = db()->query('SELECT area, COUNT(*) FROM audit_log GROUP BY area')->fetchAll(PDO::FETCH_KEY_PAIR);
$actorName = '';
if ($actorId > 0) {
$nameStmt = db()->prepare("SELECT actor_name FROM audit_log WHERE user_id = ? AND actor_name <> '' ORDER BY id DESC LIMIT 1");
$nameStmt->execute([$actorId]);
$actorName = (string) ($nameStmt->fetchColumn() ?: 'Konto #' . $actorId);
}
$filtered = $query !== '' || $area !== '' || $tone !== '' || $actorId > 0 || $from !== '' || $to !== '';
/** Link mit den aktuellen Filtern, einzelne Werte ueberschrieben ('' = weglassen). */
$auditUrl = static function (array $overrides = []) use ($query, $area, $tone, $actorId, $from, $to): string {
$params = array_merge(['q' => $query, 'area' => $area, 'tone' => $tone, 'user' => $actorId ?: '', 'from' => $from, 'to' => $to], $overrides);
$params = array_filter($params, static fn ($value): bool => $value !== '' && $value !== null && $value !== 0);
return '/admin/audit.php?' . ($params ? http_build_query($params) . '&' : '');
};
$pageTitle = 'Audit-Log';
$containerWide = true;
include __DIR__ . '/../includes/header.php';
?>
<div class="panel">
<h1>Audit-Log</h1>
<p class="text-muted">
Alle Verwaltungsaktionen und Sicherheitsereignisse der Website - dieselben Einträge wie im
Discord-Log, aber dauerhaft gespeichert und hier nicht lösch- oder änderbar.
</p>
<form class="list-toolbar" method="get" action="/admin/audit.php">
<?php if ($actorId > 0): ?><input type="hidden" name="user" value="<?= $actorId ?>"><?php endif; ?>
<input type="hidden" name="area" value="<?= e($area) ?>">
<input type="hidden" name="tone" value="<?= e($tone) ?>">
<div class="audit-search-row">
<label class="search-field">
<?= icon('search') ?>
<input type="search" name="q" value="<?= e($query) ?>" placeholder="Aktion, Person oder Details suchen …" aria-label="Audit-Log durchsuchen">
</label>
<label class="audit-date">von <input type="date" name="from" value="<?= e($from) ?>" onchange="this.form.submit()"></label>
<label class="audit-date">bis <input type="date" name="to" value="<?= e($to) ?>" onchange="this.form.submit()"></label>
</div>
<div class="members-chips" role="group" aria-label="Nach Bereich filtern">
<button type="submit" name="area" value="" class="members-chip<?= $area === '' ? ' active' : '' ?>">Alle</button>
<?php foreach (AUDIT_AREAS as $key => $label): ?>
<?php if (empty($areaCounts[$key])) continue; ?>
<button type="submit" name="area" value="<?= e($key) ?>" class="members-chip<?= $area === $key ? ' active' : '' ?>">
<?= e($label) ?> <span class="chip-count"><?= number_format((int) $areaCounts[$key], 0, ',', '.') ?></span>
</button>
<?php endforeach; ?>
</div>
<div class="list-toolbar-row">
<div class="list-toolbar-meta">
<span class="members-filter-label">Wichtigkeit</span>
<div class="members-segment" role="group" aria-label="Nach Wichtigkeit filtern">
<button type="submit" name="tone" value="" class="<?= $tone === '' ? 'active' : '' ?>">Alle</button>
<button type="submit" name="tone" value="warning" class="<?= $tone === 'warning' ? 'active' : '' ?>">Warnungen</button>
<button type="submit" name="tone" value="danger" class="<?= $tone === 'danger' ? 'active' : '' ?>">Kritisch</button>
</div>
<?php if ($actorId > 0): ?>
<a class="audit-filter-pill" href="<?= e($auditUrl(['user' => '']) ) ?>" title="Filter entfernen">Person: <?= e($actorName) ?> <?= icon('x') ?></a>
<?php endif; ?>
</div>
<span class="list-search-count text-muted">
<?= number_format($total, 0, ',', '.') ?> <?= $total === 1 ? 'Eintrag' : 'Einträge' ?>
<?php if ($filtered): ?> · <a href="/admin/audit.php">Filter zurücksetzen</a><?php endif; ?>
</span>
</div>
</form>
<?php if (empty($entries)): ?>
<p class="text-muted"><?= $filtered ? 'Kein Eintrag passt zu Suche und Filter.' : 'Noch keine Einträge.' ?></p>
<?php else: ?>
<ol class="audit-list">
<?php foreach ($entries as $entry): ?>
<?php $details = json_decode((string) ($entry['details'] ?? ''), true) ?: []; ?>
<li class="audit-entry audit-<?= e((string) $entry['tone']) ?>">
<div class="audit-entry-head">
<strong class="audit-title"><?= e((string) $entry['title']) ?></strong>
<span class="badge audit-area"><?= e(AUDIT_AREAS[$entry['area']] ?? $entry['area']) ?></span>
</div>
<?php if ($details): ?>
<dl class="audit-details">
<?php foreach ($details as $name => $value): ?>
<div><dt><?= e((string) $name) ?></dt><dd><?= e((string) $value) ?></dd></div>
<?php endforeach; ?>
</dl>
<?php endif; ?>
<div class="audit-meta text-muted">
<time datetime="<?= e(date('c', strtotime((string) $entry['created_at']))) ?>"><?= e(date('d.m.Y H:i:s', strtotime((string) $entry['created_at']))) ?></time>
·
<?php if ($entry['user_id'] !== null): ?>
<a href="<?= e($auditUrl(['user' => (int) $entry['user_id']])) ?>" title="Nur Einträge dieser Person"><?= e($entry['actor_name'] !== '' ? (string) $entry['actor_name'] : 'Konto #' . $entry['user_id']) ?></a>
<?php else: ?>
<?= e($entry['actor_name'] !== '' ? (string) $entry['actor_name'] . ' (Konto gelöscht)' : 'nicht angemeldet') ?>
<?php endif; ?>
<?php if (!empty($entry['ip'])): ?>
· <span title="IP-Adresse, gekürzt">IP <?= e((string) $entry['ip']) ?></span>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ol>
<div class="warehouse-pages">
<span class="text-muted">
Einträge <?= ($page - 1) * $perPage + 1 ?>–<?= min($page * $perPage, $total) ?> von <?= number_format($total, 0, ',', '.') ?>
</span>
<?php render_pagination($auditUrl(), $page, $pageCount); ?>
</div>
<?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>