'png', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_GIF => 'gif', IMAGETYPE_WEBP => 'webp']; /** * Hochgeladenes Symbol speichern. * * @param array $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.'); }