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