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

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