'svg', 'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/webp' => 'webp', 'font/woff2' => 'woff2', ]; /** * @param array $theme * @return array */ 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, 'profile_covers', 'icons', 'stylesheet'])->all(), 'files' => $files, ]; } /** * @return array 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, 'profile_covers', 'icons', 'stylesheet'])->all(); // Titelbilder: übernommen wird das eine exportierte (die Liste zeigte auf Dateien dieser Installation) $values['profile_covers'] = []; 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]); } }