From 16ab0d8c264d88db5e3e961e66e6820b365f45ac Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 17 Apr 2011 17:58:11 +0800 Subject: [PATCH 1/5] [ticket/217] Multiline [url] not converted This is the second attempt parse multiline URL text, see the ticket comments. PHPBB3-217 --- phpBB/includes/message_parser.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php index 6951dcf820..8f5e72cbf7 100644 --- a/phpBB/includes/message_parser.php +++ b/phpBB/includes/message_parser.php @@ -109,13 +109,15 @@ class bbcode_firstpass extends bbcode // This array holds all bbcode data. BBCodes will be processed in this // order, so it is important to keep [code] in first position and // [quote] in second position. + // To parse multiline URL we enable dotall option setting only for URL text + // but not for link itself, thus [url][/url] is not affected. $this->bbcodes = array( 'code' => array('bbcode_id' => 8, 'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uise' => "\$this->bbcode_code('\$1', '\$2')")), 'quote' => array('bbcode_id' => 0, 'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#uise' => "\$this->bbcode_quote('\$0')")), 'attachment' => array('bbcode_id' => 12, 'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uise' => "\$this->bbcode_attachment('\$1', '\$2')")), 'b' => array('bbcode_id' => 1, 'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->bbcode_strong('\$1')")), 'i' => array('bbcode_id' => 2, 'regexp' => array('#\[i\](.*?)\[/i\]#uise' => "\$this->bbcode_italic('\$1')")), - 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](.*)\[/url\]#uiUe' => "\$this->validate_url('\$2', '\$3')")), + 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', '\$3')")), 'img' => array('bbcode_id' => 4, 'regexp' => array('#\[img\](.*)\[/img\]#uiUe' => "\$this->bbcode_img('\$1')")), 'size' => array('bbcode_id' => 5, 'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uise' => "\$this->bbcode_size('\$1', '\$2')")), 'color' => array('bbcode_id' => 6, 'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uise' => "\$this->bbcode_color('\$1', '\$2')")), From 6585d938d2c441900d8af6d25da2433d3beec856 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 5 Jun 2011 21:55:28 +0800 Subject: [PATCH 2/5] [ticket/217] Adjust patch, add tests PHPBB3-217 --- phpBB/includes/message_parser.php | 11 ++++++--- tests/bbcode/url_bbcode_test.php | 41 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/bbcode/url_bbcode_test.php diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php index 8f5e72cbf7..a6a4e530f0 100644 --- a/phpBB/includes/message_parser.php +++ b/phpBB/includes/message_parser.php @@ -102,7 +102,7 @@ class bbcode_firstpass extends bbcode /** * Init bbcode data for later parsing */ - function bbcode_init() + function bbcode_init($no_custom_bbcode = false) { static $rowset; @@ -117,7 +117,7 @@ class bbcode_firstpass extends bbcode 'attachment' => array('bbcode_id' => 12, 'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uise' => "\$this->bbcode_attachment('\$1', '\$2')")), 'b' => array('bbcode_id' => 1, 'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->bbcode_strong('\$1')")), 'i' => array('bbcode_id' => 2, 'regexp' => array('#\[i\](.*?)\[/i\]#uise' => "\$this->bbcode_italic('\$1')")), - 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', '\$3')")), + 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', ('\$3') ? '\$3' : '\$4')")), 'img' => array('bbcode_id' => 4, 'regexp' => array('#\[img\](.*)\[/img\]#uiUe' => "\$this->bbcode_img('\$1')")), 'size' => array('bbcode_id' => 5, 'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uise' => "\$this->bbcode_size('\$1', '\$2')")), 'color' => array('bbcode_id' => 6, 'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uise' => "\$this->bbcode_color('\$1', '\$2')")), @@ -135,6 +135,11 @@ class bbcode_firstpass extends bbcode $this->parsed_items[$tag] = 0; } + if ($no_custom_bbcode) + { + return; + } + if (!is_array($rowset)) { global $db; @@ -970,7 +975,7 @@ class bbcode_firstpass extends bbcode } // Is this a link to somewhere inside this board? If so then remove the session id from the url - if (strpos($url, generate_board_url()) !== false && strpos($url, 'sid=') !== false) + if (strpos($url, @generate_board_url()) !== false && strpos($url, 'sid=') !== false) { $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}&/', '\1', $url); $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}$/', '', $url); diff --git a/tests/bbcode/url_bbcode_test.php b/tests/bbcode/url_bbcode_test.php new file mode 100644 index 0000000000..6d9e51664d --- /dev/null +++ b/tests/bbcode/url_bbcode_test.php @@ -0,0 +1,41 @@ +message = $message; + $bbcode->bbcode_init(true); + $bbcode->parse_bbcode(); + $this->assertNotEquals($bbcode->message, '[url][/url]'); + $this->assertNotEquals($bbcode->message, $message); + $this->assertNotEquals($bbcode->message, NULL); + } +} From d44b6ba5caeafe220b4959a6de99d035fe10b4f1 Mon Sep 17 00:00:00 2001 From: rxu Date: Mon, 6 Jun 2011 00:50:53 +0800 Subject: [PATCH 3/5] [ticket/217] Use positive parameter statement for bbcode_init() PHPBB3-217 --- phpBB/includes/message_parser.php | 4 ++-- tests/bbcode/url_bbcode_test.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php index a6a4e530f0..9e0e61d0ba 100644 --- a/phpBB/includes/message_parser.php +++ b/phpBB/includes/message_parser.php @@ -102,7 +102,7 @@ class bbcode_firstpass extends bbcode /** * Init bbcode data for later parsing */ - function bbcode_init($no_custom_bbcode = false) + function bbcode_init($allow_custom_bbcode = true) { static $rowset; @@ -135,7 +135,7 @@ class bbcode_firstpass extends bbcode $this->parsed_items[$tag] = 0; } - if ($no_custom_bbcode) + if (!$allow_custom_bbcode) { return; } diff --git a/tests/bbcode/url_bbcode_test.php b/tests/bbcode/url_bbcode_test.php index 6d9e51664d..597933c6f8 100644 --- a/tests/bbcode/url_bbcode_test.php +++ b/tests/bbcode/url_bbcode_test.php @@ -32,7 +32,7 @@ class phpbb_url_bbcode_test extends phpbb_test_case { $bbcode = new bbcode_firstpass(); $bbcode->message = $message; - $bbcode->bbcode_init(true); + $bbcode->bbcode_init(false); $bbcode->parse_bbcode(); $this->assertNotEquals($bbcode->message, '[url][/url]'); $this->assertNotEquals($bbcode->message, $message); From 2d1e426ba745fa5b0b7666e5fe4a5fee97caccd7 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 5 Jun 2011 13:23:55 -0400 Subject: [PATCH 4/5] [ticket/217] Silence errors in tests, not code. Use a mock user object for testing bbcode. PHPBB3-217 --- phpBB/includes/message_parser.php | 2 +- tests/bbcode/url_bbcode_test.php | 4 ++++ tests/mock_user.php | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/mock_user.php diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php index 9e0e61d0ba..a134fab5d3 100644 --- a/phpBB/includes/message_parser.php +++ b/phpBB/includes/message_parser.php @@ -975,7 +975,7 @@ class bbcode_firstpass extends bbcode } // Is this a link to somewhere inside this board? If so then remove the session id from the url - if (strpos($url, @generate_board_url()) !== false && strpos($url, 'sid=') !== false) + if (strpos($url, generate_board_url()) !== false && strpos($url, 'sid=') !== false) { $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}&/', '\1', $url); $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}$/', '', $url); diff --git a/tests/bbcode/url_bbcode_test.php b/tests/bbcode/url_bbcode_test.php index 597933c6f8..7bbf731c74 100644 --- a/tests/bbcode/url_bbcode_test.php +++ b/tests/bbcode/url_bbcode_test.php @@ -11,6 +11,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/bbcode.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/message_parser.php'; +require_once dirname(__FILE__) . '/../mock_user.php'; class phpbb_url_bbcode_test extends phpbb_test_case { @@ -30,6 +31,9 @@ class phpbb_url_bbcode_test extends phpbb_test_case */ public function test_url($message) { + global $user; + $user = new phpbb_mock_user; + $bbcode = new bbcode_firstpass(); $bbcode->message = $message; $bbcode->bbcode_init(false); diff --git a/tests/mock_user.php b/tests/mock_user.php new file mode 100644 index 0000000000..74d31c4c4a --- /dev/null +++ b/tests/mock_user.php @@ -0,0 +1,20 @@ + '/'); +} From c93164db587391aaff7dd810d07cd0671c8bce3c Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 5 Jun 2011 13:48:37 -0400 Subject: [PATCH 5/5] [ticket/217] Use positive assertions in tests. PHPBB3-217 --- tests/bbcode/url_bbcode_test.php | 36 ++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/bbcode/url_bbcode_test.php b/tests/bbcode/url_bbcode_test.php index 7bbf731c74..cd85dbd0d9 100644 --- a/tests/bbcode/url_bbcode_test.php +++ b/tests/bbcode/url_bbcode_test.php @@ -18,18 +18,38 @@ class phpbb_url_bbcode_test extends phpbb_test_case public function url_bbcode_test_data() { return array( - array('[url]http://www.phpbb.com/community/[/url]'), - array('[url=http://www.phpbb.com/community/]One line URL text[/url]'), - array("[url=http://www.phpbb.com/community/]Multiline\x0AURL\x0Atext[/url]"), - array("test [url] test \x0A test [url=http://www.phpbb.com/]test[/url] test"), - array("test [url=http://www.phpbb.com/]test \x0A [url]http://phpbb.com[/url] test"), + array( + 'url only', + '[url]http://www.phpbb.com/community/[/url]', + '[url:]http://www.phpbb.com/community/[/url:]' + ), + array( + 'url with title', + '[url=http://www.phpbb.com/community/]One line URL text[/url]', + '[url=http://www.phpbb.com/community/:]One line URL text[/url:]' + ), + array( + 'url with multiline title', + "[url=http://www.phpbb.com/community/]Multiline\x0AURL\x0Atext[/url]", + "[url=http://www.phpbb.com/community/:]Multiline\x0AURL\x0Atext[/url:]" + ), + array( + 'unclosed url with multiline', + "test [url] test \x0A test [url=http://www.phpbb.com/]test[/url] test", + "test [url] test \x0A test [url=http://www.phpbb.com/:]test[/url:] test" + ), + array( + 'unclosed url with multiline and title', + "test [url=http://www.phpbb.com/]test \x0A [url]http://phpbb.com[/url] test", + "test [url=http://www.phpbb.com/:]test \x0A [url]http://phpbb.com[/url:] test" + ), ); } /** * @dataProvider url_bbcode_test_data */ - public function test_url($message) + public function test_url($description, $message, $expected) { global $user; $user = new phpbb_mock_user; @@ -38,8 +58,6 @@ class phpbb_url_bbcode_test extends phpbb_test_case $bbcode->message = $message; $bbcode->bbcode_init(false); $bbcode->parse_bbcode(); - $this->assertNotEquals($bbcode->message, '[url][/url]'); - $this->assertNotEquals($bbcode->message, $message); - $this->assertNotEquals($bbcode->message, NULL); + $this->assertEquals($expected, $bbcode->message); } }