diff options
Diffstat (limited to 'phpBB/includes/bbcode.php')
-rw-r--r-- | phpBB/includes/bbcode.php | 109 |
1 files changed, 83 insertions, 26 deletions
diff --git a/phpBB/includes/bbcode.php b/phpBB/includes/bbcode.php index 86390c0901..c31b63a403 100644 --- a/phpBB/includes/bbcode.php +++ b/phpBB/includes/bbcode.php @@ -35,9 +35,18 @@ class bbcode /** * Constructor + */ + function __construct($bitfield = '') + { + $this->bbcode_set_bitfield($bitfield); + } + + /** * Init bbcode cache entries if bitfield is specified + * + * @param string $bbcode_bitfield The bbcode bitfield */ - function bbcode($bitfield = '') + function bbcode_set_bitfield($bitfield = '') { if ($bitfield) { @@ -94,13 +103,13 @@ class bbcode ${$type}['replace'][] = $replace; } - if (sizeof($str['search'])) + if (count($str['search'])) { $message = str_replace($str['search'], $str['replace'], $message); $str = array('search' => array(), 'replace' => array()); } - if (sizeof($preg['search'])) + if (count($preg['search'])) { // we need to turn the entities back into their original form to allow the // search patterns to work properly @@ -110,7 +119,18 @@ class bbcode $undid_bbcode_specialchars = true; } - $message = preg_replace($preg['search'], $preg['replace'], $message); + foreach ($preg['search'] as $key => $search) + { + if (is_callable($preg['replace'][$key])) + { + $message = preg_replace_callback($search, $preg['replace'][$key], $message); + } + else + { + $message = preg_replace($search, $preg['replace'][$key], $message); + } + } + $preg = array('search' => array(), 'replace' => array()); } } @@ -129,13 +149,32 @@ class bbcode */ function bbcode_cache_init() { - global $phpbb_root_path, $phpEx, $config, $user, $phpbb_dispatcher, $phpbb_extension_manager, $phpbb_path_helper; + global $user, $phpbb_dispatcher, $phpbb_extension_manager, $phpbb_container, $phpbb_filesystem; if (empty($this->template_filename)) { $this->template_bitfield = new bitfield($user->style['bbcode_bitfield']); - $template = new phpbb\template\twig\twig($phpbb_path_helper, $config, $user, new phpbb\template\context(), $phpbb_extension_manager); + $template = new \phpbb\template\twig\twig( + $phpbb_container->get('path_helper'), + $phpbb_container->get('config'), + new \phpbb\template\context(), + new \phpbb\template\twig\environment( + $phpbb_container->get('config'), + $phpbb_container->get('filesystem'), + $phpbb_container->get('path_helper'), + $phpbb_container->getParameter('core.cache_dir'), + $phpbb_container->get('ext.manager'), + new \phpbb\template\twig\loader( + $phpbb_filesystem + ) + ), + $phpbb_container->getParameter('core.cache_dir'), + $phpbb_container->get('user'), + $phpbb_container->get('template.twig.extensions.collection'), + $phpbb_extension_manager + ); + $template->set_style(); $template->set_filenames(array('bbcode.html' => 'bbcode.html')); $this->template_filename = $template->get_source_file_for_handle('bbcode.html'); @@ -161,7 +200,7 @@ class bbcode } } - if (sizeof($sql)) + if (count($sql)) { global $db; @@ -188,18 +227,25 @@ class bbcode { switch ($bbcode_id) { - case 0: + case BBCODE_ID_QUOTE: $this->bbcode_cache[$bbcode_id] = array( 'str' => array( '[/quote:$uid]' => $this->bbcode_tpl('quote_close', $bbcode_id) ), 'preg' => array( - '#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise' => "\$this->bbcode_second_pass_quote('\$1', '\$2')" + '#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#is' => function ($match) { + if (!isset($match[2])) + { + $match[2] = ''; + } + + return $this->bbcode_second_pass_quote($match[1], $match[2]); + }, ) ); break; - case 1: + case BBCODE_ID_B: $this->bbcode_cache[$bbcode_id] = array( 'str' => array( '[b:$uid]' => $this->bbcode_tpl('b_open', $bbcode_id), @@ -208,7 +254,7 @@ class bbcode ); break; - case 2: + case BBCODE_ID_I: $this->bbcode_cache[$bbcode_id] = array( 'str' => array( '[i:$uid]' => $this->bbcode_tpl('i_open', $bbcode_id), @@ -217,7 +263,7 @@ class bbcode ); break; - case 3: + case BBCODE_ID_URL: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( '#\[url:$uid\]((.*?))\[/url:$uid\]#s' => $this->bbcode_tpl('url', $bbcode_id), @@ -226,7 +272,7 @@ class bbcode ); break; - case 4: + case BBCODE_ID_IMG: if ($user->optionget('viewimg')) { $this->bbcode_cache[$bbcode_id] = array( @@ -245,7 +291,7 @@ class bbcode } break; - case 5: + case BBCODE_ID_SIZE: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( '#\[size=([\-\+]?\d+):$uid\](.*?)\[/size:$uid\]#s' => $this->bbcode_tpl('size', $bbcode_id), @@ -253,7 +299,7 @@ class bbcode ); break; - case 6: + case BBCODE_ID_COLOR: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( '!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+):$uid\](.*?)\[/color:$uid\]!is' => $this->bbcode_tpl('color', $bbcode_id), @@ -261,7 +307,7 @@ class bbcode ); break; - case 7: + case BBCODE_ID_U: $this->bbcode_cache[$bbcode_id] = array( 'str' => array( '[u:$uid]' => $this->bbcode_tpl('u_open', $bbcode_id), @@ -270,20 +316,24 @@ class bbcode ); break; - case 8: + case BBCODE_ID_CODE: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( - '#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#ise' => "\$this->bbcode_second_pass_code('\$1', '\$2')", + '#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#is' => function ($match) { + return $this->bbcode_second_pass_code($match[1], $match[2]); + }, ) ); break; - case 9: + case BBCODE_ID_LIST: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( '#(\[\/?(list|\*):[mou]?:?$uid\])[\n]{1}#' => "\$1", '#(\[list=([^\[]+):$uid\])[\n]{1}#' => "\$1", - '#\[list=([^\[]+):$uid\]#e' => "\$this->bbcode_list('\$1')", + '#\[list=([^\[]+):$uid\]#' => function ($match) { + return $this->bbcode_list($match[1]); + }, ), 'str' => array( '[list:$uid]' => $this->bbcode_tpl('ulist_open_default', $bbcode_id), @@ -296,7 +346,7 @@ class bbcode ); break; - case 10: + case BBCODE_ID_EMAIL: $this->bbcode_cache[$bbcode_id] = array( 'preg' => array( '#\[email:$uid\]((.*?))\[/email:$uid\]#is' => $this->bbcode_tpl('email', $bbcode_id), @@ -305,7 +355,7 @@ class bbcode ); break; - case 11: + case BBCODE_ID_FLASH: if ($user->optionget('viewflash')) { $this->bbcode_cache[$bbcode_id] = array( @@ -324,7 +374,7 @@ class bbcode } break; - case 12: + case BBCODE_ID_ATTACH: $this->bbcode_cache[$bbcode_id] = array( 'str' => array( '[/attachment:$uid]' => $this->bbcode_tpl('inline_attachment_close', $bbcode_id) @@ -367,7 +417,9 @@ class bbcode } // Replace {L_*} lang strings - $bbcode_tpl = preg_replace('/{L_([A-Z0-9_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl); + $bbcode_tpl = preg_replace_callback('/{L_([A-Z0-9_]+)}/', function ($match) use ($user) { + return (!empty($user->lang[$match[1]])) ? $user->lang($match[1]) : ucwords(strtolower(str_replace('_', ' ', $match[1]))); + }, $bbcode_tpl); if (!empty($rowset[$bbcode_id]['second_pass_replace'])) { @@ -458,7 +510,10 @@ class bbcode // Turn template blocks into PHP assignment statements for the values of $bbcode_tpl.. $this->bbcode_template = array(); - $matches = preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match); + // Capture the BBCode template matches + // Allow phpBB template or the Twig syntax + $matches = (preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match)) ?: + preg_match_all('#{% for (.*?) in .*? %}(.*?){% endfor %}#s', $tpl, $match); for ($i = 0; $i < $matches; $i++) { @@ -491,7 +546,9 @@ class bbcode 'email' => array('{EMAIL}' => '$1', '{DESCRIPTION}' => '$2') ); - $tpl = preg_replace('/{L_([A-Z0-9_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $tpl); + $tpl = preg_replace_callback('/{L_([A-Z0-9_]+)}/', function ($match) use ($user) { + return (!empty($user->lang[$match[1]])) ? $user->lang($match[1]) : ucwords(strtolower(str_replace('_', ' ', $match[1]))); + }, $tpl); if (!empty($replacements[$tpl_name])) { |