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

base.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 6.40 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\textreparser;
015   
016  abstract class base implements reparser_interface
017  {
018      /**
019       * @var string The reparser name
020       */
021      protected $name;
022   
023      /**
024      * @var bool Whether to save changes to the database
025      */
026      protected $save_changes = true;
027   
028      /**
029      * {@inheritdoc}
030      */
031      abstract public function get_max_id();
032   
033      /**
034      * Return all records in given range
035      *
036      * @param  integer $min_id Lower bound
037      * @param  integer $max_id Upper bound
038      * @return array           Array of records
039      */
040      abstract protected function get_records_by_range($min_id, $max_id);
041   
042      /**
043      * {@inheritdoc}
044      */
045      abstract protected function save_record(array $record);
046   
047      /**
048      * Add fields to given record, if applicable
049      *
050      * The enable_* fields are not always saved to the database. Sometimes we need to guess their
051      * original value based on the text content or possibly other fields
052      *
053      * @param  array $record Original record
054      * @return array         Complete record
055      */
056      protected function add_missing_fields(array $record)
057      {
058          if (!isset($record['enable_bbcode'], $record['enable_smilies'], $record['enable_magic_url']))
059          {
060              if (isset($record['options']))
061              {
062                  $record += array(
063                      'enable_bbcode'    => (bool) ($record['options'] & OPTION_FLAG_BBCODE),
064                      'enable_smilies'   => (bool) ($record['options'] & OPTION_FLAG_SMILIES),
065                      'enable_magic_url' => (bool) ($record['options'] & OPTION_FLAG_LINKS),
066                  );
067              }
068              else
069              {
070                  $record += array(
071                      'enable_bbcode'    => $this->guess_bbcodes($record),
072                      'enable_smilies'   => $this->guess_smilies($record),
073                      'enable_magic_url' => $this->guess_magic_url($record),
074                  );
075              }
076          }
077   
078          // Those BBCodes are disabled based on context and user permissions and that value is never
079          // stored in the database. Here we test whether they were used in the original text.
080          $bbcodes = array('flash', 'img', 'quote', 'url');
081          foreach ($bbcodes as $bbcode)
082          {
083              $field_name = 'enable_' . $bbcode . '_bbcode';
084              $record[$field_name] = $this->guess_bbcode($record, $bbcode);
085          }
086   
087          // Magic URLs are tied to the URL BBCode, that's why if magic URLs are enabled we make sure
088          // that the URL BBCode is also enabled
089          if ($record['enable_magic_url'])
090          {
091              $record['enable_url_bbcode'] = true;
092          }
093   
094          return $record;
095      }
096   
097      /**
098       * Returns the name of the reparser
099       *
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></' . strtoupper($bbcode) . '>';
157              if (strpos($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  }
270