Loheide-eu-mitarbeiterplatt.../app/Support/Theme/ThemeTransfer.php
Friederich Loheide 05b4b79cb3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m6s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m23s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m40s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Titelbild im Profil, durchsuchbare Baum-Auswahl im IT-Ticket (0.20.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 05:20:19 +00:00

92 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\Theme;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
/**
* Erscheinungsbild als Datei – z. B. vom Test- auf das Produktivsystem.
* Bilder und Schrift reisen als Daten-URLs mit.
*/
class ThemeTransfer
{
public const FORMAT = 'inhaus-theme';
private const FILES = ['logo_light', 'logo_dark', 'logo_mark', 'app_icon', 'login_image', 'profile_cover', 'custom_font'];
private const MIME = [
'image/svg+xml' => 'svg', 'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/webp' => 'webp', 'font/woff2' => 'woff2',
];
/**
* @param array<string, mixed> $theme
* @return array<string, mixed>
*/
public function export(array $theme): array
{
$disk = Storage::disk('uploads');
$files = [];
foreach (self::FILES as $key) {
if ($theme[$key] && $disk->exists($theme[$key])) {
$mime = $key === 'custom_font' ? 'font/woff2' : ($disk->mimeType($theme[$key]) ?: 'application/octet-stream');
$files[$key] = 'data:'.$mime.';base64,'.base64_encode((string) $disk->get($theme[$key]));
}
}
return [
'format' => self::FORMAT,
'version' => 1,
'exported_at' => now()->toIso8601String(),
'theme' => collect($theme)->except([...self::FILES, 'icons', 'stylesheet'])->all(),
'files' => $files,
];
}
/**
* @return array<string, mixed> Werte für den Entwurf
*/
public function import(string $json): array
{
$data = json_decode($json, true);
if (! is_array($data) || ($data['format'] ?? null) !== self::FORMAT || ! is_array($data['theme'] ?? null)) {
throw new RuntimeException('Die Datei ist kein exportiertes Erscheinungsbild.');
}
$values = collect($data['theme'])->except([...self::FILES, 'icons', 'stylesheet'])->all();
foreach (self::FILES as $key) {
$values[$key] = null;
$uri = $data['files'][$key] ?? null;
if (! is_string($uri) || ! preg_match('#^data:([a-z0-9.+/-]+);base64,(.+)$#i', $uri, $m)) {
continue;
}
$extension = self::MIME[strtolower($m[1])] ?? null;
$binary = base64_decode($m[2], true);
if (! $extension || $binary === false || strlen($binary) > 8 * 1024 * 1024) {
continue;
}
// Inhalt prüfen: Bilder müssen sich lesen lassen, SVG wird von allem Ausführbaren befreit.
if ($extension === 'svg' && ($binary = SvgSanitizer::sanitize($binary)) === null) {
continue;
}
if (in_array($extension, ['png', 'jpg', 'webp'], true) && @getimagesizefromstring($binary) === false) {
continue;
}
$path = ($key === 'custom_font' ? 'theme/fonts/' : 'theme/').'import-'.Str::random(20).'.'.$extension;
Storage::disk('uploads')->put($path, $binary);
$values[$key] = $path;
}
return Theme::normalize([...Theme::DEFAULTS, ...$values]);
}
}