Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

parser.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 8.41 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\textformatter\s9e;
 015   
 016  use s9e\TextFormatter\Parser\BuiltInFilters;
 017  use s9e\TextFormatter\Parser\Logger;
 018   
 019  /**
 020  * s9e\TextFormatter\Parser adapter
 021  */
 022  class parser implements \phpbb\textformatter\parser_interface
 023  {
 024      /**
 025      * @var \phpbb\event\dispatcher_interface
 026      */
 027      protected $dispatcher;
 028   
 029      /**
 030      * @var \s9e\TextFormatter\Parser
 031      */
 032      protected $parser;
 033   
 034      /**
 035      * Constructor
 036      *
 037      * @param \phpbb\cache\driver_interface $cache
 038      * @param string $key Cache key
 039      * @param factory $factory
 040      * @param \phpbb\event\dispatcher_interface $dispatcher
 041      */
 042      public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher)
 043      {
 044          $parser = $cache->get($key);
 045          if (!$parser)
 046          {
 047              $objects = $factory->regenerate();
 048              $parser  = $objects['parser'];
 049          }
 050   
 051          $this->dispatcher = $dispatcher;
 052          $this->parser = $parser;
 053   
 054          $parser = $this;
 055   
 056          /**
 057          * Configure the parser service
 058          *
 059          * Can be used to:
 060          *  - toggle features or BBCodes
 061          *  - register variables or custom parsers in the s9e\TextFormatter parser
 062          *  - configure the s9e\TextFormatter parser's runtime settings
 063          *
 064          * @event core.text_formatter_s9e_parser_setup
 065          * @var \phpbb\textformatter\s9e\parser parser This parser service
 066          * @since 3.2.0-a1
 067          */
 068          $vars = array('parser');
 069          extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars)));
 070      }
 071   
 072      /**
 073      * {@inheritdoc}
 074      */
 075      public function parse($text)
 076      {
 077          $parser = $this;
 078   
 079          /**
 080          * Modify a text before it is parsed
 081          *
 082          * @event core.text_formatter_s9e_parse_before
 083          * @var \phpbb\textformatter\s9e\parser parser This parser service
 084          * @var string text The original text
 085          * @since 3.2.0-a1
 086          */
 087          $vars = array('parser', 'text');
 088          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_before', compact($vars)));
 089   
 090          $xml = $this->parser->parse($text);
 091   
 092          /**
 093          * Modify a parsed text in its XML form
 094          *
 095          * @event core.text_formatter_s9e_parse_after
 096          * @var \phpbb\textformatter\s9e\parser parser This parser service
 097          * @var string xml The parsed text, in XML
 098          * @since 3.2.0-a1
 099          */
 100          $vars = array('parser', 'xml');
 101          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_parse_after', compact($vars)));
 102   
 103          return $xml;
 104      }
 105   
 106      /**
 107      * {@inheritdoc}
 108      */
 109      public function disable_bbcode($name)
 110      {
 111          $this->parser->disableTag(strtoupper($name));
 112      }
 113   
 114      /**
 115      * {@inheritdoc}
 116      */
 117      public function disable_bbcodes()
 118      {
 119          $this->parser->disablePlugin('BBCodes');
 120      }
 121   
 122      /**
 123      * {@inheritdoc}
 124      */
 125      public function disable_censor()
 126      {
 127          $this->parser->disablePlugin('Censor');
 128      }
 129   
 130      /**
 131      * {@inheritdoc}
 132      */
 133      public function disable_magic_url()
 134      {
 135          $this->parser->disablePlugin('Autoemail');
 136          $this->parser->disablePlugin('Autolink');
 137      }
 138   
 139      /**
 140      * {@inheritdoc}
 141      */
 142      public function disable_smilies()
 143      {
 144          $this->parser->disablePlugin('Emoticons');
 145      }
 146   
 147      /**
 148      * {@inheritdoc}
 149      */
 150      public function enable_bbcode($name)
 151      {
 152          $this->parser->enableTag(strtoupper($name));
 153      }
 154   
 155      /**
 156      * {@inheritdoc}
 157      */
 158      public function enable_bbcodes()
 159      {
 160          $this->parser->enablePlugin('BBCodes');
 161      }
 162   
 163      /**
 164      * {@inheritdoc}
 165      */
 166      public function enable_censor()
 167      {
 168          $this->parser->enablePlugin('Censor');
 169      }
 170   
 171      /**
 172      * {@inheritdoc}
 173      */
 174      public function enable_magic_url()
 175      {
 176          $this->parser->enablePlugin('Autoemail');
 177          $this->parser->enablePlugin('Autolink');
 178      }
 179   
 180      /**
 181      * {@inheritdoc}
 182      */
 183      public function enable_smilies()
 184      {
 185          $this->parser->enablePlugin('Emoticons');
 186      }
 187   
 188      /**
 189      * {@inheritdoc}
 190      *
 191      * This will convert the log entries found in s9e\TextFormatter's logger into phpBB error
 192      * messages
 193      */
 194      public function get_errors()
 195      {
 196          $errors = array();
 197          foreach ($this->parser->getLogger()->get() as $entry)
 198          {
 199              list(, $msg, $context) = $entry;
 200   
 201              if ($msg === 'Tag limit exceeded')
 202              {
 203                  if ($context['tagName'] === 'E')
 204                  {
 205                      $errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']);
 206                  }
 207                  else if ($context['tagName'] === 'URL')
 208                  {
 209                      $errors[] = array('TOO_MANY_URLS', $context['tagLimit']);
 210                  }
 211              }
 212              else if ($msg === 'MAX_FONT_SIZE_EXCEEDED')
 213              {
 214                  $errors[] = array($msg, $context['max_size']);
 215              }
 216              else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m))
 217              {
 218                  $errors[] = array($msg, $context['max_' . strtolower($m[1])]);
 219              }
 220              else if ($msg === 'Tag is disabled')
 221              {
 222                  $name = strtolower($context['tag']->getName());
 223                  $errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']');
 224              }
 225              else if ($msg === 'UNABLE_GET_IMAGE_SIZE')
 226              {
 227                  $errors[] = array($msg);
 228              }
 229          }
 230   
 231          // Deduplicate error messages. array_unique() only works on strings so we have to serialize
 232          if (!empty($errors))
 233          {
 234              $errors = array_map('unserialize', array_unique(array_map('serialize', $errors)));
 235          }
 236   
 237          return $errors;
 238      }
 239   
 240      /**
 241      * Return the instance of s9e\TextFormatter\Parser used by this object
 242      *
 243      * @return \s9e\TextFormatter\Parser
 244      */
 245      public function get_parser()
 246      {
 247          return $this->parser;
 248      }
 249   
 250      /**
 251      * {@inheritdoc}
 252      */
 253      public function set_var($name, $value)
 254      {
 255          if ($name === 'max_smilies')
 256          {
 257              $this->parser->setTagLimit('E', $value ?: PHP_INT_MAX);
 258          }
 259          else if ($name === 'max_urls')
 260          {
 261              $this->parser->setTagLimit('URL', $value ?: PHP_INT_MAX);
 262          }
 263          else
 264          {
 265              $this->parser->registeredVars[$name] = $value;
 266          }
 267      }
 268   
 269      /**
 270      * {@inheritdoc}
 271      */
 272      public function set_vars(array $vars)
 273      {
 274          foreach ($vars as $name => $value)
 275          {
 276              $this->set_var($name, $value);
 277          }
 278      }
 279   
 280      /**
 281      * Filter a flash object's height
 282      *
 283      * @see bbcode_firstpass::bbcode_flash()
 284      *
 285      * @param  string  $height
 286      * @param  integer $max_height
 287      * @param  Logger  $logger
 288      * @return mixed              Original value if valid, FALSE otherwise
 289      */
 290      static public function filter_flash_height($height, $max_height, Logger $logger)
 291      {
 292          if ($max_height && $height > $max_height)
 293          {
 294              $logger->err('MAX_FLASH_HEIGHT_EXCEEDED', array('max_height' => $max_height));
 295   
 296              return false;
 297          }
 298   
 299          return $height;
 300      }
 301   
 302      /**
 303      * Filter a flash object's width
 304      *
 305      * @see bbcode_firstpass::bbcode_flash()
 306      *
 307      * @param  string  $width
 308      * @param  integer $max_width
 309      * @param  Logger  $logger
 310      * @return mixed              Original value if valid, FALSE otherwise
 311      */
 312      static public function filter_flash_width($width, $max_width, Logger $logger)
 313      {
 314          if ($max_width && $width > $max_width)
 315          {
 316              $logger->err('MAX_FLASH_WIDTH_EXCEEDED', array('max_width' => $max_width));
 317   
 318              return false;
 319          }
 320   
 321          return $width;
 322      }
 323   
 324      /**
 325      * Filter the value used in a [size] BBCode
 326      *
 327      * @see bbcode_firstpass::bbcode_size()
 328      *
 329      * @param  string  $size     Original size
 330      * @param  integer $max_size Maximum allowed size
 331      * @param  Logger  $logger
 332      * @return mixed             Original value if valid, FALSE otherwise
 333      */
 334      static public function filter_font_size($size, $max_size, Logger $logger)
 335      {
 336          if ($max_size && $size > $max_size)
 337          {
 338              $logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
 339   
 340              return false;
 341          }
 342   
 343          if ($size < 1)
 344          {
 345              return false;
 346          }
 347   
 348          return $size;
 349      }
 350   
 351      /**
 352      * Filter an image's URL to enforce restrictions on its dimensions
 353      *
 354      * @see bbcode_firstpass::bbcode_img()
 355      *
 356      * @param  string  $url        Original URL
 357      * @param  array   $url_config Config used by the URL filter
 358      * @param  Logger  $logger
 359      * @param  integer $max_height Maximum height allowed
 360      * @param  integer $max_width  Maximum width allowed
 361      * @return string|bool         Original value if valid, FALSE otherwise
 362      */
 363      static public function filter_img_url($url, array $url_config, Logger $logger, $max_height, $max_width)
 364      {
 365          // Validate the URL
 366          $url = BuiltInFilters::filterUrl($url, $url_config, $logger);
 367          if ($url === false)
 368          {
 369              return false;
 370          }
 371   
 372          if ($max_height || $max_width)
 373          {
 374              $imagesize = new \FastImageSize\FastImageSize();
 375              $size_info = $imagesize->getImageSize($url);
 376              if ($size_info === false)
 377              {
 378                  $logger->err('UNABLE_GET_IMAGE_SIZE');
 379                  return false;
 380              }
 381   
 382              if ($max_height && $max_height < $size_info['height'])
 383              {
 384                  $logger->err('MAX_IMG_HEIGHT_EXCEEDED', array('max_height' => $max_height));
 385                  return false;
 386              }
 387   
 388              if ($max_width && $max_width < $size_info['width'])
 389              {
 390                  $logger->err('MAX_IMG_WIDTH_EXCEEDED', array('max_width' => $max_width));
 391                  return false;
 392              }
 393          }
 394   
 395          return $url;
 396      }
 397  }
 398