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

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