aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/FormattingProvider/SanitisedHtml.php
blob: fa2e5ebde90e13cc3a2df7d8b0ec2cb0d39fcd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace Digitigrade\FormattingProvider;

use Digitigrade\FormattingProvider;
use Digitigrade\Singleton;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;

class SanitisedHtml extends Singleton implements FormattingProvider {
    private HtmlSanitizer $sanitiser;

    public const ALLOWED_URL_SCHEMES = [
        'http', 'https', 'ftp', 'gopher', 'gemini', 'web+pawpub', 'web+ap', 'matrix', 'xmpp', 'mailto'
    ];

    protected function __construct() {
        $this->sanitiser = new HtmlSanitizer((new HtmlSanitizerConfig())
            ->allowSafeElements()
            ->allowLinkSchemes(self::ALLOWED_URL_SCHEMES)
        );
    }

    public function renderToHtml(string $input): string {
        return $this->sanitiser->sanitize($input);
    }

    public function renderToPlainText(string $input): string {
        // TODO: somehow remove tags and leave the unformatted content behind
        return htmlspecialchars($input);
    }
}