]> git.mxchange.org Git - friendica-addons.git/commitdiff
pumpio, statusnet, tumblr, twitter, wordpress: Changed the way, shares are displayed
authorMichael Vogel <icarus@dabo.de>
Sun, 26 Jan 2014 08:53:12 +0000 (09:53 +0100)
committerMichael Vogel <icarus@dabo.de>
Sun, 26 Jan 2014 08:53:12 +0000 (09:53 +0100)
twitter: Look for the entities when importing tweets.

fbpost/fbpost.php
pumpio/pumpio.php
rendertime/rendertime.php
statusnet/statusnet.php
tumblr/tumblr.php
twitter/twitter.php
wppost/wppost.php

index 48865f04e830c368fab94d053b38a58b8861c98a..7489b17e5192f110c2fc095b5086d90734c2e1ec 100644 (file)
@@ -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"];
index 86975e47774799e989fd6c4d68689eadd56f3e70..ac28a00da22575c0b8a49390cc6a53b42526d980 100755 (executable)
@@ -428,13 +428,13 @@ function pumpio_send(&$a,&$b) {
                if ($title != '')
                        $title = "<h4>".$title."</h4>";
 
-               $content = bbcode($b['body'], false, false);
+               $content = bbcode($b['body'], false, false, 4);
 
                // Enhance the way, videos are displayed
-               $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
-               $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
-               $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
-               $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
+               $content = preg_replace('/<a href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
+               $content = preg_replace('/<a href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
+               $content = preg_replace('/<a href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
+               $content = preg_replace('/<a href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
 
                $URLSearchString = "^\[\]";
                $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
index 1521dcfd575c4bb8ec50a5030fa407b149bad0d7..213ed316aa479d22b340ee889206071d4a4d9a34 100755 (executable)
@@ -26,10 +26,8 @@ function rendertime_page_end(&$a, &$o) {
 
        $duration = microtime(true)-$a->performance["start"];
 
-       if (!is_site_admin())
-               return
-
-       $o = $o.'<div class="renderinfo">'.sprintf(t("Performance: Database: %s, Network: %s, Rendering: %s, Parser: %s, I/O: %s, Other: %s, Total: %s"),
+       if (is_site_admin())
+               $o = $o.'<div class="renderinfo">'.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),
index b56bea059b5a37a363efed69acc377bc0c258716..39aec43b284147fbe6daf8edd751bd5b0a6d7742 100755 (executable)
@@ -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));
index c07dc4cff2710fc6bccf3b3b154eec7c88ad3cdf..0102d6334972514979f88842650fb20d221cb099 100755 (executable)
@@ -350,18 +350,18 @@ function tumblr_send(&$a,&$b) {
                        $params['embed'] = $link;
                        if ($title != '')
                                $params['caption'] = '<h1><a href="'.$link.'">'.$title.
-                                                       "</a></h1><p>".bbcode($body, false, false)."</p>";
+                                                       "</a></h1><p>".bbcode($body, false, false, 4)."</p>";
                        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');
index 0043a3a9bd989de8ec90b2f63af130a1097f4944..c61e94e49ec863c414ec0e7c1d57a09dbf28154d 100755 (executable)
@@ -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));
 }
 
index 9cb258200a202d7c051ab6b8323e88959ac85e70..4c862151e3032577cefb35116073f4d2d86d5e33 100755 (executable)
@@ -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 = '<title>' . (($wptitle) ? $wptitle : t('Post from Friendica')) . '</title>';
-               $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.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);