diff options
Diffstat (limited to 'phpBB/phpbb/textformatter')
-rw-r--r-- | phpBB/phpbb/textformatter/parser_interface.php | 3 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/parser.php | 42 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/renderer.php | 43 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/utils.php | 63 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/utils_interface.php | 20 |
5 files changed, 104 insertions, 67 deletions
diff --git a/phpBB/phpbb/textformatter/parser_interface.php b/phpBB/phpbb/textformatter/parser_interface.php index 3cb9f8e977..ad611fb5b4 100644 --- a/phpBB/phpbb/textformatter/parser_interface.php +++ b/phpBB/phpbb/textformatter/parser_interface.php @@ -82,7 +82,8 @@ interface parser_interface /** * Get the list of errors that were generated during last parsing * - * @return array + * @return array[] Array of arrays. Each array contains a lang string at index 0 plus any number + * of optional parameters */ public function get_errors(); diff --git a/phpBB/phpbb/textformatter/s9e/parser.php b/phpBB/phpbb/textformatter/s9e/parser.php index e46a0578d2..838c211e56 100644 --- a/phpBB/phpbb/textformatter/s9e/parser.php +++ b/phpBB/phpbb/textformatter/s9e/parser.php @@ -32,20 +32,14 @@ class parser implements \phpbb\textformatter\parser_interface protected $parser; /** - * @var \phpbb\user User object, used for translating errors - */ - protected $user; - - /** * Constructor * * @param \phpbb\cache\driver_interface $cache * @param string $key Cache key - * @param \phpbb\user $user * @param factory $factory * @param \phpbb\event\dispatcher_interface $dispatcher */ - public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, \phpbb\user $user, factory $factory, \phpbb\event\dispatcher_interface $dispatcher) + public function __construct(\phpbb\cache\driver\driver_interface $cache, $key, factory $factory, \phpbb\event\dispatcher_interface $dispatcher) { $parser = $cache->get($key); if (!$parser) @@ -56,24 +50,21 @@ class parser implements \phpbb\textformatter\parser_interface $this->dispatcher = $dispatcher; $this->parser = $parser; - $this->user = $user; $parser = $this; /** * Configure the parser service * * Can be used to: - * - toggle features according to the user's preferences, - * - toggle BBCodes according to the user's permissions, - * - register variables or custom parsers in the s9e\TextFormatter - * - configure the s9e\TextFormatter parser + * - toggle features or BBCodes + * - register variables or custom parsers in the s9e\TextFormatter parser + * - configure the s9e\TextFormatter parser's runtime settings * * @event core.text_formatter_s9e_parser_setup * @var \phpbb\textformatter\s9e\parser parser This parser service - * @var \phpbb\user user Current user * @since 3.2.0-a1 */ - $vars = array('parser', 'user'); + $vars = array('parser'); extract($dispatcher->trigger_event('core.text_formatter_s9e_parser_setup', compact($vars))); } @@ -196,13 +187,12 @@ class parser implements \phpbb\textformatter\parser_interface /** * {@inheritdoc} * - * This will translate the log entries found in s9e\TextFormatter's logger into phpBB error + * This will convert the log entries found in s9e\TextFormatter's logger into phpBB error * messages */ public function get_errors() { $errors = array(); - foreach ($this->parser->getLogger()->get() as $entry) { list($type, $msg, $context) = $entry; @@ -211,33 +201,39 @@ class parser implements \phpbb\textformatter\parser_interface { if ($context['tagName'] === 'E') { - $errors[] = $this->user->lang('TOO_MANY_SMILIES', $context['tagLimit']); + $errors[] = array('TOO_MANY_SMILIES', $context['tagLimit']); } else if ($context['tagName'] === 'URL') { - $errors[] = $this->user->lang('TOO_MANY_URLS', $context['tagLimit']); + $errors[] = array('TOO_MANY_URLS', $context['tagLimit']); } } else if ($msg === 'MAX_FONT_SIZE_EXCEEDED') { - $errors[] = $this->user->lang($msg, $context['max_size']); + $errors[] = array($msg, $context['max_size']); } else if (preg_match('/^MAX_(?:FLASH|IMG)_(HEIGHT|WIDTH)_EXCEEDED$/D', $msg, $m)) { - $errors[] = $this->user->lang($msg, $context['max_' . strtolower($m[1])]); + $errors[] = array($msg, $context['max_' . strtolower($m[1])]); } else if ($msg === 'Tag is disabled') { $name = strtolower($context['tag']->getName()); - $errors[] = $this->user->lang('UNAUTHORISED_BBCODE', '[' . $name . ']'); + $errors[] = array('UNAUTHORISED_BBCODE', '[' . $name . ']'); } else if ($msg === 'UNABLE_GET_IMAGE_SIZE') { - $errors[] = $this->user->lang($msg); + $errors[] = array($msg); } } - return array_unique($errors); + // Deduplicate error messages. array_unique() only works on strings so we have to serialize + if (!empty($errors)) + { + $errors = array_map('unserialize', array_unique(array_map('serialize', $errors))); + } + + return $errors; } /** diff --git a/phpBB/phpbb/textformatter/s9e/renderer.php b/phpBB/phpbb/textformatter/s9e/renderer.php index 8999f1d25f..51bc44f339 100644 --- a/phpBB/phpbb/textformatter/s9e/renderer.php +++ b/phpBB/phpbb/textformatter/s9e/renderer.php @@ -234,10 +234,6 @@ class renderer implements \phpbb\textformatter\renderer_interface } $html = $this->renderer->render($xml); - if (stripos($html, '<code') !== false) - { - $html = $this->replace_tabs_in_code($html); - } /** * Modify a rendered text @@ -254,45 +250,6 @@ class renderer implements \phpbb\textformatter\renderer_interface } /** - * Replace tabs in code elements - * - * @see bbcode::bbcode_second_pass_code() - * - * @param string $html Original HTML - * @return string Modified HTML - */ - protected function replace_tabs_in_code($html) - { - return preg_replace_callback( - '((<code[^>]*>)(.*?)(</code>))is', - function ($captures) - { - $code = $captures[2]; - - $code = str_replace("\t", ' ', $code); - $code = str_replace(' ', ' ', $code); - $code = str_replace(' ', ' ', $code); - $code = str_replace("\n ", "\n ", $code); - - // keep space at the beginning - if (!empty($code) && $code[0] == ' ') - { - $code = ' ' . substr($code, 1); - } - - // remove newline at the beginning - if (!empty($code) && $code[0] == "\n") - { - $code = substr($code, 1); - } - - return $captures[1] . $code . $captures[3]; - }, - $html - ); - } - - /** * {@inheritdoc} */ public function set_smilies_path($path) diff --git a/phpBB/phpbb/textformatter/s9e/utils.php b/phpBB/phpbb/textformatter/s9e/utils.php index 2018bbf519..04df589930 100644 --- a/phpBB/phpbb/textformatter/s9e/utils.php +++ b/phpBB/phpbb/textformatter/s9e/utils.php @@ -35,6 +35,69 @@ class utils implements \phpbb\textformatter\utils_interface } /** + * Return given string between quotes + * + * Will use either single- or double- quotes depending on whichever requires less escaping. + * Quotes and backslashes are escaped with backslashes where necessary + * + * @param string $str Original string + * @return string Escaped string within quotes + */ + protected function enquote($str) + { + $singleQuoted = "'" . addcslashes($str, "\\'") . "'"; + $doubleQuoted = '"' . addcslashes($str, '\\"') . '"'; + + return (strlen($singleQuoted) < strlen($doubleQuoted)) ? $singleQuoted : $doubleQuoted; + } + + /** + * {@inheritdoc} + */ + public function generate_quote($text, array $attributes = array()) + { + $quote = '[quote'; + if (isset($attributes['author'])) + { + // Add the author as the BBCode's default attribute + $quote .= '=' . $this->enquote($attributes['author']); + unset($attributes['author']); + } + foreach ($attributes as $name => $value) + { + $quote .= ' ' . $name . '=' . $this->enquote($value); + } + $quote .= ']' . $text . '[/quote]'; + + return $quote; + } + + /** + * Get a list of quote authors, limited to the outermost quotes + * + * @param string $xml Parsed text + * @return string[] List of authors + */ + public function get_outermost_quote_authors($xml) + { + $authors = array(); + if (strpos($xml, '<QUOTE ') === false) + { + return $authors; + } + + $dom = new \DOMDocument; + $dom->loadXML($xml); + $xpath = new \DOMXPath($dom); + foreach ($xpath->query('//QUOTE[not(ancestor::QUOTE)]/@author') as $author) + { + $authors[] = $author->textContent; + } + + return $authors; + } + + /** * Remove given BBCode and its content, at given nesting depth * * @param string $xml Parsed text diff --git a/phpBB/phpbb/textformatter/utils_interface.php b/phpBB/phpbb/textformatter/utils_interface.php index 132dc8ece4..41a6ba2345 100644 --- a/phpBB/phpbb/textformatter/utils_interface.php +++ b/phpBB/phpbb/textformatter/utils_interface.php @@ -29,6 +29,26 @@ interface utils_interface public function clean_formatting($text); /** + * Create a quote block for given text + * + * Possible attributes: + * - author + * + * @param string $text Quote's text + * @param array $attributes Quote's attributes + * @return string Quote block to be used in a new post/text + */ + public function generate_quote($text, array $attributes = array()); + + /** + * Get a list of quote authors, limited to the outermost quotes + * + * @param string $text Parsed text + * @return string[] List of authors + */ + public function get_outermost_quote_authors($text); + + /** * Remove given BBCode and its content, at given nesting depth * * @param string $text Parsed text |