LuminaWebsite/includes/functions.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

417 lines
21 KiB
PHP
Raw 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
declare(strict_types=1);
// Spielbare Klassen (identisch zur Klassenauswahl des Discord-Bots), genutzt bei der
// Raid-Anmeldung und der optionalen Klassenangabe in Premades.
const AION_CLASSES = ['Templer', 'Gladiator', 'Assassine', 'Jäger', 'Magier', 'Beschwörer', 'Kleriker', 'Kantor'];
function e(?string $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}
function redirect(string $path): void
{
header('Location: ' . $path);
exit;
}
function csrf_token(): string
{
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function csrf_field(): string
{
return '<input type="hidden" name="csrf_token" value="' . e(csrf_token()) . '">';
}
function verify_csrf(): void
{
verify_csrf_token($_POST['csrf_token'] ?? '');
}
// Fuer JSON-Endpunkte (fetch()), die kein normales $_POST haben.
function verify_csrf_token($token): void
{
if (!is_string($token) || $token === '' || !hash_equals($_SESSION['csrf_token'] ?? '', $token)) {
http_response_code(400);
exit('Ungültige Anfrage (CSRF-Token fehlt oder ist abgelaufen). Bitte Seite neu laden und erneut versuchen.');
}
}
/**
* Liest einen JSON-Request-Body (fuer fetch()-Endpunkte). Bricht mit 400 ab,
* wenn der Body kein gueltiges JSON-Objekt ist.
*
* @return array<string, mixed>
*/
function read_json_body(): array
{
$raw = file_get_contents('php://input');
$data = json_decode((string) $raw, true);
if (!is_array($data)) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Ungültiger Request-Body.']);
exit;
}
return $data;
}
/**
* Beendet einen JSON-Endpunkt mit einer Erfolgs- oder Fehlerantwort.
*
* @param array<string, mixed> $extra Zusaetzliche Felder fuer die JSON-Antwort.
*/
function json_response(bool $success, string $message = '', array $extra = []): void
{
header('Content-Type: application/json');
echo json_encode(array_merge([
'status' => $success ? 'success' : 'error',
'message' => $message,
], $extra), JSON_UNESCAPED_UNICODE);
exit;
}
function set_flash(string $type, string $message): void
{
$_SESSION['flash'][] = ['type' => $type, 'message' => $message];
}
function get_flashes(): array
{
$flashes = $_SESSION['flash'] ?? [];
unset($_SESSION['flash']);
return $flashes;
}
function generate_temp_password(int $length = 12): string
{
// Ohne visuell mehrdeutige Zeichen (0/O, 1/l/I, ...), damit sich das
// Einmalpasswort leicht von Hand abtippen lässt.
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789';
$max = strlen($chars) - 1;
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $chars[random_int(0, $max)];
}
return $password;
}
// Kleine, abhängigkeitsfreie Icon-Sammlung (Strichgrafiken, angelehnt an
// Feather Icons) für die Sidebar-Navigation.
function icon(string $name): string
{
$paths = [
'home' => '<path d="M3 9.5 12 3l9 6.5V20a1 1 0 0 1-1 1h-5v-7H9v7H4a1 1 0 0 1-1-1z"/>',
'users' => '<path d="M16 19v-1.5a3.5 3.5 0 0 0-3.5-3.5h-5A3.5 3.5 0 0 0 4 17.5V19"/><circle cx="9" cy="8" r="3"/><path d="M20 19v-1.5a3.5 3.5 0 0 0-2.5-3.36"/><path d="M14.5 4.6a3 3 0 0 1 0 5.8"/>',
'shield' => '<path d="M12 21c4.5-1.8 7-5.2 7-9.5V6l-7-3-7 3v5.5c0 4.3 2.5 7.7 7 9.5z"/>',
'log-out' => '<path d="M9 20H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h3"/><path d="m14 16 4-4-4-4"/><path d="M18 12H9"/>',
'lock' => '<rect x="4" y="11" width="16" height="9" rx="2"/><path d="M7.5 11V7.5a4.5 4.5 0 0 1 9 0V11"/>',
'calendar' => '<rect x="3" y="5" width="18" height="16" rx="2"/><path d="M16 3v4"/><path d="M8 3v4"/><path d="M3 10h18"/>',
'profile' => '<circle cx="12" cy="8" r="4"/><path d="M4.5 20c0-4 3.5-6.5 7.5-6.5s7.5 2.5 7.5 6.5"/>',
'link' => '<path d="M9 17H7a5 5 0 0 1 0-10h2"/><path d="M15 7h2a5 5 0 0 1 0 10h-2"/><line x1="8" y1="12" x2="16" y2="12"/>',
'list' => '<line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="4.5" cy="6" r="1"/><circle cx="4.5" cy="12" r="1"/><circle cx="4.5" cy="18" r="1"/>',
'edit' => '<path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z"/>',
'x' => '<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>',
'move' => '<polyline points="5 9 2 12 5 15"/><polyline points="9 5 12 2 15 5"/><polyline points="15 19 12 22 9 19"/><polyline points="19 9 22 12 19 15"/><line x1="2" y1="12" x2="22" y2="12"/><line x1="12" y1="2" x2="12" y2="22"/>',
'archive' => '<polyline points="21 8 21 21 3 21 3 8"/><rect x="1" y="3" width="22" height="5"/><line x1="10" y1="12" x2="14" y2="12"/>',
'restore' => '<polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/>',
'trash' => '<polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>',
'crown' => '<path d="M4 8l4 3 4-6 4 6 4-3-2 9H6z"/><path d="M6 20h12"/>',
'menu' => '<line x1="4" y1="7" x2="20" y2="7"/><line x1="4" y1="12" x2="20" y2="12"/><line x1="4" y1="17" x2="20" y2="17"/>',
'flag' => '<path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z"/><line x1="4" y1="22" x2="4" y2="15"/>',
'map' => '<polygon points="2 7 9 4 15 7 22 4 22 17 15 20 9 17 2 20 2 7"/><line x1="9" y1="4" x2="9" y2="17"/><line x1="15" y1="7" x2="15" y2="20"/>',
'map-pin' => '<path d="M12 21s7-5.6 7-11a7 7 0 1 0-14 0c0 5.4 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/>',
'route' => '<circle cx="5" cy="18" r="2.5"/><circle cx="19" cy="6" r="2.5"/><path d="M7.5 18h5a4 4 0 0 0 0-8h-1a4 4 0 0 1 0-8h5" transform="translate(0 2)"/>',
'area' => '<polygon points="4 8 10 4 20 7 18 18 7 20"/><circle cx="4" cy="8" r="1.6"/><circle cx="10" cy="4" r="1.6"/><circle cx="20" cy="7" r="1.6"/><circle cx="18" cy="18" r="1.6"/><circle cx="7" cy="20" r="1.6"/>',
'bell' => '<path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/>',
'hash' => '<line x1="4" y1="9" x2="20" y2="9"/><line x1="4" y1="15" x2="20" y2="15"/><line x1="10" y1="3" x2="8" y2="21"/><line x1="16" y1="3" x2="14" y2="21"/>',
'at-sign' => '<circle cx="12" cy="12" r="4"/><path d="M16 8v5a3 3 0 0 0 6 0v-1a10 10 0 1 0-3.92 7.94"/>',
'chevron-down' => '<polyline points="6 9 12 15 18 9"/>',
'check' => '<polyline points="20 6 9 17 4 12"/>',
'heart' => '<path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21l7.8-7.5 1-1.1a5.5 5.5 0 0 0 0-7.8z"/>',
'bag' => '<path d="M5 8h14l-1.2 11.2a2 2 0 0 1-2 1.8H8.2a2 2 0 0 1-2-1.8z"/><path d="M9 8V6a3 3 0 0 1 6 0v2"/>',
'box' => '<path d="M21 8 12 3 3 8v8l9 5 9-5z"/><path d="M3 8l9 5 9-5"/><line x1="12" y1="13" x2="12" y2="21"/>',
'ticket' => '<path d="M3 8a2 2 0 0 0 2-2h14a2 2 0 0 0 2 2v2a2 2 0 0 0 0 4v2a2 2 0 0 0-2 2H5a2 2 0 0 0-2-2v-2a2 2 0 0 0 0-4z"/><line x1="14" y1="7" x2="14" y2="9"/><line x1="14" y1="11" x2="14" y2="13"/><line x1="14" y1="15" x2="14" y2="17"/>',
'message' => '<path d="M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.6A8 8 0 1 1 21 12z"/><line x1="8.5" y1="10.5" x2="15.5" y2="10.5"/><line x1="8.5" y1="14" x2="13" y2="14"/>',
'search' => '<circle cx="11" cy="11" r="7"/><line x1="16.5" y1="16.5" x2="21" y2="21"/>',
'grid' => '<rect x="3" y="3" width="7.5" height="7.5" rx="1.5"/><rect x="13.5" y="3" width="7.5" height="7.5" rx="1.5"/><rect x="3" y="13.5" width="7.5" height="7.5" rx="1.5"/><rect x="13.5" y="13.5" width="7.5" height="7.5" rx="1.5"/>',
'sort' => '<path d="M7 4v16"/><polyline points="3 8 7 4 11 8"/><path d="M17 20V4"/><polyline points="13 16 17 20 21 16"/>',
'book' => '<path d="M4 19.5V5a2 2 0 0 1 2-2h13v16H6.5A2.5 2.5 0 0 0 4 21.5v-2z"/><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H19"/><line x1="8" y1="7" x2="15" y2="7"/><line x1="8" y1="11" x2="13" y2="11"/>',
'code' => '<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>',
'bug' => '<rect x="8" y="6" width="8" height="14" rx="4"/><path d="M19 7l-3 2"/><path d="M5 7l3 2"/><path d="M19 19l-3-2"/><path d="M5 19l3-2"/><path d="M20 13h-4"/><path d="M4 13h4"/><path d="M10 4l1 2"/><path d="M14 4l-1 2"/>',
'shield-check' => '<path d="M12 21c4.5-1.8 7-5.2 7-9.5V6l-7-3-7 3v5.5c0 4.3 2.5 7.7 7 9.5z"/><polyline points="9 12 11.2 14.2 15.5 9.8"/>',
];
$path = $paths[$name] ?? '';
return '<svg class="icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" '
. 'stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' . $path . '</svg>';
}
// Einladung auf den Discord-Server der Legion (Startseite, Kopfzeile oeffentlicher Seiten).
// Die Vanity-Adresse des Servers laeuft im Gegensatz zu normalen Einladungen nicht ab.
const DISCORD_INVITE_CODE = 'luminaascendens';
const DISCORD_INVITE_URL = 'https://discord.gg/' . DISCORD_INVITE_CODE;
// Absolute Adresse dieser Website, z.B. fuer Link-Vorschauen (og:image).
function site_url(string $path = '/'): string
{
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
return $scheme . '://' . (string) ($_SERVER['HTTP_HOST'] ?? '') . $path;
}
// Offizielles Discord-Markenzeichen (gefuellte Flaeche statt Strichgrafik).
function discord_logo(int $size = 24): string
{
$path = 'M20.317 4.3698a19.7913 19.7913 0 0 0-4.8851-1.5152.0741.0741 0 0 0-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 0 0-.0785-.037 19.7363 19.7363 0 0 0-4.8852 1.515.0699.0699 0 0 0-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 0 0 .0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 0 0 .0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 0 0-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 0 1-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 0 1 .0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 0 1 .0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 0 1-.0066.1276 12.2986 12.2986 0 0 1-1.873.8914.0766.0766 0 0 0-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 0 0 .0842.0286c1.961-.6067 3.9495-1.522 6.0023-3.0294a.077.077 0 0 0 .0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 0 0-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z';
return '<svg class="discord-logo" width="' . $size . '" height="' . $size . '" viewBox="0 0 24 24" '
. 'fill="currentColor" aria-hidden="true"><path d="' . $path . '"/></svg>';
}
// Profilbild: selbst hochgeladenes Bild, falls vorhanden, sonst ein Platzhalter
// mit dem Anfangsbuchstaben des Anzeigenamens auf einem nutzerfest zugeordneten
// Hintergrund aus einer zur Lumina-Optik passenden Palette.
// Der Platzhalter wird zusaetzlich immer mitgerendert (nur unsichtbar), damit
// bei einem fehlenden/kaputten Bild per onerror automatisch darauf
// zurueckgefallen wird, statt ein kaputtes Bild-Icon anzuzeigen.
function avatar_html(array $user, int $size = 40): string
{
static $palette = ['#4f6f52', '#6a4c93', '#1b6ca8', '#b5533c', '#8a6d3b', '#3f6c86', '#4a5568', '#a4443a'];
$name = trim((string) ($user['display_name'] ?? '')) !== '' ? $user['display_name'] : (string) ($user['username'] ?? '');
$letter = mb_strtoupper(mb_substr(trim($name), 0, 1, 'UTF-8'), 'UTF-8');
if ($letter === '') {
$letter = '?';
}
$seed = (string) ($user['id'] ?? $name);
$color = $palette[crc32($seed) % count($palette)];
$fontSize = round($size * 0.42, 1);
$hasCustomAvatar = !empty($user['avatar_filename']);
$placeholderStyle = 'width:' . $size . 'px;height:' . $size . 'px;background:' . e($color) . ';font-size:' . $fontSize . 'px;'
. ($hasCustomAvatar ? 'display:none;' : '');
$placeholder = '<span class="avatar avatar-placeholder" style="' . $placeholderStyle . '">' . e($letter) . '</span>';
if (!$hasCustomAvatar) {
return $placeholder;
}
$url = '/assets/uploads/avatars/' . rawurlencode((string) $user['avatar_filename']);
return '<span class="avatar-wrap">'
. '<img class="avatar" src="' . e($url) . '" alt="" width="' . $size . '" height="' . $size . '" '
. 'style="width:' . $size . 'px;height:' . $size . 'px;" '
. "onerror=\"this.style.display='none';this.nextElementSibling.style.display='inline-flex';\">"
. $placeholder
. '</span>';
}
// Ordnet die (teils deutschen) Klassennamen aus dem Raid-Bot den Icon-Dateien
// unter assets/images/Classes zu. Gibt null zurück, wenn keine passende
// Datei existiert (z.B. bei "Ausstehend").
function class_icon_url(string $className): ?string
{
static $map = [
'assassin' => 'Assassin_ICO.png',
'assassine' => 'Assassin_ICO.png',
'chanter' => 'Chanter_ICO.png',
'barde' => 'Chanter_ICO.png',
'gesangsmeister' => 'Chanter_ICO.png',
'cleric' => 'Cleric_ICO.png',
'kleriker' => 'Cleric_ICO.png',
'gladiator' => 'Gladiator_ICO.png',
'ranger' => 'Ranger_ICO.png',
'waldläufer' => 'Ranger_ICO.png',
'jäger' => 'Ranger_ICO.png',
'sorcerer' => 'Sorcerer_ICO.png',
'magier' => 'Sorcerer_ICO.png',
'spiritmaster' => 'Spiritmaster_ICO.png',
'geisterbeschwörer' => 'Spiritmaster_ICO.png',
'beschwörer' => 'Spiritmaster_ICO.png',
'kantor' => 'Chanter_ICO.png',
'templar' => 'Templar_ICO.png',
'templer' => 'Templar_ICO.png',
];
$key = mb_strtolower(trim($className), 'UTF-8');
if (!isset($map[$key])) {
return null;
}
return '/assets/images/Classes/normalized/' . $map[$key];
}
/**
* Erkennt eine Aion-Klasse in einem Text (z.B. einem Discord-Rollennamen wie "⚔️ Gladiator"
* oder "Templar") und gibt den deutschen Namen aus AION_CLASSES zurueck.
*/
function class_from_text(string $text): ?string
{
static $synonyms = [
'Templer' => ['templer', 'templar'],
'Gladiator' => ['gladiator'],
'Assassine' => ['assassine', 'assassin'],
'Jäger' => ['jäger', 'jaeger', 'ranger', 'waldläufer'],
'Magier' => ['magier', 'sorcerer', 'zauberer'],
'Beschwörer' => ['beschwörer', 'geisterbeschwörer', 'spiritmaster'],
'Kleriker' => ['kleriker', 'cleric'],
'Kantor' => ['kantor', 'chanter', 'barde', 'gesangsmeister'],
];
$text = mb_strtolower($text, 'UTF-8');
foreach ($synonyms as $class => $words) {
foreach ($words as $word) {
if (preg_match('/(^|[^\p{L}])' . preg_quote($word, '/') . '($|[^\p{L}])/u', $text)) {
return $class;
}
}
}
return null;
}
/** Kleines Klassen-Icon hinter einem Namen (wie das Rollen-Icon in Discord). */
function class_badge_html(?string $className, int $size = 20): string
{
$url = $className ? class_icon_url($className) : null;
if ($url === null) {
return '';
}
return '<img class="class-badge" src="' . e($url) . '" alt="' . e($className) . '" title="' . e($className) . '"'
. ' width="' . $size . '" height="' . $size . '">';
}
/**
* Seitenzahlen fuer eine Blaetter-Leiste: immer die erste und letzte Seite, dazu zwei Seiten
* um die aktuelle herum, Luecken als null (wird als "..." angezeigt).
*
* @return array<int, int|null>
*/
function page_numbers(int $current, int $last): array
{
$pages = [];
$previous = 0;
for ($p = 1; $p <= $last; $p++) {
if ($p === 1 || $p === $last || abs($p - $current) <= 2) {
if ($previous && $p - $previous > 1) {
// Eine einzelne fehlende Seite lieber anzeigen als durch "..." ersetzen.
$pages[] = $p - $previous === 2 ? $p - 1 : null;
}
$pages[] = $p;
$previous = $p;
}
}
return $pages;
}
/**
* Blaetter-Leiste "Zurueck 1 ... 4 5 [6] 7 8 ... 20 Weiter". $baseUrl endet auf "?" oder "&",
* $anchor (z.B. "#verlauf") wird an jeden Link gehaengt.
*/
function render_pagination(string $baseUrl, int $page, int $pageCount, string $anchor = ''): void
{
if ($pageCount <= 1) {
return;
}
$link = static fn (int $number): string => e($baseUrl . 'page=' . $number . $anchor);
?>
<nav class="pagination" aria-label="Seiten">
<?php if ($page > 1): ?>
<a class="page-link" href="<?= $link($page - 1) ?>">‹ Zurück</a>
<?php else: ?>
<span class="page-link disabled">‹ Zurück</span>
<?php endif; ?>
<?php foreach (page_numbers($page, $pageCount) as $number): ?>
<?php if ($number === null): ?>
<span class="page-gap">…</span>
<?php elseif ($number === $page): ?>
<span class="page-link active" aria-current="page"><?= $number ?></span>
<?php else: ?>
<a class="page-link" href="<?= $link($number) ?>"><?= $number ?></a>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($page < $pageCount): ?>
<a class="page-link" href="<?= $link($page + 1) ?>">Weiter ›</a>
<?php else: ?>
<span class="page-link disabled">Weiter ›</span>
<?php endif; ?>
</nav>
<?php
}
/**
* URL einer Datei unter assets/ mit Versionskennung (Aenderungsdatum). Nach einem Upload
* laedt der Browser so die neue Fassung, statt eine alte aus seinem Zwischenspeicher zu nehmen.
*/
function asset_url(string $path): string
{
$file = __DIR__ . '/..' . $path;
return $path . (is_file($file) ? '?v=' . filemtime($file) : '');
}
/**
* Speichert ein hochgeladenes Bild sicher unter $target. JPG, PNG und WEBP werden immer neu
* gerechnet (GD): Dabei faellt alles weg, was kein Bild ist - an ein Bild angehaengter Code
* ("Polyglot"-Dateien) ebenso wie EXIF-Daten (z.B. GPS-Position aus Handyfotos). Zu grosse
* Bilder werden dabei auf $maxSide verkleinert. Animierte GIFs bleiben unveraendert (GD kann
* keine Animation), werden aber auf eingebetteten Code geprueft.
*
* @param int $imageType IMAGETYPE_* aus getimagesize()
* @return string|null Fehlermeldung, null bei Erfolg
*/
function store_uploaded_image(string $tmpPath, string $target, int $imageType, int $maxSide = 0): ?string
{
$raw = (string) @file_get_contents($tmpPath);
if ($raw === '') {
return 'Das Bild konnte nicht gelesen werden.';
}
$isAnimatedGif = $imageType === IMAGETYPE_GIF && substr_count($raw, "\x21\xF9\x04") > 1;
$source = !$isAnimatedGif && function_exists('imagecreatefromstring') ? @imagecreatefromstring($raw) : false;
if ($source === false) {
// Unveraendert speichern (animiertes GIF oder kein GD) - dann wenigstens auf Code pruefen.
if (preg_match('/<\?php|<\?=|<script|<html|<svg|<iframe|javascript:/i', $raw)) {
return 'Die Datei enthält neben dem Bild noch etwas anderes und wurde abgelehnt.';
}
return move_uploaded_file($tmpPath, $target) || @copy($tmpPath, $target)
? null : 'Das Bild konnte nicht gespeichert werden (Schreibrechte auf dem Server prüfen).';
}
$width = imagesx($source);
$height = imagesy($source);
$scale = $maxSide > 0 && max($width, $height) > $maxSide ? $maxSide / max($width, $height) : 1;
$newW = max(1, (int) round($width * $scale));
$newH = max(1, (int) round($height * $scale));
$image = imagecreatetruecolor($newW, $newH);
imagealphablending($image, false);
imagesavealpha($image, true);
imagecopyresampled($image, $source, 0, 0, 0, 0, $newW, $newH, $width, $height);
imagedestroy($source);
$saved = match ($imageType) {
IMAGETYPE_PNG => imagepng($image, $target, 6),
IMAGETYPE_WEBP => function_exists('imagewebp') && imagewebp($image, $target, 85),
IMAGETYPE_GIF => imagegif($image, $target),
default => imagejpeg($image, $target, 86),
};
imagedestroy($image);
return $saved ? null : 'Das Bild konnte nicht gespeichert werden (Schreibrechte auf dem Server prüfen).';
}
/**
* 404-Seite ausgeben und beenden. Auch fuer fehlende Berechtigungen (siehe require_permission()),
* damit niemand ausprobieren kann, welche Seiten es gibt.
*/
function show_not_found(): void
{
http_response_code(404);
include __DIR__ . '/not_found.php';
exit;
}