[ticket/14323] Added support for truncating local URLs

PHPBB3-14323
This commit is contained in:
JoshyPHP 2015-12-05 19:48:01 +01:00
parent eb6ceb963e
commit 8fe94a19b4
6 changed files with 37 additions and 16 deletions

View file

@ -415,7 +415,8 @@ class factory implements \phpbb\textformatter\cache_interface
->add(__NAMESPACE__ . '\\parser::generate_autolink_text') ->add(__NAMESPACE__ . '\\parser::generate_autolink_text')
->resetParameters() ->resetParameters()
->addParameterByName('tag') ->addParameterByName('tag')
->addParameterByName('parser'); ->addParameterByName('parser')
->addParameterByValue(generate_board_url() . '/');
} }
/** /**

View file

@ -400,11 +400,12 @@ class parser implements \phpbb\textformatter\parser_interface
* Will only apply to URL tags that do not use any markup (e.g. not "[url]") on the assumption * Will only apply to URL tags that do not use any markup (e.g. not "[url]") on the assumption
* that those tags were created by the Autolink plugin to linkify URLs found in plain text * that those tags were created by the Autolink plugin to linkify URLs found in plain text
* *
* @param \s9e\TextFormatter\Parser\Tag $url_tag URL tag (start tag) * @param \s9e\TextFormatter\Parser\Tag $url_tag URL tag (start tag)
* @param \s9e\TextFormatter\Parser $parser Parser * @param \s9e\TextFormatter\Parser $parser Parser
* @return bool Always TRUE to indicate that the tag is valid * @param string $board_url Forum's root URL (with trailing slash)
* @return bool Always TRUE to indicate that the tag is valid
*/ */
public static function generate_autolink_text(\s9e\TextFormatter\Parser\Tag $url_tag, \s9e\TextFormatter\Parser $parser) public static function generate_autolink_text(\s9e\TextFormatter\Parser\Tag $url_tag, \s9e\TextFormatter\Parser $parser, $board_url)
{ {
// If the tag consumes any text then we ignore it because it's not a linkified URL. Same if // If the tag consumes any text then we ignore it because it's not a linkified URL. Same if
// it's not paired with an end tag that doesn't consume any text either // it's not paired with an end tag that doesn't consume any text either
@ -419,16 +420,20 @@ class parser implements \phpbb\textformatter\parser_interface
$length = $end - $start; $length = $end - $start;
$text = substr($parser->getText(), $start, $length); $text = substr($parser->getText(), $start, $length);
if ($length <= 55 || utf8_strlen($text) <= 55) // Remove the board's root URL from the link if applicable
if (stripos($text, $board_url) === 0 && strlen($text) > strlen($board_url))
{ {
// Do not do anything if the text is not longer than 55 characters $text = substr($text, strlen($board_url));
return true;
} }
$tag = $parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length); // Truncate the text if it's longer than 55 characters
$url_tag->cascadeInvalidationTo($tag); if (utf8_strlen($text) > 55)
{
$text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10);
}
$text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10); // Create a tag that consumes the link's text
$tag = $parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length);
$tag->setAttribute('text', $text); $tag->setAttribute('text', $text);
return true; return true;

View file

@ -315,7 +315,7 @@ class phpbb_test_case_helpers
public function set_s9e_services(ContainerInterface $container = null, $fixture = null, $styles_path = null) public function set_s9e_services(ContainerInterface $container = null, $fixture = null, $styles_path = null)
{ {
static $first_run; static $first_run;
global $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $user; global $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $request, $user;
$cache_dir = __DIR__ . '/../tmp/'; $cache_dir = __DIR__ . '/../tmp/';
@ -490,6 +490,12 @@ class phpbb_test_case_helpers
} }
} }
// Create a fake request
if (!isset($request))
{
$request = new phpbb_mock_request;
}
// Create and register the text_formatter.s9e.factory service // Create and register the text_formatter.s9e.factory service
$factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, $cache_dir, $cache_key_parser, $cache_key_renderer); $factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, $cache_dir, $cache_key_parser, $cache_key_renderer);
$container->set('text_formatter.s9e.factory', $factory); $container->set('text_formatter.s9e.factory', $factory);
@ -513,6 +519,7 @@ class phpbb_test_case_helpers
->will($this->test_case->returnCallback(__CLASS__ . '::format_date')); ->will($this->test_case->returnCallback(__CLASS__ . '::format_date'));
$user->date_format = 'Y-m-d H:i:s'; $user->date_format = 'Y-m-d H:i:s';
$user->host = 'localhost';
$user->optionset('viewcensors', true); $user->optionset('viewcensors', true);
$user->optionset('viewflash', true); $user->optionset('viewflash', true);
$user->optionset('viewimg', true); $user->optionset('viewimg', true);

View file

@ -235,6 +235,14 @@ class phpbb_textformatter_s9e_default_formatting_test extends phpbb_test_case
'http://www.phpbb.com/community/path/to/long/url/file.ext#section', 'http://www.phpbb.com/community/path/to/long/url/file.ext#section',
'<a href="http://www.phpbb.com/community/path/to/long/url/file.ext#section" class="postlink">http://www.phpbb.com/community/path/to/ ... xt#section</a>' '<a href="http://www.phpbb.com/community/path/to/long/url/file.ext#section" class="postlink">http://www.phpbb.com/community/path/to/ ... xt#section</a>'
), ),
array(
'http://localhost/ http://localhost/viewforum.php?f=1',
'<a href="http://localhost/" class="postlink">http://localhost/</a> <a href="http://localhost/viewforum.php?f=1" class="postlink">viewforum.php?f=1</a>'
),
array(
'http://localhost/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'<a href="http://localhost/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxx ... xxxxxxxxxx</a>'
),
array( array(
'[quote="[url=http://example.org]xxx[/url]"]...[/quote]', '[quote="[url=http://example.org]xxx[/url]"]...[/quote]',
'<blockquote><div><cite><a href="http://example.org" class="postlink">xxx</a> wrote:</cite>...</div></blockquote>' '<blockquote><div><cite><a href="http://example.org" class="postlink">xxx</a> wrote:</cite>...</div></blockquote>'

View file

@ -1,2 +1,2 @@
<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&amp;action=brackets</a><br> <a href="http://example.org/?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">http://example.org/?tourney[id]=34&amp;action=brackets</a><br>
<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">link</a> <a href="http://example.org/?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">link</a>

View file

@ -1,2 +1,2 @@
[url]http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets[/url] [url]http://example.org/?tourney[id]=34&action=brackets[/url]
[url="http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets"]link[/url] [url="http://example.org/?tourney[id]=34&action=brackets"]link[/url]