diff options
| -rw-r--r-- | phpBB/config/default/container/services_text_formatter.yml | 4 | ||||
| -rw-r--r-- | phpBB/phpbb/textformatter/s9e/factory.php | 53 | ||||
| -rw-r--r-- | phpBB/phpbb/textformatter/s9e/link_helper.php | 118 | ||||
| -rw-r--r-- | tests/test_framework/phpbb_test_case_helpers.php | 20 | ||||
| -rw-r--r-- | tests/text_formatter/s9e/default_formatting_test.php | 30 | ||||
| -rw-r--r-- | tests/text_formatter/s9e/factory_test.php | 17 | ||||
| -rw-r--r-- | tests/text_processing/tickets_data/PHPBB3-10587.html | 4 | ||||
| -rw-r--r-- | tests/text_processing/tickets_data/PHPBB3-10587.txt | 4 | ||||
| -rw-r--r-- | tests/text_processing/tickets_data/PHPBB3-9791.html | 2 | 
9 files changed, 236 insertions, 16 deletions
| diff --git a/phpBB/config/default/container/services_text_formatter.yml b/phpBB/config/default/container/services_text_formatter.yml index f86d30658e..943a79cd65 100644 --- a/phpBB/config/default/container/services_text_formatter.yml +++ b/phpBB/config/default/container/services_text_formatter.yml @@ -33,10 +33,14 @@ services:              - '@cache.driver'              - '@dispatcher'              - '@config' +            - '@text_formatter.s9e.link_helper'              - '%text_formatter.cache.dir%'              - '%text_formatter.cache.parser.key%'              - '%text_formatter.cache.renderer.key%' +    text_formatter.s9e.link_helper: +        class: phpbb\textformatter\s9e\link_helper +      text_formatter.s9e.parser:          class: phpbb\textformatter\s9e\parser          arguments: diff --git a/phpBB/phpbb/textformatter/s9e/factory.php b/phpBB/phpbb/textformatter/s9e/factory.php index 6ac55e82a8..8c273c342e 100644 --- a/phpBB/phpbb/textformatter/s9e/factory.php +++ b/phpBB/phpbb/textformatter/s9e/factory.php @@ -23,6 +23,11 @@ use s9e\TextFormatter\Configurator\Items\UnsafeTemplate;  class factory implements \phpbb\textformatter\cache_interface  {  	/** +	* @var \phpbb\textformatter\s9e\link_helper +	*/ +	protected $link_helper; + +	/**  	* @var \phpbb\cache\driver\driver_interface  	*/  	protected $cache; @@ -133,12 +138,14 @@ class factory implements \phpbb\textformatter\cache_interface  	* @param \phpbb\cache\driver\driver_interface $cache  	* @param \phpbb\event\dispatcher_interface $dispatcher  	* @param \phpbb\config\config $config +	* @param \phpbb\textformatter\s9e\link_helper $link_helper  	* @param string $cache_dir          Path to the cache dir  	* @param string $cache_key_parser   Cache key used for the parser  	* @param string $cache_key_renderer Cache key used for the renderer  	*/ -	public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, $cache_dir, $cache_key_parser, $cache_key_renderer) +	public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, \phpbb\textformatter\s9e\link_helper $link_helper, $cache_dir, $cache_key_parser, $cache_key_renderer)  	{ +		$this->link_helper = $link_helper;  		$this->cache = $cache;  		$this->cache_dir = $cache_dir;  		$this->cache_key_parser = $cache_key_parser; @@ -332,8 +339,7 @@ class factory implements \phpbb\textformatter\cache_interface  		}  		// Load the magic links plugins. We do that after BBCodes so that they use the same tags -		$configurator->plugins->load('Autoemail'); -		$configurator->plugins->load('Autolink', array('matchWww' => true)); +		$this->configure_autolink($configurator);  		// Register some vars with a default value. Those should be set at runtime by whatever calls  		// the parser @@ -395,6 +401,47 @@ class factory implements \phpbb\textformatter\cache_interface  	}  	/** +	* Configure the Autolink / Autoemail plugins used to linkify text +	* +	* @param  \s9e\TextFormatter\Configurator $configurator +	* @return void +	*/ +	protected function configure_autolink(Configurator $configurator) +	{ +		$configurator->plugins->load('Autoemail'); +		$configurator->plugins->load('Autolink', array('matchWww' => true)); + +		// Add a tag filter that creates a tag that stores and replace the +		// content of a link created by the Autolink plugin +		$configurator->Autolink->getTag()->filterChain +			->add(array($this->link_helper, 'generate_link_text_tag')) +			->resetParameters() +			->addParameterByName('tag') +			->addParameterByName('parser'); + +		// Create a tag that will be used to display the truncated text by +		// replacing the original content with the content of the @text attribute +		$tag = $configurator->tags->add('LINK_TEXT'); +		$tag->attributes->add('text'); +		$tag->template = '<xsl:value-of select="@text"/>'; + +		$tag->filterChain +			->add(array($this->link_helper, 'truncate_local_url')) +			->resetParameters() +			->addParameterByName('tag') +			->addParameterByValue(generate_board_url() . '/'); +		$tag->filterChain +			->add(array($this->link_helper, 'truncate_text')) +			->resetParameters() +			->addParameterByName('tag'); +		$tag->filterChain +			->add(array($this->link_helper, 'cleanup_tag')) +			->resetParameters() +			->addParameterByName('tag') +			->addParameterByName('parser'); +	} + +	/**  	* Return the default BBCodes configuration  	*  	* @return array 2D array. Each element has a 'usage' key, a 'template' key, and an optional 'options' key diff --git a/phpBB/phpbb/textformatter/s9e/link_helper.php b/phpBB/phpbb/textformatter/s9e/link_helper.php new file mode 100644 index 0000000000..0f44603dec --- /dev/null +++ b/phpBB/phpbb/textformatter/s9e/link_helper.php @@ -0,0 +1,118 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\textformatter\s9e; + +class link_helper +{ +	/** +	* Clean up and invalidate a LINK_TEXT tag if applicable +	* +	* Will invalidate the tag if its replacement text is the same as the original +	* text and would have no visible effect +	* +	* @param  \s9e\TextFormatter\Parser\Tag $tag    LINK_TEXT tag +	* @param  \s9e\TextFormatter\Parser     $parser Parser +	* @return bool                                  Whether the tag is valid +	*/ +	public function cleanup_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser) +	{ +		// Invalidate if the content of the tag matches the text attribute +		$text = substr($parser->getText(), $tag->getPos(), $tag->getLen()); + +		return ($text !== $tag->getAttribute('text')); +	} + +	/** +	* Create a LINK_TEXT tag inside of a link +	* +	* Meant to only apply to linkified URLs and [url] BBCodes without a parameter +	* +	* @param  \s9e\TextFormatter\Parser\Tag $tag    URL tag (start tag) +	* @param  \s9e\TextFormatter\Parser     $parser Parser +	* @return bool                                  Always true to indicate that the tag is valid +	*/ +	public function generate_link_text_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser) +	{ +		// Only create a LINK_TEXT tag if the start tag is paired with an end +		// tag, which is the case with tags from the Autolink plugins and with +		// the [url] BBCode when its content is used for the URL +		if (!$tag->getEndTag() || !$this->should_shorten($tag, $parser->getText())) +		{ +			return true; +		} + +		// Capture the text between the start tag and its end tag +		$start  = $tag->getPos() + $tag->getLen(); +		$end    = $tag->getEndTag()->getPos(); +		$length = $end - $start; +		$text   = substr($parser->getText(), $start, $length); + +		// Create a tag that consumes the link's text +		$parser->addSelfClosingTag('LINK_TEXT', $start, $length)->setAttribute('text', $text); + +		return true; +	} + +	/** +	* Test whether we should shorten this tag's text +	* +	* Will test whether the tag either does not use any markup or uses a single +	* [url] BBCode +	* +	* @param  \s9e\TextFormatter\Parser\Tag $tag  URL tag +	* @param  string                        $text Original text +	* @return bool +	*/ +	protected function should_shorten(\s9e\TextFormatter\Parser\Tag $tag, $text) +	{ +		return ($tag->getLen() === 0 || strtolower(substr($text, $tag->getPos(), $tag->getLen())) === '[url]'); +	} + +	/** +	* Remove the board's root URL from a the start of a string +	* +	* @param  \s9e\TextFormatter\Parser\Tag $tag       LINK_TEXT tag +	* @param  string                        $board_url Forum's root URL (with trailing slash) +	* @return bool                                     Always true to indicate that the tag is valid +	*/ +	public function truncate_local_url(\s9e\TextFormatter\Parser\Tag $tag, $board_url) +	{ +		$text = $tag->getAttribute('text'); +		if (stripos($text, $board_url) === 0 && strlen($text) > strlen($board_url)) +		{ +			$tag->setAttribute('text', substr($text, strlen($board_url))); +		} + +		return true; +	} + +	/** +	* Truncate the replacement text set in a LINK_TEXT tag +	* +	* @param  \s9e\TextFormatter\Parser\Tag $tag LINK_TEXT tag +	* @return bool                               Always true to indicate that the tag is valid +	*/ +	public function truncate_text(\s9e\TextFormatter\Parser\Tag $tag) +	{ +		$text = $tag->getAttribute('text'); +		if (utf8_strlen($text) > 55) +		{ +			$text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10); +		} + +		$tag->setAttribute('text', $text); + +		return true; +	} +} diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index 0166b11d06..c4b653ec7c 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -315,7 +315,7 @@ class phpbb_test_case_helpers  	public function set_s9e_services(ContainerInterface $container = null, $fixture = null, $styles_path = null)  	{  		static $first_run; -		global $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $user; +		global $config, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $request, $user;  		$cache_dir = __DIR__ . '/../tmp/'; @@ -473,14 +473,18 @@ class phpbb_test_case_helpers  		{  			$config = $container->get('config');  		} -		else +		elseif (!isset($config))  		{  			$config = new \phpbb\config\config(array());  		}  		$default_config = array( -			'allow_nocensors' => false, +			'allow_nocensors'       => false,  			'allowed_schemes_links' => 'http,https,ftp', -			'smilies_path' => 'images/smilies', +			'script_path'           => '/phpbb', +			'server_name'           => 'localhost', +			'server_port'           => 80, +			'server_protocol'       => 'http://', +			'smilies_path'          => 'images/smilies',  		);  		foreach ($default_config as $config_name => $config_value)  		{ @@ -490,8 +494,14 @@ 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 -		$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, new \phpbb\textformatter\s9e\link_helper, $cache_dir, $cache_key_parser, $cache_key_renderer);  		$container->set('text_formatter.s9e.factory', $factory);  		// Create a user if none was provided, and add the common lang strings diff --git a/tests/text_formatter/s9e/default_formatting_test.php b/tests/text_formatter/s9e/default_formatting_test.php index 3047653d51..1f7df15434 100644 --- a/tests/text_formatter/s9e/default_formatting_test.php +++ b/tests/text_formatter/s9e/default_formatting_test.php @@ -226,6 +226,36 @@ class phpbb_textformatter_s9e_default_formatting_test extends phpbb_test_case  				'... <a href="http://www.example.org" class="postlink">www.example.org</a> ...'  			),  			array( +				// From make_clickable_test.php +				'www.phpbb.com/community/?', +				'<a href="http://www.phpbb.com/community/" class="postlink">www.phpbb.com/community/</a>?' +			), +			array( +				// From make_clickable_test.php +				'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>' +			), +			array( +				'http://localhost/ http://localhost/phpbb/ http://localhost/phpbb/viewforum.php?f=1', +				'<a href="http://localhost/" class="postlink">http://localhost/</a> <a href="http://localhost/phpbb/" class="postlink">http://localhost/phpbb/</a> <a href="http://localhost/phpbb/viewforum.php?f=1" class="postlink">viewforum.php?f=1</a>' +			), +			array( +				'http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', +				'<a href="http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxx ... xxxxxxxxxx</a>' +			), +			array( +				'[url]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]', +				'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>' +			), +			array( +				'[URL]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]', +				'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>' +			), +			array( +				'[url=http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[/url]', +				'<a href="http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</a>' +			), +			array(  				'[quote="[url=http://example.org]xxx[/url]"]...[/quote]',  				'<blockquote><div><cite><a href="http://example.org" class="postlink">xxx</a> wrote:</cite>...</div></blockquote>'  			), diff --git a/tests/text_formatter/s9e/factory_test.php b/tests/text_formatter/s9e/factory_test.php index c8bf595092..8f8ec7ebc7 100644 --- a/tests/text_formatter/s9e/factory_test.php +++ b/tests/text_formatter/s9e/factory_test.php @@ -35,7 +35,7 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case  	public function get_factory()  	{ -		global $phpbb_root_path; +		global $config, $phpbb_root_path, $request, $user;  		$this->cache = new phpbb_mock_cache;  		$dal = new \phpbb\textformatter\data_access(  			$this->new_dbal(), @@ -50,11 +50,22 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case  			$this->cache,  			$this->dispatcher,  			new \phpbb\config\config(array('allowed_schemes_links' => 'http,https,ftp')), +			new \phpbb\textformatter\s9e\link_helper,  			$this->get_cache_dir(),  			'_foo_parser',  			'_foo_renderer'  		); +		// Global objects required by generate_board_url() +		$config = new \phpbb\config\config(array( +			'script_path'           => '/phpbb', +			'server_name'           => 'localhost', +			'server_port'           => 80, +			'server_protocol'       => 'http://', +		)); +		$request = new phpbb_mock_request; +		$user = new phpbb_mock_user; +  		return $factory;  	} @@ -128,14 +139,14 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case  	public function test_local_url()  	{  		global $config, $user, $request; -		$config = array( +		$config = new \phpbb\config\config(array(  			'force_server_vars' => true,  			'server_protocol' => 'http://',  			'server_name' => 'path',  			'server_port' => 80,  			'script_path' => '/to',  			'cookie_secure' => false -		); +		));  		$user = new phpbb_mock_user;  		$request = new phpbb_mock_request; diff --git a/tests/text_processing/tickets_data/PHPBB3-10587.html b/tests/text_processing/tickets_data/PHPBB3-10587.html index dd0a483244..4c2e536989 100644 --- a/tests/text_processing/tickets_data/PHPBB3-10587.html +++ b/tests/text_processing/tickets_data/PHPBB3-10587.html @@ -1,2 +1,2 @@ -<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&action=brackets" class="postlink">http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets</a><br> -<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&action=brackets" class="postlink">link</a>
\ No newline at end of file +<a href="http://example.org/?tourney%5Bid%5D=34&action=brackets" class="postlink">http://example.org/?tourney[id]=34&action=brackets</a><br> +<a href="http://example.org/?tourney%5Bid%5D=34&action=brackets" class="postlink">link</a>
\ No newline at end of file diff --git a/tests/text_processing/tickets_data/PHPBB3-10587.txt b/tests/text_processing/tickets_data/PHPBB3-10587.txt index f81a35eb5f..84788b720d 100644 --- a/tests/text_processing/tickets_data/PHPBB3-10587.txt +++ b/tests/text_processing/tickets_data/PHPBB3-10587.txt @@ -1,2 +1,2 @@ -[url]http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets[/url] -[url="http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets"]link[/url]
\ No newline at end of file +[url]http://example.org/?tourney[id]=34&action=brackets[/url] +[url="http://example.org/?tourney[id]=34&action=brackets"]link[/url]
\ No newline at end of file diff --git a/tests/text_processing/tickets_data/PHPBB3-9791.html b/tests/text_processing/tickets_data/PHPBB3-9791.html index cabed5b12f..3d0108c8a6 100644 --- a/tests/text_processing/tickets_data/PHPBB3-9791.html +++ b/tests/text_processing/tickets_data/PHPBB3-9791.html @@ -1 +1 @@ -<a href="http://www.phpbb.com/community/search.php?keywords=bogus&terms=all&author=&fid%5B%5D=46&sc=1&sf=all&sr=posts&sk=t&sd=d&st=0&ch=300&t=0&submit=Search" class="postlink">http://www.phpbb.com/community/search.php?keywords=bogus&terms=all&author=&fid[]=46&sc=1&sf=all&sr=posts&sk=t&sd=d&st=0&ch=300&t=0&submit=Search</a>
\ No newline at end of file +<a href="http://www.phpbb.com/community/search.php?keywords=bogus&terms=all&author=&fid%5B%5D=46&sc=1&sf=all&sr=posts&sk=t&sd=d&st=0&ch=300&t=0&submit=Search" class="postlink">http://www.phpbb.com/community/search.p ... mit=Search</a>
\ No newline at end of file | 
