phpBB

Code Changes

File: includes/message_parser.php

  Unmodified   Added   Modified   Removed
Line 21Line 21

if (!class_exists('bbcode'))
{


if (!class_exists('bbcode'))
{

 
	// The following lines are for extensions which include message_parser.php
// while $phpbb_root_path and $phpEx are out of the script scope
// which may lead to the 'Undefined variable' and 'failed to open stream' errors
if (!isset($phpbb_root_path))
{
global $phpbb_root_path;
}

if (!isset($phpEx))
{
global $phpEx;
}


	include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
}


	include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
}


Line 115Line 128
		// [quote] in second position.
// To parse multiline URL we enable dotall option setting only for URL text
// but not for link itself, thus [url][/url] is not affected.

		// [quote] in second position.
// To parse multiline URL we enable dotall option setting only for URL text
// but not for link itself, thus [url][/url] is not affected.

 
		//
// To perform custom validation in extension, use $this->validate_bbcode_by_extension()
// method which accepts variable number of parameters

		$this->bbcodes = array(
'code' => array('bbcode_id' => 8, 'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uise' => "\$this->bbcode_code('\$1', '\$2')")),
'quote' => array('bbcode_id' => 0, 'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#uise' => "\$this->bbcode_quote('\$0')")),

		$this->bbcodes = array(
'code' => array('bbcode_id' => 8, 'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uise' => "\$this->bbcode_code('\$1', '\$2')")),
'quote' => array('bbcode_id' => 0, 'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#uise' => "\$this->bbcode_quote('\$0')")),

Line 313Line 329
		$in = str_replace(' ', '%20', $in);

// Checking urls

		$in = str_replace(' ', '%20', $in);

// Checking urls

		if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) && !preg_match('#^' . get_preg_expression('www_url') . '$#i', $in))

		if (!preg_match('#^' . get_preg_expression('url') . '$#iu', $in) && !preg_match('#^' . get_preg_expression('www_url') . '$#iu', $in))

		{
return '[img]' . $in . '[/img]';
}

		{
return '[img]' . $in . '[/img]';
}

Line 381Line 397
		$in = str_replace(' ', '%20', $in);

// Make sure $in is a URL.

		$in = str_replace(' ', '%20', $in);

// Make sure $in is a URL.

		if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) &&
!preg_match('#^' . get_preg_expression('www_url') . '$#i', $in))

		if (!preg_match('#^' . get_preg_expression('url') . '$#iu', $in) &&
!preg_match('#^' . get_preg_expression('www_url') . '$#iu', $in))

		{
return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
}

		{
return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]';
}

