Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
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 |
bbcode.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 /**
015 * @ignore
016 */
017 if (!defined('IN_PHPBB'))
018 {
019 exit;
020 }
021
022 /**
023 * BBCode class
024 */
025 class bbcode
026 {
027 var $bbcode_uid = '';
028 var $bbcode_bitfield = '';
029 var $bbcode_cache = array();
030 var $bbcode_template = array();
031
032 var $bbcodes = array();
033
034 var $template_bitfield;
035
036 /**
037 * Constructor
038 * Init bbcode cache entries if bitfield is specified
039 */
040 function bbcode($bitfield = '')
041 {
042 if ($bitfield)
043 {
044 $this->bbcode_bitfield = $bitfield;
045 $this->bbcode_cache_init();
046 }
047 }
048
049 /**
050 * Second pass bbcodes
051 */
052 function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = false)
053 {
054 if ($bbcode_uid)
055 {
056 $this->bbcode_uid = $bbcode_uid;
057 }
058
059 if ($bbcode_bitfield !== false)
060 {
061 $this->bbcode_bitfield = $bbcode_bitfield;
062
063 // Init those added with a new bbcode_bitfield (already stored codes will not get parsed again)
064 $this->bbcode_cache_init();
065 }
066
067 if (!$this->bbcode_bitfield)
068 {
069 // Remove the uid from tags that have not been transformed into HTML
070 if ($this->bbcode_uid)
071 {
072 $message = str_replace(':' . $this->bbcode_uid, '', $message);
073 }
074
075 return;
076 }
077
078 $str = array('search' => array(), 'replace' => array());
079 $preg = array('search' => array(), 'replace' => array());
080
081 $bitfield = new bitfield($this->bbcode_bitfield);
082 $bbcodes_set = $bitfield->get_all_set();
083
084 $undid_bbcode_specialchars = false;
085 foreach ($bbcodes_set as $bbcode_id)
086 {
087 if (!empty($this->bbcode_cache[$bbcode_id]))
088 {
089 foreach ($this->bbcode_cache[$bbcode_id] as $type => $array)
090 {
091 foreach ($array as $search => $replace)
092 {
093 ${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
094 ${$type}['replace'][] = $replace;
095 }
096
097 if (sizeof($str['search']))
098 {
099 $message = str_replace($str['search'], $str['replace'], $message);
100 $str = array('search' => array(), 'replace' => array());
101 }
102
103 if (sizeof($preg['search']))
104 {
105 // we need to turn the entities back into their original form to allow the
106 // search patterns to work properly
107 if (!$undid_bbcode_specialchars)
108 {
109 $message = str_replace(array(':', '.'), array(':', '.'), $message);
110 $undid_bbcode_specialchars = true;
111 }
112
113 $message = preg_replace($preg['search'], $preg['replace'], $message);
114 $preg = array('search' => array(), 'replace' => array());
115 }
116 }
117 }
118 }
119
120 // Remove the uid from tags that have not been transformed into HTML
121 $message = str_replace(':' . $this->bbcode_uid, '', $message);
122 }
123
124 /**
125 * Init bbcode cache
126 *
127 * requires: $this->bbcode_bitfield
128 * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
129 */
130 function bbcode_cache_init()
131 {
132 global $phpbb_root_path, $phpEx, $config, $user, $phpbb_extension_manager, $phpbb_path_helper;
133
134 if (empty($this->template_filename))
135 {
136 $this->template_bitfield = new bitfield($user->style['bbcode_bitfield']);
137
138 $template = new phpbb\template\twig\twig($phpbb_path_helper, $config, $user, new phpbb\template\context(), $phpbb_extension_manager);
139 $template->set_style();
140 $template->set_filenames(array('bbcode.html' => 'bbcode.html'));
141 $this->template_filename = $template->get_source_file_for_handle('bbcode.html');
142 }
143
144 $bbcode_ids = $rowset = $sql = array();
145
146 $bitfield = new bitfield($this->bbcode_bitfield);
147 $bbcodes_set = $bitfield->get_all_set();
148
149 foreach ($bbcodes_set as $bbcode_id)
150 {
151 if (isset($this->bbcode_cache[$bbcode_id]))
152 {
153 // do not try to re-cache it if it's already in
154 continue;
155 }
156 $bbcode_ids[] = $bbcode_id;
157
158 if ($bbcode_id > NUM_CORE_BBCODES)
159 {
160 $sql[] = $bbcode_id;
161 }
162 }
163
164 if (sizeof($sql))
165 {
166 global $db;
167
168 $sql = 'SELECT *
169 FROM ' . BBCODES_TABLE . '
170 WHERE ' . $db->sql_in_set('bbcode_id', $sql);
171 $result = $db->sql_query($sql, 3600);
172
173 while ($row = $db->sql_fetchrow($result))
174 {
175 // To circumvent replacing newlines with <br /> for the generated html,
176 // we use carriage returns here. They are later changed back to newlines
177 $row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']);
178 $row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']);
179
180 $rowset[$row['bbcode_id']] = $row;
181 }
182 $db->sql_freeresult($result);
183 }
184
185 foreach ($bbcode_ids as $bbcode_id)
186 {
187 switch ($bbcode_id)
188 {
189 case 0:
190 $this->bbcode_cache[$bbcode_id] = array(
191 'str' => array(
192 '[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id)
193 ),
194 'preg' => array(
195 '#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')"
196 )
197 );
198 break;
199
200 case 1:
201 $this->bbcode_cache[$bbcode_id] = array(
202 'str' => array(
203 '[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id),
204 '[/b:$uid]' => $this->bbcode_tpl('b_close', $bbcode_id),
205 )
206 );
207 break;
208
209 case 2:
210 $this->bbcode_cache[$bbcode_id] = array(
211 'str' => array(
212 '[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id),
213 '[/i:$uid]' => $this->bbcode_tpl('i_close', $bbcode_id),
214 )
215 );
216 break;
217
218 case 3:
219 $this->bbcode_cache[$bbcode_id] = array(
220 'preg' => array(
221 '#\[url:$uid\]((.*?))\[/url:$uid\]#s' => $this->bbcode_tpl('url', $bbcode_id),
222 '#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s' => $this->bbcode_tpl('url', $bbcode_id),
223 )
224 );
225 break;
226
227 case 4:
228 if ($user->optionget('viewimg'))
229 {
230 $this->bbcode_cache[$bbcode_id] = array(
231 'preg' => array(
232 '#\[img:$uid\](.*?)\[/img:$uid\]#s' => $this->bbcode_tpl('img', $bbcode_id),
233 )
234 );
235 }
236 else
237 {
238 $this->bbcode_cache[$bbcode_id] = array(
239 'preg' => array(
240 '#\[img:$uid\](.*?)\[/img:$uid\]#s' => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true)),
241 )
242 );
243 }
244 break;
245
246 case 5:
247 $this->bbcode_cache[$bbcode_id] = array(
248 'preg' => array(
249 '#\[size=([\-\+]?\d+):$uid\](.*?)\[/size:$uid\]#s' => $this->bbcode_tpl('size', $bbcode_id),
250 )
251 );
252 break;
253
254 case 6:
255 $this->bbcode_cache[$bbcode_id] = array(
256 'preg' => array(
257 '!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+):$uid\](.*?)\[/color:$uid\]!is' => $this->bbcode_tpl('color', $bbcode_id),
258 )
259 );
260 break;
261
262 case 7:
263 $this->bbcode_cache[$bbcode_id] = array(
264 'str' => array(
265 '[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id),
266 '[/u:$uid]' => $this->bbcode_tpl('u_close', $bbcode_id),
267 )
268 );
269 break;
270
271 case 8:
272 $this->bbcode_cache[$bbcode_id] = array(
273 'preg' => array(
274 '#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')",
275 )
276 );
277 break;
278
279 case 9:
280 $this->bbcode_cache[$bbcode_id] = array(
281 'preg' => array(
282 '#(\[\/?(list|\*):[mou]?:?$uid\])[\n]{1}#' => "\$1",
283 '#(\[list=([^\[]+):$uid\])[\n]{1}#' => "\$1",
284 '#\[list=([^\[]+):$uid\]#e' => "\$this->bbcode_list('\$1')",
285 ),
286 'str' => array(
287 '[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id),
288 '[/list:u:$uid]' => $this->bbcode_tpl('ulist_close', $bbcode_id),
289 '[/list:o:$uid]' => $this->bbcode_tpl('olist_close', $bbcode_id),
290 '[*:$uid]' => $this->bbcode_tpl('listitem', $bbcode_id),
291 '[/*:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id),
292 '[/*:m:$uid]' => $this->bbcode_tpl('listitem_close', $bbcode_id)
293 ),
294 );
295 break;
296
297 case 10:
298 $this->bbcode_cache[$bbcode_id] = array(
299 'preg' => array(
300 '#\[email:$uid\]((.*?))\[/email:$uid\]#is' => $this->bbcode_tpl('email', $bbcode_id),
301 '#\[email=([^\[]+):$uid\](.*?)\[/email:$uid\]#is' => $this->bbcode_tpl('email', $bbcode_id)
302 )
303 );
304 break;
305
306 case 11:
307 if ($user->optionget('viewflash'))
308 {
309 $this->bbcode_cache[$bbcode_id] = array(
310 'preg' => array(
311 '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#' => $this->bbcode_tpl('flash', $bbcode_id),
312 )
313 );
314 }
315 else
316 {
317 $this->bbcode_cache[$bbcode_id] = array(
318 'preg' => array(
319 '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#' => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true)))
320 )
321 );
322 }
323 break;
324
325 case 12:
326 $this->bbcode_cache[$bbcode_id] = array(
327 'str' => array(
328 '[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id)
329 ),
330 'preg' => array(
331 '#\[attachment=([0-9]+):$uid\]#' => $this->bbcode_tpl('inline_attachment_open', $bbcode_id)
332 )
333 );
334 break;
335
336 default:
337 if (isset($rowset[$bbcode_id]))
338 {
339 if ($this->template_bitfield->get($bbcode_id))
340 {
341 // The bbcode requires a custom template to be loaded
342 if (!$bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))
343 {
344 // For some reason, the required template seems not to be available, use the default template
345 $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
346 }
347 else
348 {
349 // In order to use templates with custom bbcodes we need
350 // to replace all {VARS} to corresponding backreferences
351 // Note that backreferences are numbered from bbcode_match
352 if (preg_match_all('/\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|INTTEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\}/', $rowset[$bbcode_id]['bbcode_match'], $m))
353 {
354 foreach ($m[0] as $i => $tok)
355 {
356 $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
357 }
358 }
359 }
360 }
361 else
362 {
363 // Default template
364 $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
365 }
366
367 // Replace {L_*} lang strings
368 $bbcode_tpl = preg_replace('/{L_([A-Z0-9_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl);
369
370 if (!empty($rowset[$bbcode_id]['second_pass_replace']))
371 {
372 // The custom BBCode requires second-pass pattern replacements
373 $this->bbcode_cache[$bbcode_id] = array(
374 'preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
375 );
376 }
377 else
378 {
379 $this->bbcode_cache[$bbcode_id] = array(
380 'str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
381 );
382 }
383 }
384 else
385 {
386 $this->bbcode_cache[$bbcode_id] = false;
387 }
388 break;
389 }
390 }
391 }
392
393 /**
394 * Return bbcode template
395 */
396 function bbcode_tpl($tpl_name, $bbcode_id = -1, $skip_bitfield_check = false)
397 {
398 static $bbcode_hardtpl = array();
399 if (empty($bbcode_hardtpl))
400 {
401 global $user;
402
403 $bbcode_hardtpl = array(
404 'b_open' => '<span style="font-weight: bold">',
405 'b_close' => '</span>',
406 'i_open' => '<span style="font-style: italic">',
407 'i_close' => '</span>',
408 'u_open' => '<span style="text-decoration: underline">',
409 'u_close' => '</span>',
410 'img' => '<img src="$1" class="postimage" alt="' . $user->lang['IMAGE'] . '" />',
411 'size' => '<span style="font-size: $1%; line-height: normal">$2</span>',
412 'color' => '<span style="color: $1">$2</span>',
413 'email' => '<a href="mailto:$1">$2</a>'
414 );
415 }
416
417 if ($bbcode_id != -1 && !$skip_bitfield_check && !$this->template_bitfield->get($bbcode_id))
418 {
419 return (isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false;
420 }
421
422 if (empty($this->bbcode_template))
423 {
424 if (($tpl = file_get_contents($this->template_filename)) === false)
425 {
426 trigger_error('Could not load bbcode template', E_USER_ERROR);
427 }
428
429 // replace \ with \\ and then ' with \'.
430 $tpl = str_replace('\\', '\\\\', $tpl);
431 $tpl = str_replace("'", "\'", $tpl);
432
433 // strip newlines and indent
434 $tpl = preg_replace("/\n[\n\r\s\t]*/", '', $tpl);
435
436 // Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
437 $this->bbcode_template = array();
438
439 $matches = preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match);
440
441 for ($i = 0; $i < $matches; $i++)
442 {
443 if (empty($match[1][$i]))
444 {
445 continue;
446 }
447
448 $this->bbcode_template[$match[1][$i]] = $this->bbcode_tpl_replace($match[1][$i], $match[2][$i]);
449 }
450 }
451
452 return (isset($this->bbcode_template[$tpl_name])) ? $this->bbcode_template[$tpl_name] : ((isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false);
453 }
454
455 /**
456 * Return bbcode template replacement
457 */
458 function bbcode_tpl_replace($tpl_name, $tpl)
459 {
460 global $user;
461
462 static $replacements = array(
463 'quote_username_open' => array('{USERNAME}' => '$1'),
464 'color' => array('{COLOR}' => '$1', '{TEXT}' => '$2'),
465 'size' => array('{SIZE}' => '$1', '{TEXT}' => '$2'),
466 'img' => array('{URL}' => '$1'),
467 'flash' => array('{WIDTH}' => '$1', '{HEIGHT}' => '$2', '{URL}' => '$3'),
468 'url' => array('{URL}' => '$1', '{DESCRIPTION}' => '$2'),
469 'email' => array('{EMAIL}' => '$1', '{DESCRIPTION}' => '$2')
470 );
471
472 $tpl = preg_replace('/{L_([A-Z0-9_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $tpl);
473
474 if (!empty($replacements[$tpl_name]))
475 {
476 $tpl = strtr($tpl, $replacements[$tpl_name]);
477 }
478
479 return trim($tpl);
480 }
481
482 /**
483 * Second parse list bbcode
484 */
485 function bbcode_list($type)
486 {
487 if ($type == '')
488 {
489 $tpl = 'ulist_open_default';
490 $type = 'default';
491 }
492 else if ($type == 'i')
493 {
494 $tpl = 'olist_open';
495 $type = 'lower-roman';
496 }
497 else if ($type == 'I')
498 {
499 $tpl = 'olist_open';
500 $type = 'upper-roman';
501 }
502 else if (preg_match('#^(disc|circle|square)$#i', $type))
503 {
504 $tpl = 'ulist_open';
505 $type = strtolower($type);
506 }
507 else if (preg_match('#^[a-z]$#', $type))
508 {
509 $tpl = 'olist_open';
510 $type = 'lower-alpha';
511 }
512 else if (preg_match('#[A-Z]#', $type))
513 {
514 $tpl = 'olist_open';
515 $type = 'upper-alpha';
516 }
517 else if (is_numeric($type))
518 {
519 $tpl = 'olist_open';
520 $type = 'decimal';
521 }
522 else
523 {
524 $tpl = 'olist_open';
525 $type = 'decimal';
526 }
527
528 return str_replace('{LIST_TYPE}', $type, $this->bbcode_tpl($tpl));
529 }
530
531 /**
532 * Second parse quote tag
533 */
534 function bbcode_second_pass_quote($username, $quote)
535 {
536 // when using the /e modifier, preg_replace slashes double-quotes but does not
537 // seem to slash anything else
538 $quote = str_replace('\"', '"', $quote);
539 $username = str_replace('\"', '"', $username);
540
541 // remove newline at the beginning
542 if ($quote == "\n")
543 {
544 $quote = '';
545 }
546
547 $quote = (($username) ? str_replace('$1', $username, $this->bbcode_tpl('quote_username_open')) : $this->bbcode_tpl('quote_open')) . $quote;
548
549 return $quote;
550 }
551
552 /**
553 * Second parse code tag
554 */
555 function bbcode_second_pass_code($type, $code)
556 {
557 // when using the /e modifier, preg_replace slashes double-quotes but does not
558 // seem to slash anything else
559 $code = str_replace('\"', '"', $code);
560
561 switch ($type)
562 {
563 case 'php':
564 // Not the english way, but valid because of hardcoded syntax highlighting
565 if (strpos($code, '<span class="syntaxdefault"><br /></span>') === 0)
566 {
567 $code = substr($code, 41);
568 }
569
570 // no break;
571
572 default:
573 $code = str_replace("\t", ' ', $code);
574 $code = str_replace(' ', ' ', $code);
575 $code = str_replace(' ', ' ', $code);
576 $code = str_replace("\n ", "\n ", $code);
577
578 // keep space at the beginning
579 if (!empty($code) && $code[0] == ' ')
580 {
581 $code = ' ' . substr($code, 1);
582 }
583
584 // remove newline at the beginning
585 if (!empty($code) && $code[0] == "\n")
586 {
587 $code = substr($code, 1);
588 }
589 break;
590 }
591
592 $code = $this->bbcode_tpl('code_open') . $code . $this->bbcode_tpl('code_close');
593
594 return $code;
595 }
596 }
597