vendor/shopware/core/Framework/Util/HtmlSanitizer.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shopware\Core\Framework\Util;
  4. use Shopware\Core\Framework\Log\Package;
  5. #[Package('core')]
  6. class HtmlSanitizer
  7. {
  8. /**
  9. * @var \HTMLPurifier[]
  10. */
  11. private array $purifiers = [];
  12. private string $cacheDir;
  13. private bool $cacheEnabled;
  14. private array $sets;
  15. private array $fieldSets;
  16. private array $cache = [];
  17. /**
  18. * @internal
  19. */
  20. public function __construct(
  21. ?string $cacheDir = null,
  22. bool $cacheEnabled = true,
  23. array $sets = [],
  24. array $fieldSets = []
  25. ) {
  26. $this->cacheDir = (string) $cacheDir;
  27. $this->cacheEnabled = $cacheEnabled;
  28. $this->sets = $sets;
  29. $this->fieldSets = $fieldSets;
  30. }
  31. public function sanitize(string $text, ?array $options = [], bool $override = false, ?string $field = null): string
  32. {
  33. $options = $options ?? [];
  34. $hash = md5(sprintf('%s%s', (string) json_encode($options), (string) $field));
  35. if ($override) {
  36. $hash .= '-override';
  37. }
  38. $textKey = $hash . md5($text);
  39. if (isset($this->cache[$textKey])) {
  40. return $this->cache[$textKey];
  41. }
  42. if (!isset($this->purifiers[$hash])) {
  43. $config = $this->getConfig($options, $override, $field);
  44. $this->purifiers[$hash] = new \HTMLPurifier($config);
  45. }
  46. $this->cache[$textKey] = $this->purifiers[$hash]->purify($text);
  47. return $this->cache[$textKey];
  48. }
  49. private function getBaseConfig(): \HTMLPurifier_Config
  50. {
  51. $config = \HTMLPurifier_Config::createDefault();
  52. if ($this->cacheDir !== '') {
  53. $config->set('Cache.SerializerPath', $this->cacheDir);
  54. }
  55. if (!$this->cacheEnabled) {
  56. $config->set('Cache.DefinitionImpl', null);
  57. }
  58. $config->set('Cache.SerializerPermissions', 0775 & ~umask());
  59. return $config;
  60. }
  61. private function getConfig(array $options, bool $override, ?string $field): \HTMLPurifier_Config
  62. {
  63. $config = $this->getBaseConfig();
  64. $allowedElements = [];
  65. $allowedAttributes = [];
  66. foreach ($options as $element => $attributes) {
  67. if ($element !== '*') {
  68. $allowedElements[] = $element;
  69. }
  70. foreach ($attributes as $attr) {
  71. $allowedAttributes[] = $element === '*' ? $attr : "{$element}.{$attr}";
  72. }
  73. }
  74. if (!$override) {
  75. $sets = $this->fieldSets[$field]['sets'] ?? ['basic'];
  76. foreach ($sets as $set) {
  77. if (isset($this->sets[$set]['tags'])) {
  78. $allowedElements = array_merge($allowedElements, $this->sets[$set]['tags']);
  79. }
  80. if (isset($this->sets[$set]['attributes'])) {
  81. $allowedAttributes = array_merge($allowedAttributes, $this->sets[$set]['attributes']);
  82. }
  83. if (isset($this->sets[$set]['options'])) {
  84. foreach ($this->sets[$set]['options'] as $key => $value) {
  85. $config->set($key, $value);
  86. }
  87. }
  88. }
  89. }
  90. $config->set('HTML.AllowedElements', $allowedElements);
  91. $config->set('HTML.AllowedAttributes', $allowedAttributes);
  92. return $config;
  93. }
  94. }