From d8bd1921eec4b0e1ae6e87fecab0eccde2576117 Mon Sep 17 00:00:00 2001 From: Peter Liebetrau Date: Tue, 29 Jan 2019 21:17:11 +0100 Subject: [PATCH 1/4] BBcode, Tags - fix BBCode created tags in [code] blocks, fix usage of multiple ## created tags --- src/Content/Text/BBCode.php | 2 +- src/Model/Item.php | 39 ++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2c2054750c..e0190c0c1d 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1178,7 +1178,7 @@ class BBCode extends BaseObject // Extracting multi-line code blocks before the whitespace processing $codeblocks = []; - $text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#is", + $text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#ism", function ($matches) use (&$codeblocks) { $return = $matches[0]; if (strpos($matches[2], "\n") !== false) { diff --git a/src/Model/Item.php b/src/Model/Item.php index 67071db318..192c9b1410 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2400,6 +2400,17 @@ class Item extends BaseObject $URLSearchString = "^\[\]"; + // What happens in [code], stays in [code]! + // escape the # and the [ + // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 + $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", + function ($match) { + // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks + $find = ['#', '[']; + $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; + return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); + }, $item["body"]); + // All hashtags should point to the home server if "local_tags" is activated if (Config::get('system', 'local_tags')) { $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", @@ -2435,21 +2446,35 @@ class Item extends BaseObject } $basetag = str_replace('_',' ',substr($tag,1)); + if($basetag[0] != '#') { + $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; - $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; + $item["body"] = str_replace($tag, $newtag, $item["body"]); - $item["body"] = str_replace($tag, $newtag, $item["body"]); - - if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) { - if (strlen($item["tag"])) { - $item["tag"] = ',' . $item["tag"]; + if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) { + if (strlen($item["tag"])) { + $item["tag"] = ',' . $item["tag"]; + } + $item["tag"] = $newtag . $item["tag"]; } - $item["tag"] = $newtag . $item["tag"]; } } // Convert back the masked hashtags $item["body"] = str_replace("#", "#", $item["body"]); + + // Remember! What happens in [code], stays in [code] + // roleback the # and [ + $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", + function ($match) { + // we truly unESCape all sharp and leftsquarebracket + $find = [chr(27).'sharp', chr(27).'leftsquarebracket']; + $replace = ['#', '[']; + return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); + }, $item["body"]); + + // Convert back the masked hashtags + $item["body"] = str_replace("#", "#", $item["body"]); } public static function getGuidById($id) From 87b1d0a4d74764b8fc49e732e4468a4f5a197551 Mon Sep 17 00:00:00 2001 From: Peter Liebetrau Date: Tue, 29 Jan 2019 21:35:11 +0100 Subject: [PATCH 2/4] BBcode, Tags - fixed doubbbllee trroubbllee --- src/Model/Item.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 192c9b1410..77ba9e6344 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2472,9 +2472,6 @@ class Item extends BaseObject $replace = ['#', '[']; return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); }, $item["body"]); - - // Convert back the masked hashtags - $item["body"] = str_replace("#", "#", $item["body"]); } public static function getGuidById($id) From 5080778ea661fe08cdc995ed447eca103dc61bf7 Mon Sep 17 00:00:00 2001 From: Peter Liebetrau Date: Tue, 29 Jan 2019 21:54:20 +0100 Subject: [PATCH 3/4] BBcode, Tags - moved [code] escaping before BBCode::getTags() call --- src/Model/Item.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 77ba9e6344..c5b952a660 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2387,6 +2387,17 @@ class Item extends BaseObject public static function setHashtags(&$item) { + // What happens in [code], stays in [code]! + // escape the # and the [ + // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 + $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", + function ($match) { + // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks + $find = ['#', '[']; + $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; + return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); + }, $item["body"]); + $tags = BBCode::getTags($item["body"]); // No hashtags? @@ -2400,17 +2411,6 @@ class Item extends BaseObject $URLSearchString = "^\[\]"; - // What happens in [code], stays in [code]! - // escape the # and the [ - // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 - $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", - function ($match) { - // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks - $find = ['#', '[']; - $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; - return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); - }, $item["body"]); - // All hashtags should point to the home server if "local_tags" is activated if (Config::get('system', 'local_tags')) { $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", From c3e5c77eb3085c86124b625c137ece8528d4144d Mon Sep 17 00:00:00 2001 From: Peter Liebetrau Date: Tue, 29 Jan 2019 22:34:28 +0100 Subject: [PATCH 4/4] BBcode, Tags - changes for #pullrequestreview-197771905 --- src/Model/Item.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index c5b952a660..c08eb861cc 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2441,22 +2441,20 @@ class Item extends BaseObject "#$2", $item["body"]); foreach ($tags as $tag) { - if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=')) { + if ((strpos($tag, '#') !== 0) || strpos($tag, '[url=') || $tag[1] == '#') { continue; } $basetag = str_replace('_',' ',substr($tag,1)); - if($basetag[0] != '#') { - $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; + $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; - $item["body"] = str_replace($tag, $newtag, $item["body"]); + $item["body"] = str_replace($tag, $newtag, $item["body"]); - if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) { - if (strlen($item["tag"])) { - $item["tag"] = ',' . $item["tag"]; - } - $item["tag"] = $newtag . $item["tag"]; + if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) { + if (strlen($item["tag"])) { + $item["tag"] = ',' . $item["tag"]; } + $item["tag"] = $newtag . $item["tag"]; } }