presseportale/app/Services/PressRelease/PressReleaseHtmlSanitizer.php
Kevin Adametz bcac326814 Fix: Umbrueche bleiben beim Bearbeiten von Legacy-PMs erhalten
Bestehende (importierte) Pressemitteilungen speichern ihren Text als
Plaintext mit \n-Umbruechen. Beim Laden in den HTML-Editor (flux:editor)
kollabierten diese zu einem Fliesssatz, weil HTML Whitespace faltet.

Die Plaintext->HTML-Konvertierung (Doppelumbruch = Absatz, Einzelumbruch
= <br>) lag bisher nur im Anzeige-Pfad (render()). Sie ist jetzt als
toEditorHtml() wiederverwendbar und wird beim Laden des Editors genutzt;
die Link-Policy (rel/target) bleibt dabei dem Anzeige-Pfad vorbehalten.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 12:57:45 +00:00

120 lines
3.7 KiB
PHP

<?php
namespace App\Services\PressRelease;
use Illuminate\Support\HtmlString;
use Mews\Purifier\Facades\Purifier;
/**
* Sanitizes press-release HTML coming out of the Flux/Tiptap editor.
*
* - On save: clean() strips everything outside the press_release allowlist.
* - On display: render() returns a safe HtmlString. Legacy plain-text content
* (PMs imported before Phase 7) is detected heuristically and wrapped in
* <p>/<br> so it renders consistently next to new HTML content.
*/
class PressReleaseHtmlSanitizer
{
private const string PURIFIER_PROFILE = 'press_release';
public function __construct(private readonly PressReleaseLinkPolicy $linkPolicy) {}
/**
* Sanitize HTML before persisting to the database.
*/
public function clean(?string $html): string
{
if ($html === null || trim($html) === '') {
return '';
}
return (string) Purifier::clean($html, self::PURIFIER_PROFILE);
}
/**
* Detect whether the stored text is already HTML (Phase 7+) or
* legacy plain text from older imports.
*/
public function isHtml(?string $text): bool
{
if ($text === null || $text === '') {
return false;
}
return (bool) preg_match('/<(p|br|h2|h3|strong|em|ul|ol|li|blockquote|a)\b[^>]*>/i', $text);
}
/**
* Produce a display-ready, safe HtmlString.
*
* Die Link-Policy (rel systemseitig: extern sponsored/nofollow,
* portalintern follow) greift hier beim Rendern — so wirken
* Regel-Änderungen rückwirkend auf alle gespeicherten Inhalte.
*/
public function render(?string $text): HtmlString
{
if ($text === null || trim($text) === '') {
return new HtmlString('');
}
if ($this->isHtml($text)) {
return new HtmlString($this->linkPolicy->apply($this->clean($text)));
}
return new HtmlString($this->plainTextToParagraphs($text));
}
/**
* Liefert editierbares HTML für den Flux/Tiptap-Editor.
*
* Bereits gespeichertes HTML (Phase 7+) wird nur gesäubert. Legacy-
* Plaintext (ältere Importe) wird in <p>/<br> gewandelt — sonst kollabieren
* die Zeilenumbrüche beim Laden in den HTML-Editor zu einem Fließsatz.
* Anders als render() wird hier KEINE Link-Policy (rel/target) eingebrannt,
* da diese erst beim Anzeigen greift und nicht in den Editor gehört.
*/
public function toEditorHtml(?string $text): string
{
if ($text === null || trim($text) === '') {
return '';
}
if ($this->isHtml($text)) {
return $this->clean($text);
}
return $this->plainTextToParagraphs($text);
}
/**
* Wandelt Plaintext mit \n-Umbrüchen in <p>-Absätze (Doppelumbruch =
* neuer Absatz, Einzelumbruch = <br>) und escaped dabei HTML-Sonderzeichen.
*/
private function plainTextToParagraphs(string $text): string
{
$escaped = e($text);
$withBreaks = nl2br($escaped, false);
$paragraphs = preg_split('/(?:<br\s*\/?>\s*){2,}/i', $withBreaks) ?: [$withBreaks];
return collect($paragraphs)
->map(fn (string $chunk): string => trim($chunk))
->filter()
->map(fn (string $chunk): string => '<p>'.$chunk.'</p>')
->implode('');
}
/**
* Plain-text length for character counters (without HTML noise).
*/
public function plainTextLength(?string $text): int
{
if ($text === null || $text === '') {
return 0;
}
$stripped = strip_tags($text);
$decoded = html_entity_decode($stripped, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return mb_strlen(trim((string) preg_replace('/\s+/u', ' ', $decoded)));
}
}