[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/MediaEmbed/ -> Configurator.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\MediaEmbed;
   9  use InvalidArgumentException;
  10  use RuntimeException;
  11  use s9e\TextFormatter\Configurator\Items\Regexp;
  12  use s9e\TextFormatter\Configurator\Items\Tag;
  13  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  14  use s9e\TextFormatter\Plugins\ConfiguratorBase;
  15  use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections\CachedDefinitionCollection;
  16  use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\TemplateBuilder;
  17  class Configurator extends ConfiguratorBase
  18  {
  19      public $allowedFilters = ['stripslashes', 'urldecode'];
  20      protected $createMediaBBCode = \true;
  21      public $defaultSites;
  22      protected $quickMatch = '://';
  23      protected $regexp = '/\\bhttps?:\\/\\/[^["\'\\s]+/Si';
  24      protected $sites = [];
  25      protected $tagName = 'MEDIA';
  26      protected $templateBuilder;
  27  	protected function setUp()
  28      {
  29          $this->defaultSites    = new CachedDefinitionCollection;
  30          $this->templateBuilder = new TemplateBuilder;
  31          $this->configurator->registeredVars['MediaEmbed.hosts'] = new Dictionary;
  32          $this->configurator->registeredVars['MediaEmbed.sites'] = new Dictionary;
  33          $this->createMediaTag();
  34          if ($this->createMediaBBCode)
  35              $this->configurator->BBCodes->set($this->tagName, ['contentAttributes' => ['url']]);
  36      }
  37  	public function asConfig()
  38      {
  39          if (empty($this->sites))
  40              return;
  41          return [
  42              'quickMatch' => $this->quickMatch,
  43              'regexp'     => $this->regexp,
  44              'tagName'    => $this->tagName
  45          ];
  46      }
  47  	public function add($siteId, array $siteConfig = \null)
  48      {
  49          $siteId = $this->normalizeId($siteId);
  50          if (isset($siteConfig))
  51              $siteConfig = $this->defaultSites->normalizeValue($siteConfig);
  52          else
  53              $siteConfig = $this->defaultSites->get($siteId);
  54          $siteConfig['extract'] = $this->convertRegexps($siteConfig['extract']);
  55          $siteConfig['scrape']  = $this->convertScrapes($siteConfig['scrape']);
  56          $this->checkAttributeFilters($siteConfig['attributes']);
  57          $tag = $this->addTag($siteId, $siteConfig);
  58          $this->sites[$siteId] = $siteConfig;
  59          foreach ($siteConfig['host'] as $host)
  60              $this->configurator->registeredVars['MediaEmbed.hosts'][$host] = $siteId;
  61          $this->configurator->registeredVars['MediaEmbed.sites'][$siteId] = [$siteConfig['extract'], $siteConfig['scrape']];
  62          return $tag;
  63      }
  64  	public function getSites()
  65      {
  66          return $this->sites;
  67      }
  68  	protected function addTag($siteId, array $siteConfig)
  69      {
  70          $tag = new Tag([
  71              'attributes' => $this->getAttributesConfig($siteConfig),
  72              'rules'      => [
  73                  'allowChild' => 'URL',
  74                  'autoClose'  => \true,
  75                  'denyChild'  => [$siteId, $this->tagName]
  76              ],
  77              'template'   => $this->templateBuilder->build($siteId, $siteConfig)
  78          ]);
  79          $this->configurator->templateNormalizer->normalizeTag($tag);
  80          $this->configurator->templateChecker->checkTag($tag);
  81          $this->configurator->tags->add($siteId, $tag);
  82          return $tag;
  83      }
  84  	protected function checkAttributeFilters(array $attributes)
  85      {
  86          foreach ($attributes as $attrConfig)
  87          {
  88              if (empty($attrConfig['filterChain']))
  89                  continue;
  90              foreach ($attrConfig['filterChain'] as $filter)
  91                  if (\substr($filter, 0, 1) !== '#' && !\in_array($filter, $this->allowedFilters, \true))
  92                      throw new RuntimeException("Filter '$filter' is not allowed in media sites");
  93          }
  94      }
  95  	protected function convertRegexp($regexp)
  96      {
  97          $regexp = new Regexp($regexp);
  98          return [$regexp, $regexp->getCaptureNames()];
  99      }
 100  	protected function convertRegexps(array $regexps)
 101      {
 102          return \array_map([$this, 'convertRegexp'], $regexps);
 103      }
 104  	protected function convertScrapeConfig(array $config)
 105      {
 106          $config['extract'] = $this->convertRegexps($config['extract']);
 107          $config['match']   = $this->convertRegexps($config['match']);
 108          return $config;
 109      }
 110  	protected function convertScrapes(array $scrapes)
 111      {
 112          return \array_map([$this, 'convertScrapeConfig'], $scrapes);
 113      }
 114  	protected function createMediaTag()
 115      {
 116          $tag = $this->configurator->tags->add($this->tagName);
 117          $tag->rules->autoClose();
 118          $tag->rules->denyChild($this->tagName);
 119          $tag->filterChain->clear();
 120          $tag->filterChain
 121              ->append(__NAMESPACE__ . '\\Parser::filterTag')
 122              ->resetParameters()
 123              ->addParameterByName('tag')
 124              ->addParameterByName('parser')
 125              ->addParameterByName('MediaEmbed.hosts')
 126              ->addParameterByName('MediaEmbed.sites')
 127              ->addParameterByName('cacheDir')
 128              ->setJS(\file_get_contents(__DIR__ . '/Parser/tagFilter.js'));
 129      }
 130  	protected function getAttributeNamesFromRegexps(array $regexps)
 131      {
 132          $attrNames = [];
 133          foreach ($regexps as $_53d26d37)
 134          {
 135              list($regexp, $map) = $_53d26d37;
 136              $attrNames += \array_flip(\array_filter($map));
 137          }
 138          return $attrNames;
 139      }
 140  	protected function getAttributesConfig(array $siteConfig)
 141      {
 142          $attrNames = $this->getAttributeNamesFromRegexps($siteConfig['extract']);
 143          foreach ($siteConfig['scrape'] as $scrapeConfig)
 144              $attrNames += $this->getAttributeNamesFromRegexps($scrapeConfig['extract']);
 145          $attributes = $siteConfig['attributes'] + \array_fill_keys(\array_keys($attrNames), []);
 146          foreach ($attributes as &$attrConfig)
 147              $attrConfig += ['required' => \false];
 148          unset($attrConfig);
 149          return $attributes;
 150      }
 151  	protected function normalizeId($siteId)
 152      {
 153          $siteId = \strtolower($siteId);
 154          if (!\preg_match('(^[a-z0-9]+$)', $siteId))
 155              throw new InvalidArgumentException('Invalid site ID');
 156          return $siteId;
 157      }
 158  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1