From 4874f7f8abebaf242e274d167ffd335897435cdf Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 22 Nov 2018 09:23:42 -0500 Subject: [PATCH 1/3] Remove useless rawurlencode calls for building hashtag bbcode - Fix some formatting --- include/items.php | 4 ++-- mod/photos.php | 2 +- mod/tagger.php | 4 ++-- src/Model/Item.php | 6 +++--- src/Model/Term.php | 22 +++++++++++----------- src/Protocol/Feed.php | 2 +- src/Protocol/OStatus.php | 10 +++++----- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/include/items.php b/include/items.php index b9e1a0c994..c311383613 100644 --- a/include/items.php +++ b/include/items.php @@ -97,7 +97,7 @@ function add_page_info_data(array $data, $no_photos = false) /// @TODO make a positive list of allowed characters $hashtag = str_replace([" ", "+", "/", ".", "#", "'", "’", "`", "(", ")", "„", "“"], ["", "", "", "", "", "", "", "", "", "", "", ""], $keyword); - $hashtags .= "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url] "; + $hashtags .= "#[url=" . System::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url] "; } } @@ -148,7 +148,7 @@ function add_page_keywords($url, $photo = "", $keywords = false, $keyword_blackl $tags .= ", "; } - $tags .= "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]"; + $tags .= "#[url=" . System::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url]"; } } diff --git a/mod/photos.php b/mod/photos.php index 65a48c4c3f..70e0e1882d 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -613,7 +613,7 @@ function photos_post(App $a) } $profile = str_replace(',', '%2c', $profile); - $str_tags .= '@[url='.$profile.']'.$newname.'[/url]'; + $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]'; } } elseif (strpos($tag, '#') === 0) { $tagname = substr($tag, 1); diff --git a/mod/tagger.php b/mod/tagger.php index 6c3c6157a5..09668cd6de 100644 --- a/mod/tagger.php +++ b/mod/tagger.php @@ -93,7 +93,7 @@ function tagger_content(App $a) { EOT; - $tagid = System::baseUrl() . '/search?tag=' . $term; + $tagid = System::baseUrl() . '/search?tag=' . $xterm; $objtype = ACTIVITY_OBJ_TAGTERM; $obj = <<< EOT @@ -113,7 +113,7 @@ EOT; return; } - $termlink = html_entity_decode('⌗') . '[url=' . System::baseUrl() . '/search?tag=' . urlencode($term) . ']'. $term . '[/url]'; + $termlink = html_entity_decode('⌗') . '[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]'; $arr = []; diff --git a/src/Model/Item.php b/src/Model/Item.php index 9aaac7f7a0..0c420550b8 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2448,15 +2448,15 @@ class Item extends BaseObject $basetag = str_replace('_',' ',substr($tag,1)); - $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]'; $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"] = ',' . $item["tag"]; } - $item["tag"] = $newtag.$item["tag"]; + $item["tag"] = $newtag . $item["tag"]; } } diff --git a/src/Model/Term.php b/src/Model/Term.php index 2870eb167d..da07659b4e 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -266,25 +266,25 @@ class Term $tag["url"] = $searchpath . $tag["term"]; } - $orig_tag = $tag["url"]; + $orig_tag = $tag['url']; $author = ['uid' => 0, 'id' => $item['author-id'], 'network' => $item['author-network'], 'url' => $item['author-link']]; - $tag["url"] = Contact::magicLinkByContact($author, $tag['url']); + $tag['url'] = Contact::magicLinkByContact($author, $tag['url']); - if ($tag["type"] == TERM_HASHTAG) { - if ($orig_tag != $tag["url"]) { - $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']); + if ($tag['type'] == TERM_HASHTAG) { + if ($orig_tag != $tag['url']) { + $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']); } - $return['hashtags'][] = "#" . $tag["term"] . ""; - $prefix = "#"; - } elseif ($tag["type"] == TERM_MENTION) { - $return['mentions'][] = "@" . $tag["term"] . ""; - $prefix = "@"; + $return['hashtags'][] = '#' . $tag['term'] . ''; + $prefix = '#'; + } elseif ($tag['type'] == TERM_MENTION) { + $return['mentions'][] = '@' . $tag['term'] . ''; + $prefix = '@'; } - $return['tags'][] = $prefix . "" . $tag["term"] . ""; + $return['tags'][] = $prefix . '' . $tag['term'] . ''; } DBA::close($taglist); diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index d63b57b8c9..5939e5ce1a 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -350,7 +350,7 @@ class Feed { $tags .= ', '; } - $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]"; + $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url]"; $tags .= $taglink; } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 875880a936..45000d7700 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -634,15 +634,15 @@ class OStatus if ($categories) { foreach ($categories as $category) { foreach ($category->attributes as $attributes) { - if ($attributes->name == "term") { + if ($attributes->name == 'term') { $term = $attributes->textContent; - if (!empty($item["tag"])) { - $item["tag"] .= ','; + if (!empty($item['tag'])) { + $item['tag'] .= ','; } else { - $item["tag"] = ''; + $item['tag'] = ''; } - $item["tag"] .= "#[url=".System::baseUrl()."/search?tag=".$term."]".$term."[/url]"; + $item['tag'] .= '#[url=' . System::baseUrl() . '/search?tag=' . $term . ']' . $term . '[/url]'; } } } From 81f89ccae82786c5a99de921e1a44e8b47d8d592 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 22 Nov 2018 09:33:42 -0500 Subject: [PATCH 2/3] Remove stored URL from hashtag terms --- mod/tagger.php | 4 ++-- src/Model/Term.php | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mod/tagger.php b/mod/tagger.php index 09668cd6de..5a8047414d 100644 --- a/mod/tagger.php +++ b/mod/tagger.php @@ -170,7 +170,7 @@ EOT; $term_objtype, TERM_HASHTAG, DBA::escape($term), - DBA::escape(System::baseUrl() . '/search?tag=' . $term), + '', intval($owner_uid) ); } @@ -192,7 +192,7 @@ EOT; $term_objtype, TERM_HASHTAG, DBA::escape($term), - DBA::escape(System::baseUrl() . '/search?tag=' . $term), + '', intval($owner_uid) ); } diff --git a/src/Model/Term.php b/src/Model/Term.php index da07659b4e..3718887122 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -140,6 +140,7 @@ class Term $type = TERM_HASHTAG; $term = substr($tag, 1); + $link = ''; } elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) { $type = TERM_MENTION; @@ -152,6 +153,7 @@ class Term } else { // This shouldn't happen $type = TERM_HASHTAG; $term = $tag; + $link = ''; } if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'url' => $link])) { @@ -262,8 +264,8 @@ class Term ); while ($tag = DBA::fetch($taglist)) { - if ($tag["url"] == "") { - $tag["url"] = $searchpath . $tag["term"]; + if ($tag['url'] == '') { + $tag['url'] = $searchpath . rawurlencode($tag['term']); } $orig_tag = $tag['url']; From ee88de1ef994d77051657e94d8a30f734dd454a9 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 22 Nov 2018 09:34:17 -0500 Subject: [PATCH 3/3] Normalize BBCode Hashtag replacement --- src/Content/Text/BBCode.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 9c9adec0fa..cb375dcd21 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -28,6 +28,7 @@ use Friendica\Util\Network; use Friendica\Util\ParseUrl; use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Strings; +use Friendica\Util\XML; class BBCode extends BaseObject { @@ -1340,15 +1341,21 @@ class BBCode extends BaseObject $expression = "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism"; $text = preg_replace($expression, System::baseUrl()."/display/$1", $text); - $text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", - '$1$3', $text); - - $text = preg_replace("/\[url\=([$URLSearchString]*)\]#(.*?)\[\/url\]/ism", - '#$2', $text); + /* Tag conversion + * Supports: + * - #[url=][/url] + * - [url=]#[/url] + */ + $text = preg_replace_callback("/(?:#\[url\=[$URLSearchString]*\]|\[url\=[$URLSearchString]*\]#)(.*?)\[\/url\]/ism", function($matches) { + return '#' + . XML::escape($matches[1]) + . ''; + }, $text); $text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $text); $text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $text); - //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text); // Red compatibility, though the link can't be authenticated on Friendica $text = preg_replace("/\[zrl\=([$URLSearchString]*)\](.*?)\[\/zrl\]/ism", '$2', $text);