Loheide-eu-mitarbeiterplatt.../app/Pagebuilder/Field.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

234 lines
8.8 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\Pagebuilder;
use App\Media\ImagePaths;
use App\Support\Icons;
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]);
}
/**
* Mehrfachauswahl (Häkchen), z. B. mehrere Kategorien.
*
* @param array<string, string>|Closure(): array<string, string> $choices
*/
public static function multiselect(string $name, string $label, array|Closure $choices): self
{
return new self($name, 'multiselect', $label, ['choices' => $choices, '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 bzw. Bild aus der Mediathek (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' => Icons::choices()]);
}
/** 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),
'multiselect' => $this->sanitizeChoices($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' => ImagePaths::isCandidate($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' => Icons::has($value) ? $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 : '';
}
/**
* @return list<string>
*/
private function sanitizeChoices(mixed $value): array
{
$choices = $this->options['choices'] instanceof Closure ? ($this->options['choices'])() : $this->options['choices'];
return array_values(array_unique(array_filter(
array_map(fn ($v) => is_scalar($v) ? (string) $v : '', is_array($value) ? $value : []),
fn (string $v) => $v !== '' && array_key_exists($v, $choices),
)));
}
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);
}
}