98 lines
3.7 KiB
PHP
98 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use DOMDocument;
|
|
use DOMElement;
|
|
use DOMNode;
|
|
use League\CommonMark\CommonMarkConverter;
|
|
|
|
final class Html
|
|
{
|
|
private const TAGS = [
|
|
'a' => ['href'], 'article' => [], 'b' => [], 'blockquote' => [], 'br' => [],
|
|
'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'],
|
|
'dl' => [], 'dt' => [], 'em' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'hr' => [], 'i' => [],
|
|
'img' => ['src', 'alt', 'loading', 'width', 'height'], 'label' => ['for'],
|
|
'li' => [], 'ol' => [], 'p' => [], 'pre' => [], 'progress' => ['max', 'value'], 's' => [],
|
|
'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [],
|
|
'table' => [], 'tbody' => [], 'td' => ['colspan', 'rowspan'], 'tfoot' => [],
|
|
'th' => ['scope', 'colspan', 'rowspan'], 'thead' => [], 'tr' => [], 'ul' => [],
|
|
'video' => ['controls', 'muted', 'playsinline', 'poster', 'preload'],
|
|
];
|
|
|
|
public static function url(string $value, bool $media = false): bool
|
|
{
|
|
if ($value === '' || preg_match('/[\x00-\x20\x7f\\\\]/', $value)) {
|
|
return false;
|
|
}
|
|
if (str_starts_with($value, '/')) {
|
|
return !str_starts_with($value, '//');
|
|
}
|
|
if (str_starts_with($value, '#')) {
|
|
return !$media && strlen($value) > 1;
|
|
}
|
|
$scheme = strtolower((string) parse_url($value, PHP_URL_SCHEME));
|
|
|
|
return in_array($scheme, $media ? ['https'] : ['https', 'http', 'mailto', 'tel'], true);
|
|
}
|
|
|
|
public static function markdown(string $text): string
|
|
{
|
|
static $converter;
|
|
$converter ??= new CommonMarkConverter(['html_input' => 'strip', 'allow_unsafe_links' => false, 'max_nesting_level' => 30]);
|
|
|
|
$html = $converter->convert($text)->getContent();
|
|
|
|
return str_replace(['<h1>', '</h1>'], ['<h2>', '</h2>'], $html);
|
|
}
|
|
|
|
public static function sanitize(string $html): string
|
|
{
|
|
$document = new DOMDocument('1.0', 'UTF-8');
|
|
$document->loadHTML('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $html . '</body></html>', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
|
$body = $document->getElementsByTagName('body')->item(0);
|
|
if (!$body) {
|
|
return '';
|
|
}
|
|
self::sanitizeChildren($body);
|
|
$output = '';
|
|
foreach ($body->childNodes as $child) {
|
|
$output .= $document->saveHTML($child);
|
|
}
|
|
|
|
return trim($output);
|
|
}
|
|
|
|
private static function sanitizeChildren(DOMNode $parent): void
|
|
{
|
|
foreach (iterator_to_array($parent->childNodes) as $element) {
|
|
if ($element->nodeType === XML_COMMENT_NODE) {
|
|
$parent->removeChild($element);
|
|
continue;
|
|
}
|
|
if (!$element instanceof DOMElement) {
|
|
continue;
|
|
}
|
|
if (!array_key_exists($element->tagName, self::TAGS)) {
|
|
$parent->removeChild($element);
|
|
continue;
|
|
}
|
|
$allowed = [...self::TAGS[$element->tagName], 'class', 'id'];
|
|
foreach (iterator_to_array($element->attributes) as $attribute) {
|
|
$name = $attribute->name;
|
|
$value = $attribute->value;
|
|
if (!in_array($name, $allowed, true)
|
|
|| ($name === 'href' && !self::url($value))
|
|
|| (in_array($name, ['src', 'poster'], true) && !self::url($value, true))
|
|
|| ($name === 'tabindex' && $value !== '0')) {
|
|
$element->removeAttribute($name);
|
|
}
|
|
}
|
|
self::sanitizeChildren($element);
|
|
}
|
|
}
|
|
}
|