[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/textreparser/ -> base.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\textreparser;
  15  
  16  abstract class base implements reparser_interface
  17  {
  18      /**
  19       * @var string The reparser name
  20       */
  21      protected $name;
  22  
  23      /**
  24      * @var bool Whether to save changes to the database
  25      */
  26      protected $save_changes = true;
  27  
  28      /**
  29      * {@inheritdoc}
  30      */
  31      abstract public function get_max_id();
  32  
  33      /**
  34      * Return all records in given range
  35      *
  36      * @param  integer $min_id Lower bound
  37      * @param  integer $max_id Upper bound
  38      * @return array           Array of records
  39      */
  40      abstract protected function get_records_by_range($min_id, $max_id);
  41  
  42      /**
  43      * {@inheritdoc}
  44      */
  45      abstract protected function save_record(array $record);
  46  
  47      /**
  48      * Add fields to given record, if applicable
  49      *
  50      * The enable_* fields are not always saved to the database. Sometimes we need to guess their
  51      * original value based on the text content or possibly other fields
  52      *
  53      * @param  array $record Original record
  54      * @return array         Complete record
  55      */
  56  	protected function add_missing_fields(array $record)
  57      {
  58          if (!isset($record['enable_bbcode'], $record['enable_smilies'], $record['enable_magic_url']))
  59          {
  60              if (isset($record['options']))
  61              {
  62                  $record += array(
  63                      'enable_bbcode'    => (bool) ($record['options'] & OPTION_FLAG_BBCODE),
  64                      'enable_smilies'   => (bool) ($record['options'] & OPTION_FLAG_SMILIES),
  65                      'enable_magic_url' => (bool) ($record['options'] & OPTION_FLAG_LINKS),
  66                  );
  67              }
  68              else
  69              {
  70                  $record += array(
  71                      'enable_bbcode'    => $this->guess_bbcodes($record),
  72                      'enable_smilies'   => $this->guess_smilies($record),
  73                      'enable_magic_url' => $this->guess_magic_url($record),
  74                  );
  75              }
  76          }
  77  
  78          // Those BBCodes are disabled based on context and user permissions and that value is never
  79          // stored in the database. Here we test whether they were used in the original text.
  80          $bbcodes = array('flash', 'img', 'quote', 'url');
  81          foreach ($bbcodes as $bbcode)
  82          {
  83              $field_name = 'enable_' . $bbcode . '_bbcode';
  84              $record[$field_name] = $this->guess_bbcode($record, $bbcode);
  85          }
  86  
  87          // Magic URLs are tied to the URL BBCode, that's why if magic URLs are enabled we make sure
  88          // that the URL BBCode is also enabled
  89          if ($record['enable_magic_url'])
  90          {
  91              $record['enable_url_bbcode'] = true;
  92          }
  93  
  94          return $record;
  95      }
  96  
  97      /**
  98       * Returns the name of the reparser
  99       *
 100       * @return string Name of reparser
 101       */
 102  	public function get_name()
 103      {
 104          return $this->name;
 105      }
 106  
 107      /**
 108       * Sets the name of the reparser
 109       *
 110       * @param string $name The reparser name
 111       */
 112  	public function set_name($name)
 113      {
 114          $this->name = $name;
 115      }
 116  
 117      /**
 118      * Disable saving changes to the database
 119      */
 120  	public function disable_save()
 121      {
 122          $this->save_changes = false;
 123      }
 124  
 125      /**
 126      * Enable saving changes to the database
 127      */
 128  	public function enable_save()
 129      {
 130          $this->save_changes = true;
 131      }
 132  
 133      /**
 134      * Guess whether given BBCode is in use in given record
 135      *
 136      * @param  array  $record
 137      * @param  string $bbcode
 138      * @return bool
 139      */
 140  	protected function guess_bbcode(array $record, $bbcode)
 141      {
 142          if (!empty($record['bbcode_uid']))
 143          {
 144              // Look for the closing tag, e.g. [/url]
 145              $match = '[/' . $bbcode . ':' . $record['bbcode_uid'];
 146              if (strpos($record['text'], $match) !== false)
 147              {
 148                  return true;
 149              }
 150          }
 151  
 152          if (substr($record['text'], 0, 2) === '<r')
 153          {
 154              // Look for the closing tag inside of a e element, in an element of the same name, e.g.
 155              // <e>[/url]</e></URL>
 156              $match = '<e>[/' . $bbcode . ']</e></' . $bbcode . '>';
 157              if (stripos($record['text'], $match) !== false)
 158              {
 159                  return true;
 160              }
 161          }
 162  
 163          return false;
 164      }
 165  
 166      /**
 167      * Guess whether any BBCode is in use in given record
 168      *
 169      * @param  array $record
 170      * @return bool
 171      */
 172  	protected function guess_bbcodes(array $record)
 173      {
 174          if (!empty($record['bbcode_uid']))
 175          {
 176              // Test whether the bbcode_uid is in use
 177              $match = ':' . $record['bbcode_uid'];
 178              if (strpos($record['text'], $match) !== false)
 179              {
 180                  return true;
 181              }
 182          }
 183  
 184          if (substr($record['text'], 0, 2) === '<r')
 185          {
 186              // Look for a closing tag inside of an e element
 187              return (bool) preg_match('(<e>\\[/\\w+\\]</e>)', $match);
 188          }
 189  
 190          return false;
 191      }
 192  
 193      /**
 194      * Guess whether magic URLs are in use in given record
 195      *
 196      * @param  array $record
 197      * @return bool
 198      */
 199  	protected function guess_magic_url(array $record)
 200      {
 201          // Look for <!-- m --> or for a URL tag that's not immediately followed by <s>
 202          return (strpos($record['text'], '<!-- m -->') !== false || preg_match('(<URL [^>]++>(?!<s>))', $record['text']));
 203      }
 204  
 205      /**
 206      * Guess whether smilies are in use in given record
 207      *
 208      * @param  array $record
 209      * @return bool
 210      */
 211  	protected function guess_smilies(array $record)
 212      {
 213          return (strpos($record['text'], '<!-- s') !== false || strpos($record['text'], '<E>') !== false);
 214      }
 215  
 216      /**
 217      * {@inheritdoc}
 218      */
 219  	public function reparse_range($min_id, $max_id)
 220      {
 221          foreach ($this->get_records_by_range($min_id, $max_id) as $record)
 222          {
 223              $this->reparse_record($record);
 224          }
 225      }
 226  
 227      /**
 228      * Reparse given record
 229      *
 230      * @param array $record Associative array containing the record's data
 231      */
 232  	protected function reparse_record(array $record)
 233      {
 234          $record = $this->add_missing_fields($record);
 235          $flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0;
 236          $flags |= ($record['enable_smilies']) ? OPTION_FLAG_SMILIES : 0;
 237          $flags |= ($record['enable_magic_url']) ? OPTION_FLAG_LINKS : 0;
 238          $unparsed = array_merge(
 239              $record,
 240              generate_text_for_edit($record['text'], $record['bbcode_uid'], $flags)
 241          );
 242  
 243          // generate_text_for_edit() and decode_message() actually return the text as HTML. It has to
 244          // be decoded to plain text before it can be reparsed
 245          $text = html_entity_decode($unparsed['text'], ENT_QUOTES, 'UTF-8');
 246          $bitfield = $flags = null;
 247          generate_text_for_storage(
 248              $text,
 249              $unparsed['bbcode_uid'],
 250              $bitfield,
 251              $flags,
 252              $unparsed['enable_bbcode'],
 253              $unparsed['enable_magic_url'],
 254              $unparsed['enable_smilies'],
 255              $unparsed['enable_img_bbcode'],
 256              $unparsed['enable_flash_bbcode'],
 257              $unparsed['enable_quote_bbcode'],
 258              $unparsed['enable_url_bbcode'],
 259              'text_reparser.' . $this->get_name()
 260          );
 261  
 262          // Save the new text if it has changed and it's not a dry run
 263          if ($text !== $record['text'] && $this->save_changes)
 264          {
 265              $record['text'] = $text;
 266              $this->save_record($record);
 267          }
 268      }
 269  }


Generated: Sat Oct 26 11:28:42 2019 Cross-referenced by PHPXref 0.7.1