vendor/shopware/platform/src/Core/Framework/Util/HtmlSanitizer.php line 56

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