|null $file Eintrag aus $_FILES * @return string Dateiname * @throws RuntimeException mit verstaendlicher Meldung */ function profile_store_upload(?array $file, string $prefix, int $maxSide): string { if (!$file || ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) { throw new RuntimeException('Bitte ein Bild auswählen.'); } if ($file['error'] !== UPLOAD_ERR_OK) { throw new RuntimeException(in_array($file['error'], [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true) ? 'Das Bild ist zu groß.' : 'Upload fehlgeschlagen.'); } if ($file['size'] > PROFILE_MAX_UPLOAD) { throw new RuntimeException('Das Bild ist zu groß (höchstens 8 MB).'); } $info = @getimagesize($file['tmp_name']); $types = [IMAGETYPE_PNG => 'png', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_GIF => 'gif', IMAGETYPE_WEBP => 'webp']; if ($info === false || !isset($types[$info[2]])) { throw new RuntimeException('Nur PNG, JPG, GIF oder WEBP sind erlaubt.'); } $ext = $types[$info[2]]; $name = $prefix . '_' . bin2hex(random_bytes(8)) . '.' . $ext; $target = PROFILE_UPLOAD_DIR . $name; // Immer neu rechnen (entfernt angehaengten Code und EXIF-Daten), zu grosse Bilder verkleinern $error = store_uploaded_image($file['tmp_name'], $target, $info[2], $maxSide); if ($error !== null) { throw new RuntimeException($error); } return $name; } function profile_delete_file(?string $filename): void { if ($filename && strpos($filename, '/') === false && is_file(PROFILE_UPLOAD_DIR . $filename)) { @unlink(PROFILE_UPLOAD_DIR . $filename); } } /** * Charaktere aller (oder bestimmter) Mitglieder, gruppiert nach user_id. Hauptcharakter zuerst. * * @return array>> */ function profile_characters_by_user(?int $userId = null): array { $sql = 'SELECT * FROM member_characters' . ($userId !== null ? ' WHERE user_id = ?' : '') . ' ORDER BY is_main DESC, sort_order, id'; $stmt = db()->prepare($sql); $stmt->execute($userId !== null ? [$userId] : []); $byUser = []; foreach ($stmt->fetchAll() as $row) { $byUser[(int) $row['user_id']][] = $row; } return $byUser; } // ---- Kommentare unter Galeriebildern ---- const GALLERY_COMMENT_MAX = 1000; /** * Anzahl der Kommentare je Bild. * * @param array $galleryIds * @return array Bild-ID => Anzahl */ function gallery_comment_counts(array $galleryIds): array { if (empty($galleryIds)) { return []; } $placeholders = implode(',', array_fill(0, count($galleryIds), '?')); try { $stmt = db()->prepare("SELECT gallery_id, COUNT(*) FROM gallery_comments WHERE status = 'visible' AND gallery_id IN ($placeholders) GROUP BY gallery_id"); $stmt->execute(array_values(array_map('intval', $galleryIds))); return array_map('intval', $stmt->fetchAll(PDO::FETCH_KEY_PAIR)); } catch (PDOException $e) { return []; } } /** Darf das angemeldete Konto diesen Kommentar loeschen? (Verfasser, Bild-Besitzer, Moderation) */ function gallery_comment_can_delete(array $comment, int $pictureOwnerId): bool { $me = current_user(); if (!$me) { return false; } return (int) $comment['author_id'] === (int) $me['id'] || $pictureOwnerId === (int) $me['id'] || user_has_permission('comments.moderate'); } /** Kommentartext: nur Text mit Zeilenumbruechen und klickbaren Links, kein HTML/Markdown. */ function gallery_comment_html(string $body): string { $html = e($body); $html = (string) preg_replace_callback( '~https?://[^\s<>"\']+~i', static function (array $m): string { // Orte auf unserer Karte als "📍 Verteron – Treffpunkt" $mapLink = map_link_parse($m[0]); if ($mapLink !== null) { return map_link_html($mapLink); } return '' . $m[0] . ''; }, $html ); return nl2br($html, false); }