ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilHtmlPurifierComposite.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
28 private array $purifiers = [];
29
35 public function addPurifier(ilHtmlPurifierInterface $purifier): bool
36 {
37 if (!in_array($purifier, $this->purifiers, true)) {
38 $this->purifiers[] = $purifier;
39 return true;
40 }
41
42 return false;
43 }
44
50 public function removePurifier(ilHtmlPurifierInterface $purifier): bool
51 {
52 $key = array_search($purifier, $this->purifiers, true);
53 if (false === $key) {
54 return false;
55 }
56 unset($this->purifiers[$key]);
57
58 return true;
59 }
60
61 public function purify(string $html): string
62 {
63 foreach ($this->purifiers as $purifier) {
64 $html = $purifier->purify($html);
65 }
66
67 return $html;
68 }
69
70 public function purifyArray(array $htmlCollection): array
71 {
72 foreach ($htmlCollection as $key => $html) {
73 if (!is_string($html)) {
74 throw new InvalidArgumentException(sprintf(
75 'The element on index %s is not of type string: %s',
76 $key,
77 print_r($html, true)
78 ));
79 }
80 }
81
82 foreach ($htmlCollection as $key => $html) {
83 foreach ($this->purifiers as $purifier) {
84 $html = $purifier->purify($html);
85 }
86
87 $htmlCollection[$key] = $html;
88 }
89
90 return $htmlCollection;
91 }
92}
Composite for nesting multiple purifiers.
removePurifier(ilHtmlPurifierInterface $purifier)
Removes a node from composite.
purifyArray(array $htmlCollection)
Filters an array of HTML snippets/documents to be XSS-free and standards-compliant.
addPurifier(ilHtmlPurifierInterface $purifier)
Adds a node to composite.
purify(string $html)
Filters an HTML snippet/document to be XSS-free and standards-compliant.
Interface for html sanitizing functionality.