aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/FormattingProvider/SanitisedHtml.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/FormattingProvider/SanitisedHtml.php')
-rw-r--r--Digitigrade/FormattingProvider/SanitisedHtml.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/Digitigrade/FormattingProvider/SanitisedHtml.php b/Digitigrade/FormattingProvider/SanitisedHtml.php
new file mode 100644
index 0000000..fa2e5eb
--- /dev/null
+++ b/Digitigrade/FormattingProvider/SanitisedHtml.php
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file