In Berechtigungsgruppen -> Gruppe bearbeiten the new field "Symbol" takes an uploaded image or a link to one, with a checkbox to remove it and a live preview in the tag. - A link is downloaded once when saving (includes/role_icons.php): public http(s) addresses only, nothing in the local network (redirects checked one by one, the checked IP is pinned), at most 2 MB, PNG/JPG/GIF/WEBP. Like uploads the image is re-encoded, scaled to 128 px and stored under assets/uploads/roles/ (with .htaccess), so visitors never load anything from other servers and expiring links (Discord attachments) don't break the icon. - Shown in the group tags (member list, profile, group admin), the group filter chips and the rank section headings of the member list, and the group badges on the dashboard. - Replaced or removed icons and the icon of a deleted group are deleted from disk; the new file is only stored once all other fields are valid and removed again if saving fails. - New column roles.tag_icon (schema version 55, also in sql/schema.sql); README. - file-input.js: "Entfernen" fires a change event, so previews of other scripts clear too. - Live updates of the member list also react to changes of tag_bg2 and tag_icon. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
7.3 KiB
PHP
173 lines
7.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Symbole der Berechtigungsgruppen speichern: hochgeladen oder per Link. Einen Link laedt die Website
|
|
* beim Speichern einmal herunter und legt das Bild selbst ab - Besucher laden so nichts von fremden
|
|
* Servern, und ein Link, der spaeter nicht mehr geht (z.B. abgelaufene Discord-Anhaenge), macht das
|
|
* Symbol nicht kaputt. Angezeigt werden die Symbole ueber role_tag_html() in includes/permissions.php.
|
|
*/
|
|
|
|
const ROLE_ICON_MAX_BYTES = 2 * 1024 * 1024;
|
|
// Laengste Seite in Pixeln: angezeigt wird das Symbol mit gut 20 px, so bleibt es auch auf
|
|
// hochaufloesenden Bildschirmen scharf
|
|
const ROLE_ICON_MAX_SIDE = 128;
|
|
// Weiterleitungen, denen ein Link hoechstens folgt (jede wird wie der Link selbst geprueft)
|
|
const ROLE_ICON_MAX_REDIRECTS = 3;
|
|
const ROLE_ICON_TYPES = [IMAGETYPE_PNG => 'png', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_GIF => 'gif', IMAGETYPE_WEBP => 'webp'];
|
|
|
|
/**
|
|
* Hochgeladenes Symbol speichern.
|
|
*
|
|
* @param array<string, mixed> $file Eintrag aus $_FILES
|
|
* @return string Dateiname
|
|
* @throws RuntimeException mit verstaendlicher Meldung
|
|
*/
|
|
function role_icon_store_upload(array $file): string
|
|
{
|
|
$error = (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE);
|
|
if ($error !== UPLOAD_ERR_OK) {
|
|
throw new RuntimeException(in_array($error, [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true)
|
|
? 'Das Symbol ist zu groß (höchstens 2 MB).' : 'Das Symbol konnte nicht hochgeladen werden.');
|
|
}
|
|
if ((int) ($file['size'] ?? 0) > ROLE_ICON_MAX_BYTES) {
|
|
throw new RuntimeException('Das Symbol ist zu groß (höchstens 2 MB).');
|
|
}
|
|
return role_icon_store((string) $file['tmp_name']);
|
|
}
|
|
|
|
/**
|
|
* Symbol von einem Link laden und speichern.
|
|
*
|
|
* @return string Dateiname
|
|
* @throws RuntimeException mit verstaendlicher Meldung
|
|
*/
|
|
function role_icon_store_from_url(string $url): string
|
|
{
|
|
$body = role_icon_download($url);
|
|
$info = @getimagesizefromstring($body);
|
|
if ($info === false || !isset(ROLE_ICON_TYPES[$info[2]])) {
|
|
throw new RuntimeException('Hinter dem Link steckt kein Bild (PNG, JPG, GIF oder WEBP). Nimm den direkten Link zum Bild: Rechtsklick aufs Bild → „Bildadresse kopieren“.');
|
|
}
|
|
// Zwischendatei im Symbol-Ordner - der ist sicher beschreibbar, das temporaere Verzeichnis des Servers nicht immer
|
|
role_icon_ensure_dir();
|
|
$tmp = ROLE_ICON_DIR . 'download_' . bin2hex(random_bytes(8)) . '.tmp';
|
|
if (@file_put_contents($tmp, $body) === false) {
|
|
throw new RuntimeException('Das Symbol konnte nicht gespeichert werden (Schreibrechte auf dem Server prüfen).');
|
|
}
|
|
try {
|
|
return role_icon_store($tmp);
|
|
} finally {
|
|
@unlink($tmp);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bild pruefen, neu rechnen (zu grosse verkleinert, angehaengter Code und EXIF-Daten fallen weg, siehe
|
|
* store_uploaded_image) und unter einem neuen Namen ablegen.
|
|
*
|
|
* @return string Dateiname
|
|
* @throws RuntimeException mit verstaendlicher Meldung
|
|
*/
|
|
function role_icon_store(string $path): string
|
|
{
|
|
$info = @getimagesize($path);
|
|
if ($info === false || !isset(ROLE_ICON_TYPES[$info[2]])) {
|
|
throw new RuntimeException('Das Symbol muss ein Bild sein: PNG, JPG, GIF oder WEBP.');
|
|
}
|
|
role_icon_ensure_dir();
|
|
$name = 'role_' . bin2hex(random_bytes(8)) . '.' . ROLE_ICON_TYPES[$info[2]];
|
|
$error = store_uploaded_image($path, ROLE_ICON_DIR . $name, $info[2], ROLE_ICON_MAX_SIDE);
|
|
if ($error !== null) {
|
|
throw new RuntimeException($error);
|
|
}
|
|
return $name;
|
|
}
|
|
|
|
function role_icon_ensure_dir(): void
|
|
{
|
|
if (!is_dir(ROLE_ICON_DIR)) {
|
|
@mkdir(ROLE_ICON_DIR, 0755, true);
|
|
}
|
|
}
|
|
|
|
/** Symbol-Datei loeschen (ersetzt, entfernt oder mit der Gruppe geloescht). */
|
|
function role_icon_delete(?string $filename): void
|
|
{
|
|
if ($filename !== null && $filename !== '' && strpos($filename, '/') === false && is_file(ROLE_ICON_DIR . $filename)) {
|
|
@unlink(ROLE_ICON_DIR . $filename);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Laedt ein Bild von einem Link: nur http(s), hoechstens ROLE_ICON_MAX_BYTES, Weiterleitungen werden einzeln
|
|
* geprueft. Adressen im eigenen Netz (localhost, 192.168.…) sind gesperrt - sonst liesse sich der Webserver
|
|
* ueber das Formular auf Dienste ansetzen, die nur von innen erreichbar sind.
|
|
*
|
|
* @throws RuntimeException mit verstaendlicher Meldung
|
|
*/
|
|
function role_icon_download(string $url): string
|
|
{
|
|
if (!function_exists('curl_init')) {
|
|
throw new RuntimeException('Links gehen auf diesem Server nicht (die PHP-cURL-Erweiterung fehlt) - bitte das Bild hochladen.');
|
|
}
|
|
for ($redirects = 0; $redirects <= ROLE_ICON_MAX_REDIRECTS; $redirects++) {
|
|
$parts = parse_url($url);
|
|
$scheme = strtolower((string) ($parts['scheme'] ?? ''));
|
|
$host = strtolower(trim((string) ($parts['host'] ?? ''), '[]'));
|
|
if (!in_array($scheme, ['http', 'https'], true) || $host === '') {
|
|
throw new RuntimeException('Der Link zum Symbol muss mit https:// (oder http://) beginnen.');
|
|
}
|
|
$isIp = filter_var($host, FILTER_VALIDATE_IP) !== false;
|
|
$ips = $isIp ? [$host] : (gethostbynamel($host) ?: []);
|
|
if (!$ips) {
|
|
throw new RuntimeException('Den Server „' . $host . '“ aus dem Link gibt es nicht.');
|
|
}
|
|
foreach ($ips as $ip) {
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
|
|
throw new RuntimeException('Links auf Adressen im eigenen Netz sind nicht erlaubt.');
|
|
}
|
|
}
|
|
|
|
$body = '';
|
|
$options = [
|
|
CURLOPT_FOLLOWLOCATION => false,
|
|
CURLOPT_TIMEOUT => 10,
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
CURLOPT_USERAGENT => 'LuminaAscendens/1.0 (+Legion-Website, Gruppensymbol)',
|
|
CURLOPT_HTTPHEADER => ['Accept: image/webp,image/png,image/jpeg,image/gif,*/*;q=0.8'],
|
|
// Groesse schon beim Laden begrenzen: 0 zurueckgeben bricht die Uebertragung ab
|
|
CURLOPT_WRITEFUNCTION => static function ($ch, string $chunk) use (&$body): int {
|
|
$body .= $chunk;
|
|
return strlen($body) > ROLE_ICON_MAX_BYTES ? 0 : strlen($chunk);
|
|
},
|
|
];
|
|
if (!$isIp) {
|
|
// Genau die gepruefte Adresse verbinden - ein zweites Nachschlagen koennte woanders hin zeigen
|
|
$port = (int) ($parts['port'] ?? ($scheme === 'https' ? 443 : 80));
|
|
$options[CURLOPT_RESOLVE] = [$host . ':' . $port . ':' . $ips[0]];
|
|
}
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, $options);
|
|
$ok = curl_exec($ch);
|
|
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
|
$location = (string) curl_getinfo($ch, CURLINFO_REDIRECT_URL);
|
|
$curlError = curl_error($ch);
|
|
|
|
if (strlen($body) > ROLE_ICON_MAX_BYTES) {
|
|
throw new RuntimeException('Das Bild hinter dem Link ist zu groß (höchstens 2 MB).');
|
|
}
|
|
if ($ok === false) {
|
|
throw new RuntimeException('Der Link ließ sich nicht laden (' . $curlError . ').');
|
|
}
|
|
if (in_array($status, [301, 302, 303, 307, 308], true) && $location !== '') {
|
|
$url = $location;
|
|
continue;
|
|
}
|
|
if ($status !== 200) {
|
|
throw new RuntimeException('Der Link antwortet mit Status ' . $status . ' - ist das Bild öffentlich erreichbar?');
|
|
}
|
|
return $body;
|
|
}
|
|
throw new RuntimeException('Der Link leitet zu oft weiter.');
|
|
}
|