ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilHtmlPurifierAbstractLibWrapper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 private const HTML_PURIFIER_DIRECTORY = '/HTMLPurifier';
28 private const NOT_SUPPORTED_TAGS = [
29 'rp',
30 'rt',
31 'rb',
32 'rtc',
33 'rbc',
34 'ruby',
35 'u',
36 'strike',
37 'param',
38 'object'
39 ];
40
41 protected HTMLPurifier $purifier;
42
46 public function __construct()
47 {
48 $this->setPurifier(
49 new HTMLPurifier($this->getPurifierConfigInstance())
50 );
51 }
52
53 final public function purify(string $html): string
54 {
55 return $this->purifier->purify($html);
56 }
57
58 final public function purifyArray(array $htmlCollection): array
59 {
60 foreach ($htmlCollection as $key => $html) {
61 if (!is_string($html)) {
62 throw new InvalidArgumentException(sprintf(
63 'The element on index %s is not of type string: %s',
64 $key,
65 print_r($html, true)
66 ));
67 }
68 }
69
70 return $this->purifier->purifyArray($htmlCollection);
71 }
72
73 abstract protected function getPurifierConfigInstance(): HTMLPurifier_Config;
74
75 final protected function setPurifier(HTMLPurifier $purifier): self
76 {
77 $this->purifier = $purifier;
78 return $this;
79 }
80
81 final protected function getPurifier(): HTMLPurifier
82 {
83 return $this->purifier;
84 }
85
86 final public static function _getCacheDirectory(): string
87 {
88 if (!is_dir(ilFileUtils::getDataDir() . self::HTML_PURIFIER_DIRECTORY)) {
89 ilFileUtils::makeDirParents(ilFileUtils::getDataDir() . self::HTML_PURIFIER_DIRECTORY);
90 }
91
93 }
94
100 final protected function removeUnsupportedElements(array $elements): array
101 {
102 $supportedElements = [];
103
104 foreach ($elements as $element) {
105 if (!in_array($element, self::NOT_SUPPORTED_TAGS)) {
106 $supportedElements[] = $element;
107 }
108 }
109
110 return $supportedElements;
111 }
112
117 final protected function makeElementListTinyMceCompliant(array $elements): array
118 {
119 // Bugfix #5945: Necessary because TinyMCE does not use the "u"
120 // html element but <span style="text-decoration: underline">E</span>
121
122 if (in_array('u', $elements) && !in_array('span', $elements)) {
123 $elements[] = 'span';
124 }
125
126 return $elements;
127 }
128}
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getDataDir()
get data directory (outside webspace)
Abstract class wrapping the HTMLPurifier instance.
purify(string $html)
Filters an HTML snippet/document to be XSS-free and standards-compliant.
removeUnsupportedElements(array $elements)
Removes all unsupported elements.
purifyArray(array $htmlCollection)
Filters an array of HTML snippets/documents to be XSS-free and standards-compliant.
__construct()
ilHtmlPurifierAbstractLibWrapper constructor.
Interface for html sanitizing functionality.