'Bild-Block', 'gallery' => 'Galerie', 'hero' => 'Hero', 'text' => 'Im Text', ]; public const HTML_CONTEXT = 'Im Text'; /** * Sieht der Wert wie ein Bildpfad im Upload-Speicher aus (relativ, ohne „..“)? */ public static function isCandidate(mixed $value): bool { return is_string($value) && strlen($value) <= 255 && preg_match('#^[A-Za-z0-9_-][A-Za-z0-9/_.\-]*\.(webp|jpe?g|png|gif)$#i', $value) === 1 && ! str_contains($value, '..') && ! str_contains($value, '//'); } /** * Pfade aus src-Attributen, die auf den Upload-Speicher zeigen. * * @return list */ public static function fromHtml(?string $html): array { if (! $html || ! str_contains($html, ' preg_match_all('/\sdata-id\s*=\s*(["\'])(.*?)\1/i', $html, $ids); foreach ($ids[2] as $id) { if (self::isCandidate($id = html_entity_decode($id))) { $paths[] = $id; } } return array_values(array_unique($paths)); } /** * Pfad im Upload-Speicher zu einer Adresse („/uploads/news/a.webp?v=1“), sonst null. */ public static function fromUrl(string $url): ?string { $parts = parse_url(trim($url)); if ($parts === false || ! isset($parts['path'])) { return null; } if (isset($parts['host'])) { $ownHost = parse_url((string) Storage::disk('uploads')->url(''), PHP_URL_HOST); if ($ownHost && strcasecmp($parts['host'], $ownHost) !== 0) { return null; } } $position = strpos($parts['path'], '/uploads/'); if ($position === false) { return null; } $path = rawurldecode(substr($parts['path'], $position + strlen('/uploads/'))); return self::isCandidate($path) ? $path : null; } /** * Alle Bildpfade eines Pagebuilder-Inhalts, gruppiert nach Stelle * („Bild-Block“, „Galerie“, „Hero“, „Im Text“ oder die Blockbezeichnung). * * @param array|null $content * @return array> Stelle => Pfade */ public static function fromContent(?array $content): array { $result = []; $blocks = app(Blocks::class); $walkBlocks = function (mixed $list, int $depth) use (&$walkBlocks, &$result, $blocks): void { if (! is_array($list) || $depth > 10) { return; } foreach ($list as $block) { if (! is_array($block)) { continue; } $type = (string) ($block['type'] ?? ''); $context = self::BLOCK_CONTEXTS[$type] ?? ($blocks->get($type)?->label ?? 'Inhalt'); $data = is_array($block['data'] ?? null) ? $block['data'] : []; array_walk_recursive($data, function ($value) use (&$result, $context) { if (self::isCandidate($value)) { $result[$context][] = $value; } elseif (is_string($value) && str_contains($value, '<')) { foreach (self::fromHtml($value) as $path) { $result[self::HTML_CONTEXT][] = $path; } } }); foreach ((array) ($block['items'] ?? []) as $item) { $walkBlocks(is_array($item) ? ($item['blocks'] ?? []) : [], $depth + 1); } } }; if (is_array($content)) { $walkBlocks($content['blocks'] ?? [], 1); } return array_map(fn (array $paths) => array_values(array_unique($paths)), $result); } }