From 91a9d5e2ef6e7497624a94379bcee82414be75d5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Feb 2018 22:41:25 -0500 Subject: [PATCH 1/2] [twitter] Remove unused function parameter in twitter_expand_entities - Remove duplicate assignment to $tags --- twitter/twitter.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 47b4fa07..279a660f 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1147,10 +1147,8 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") return $contact_id; } -function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture) +function twitter_expand_entities(App $a, $body, $item, $picture) { - $tags = ""; - $plain = $body; if (isset($item->entities->urls)) { @@ -1234,10 +1232,6 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur $body = add_page_info_to_body($body); } - if ($no_tags) { - return ["body" => $body, "tags" => "", "plain" => $plain]; - } - $tags_arr = []; foreach ($item->entities->hashtags AS $hashtag) { @@ -1302,7 +1296,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur * @param object $post Twitter object with the post * @param array $postarray Array of the item that is about to be posted * - * @return $picture string Returns a a single picture string if it isn't a media post + * @return $picture string Image URL or empty string */ function twitter_media_entities($post, &$postarray) { @@ -1480,7 +1474,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis // Search for media links $picture = twitter_media_entities($post, $postarray); - $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture); + $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture); $postarray['body'] = $converted["body"]; $postarray['tag'] = $converted["tags"]; $postarray['created'] = DateTimeFormat::utc($post->created_at); From 471b3788eb3a8abd9f4acc90186c1814020fa934 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Feb 2018 22:43:22 -0500 Subject: [PATCH 2/2] [twitter] Perform mention/hashtags replacements before expanding links - Move mention/hashtags replacement out of URL existence check --- twitter/twitter.php | 93 +++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 279a660f..7a94ebbc 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1151,6 +1151,20 @@ function twitter_expand_entities(App $a, $body, $item, $picture) { $plain = $body; + $tags_arr = []; + + foreach ($item->entities->hashtags AS $hashtag) { + $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]"; + $tags_arr["#" . $hashtag->text] = $url; + $body = str_replace("#" . $hashtag->text, $url, $body); + } + + foreach ($item->entities->user_mentions AS $mention) { + $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]"; + $tags_arr["@" . $mention->screen_name] = $url; + $body = str_replace("@" . $mention->screen_name, $url, $body); + } + if (isset($item->entities->urls)) { $type = ""; $footerurl = ""; @@ -1231,62 +1245,49 @@ function twitter_expand_entities(App $a, $body, $item, $picture) } elseif (($footer == "") && ($picture == "")) { $body = add_page_info_to_body($body); } + } - $tags_arr = []; + // it seems as if the entities aren't always covering all mentions. So the rest will be checked here + $tags = get_tags($body); - foreach ($item->entities->hashtags AS $hashtag) { - $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]"; - $tags_arr["#" . $hashtag->text] = $url; - $body = str_replace("#" . $hashtag->text, $url, $body); - } + if (count($tags)) { + foreach ($tags as $tag) { + if (strstr(trim($tag), " ")) { + continue; + } - foreach ($item->entities->user_mentions AS $mention) { - $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]"; - $tags_arr["@" . $mention->screen_name] = $url; - $body = str_replace("@" . $mention->screen_name, $url, $body); - } - - // it seems as if the entities aren't always covering all mentions. So the rest will be checked here - $tags = get_tags($body); - - if (count($tags)) { - foreach ($tags as $tag) { - if (strstr(trim($tag), " ")) { + if (strpos($tag, '#') === 0) { + if (strpos($tag, '[url=')) { continue; } - if (strpos($tag, '#') === 0) { - if (strpos($tag, '[url=')) { - continue; - } - - // don't link tags that are already embedded in links - if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) { - continue; - } - if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) { - continue; - } - - $basetag = str_replace('_', ' ', substr($tag, 1)); - $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; - $body = str_replace($tag, $url, $body); - $tags_arr["#" . $basetag] = $url; - } elseif (strpos($tag, '@') === 0) { - if (strpos($tag, '[url=')) { - continue; - } - - $basetag = substr($tag, 1); - $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; - $body = str_replace($tag, $url, $body); - $tags_arr["@" . $basetag] = $url; + // don't link tags that are already embedded in links + if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) { + continue; } + if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) { + continue; + } + + $basetag = str_replace('_', ' ', substr($tag, 1)); + $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + $body = str_replace($tag, $url, $body); + $tags_arr["#" . $basetag] = $url; + } elseif (strpos($tag, '@') === 0) { + if (strpos($tag, '[url=')) { + continue; + } + + $basetag = substr($tag, 1); + $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + $body = str_replace($tag, $url, $body); + $tags_arr["@" . $basetag] = $url; } } - - $tags = implode($tags_arr, ","); } + + $tags = implode($tags_arr, ","); + return ["body" => $body, "tags" => $tags, "plain" => $plain]; }