354 lines
19 KiB
PHP
354 lines
19 KiB
PHP
<?php
|
||
require_once __DIR__ . '/includes/bootstrap.php';
|
||
require_once __DIR__ . '/includes/member_profile.php';
|
||
require_once __DIR__ . '/includes/professions.php';
|
||
require_once __DIR__ . '/includes/armory.php';
|
||
require_permission('members.view');
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||
redirect('/members.php');
|
||
}
|
||
// Zu grosse Uploads leeren $_POST komplett - dann fehlt auch das CSRF-Token.
|
||
if (empty($_POST) && (int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > 0) {
|
||
set_flash('error', 'Die Dateien waren zu groß für den Server. Bitte weniger oder kleinere Bilder auf einmal hochladen.');
|
||
redirect('/member_edit.php');
|
||
}
|
||
verify_csrf();
|
||
|
||
$action = (string) ($_POST['action'] ?? '');
|
||
$userId = (int) ($_POST['user_id'] ?? 0);
|
||
$me = current_user();
|
||
if (!profile_can_edit($userId)) {
|
||
set_flash('error', 'Du darfst nur deine eigene Profilseite bearbeiten.');
|
||
redirect('/member.php?id=' . $userId);
|
||
}
|
||
$pdo = db();
|
||
$back = '/member_edit.php?id=' . $userId;
|
||
// Aenderungen der Leitung an fremden Profilen gehoeren ins Discord-Log (Moderation).
|
||
$moderating = (int) $me['id'] !== $userId;
|
||
$modLog = static function (string $what) use ($moderating, $userId): void {
|
||
if ($moderating) {
|
||
admin_log('🧑 Profilseite eines Mitglieds bearbeitet (Leitung)', ['Mitglied' => admin_log_user($userId), 'Änderung' => $what], 'warning');
|
||
}
|
||
};
|
||
|
||
switch ($action) {
|
||
case 'bio_save':
|
||
$bio = trim(str_replace("\r\n", "\n", (string) ($_POST['profile_bio'] ?? '')));
|
||
if (mb_strlen($bio) > PROFILE_MAX_BIO) {
|
||
set_flash('error', 'Der Text ist zu lang (höchstens ' . number_format(PROFILE_MAX_BIO, 0, ',', '.') . ' Zeichen).');
|
||
redirect($back);
|
||
}
|
||
$pdo->prepare('UPDATE users SET profile_bio = ? WHERE id = ?')->execute([$bio !== '' ? $bio : null, $userId]);
|
||
$modLog('Text „Über mich“ geändert');
|
||
set_flash('success', 'Gespeichert.');
|
||
redirect($back);
|
||
|
||
case 'character_save':
|
||
$characterId = (int) ($_POST['character_id'] ?? 0);
|
||
$existing = null;
|
||
if ($characterId > 0) {
|
||
$stmt = $pdo->prepare('SELECT * FROM member_characters WHERE id = ? AND user_id = ?');
|
||
$stmt->execute([$characterId, $userId]);
|
||
$existing = $stmt->fetch();
|
||
if (!$existing) {
|
||
set_flash('error', 'Diesen Charakter gibt es nicht mehr.');
|
||
redirect($back);
|
||
}
|
||
} else {
|
||
$count = $pdo->prepare('SELECT COUNT(*) FROM member_characters WHERE user_id = ?');
|
||
$count->execute([$userId]);
|
||
if ((int) $count->fetchColumn() >= PROFILE_MAX_CHARACTERS) {
|
||
set_flash('error', 'Mehr als ' . PROFILE_MAX_CHARACTERS . ' Charaktere gehen nicht.');
|
||
redirect($back);
|
||
}
|
||
}
|
||
|
||
$name = trim((string) ($_POST['name'] ?? ''));
|
||
if ($name === '' || mb_strlen($name) > 60) {
|
||
set_flash('error', 'Bitte einen Namen mit höchstens 60 Zeichen angeben.');
|
||
redirect($back);
|
||
}
|
||
$class = in_array($_POST['class_name'] ?? '', AION_CLASSES, true) ? $_POST['class_name'] : null;
|
||
$levelInput = trim((string) ($_POST['level'] ?? ''));
|
||
$level = filter_var($levelInput, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => PROFILE_MAX_LEVEL]]) ?: null;
|
||
if ($levelInput !== '' && $level === null) {
|
||
set_flash('error', 'Die Stufe muss zwischen 1 und ' . PROFILE_MAX_LEVEL . ' liegen.');
|
||
redirect($back);
|
||
}
|
||
$note = mb_substr(trim((string) ($_POST['note'] ?? '')), 0, 255);
|
||
$isMain = !empty($_POST['is_main']) ? 1 : 0;
|
||
// Verknuepfung mit dem Charakter im Spiel (Suche im Formular, assets/js/armory.js) - leer = keine
|
||
$aionCharId = trim((string) ($_POST['aion_char_id'] ?? ''));
|
||
$aionServerId = (int) ($_POST['aion_server_id'] ?? 0);
|
||
if ($aionCharId !== '' && (!armory_valid_char_id($aionCharId) || !isset(ARMORY_SERVERS[$aionServerId]))) {
|
||
set_flash('error', 'Die Verknüpfung mit dem Charakter im Spiel ist ungültig – bitte neu suchen.');
|
||
redirect($back);
|
||
}
|
||
$aionChanged = $aionCharId !== (string) ($existing['aion_char_id'] ?? '') || ($aionCharId !== '' && $aionServerId !== (int) ($existing['aion_server_id'] ?? 0));
|
||
if ($aionCharId !== '' && $aionChanged) {
|
||
// Ein Charakter aus dem Spiel gehoert nur zu einem Profil-Charakter
|
||
$taken = $pdo->prepare(
|
||
"SELECT c.name, COALESCE(NULLIF(u.display_name, ''), u.username) AS owner FROM member_characters c JOIN users u ON u.id = c.user_id
|
||
WHERE c.aion_char_id = ? AND c.aion_server_id = ? AND c.id <> ?"
|
||
);
|
||
$taken->execute([$aionCharId, $aionServerId, $characterId]);
|
||
if ($row = $taken->fetch()) {
|
||
set_flash('error', 'Dieser Charakter ist schon bei ' . $row['owner'] . ' („' . $row['name'] . '“) verknüpft.');
|
||
redirect($back);
|
||
}
|
||
}
|
||
// Berufe und Sammel-Spezialisierung (includes/professions.php)
|
||
[$professions, $gathering, $professionErrors] = professions_from_input($_POST);
|
||
if ($professionErrors) {
|
||
set_flash('error', implode(' ', $professionErrors));
|
||
redirect($back);
|
||
}
|
||
|
||
$portrait = $existing['portrait_filename'] ?? null;
|
||
$image = $existing['image_filename'] ?? null;
|
||
// Erst beide neuen Bilder speichern; scheitert eines, das andere wieder wegraeumen,
|
||
// damit weder halbe Aenderungen noch verwaiste Dateien entstehen.
|
||
$newPortrait = null;
|
||
$newImage = null;
|
||
try {
|
||
if (($_FILES['portrait']['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE) {
|
||
$newPortrait = profile_store_upload($_FILES['portrait'], 'char' . $userId . 'p', 600);
|
||
}
|
||
if (($_FILES['image']['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE) {
|
||
$newImage = profile_store_upload($_FILES['image'], 'char' . $userId . 'i', 1600);
|
||
}
|
||
} catch (RuntimeException $e) {
|
||
profile_delete_file($newPortrait);
|
||
profile_delete_file($newImage);
|
||
set_flash('error', $e->getMessage());
|
||
redirect($back);
|
||
}
|
||
if ($newPortrait !== null || !empty($_POST['remove_portrait'])) {
|
||
profile_delete_file($portrait);
|
||
$portrait = $newPortrait;
|
||
}
|
||
if ($newImage !== null || !empty($_POST['remove_image'])) {
|
||
profile_delete_file($image);
|
||
$image = $newImage;
|
||
}
|
||
|
||
// Nur ein Hauptcharakter pro Mitglied
|
||
if ($isMain) {
|
||
$pdo->prepare('UPDATE member_characters SET is_main = 0 WHERE user_id = ?')->execute([$userId]);
|
||
}
|
||
if ($existing) {
|
||
$pdo->prepare(
|
||
'UPDATE member_characters SET name = ?, class_name = ?, level = ?, note = ?, portrait_filename = ?, image_filename = ?, is_main = ? WHERE id = ?'
|
||
)->execute([$name, $class, $level, $note !== '' ? $note : null, $portrait, $image, $isMain, $characterId]);
|
||
professions_store($characterId, $professions, $gathering);
|
||
$modLog('Charakter „' . $name . '“ bearbeitet');
|
||
set_flash('success', '„' . $name . '“ gespeichert.');
|
||
} else {
|
||
$order = $pdo->prepare('SELECT COALESCE(MAX(sort_order), 0) + 10 FROM member_characters WHERE user_id = ?');
|
||
$order->execute([$userId]);
|
||
$pdo->prepare(
|
||
'INSERT INTO member_characters (user_id, name, class_name, level, note, portrait_filename, image_filename, is_main, sort_order)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
||
)->execute([$userId, $name, $class, $level, $note !== '' ? $note : null, $portrait, $image, $isMain, (int) $order->fetchColumn()]);
|
||
$characterId = (int) $pdo->lastInsertId();
|
||
professions_store($characterId, $professions, $gathering);
|
||
$modLog('Charakter „' . $name . '“ angelegt');
|
||
set_flash('success', '„' . $name . '“ angelegt.');
|
||
}
|
||
|
||
if ($aionChanged) {
|
||
// Neue Verknuepfung: alte Live-Daten weg, gleich einmal abrufen (uebernimmt Name, Klasse und Stufe aus dem Spiel)
|
||
$pdo->prepare('UPDATE member_characters SET aion_char_id = ?, aion_server_id = ?, aion_data = NULL, aion_fetched_at = NULL, aion_error = NULL WHERE id = ?')
|
||
->execute([$aionCharId !== '' ? $aionCharId : null, $aionCharId !== '' ? $aionServerId : null, $characterId]);
|
||
foreach (armory_portrait_files([$characterId]) as $path) {
|
||
@unlink($path);
|
||
}
|
||
if ($aionCharId !== '') {
|
||
$linked = $pdo->prepare('SELECT * FROM member_characters WHERE id = ?');
|
||
$linked->execute([$characterId]);
|
||
try {
|
||
$row = armory_refresh($linked->fetch());
|
||
if (!empty($row['aion_error'])) {
|
||
set_flash('error', $row['aion_error']);
|
||
}
|
||
} catch (RuntimeException $e) {
|
||
set_flash('error', 'Verknüpft, aber die Daten aus dem Spiel ließen sich gerade nicht laden: ' . $e->getMessage() . ' Die Profilseite versucht es später erneut.');
|
||
}
|
||
}
|
||
$modLog($aionCharId !== '' ? 'Charakter „' . $name . '“ mit dem Spiel verknüpft' : 'Verknüpfung von „' . $name . '“ mit dem Spiel gelöst');
|
||
}
|
||
redirect($back);
|
||
|
||
case 'character_delete':
|
||
$stmt = $pdo->prepare('SELECT * FROM member_characters WHERE id = ? AND user_id = ?');
|
||
$stmt->execute([(int) ($_POST['character_id'] ?? 0), $userId]);
|
||
$character = $stmt->fetch();
|
||
if ($character) {
|
||
$pdo->prepare('DELETE FROM member_characters WHERE id = ?')->execute([(int) $character['id']]);
|
||
profile_delete_file($character['portrait_filename']);
|
||
profile_delete_file($character['image_filename']);
|
||
foreach (armory_portrait_files([(int) $character['id']]) as $path) {
|
||
@unlink($path);
|
||
}
|
||
$modLog('Charakter „' . $character['name'] . '“ gelöscht');
|
||
set_flash('success', '„' . $character['name'] . '“ gelöscht.');
|
||
}
|
||
redirect($back);
|
||
|
||
case 'gallery_upload':
|
||
$count = $pdo->prepare('SELECT COUNT(*), COALESCE(MAX(sort_order), 0) FROM member_gallery WHERE user_id = ? AND is_background = 0');
|
||
$count->execute([$userId]);
|
||
[$existingCount, $order] = array_map('intval', $count->fetch(PDO::FETCH_NUM));
|
||
$caption = mb_substr(trim((string) ($_POST['caption'] ?? '')), 0, 150);
|
||
|
||
// $_FILES['images'] kommt spaltenweise (name[], tmp_name[] ...) - in Einzeldateien umbauen.
|
||
$files = [];
|
||
foreach ((array) ($_FILES['images']['error'] ?? []) as $i => $error) {
|
||
$files[] = [
|
||
'error' => $error,
|
||
'size' => $_FILES['images']['size'][$i] ?? 0,
|
||
'tmp_name' => $_FILES['images']['tmp_name'][$i] ?? '',
|
||
];
|
||
}
|
||
if (moderation_blocks($caption, 'Bildunterschrift (Galerie)')) {
|
||
set_flash('error', 'Die Bildunterschrift enthält ein Wort, das hier nicht erlaubt ist.');
|
||
redirect($back);
|
||
}
|
||
$uploadCount = count(array_filter($files, static fn (array $f): bool => $f['error'] !== UPLOAD_ERR_NO_FILE));
|
||
if ($uploadCount > 0 && !security_throttle('upload', $uploadCount)) {
|
||
set_flash('error', 'Du hast in den letzten Minuten schon sehr viele Bilder hochgeladen. Bitte warte ein paar Minuten.');
|
||
redirect($back);
|
||
}
|
||
$added = 0;
|
||
$errors = [];
|
||
// Neue Mitglieder, Links in der Bildunterschrift oder "Alles manuell": erst nach Freigabe sichtbar
|
||
[$status, $moderationNote] = moderation_initial_status('picture', $caption);
|
||
$insert = $pdo->prepare('INSERT INTO member_gallery (user_id, filename, caption, status, moderation_note, sort_order) VALUES (?, ?, ?, ?, ?, ?)');
|
||
foreach ($files as $file) {
|
||
if ($file['error'] === UPLOAD_ERR_NO_FILE) {
|
||
continue;
|
||
}
|
||
if ($existingCount + $added >= PROFILE_MAX_GALLERY) {
|
||
$errors[] = 'Die Galerie ist voll (' . PROFILE_MAX_GALLERY . ' Bilder).';
|
||
break;
|
||
}
|
||
try {
|
||
$filename = profile_store_upload($file, 'gal' . $userId, 1920);
|
||
$order += 10;
|
||
$insert->execute([$userId, $filename, $caption !== '' ? $caption : null, $status, $moderationNote, $order]);
|
||
$added++;
|
||
} catch (RuntimeException $e) {
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
}
|
||
if ($added > 0) {
|
||
set_flash('success', ($added === 1 ? 'Bild hochgeladen.' : $added . ' Bilder hochgeladen.')
|
||
. ($status === 'pending' ? ' Sichtbar für alle nach einer kurzen Freigabe durch die Moderation.' : ''));
|
||
}
|
||
foreach (array_unique($errors) as $error) {
|
||
set_flash('error', $error);
|
||
}
|
||
$modLog($added . ' Bild(er) in die Galerie hochgeladen');
|
||
redirect($back);
|
||
|
||
case 'background_save':
|
||
$choice = (string) ($_POST['background'] ?? '');
|
||
$value = null;
|
||
if (preg_match('/^preset:(landscape\d+)$/', $choice, $m) && in_array($m[1], PROFILE_BACKGROUND_PRESETS, true)) {
|
||
$value = $choice;
|
||
} elseif (preg_match('/^gallery:(\d+)$/', $choice, $m)) {
|
||
$own = $pdo->prepare("SELECT COUNT(*) FROM member_gallery WHERE id = ? AND user_id = ? AND premade_id IS NULL AND (status = 'visible' OR is_background = 1)");
|
||
$own->execute([(int) $m[1], $userId]);
|
||
if ((int) $own->fetchColumn() === 0) {
|
||
set_flash('error', 'Dieses Bild kann nicht als Hintergrund dienen (nicht in deiner Galerie oder noch nicht freigegeben).');
|
||
redirect($back . '#hintergrund');
|
||
}
|
||
$value = $choice;
|
||
}
|
||
$pdo->prepare('UPDATE users SET profile_background = ? WHERE id = ?')->execute([$value, $userId]);
|
||
$modLog('Hintergrund der Profilseite geändert');
|
||
set_flash('success', $value === null ? 'Hintergrund zurückgesetzt.' : 'Hintergrund gespeichert.');
|
||
redirect($back . '#hintergrund');
|
||
|
||
case 'background_upload':
|
||
// Eigenes Bild nur fuer den Hintergrund: kommt nicht in die Galerie, geht aber durch dieselbe
|
||
// Moderation (neue Mitglieder / "alles manuell" -> erst nach Freigabe sichtbar).
|
||
$file = $_FILES['background_image'] ?? null;
|
||
if (!security_throttle('upload')) {
|
||
set_flash('error', 'Du hast in den letzten Minuten schon sehr viele Bilder hochgeladen. Bitte warte ein paar Minuten.');
|
||
redirect($back . '#hintergrund');
|
||
}
|
||
try {
|
||
$filename = profile_store_upload(is_array($file) ? $file : null, 'bg' . $userId, 2560);
|
||
} catch (RuntimeException $e) {
|
||
set_flash('error', $e->getMessage());
|
||
redirect($back . '#hintergrund');
|
||
}
|
||
[$status, $moderationNote] = moderation_initial_status('picture');
|
||
// Es gibt hoechstens ein eigenes Hintergrundbild - das alte faellt weg
|
||
profile_background_image_delete($userId);
|
||
$pdo->prepare('INSERT INTO member_gallery (user_id, filename, status, moderation_note, is_background) VALUES (?, ?, ?, ?, 1)')
|
||
->execute([$userId, $filename, $status, $moderationNote]);
|
||
$pdo->prepare('UPDATE users SET profile_background = ? WHERE id = ?')
|
||
->execute(['gallery:' . (int) $pdo->lastInsertId(), $userId]);
|
||
$modLog('Eigenes Hintergrundbild hochgeladen');
|
||
set_flash('success', $status === 'pending'
|
||
? 'Hintergrundbild hochgeladen. Es erscheint nach einer kurzen Freigabe durch die Moderation - bis dahin bleibt der Standard-Hintergrund.'
|
||
: 'Hintergrundbild hochgeladen und als Hintergrund gesetzt.');
|
||
redirect($back . '#hintergrund');
|
||
|
||
case 'background_delete':
|
||
profile_background_image_delete($userId);
|
||
$modLog('Eigenes Hintergrundbild entfernt');
|
||
set_flash('success', 'Eigenes Hintergrundbild entfernt.');
|
||
redirect($back . '#hintergrund');
|
||
|
||
case 'gallery_comments_toggle':
|
||
$enabled = !empty($_POST['enabled']) ? 1 : 0;
|
||
$pdo->prepare('UPDATE users SET gallery_comments_enabled = ? WHERE id = ?')->execute([$enabled, $userId]);
|
||
$modLog($enabled ? 'Kommentare unter den Bildern eingeschaltet' : 'Kommentare unter den Bildern abgeschaltet');
|
||
set_flash('success', $enabled ? 'Kommentare unter deinen Bildern sind eingeschaltet.' : 'Kommentare unter deinen Bildern sind abgeschaltet - vorhandene werden ausgeblendet.');
|
||
redirect($back);
|
||
|
||
case 'gallery_update':
|
||
$caption = mb_substr(trim((string) ($_POST['caption'] ?? '')), 0, 150);
|
||
if (moderation_blocks($caption, 'Bildunterschrift (Galerie)')) {
|
||
set_flash('error', 'Die Bildunterschrift enthält ein Wort, das hier nicht erlaubt ist.');
|
||
redirect($back);
|
||
}
|
||
$pdo->prepare('UPDATE member_gallery SET caption = ? WHERE id = ? AND user_id = ?')
|
||
->execute([$caption !== '' ? $caption : null, (int) ($_POST['picture_id'] ?? 0), $userId]);
|
||
// Nachtraeglich ein Link oder zurueckgehaltenes Wort in der Unterschrift? Dann erst wieder nach Freigabe sichtbar.
|
||
[$captionStatus, $captionNote] = $caption !== '' ? moderation_initial_status('caption', $caption) : ['visible', null];
|
||
$captionReason = $captionNote !== null && (moderation_has_link($caption) || str_starts_with($captionNote, 'Schimpfwortfilter'));
|
||
if ($captionStatus === 'pending' && $captionReason) {
|
||
$pdo->prepare("UPDATE member_gallery SET status = 'pending', moderation_note = ? WHERE id = ? AND user_id = ?")
|
||
->execute([$captionNote, (int) ($_POST['picture_id'] ?? 0), $userId]);
|
||
set_flash('success', 'Bildunterschrift gespeichert. Das Bild ist bis zur Freigabe durch die Moderation nur für dich sichtbar.');
|
||
redirect($back);
|
||
}
|
||
set_flash('success', 'Bildunterschrift gespeichert.');
|
||
redirect($back);
|
||
|
||
case 'gallery_delete':
|
||
$stmt = $pdo->prepare('SELECT filename FROM member_gallery WHERE id = ? AND user_id = ?');
|
||
$stmt->execute([(int) ($_POST['picture_id'] ?? 0), $userId]);
|
||
$filename = $stmt->fetchColumn();
|
||
if ($filename !== false) {
|
||
require_once __DIR__ . '/includes/reactions.php';
|
||
$commentIds = $pdo->prepare('SELECT id FROM gallery_comments WHERE gallery_id = ?');
|
||
$commentIds->execute([(int) ($_POST['picture_id'] ?? 0)]);
|
||
reactions_delete('comment', $commentIds->fetchAll(PDO::FETCH_COLUMN));
|
||
reactions_delete('picture', [(int) ($_POST['picture_id'] ?? 0)]);
|
||
$pdo->prepare('DELETE FROM member_gallery WHERE id = ?')->execute([(int) ($_POST['picture_id'] ?? 0)]);
|
||
profile_delete_file((string) $filename);
|
||
moderation_clear_reports('picture', (int) ($_POST['picture_id'] ?? 0));
|
||
$modLog('Bild aus der Galerie gelöscht');
|
||
set_flash('success', 'Bild gelöscht.');
|
||
}
|
||
redirect($back);
|
||
|
||
default:
|
||
redirect($back);
|
||
}
|