From 3f73ae8545fa0746519935ebe75b92988fb969fc Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 3 May 2024 20:25:44 +0200 Subject: [PATCH] [ticket/17130] Reparse magic urls in posts based on actual content PHPBB3-17130 --- phpBB/phpbb/textreparser/base.php | 7 ++- .../text_reparser/plugins/post_text_test.php | 63 +++++++++++++++++++ .../plugins/test_row_based_plugin.php | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/textreparser/base.php b/phpBB/phpbb/textreparser/base.php index 2ee6ea2cb3..82a59a075d 100644 --- a/phpBB/phpbb/textreparser/base.php +++ b/phpBB/phpbb/textreparser/base.php @@ -198,8 +198,8 @@ abstract class base implements reparser_interface */ protected function guess_magic_url(array $record) { - // Look for or for a URL tag that's not immediately followed by - return (strpos($record['text'], '') !== false || preg_match('(]++>(?!))', $record['text'])); + // Look for magic URL markers or for a URL tag that's not immediately followed by + return preg_match('#.*?#', $record['text']) || preg_match('(]++>(?!))', $record['text']); } /** @@ -231,7 +231,10 @@ abstract class base implements reparser_interface */ protected function reparse_record(array $record) { + // Guess magic URL state based on actual record content before adding fields + $record['enable_magic_url'] = $this->guess_magic_url($record); $record = $this->add_missing_fields($record); + $flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0; $flags |= ($record['enable_smilies']) ? OPTION_FLAG_SMILIES : 0; $flags |= ($record['enable_magic_url']) ? OPTION_FLAG_LINKS : 0; diff --git a/tests/text_reparser/plugins/post_text_test.php b/tests/text_reparser/plugins/post_text_test.php index 8ea71e65f5..ecd06ac9bf 100644 --- a/tests/text_reparser/plugins/post_text_test.php +++ b/tests/text_reparser/plugins/post_text_test.php @@ -23,4 +23,67 @@ class phpbb_textreparser_post_text_test extends phpbb_textreparser_test_row_base { return new \phpbb\textreparser\plugins\post_text($this->db, POSTS_TABLE); } + + public function data_reparse_url(): array + { + return [ + [ // Reparse the same + 'https://www.example.com and some more text test included', + 'https://www.example.com and some more text test included', + ], + [ // Reparse without magic URL, shouldn't change + 'https://www.example.com and some more text test included', + 'https://www.example.com and some more text test included', + ], + [ // Reparse new format without magic URL, shouldn't change + 'https://www.example.com and some more text test included', + 'https://www.example.com and some more text test included', + ], + [ // Reparse with magic URL, should update to text formatter format + 'Foo is https://symfony.com/doc/current/service_container.html good', + 'Foo is https://symfony.com/doc/current/service_container.html good', + ], + [ // Reparse new format with magic URL, shouldn't change + 'Foo is https://symfony.com/doc/current/service_container.html good', + 'Foo is https://symfony.com/doc/current/service_container.html good', + ] + ]; + } + + /** + * @dataProvider data_reparse_url + */ + public function test_reparse_url(string $input_text, string $expected_text) + { + foreach ([true, false] as $enable_magic_url) + { + $record = [ + 'enable_bbcode' => true, + 'enable_smilies' => true, + 'enable_magic_url' => $enable_magic_url, + 'post_text' => $input_text, + 'bbcode_uid' => '', + ]; + + $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $record); + $this->db->sql_query($sql); + + $record['id'] = $this->db->sql_last_inserted_id(); + $record['text'] = $record['post_text']; + + // Call reparse_record via reflection + $reparser = $this->get_reparser(); + $reparser_reflection = new \ReflectionMethod($reparser, 'reparse_record'); + $reparser_reflection->setAccessible(true); + $reparser_reflection->invoke($reparser, $record); + + // Retrieve reparsed post text and compare with expectec + $sql = 'SELECT post_id, post_text FROM ' . POSTS_TABLE . ' WHERE post_id = ' . (int) $record['id']; + $result = $this->db->sql_query($sql); + $actual_text = $this->db->sql_fetchfield('post_text'); + $this->db->sql_freeresult($result); + + $this->assertSame($expected_text, $actual_text); + } + } } diff --git a/tests/text_reparser/plugins/test_row_based_plugin.php b/tests/text_reparser/plugins/test_row_based_plugin.php index 700bead7e5..eca6b2dd67 100644 --- a/tests/text_reparser/plugins/test_row_based_plugin.php +++ b/tests/text_reparser/plugins/test_row_based_plugin.php @@ -15,6 +15,7 @@ require_once __DIR__ . '/../../test_framework/phpbb_database_test_case.php'; abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_test_case { + /** @var \phpbb\db\driver\driver_interface */ protected $db; abstract protected function get_reparser();