aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/message_parser.php
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/includes/message_parser.php')
-rw-r--r--phpBB/includes/message_parser.php282
1 files changed, 218 insertions, 64 deletions
diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php
index 8526195664..97e0dd9f0d 100644
--- a/phpBB/includes/message_parser.php
+++ b/phpBB/includes/message_parser.php
@@ -83,7 +83,14 @@ class bbcode_firstpass extends bbcode
// it should not demand recompilation
if (preg_match($regexp, $this->message))
{
- $this->message = preg_replace($regexp, $replacement, $this->message);
+ if (is_callable($replacement))
+ {
+ $this->message = preg_replace_callback($regexp, $replacement, $this->message);
+ }
+ else
+ {
+ $this->message = preg_replace($regexp, $replacement, $this->message);
+ }
$bitfield->set($bbcode_data['bbcode_id']);
}
}
@@ -123,25 +130,82 @@ class bbcode_firstpass extends bbcode
static $rowset;
+ $bbcode_class = $this;
+
// This array holds all bbcode data. BBCodes will be processed in this
// order, so it is important to keep [code] in first position and
// [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')")),
- 'attachment' => array('bbcode_id' => 12, 'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uise' => "\$this->bbcode_attachment('\$1', '\$2')")),
- 'b' => array('bbcode_id' => 1, 'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->bbcode_strong('\$1')")),
- 'i' => array('bbcode_id' => 2, 'regexp' => array('#\[i\](.*?)\[/i\]#uise' => "\$this->bbcode_italic('\$1')")),
- 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', ('\$3') ? '\$3' : '\$4')")),
- 'img' => array('bbcode_id' => 4, 'regexp' => array('#\[img\](.*)\[/img\]#uiUe' => "\$this->bbcode_img('\$1')")),
- 'size' => array('bbcode_id' => 5, 'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uise' => "\$this->bbcode_size('\$1', '\$2')")),
- 'color' => array('bbcode_id' => 6, 'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uise' => "\$this->bbcode_color('\$1', '\$2')")),
- 'u' => array('bbcode_id' => 7, 'regexp' => array('#\[u\](.*?)\[/u\]#uise' => "\$this->bbcode_underline('\$1')")),
- 'list' => array('bbcode_id' => 9, 'regexp' => array('#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#uise' => "\$this->bbcode_parse_list('\$0')")),
- 'email' => array('bbcode_id' => 10, 'regexp' => array('#\[email=?(.*?)?\](.*?)\[/email\]#uise' => "\$this->validate_email('\$1', '\$2')")),
- 'flash' => array('bbcode_id' => 11, 'regexp' => array('#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#uie' => "\$this->bbcode_flash('\$1', '\$2', '\$3')"))
+ 'code' => array('bbcode_id' => 8, 'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_code($match[1], $match[2]);
+ }
+ )),
+ 'quote' => array('bbcode_id' => 0, 'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_quote($match[0]);
+ }
+ )),
+ 'attachment' => array('bbcode_id' => 12, 'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_attachment($match[1], $match[2]);
+ }
+ )),
+ 'b' => array('bbcode_id' => 1, 'regexp' => array('#\[b\](.*?)\[/b\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_strong($match[1]);
+ }
+ )),
+ 'i' => array('bbcode_id' => 2, 'regexp' => array('#\[i\](.*?)\[/i\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_italic($match[1]);
+ }
+ )),
+ 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiU' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->validate_url($match[2], ($match[3]) ? $match[3] : $match[4]);
+ }
+ )),
+ 'img' => array('bbcode_id' => 4, 'regexp' => array('#\[img\](.*)\[/img\]#uiU' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_img($match[1]);
+ }
+ )),
+ 'size' => array('bbcode_id' => 5, 'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_size($match[1], $match[2]);
+ }
+ )),
+ 'color' => array('bbcode_id' => 6, 'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_color($match[1], $match[2]);
+ }
+ )),
+ 'u' => array('bbcode_id' => 7, 'regexp' => array('#\[u\](.*?)\[/u\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_underline($match[1]);
+ }
+ )),
+ 'list' => array('bbcode_id' => 9, 'regexp' => array('#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_parse_list($match[0]);
+ }
+ )),
+ 'email' => array('bbcode_id' => 10, 'regexp' => array('#\[email=?(.*?)?\](.*?)\[/email\]#uis' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->validate_email($match[1], $match[2]);
+ }
+ )),
+ 'flash' => array('bbcode_id' => 11, 'regexp' => array('#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#ui' => function ($match) use($bbcode_class)
+ {
+ return $bbcode_class->bbcode_flash($match[1], $match[2], $match[3]);
+ }
+ ))
);
// Zero the parsed items array
@@ -339,7 +403,7 @@ class bbcode_firstpass extends bbcode
if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width'])
{
- $imagesize = new \fastImageSize\fastImageSize();
+ $imagesize = new \FastImageSize\FastImageSize();
$size_info = $imagesize->getImageSize(htmlspecialchars_decode($in));
if ($size_info === false)
@@ -734,8 +798,6 @@ class bbcode_firstpass extends bbcode
*/
function bbcode_quote($in)
{
- global $config, $user;
-
$in = str_replace("\r\n", "\n", str_replace('\"', '"', trim($in)));
if (!$in)
@@ -744,7 +806,9 @@ class bbcode_firstpass extends bbcode
}
// To let the parser not catch tokens within quote_username quotes we encode them before we start this...
- $in = preg_replace('#quote="(.*?)"\]#ie', "'quote="' . str_replace(array('[', ']', '\\\"'), array('[', ']', '\"'), '\$1') . '"]'", $in);
+ $in = preg_replace_callback('#quote="(.*?)"\]#i', function ($match) {
+ return 'quote="' . str_replace(array('[', ']', '\\\"'), array('[', ']', '\"'), $match[1]) . '"]';
+ }, $in);
$tok = ']';
$out = '[';
@@ -789,28 +853,6 @@ class bbcode_firstpass extends bbcode
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])
@@ -965,8 +1007,6 @@ class bbcode_firstpass extends bbcode
*/
function validate_url($var1, $var2)
{
- global $config;
-
$var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1)));
$var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2)));
@@ -1097,12 +1137,6 @@ class parse_message extends bbcode_firstpass
protected $plupload;
/**
- * The mimetype guesser object used for attachment mimetypes
- * @var \phpbb\mimetype\guesser
- */
- protected $mimetype_guesser;
-
- /**
* Init - give message here or manually
*/
function parse_message($message = '')
@@ -1117,7 +1151,7 @@ class parse_message extends bbcode_firstpass
*/
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, $phpbb_dispatcher, $phpbb_container;
+ global $config, $user, $phpbb_dispatcher, $phpbb_container;
$this->mode = $mode;
@@ -1242,6 +1276,12 @@ class parse_message extends bbcode_firstpass
// Parse this message
$this->message = $parser->parse(htmlspecialchars_decode($this->message, ENT_QUOTES));
+ // 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.
// The maximum length check happened before any parsings.
if ($mode === 'post' && utf8_clean_string($this->message) === '')
@@ -1299,6 +1339,29 @@ class parse_message extends bbcode_firstpass
$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);
+
// NOTE: message_status is unreliable for detecting unparsed text because some callers
// change $this->message without resetting $this->message_status to 'plain' so we
// inspect the message instead
@@ -1468,6 +1531,7 @@ class parse_message extends bbcode_firstpass
function parse_attachments($form_name, $mode, $forum_id, $submit, $preview, $refresh, $is_message = false)
{
global $config, $auth, $user, $phpbb_root_path, $phpEx, $db, $request;
+ global $phpbb_container;
$error = array();
@@ -1503,7 +1567,9 @@ class parse_message extends bbcode_firstpass
{
if ($num_attachments < $cfg['max_attachments'] || $auth->acl_get('a_') || $auth->acl_get('m_', $forum_id))
{
- $filedata = upload_attachment($form_name, $forum_id, false, '', $is_message);
+ /** @var \phpbb\attachment\manager $attachment_manager */
+ $attachment_manager = $phpbb_container->get('attachment.manager');
+ $filedata = $attachment_manager->upload($form_name, $forum_id, false, '', $is_message);
$error = $filedata['error'];
if ($filedata['post_attach'] && !sizeof($error))
@@ -1533,7 +1599,9 @@ class parse_message extends bbcode_firstpass
);
$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
- $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
+ $this->message = preg_replace_callback('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#', function ($match) {
+ return '[attachment='.($match[1] + 1).']' . $match[2] . '[/attachment]';
+ }, $this->message);
$this->filename_data['filecomment'] = '';
@@ -1571,6 +1639,9 @@ class parse_message extends bbcode_firstpass
if ($index !== false && !empty($this->attachment_data[$index]))
{
+ /** @var \phpbb\attachment\manager $attachment_manager */
+ $attachment_manager = $phpbb_container->get('attachment.manager');
+
// delete selected attachment
if ($this->attachment_data[$index]['is_orphan'])
{
@@ -1585,11 +1656,11 @@ class parse_message extends bbcode_firstpass
if ($row)
{
- phpbb_unlink($row['physical_filename'], 'file');
+ $attachment_manager->unlink($row['physical_filename'], 'file');
if ($row['thumbnail'])
{
- phpbb_unlink($row['physical_filename'], 'thumbnail');
+ $attachment_manager->unlink($row['physical_filename'], 'thumbnail');
}
$db->sql_query('DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']);
@@ -1597,11 +1668,13 @@ class parse_message extends bbcode_firstpass
}
else
{
- delete_attachments('attach', array(intval($this->attachment_data[$index]['attach_id'])));
+ $attachment_manager->delete('attach', $this->attachment_data[$index]['attach_id']);
}
unset($this->attachment_data[$index]);
- $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $this->message);
+ $this->message = preg_replace_callback('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#', function ($match) use($index) {
+ return ($match[1] == $index) ? '' : (($match[1] > $index) ? '[attachment=' . ($match[1] - 1) . ']' . $match[2] . '[/attachment]' : $match[0]);
+ }, $this->message);
// Reindex Array
$this->attachment_data = array_values($this->attachment_data);
@@ -1615,7 +1688,9 @@ class parse_message extends bbcode_firstpass
{
if ($num_attachments < $cfg['max_attachments'] || $auth->acl_gets('m_', 'a_', $forum_id))
{
- $filedata = upload_attachment($form_name, $forum_id, false, '', $is_message, false, $this->mimetype_guesser, $this->plupload);
+ /** @var \phpbb\attachment\manager $attachment_manager */
+ $attachment_manager = $phpbb_container->get('attachment.manager');
+ $filedata = $attachment_manager->upload($form_name, $forum_id, false, '', $is_message);
$error = array_merge($error, $filedata['error']);
if (!sizeof($error))
@@ -1645,7 +1720,9 @@ class parse_message extends bbcode_firstpass
);
$this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
- $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message);
+ $this->message = preg_replace_callback('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#', function ($match) {
+ return '[attachment=' . ($match[1] + 1) . ']' . $match[2] . '[/attachment]';
+ }, $this->message);
$this->filename_data['filecomment'] = '';
if (isset($this->plupload) && $this->plupload->is_active())
@@ -1689,7 +1766,7 @@ class parse_message extends bbcode_firstpass
*/
function get_submitted_attachment_data($check_user_id = false)
{
- global $user, $db, $phpbb_root_path, $phpEx, $config;
+ global $user, $db;
global $request;
$this->filename_data['filecomment'] = $request->variable('filecomment', '', true);
@@ -1777,7 +1854,7 @@ class parse_message extends bbcode_firstpass
*/
function parse_poll(&$poll)
{
- global $auth, $user, $config;
+ global $user, $config;
$poll_max_options = $poll['poll_max_options'];
@@ -1833,6 +1910,63 @@ class parse_message extends bbcode_firstpass
}
/**
+ * 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)
+ {
+ global $phpbb_container;
+
+ if (preg_match('#^<[rt][ >]#', $this->message))
+ {
+ $this->message = $phpbb_container->get('text_formatter.utils')->remove_bbcode(
+ $this->message,
+ 'quote',
+ $max_depth
+ );
+
+ return;
+ }
+
+ // 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);
+ }
+ }
+
+ /**
* Setter function for passing the plupload object
*
* @param \phpbb\plupload\plupload $plupload The plupload object
@@ -1845,14 +1979,34 @@ class parse_message extends bbcode_firstpass
}
/**
- * Setter function for passing the mimetype_guesser object
+ * 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')")
*
- * @param \phpbb\mimetype\guesser $mimetype_guesser The mimetype_guesser object
+ * Accepts variable number of parameters
*
- * @return null
+ * @return mixed Validation result
*/
- public function set_mimetype_guesser(\phpbb\mimetype\guesser $mimetype_guesser)
+ public function validate_bbcode_by_extension()
{
- $this->mimetype_guesser = $mimetype_guesser;
+ 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;
}
}