Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
data_access.php
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;
015
016 /**
017 * Data access layer that fetchs BBCodes, smilies and censored words from the database.
018 * To be extended to include insert/update/delete operations.
019 *
020 * Also used to get templates.
021 */
022 class data_access
023 {
024 /**
025 * @var string Name of the BBCodes table
026 */
027 protected $bbcodes_table;
028
029 /**
030 * @var \phpbb\db\driver\driver_interface
031 */
032 protected $db;
033
034 /**
035 * @var string Name of the smilies table
036 */
037 protected $smilies_table;
038
039 /**
040 * @var string Name of the styles table
041 */
042 protected $styles_table;
043
044 /**
045 * @var string Path to the styles dir
046 */
047 protected $styles_path;
048
049 /**
050 * @var string Name of the words table
051 */
052 protected $words_table;
053
054 /**
055 * Constructor
056 *
057 * @param \phpbb\db\driver\driver_interface $db Database connection
058 * @param string $bbcodes_table Name of the BBCodes table
059 * @param string $smilies_table Name of the smilies table
060 * @param string $styles_table Name of the styles table
061 * @param string $words_table Name of the words table
062 * @param string $styles_path Path to the styles dir
063 */
064 public function __construct(\phpbb\db\driver\driver_interface $db, $bbcodes_table, $smilies_table, $styles_table, $words_table, $styles_path)
065 {
066 $this->db = $db;
067
068 $this->bbcodes_table = $bbcodes_table;
069 $this->smilies_table = $smilies_table;
070 $this->styles_table = $styles_table;
071 $this->words_table = $words_table;
072
073 $this->styles_path = $styles_path;
074 }
075
076 /**
077 * Return the list of custom BBCodes
078 *
079 * @return array
080 */
081 public function get_bbcodes()
082 {
083 $sql = 'SELECT bbcode_match, bbcode_tpl FROM ' . $this->bbcodes_table;
084
085 return $this->fetch_decoded_rowset($sql, ['bbcode_match']);
086 }
087
088 /**
089 * Return the list of smilies
090 *
091 * @return array
092 */
093 public function get_smilies()
094 {
095 // NOTE: smilies that are displayed on the posting page are processed first because they're
096 // typically the most used smilies and it ends up producing a slightly more efficient
097 // renderer
098 $sql = 'SELECT code, emotion, smiley_url, smiley_width, smiley_height
099 FROM ' . $this->smilies_table . '
100 ORDER BY display_on_posting DESC';
101
102 return $this->fetch_decoded_rowset($sql, ['code', 'emotion', 'smiley_url']);
103 }
104
105 /**
106 * Return the list of installed styles
107 *
108 * @return array
109 */
110 protected function get_styles()
111 {
112 $sql = 'SELECT style_id, style_path, style_parent_id, bbcode_bitfield FROM ' . $this->styles_table;
113
114 return $this->fetch_decoded_rowset($sql);
115 }
116
117 /**
118 * Return the bbcode.html template for every installed style
119 *
120 * @return array 2D array. style_id as keys, each element is an array with a "template" element that contains the style's bbcode.html and a "bbcodes" element that contains the name of each BBCode that is to be stylised
121 */
122 public function get_styles_templates()
123 {
124 $templates = array();
125
126 $bbcode_ids = array(
127 'quote' => 0,
128 'b' => 1,
129 'i' => 2,
130 'url' => 3,
131 'img' => 4,
132 'size' => 5,
133 'color' => 6,
134 'u' => 7,
135 'code' => 8,
136 'list' => 9,
137 '*' => 9,
138 'email' => 10,
139 'flash' => 11,
140 'attachment' => 12,
141 );
142
143 $styles = array();
144 foreach ($this->get_styles() as $row)
145 {
146 $styles[$row['style_id']] = $row;
147 }
148
149 foreach ($styles as $style_id => $style)
150 {
151 $bbcodes = array();
152
153 // Collect the name of the BBCodes whose bit is set in the style's bbcode_bitfield
154 $template_bitfield = new \bitfield($style['bbcode_bitfield']);
155 foreach ($bbcode_ids as $bbcode_name => $bit)
156 {
157 if ($template_bitfield->get($bit))
158 {
159 $bbcodes[] = $bbcode_name;
160 }
161 }
162
163 $filename = $this->resolve_style_filename($styles, $style);
164 if ($filename === false)
165 {
166 // Ignore this style, it will use the default templates
167 continue;
168 }
169
170 $templates[$style_id] = array(
171 'bbcodes' => $bbcodes,
172 'template' => file_get_contents($filename),
173 );
174 }
175
176 return $templates;
177 }
178
179 /**
180 * Resolve inheritance for given style and return the path to their bbcode.html file
181 *
182 * @param array $styles Associative array of [style_id => style] containing all styles
183 * @param array $style Style for which we resolve
184 * @return string|bool Path to this style's bbcode.html, or FALSE
185 */
186 protected function resolve_style_filename(array $styles, array $style)
187 {
188 // Look for a bbcode.html in this style's dir
189 $filename = $this->styles_path . $style['style_path'] . '/template/bbcode.html';
190 if (file_exists($filename))
191 {
192 return $filename;
193 }
194
195 // Resolve using this style's parent
196 $parent_id = $style['style_parent_id'];
197 if ($parent_id && !empty($styles[$parent_id]))
198 {
199 return $this->resolve_style_filename($styles, $styles[$parent_id]);
200 }
201
202 return false;
203 }
204
205 /**
206 * Return the list of censored words
207 *
208 * @return array
209 */
210 public function get_censored_words()
211 {
212 $sql = 'SELECT word, replacement FROM ' . $this->words_table;
213
214 return $this->fetch_decoded_rowset($sql, ['word', 'replacement']);
215 }
216
217 /**
218 * Decode HTML special chars in given rowset
219 *
220 * @param array $rows Original rowset
221 * @param array $columns List of columns to decode
222 * @return array Decoded rowset
223 */
224 protected function decode_rowset(array $rows, array $columns)
225 {
226 foreach ($rows as &$row)
227 {
228 foreach ($columns as $column)
229 {
230 $row[$column] = html_entity_decode($row[$column], ENT_COMPAT);
231 }
232 }
233
234 return $rows;
235 }
236
237 /**
238 * Fetch all rows for given query and decode plain text columns
239 *
240 * @param string $sql SELECT query
241 * @param array $columns List of columns to decode
242 * @return array
243 */
244 protected function fetch_decoded_rowset($sql, array $columns = [])
245 {
246 $result = $this->db->sql_query($sql);
247 $rows = $this->db->sql_fetchrowset($result);
248 $this->db->sql_freeresult($result);
249
250 return $this->decode_rowset($rows, $columns);
251 }
252 }
253