Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten - Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt, Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat, Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie, Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten, Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern - Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf, Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre, Freigabe anfordern - Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst - Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen - Bereich „Redaktion“, Benachrichtigungen im Portal - storage/.migrations-fingerprint nicht mehr ausliefern Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
209 lines
8 KiB
PHP
209 lines
8 KiB
PHP
<?php
|
||
|
||
namespace App\Pagebuilder;
|
||
|
||
use App\Support\QuickLinks;
|
||
use App\Support\RichText;
|
||
use Closure;
|
||
|
||
/**
|
||
* Ein Eingabefeld eines Blocks. Beschreibt zugleich das Formular im Editor
|
||
* (toArray) und die Bereinigung beim Speichern (sanitize) – Plugin-Blöcke
|
||
* bekommen so Editor und Absicherung ohne eigenen Code.
|
||
*/
|
||
final class Field
|
||
{
|
||
/**
|
||
* @param array<string, mixed> $options
|
||
*/
|
||
private function __construct(
|
||
public readonly string $name,
|
||
public readonly string $type,
|
||
public readonly string $label,
|
||
private readonly array $options = [],
|
||
) {}
|
||
|
||
public static function text(string $name, string $label, int $max = 200, string $default = '', ?string $placeholder = null): self
|
||
{
|
||
return new self($name, 'text', $label, ['max' => $max, 'default' => $default, 'placeholder' => $placeholder]);
|
||
}
|
||
|
||
public static function textarea(string $name, string $label, int $max = 2000, string $default = ''): self
|
||
{
|
||
return new self($name, 'textarea', $label, ['max' => $max, 'default' => $default]);
|
||
}
|
||
|
||
/** Formatierter Text – im Editor direkt im Block bearbeitet (TipTap). */
|
||
public static function html(string $name, string $label): self
|
||
{
|
||
return new self($name, 'html', $label, ['default' => '']);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, string>|Closure(): array<string, string> $choices
|
||
*/
|
||
public static function select(string $name, string $label, array|Closure $choices, ?string $default = null): self
|
||
{
|
||
return new self($name, 'select', $label, ['choices' => $choices, 'default' => $default]);
|
||
}
|
||
|
||
public static function toggle(string $name, string $label, bool $default = false): self
|
||
{
|
||
return new self($name, 'toggle', $label, ['default' => $default]);
|
||
}
|
||
|
||
public static function number(string $name, string $label, int $min, int $max, int $default): self
|
||
{
|
||
return new self($name, 'number', $label, ['min' => $min, 'max' => $max, 'default' => $default]);
|
||
}
|
||
|
||
public static function url(string $name, string $label, string $default = ''): self
|
||
{
|
||
return new self($name, 'url', $label, ['default' => $default]);
|
||
}
|
||
|
||
/** Hochgeladenes Bild (Pfad im Upload-Speicher). */
|
||
public static function image(string $name, string $label): self
|
||
{
|
||
return new self($name, 'image', $label, ['default' => null]);
|
||
}
|
||
|
||
/** Personen aus dem Verzeichnis (IDs). */
|
||
public static function people(string $name, string $label, int $max = 24): self
|
||
{
|
||
return new self($name, 'people', $label, ['max' => $max, 'default' => []]);
|
||
}
|
||
|
||
/** Einfache Tabelle: Zeilen aus Textzellen. */
|
||
public static function table(string $name, string $label): self
|
||
{
|
||
return new self($name, 'table', $label, ['default' => [['', ''], ['', '']]]);
|
||
}
|
||
|
||
/** Symbol aus dem Symbolsatz der Schnellzugriffe. */
|
||
public static function icon(string $name, string $label, string $default = 'link'): self
|
||
{
|
||
return new self($name, 'icon', $label, ['default' => $default, 'choices' => QuickLinks::ICONS]);
|
||
}
|
||
|
||
/** Liste gleichartiger Einträge, z. B. Schaltflächen oder Galeriebilder. */
|
||
public static function repeater(string $name, string $label, array $fields, int $max = 20, string $itemLabel = 'Eintrag'): self
|
||
{
|
||
return new self($name, 'repeater', $label, ['fields' => $fields, 'max' => $max, 'default' => [], 'itemLabel' => $itemLabel]);
|
||
}
|
||
|
||
/** Beliebige Liste von IDs, deren Einträge der Block selbst verwaltet (z. B. Dateien). */
|
||
public static function ids(string $name, string $label, string $kind, int $max = 50): self
|
||
{
|
||
return new self($name, 'ids', $label, ['kind' => $kind, 'max' => $max, 'default' => []]);
|
||
}
|
||
|
||
public function default(): mixed
|
||
{
|
||
if ($this->type === 'repeater') {
|
||
return [];
|
||
}
|
||
|
||
return $this->options['default'] ?? null;
|
||
}
|
||
|
||
public function help(string $help): self
|
||
{
|
||
return new self($this->name, $this->type, $this->label, [...$this->options, 'help' => $help]);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed> Beschreibung für das Editor-Formular
|
||
*/
|
||
public function toArray(): array
|
||
{
|
||
$options = $this->options;
|
||
|
||
if (isset($options['choices'])) {
|
||
$choices = $options['choices'] instanceof Closure ? ($options['choices'])() : $options['choices'];
|
||
$options['choices'] = collect($choices)->map(fn ($label, $value) => ['value' => (string) $value, 'label' => $label])->values()->all();
|
||
}
|
||
|
||
if (isset($options['fields'])) {
|
||
$options['fields'] = array_map(fn (Field $field) => $field->toArray(), $options['fields']);
|
||
}
|
||
|
||
return ['name' => $this->name, 'type' => $this->type, 'label' => $this->label, ...$options];
|
||
}
|
||
|
||
public function sanitize(mixed $value): mixed
|
||
{
|
||
return match ($this->type) {
|
||
'text', 'textarea' => mb_substr(trim(is_scalar($value) ? (string) $value : ''), 0, $this->options['max']),
|
||
'html' => app(RichText::class)->sanitize(is_string($value) ? mb_substr($value, 0, 200_000) : ''),
|
||
'select' => $this->sanitizeChoice($value),
|
||
'toggle' => (bool) $value,
|
||
'number' => max($this->options['min'], min($this->options['max'], is_numeric($value) ? (int) $value : $this->options['default'])),
|
||
'url' => self::safeUrl($value),
|
||
'image' => is_string($value) && preg_match('#^pagebuilder/[A-Za-z0-9/_.-]+$#', $value) && ! str_contains($value, '..') ? $value : null,
|
||
'people', 'ids' => array_values(array_slice(array_unique(array_filter(array_map('intval', is_array($value) ? $value : []))), 0, $this->options['max'])),
|
||
'table' => $this->sanitizeTable($value),
|
||
'icon' => is_string($value) && array_key_exists($value, QuickLinks::ICONS) ? $value : $this->options['default'],
|
||
'repeater' => $this->sanitizeRepeater($value),
|
||
default => null,
|
||
};
|
||
}
|
||
|
||
public static function safeUrl(mixed $value): string
|
||
{
|
||
$url = trim(is_string($value) ? $value : '');
|
||
|
||
if ($url === '' || mb_strlen($url) > 2000) {
|
||
return '';
|
||
}
|
||
|
||
// Relative Adressen im Portal, Anker oder http(s)/mailto/tel – nichts anderes (kein javascript:).
|
||
if (preg_match('#^(/(?!/)|\#)#', $url) || preg_match('#^(https?://|mailto:|tel:)#i', $url)) {
|
||
return $url;
|
||
}
|
||
|
||
return preg_match('#^[a-z0-9.-]+\.[a-z]{2,}(/|$)#i', $url) ? 'https://'.$url : '';
|
||
}
|
||
|
||
private function sanitizeChoice(mixed $value): ?string
|
||
{
|
||
$choices = $this->options['choices'] instanceof Closure ? ($this->options['choices'])() : $this->options['choices'];
|
||
$value = is_scalar($value) ? (string) $value : null;
|
||
|
||
return $value !== null && array_key_exists($value, $choices) ? $value : $this->options['default'];
|
||
}
|
||
|
||
/**
|
||
* @return list<list<string>>
|
||
*/
|
||
private function sanitizeTable(mixed $value): array
|
||
{
|
||
$rows = array_slice(is_array($value) ? array_values($value) : [], 0, 100);
|
||
$width = min(10, max(1, ...array_map(fn ($row) => is_array($row) ? count($row) : 1, $rows ?: [[]])));
|
||
|
||
return array_map(function ($row) use ($width) {
|
||
$cells = array_slice(is_array($row) ? array_values($row) : [], 0, $width);
|
||
|
||
return array_pad(array_map(fn ($cell) => mb_substr(is_scalar($cell) ? (string) $cell : '', 0, 1000), $cells), $width, '');
|
||
}, $rows);
|
||
}
|
||
|
||
/**
|
||
* @return list<array<string, mixed>>
|
||
*/
|
||
private function sanitizeRepeater(mixed $value): array
|
||
{
|
||
$items = array_slice(is_array($value) ? array_values($value) : [], 0, $this->options['max']);
|
||
|
||
return array_map(function ($item) {
|
||
$item = is_array($item) ? $item : [];
|
||
$clean = [];
|
||
|
||
foreach ($this->options['fields'] as $field) {
|
||
$clean[$field->name] = $field->sanitize($item[$field->name] ?? $field->default());
|
||
}
|
||
|
||
return $clean;
|
||
}, $items);
|
||
}
|
||
}
|