Name nur zur Orientierung, Sprungmarke als Element-Kennung (je Seite eindeutig, reservierte Kennungen gesperrt), ausgeblendete Abschnitte erscheinen nie öffentlich und zählen weder für die Hauptüberschrift noch für Trenner. SafeUrl nimmt #marke an. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms\Widgets\Support;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
/**
|
|
* Zulässige Linkziele in redaktionellen Inhalten: Seitenpfade ("/...", auch mit "#marke"), Sprungmarken auf der
|
|
* eigenen Seite ("#marke") oder http(s)-Adressen. Eine Sprungmarke besteht aus Buchstaben, Ziffern, Bindestrich und Unterstrich.
|
|
*/
|
|
final class SafeUrl implements ValidationRule
|
|
{
|
|
public static function isSafe(mixed $url): bool
|
|
{
|
|
if (! is_string($url) || $url === '' || preg_match('/[\s\x00-\x1F\x7F]/', $url) === 1) {
|
|
return false;
|
|
}
|
|
|
|
if (str_starts_with($url, '#')) {
|
|
return preg_match('/^#[A-Za-z0-9_-]{1,80}$/', $url) === 1;
|
|
}
|
|
|
|
$isRelative = str_starts_with($url, '/') && ! str_starts_with($url, '//') && ! str_starts_with($url, '/\\');
|
|
$isAbsolute = preg_match('#^https?://[^/\s]+#i', $url) === 1;
|
|
|
|
return $isRelative || $isAbsolute;
|
|
}
|
|
|
|
public static function message(): string
|
|
{
|
|
return 'Erlaubt sind Seitenpfade (beginnend mit /), Sprungmarken (#marke) oder http(s)-Adressen.';
|
|
}
|
|
|
|
/**
|
|
* Prüfregel für Formularfelder.
|
|
*/
|
|
public static function rule(): self
|
|
{
|
|
return new self;
|
|
}
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if ($value !== null && $value !== '' && ! self::isSafe($value)) {
|
|
$fail(self::message());
|
|
}
|
|
}
|
|
}
|