From bbc8a1328dbb7168f42c605325c1aee31b15a5cf Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 26 Jan 2014 09:53:12 +0100 Subject: [PATCH] pumpio, statusnet, tumblr, twitter, wordpress: Changed the way, shares are displayed twitter: Look for the entities when importing tweets. --- fbpost/fbpost.php | 5 +- pumpio/pumpio.php | 10 +-- rendertime/rendertime.php | 6 +- statusnet/statusnet.php | 10 ++- tumblr/tumblr.php | 8 +- twitter/twitter.php | 161 +++++++++++++++++++++++++++++++++++--- wppost/wppost.php | 7 +- 7 files changed, 178 insertions(+), 29 deletions(-) diff --git a/fbpost/fbpost.php b/fbpost/fbpost.php index 48865f04e..7489b17e5 100644 --- a/fbpost/fbpost.php +++ b/fbpost/fbpost.php @@ -764,7 +764,10 @@ function fbpost_post_hook(&$a,&$b) { ); } else { - if(! $likes) { + // Only add to queue if its a toplevel post. + // Sometimes posts are accepted from facebook although it telling an error + // This leads to endless comment flooding. + if(! $likes AND $toplevel) { $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid'])); if (count($r)) $a->contact = $r[0]["id"]; diff --git a/pumpio/pumpio.php b/pumpio/pumpio.php index 86975e477..ac28a00da 100755 --- a/pumpio/pumpio.php +++ b/pumpio/pumpio.php @@ -428,13 +428,13 @@ function pumpio_send(&$a,&$b) { if ($title != '') $title = "

".$title."

"; - $content = bbcode($b['body'], false, false); + $content = bbcode($b['body'], false, false, 4); // Enhance the way, videos are displayed - $content = preg_replace('/(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content); - $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); - $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); - $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); + $content = preg_replace('/(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content); + $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); + $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); + $content = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$content); $URLSearchString = "^\[\]"; $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content); diff --git a/rendertime/rendertime.php b/rendertime/rendertime.php index 1521dcfd5..213ed316a 100755 --- a/rendertime/rendertime.php +++ b/rendertime/rendertime.php @@ -26,10 +26,8 @@ function rendertime_page_end(&$a, &$o) { $duration = microtime(true)-$a->performance["start"]; - if (!is_site_admin()) - return - - $o = $o.'
'.sprintf(t("Performance: Database: %s, Network: %s, Rendering: %s, Parser: %s, I/O: %s, Other: %s, Total: %s"), + if (is_site_admin()) + $o = $o.'
'.sprintf(t("Performance: Database: %s, Network: %s, Rendering: %s, Parser: %s, I/O: %s, Other: %s, Total: %s"), round($a->performance["database"], 3), round($a->performance["network"], 3), round($a->performance["rendering"], 3), diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index b56bea059..39aec43b2 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -448,19 +448,21 @@ function short_link($url) { } }; function statusnet_shortenmsg($b, $max_char) { + require_once("include/api.php"); require_once("include/bbcode.php"); require_once("include/html2plain.php"); // Looking for the first image + $cleaned_body = api_clean_plain_items($b['body']); $image = ''; - if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches)) + if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$cleaned_body,$matches)) $image = $matches[3]; if ($image == '') - if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches)) + if(preg_match("/\[img\](.*?)\[\/img\]/is",$cleaned_body,$matches)) $image = $matches[1]; - $multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img")); + $multipleimages = (strpos($cleaned_body, "[img") != strrpos($cleaned_body, "[img")); // When saved into the database the content is sent through htmlspecialchars // That means that we have to decode all image-urls @@ -500,7 +502,7 @@ function statusnet_shortenmsg($b, $max_char) { //$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body); // At first convert the text to html - $html = bbcode($body, false, false, 2); + $html = bbcode(api_clean_plain_items($body), false, false, 2, true); // Then convert it to plain text //$msg = trim($b['title']." \n\n".html2plain($html, 0, true)); diff --git a/tumblr/tumblr.php b/tumblr/tumblr.php index c07dc4cff..0102d6334 100755 --- a/tumblr/tumblr.php +++ b/tumblr/tumblr.php @@ -350,18 +350,18 @@ function tumblr_send(&$a,&$b) { $params['embed'] = $link; if ($title != '') $params['caption'] = '

'.$title. - "

".bbcode($body, false, false)."

"; + "

".bbcode($body, false, false, 4)."

"; else - $params['caption'] = bbcode($body, false, false); + $params['caption'] = bbcode($body, false, false, 4); } else if (($link != '') and !$video) { $params['type'] = "link"; $params['title'] = $title; $params['url'] = $link; - $params['description'] = bbcode($b["body"], false, false); + $params['description'] = bbcode($b["body"], false, false, 4); } else { $params['type'] = "text"; $params['title'] = $title; - $params['body'] = bbcode($b['body'], false, false); + $params['body'] = bbcode($b['body'], false, false, 4); } $consumer_key = get_config('tumblr','consumer_key'); diff --git a/twitter/twitter.php b/twitter/twitter.php index 0043a3a9b..c61e94e49 100755 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -372,21 +372,23 @@ function short_link ($url) { } }; function twitter_shortenmsg($b, $shortlink = false) { + require_once("include/api.php"); require_once("include/bbcode.php"); require_once("include/html2plain.php"); $max_char = 140; // Looking for the first image + $cleaned_body = api_clean_plain_items($b['body']); $image = ''; - if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches)) + if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$cleaned_body,$matches)) $image = $matches[3]; if ($image == '') - if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches)) + if(preg_match("/\[img\](.*?)\[\/img\]/is",$cleaned_body,$matches)) $image = $matches[1]; - $multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img")); + $multipleimages = (strpos($cleaned_body, "[img") != strrpos($cleaned_body, "[img")); // When saved into the database the content is sent through htmlspecialchars // That means that we have to decode all image-urls @@ -426,7 +428,7 @@ function twitter_shortenmsg($b, $shortlink = false) { //$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body); // At first convert the text to html - $html = bbcode($body, false, false, 2); + $html = bbcode(api_clean_plain_items($body), false, false, 2, true); // Then convert it to plain text $msg = trim(html2plain($html, 0, true)); @@ -1002,7 +1004,8 @@ function twitter_fetchtimeline($a, $uid) { } } - $converted = twitter_convertmsg($a, $_REQUEST['body'], true, $has_picture); + //$converted = twitter_convertmsg($a, $_REQUEST['body'], true, $has_picture); + $converted = twitter_expand_entities($a, $_REQUEST['body'], $post->retweeted_status, true, $has_picture); $_REQUEST['body'] = $converted["body"]; $_REQUEST['body'] = "[share author='".$post->retweeted_status->user->name. @@ -1025,7 +1028,8 @@ function twitter_fetchtimeline($a, $uid) { } } - $converted = twitter_convertmsg($a, $_REQUEST["body"], true, $has_picture); + //$converted = twitter_convertmsg($a, $_REQUEST["body"], true, $has_picture); + $converted = twitter_expand_entities($a, $_REQUEST["body"], $post, true, $has_picture); $_REQUEST['body'] = $converted["body"]; } @@ -1297,6 +1301,141 @@ function twitter_fetchuser($a, $uid, $screen_name = "", $user_id = "") { return $contact_id; } +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)); +} + function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing_contact) { $has_picture = false; @@ -1405,7 +1544,8 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing } } - $converted = twitter_convertmsg($a, $postarray['body'], false, $has_picture); + //$converted = twitter_convertmsg($a, $postarray['body'], false, $has_picture); + $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $has_picture); $postarray['body'] = $converted["body"]; $postarray['tag'] = $converted["tags"]; @@ -1442,7 +1582,8 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing } } - $converted = twitter_convertmsg($a, $postarray['body'], false, $has_picture); + //$converted = twitter_convertmsg($a, $postarray['body'], false, $has_picture); + $converted = twitter_expand_entities($a, $postarray['body'], $post->retweeted_status, false, $has_picture); $postarray['body'] = $converted["body"]; $postarray['tag'] = $converted["tags"]; @@ -1854,7 +1995,7 @@ function twitter_convertmsg($a, $body, $no_tags = false, $dontincludemedia) { } if ($no_tags) - return(array("body" => $body, $tags => "")); + return(array("body" => $body, "tags" => "")); $str_tags = ''; @@ -1980,6 +2121,8 @@ function twitter_is_retweet($a, $uid, $body) { $result = $connection->post('statuses/retweet/'.$id); + logger('twitter_is_retweet: result '.print_r($result, true), LOGGER_DEBUG); + return(!isset($result->errors)); } diff --git a/wppost/wppost.php b/wppost/wppost.php index 9cb258200..4c862151e 100755 --- a/wppost/wppost.php +++ b/wppost/wppost.php @@ -194,7 +194,10 @@ function wppost_send(&$a,&$b) { // If no bookmark is found then take the first line if ($wptitle == '') { - $title = html2plain(bbcode($b['body'], false, false), 0, true)."\n"; + // Remove the share element before fetching the first line + $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body'])); + + $title = html2plain(bbcode($title, false, false), 0, true)."\n"; $pos = strpos($title, "\n"); $trailer = ""; if (($pos == 0) or ($pos > 100)) { @@ -207,7 +210,7 @@ function wppost_send(&$a,&$b) { } $title = '' . (($wptitle) ? $wptitle : t('Post from Friendica')) . ''; - $post = bbcode($b['body'], false, false); + $post = bbcode($b['body'], false, false, 4); // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it $post = preg_replace('/(.*?)<\/a>/ism',"\n$1\n",$post);