Asked for: a member's 4K screenshot (7.8 MB) arrived as a black picture in the gallery and as portrait - with Firefox; the same file worked for others. - assets/js/image-resize.js: the shrunken picture is checked before sending. The canvas is tested for readback first (fingerprinting protection returns placeholders), the result is sampled, and the encoded blob is decoded again. An empty, invisible or single-colour result gets a second try via WebGL, otherwise the original goes to the server, which shrinks it itself. The old code sent a 12 KB black JPEG in these cases. - Transparent pictures stay PNG (as JPEG the transparent parts turned black). A broken alpha channel (almost all transparent, but a whole picture underneath - some games save screenshots like that) is read without the alpha channel through WebGL, since a 2D canvas loses the colours at transparent spots. - includes/functions.php: store_uploaded_image() makes pictures with a broken alpha channel opaque before resampling (image_alpha_is_broken(), GD colorize with alpha -127) and, with $rejectBlank, refuses completely black or invisible pictures with a hint to try another browser (image_is_blank()); profile_store_upload() uses it for gallery, portraits, character pictures, backgrounds and premade banners. - "Das Bild ist zu groß" names the webspace limit (upload_max_filesize, php_ini_bytes()). - Patch note and README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
203 lines
7.9 KiB
PHP
203 lines
7.9 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Profilseiten der Mitglieder: Beschreibungstext, Charaktere (mit Portrait und Bild) und
|
||
* Bildergalerie. Jedes Mitglied pflegt seine eigene Seite; wer users.edit hat, darf bei
|
||
* anderen unpassende Inhalte entfernen.
|
||
*/
|
||
|
||
const PROFILE_UPLOAD_DIR = __DIR__ . '/../assets/uploads/profiles/';
|
||
const PROFILE_UPLOAD_URL = '/assets/uploads/profiles/';
|
||
const PROFILE_MAX_CHARACTERS = 12;
|
||
// Hoechststufe eines Charakters in Aion 2
|
||
const PROFILE_MAX_LEVEL = 45;
|
||
const PROFILE_MAX_GALLERY = 30;
|
||
const PROFILE_MAX_BIO = 10000;
|
||
const PROFILE_MAX_UPLOAD = 8 * 1024 * 1024;
|
||
|
||
/** Darf der aktuelle Nutzer die Profilseite von $userId bearbeiten? */
|
||
function profile_can_edit(int $userId): bool
|
||
{
|
||
$me = current_user();
|
||
return $me && ((int) $me['id'] === $userId || user_has_permission('users.edit'));
|
||
}
|
||
|
||
function profile_image_url(?string $filename): ?string
|
||
{
|
||
return $filename ? PROFILE_UPLOAD_URL . rawurlencode($filename) : null;
|
||
}
|
||
|
||
/**
|
||
* Speichert ein hochgeladenes Bild unter assets/uploads/profiles/. Der Typ wird am Inhalt
|
||
* erkannt, die Endung selbst vergeben. Ist GD vorhanden, werden grosse Bilder auf $maxSide
|
||
* Pixel (laengste Seite) verkleinert - animierte GIFs bleiben unangetastet.
|
||
*
|
||
* @param array<string, mixed>|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) {
|
||
// Grenze des Webspaces (upload_max_filesize) - greift, wenn der Browser das Bild nicht verkleinern konnte
|
||
$limit = min(PROFILE_MAX_UPLOAD, php_ini_bytes((string) ini_get('upload_max_filesize')) ?: PROFILE_MAX_UPLOAD);
|
||
throw new RuntimeException(in_array($file['error'], [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true)
|
||
? 'Das Bild ist zu groß (höchstens ' . max(1, intdiv($limit, 1024 * 1024)) . ' MB).' : '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, true);
|
||
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<int, array<int, array<string, mixed>>>
|
||
*/
|
||
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<int, int> $galleryIds
|
||
* @return array<int, int> 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 '<a href="' . $m[0] . '" target="_blank" rel="noopener nofollow ugc">' . $m[0] . '</a>';
|
||
},
|
||
$html
|
||
);
|
||
return nl2br($html, false);
|
||
}
|
||
|
||
// ---- Hintergrund der Profilseite ----
|
||
|
||
// Landschaftsbilder der Website zur Auswahl (assets/images/Landscapes/web/<name>-bg.jpg, Vorschau in thumbs/)
|
||
const PROFILE_BACKGROUND_PRESETS = [
|
||
'landscape1', 'landscape2', 'landscape3', 'landscape4', 'landscape5', 'landscape6',
|
||
'landscape7', 'landscape8', 'landscape9', 'landscape10', 'landscape11',
|
||
];
|
||
|
||
/**
|
||
* Bild-URL fuer den gewaehlten Hintergrund, null = Standard. Galeriebilder nur, solange sie dem
|
||
* Mitglied gehoeren und freigegeben sind - sonst wieder der Standard.
|
||
*/
|
||
/** Eigenes Hintergrundbild eines Mitglieds (nur fuer den Hintergrund hochgeladen), falls vorhanden. */
|
||
function profile_background_image(int $userId): ?array
|
||
{
|
||
$stmt = db()->prepare('SELECT * FROM member_gallery WHERE user_id = ? AND is_background = 1 AND premade_id IS NULL ORDER BY id DESC LIMIT 1');
|
||
$stmt->execute([$userId]);
|
||
return $stmt->fetch() ?: null;
|
||
}
|
||
|
||
/**
|
||
* Eigenes Hintergrundbild loeschen (Zeile, Datei, Meldungen). War es gewaehlt, gilt wieder der
|
||
* Standard-Hintergrund.
|
||
*/
|
||
function profile_background_image_delete(int $userId): void
|
||
{
|
||
$pdo = db();
|
||
$stmt = $pdo->prepare('SELECT id, filename FROM member_gallery WHERE user_id = ? AND is_background = 1 AND premade_id IS NULL');
|
||
$stmt->execute([$userId]);
|
||
foreach ($stmt->fetchAll() as $row) {
|
||
$pdo->prepare('DELETE FROM member_gallery WHERE id = ?')->execute([$row['id']]);
|
||
$pdo->prepare('UPDATE users SET profile_background = NULL WHERE id = ? AND profile_background = ?')
|
||
->execute([$userId, 'gallery:' . (int) $row['id']]);
|
||
moderation_clear_reports('picture', (int) $row['id']);
|
||
profile_delete_file((string) $row['filename']);
|
||
}
|
||
}
|
||
|
||
function profile_background_url(?string $value, int $userId): ?string
|
||
{
|
||
$value = (string) $value;
|
||
if (preg_match('/^preset:(landscape\d+)$/', $value, $m) && in_array($m[1], PROFILE_BACKGROUND_PRESETS, true)) {
|
||
return '/assets/images/Landscapes/web/' . $m[1] . '-bg.jpg';
|
||
}
|
||
if (preg_match('/^gallery:(\d+)$/', $value, $m)) {
|
||
$stmt = db()->prepare("SELECT filename FROM member_gallery WHERE id = ? AND user_id = ? AND status = 'visible'");
|
||
$stmt->execute([(int) $m[1], $userId]);
|
||
$filename = $stmt->fetchColumn();
|
||
return $filename !== false ? profile_image_url((string) $filename) : null;
|
||
}
|
||
return null;
|
||
}
|