+function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontincludemedia) {
+ require_once("include/oembed.php");
+
+ $tags = "";
+
+ if (isset($item->entities->urls)) {
+ $type = "";
+ $footerurl = "";
+ $footerlink = "";
+ $footer = "";
+
+ foreach ($item->entities->urls AS $url) {
+ if ($url->url AND $url->expanded_url AND $url->display_url) {
+
+ $expanded_url = twitter_original_url($url->expanded_url);
+
+ $oembed_data = oembed_fetch_url($expanded_url);
+
+ // Quickfix: Workaround for URL with "[" and "]" in it
+ if (strpos($expanded_url, "[") OR strpos($expanded_url, "]"))
+ $expanded_url = $url->url;
+
+ if ($type == "")
+ $type = $oembed_data->type;
+
+ if ($oembed_data->type == "video") {
+ $body = str_replace($url->url,
+ "[video]".$expanded_url."[/video]", $body);
+ $dontincludemedia = true;
+ } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
+ $body = str_replace($url->url,
+ "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]",
+ $body);
+ $dontincludemedia = true;
+ } elseif ($oembed_data->type != "link")
+ $body = str_replace($url->url,
+ "[url=".$expanded_url."]".$expanded_url."[/url]",
+ $body);
+ //"[url=".$expanded_url."]".$url->display_url."[/url]",
+ else {
+ $img_str = fetch_url($expanded_url, true, $redirects, 4);
+
+ $tempfile = tempnam(get_config("system","temppath"), "cache");
+ file_put_contents($tempfile, $img_str);
+ $mime = image_type_to_mime_type(exif_imagetype($tempfile));
+ unlink($tempfile);
+
+ if (substr($mime, 0, 6) == "image/") {
+ $type = "photo";
+ $body = str_replace($url->url, "[img]".$expanded_url."[/img]", $body);
+ $dontincludemedia = true;
+ } else {
+ $type = $oembed_data->type;
+ $footerurl = $expanded_url;
+ $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
+ //$footerlink = "[url=".$expanded_url."]".$url->display_url."[/url]";
+
+ $body = str_replace($url->url, $footerlink, $body);
+ }
+ }
+ }
+ }
+
+ if ($footerurl != "")
+ $footer = twitter_siteinfo($footerurl, $dontincludemedia);
+
+ if (($footerlink != "") AND (trim($footer) != "")) {
+ $removedlink = trim(str_replace($footerlink, "", $body));
+
+ if (strstr($body, $removedlink))
+ $body = $removedlink;
+
+ $body .= "\n\n[class=type-".$type."]".$footer."[/class]";
+ }
+
+ if ($no_tags)
+ return(array("body" => $body, "tags" => ""));
+
+ $tags_arr = array();
+
+ 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);
+ }
+
+ // 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), " "))
+ 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;
+ continue;
+ } 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, ",");
+
+ }
+ return(array("body" => $body, "tags" => $tags));
+}
+