Line 775Line 791
				else if (preg_match('#^quote(?:="(.*?)")?$#is', $buffer, $m) && substr($out, -1, 1) == '[')
{
$this->parsed_items['quote']++;

				else if (preg_match('#^quote(?:="(.*?)")?$#is', $buffer, $m) && substr($out, -1, 1) == '[')
{
$this->parsed_items['quote']++;


// the buffer holds a valid opening tag
if ($config['max_quote_depth'] && sizeof($close_tags) >= $config['max_quote_depth'])
{
if ($config['max_quote_depth'] == 1)
{
// Depth 1 - no nesting is allowed
$error_ary['quote_depth'] = $user->lang('QUOTE_NO_NESTING');
}
else
{
// There are too many nested quotes
$error_ary['quote_depth'] = $user->lang('QUOTE_DEPTH_EXCEEDED', (int) $config['max_quote_depth']);
}

$out .= $buffer . $tok;
$tok = '[]';
$buffer = '';

continue;
}


 
					array_push($close_tags, '/quote:' . $this->bbcode_uid);

if (isset($m[1]) && $m[1])

					array_push($close_tags, '/quote:' . $this->bbcode_uid);

if (isset($m[1]) && $m[1])

Line 973Line 967
		$url = str_replace(' ', '%20', $url);

// Checking urls

		$url = str_replace(' ', '%20', $url);

// Checking urls

		if (preg_match('#^' . get_preg_expression('url') . '$#i', $url) ||
preg_match('#^' . get_preg_expression('www_url') . '$#i', $url) ||
preg_match('#^' . preg_quote(generate_board_url(), '#') . get_preg_expression('relative_url') . '$#i', $url))

		if (preg_match('#^' . get_preg_expression('url') . '$#iu', $url) ||
preg_match('#^' . get_preg_expression('www_url') . '$#iu', $url) ||
preg_match('#^' . preg_quote(generate_board_url(), '#') . get_preg_expression('relative_url') . '$#iu', $url))

		{
$valid = true;
}

		{
$valid = true;
}

Line 1103Line 1097
	*/
function parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post')
{

	*/
function parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post')
{

		global $config, $db, $user;

		global $config, $db, $user, $phpbb_dispatcher;


$this->mode = $mode;



$this->mode = $mode;


Line 1156Line 1150
				$this->warn_msg[] = (!$message_length) ? $user->lang['TOO_FEW_CHARS'] : ($user->lang('CHARS_POST_CONTAINS', $message_length) . '<br />' . $user->lang('TOO_FEW_CHARS_LIMIT', (int) $config['min_post_chars']));
return (!$update_this_message) ? $return_message : $this->warn_msg;
}

				$this->warn_msg[] = (!$message_length) ? $user->lang['TOO_FEW_CHARS'] : ($user->lang('CHARS_POST_CONTAINS', $message_length) . '<br />' . $user->lang('TOO_FEW_CHARS_LIMIT', (int) $config['min_post_chars']));
return (!$update_this_message) ? $return_message : $this->warn_msg;
}

 
		}

/**
* This event can be used for additional message checks/cleanup before parsing
*
* @event core.message_parser_check_message
* @var bool allow_bbcode Do we allow BBCodes
* @var bool allow_magic_url Do we allow magic urls
* @var bool allow_smilies Do we allow smilies
* @var bool allow_img_bbcode Do we allow image BBCode
* @var bool allow_flash_bbcode Do we allow flash BBCode
* @var bool allow_quote_bbcode Do we allow quote BBCode
* @var bool allow_url_bbcode Do we allow url BBCode
* @var bool update_this_message Do we alter the parsed message
* @var string mode Posting mode
* @var string message The message text to parse
* @var string bbcode_bitfield The bbcode_bitfield before parsing
* @var string bbcode_uid The bbcode_uid before parsing
* @var bool return Do we return after the event is triggered if $warn_msg is not empty
* @var array warn_msg Array of the warning messages
* @since 3.1.2-RC1
* @changed 3.1.3-RC1 Added vars $bbcode_bitfield and $bbcode_uid
*/
$message = $this->message;
$warn_msg = $this->warn_msg;
$return = false;
$bbcode_bitfield = $this->bbcode_bitfield;
$bbcode_uid = $this->bbcode_uid;
$vars = array(
'allow_bbcode',
'allow_magic_url',
'allow_smilies',
'allow_img_bbcode',
'allow_flash_bbcode',
'allow_quote_bbcode',
'allow_url_bbcode',
'update_this_message',
'mode',
'message',
'bbcode_bitfield',
'bbcode_uid',
'return',
'warn_msg',
);
extract($phpbb_dispatcher->trigger_event('core.message_parser_check_message', compact($vars)));
$this->message = $message;
$this->warn_msg = $warn_msg;
$this->bbcode_bitfield = $bbcode_bitfield;
$this->bbcode_uid = $bbcode_uid;
if ($return && !empty($this->warn_msg))
{
return (!$update_this_message) ? $return_message : $this->warn_msg;

		}

// Prepare BBcode (just prepares some tags for better parsing)

		}

// Prepare BBcode (just prepares some tags for better parsing)

Line 1207Line 1253
			$character_list = implode('<br />', $matches[0]);
$this->warn_msg[] = $user->lang('UNSUPPORTED_CHARACTERS_MESSAGE', $character_list);
return $update_this_message ? $this->warn_msg : $return_message;

			$character_list = implode('<br />', $matches[0]);
$this->warn_msg[] = $user->lang('UNSUPPORTED_CHARACTERS_MESSAGE', $character_list);
return $update_this_message ? $this->warn_msg : $return_message;

 
		}

// Remove quotes that are nested too deep
if ($config['max_quote_depth'] > 0)
{
$this->remove_nested_quotes($config['max_quote_depth']);

		}

// Check for "empty" message. We do not check here for maximum length, because bbcode, smilies, etc. can add to the length.

		}

// Check for "empty" message. We do not check here for maximum length, because bbcode, smilies, etc. can add to the length.

Line 1248Line 1300
			$tmp_message = $this->message;
$return_message = &$this->message;
}

			$tmp_message = $this->message;
$return_message = &$this->message;
}

 

