1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-11 19:24:01 +02:00

Merge pull request #3461 from s9e/ticket/11768

[3.2][ticket/11768] Integrate s9e\TextFormatter
This commit is contained in:
Tristan Darricau
2015-04-05 23:32:38 +02:00
81 changed files with 6463 additions and 145 deletions

View File

@@ -25,7 +25,7 @@ class acp_bbcodes
function main($id, $mode)
{
global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher;
global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher, $phpbb_container;
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_log;
$user->add_lang('acp/posting');
@@ -269,6 +269,7 @@ class acp_bbcodes
$db->sql_query('INSERT INTO ' . BBCODES_TABLE . $db->sql_build_array('INSERT', $sql_ary));
$cache->destroy('sql', BBCODES_TABLE);
$phpbb_container->get('text_formatter.cache')->invalidate();
$lang = 'BBCODE_ADDED';
$log_action = 'LOG_BBCODE_ADD';
@@ -280,6 +281,7 @@ class acp_bbcodes
WHERE bbcode_id = ' . $bbcode_id;
$db->sql_query($sql);
$cache->destroy('sql', BBCODES_TABLE);
$phpbb_container->get('text_formatter.cache')->invalidate();
$lang = 'BBCODE_EDITED';
$log_action = 'LOG_BBCODE_EDIT';
@@ -319,6 +321,7 @@ class acp_bbcodes
{
$db->sql_query('DELETE FROM ' . BBCODES_TABLE . " WHERE bbcode_id = $bbcode_id");
$cache->destroy('sql', BBCODES_TABLE);
$phpbb_container->get('text_formatter.cache')->invalidate();
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_BBCODE_DELETE', false, array($row['bbcode_tag']));
if ($request->is_ajax())

View File

@@ -28,7 +28,7 @@ class acp_icons
function main($id, $mode)
{
global $db, $user, $auth, $template, $cache;
global $db, $user, $auth, $template, $cache, $phpbb_container;
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
global $request, $phpbb_container;
@@ -486,6 +486,7 @@ class acp_icons
$cache->destroy('_icons');
$cache->destroy('sql', $table);
$phpbb_container->get('text_formatter.cache')->invalidate();
$level = ($icons_updated) ? E_USER_NOTICE : E_USER_WARNING;
$errormsgs = '';
@@ -661,6 +662,7 @@ class acp_icons
$cache->destroy('_icons');
$cache->destroy('sql', $table);
$phpbb_container->get('text_formatter.cache')->invalidate();
trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
}
@@ -783,6 +785,7 @@ class acp_icons
$cache->destroy('_icons');
$cache->destroy('sql', $table);
$phpbb_container->get('text_formatter.cache')->invalidate();
if ($request->is_ajax())
{
@@ -848,6 +851,7 @@ class acp_icons
$cache->destroy('_icons');
$cache->destroy('sql', $table);
$phpbb_container->get('text_formatter.cache')->invalidate();
if ($request->is_ajax())
{

View File

@@ -352,6 +352,11 @@ class acp_main
$config->increment('assets_version', 1);
$cache->purge();
// Remove old renderers from the text_formatter service. Since this
// operation is performed after the cache is purged, there is not "current"
// renderer and in effect all renderers will be purged
$phpbb_container->get('text_formatter.cache')->tidy();
// Clear permissions
$auth->acl_clear_prefetch();
phpbb_cache_moderators($db, $cache, $auth);

View File

@@ -53,6 +53,9 @@ class acp_styles
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\textformatter\cache_interface */
protected $text_formatter_cache;
/** @var string */
protected $phpbb_root_path;
@@ -61,7 +64,7 @@ class acp_styles
public function main($id, $mode)
{
global $db, $user, $phpbb_admin_path, $phpbb_root_path, $phpEx, $template, $request, $cache, $auth, $config;
global $db, $user, $phpbb_admin_path, $phpbb_root_path, $phpEx, $template, $request, $cache, $auth, $config, $phpbb_container;
$this->db = $db;
$this->user = $user;
@@ -69,6 +72,7 @@ class acp_styles
$this->request = $request;
$this->cache = $cache;
$this->auth = $auth;
$this->text_formatter_cache = $phpbb_container->get('text_formatter.cache');
$this->config = $config;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
@@ -216,6 +220,12 @@ class acp_styles
}
}
// Invalidate the text formatter's cache for the new styles to take effect
if (!empty($installed_names))
{
$this->text_formatter_cache->invalidate();
}
// Show message
if (!count($messages))
{

View File

@@ -28,7 +28,7 @@ class acp_words
function main($id, $mode)
{
global $db, $user, $auth, $template, $cache, $phpbb_log, $request;
global $db, $user, $auth, $template, $cache, $phpbb_log, $request, $phpbb_container;
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
$user->add_lang('acp/posting');
@@ -115,6 +115,7 @@ class acp_words
}
$cache->destroy('_word_censors');
$phpbb_container->get('text_formatter.cache')->invalidate();
$log_action = ($word_id) ? 'LOG_WORD_EDIT' : 'LOG_WORD_ADD';
@@ -148,6 +149,7 @@ class acp_words
$db->sql_query($sql);
$cache->destroy('_word_censors');
$phpbb_container->get('text_formatter.cache')->invalidate();
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_WORD_DELETE', false, array($deleted_word));

View File

@@ -389,46 +389,68 @@ function phpbb_clean_search_string($search_string)
/**
* Decode text whereby text is coming from the db and expected to be pre-parsed content
* We are placing this outside of the message parser because we are often in need of it...
*
* NOTE: special chars are kept encoded
*
* @param string &$message Original message, passed by reference
* @param string $bbcode_uid BBCode UID
* @return null
*/
function decode_message(&$message, $bbcode_uid = '')
{
global $config;
global $phpbb_container;
if ($bbcode_uid)
if (preg_match('#^<[rt][ >]#', $message))
{
$match = array('<br />', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
$replace = array("\n", '', '', '', '');
$message = htmlspecialchars($phpbb_container->get('text_formatter.utils')->unparse($message), ENT_COMPAT);
}
else
{
$match = array('<br />');
$replace = array("\n");
if ($bbcode_uid)
{
$match = array('<br />', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
$replace = array("\n", '', '', '', '');
}
else
{
$match = array('<br />');
$replace = array("\n");
}
$message = str_replace($match, $replace, $message);
$match = get_preg_expression('bbcode_htm');
$replace = array('\1', '\1', '\2', '\1', '', '');
$message = preg_replace($match, $replace, $message);
}
$message = str_replace($match, $replace, $message);
$match = get_preg_expression('bbcode_htm');
$replace = array('\1', '\1', '\2', '\1', '', '');
$message = preg_replace($match, $replace, $message);
}
/**
* Strips all bbcode from a text and returns the plain content
* Strips all bbcode from a text in place
*/
function strip_bbcode(&$text, $uid = '')
{
if (!$uid)
global $phpbb_container;
if (preg_match('#^<[rt][ >]#', $text))
{
$uid = '[0-9a-z]{5,}';
$text = $phpbb_container->get('text_formatter.utils')->clean_formatting($text);
}
else
{
if (!$uid)
{
$uid = '[0-9a-z]{5,}';
}
$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
$match = get_preg_expression('bbcode_htm');
$replace = array('\1', '\1', '\2', '\1', '', '');
$match = get_preg_expression('bbcode_htm');
$replace = array('\1', '\1', '\2', '\1', '', '');
$text = preg_replace($match, $replace, $text);
$text = preg_replace($match, $replace, $text);
}
}
/**
@@ -438,7 +460,7 @@ function strip_bbcode(&$text, $uid = '')
function generate_text_for_display($text, $uid, $bitfield, $flags, $censor_text = true)
{
static $bbcode;
global $phpbb_dispatcher;
global $phpbb_dispatcher, $phpbb_container;
if ($text === '')
{
@@ -459,35 +481,57 @@ function generate_text_for_display($text, $uid, $bitfield, $flags, $censor_text
$vars = array('text', 'uid', 'bitfield', 'flags', 'censor_text');
extract($phpbb_dispatcher->trigger_event('core.modify_text_for_display_before', compact($vars)));
if ($censor_text)
if (preg_match('#^<[rt][ >]#', $text))
{
$text = censor_text($text);
}
$renderer = $phpbb_container->get('text_formatter.renderer');
// Parse bbcode if bbcode uid stored and bbcode enabled
if ($uid && ($flags & OPTION_FLAG_BBCODE))
// Temporarily switch off viewcensors if applicable
$old_censor = $renderer->get_viewcensors();
if ($old_censor !== $censor_text)
{
$renderer->set_viewcensors($censor_text);
}
$text = $renderer->render($text);
// Restore the previous value
if ($old_censor !== $censor_text)
{
$renderer->set_viewcensors($old_censor);
}
}
else
{
if (!class_exists('bbcode'))
if ($censor_text)
{
global $phpbb_root_path, $phpEx;
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
$text = censor_text($text);
}
if (empty($bbcode))
// Parse bbcode if bbcode uid stored and bbcode enabled
if ($uid && ($flags & OPTION_FLAG_BBCODE))
{
$bbcode = new bbcode($bitfield);
}
else
{
$bbcode->bbcode($bitfield);
if (!class_exists('bbcode'))
{
global $phpbb_root_path, $phpEx;
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
}
if (empty($bbcode))
{
$bbcode = new bbcode($bitfield);
}
else
{
$bbcode->bbcode($bitfield);
}
$bbcode->bbcode_second_pass($text, $uid);
}
$bbcode->bbcode_second_pass($text, $uid);
$text = bbcode_nl2br($text);
$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
}
$text = bbcode_nl2br($text);
$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
/**
* Use this event to modify the text after it is parsed
*
@@ -550,11 +594,6 @@ function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bb
$uid = $bitfield = '';
$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
if ($text === '')
{
return;
}
if (!class_exists('parse_message'))
{
include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

View File

@@ -1116,7 +1116,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;
global $config, $db, $user, $phpbb_dispatcher, $phpbb_container;
$this->mode = $mode;
@@ -1145,12 +1145,6 @@ class parse_message extends bbcode_firstpass
$this->decode_message();
}
// Do some general 'cleanup' first before processing message,
// e.g. remove excessive newlines(?), smilies(?)
$match = array('#(script|about|applet|activex|chrome):#i');
$replace = array("\\1&#058;");
$this->message = preg_replace($match, $replace, trim($this->message));
// Store message length...
$message_length = ($mode == 'post') ? utf8_strlen($this->message) : utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message));
@@ -1223,47 +1217,29 @@ class parse_message extends bbcode_firstpass
return (!$update_this_message) ? $return_message : $this->warn_msg;
}
// Prepare BBcode (just prepares some tags for better parsing)
if ($allow_bbcode && strpos($this->message, '[') !== false)
{
$this->bbcode_init();
$disallow = array('img', 'flash', 'quote', 'url');
foreach ($disallow as $bool)
{
if (!${'allow_' . $bool . '_bbcode'})
{
$this->bbcodes[$bool]['disabled'] = true;
}
}
// Get the parser
$parser = $phpbb_container->get('text_formatter.parser');
$this->prepare_bbcodes();
}
// Set the parser's options
($allow_bbcode) ? $parser->enable_bbcodes() : $parser->disable_bbcodes();
($allow_magic_url) ? $parser->enable_magic_url() : $parser->disable_magic_url();
($allow_smilies) ? $parser->enable_smilies() : $parser->disable_smilies();
($allow_img_bbcode) ? $parser->enable_bbcode('img') : $parser->disable_bbcode('img');
($allow_flash_bbcode) ? $parser->enable_bbcode('flash') : $parser->disable_bbcode('flash');
($allow_quote_bbcode) ? $parser->enable_bbcode('quote') : $parser->disable_bbcode('quote');
($allow_url_bbcode) ? $parser->enable_bbcode('url') : $parser->disable_bbcode('url');
// Parse smilies
if ($allow_smilies)
{
$this->smilies($config['max_' . $mode . '_smilies']);
}
// Set some config values
$parser->set_vars(array(
'max_font_size' => $config['max_' . $this->mode . '_font_size'],
'max_img_height' => $config['max_' . $this->mode . '_img_height'],
'max_img_width' => $config['max_' . $this->mode . '_img_width'],
'max_smilies' => $config['max_' . $this->mode . '_smilies'],
'max_urls' => $config['max_' . $this->mode . '_urls']
));
$num_urls = 0;
// Parse BBCode
if ($allow_bbcode && strpos($this->message, '[') !== false)
{
$this->parse_bbcode();
$num_urls += $this->parsed_items['url'];
}
// Parse URL's
if ($allow_magic_url)
{
$this->magic_url(generate_board_url());
if ($config['max_' . $mode . '_urls'])
{
$num_urls += preg_match_all('#\<!-- ([lmwe]) --\>.*?\<!-- \1 --\>#', $this->message, $matches);
}
}
// Parse this message
$this->message = $parser->parse(htmlspecialchars_decode($this->message, ENT_QUOTES));
// Check for out-of-bounds characters that are currently
// not supported by utf8_bin in MySQL
@@ -1282,10 +1258,12 @@ class parse_message extends bbcode_firstpass
return (!$update_this_message) ? $return_message : $this->warn_msg;
}
// Check number of links
if ($config['max_' . $mode . '_urls'] && $num_urls > $config['max_' . $mode . '_urls'])
// Check for errors
$errors = $parser->get_errors();
if ($errors)
{
$this->warn_msg[] = sprintf($user->lang['TOO_MANY_URLS'], $config['max_' . $mode . '_urls']);
$this->warn_msg = array_merge($this->warn_msg, $errors);
return (!$update_this_message) ? $return_message : $this->warn_msg;
}
@@ -1305,7 +1283,7 @@ class parse_message extends bbcode_firstpass
*/
function format_display($allow_bbcode, $allow_magic_url, $allow_smilies, $update_this_message = true)
{
global $phpbb_dispatcher;
global $phpbb_container, $phpbb_dispatcher;
// If false, then the parsed message get returned but internal message not processed.
if (!$update_this_message)
@@ -1314,26 +1292,25 @@ class parse_message extends bbcode_firstpass
$return_message = &$this->message;
}
if ($this->message_status == 'plain')
// 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
//if ($this->message_status == 'plain')
if (!preg_match('/^<[rt][ >]/', $this->message))
{
// Force updating message - of course.
$this->parse($allow_bbcode, $allow_magic_url, $allow_smilies, $this->allow_img_bbcode, $this->allow_flash_bbcode, $this->allow_quote_bbcode, $this->allow_url_bbcode, true);
}
// Replace naughty words such as farty pants
$this->message = censor_text($this->message);
// Parse BBcode
if ($allow_bbcode)
// There's a bug when previewing a topic with no poll, because the empty title of the poll
// gets parsed but $this->message still ends up empty. This fixes it, until a proper fix is
// devised
if ($this->message === '')
{
$this->bbcode_cache_init();
// We are giving those parameters to be able to use the bbcode class on its own
$this->bbcode_second_pass($this->message, $this->bbcode_uid);
$this->message = $phpbb_container->get('text_formatter.parser')->parse($this->message);
}
$this->message = bbcode_nl2br($this->message);
$this->message = smiley_text($this->message, !$allow_smilies);
$this->message = $phpbb_container->get('text_formatter.renderer')->render($this->message);
$text = $this->message;
$uid = $this->bbcode_uid;
@@ -1797,24 +1774,22 @@ class parse_message extends bbcode_firstpass
$poll_max_options = $poll['poll_max_options'];
// Parse Poll Option text ;)
// Parse Poll Option text
$tmp_message = $this->message;
$this->message = $poll['poll_option_text'];
$bbcode_bitfield = $this->bbcode_bitfield;
$poll['poll_option_text'] = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll');
$bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
$this->message = $tmp_message;
// Parse Poll Title
$tmp_message = $this->message;
$this->message = $poll['poll_title'];
$this->bbcode_bitfield = $bbcode_bitfield;
$poll['poll_options'] = explode("\n", trim($poll['poll_option_text']));
$poll['poll_options_size'] = sizeof($poll['poll_options']);
foreach ($poll['poll_options'] as &$poll_option)
{
$this->message = $poll_option;
$poll_option = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll');
}
unset($poll_option);
$poll['poll_option_text'] = implode("\n", $poll['poll_options']);
// Parse Poll Title
$this->message = $poll['poll_title'];
if (!$poll['poll_title'] && $poll['poll_options_size'])
{
$this->warn_msg[] = $user->lang['NO_POLL_TITLE'];
@@ -1832,10 +1807,6 @@ class parse_message extends bbcode_firstpass
}
}
$this->bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield));
$this->message = $tmp_message;
unset($tmp_message);
if (sizeof($poll['poll_options']) == 1)
{
$this->warn_msg[] = $user->lang['TOO_FEW_POLL_OPTIONS'];
@@ -1850,6 +1821,8 @@ class parse_message extends bbcode_firstpass
}
$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']);
$this->message = $tmp_message;
}
/**