[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/phpbb/textformatter/s9e/ -> parser.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\textformatter\s9e;
  15  
  16  use s9e\TextFormatter\Parser\AttributeFilters\UrlFilter;
  17  use s9e\TextFormatter\Parser\Logger;
  18  use s9e\TextFormatter\Parser\Tag;
  19  
  20  /**
  21  * s9e\TextFormatter\Parser adapter
  22  */
  23  class parser implements \phpbb\textformatter\parser_interface
  24  {
  25      /**
  26      * @var \phpbb\event\dispatcher_interface
  27      */
  28      protected $dispatcher;
  29  
  30      /**
  31      * @var \s9e\TextFormatter\Parser
  32      */
  33      protected $parser;
  34  
  35      /**
  36      * Constructor
  37      *
  38      * @param \phpbb\cache\driver\driver_interface $cache
  39      * @param string $key Cache key
  40      * @param factory $factory
  41      * @param \phpbb\event\dispatcher_interface $dispatcher
  42      */
  43  	public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
  44      {
  45          $parser = $cache->get($key);
  46          if (!$parser)
  47          {
  48              $objects = $factory->regenerate();
  49              $parser  = $objects['parser'];
  50          }
  51  
  52          $this->dispatcher = $dispatcher;
  53          $this->parser = $parser;
  54  
  55          $parser = $this;
  56  
  57          /**
  58          * Configure the parser service
  59          *
  60          * Can be used to:
  61          *  - toggle features or BBCodes
  62          *  - register variables or custom parsers in the s9e\TextFormatter parser
  63          *  - configure the s9e\TextFormatter parser's runtime settings
  64          *
  65          * @event core.text_formatter_s9e_parser_setup
  66          * @var \phpbb\textformatter\s9e\parser parser This parser service
  67          * @since 3.2.0-a1
  68          */
  69          $vars = array('parser');
  70          extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars)));
  71      }
  72  
  73      /**
  74      * {@inheritdoc}
  75      */
  76  	public function parse($text)
  77      {
  78          $parser = $this;
  79  
  80          /**
  81          * Modify a text before it is parsed
  82          *
  83          * @event core.text_formatter_s9e_parse_before
  84          * @var \phpbb\textformatter\s9e\parser parser This parser service
  85          * @var string text The original text
  86          * @since 3.2.0-a1
  87          */
  88          $vars = array('parser', 'text');
  89          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_before', compact($vars)));
  90  
  91          $xml = $this->parser->parse($text);
  92  
  93          /**
  94          * Modify a parsed text in its XML form
  95          *
  96          * @event core.text_formatter_s9e_parse_after
  97          * @var \phpbb\textformatter\s9e\parser parser This parser service
  98          * @var string xml The parsed text, in XML
  99          * @since 3.2.0-a1
 100          */
 101          $vars = array('parser', 'xml');
 102          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_after', compact($vars)));
 103  
 104          return $xml;
 105      }
 106  
 107      /**
 108      * {@inheritdoc}
 109      */
 110  	public function disable_bbcode($name)
 111      {
 112          $this->parser->disableTag(strtoupper($name));
 113      }
 114  
 115      /**
 116      * {@inheritdoc}
 117      */
 118  	public function disable_bbcodes()
 119      {
 120          $this->parser->disablePlugin('BBCodes');
 121      }
 122  
 123      /**
 124      * {@inheritdoc}
 125      */
 126  	public function disable_censor()
 127      {
 128          $this->parser->disablePlugin('Censor');
 129      }
 130  
 131      /**
 132      * {@inheritdoc}
 133      */
 134  	public function disable_magic_url()
 135      {
 136          $this->parser->disablePlugin('Autoemail');
 137          $this->parser->disablePlugin('Autolink');
 138      }
 139  
 140      /**
 141      * {@inheritdoc}
 142      */
 143  	public function disable_smilies()
 144      {
 145          $this->parser->disablePlugin('Emoticons');
 146          $this->parser->disablePlugin('Emoji');
 147      }
 148  
 149      /**
 150      * {@inheritdoc}
 151      */
 152  	public function enable_bbcode($name)
 153      {
 154          $this->parser->enableTag(strtoupper($name));
 155      }
 156  
 157      /**
 158      * {@inheritdoc}
 159      */
 160  	public function enable_bbcodes()
 161      {
 162          $this->parser->enablePlugin('BBCodes');
 163      }
 164  
 165      /**
 166      * {@inheritdoc}
 167      */
 168  	public function enable_censor()
 169      {
 170          $this->parser->enablePlugin('Censor');
 171      }
 172  
 173      /**
 174      * {@inheritdoc}
 175      */
 176  	public function enable_magic_url()
 177      {
 178          $this->parser->enablePlugin('Autoemail');
 179          $this->parser->enablePlugin('Autolink');
 180      }
 181  
 182      /**
 183      * {@inheritdoc}
 184      */
 185  	public function enable_smilies()
 186      {
 187          $this->parser->enablePlugin('Emoticons');
 188          $this->parser->enablePlugin('Emoji');
 189      }
 190  
 191      /**
 192      * {@inheritdoc}
 193      *
 194      * This will convert the log entries found in s9e\TextFormatter's logger into phpBB error
 195      * messages
 196      */
 197  	public function get_errors()
 198      {
 199          $errors = array();
 200          $entries = $this->parser->getLogger()->getLogs();
 201  
 202          foreach ($entries as $entry)
 203          {
 204              list(, $msg, $context) = $entry;
 205  
 206              if ($msg === 'Tag limit exceeded')
 207              {
 208                  if ($context['tagName'] === 'E')
 209                  {
 210                      $errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']);
 211                  }
 212                  else if ($context['tagName'] === 'URL')
 213                  {
 214                      $errors[] = array('TOO_MANY_URLS', $context['tagLimit']);
 215                  }
 216              }
 217              else if ($msg === 'MAX_FONT_SIZE_EXCEEDED')
 218              {
 219                  $errors[] = array($msg, $context['max_size']);
 220              }
 221              else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m))
 222              {
 223                  $errors[] = array($msg, $context['max_' . strtolower($m[1])]);
 224              }
 225              else if ($msg === 'Tag is disabled' && $this->is_a_bbcode($context['tag']))
 226              {
 227                  $name = strtolower($context['tag']->getName());
 228                  $errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']');
 229              }
 230              else if ($msg === 'UNABLE_GET_IMAGE_SIZE')
 231              {
 232                  $errors[] = array($msg);
 233              }
 234          }
 235  
 236          // Deduplicate error messages. array_unique() only works on strings so we have to serialize
 237          if (!empty($errors))
 238          {
 239              $errors = array_map('unserialize', array_unique(array_map('serialize', $errors)));
 240          }
 241  
 242          $parser = $this;
 243  
 244          /**
 245          * Modify error messages generated by the s9e\TextFormatter's logger
 246          *
 247          * @event core.text_formatter_s9e_get_errors
 248          * @var parser    parser        This parser service
 249          * @var array    entries        s9e\TextFormatter's logger entries
 250          * @var array    errors        Error arrays with language key and optional arguments
 251          * @since 3.2.10-RC1
 252          * @since 3.3.1-RC1
 253          */
 254          $vars = [
 255              'parser',
 256              'entries',
 257              'errors',
 258          ];
 259          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_get_errors', compact($vars)));
 260  
 261          return $errors;
 262      }
 263  
 264      /**
 265      * Return the instance of s9e\TextFormatter\Parser used by this object
 266      *
 267      * @return \s9e\TextFormatter\Parser
 268      */
 269  	public function get_parser()
 270      {
 271          return $this->parser;
 272      }
 273  
 274      /**
 275      * {@inheritdoc}
 276      */
 277  	public function set_var($name, $value)
 278      {
 279          if ($name === 'max_smilies')
 280          {
 281              $this->parser->setTagLimit('E', $value ?: PHP_INT_MAX);
 282          }
 283          else if ($name === 'max_urls')
 284          {
 285              $this->parser->setTagLimit('URL', $value ?: PHP_INT_MAX);
 286          }
 287          else
 288          {
 289              $this->parser->registeredVars[$name] = $value;
 290          }
 291      }
 292  
 293      /**
 294      * {@inheritdoc}
 295      */
 296  	public function set_vars(array $vars)
 297      {
 298          foreach ($vars as $name => $value)
 299          {
 300              $this->set_var($name, $value);
 301          }
 302      }
 303  
 304      /**
 305      * Filter a flash object's height
 306      *
 307      * @see bbcode_firstpass::bbcode_flash()
 308      *
 309      * @param  string  $height
 310      * @param  integer $max_height
 311      * @param  Logger  $logger
 312      * @return mixed              Original value if valid, FALSE otherwise
 313      */
 314  	static public function filter_flash_height($height, $max_height, Logger $logger)
 315      {
 316          if ($max_height && $height > $max_height)
 317          {
 318              $logger->err('MAX_FLASH_HEIGHT_EXCEEDED', array('max_height' => $max_height));
 319  
 320              return false;
 321          }
 322  
 323          return $height;
 324      }
 325  
 326      /**
 327      * Filter a flash object's width
 328      *
 329      * @see bbcode_firstpass::bbcode_flash()
 330      *
 331      * @param  string  $width
 332      * @param  integer $max_width
 333      * @param  Logger  $logger
 334      * @return mixed              Original value if valid, FALSE otherwise
 335      */
 336  	static public function filter_flash_width($width, $max_width, Logger $logger)
 337      {
 338          if ($max_width && $width > $max_width)
 339          {
 340              $logger->err('MAX_FLASH_WIDTH_EXCEEDED', array('max_width' => $max_width));
 341  
 342              return false;
 343          }
 344  
 345          return $width;
 346      }
 347  
 348      /**
 349      * Filter the value used in a [size] BBCode
 350      *
 351      * @see bbcode_firstpass::bbcode_size()
 352      *
 353      * @param  string  $size     Original size
 354      * @param  integer $max_size Maximum allowed size
 355      * @param  Logger  $logger
 356      * @return mixed             Original value if valid, FALSE otherwise
 357      */
 358  	static public function filter_font_size($size, $max_size, Logger $logger)
 359      {
 360          if ($max_size && $size > $max_size)
 361          {
 362              $logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
 363  
 364              return false;
 365          }
 366  
 367          if ($size < 1 || !is_numeric($size))
 368          {
 369              return false;
 370          }
 371  
 372          return $size;
 373      }
 374  
 375      /**
 376      * Filter an image's URL to enforce restrictions on its dimensions
 377      *
 378      * @see bbcode_firstpass::bbcode_img()
 379      *
 380      * @param  string  $url        Original URL
 381      * @param  array   $url_config Config used by the URL filter
 382      * @param  Logger  $logger
 383      *
 384      * @return string|bool         Original value if valid, FALSE otherwise
 385      */
 386  	static public function filter_img_url($url, array $url_config, Logger $logger)
 387      {
 388          // Validate the URL
 389          $url = UrlFilter::filter($url, $url_config, $logger);
 390          if ($url === false)
 391          {
 392              return false;
 393          }
 394  
 395          return $url;
 396      }
 397  
 398      /**
 399      * Test whether given tag consumes text that looks like BBCode-styled markup
 400      *
 401      * @param  Tag  $tag Original tag
 402      * @return bool
 403      */
 404  	protected function is_a_bbcode(Tag $tag)
 405      {
 406          if ($tag->getLen() < 3)
 407          {
 408              return false;
 409          }
 410          $markup = substr($this->parser->getText(), $tag->getPos(), $tag->getLen());
 411  
 412          return (bool) preg_match('(^\\[\\w++.*?\\]$)s', $markup);
 413      }
 414  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1