[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/phpbb/textformatter/s9e/ -> factory.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\Configurator;
  17  use s9e\TextFormatter\Configurator\Items\AttributeFilters\RegexpFilter;
  18  use s9e\TextFormatter\Configurator\Items\UnsafeTemplate;
  19  
  20  /**
  21  * Creates s9e\TextFormatter objects
  22  */
  23  class factory implements \phpbb\textformatter\cache_interface
  24  {
  25      /**
  26      * @var \phpbb\textformatter\s9e\link_helper
  27      */
  28      protected $link_helper;
  29  
  30      /**
  31      * @var \phpbb\cache\driver\driver_interface
  32      */
  33      protected $cache;
  34  
  35      /**
  36      * @var string Path to the cache dir
  37      */
  38      protected $cache_dir;
  39  
  40      /**
  41      * @var string Cache key used for the parser
  42      */
  43      protected $cache_key_parser;
  44  
  45      /**
  46      * @var string Cache key used for the renderer
  47      */
  48      protected $cache_key_renderer;
  49  
  50      /**
  51      * @var \phpbb\config\config
  52      */
  53      protected $config;
  54  
  55      /**
  56      * @var array Custom tokens used in bbcode.html and their corresponding token from the definition
  57      */
  58      protected $custom_tokens = array(
  59          'email' => array('{DESCRIPTION}' => '{TEXT}'),
  60          'flash' => array('{WIDTH}' => '{NUMBER1}', '{HEIGHT}' => '{NUMBER2}'),
  61          'img'   => array('{URL}' => '{IMAGEURL}'),
  62          'list'  => array('{LIST_TYPE}' => '{HASHMAP}'),
  63          'quote' => array('{USERNAME}' => '{TEXT1}'),
  64          'size'  => array('{SIZE}' => '{FONTSIZE}'),
  65          'url'   => array('{DESCRIPTION}' => '{TEXT}'),
  66      );
  67  
  68      /**
  69      * @var \phpbb\textformatter\data_access
  70      */
  71      protected $data_access;
  72  
  73      /**
  74      * @var array Default BBCode definitions
  75      */
  76      protected $default_definitions = array(
  77          'attachment' => '[ATTACHMENT index={NUMBER} filename={TEXT;useContent}]',
  78          'b'     => '[B]{TEXT}[/B]',
  79          'code'  => '[CODE lang={IDENTIFIER;optional}]{TEXT}[/CODE]',
  80          'color' => '[COLOR={COLOR}]{TEXT}[/COLOR]',
  81          'email' => '[EMAIL={EMAIL;useContent} subject={TEXT1;optional;postFilter=rawurlencode} body={TEXT2;optional;postFilter=rawurlencode}]{TEXT}[/EMAIL]',
  82          'flash' => '[FLASH={NUMBER1},{NUMBER2} width={NUMBER1;postFilter=#flashwidth} height={NUMBER2;postFilter=#flashheight} url={URL;useContent} /]',
  83          'i'     => '[I]{TEXT}[/I]',
  84          'img'   => '[IMG src={IMAGEURL;useContent}]',
  85          'list'  => '[LIST type={HASHMAP=1:decimal,a:lower-alpha,A:upper-alpha,i:lower-roman,I:upper-roman;optional;postFilter=#simpletext} #createChild=LI]{TEXT}[/LIST]',
  86          'li'    => '[* $tagName=LI]{TEXT}[/*]',
  87          'quote' =>
  88              "[QUOTE
  89                  author={TEXT1;optional}
  90                  post_id={UINT;optional}
  91                  post_url={URL;optional;postFilter=#false}
  92                  msg_id={UINT;optional}
  93                  msg_url={URL;optional;postFilter=#false}
  94                  profile_url={URL;optional;postFilter=#false}
  95                  time={UINT;optional}
  96                  url={URL;optional}
  97                  user_id={UINT;optional}
  98                  author={PARSE=/^\\[url=(?'url'.*?)](?'author'.*)\\[\\/url]$/i}
  99                  author={PARSE=/^\\[url](?'author'(?'url'.*?))\\[\\/url]$/i}
 100                  author={PARSE=/(?'url'https?:\\/\\/[^[\\]]+)/i}
 101              ]{TEXT2}[/QUOTE]",
 102          'size'  => '[SIZE={FONTSIZE}]{TEXT}[/SIZE]',
 103          'u'     => '[U]{TEXT}[/U]',
 104          'url'   => '[URL={URL;useContent} $forceLookahead=true]{TEXT}[/URL]',
 105      );
 106  
 107      /**
 108      * @var array Default templates, taken from bbcode::bbcode_tpl()
 109      */
 110      protected $default_templates = array(
 111          'b'     => '<span style="font-weight: bold"><xsl:apply-templates/></span>',
 112          'i'     => '<span style="font-style: italic"><xsl:apply-templates/></span>',
 113          'u'     => '<span style="text-decoration: underline"><xsl:apply-templates/></span>',
 114          'img'   => '<img src="{IMAGEURL}" class="postimage" alt="{L_IMAGE}"/>',
 115          'size'    => '<span><xsl:attribute name="style"><xsl:text>font-size: </xsl:text><xsl:value-of select="substring(@size, 1, 4)"/><xsl:text>%; line-height: normal</xsl:text></xsl:attribute><xsl:apply-templates/></span>',
 116          'color' => '<span style="color: {COLOR}"><xsl:apply-templates/></span>',
 117          'email' => '<a>
 118              <xsl:attribute name="href">
 119                  <xsl:text>mailto:</xsl:text>
 120                  <xsl:value-of select="@email"/>
 121                  <xsl:if test="@subject or @body">
 122                      <xsl:text>?</xsl:text>
 123                      <xsl:if test="@subject">subject=<xsl:value-of select="@subject"/></xsl:if>
 124                      <xsl:if test="@body"><xsl:if test="@subject">&amp;</xsl:if>body=<xsl:value-of select="@body"/></xsl:if>
 125                  </xsl:if>
 126              </xsl:attribute>
 127              <xsl:apply-templates/>
 128          </a>',
 129      );
 130  
 131      /**
 132      * @var \phpbb\event\dispatcher_interface
 133      */
 134      protected $dispatcher;
 135  
 136      /**
 137      * @var \phpbb\log\log_interface
 138      */
 139      protected $log;
 140  
 141      /**
 142      * Constructor
 143      *
 144      * @param \phpbb\textformatter\data_access $data_access
 145      * @param \phpbb\cache\driver\driver_interface $cache
 146      * @param \phpbb\event\dispatcher_interface $dispatcher
 147      * @param \phpbb\config\config $config
 148      * @param \phpbb\textformatter\s9e\link_helper $link_helper
 149      * @param \phpbb\log\log_interface $log
 150      * @param string $cache_dir          Path to the cache dir
 151      * @param string $cache_key_parser   Cache key used for the parser
 152      * @param string $cache_key_renderer Cache key used for the renderer
 153      */
 154  	public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, \phpbb\textformatter\s9e\link_helper $link_helper, \phpbb\log\log_interface $log, $cache_dir, $cache_key_parser, $cache_key_renderer)
 155      {
 156          $this->link_helper = $link_helper;
 157          $this->cache = $cache;
 158          $this->cache_dir = $cache_dir;
 159          $this->cache_key_parser = $cache_key_parser;
 160          $this->cache_key_renderer = $cache_key_renderer;
 161          $this->config = $config;
 162          $this->data_access = $data_access;
 163          $this->dispatcher = $dispatcher;
 164          $this->log = $log;
 165      }
 166  
 167      /**
 168      * {@inheritdoc}
 169      */
 170  	public function invalidate()
 171      {
 172          $this->regenerate();
 173      }
 174  
 175      /**
 176      * {@inheritdoc}
 177      *
 178      * Will remove old renderers from the cache dir but won't touch the current renderer
 179      */
 180  	public function tidy()
 181      {
 182          // Get the name of current renderer
 183          $renderer_data = $this->cache->get($this->cache_key_renderer);
 184          $renderer_file = ($renderer_data) ? $renderer_data['class'] . '.php' : null;
 185  
 186          foreach (glob($this->cache_dir . 's9e_*') as $filename)
 187          {
 188              // Only remove the file if it's not the current renderer
 189              if (!$renderer_file || substr($filename, -strlen($renderer_file)) !== $renderer_file)
 190              {
 191                  unlink($filename);
 192              }
 193          }
 194      }
 195  
 196      /**
 197      * Generate and return a new configured instance of s9e\TextFormatter\Configurator
 198      *
 199      * @return Configurator
 200      */
 201  	public function get_configurator()
 202      {
 203          // Create a new Configurator
 204          $configurator = new Configurator;
 205  
 206          /**
 207          * Modify the s9e\TextFormatter configurator before the default settings are set
 208          *
 209          * @event core.text_formatter_s9e_configure_before
 210          * @var \s9e\TextFormatter\Configurator configurator Configurator instance
 211          * @since 3.2.0-a1
 212          */
 213          $vars = array('configurator');
 214          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_configure_before', compact($vars)));
 215  
 216          // Reset the list of allowed schemes
 217          foreach ($configurator->urlConfig->getAllowedSchemes() as $scheme)
 218          {
 219              $configurator->urlConfig->disallowScheme($scheme);
 220          }
 221          foreach (explode(',', $this->config['allowed_schemes_links']) as $scheme)
 222          {
 223              $configurator->urlConfig->allowScheme(trim($scheme));
 224          }
 225  
 226          // Convert newlines to br elements by default
 227          $configurator->rootRules->enableAutoLineBreaks();
 228  
 229          // Don't automatically ignore text in places where text is not allowed
 230          $configurator->rulesGenerator->remove('IgnoreTextIfDisallowed');
 231  
 232          // Don't remove comments and instead convert them to xsl:comment elements
 233          $configurator->templateNormalizer->remove('RemoveComments');
 234          $configurator->templateNormalizer->add('TransposeComments');
 235  
 236          // Set the rendering engine and configure it to save to the cache dir
 237          $configurator->rendering->engine = 'PHP';
 238          $configurator->rendering->engine->cacheDir = $this->cache_dir;
 239          $configurator->rendering->engine->defaultClassPrefix = 's9e_renderer_';
 240          $configurator->rendering->engine->enableQuickRenderer = true;
 241  
 242          // Create custom filters for BBCode tokens that are supported in phpBB but not in
 243          // s9e\TextFormatter
 244          $filter = new RegexpFilter('#^' . get_preg_expression('relative_url') . '$#Du');
 245          $configurator->attributeFilters->add('#local_url', $filter);
 246          $configurator->attributeFilters->add('#relative_url', $filter);
 247  
 248          // INTTEXT regexp from acp_bbcodes
 249          $filter = new RegexpFilter('!^([\p{L}\p{N}\-+,_. ]+)$!Du');
 250          $configurator->attributeFilters->add('#inttext', $filter);
 251  
 252          // Create custom filters for Flash restrictions, which use the same values as the image
 253          // restrictions but have their own error message
 254          $configurator->attributeFilters
 255              ->add('#flashheight', __NAMESPACE__ . '\\parser::filter_flash_height')
 256              ->addParameterByName('max_img_height')
 257              ->addParameterByName('logger');
 258  
 259          $configurator->attributeFilters
 260              ->add('#flashwidth', __NAMESPACE__ . '\\parser::filter_flash_width')
 261              ->addParameterByName('max_img_width')
 262              ->addParameterByName('logger');
 263  
 264          // Create a custom filter for phpBB's per-mode font size limits
 265          $configurator->attributeFilters
 266              ->add('#fontsize', __NAMESPACE__ . '\\parser::filter_font_size')
 267              ->addParameterByName('max_font_size')
 268              ->addParameterByName('logger')
 269              ->markAsSafeInCSS();
 270  
 271          // Create a custom filter for image URLs
 272          $configurator->attributeFilters
 273              ->add('#imageurl', __NAMESPACE__ . '\\parser::filter_img_url')
 274              ->addParameterByName('urlConfig')
 275              ->addParameterByName('logger')
 276              ->addParameterByName('max_img_height')
 277              ->addParameterByName('max_img_width')
 278              ->markAsSafeAsURL()
 279              ->setJS('UrlFilter.filter');
 280  
 281          // Add default BBCodes
 282          foreach ($this->get_default_bbcodes($configurator) as $bbcode)
 283          {
 284              $this->add_bbcode($configurator, $bbcode['usage'], $bbcode['template']);
 285          }
 286          if (isset($configurator->tags['QUOTE']))
 287          {
 288              // Remove the nesting limit and let other services remove quotes at parsing time
 289              $configurator->tags['QUOTE']->nestingLimit = PHP_INT_MAX;
 290          }
 291  
 292          // Modify the template to disable images/flash depending on user's settings
 293          foreach (array('FLASH', 'IMG') as $name)
 294          {
 295              $tag = $configurator->tags[$name];
 296              $tag->template = '<xsl:choose><xsl:when test="$S_VIEW' . $name . '">' . $tag->template . '</xsl:when><xsl:otherwise><xsl:apply-templates/></xsl:otherwise></xsl:choose>';
 297          }
 298  
 299          // Load custom BBCodes
 300          foreach ($this->data_access->get_bbcodes() as $row)
 301          {
 302              // Insert the board's URL before {LOCAL_URL} tokens
 303              $tpl = preg_replace_callback(
 304                  '#\\{LOCAL_URL\\d*\\}#',
 305                  function ($m)
 306                  {
 307                      return generate_board_url() . '/' . $m[0];
 308                  },
 309                  $row['bbcode_tpl']
 310              );
 311              $this->add_bbcode($configurator, $row['bbcode_match'], $tpl);
 312          }
 313  
 314          // Load smilies
 315          foreach ($this->data_access->get_smilies() as $row)
 316          {
 317              $configurator->Emoticons->set(
 318                  $row['code'],
 319                  '<img class="smilies" src="{$T_SMILIES_PATH}/' . $this->escape_html_attribute($row['smiley_url']) . '" width="' . $row['smiley_width'] . '" height="' . $row['smiley_height'] . '" alt="{.}" title="' . $this->escape_html_attribute($row['emotion']) . '"/>'
 320              );
 321          }
 322  
 323          if (isset($configurator->Emoticons))
 324          {
 325              // Force emoticons to be rendered as text if $S_VIEWSMILIES is not set
 326              $configurator->Emoticons->notIfCondition = 'not($S_VIEWSMILIES)';
 327  
 328              // Only parse emoticons at the beginning of the text or if they're preceded by any
 329              // one of: a new line, a space, a dot, or a right square bracket
 330              $configurator->Emoticons->notAfter = '[^\\n .\\]]';
 331  
 332              // Ignore emoticons that are immediately followed by a "word" character
 333              $configurator->Emoticons->notBefore = '\\w';
 334          }
 335  
 336          // Load the censored words
 337          $censor = $this->data_access->get_censored_words();
 338          if (!empty($censor))
 339          {
 340              // Use a namespaced tag to avoid collisions
 341              $configurator->plugins->load('Censor', array('tagName' => 'censor:tag'));
 342              foreach ($censor as $row)
 343              {
 344                  $configurator->Censor->add($row['word'], $row['replacement']);
 345              }
 346          }
 347  
 348          // Load the magic links plugins. We do that after BBCodes so that they use the same tags
 349          $this->configure_autolink($configurator);
 350  
 351          // Register some vars with a default value. Those should be set at runtime by whatever calls
 352          // the parser
 353          $configurator->registeredVars['max_font_size'] = 0;
 354          $configurator->registeredVars['max_img_height'] = 0;
 355          $configurator->registeredVars['max_img_width'] = 0;
 356  
 357          // Load the Emoji plugin and modify its tag's template to obey viewsmilies
 358          $tag = $configurator->Emoji->getTag();
 359          $tag->template = '<xsl:choose>
 360              <xsl:when test="@tseq">
 361                  <img alt="{.}" class="emoji" draggable="false" src="//twemoji.maxcdn.com/2/svg/{@tseq}.svg"/>
 362              </xsl:when>
 363              <xsl:otherwise>
 364                  <img alt="{.}" class="emoji" draggable="false" src="https://cdn.jsdelivr.net/gh/s9e/emoji-assets-twemoji@11.2/dist/svgz/{@seq}.svgz"/>
 365              </xsl:otherwise>
 366          </xsl:choose>';
 367          $tag->template = '<xsl:choose><xsl:when test="$S_VIEWSMILIES">' . str_replace('class="emoji"', 'class="emoji smilies"', $tag->template) . '</xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>';
 368  
 369          /**
 370          * Modify the s9e\TextFormatter configurator after the default settings are set
 371          *
 372          * @event core.text_formatter_s9e_configure_after
 373          * @var \s9e\TextFormatter\Configurator configurator Configurator instance
 374          * @since 3.2.0-a1
 375          */
 376          $vars = array('configurator');
 377          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_configure_after', compact($vars)));
 378  
 379          return $configurator;
 380      }
 381  
 382      /**
 383      * Regenerate and cache a new parser and renderer
 384      *
 385      * @return array Associative array with at least two elements: "parser" and "renderer"
 386      */
 387  	public function regenerate()
 388      {
 389          $configurator = $this->get_configurator();
 390  
 391          // Get the censor helper and remove the Censor plugin if applicable
 392          if (isset($configurator->Censor))
 393          {
 394              $censor = $configurator->Censor->getHelper();
 395              unset($configurator->Censor);
 396              unset($configurator->tags['censor:tag']);
 397          }
 398  
 399          $objects = $configurator->finalize();
 400  
 401          /**
 402          * Access the objects returned by finalize() before they are saved to cache
 403          *
 404          * @event core.text_formatter_s9e_configure_finalize
 405          * @var array objects Array containing a "parser" object, a "renderer" object and optionally a "js" string
 406          * @since 3.2.2-RC1
 407          */
 408          $vars = array('objects');
 409          extract($this->dispatcher->trigger_event('core.text_formatter_s9e_configure_finalize', compact($vars)));
 410  
 411          $parser   = $objects['parser'];
 412          $renderer = $objects['renderer'];
 413  
 414          // Cache the parser as-is
 415          $this->cache->put($this->cache_key_parser, $parser);
 416  
 417          // We need to cache the name of the renderer's generated class
 418          $renderer_data = array('class' => get_class($renderer));
 419          if (isset($censor))
 420          {
 421              $renderer_data['censor'] = $censor;
 422          }
 423          $this->cache->put($this->cache_key_renderer, $renderer_data);
 424  
 425          return array('parser' => $parser, 'renderer' => $renderer);
 426      }
 427  
 428      /**
 429      * Add a BBCode to given configurator
 430      *
 431      * @param  Configurator $configurator
 432      * @param  string       $usage
 433      * @param  string       $template
 434      * @return void
 435      */
 436  	protected function add_bbcode(Configurator $configurator, $usage, $template)
 437      {
 438          try
 439          {
 440              $configurator->BBCodes->addCustom($usage, new UnsafeTemplate($template));
 441          }
 442          catch (\Exception $e)
 443          {
 444              $this->log->add('critical', null, null, 'LOG_BBCODE_CONFIGURATION_ERROR', false, [$usage, $e->getMessage()]);
 445          }
 446      }
 447  
 448      /**
 449      * Configure the Autolink / Autoemail plugins used to linkify text
 450      *
 451      * @param  \s9e\TextFormatter\Configurator $configurator
 452      * @return void
 453      */
 454  	protected function configure_autolink(Configurator $configurator)
 455      {
 456          $configurator->plugins->load('Autoemail');
 457          $configurator->plugins->load('Autolink', array('matchWww' => true));
 458  
 459          // Add a tag filter that creates a tag that stores and replace the
 460          // content of a link created by the Autolink plugin
 461          $configurator->Autolink->getTag()->filterChain
 462              ->add(array($this->link_helper, 'generate_link_text_tag'))
 463              ->resetParameters()
 464              ->addParameterByName('tag')
 465              ->addParameterByName('parser');
 466  
 467          // Create a tag that will be used to display the truncated text by
 468          // replacing the original content with the content of the @text attribute
 469          $tag = $configurator->tags->add('LINK_TEXT');
 470          $tag->attributes->add('text');
 471          $tag->template = '<xsl:value-of select="@text"/>';
 472  
 473          $tag->filterChain
 474              ->add(array($this->link_helper, 'truncate_local_url'))
 475              ->resetParameters()
 476              ->addParameterByName('tag')
 477              ->addParameterByValue(generate_board_url() . '/');
 478          $tag->filterChain
 479              ->add(array($this->link_helper, 'truncate_text'))
 480              ->resetParameters()
 481              ->addParameterByName('tag');
 482          $tag->filterChain
 483              ->add(array($this->link_helper, 'cleanup_tag'))
 484              ->resetParameters()
 485              ->addParameterByName('tag')
 486              ->addParameterByName('parser');
 487      }
 488  
 489      /**
 490      * Escape a literal to be used in an HTML attribute in an XSL template
 491      *
 492      * Escapes "HTML special chars" for obvious reasons and curly braces to avoid them
 493      * being interpreted as an attribute value template
 494      *
 495      * @param  string $value Original string
 496      * @return string        Escaped string
 497      */
 498  	protected function escape_html_attribute($value)
 499      {
 500          return htmlspecialchars(strtr($value, ['{' => '{{', '}' => '}}']), ENT_COMPAT | ENT_XML1, 'UTF-8');
 501      }
 502  
 503      /**
 504      * Return the default BBCodes configuration
 505      *
 506      * @return array 2D array. Each element has a 'usage' key, a 'template' key, and an optional 'options' key
 507      */
 508  	protected function get_default_bbcodes($configurator)
 509      {
 510          // For each BBCode, build an associative array matching style_ids to their template
 511          $templates = array();
 512          foreach ($this->data_access->get_styles_templates() as $style_id => $data)
 513          {
 514              foreach ($this->extract_templates($data['template']) as $bbcode_name => $template)
 515              {
 516                  $templates[$bbcode_name][$style_id] = $template;
 517              }
 518  
 519              // Add default templates wherever missing, or for BBCodes that were not specified in
 520              // this template's bitfield. For instance, prosilver has a custom template for b but its
 521              // bitfield does not enable it so the default template is used instead
 522              foreach ($this->default_templates as $bbcode_name => $template)
 523              {
 524                  if (!isset($templates[$bbcode_name][$style_id]) || !in_array($bbcode_name, $data['bbcodes'], true))
 525                  {
 526                      $templates[$bbcode_name][$style_id] = $template;
 527                  }
 528              }
 529          }
 530  
 531          // Replace custom tokens and normalize templates
 532          foreach ($templates as $bbcode_name => $style_templates)
 533          {
 534              foreach ($style_templates as $i => $template)
 535              {
 536                  if (isset($this->custom_tokens[$bbcode_name]))
 537                  {
 538                      $template = strtr($template, $this->custom_tokens[$bbcode_name]);
 539                  }
 540  
 541                  $templates[$bbcode_name][$i] = $configurator->templateNormalizer->normalizeTemplate($template);
 542              }
 543          }
 544  
 545          $bbcodes = array();
 546          foreach ($this->default_definitions as $bbcode_name => $usage)
 547          {
 548              $bbcodes[$bbcode_name] = array(
 549                  'usage'    => $usage,
 550                  'template' => $this->merge_templates($templates[$bbcode_name]),
 551              );
 552          }
 553  
 554          return $bbcodes;
 555      }
 556  
 557      /**
 558      * Extract and recompose individual BBCode templates from a style's template file
 559      *
 560      * @param  string $template Style template (bbcode.html)
 561      * @return array Associative array matching BBCode names to their template
 562      */
 563  	protected function extract_templates($template)
 564      {
 565          // Capture the template fragments
 566          // Allow either phpBB template or the Twig syntax
 567          preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END .*? -->#s', $template, $matches, PREG_SET_ORDER) ?:
 568              preg_match_all('#{% for (.*?) in .*? %}(.*?){% endfor %}#s', $template, $matches, PREG_SET_ORDER);
 569  
 570          $fragments = array();
 571          foreach ($matches as $match)
 572          {
 573              // Normalize the whitespace
 574              $fragment = preg_replace('#>\\n\\t*<#', '><', trim($match[2]));
 575  
 576              $fragments[$match[1]] = $fragment;
 577          }
 578  
 579          // Automatically recompose templates split between *_open and *_close
 580          foreach ($fragments as $fragment_name => $fragment)
 581          {
 582              if (preg_match('#^(\\w+)_close$#', $fragment_name, $match))
 583              {
 584                  $bbcode_name = $match[1];
 585  
 586                  if (isset($fragments[$bbcode_name . '_open']))
 587                  {
 588                      $templates[$bbcode_name] = $fragments[$bbcode_name . '_open'] . '<xsl:apply-templates/>' . $fragment;
 589                  }
 590              }
 591          }
 592  
 593          // Manually recompose and overwrite irregular templates
 594          $templates['list'] =
 595              '<xsl:choose>
 596                  <xsl:when test="not(@type)">
 597                      ' . $fragments['ulist_open_default'] . '<xsl:apply-templates/>' . $fragments['ulist_close'] . '
 598                  </xsl:when>
 599                  <xsl:when test="contains(\'upperlowerdecim\',substring(@type,1,5))">
 600                      ' . $fragments['olist_open'] . '<xsl:apply-templates/>' . $fragments['olist_close'] . '
 601                  </xsl:when>
 602                  <xsl:otherwise>
 603                      ' . $fragments['ulist_open'] . '<xsl:apply-templates/>' . $fragments['ulist_close'] . '
 604                  </xsl:otherwise>
 605              </xsl:choose>';
 606  
 607          $templates['li'] = $fragments['listitem'] . '<xsl:apply-templates/>' . $fragments['listitem_close'];
 608  
 609          // Replace the regular quote template with the extended quote template if available
 610          if (isset($fragments['quote_extended']))
 611          {
 612              $templates['quote'] = $fragments['quote_extended'];
 613          }
 614  
 615          // The [attachment] BBCode uses the inline_attachment template to output a comment that
 616          // is post-processed by parse_attachments()
 617          $templates['attachment'] = $fragments['inline_attachment_open'] . '<xsl:comment> ia<xsl:value-of select="@index"/> </xsl:comment><xsl:value-of select="@filename"/><xsl:comment> ia<xsl:value-of select="@index"/> </xsl:comment>' . $fragments['inline_attachment_close'];
 618  
 619          // Add fragments as templates
 620          foreach ($fragments as $fragment_name => $fragment)
 621          {
 622              if (preg_match('#^\\w+$#', $fragment_name))
 623              {
 624                  $templates[$fragment_name] = $fragment;
 625              }
 626          }
 627  
 628          // Keep only templates that are named after an existing BBCode
 629          $templates = array_intersect_key($templates, $this->default_definitions);
 630  
 631          return $templates;
 632      }
 633  
 634      /**
 635      * Merge the templates from any number of styles into one BBCode template
 636      *
 637      * When multiple templates are available for the same BBCode (because of multiple styles) we
 638      * merge them into a single template that uses an xsl:choose construct that determines which
 639      * style to use at rendering time.
 640      *
 641      * @param  array  $style_templates Associative array matching style_ids to their template
 642      * @return string
 643      */
 644  	protected function merge_templates(array $style_templates)
 645      {
 646          // Return the template as-is if there's only one style or all styles share the same template
 647          if (count(array_unique($style_templates)) === 1)
 648          {
 649              return end($style_templates);
 650          }
 651  
 652          // Group identical templates together
 653          $grouped_templates = array();
 654          foreach ($style_templates as $style_id => $style_template)
 655          {
 656              $grouped_templates[$style_template][] = '$STYLE_ID=' . $style_id;
 657          }
 658  
 659          // Sort templates by frequency descending
 660          $templates_cnt = array_map('sizeof', $grouped_templates);
 661          array_multisort($grouped_templates, $templates_cnt);
 662  
 663          // Remove the most frequent template from the list; It becomes the default
 664          reset($grouped_templates);
 665          $default_template = key($grouped_templates);
 666          unset($grouped_templates[$default_template]);
 667  
 668          // Build an xsl:choose switch
 669          $template = '<xsl:choose>';
 670          foreach ($grouped_templates as $style_template => $exprs)
 671          {
 672              $template .= '<xsl:when test="' . implode(' or ', $exprs) . '">' . $style_template . '</xsl:when>';
 673          }
 674          $template .= '<xsl:otherwise>' . $default_template . '</xsl:otherwise></xsl:choose>';
 675  
 676          return $template;
 677      }
 678  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1