Loheide-eu-mitarbeiterplatt.../app/Media/ImagePaths.php
Friederich Loheide 694ccf6ae0 Mediathek: zentrale Bildverwaltung mit Verwendungsnachweis und Bildauswahl
- Tabellen media und media_usages, Modelle Media/MediaUsage
- App\Media\MediaLibrary (store/register/replace/delete/rebuild) und
  Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl.
  künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor
- Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über
  Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen
  bestehende Bilder
- Recht media.manage, Zugang media.use für alle Redaktionsbereiche
- /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details,
  Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl),
  Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich
- MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und
  Galerie (Mehrfachauswahl), editor.pickMedia im provide-API
- Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen
  geteilte Bilder nicht mehr selbst

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:05:50 +00:00

148 lines
4.6 KiB
PHP

<?php
namespace App\Media;
use App\Pagebuilder\Blocks;
use Illuminate\Support\Facades\Storage;
/**
* Findet Bildpfade im Speicher „uploads“ in Inhalten: in Pagebuilder-JSON
* (jeder String, der wie ein Upload-Pfad aussieht) und in HTML
* (src="/uploads/…"). Grundlage für „Eingebunden in“ der Mediathek.
*/
final class ImagePaths
{
/** Bezeichnung der Stelle je Blockart. */
public const BLOCK_CONTEXTS = [
'image' => '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<string>
*/
public static function fromHtml(?string $html): array
{
if (! $html || ! str_contains($html, '<img')) {
return [];
}
preg_match_all('/\ssrc\s*=\s*(["\'])(.*?)\1/i', $html, $matches);
$paths = [];
foreach ($matches[2] as $src) {
if ($path = self::fromUrl(html_entity_decode($src))) {
$paths[] = $path;
}
}
// Anhänge des Filament-Editors: <img data-id="news/inline/…">
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<mixed>|null $content
* @return array<string, list<string>> 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);
}
}