$text = $this->message;
$uid = $this->bbcode_uid;

/**
* Event to modify the text before it is parsed
*
* @event core.modify_format_display_text_before
* @var string text The message text to parse
* @var string uid The bbcode uid
* @var bool allow_bbcode Do we allow bbcodes
* @var bool allow_magic_url Do we allow magic urls
* @var bool allow_smilies Do we allow smilies
* @var bool update_this_message Do we update the internal message
* with the parsed result
* @since 3.1.6-RC1
*/
$vars = array('text', 'uid', 'allow_bbcode', 'allow_magic_url', 'allow_smilies', 'update_this_message');
extract($phpbb_dispatcher->trigger_event('core.modify_format_display_text_before', compact($vars)));

$this->message = $text;
$this->bbcode_uid = $uid;
unset($text, $uid);


if ($this->message_status == 'plain')
{


if ($this->message_status == 'plain')
{

Line 1747Line 1822
		$this->message = $poll['poll_title'];
$this->bbcode_bitfield = $bbcode_bitfield;


		$this->message = $poll['poll_title'];
$this->bbcode_bitfield = $bbcode_bitfield;


		$poll['poll_options'] = explode("\n", trim($poll['poll_option_text']));

		$poll['poll_options'] = preg_split('/\s*?\n\s*/', trim($poll['poll_option_text']));

		$poll['poll_options_size'] = sizeof($poll['poll_options']);

if (!$poll['poll_title'] && $poll['poll_options_size'])

		$poll['poll_options_size'] = sizeof($poll['poll_options']);

if (!$poll['poll_title'] && $poll['poll_options_size'])

Line 1785Line 1860
		}

$poll['poll_max_options'] = ($poll['poll_max_options'] < 1) ? 1 : (($poll['poll_max_options'] > $config['max_poll_options']) ? $config['max_poll_options'] : $poll['poll_max_options']);

		}

$poll['poll_max_options'] = ($poll['poll_max_options'] < 1) ? 1 : (($poll['poll_max_options'] > $config['max_poll_options']) ? $config['max_poll_options'] : $poll['poll_max_options']);

 
	}

/**
* Remove nested quotes at given depth in current parsed message
*
* @param integer $max_depth Depth limit
* @return null
*/
public function remove_nested_quotes($max_depth)
{
// Capture all [quote] and [/quote] tags
preg_match_all('(\\[/?quote(?:=&quot;(.*?)&quot;)?:' . $this->bbcode_uid . '\\])', $this->message, $matches, PREG_OFFSET_CAPTURE);

// Iterate over the quote tags to mark the ranges that must be removed
$depth = 0;
$ranges = array();
$start_pos = 0;
foreach ($matches[0] as $match)
{
if ($match[0][1] === '/')
{
--$depth;
if ($depth == $max_depth)
{
$end_pos = $match[1] + strlen($match[0]);
$length = $end_pos - $start_pos;
$ranges[] = array($start_pos, $length);
}
}
else
{
++$depth;
if ($depth == $max_depth + 1)
{
$start_pos = $match[1];
}
}
}

foreach (array_reverse($ranges) as $range)
{
list($start_pos, $length) = $range;
$this->message = substr_replace($this->message, '', $start_pos, $length);
}

	}

/**

	}

/**

Line 1809Line 1928
	public function set_mimetype_guesser(\phpbb\mimetype\guesser $mimetype_guesser)
{
$this->mimetype_guesser = $mimetype_guesser;

	public function set_mimetype_guesser(\phpbb\mimetype\guesser $mimetype_guesser)
{
$this->mimetype_guesser = $mimetype_guesser;

 
	}

/**
* Function to perform custom bbcode validation by extensions
* can be used in bbcode_init() to assign regexp replacement
* Example: 'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->validate_bbcode_by_extension('\$1')")
*
* Accepts variable number of parameters
*
* @return mixed Validation result
*/
public function validate_bbcode_by_extension()
{
global $phpbb_dispatcher;

$return = false;
$params_array = func_get_args();

/**
* Event to validate bbcode with the custom validating methods
* provided by extensions
*
* @event core.validate_bbcode_by_extension
* @var array params_array Array with the function parameters
* @var mixed return Validation result to return
*
* @since 3.1.5-RC1
*/
$vars = array('params_array', 'return');
extract($phpbb_dispatcher->trigger_event('core.validate_bbcode_by_extension', compact($vars)));

return $return;

	}
}


	}
}