aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--phpBB/includes/bbcode.php784
-rw-r--r--phpBB/includes/functions_posting.php7
-rw-r--r--phpBB/includes/message_parser.php219
-rw-r--r--phpBB/posting.php21
-rw-r--r--phpBB/viewtopic.php4
5 files changed, 472 insertions, 563 deletions
diff --git a/phpBB/includes/bbcode.php b/phpBB/includes/bbcode.php
index fc977093f5..47106edb09 100644
--- a/phpBB/includes/bbcode.php
+++ b/phpBB/includes/bbcode.php
@@ -19,597 +19,275 @@
*
***************************************************************************/
-if ( !defined('IN_PHPBB') )
+class bbcode
{
- die('Hacking attempt');
-}
-
-define('BBCODE_UID_LEN', 10);
-
-// global that holds loaded-and-prepared bbcode templates, so we only have to do
-// that stuff once.
-
-$bbcode_tpl = null;
-
-/**
- * Loads bbcode templates from the bbcode.tpl file of the current template set.
- * Creates an array, keys are bbcode names like "b_open" or "url", values
- * are the associated template.
- * Probably pukes all over the place if there's something really screwed
- * with the bbcode.tpl file.
- *
- * Nathan Codding, Sept 26 2001.
- */
-function load_bbcode_template()
-{
- global $template;
- $tpl_filename = $template->make_filename('bbcode.html');
- $tpl = fread(fopen($tpl_filename, 'r'), filesize($tpl_filename));
-
- // replace \ with \\ and then ' with \'.
- $tpl = str_replace('\\', '\\\\', $tpl);
- $tpl = str_replace('\'', '\\\'', $tpl);
-
- // strip newlines.
- $tpl = str_replace("\n", '', $tpl);
-
- // Turn template blocks into PHP assignment statements for the values of $bbcode_tpls..
- $tpl = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END (.*?) -->#', "\n" . '$bbcode_tpls[\'\\1\'] = \'\\2\';', $tpl);
-
- $bbcode_tpls = array();
-
- eval($tpl);
-
- return $bbcode_tpls;
-}
-
-
-/**
- * Prepares the loaded bbcode templates for insertion into preg_replace()
- * or str_replace() calls in the bbencode_second_pass functions. This
- * means replacing template placeholders with the appropriate preg backrefs
- * or with language vars. NOTE: If you change how the regexps work in
- * bbencode_second_pass(), you MUST change this function.
- *
- * Nathan Codding, Sept 26 2001
- *
- */
-function prepare_bbcode_template($bbcode_tpl)
-{
- global $lang;
-
- $bbcode_tpl['olist_open'] = str_replace('{LIST_TYPE}', '\\1', $bbcode_tpl['olist_open']);
-
- $bbcode_tpl['color_open'] = str_replace('{COLOR}', '\\1', $bbcode_tpl['color_open']);
-
- $bbcode_tpl['size_open'] = str_replace('{SIZE}', '\\1', $bbcode_tpl['size_open']);
-
- $bbcode_tpl['quote_open'] = str_replace('{L_QUOTE}', $lang['Quote'], $bbcode_tpl['quote_open']);
-
- $bbcode_tpl['quote_username_open'] = str_replace('{L_QUOTE}', $lang['Quote'], $bbcode_tpl['quote_username_open']);
- $bbcode_tpl['quote_username_open'] = str_replace('{L_WROTE}', $lang['wrote'], $bbcode_tpl['quote_username_open']);
- $bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $bbcode_tpl['quote_username_open']);
-
- $bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']);
-
- $bbcode_tpl['img'] = str_replace('{URL}', '\\1', $bbcode_tpl['img']);
-
- // We do URLs in several different ways..
- $bbcode_tpl['url1'] = str_replace('{URL}', '\\1\\2', $bbcode_tpl['url']);
- $bbcode_tpl['url1'] = str_replace('{DESCRIPTION}', '\\1\\2', $bbcode_tpl['url1']);
-
- $bbcode_tpl['url2'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
- $bbcode_tpl['url2'] = str_replace('{DESCRIPTION}', '\\1', $bbcode_tpl['url2']);
-
- $bbcode_tpl['url3'] = str_replace('{URL}', '\\1\\2', $bbcode_tpl['url']);
- $bbcode_tpl['url3'] = str_replace('{DESCRIPTION}', '\\3', $bbcode_tpl['url3']);
-
- $bbcode_tpl['url4'] = str_replace('{URL}', 'http://\\1', $bbcode_tpl['url']);
- $bbcode_tpl['url4'] = str_replace('{DESCRIPTION}', '\\2', $bbcode_tpl['url4']);
-
- $bbcode_tpl['email'] = str_replace('{EMAIL}', '\\1', $bbcode_tpl['email']);
-
- define('BBCODE_TPL_READY', true);
-
- return $bbcode_tpl;
-}
-
-
-/**
- * Does second-pass bbencoding. This should be used before displaying the message in
- * a thread. Assumes the message is already first-pass encoded, and we are given the
- * correct UID as used in first-pass encoding.
- */
-function bbencode_second_pass($text, $uid, $enable_img = true)
-{
- global $acl, $config, $lang, $bbcode_tpl;
-
- // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
- // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
- $text = ' ' . $text;
-
- // First: If there isn't a "[" and a "]" in the message, don't bother.
- if (! (strpos($text, '[') && strpos($text, ']')) )
- {
- // Remove padding, return.
- $text = substr($text, 1);
- return $text;
- }
-
- // Only load the templates ONCE..
- if (!defined('BBCODE_TPL_READY'))
- {
- // load templates from file into array.
- $bbcode_tpl = load_bbcode_template();
-
- // prepare array for use in regexps.
- $bbcode_tpl = prepare_bbcode_template($bbcode_tpl);
- }
-
- // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
- $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
-
- // [list] and [list=x] for (un)ordered lists.
- // unordered lists
- $text = str_replace("[list:$uid]", $bbcode_tpl['ulist_open'], $text);
- // li tags
- $text = str_replace("[*:$uid]", $bbcode_tpl['listitem'], $text);
- // ending tags
- $text = str_replace("[/list:u:$uid]", $bbcode_tpl['ulist_close'], $text);
- $text = str_replace("[/list:o:$uid]", $bbcode_tpl['olist_close'], $text);
- // Ordered lists
- $text = preg_replace("/\[list=([a1]):$uid\]/si", $bbcode_tpl['olist_open'], $text);
-
- // colours
- $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", $bbcode_tpl['color_open'], $text);
- $text = str_replace("[/color:$uid]", $bbcode_tpl['color_close'], $text);
-
- // size
- $text = preg_replace("/\[size=([\-\+]?[1-2]?[0-9]):$uid\]/si", $bbcode_tpl['size_open'], $text);
- $text = str_replace("[/size:$uid]", $bbcode_tpl['size_close'], $text);
-
- // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
- $text = str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);
- $text = str_replace("[/quote:$uid]", $bbcode_tpl['quote_close'], $text);
-
- // New one liner to deal with opening quotes with usernames...
- // replaces the two line version that I had here before..
- $text = preg_replace("/\[quote:$uid=(?:\"?([^\"]*)\"?)\]/si", $bbcode_tpl['quote_username_open'], $text);
-
- // [b] and [/b] for bolding text.
- $text = str_replace("[b:$uid]", $bbcode_tpl['b_open'], $text);
- $text = str_replace("[/b:$uid]", $bbcode_tpl['b_close'], $text);
-
- // [u] and [/u] for underlining text.
- $text = str_replace("[u:$uid]", $bbcode_tpl['u_open'], $text);
- $text = str_replace("[/u:$uid]", $bbcode_tpl['u_close'], $text);
-
- // [i] and [/i] for italicizing text.
- $text = str_replace("[i:$uid]", $bbcode_tpl['i_open'], $text);
- $text = str_replace("[/i:$uid]", $bbcode_tpl['i_close'], $text);
-
- // Patterns and replacements for URL and email tags..
- $patterns = array();
- $replacements = array();
-
- // [img]image_url_here[/img] code..
- // This one gets first-passed..
- if ( $enable_img )
- {
- $patterns[0] = "#\[img:$uid\](.*?)\[/img:$uid\]#si";
- $replacements[0] = $bbcode_tpl['img'];
- }
-
- // [email]user@domain.tld[/email] code..
- $patterns[5] = "#\[email\]([a-z0-9\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
- $replacements[5] = $bbcode_tpl['email'];
-
- $text = preg_replace($patterns, $replacements, $text);
-
- // Remove our padding from the string..
- $text = substr($text, 1);
-
- return $text;
-
-} // bbencode_second_pass()
-
-
-function make_bbcode_uid()
-{
- // Need to initialize the random numbers only ONCE
- mt_srand( (double) microtime() * 1000000);
-
- // Unique ID for this message..
- $uid = md5(mt_rand());
- $uid = substr($uid, 0, BBCODE_UID_LEN);
-
- return $uid;
-}
-
-function bbencode_first_pass($text, $uid)
-{
- // pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
- // This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
- $text = ' ' . $text;
-
- // [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
- $text = bbencode_first_pass_pda($text, $uid, '[code]', '[/code]', '', true, '');
-
- // [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
- $text = bbencode_first_pass_pda($text, $uid, '[quote]', '[/quote]', '', false, '');
-
- $text = bbencode_first_pass_pda($text, $uid, '/\[quote=(\\\\"[^"]*?\\\\")\]/is', '[/quote]', '', false, '', "[quote:$uid=\\1]");
-
- // [list] and [list=x] for (un)ordered lists.
- $open_tag = array();
- $open_tag[0] = '[list]';
-
- // unordered..
- $text = bbencode_first_pass_pda($text, $uid, $open_tag, '[/list]', '[/list:u]', false, 'replace_listitems');
-
- $open_tag[0] = '[list=1]';
- $open_tag[1] = '[list=a]';
-
- // ordered.
- $text = bbencode_first_pass_pda($text, $uid, $open_tag, '[/list]', '[/list:o]', false, 'replace_listitems');
-
- // [color] and [/color] for setting text color
- $text = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "[color=\\1:$uid]\\2[/color:$uid]", $text);
-
- // [size] and [/size] for setting text size
- $text = preg_replace("#\[size=([\-\+]?[1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text);
-
- // [b] and [/b] for bolding text.
- $text = preg_replace("#\[b\](.*?)\[/b\]#si", "[b:$uid]\\1[/b:$uid]", $text);
-
- // [u] and [/u] for underlining text.
- $text = preg_replace("#\[u\](.*?)\[/u\]#si", "[u:$uid]\\1[/u:$uid]", $text);
-
- // [i] and [/i] for italicizing text.
- $text = preg_replace("#\[i\](.*?)\[/i\]#si", "[i:$uid]\\1[/i:$uid]", $text);
-
- // [img]image_url_here[/img] code..
- $text = preg_replace("#\[img\](([a-z]+?)://([^ \"\n\r]+?))\[/img\]#si", "[img:$uid]\\1[/img:$uid]", $text);
-
- // Remove our padding from the string..
- $text = substr($text, 1);
-
- return $text;
+ var $bbcode_uid = '';
+ var $bbcode_bitfield = 0;
+ var $bbcode_cache = array();
-} // bbencode_first_pass()
-
-/**
- * $text - The text to operate on.
- * $uid - The UID to add to matching tags.
- * $open_tag - The opening tag to match. Can be an array of opening tags.
- * $close_tag - The closing tag to match.
- * $close_tag_new - The closing tag to replace with.
- * $mark_lowest_level - boolean - should we specially mark the tags that occur
- * at the lowest level of nesting? (useful for [code], because
- * we need to match these tags first and transform HTML tags
- * in their contents..
- * $func - This variable should contain a string that is the name of a function.
- * That function will be called when a match is found, and passed 2
- * parameters: ($text, $uid). The function should return a string.
- * This is used when some transformation needs to be applied to the
- * text INSIDE a pair of matching tags. If this variable is FALSE or the
- * empty string, it will not be executed.
- * If open_tag is an array, then the pda will try to match pairs consisting of
- * any element of open_tag followed by close_tag. This allows us to match things
- * like [list=A]...[/list] and [list=1]...[/list] in one pass of the PDA.
- *
- * NOTES: - this function assumes the first character of $text is a space.
- * - every opening tag and closing tag must be of the [...] format.
- */
-function bbencode_first_pass_pda($text, $uid, $open_tag, $close_tag, $close_tag_new, $mark_lowest_level, $func, $open_regexp_replace = false)
-{
- $open_tag_count = 0;
-
- if (!$close_tag_new || ($close_tag_new == ''))
+ function bbcode($uid = '', $bitfield = 0)
{
- $close_tag_new = $close_tag;
+ $this->bbcode_uid = $uid;
+ $this->bbcode_bitfield = $bitfield;
+ //$this->bbcode_cache_init();
}
- $close_tag_length = strlen($close_tag);
- $close_tag_new_length = strlen($close_tag_new);
- $uid_length = strlen($uid);
-
- $use_function_pointer = ($func && ($func != ''));
-
- $stack = array();
-
- if (is_array($open_tag))
+ function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = '')
{
- if (0 == count($open_tag))
+ if (empty($this->bbcode_cache))
{
- // No opening tags to match, so return.
- return $text;
+ $this->bbcode_cache_init();
}
- $open_tag_count = count($open_tag);
- }
- else
- {
- // only one opening tag. make it into a 1-element array.
- $open_tag_temp = $open_tag;
- $open_tag = array();
- $open_tag[0] = $open_tag_temp;
- $open_tag_count = 1;
- }
-
- $open_is_regexp = false;
-
- if ($open_regexp_replace)
- {
- $open_is_regexp = true;
- if (!is_array($open_regexp_replace))
+ if ($bbcode_uid)
{
- $open_regexp_temp = $open_regexp_replace;
- $open_regexp_replace = array();
- $open_regexp_replace[0] = $open_regexp_temp;
+ $this->bbcode_uid = $bbcode_uid;
+ }
+ if ($bbcode_bitfield)
+ {
+ $this->bbcode_bitfield = $bbcode_bitfield;
}
- }
-
- if ($mark_lowest_level && $open_is_regexp)
- {
- message_die(GENERAL_ERROR, "Unsupported operation for bbcode_first_pass_pda().");
- }
+ $str = array('search' => array(), 'replace' => array());
+ $preg = array('search' => array(), 'replace' => array());
- // Start at the 2nd char of the string, looking for opening tags.
- $curr_pos = 1;
- while ($curr_pos && ($curr_pos < strlen($text)))
- {
- $curr_pos = strpos($text, "[", $curr_pos);
-
- // If not found, $curr_pos will be 0, and the loop will end.
- if ($curr_pos)
+ for ($bbcode_id = 0; $bbcode_id < 31; ++$bbcode_id)
{
- // We found a [. It starts at $curr_pos.
- // check if it's a starting or ending tag.
- $found_start = false;
- $which_start_tag = "";
- $start_tag_index = -1;
- for ($i = 0; $i < $open_tag_count; $i++)
+ if ($this->bbcode_bitfield & pow(2, $bbcode_id))
{
- // Grab everything until the first "]"...
- $possible_start = substr($text, $curr_pos, strpos($text, "]", $curr_pos + 1) - $curr_pos + 1);
-
- //
- // We're going to try and catch usernames with "[' characters.
- //
- if( preg_match('/\[quote\=\\\\"/si', $possible_start) && !preg_match('/\[quote=\\\\"[^"]*\\\\"\]/si', $possible_start) )
+ foreach ($this->bbcode_cache[$bbcode_id] as $type => $array)
{
- //
- // OK we are in a quote tag that probably contains a ] bracket.
- // Grab a bit more of the string to hopefully get all of it..
- //
- $possible_start = substr($text, $curr_pos, strpos($text, "\"]", $curr_pos + 1) - $curr_pos + 2);
- }
- //
- // Now compare, either using regexp or not.
-
- if ($open_is_regexp)
- {
- $match_result = array();
- // PREG regexp comparison.
- if (preg_match($open_tag[$i], $possible_start, $match_result))
+ foreach ($array as $search => $replace)
{
- $found_start = true;
- $which_start_tag = $match_result[0];
- $start_tag_index = $i;
- break;
- }
- }
- else
- {
- // straightforward string comparison.
- if (0 == strcasecmp($open_tag[$i], $possible_start))
- {
- $found_start = true;
- $which_start_tag = $open_tag[$i];
- $start_tag_index = $i;
- break;
+ ${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
+ ${$type}['replace'][] = $replace;
}
}
}
+ }
+
+ $message = str_replace($str['search'], $str['replace'], $message);
+ $message = preg_replace($preg['search'], $preg['replace'], $message);
- if ($found_start)
+ return $message;
+ }
+
+ //
+ // bbcode_cache_init()
+ //
+ // requires: $this->bbcode_bitfield
+ // sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
+ //
+ function bbcode_cache_init()
+ {
+ $sql = '';
+
+ $bbcode_ids = array();
+ // TODO: only load needed bbcodes
+ for ($bbcode_id = 0; $bbcode_id < 10; ++$bbcode_id)
+ {
+ $bbcode_ids[] = $bbcode_id;
+
+ //
+ // WARNING: hardcoded values. it assumes that bbcodes with bbcode_id > 10 are user-defined bbcodes
+ // and it has to be specified which bbcodes need the template to be loaded
+ //
+ if ($bbcode_id > 10)
{
- // We have an opening tag.
- // Push its position, the text we matched, and its index in the open_tag array on to the stack, and then keep going to the right.
- $match = array("pos" => $curr_pos, "tag" => $which_start_tag, "index" => $start_tag_index);
- array_push($stack, $match);
- //
- // Rather than just increment $curr_pos
- // Set it to the ending of the tag we just found
- // Keeps error in nested tag from breaking out
- // of table structure..
- //
- $curr_pos = $curr_pos + strlen($possible_start);
+ $sql .= $bbcode_id . ',';
}
- else
+ elseif (in_array($bbcode_id, array(0, 5, 6, 8, 9)))
{
- // check for a closing tag..
- $possible_end = substr($text, $curr_pos, $close_tag_length);
- if (0 == strcasecmp($close_tag, $possible_end))
- {
- // We have an ending tag.
- // Check if we've already found a matching starting tag.
- if (sizeof($stack) > 0)
- {
- // There exists a starting tag.
- $curr_nesting_depth = sizeof($stack);
- // We need to do 2 replacements now.
- $match = array_pop($stack);
- $start_index = $match['pos'];
- $start_tag = $match['tag'];
- $start_length = strlen($start_tag);
- $start_tag_index = $match['index'];
-
- if ($open_is_regexp)
- {
- $start_tag = preg_replace($open_tag[$start_tag_index], $open_regexp_replace[$start_tag_index], $start_tag);
- }
-
- // everything before the opening tag.
- $before_start_tag = substr($text, 0, $start_index);
+ $load_template = TRUE;
+ }
+ }
+ if (!empty($load_template))
+ {
+ global $template, $user;
- // everything after the opening tag, but before the closing tag.
- $between_tags = substr($text, $start_index + $start_length, $curr_pos - $start_index - $start_length);
+ $tpl_filename = $template->make_filename('bbcode.html');
- // Run the given function on the text between the tags..
- if ($use_function_pointer)
- {
- $between_tags = $func($between_tags, $uid);
- }
+ if (!$fp = @fopen($tpl_filename, 'rb'))
+ {
+ trigger_error('Could not load bbcode template');
+ }
+ $tpl = fread($fp, filesize($tpl_filename));
+ @fclose($fp);
+
+ // replace \ with \\ and then ' with \'.
+ $tpl = str_replace('\\', '\\\\', $tpl);
+ $tpl = str_replace("'", "\'", $tpl);
+
+ // strip newlines.
+ $tpl = str_replace("\n", '', $tpl);
+
+ // Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
+ $tpl = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END (.*?) -->#', "\n" . "\$this->bbcode_tpl['\\1'] = trim('\\2');", $tpl);
+
+ $this->bbcode_tpl = array();
+ eval($tpl);
+
+ $this->bbcode_tpl['quote_open'] = str_replace('{L_QUOTE}', $user->lang['QUOTE'], $this->bbcode_tpl['quote_open']);
+ $this->bbcode_tpl['quote_username_open'] = str_replace('{L_QUOTE}', $user->lang['QUOTE'], $this->bbcode_tpl['quote_username_open']);
+ $this->bbcode_tpl['quote_username_open'] = str_replace('{L_WROTE}', $user->lang['WROTE'], $this->bbcode_tpl['quote_username_open']);
+ $this->bbcode_tpl['quote_username_open'] = str_replace('{USERNAME}', '\\1', $this->bbcode_tpl['quote_username_open']);
+ $this->bbcode_tpl['code_open'] = str_replace('{L_CODE}', $user->lang['CODE'], $this->bbcode_tpl['code_open']);
+ }
- // everything after the closing tag.
- $after_end_tag = substr($text, $curr_pos + $close_tag_length);
+ if ($sql)
+ {
+ global $db;
+ $rowset = array();
- // Mark the lowest nesting level if needed.
- if ($mark_lowest_level && ($curr_nesting_depth == 1))
- {
- if ($open_tag[0] == '[code]')
- {
- $code_entities_match = array('#<#', '#>#', '#"#', '#:#', '#\[#', '#\]#', '#\(#', '#\)#', '#\{#', '#\}#');
- $code_entities_replace = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;');
- $between_tags = preg_replace($code_entities_match, $code_entities_replace, $between_tags);
- }
- $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$curr_nesting_depth:$uid]";
- $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$curr_nesting_depth:$uid]";
- }
- else
- {
- if ($open_tag[0] == '[code]')
- {
- $text = $before_start_tag . '&#91;code&#93;';
- $text .= $between_tags . '&#91;/code&#93;';
- }
- else
- {
- if ($open_is_regexp)
- {
- $text = $before_start_tag . $start_tag;
- }
- else
- {
- $text = $before_start_tag . substr($start_tag, 0, $start_length - 1) . ":$uid]";
- }
- $text .= $between_tags . substr($close_tag_new, 0, $close_tag_new_length - 1) . ":$uid]";
- }
- }
+ $sql = 'SELECT bbcode_id, second_pass_regexp, second_pass_replacement
+ FROM ' . BBCODES_TABLE . '
+ WHERE bbcode_id IN (' . substr($sql, 0, -1) . ')
+ ORDER BY bbcode_id';
- $text .= $after_end_tag;
+ $result = $db->sql_query($sql);
+ while ($row = $db->sql_fetchrow($result))
+ {
+ $rowset[$row['bbcode_id']] = $row;
+ }
+ }
- // Now.. we've screwed up the indices by changing the length of the string.
- // So, if there's anything in the stack, we want to resume searching just after it.
- // otherwise, we go back to the start.
- if (sizeof($stack) > 0)
- {
- $match = array_pop($stack);
- $curr_pos = $match['pos'];
- array_push($stack, $match);
- ++$curr_pos;
- }
- else
- {
- $curr_pos = 1;
- }
+ foreach ($bbcode_ids as $bbcode_id)
+ {
+ switch ($bbcode_id)
+ {
+ case 0:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'str' => array(
+ '[quote:$uid]' => $this->bbcode_tpl['quote_open'],
+ '[/quote:$uid]' => $this->bbcode_tpl['quote_close']
+ ),
+ 'preg' => array(
+ '#\[quote:$uid="(.*?)"\]#' => $this->bbcode_tpl['quote_username_open']
+ )
+ );
+ break;
+ case 1:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'str' => array(
+ '[b:$uid]' => '<span style="font-weight: bold">',
+ '[/b:$uid]' => '</span>'
+ )
+ );
+ break;
+ case 2:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'str' => array(
+ '[i:$uid]' => '<span style="font-style: italic">',
+ '[/i:$uid]' => '</span>'
+ )
+ );
+ break;
+ case 3:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[url:$uid\](.*?)\[/url:$uid\]#s' => '<a href="\1" target="_blank">\1</a>',
+ '#\[url=(.*?):$uid\](.*?)\[/url:$uid\]#s' => '<a href="\1" target="_blank">\2</a>'
+ )
+ );
+ break;
+ case 4:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[img:$uid\](.*?)\[/img:$uid\]#' => '<img src="\1" border="0" />'
+ )
+ );
+ break;
+ case 5:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[size=(.*?):$uid\](.*?)\[/size:$uid\]#' => '<span style="font-size: \1px; line-height: normal">\2</span>'
+ )
+ );
+ break;
+ case 6:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[color=(.*?):$uid\](.*?)\[/color:$uid\]#' => '<span style="color: \1">\2</span>'
+ )
+ );
+ break;
+ case 7:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'str' => array(
+ '[u:$uid]' => '<span style="text-decoration: underline">',
+ '[/u:$uid]' => '</span>'
+ )
+ );
+ break;
+ case 8:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[code:$uid\](.*?)\[/code:$uid\]#ise' => '$this->bbcode_second_pass_code("\1")'
+ )
+ );
+ break;
+ case 9:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'str' => array(
+ '[list:$uid]' => '<ul>',
+ '[/list:u:$uid]' => '</ul>',
+ '[/list:o:$uid]' => '</ol>',
+ '[*:$uid]' => '<li>'
+ ),
+ 'preg' => array(
+ '#\[list=(.+?):$uid\]#e' => '$this->bbcode_ordered_list("\1")',
+ )
+ );
+ break;
+ case 10:
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array(
+ '#\[email:$uid\](.*?)\[/email:$uid\]#i' => '<a href="mailto:\1">\1</a>',
+ '#\[email=(.*?):$uid\](.*?)\[/email:$uid\]#is' => '<a href="mailto:\1">\2</a>'
+ )
+ );
+ break;
+ default:
+ if (isset($rowset[$bbcode_id]))
+ {
+ $this->bbcode_cache[$bbcode_id] = array(
+ 'preg' => array($rowset[$bbcode_id]['second_pass_regexp'], $rowset[$bbcode_id]['second_pass_replacement'])
+ );
}
else
{
- // No matching start tag found. Increment pos, keep going.
- ++$curr_pos;
+ $this->bbcode_cache[$bbcode_id] = array();
}
- }
- else
- {
- // No starting tag or ending tag.. Increment pos, keep looping.,
- ++$curr_pos;
- }
}
}
- } // while
-
- return $text;
-
-} // bbencode_first_pass_pda()
-
-/**
- * Does second-pass bbencoding of the [code] tags. This includes
- * running htmlspecialchars() over the text contained between
- * any pair of [code] tags that are at the first level of
- * nesting. Tags at the first level of nesting are indicated
- * by this format: [code:1:$uid] ... [/code:1:$uid]
- * Other tags are in this format: [code:$uid] ... [/code:$uid]
- */
-function bbencode_second_pass_code($text, $uid, $bbcode_tpl)
-{
- global $lang;
-
- $code_start_html = $bbcode_tpl['code_open'];
- $code_end_html = $bbcode_tpl['code_close'];
-
- // First, do all the 1st-level matches. These need an htmlspecialchars() run,
- // so they have to be handled differently.
- $match_count = preg_match_all("#\[code:1:$uid\](.*?)\[/code:1:$uid\]#si", $text, $matches);
-
- for ($i = 0; $i < $match_count; $i++)
+ }
+
+ function bbcode_ordered_list($chr)
{
- $before_replace = $matches[1][$i];
- $after_replace = $matches[1][$i];
-
- // Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
- $after_replace = str_replace(" ", "&nbsp; ", $after_replace);
- // now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
- $after_replace = str_replace(" ", " &nbsp;", $after_replace);
-
- // Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
- $after_replace = str_replace("\t", "&nbsp; &nbsp;", $after_replace);
-
- $str_to_match = "[code:1:$uid]" . $before_replace . "[/code:1:$uid]";
-
- $replacement = $code_start_html;
- $replacement .= $after_replace;
- $replacement .= $code_end_html;
-
- $text = str_replace($str_to_match, $replacement, $text);
+ if (is_numeric($chr))
+ {
+ $start = $chr;
+ $chr = '1';
+ }
+ elseif (strtolower($chr) == 'i')
+ {
+ $start = 1;
+ }
+ else
+ {
+ $start = ord(strtolower($chr)) - 96;
+ $chr = 'a';
+ }
+ return '<ol type="' . $chr . '" start="' . $start . '">';
}
- // Now, do all the non-first-level matches. These are simple.
- $text = str_replace("[code:$uid]", $code_start_html, $text);
- $text = str_replace("[/code:$uid]", $code_end_html, $text);
-
- return $text;
-
-} // bbencode_second_pass_code()
-
-/**
- * Nathan Codding - August 24, 2000.
- * Takes a string, and does the reverse of the PHP standard function
- * htmlspecialchars().
- */
-function undo_htmlspecialchars($input)
-{
- $input = preg_replace("/&gt;/i", ">", $input);
- $input = preg_replace("/&lt;/i", "<", $input);
- $input = preg_replace("/&quot;/i", "\"", $input);
- $input = preg_replace("/&amp;/i", "&", $input);
-
- return $input;
-}
-
-/**
- * This is used to change a [*] tag into a [*:$uid] tag as part
- * of the first-pass bbencoding of [list] tags. It fits the
- * standard required in order to be passed as a variable
- * function into bbencode_first_pass_pda().
- */
-function replace_listitems($text, $uid)
-{
- $text = str_replace("[*]", "[*:$uid]", $text);
+ function bbcode_second_pass_code($code)
+ {
+ $code = str_replace("\t", '&nbsp; &nbsp;', $code);
+ $code = str_replace(' ', '&nbsp; ', $code);
+ $code = str_replace(' ', ' &nbsp;', $code);
+ $code = $this->bbcode_tpl['code_open'] . $code . $this->bbcode_tpl['code_close'];
- return $text;
+ return $code;
+ }
}
-
?> \ No newline at end of file
diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php
index 3eaefe1b1b..48ee7f3060 100644
--- a/phpBB/includes/functions_posting.php
+++ b/phpBB/includes/functions_posting.php
@@ -125,7 +125,7 @@ function generate_topic_icons($mode, $enable_icons)
}
// DECODE TEXT -> This will/should be handled by bbcode.php eventually
-function decode_text(&$message)
+function decode_text(&$message, $bbcode_uid)
{
global $config, $censors;
@@ -158,6 +158,7 @@ function decode_text(&$message)
obtain_word_list($censors);
}
+ $message = str_replace(":$bbcode_uid", '', $message);
$message = preg_replace($match, $replace, $message);
return;
@@ -548,7 +549,7 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id)
// Format text to be displayed - from viewtopic.php - centralizing this would be nice ;)
function format_display($message, $html, $bbcode, $uid, $url, $smilies, $sig)
{
- global $auth, $forum_id, $config, $censors, $user;
+ global $auth, $forum_id, $config, $censors, $user, $bbcode;
// If the board has HTML off but the post has HTML
// on then we process it, else leave it alone
@@ -558,6 +559,7 @@ function format_display($message, $html, $bbcode, $uid, $url, $smilies, $sig)
}
// Second parse bbcode here
+ $message = $bbcode->bbcode_second_pass($message);
// If we allow users to disable display of emoticons
// we'll need an appropriate check and preg_replace here
@@ -773,6 +775,7 @@ function submit_post($mode, $message, $subject, $username, $topic_type, $bbcode_
'enable_smilies' => $post_data['enable_smilies'],
'enable_magic_url' => $post_data['enable_urls'],
'bbcode_uid' => $bbcode_uid,
+ 'bbcode_bitfield' => $post_data['bbcode_bitfield']
);
if ($mode != 'edit' || $post_data['message_md5'] != $post_data['post_checksum'])
diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php
index fcbc470b93..e9f77cc3b4 100644
--- a/phpBB/includes/message_parser.php
+++ b/phpBB/includes/message_parser.php
@@ -26,15 +26,26 @@ class parse_message
var $bbcode_tpl = null;
var $message_mode = 0; // MSG_POST/MSG_PM
+//----
+ var $bbcode_uid = '';
+ var $bbcode_bitfield = 0;
+ var $bbcode_array = array();
+ var $message = '';
+//----
+
function parse_message($message_type)
{
$this->message_mode = $message_type;
+ $this->bbcode_uid = substr(md5(time()), 0, BBCODE_UID_LEN);
}
function parse(&$message, $html, $bbcode, $uid, $url, $smilies)
{
global $config, $db, $user;
+ $this->message = $message;
+ $this->bbcode_uid = $uid;
+
$warn_msg = '';
// Do some general 'cleanup' first before processing message,
@@ -81,7 +92,10 @@ class parse_message
}
$warn_msg .= (($warn_msg != '') ? '<br />' : '') . $this->html($message, $html);
- $warn_msg .= (($warn_msg != '') ? '<br />' : '') . $this->bbcode($message, $bbcode, $uid);
+ if ($bbcode)
+ {
+ $warn_msg .= (($warn_msg != '') ? '<br />' : '') . $this->bbcode($message);
+ }
$warn_msg .= (($warn_msg != '') ? '<br />' : '') . $this->emoticons($message, $smilies);
$warn_msg .= (($warn_msg != '') ? '<br />' : '') . $this->magic_url($message, $url);
@@ -109,10 +123,209 @@ class parse_message
return;
}
- function bbcode(&$message, $bbcode, $uid)
+ function bbcode(&$message)
{
- global $config;
+ // DEBUG
+ $this->message = $message;
+
+ // Warning, Least-Significant-Bit first
+ $bbcode_bitfield = str_repeat('0', 32);
+ if (empty($this->bbcode_array))
+ {
+ $this->bbcode_init();
+ }
+
+ $size = strlen($this->message);
+ foreach ($this->bbcode_array as $offset => $row)
+ {
+ $parse = FALSE;
+ foreach ($row as $regex => $replacement)
+ {
+ $this->message = preg_replace($regex, $replacement, $this->message);
+
+ // Since we add bbcode_uid to all tags, the message length will increase whenever a tag is found
+ $new_size = strlen($this->message);
+ if ($size != $new_size)
+ {
+ $parse = TRUE;
+ }
+ }
+
+ $bbcode_bitfield{$offset} = ($parse) ? '1' : '0';
+ }
+
+ // LSB becomes MSB then we convert it to decimal
+ $this->bbcode_bitfield = bindec(strrev($bbcode_bitfield));
+
+ // DEBUG
+ $message = $this->message;
+ }
+
+ function bbcode_init()
+ {
+ // Always parse [code] first
+ $this->bbcode_array = array(
+ 8 => array('#\[code\](.+\[/code\])#ise' => '$this->bbcode_code("\1")'),
+ 10 => array('#\[email(=.*?)?\](.*?)\[/email\]#ise' => '$this->validate_email("\1", "\2")'),
+ 9 => array('#\[list(=[a-z|0-1]+)?\].*\[/list\]#ise' => '$this->bbcode_list("\0")'),
+ 7 => array('#\[u\](.*?)\[/u\]#is' => '[u:' . $this->bbcode_uid . ']\1[/u:' . $this->bbcode_uid . ']'),
+ 6 => array('!\[color=(#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]!is'
+ => '[color=\1:' . $this->bbcode_uid . ']\2[/color:' . $this->bbcode_uid . ']'),
+ 5 => array('#\[size=([\-\+]?[1-2]?[0-9])\](.*?)\[/size\]#is'
+ => '[size=\1:' . $this->bbcode_uid . ']\2[/size:' . $this->bbcode_uid . ']'),
+ 4 => array('#\[img\](https?://)([a-z0-9\-\.,\?!%\*_:;~\\&$@/=\+]+)\[/img\]#i'
+ => '[img:' . $this->bbcode_uid . ']\1\2[/img:' . $this->bbcode_uid . ']'),
+ 3 => array('#\[url=?(.*?)?\](.*?)\[/url\]#ise' => '$this->validate_url("\1", "\2")'),
+ 2 => array('#\[i\](.*?)\[/i\]#is' => '[i:' . $this->bbcode_uid . ']\1[/i:' . $this->bbcode_uid . ']'),
+ 1 => array('#\[b\](.*?)\[/b\]#is' => '[b:' . $this->bbcode_uid . ']\1[/b:' . $this->bbcode_uid . ']'),
+ 0 => array('#\[quote(=".*?")?\](.*?)\[/quote\]#is' => '[quote:' . $this->bbcode_uid . '\1]\2[/quote:' . $this->bbcode_uid . ']')
+ );
+
+/**************
+ global $db;
+ $result = $db->sql_query('SELECT bbcode_id, first_pass_regexp, first_pass_replacement FROM ' . BBCODES_TABLE);
+ while ($row = $db->sql_fetchrow($result))
+ {
+ $this->bbcode_array[$row['bbcode_id']] = array($row['first_pass_regexp'] => $row['first_pass_replacement']);
+ }
+**************/
+ }
+
+
+ function bbcode_code($in)
+ {
+ $str_from = array('<', '>', '"', ':', '[', ']', '(', ')', '{', '}', '.', '@');
+ $str_to = array('&lt;', '&gt;', '&quot;', '&#58;', '&#91;', '&#93;', '&#40;', '&#41;', '&#123;', '&#125;', '&#46;', '&#64;');
+
+ // if I remember correctly, preg_replace() will slash passed vars
+ $in = stripslashes($in);
+ $out = '';
+
+ do
+ {
+ $pos = strpos($in, '[/code]') + 7;
+ $buffer = substr($in, 0, $pos);
+ $in = substr($in, $pos);
+
+ while ($in)
+ {
+ $pos = strpos($in, '[/code]') + 7;
+ $sub_buffer = substr($in, 0, $pos);
+
+ if (preg_match('#\[code\]#i', $sub_buffer))
+ {
+ break;
+ }
+ else
+ {
+ $in = substr($in, $pos);
+ $buffer .= $sub_buffer;
+ }
+ }
+
+ $buffer = substr($buffer, 0, -7);
+ $out .= '[code:' . $this->bbcode_uid . ']' . str_replace($str_from, $str_to, $buffer) . '[/code:' . $this->bbcode_uid . ']';
+
+ $pos = strpos($in, '[code]');
+ if ($pos !== FALSE)
+ {
+ $out .= substr($in, 0, $pos);
+ $in = substr($in, $pos + 6);
+ }
+ }
+ while ($in);
+
+ return $out;
+ }
+
+ function bbcode_list($in)
+ {
+ $tok = ']';
+ $out = '[';
+ // if I remember correctly, preg_replace() will slash passed vars
+ $in = stripslashes($in);
+ $in = substr($in, 1);
+ $close_tags = array();
+
+ do
+ {
+ $pos = strlen($in);
+ for ($i = 0; $i < strlen($tok); ++$i)
+ {
+ $tmp_pos = strpos($in, $tok{$i});
+ if ($tmp_pos !== FALSE && $tmp_pos < $pos)
+ {
+ $pos = $tmp_pos;
+ }
+ }
+
+ $buffer = substr($in, 0, $pos);
+ $tok = $in{$pos};
+ $in = substr($in, $pos + 1);
+
+ if ($tok == ']')
+ {
+ if ($buffer == '/list' && count($close_tags))
+ {
+ $tag = array_pop($close_tags);
+ $out .= $tag;
+ $tok = '[';
+ }
+ elseif (preg_match('/list(=?(?:[0-9]|[a-z]|))/i', $buffer, $m))
+ {
+ array_push($close_tags, (($m[1]) ? '/list:o:' . $this->bbcode_uid . ']' : '/list:u:' . $this->bbcode_uid . ']'));
+ $out .= $buffer . ':' . $this->bbcode_uid . ']';
+ $tok = '[';
+ }
+ else
+ {
+ if ($buffer == '*' && count($close_tags))
+ {
+ $buffer = '*:' . $this->bbcode_uid;
+ }
+ $out .= $buffer . $tok;
+ $tok = '[]';
+ }
+ }
+ else
+ {
+ $out .= $buffer . $tok;
+ $tok = ($tok == '[') ? ']' : '[]';
+ }
+ }
+ while ($in);
+
+ // Close tags left = some tags still open
+ if (count($close_tags))
+ {
+ $out .= '[' . implode('[', $close_tags);
+ }
+
+ return $out;
+ }
+
+ function validate_email($var1, $var2)
+ {
+ $retval = '[email' . $var1 . ':' . $this->bbcode_uid . ']' . $var2 . '[/email:' . $this->bbcode_uid . ']';
+ return $retval;
+ }
+
+ function validate_url($var1, $var2)
+ {
+ $url = (empty($var1)) ? stripslashes($var2) : stripslashes($var1);
+
+ // Put validation regexps here
+ $valid = FALSE;
+ if (preg_match('#^http(s?)://#i', $url))
+ {
+ $valid = TRUE;
+ }
+ if ($valid)
+ {
+ return (empty($var1)) ? '[url:' . $this->bbcode_uid . ']' . $url . '[/url:' . $this->bbcode_uid . ']' : "[url=$url:" . $this->bbcode_uid . ']' . $var2 . '[/url:' . $this->bbcode_uid . ']';
+ }
+ return '[url' . $var1 . ']' . $var2 . '[/url]';
}
// Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
diff --git a/phpBB/posting.php b/phpBB/posting.php
index df9bd66025..030709de74 100644
--- a/phpBB/posting.php
+++ b/phpBB/posting.php
@@ -181,6 +181,8 @@ if ($sql != '')
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
+ $message_parser->bbcode_uid = $row['bbcode_uid'];
+
$forum_id = intval($row['forum_id']);
$topic_id = intval($row['topic_id']);
$post_id = intval($row['post_id']);
@@ -483,6 +485,9 @@ if (($submit) || ($preview) || ($refresh))
// Check checksum ... don't re-parse message if the same
if ($mode != 'edit' || $message_md5 != $post_checksum)
{
+ // DEBUG
+ $bbcode_uid = $message_parser->bbcode_uid;
+
// Parse message
if (($result = $message_parser->parse($message, $enable_html, $enable_bbcode, $bbcode_uid, $enable_urls, $enable_smilies)) != '')
{
@@ -596,7 +601,8 @@ if (($submit) || ($preview) || ($refresh))
'post_checksum' => $post_checksum,
'forum_parents' => $forum_parents,
'notify' => $notify,
- 'notify_set' => $notify_set
+ 'notify_set' => $notify_set,
+ 'bbcode_bitfield' => $message_parser->bbcode_bitfield
);
submit_post($mode, $message, $subject, $username, $topic_type, $bbcode_uid, $poll, $attachment_data, $post_data);
@@ -620,6 +626,13 @@ if ($preview)
}
$post_time = $current_time;
+
+ // DEBUG
+ $bbcode_bitfield = bindec('1111111111');
+
+ include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
+ $bbcode = new bbcode($bbcode_uid, $bbcode_bitfield);
+
$preview_message = format_display(stripslashes($message), $enable_html, $enable_bbcode, $bbcode_uid, $enable_urls, $enable_smilies, $enable_sig);
$preview_subject = (sizeof($censors)) ? preg_replace($censors['match'], $censors['replace'], $subject) : $subject;
@@ -627,7 +640,7 @@ if ($preview)
// Poll Preview
if ( ( ($mode == 'post') || ( ($mode == 'edit') && ($post_id == $topic_first_post_id) && (empty($poll_last_vote)) )) && ( ($auth->acl_get('f_poll', $forum_id)) || ($auth->acl_get('m_edit', $forum_id)) ))
{
- decode_text($poll_title);
+ decode_text($poll_title, $bbcode_uid);
$preview_poll_title = format_display(stripslashes($poll_title), $enable_html, $enable_bbcode, $bbcode_uid, $enable_urls, $enable_smilies, false, false);
$template->assign_vars(array(
@@ -645,8 +658,8 @@ if ($preview)
}
// Decode text for message display
-decode_text($post_text);
-decode_text($subject);
+decode_text($post_text, $bbcode_uid);
+decode_text($subject, $bbcode_uid);
// Save us some processing time. ;)
$poll_options_tmp = implode("\n", $poll_options);
diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php
index cd1d8313ff..0ee15c0307 100644
--- a/phpBB/viewtopic.php
+++ b/phpBB/viewtopic.php
@@ -25,6 +25,8 @@ include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
+// Instantiate BBCode class
+$bbcode = new bbcode();
// Initial var setup
$forum_id = (isset($_GET['f'])) ? max(intval($_GET['f']), 0) : 0;
@@ -777,7 +779,7 @@ if ($row = $db->sql_fetchrow($result))
// Second parse bbcode here
-
+ $bbcode->bbcode_second_pass(&$message, $bbcode_uid, $row['bbcode_bitfield']);
// If we allow users to disable display of emoticons