]> git.mxchange.org Git - friendica-addons.git/commitdiff
Merge remote-tracking branch 'upstream/master'
authorThomas Willingham <founder@kakste.com>
Wed, 4 Apr 2012 17:09:29 +0000 (18:09 +0100)
committerThomas Willingham <founder@kakste.com>
Wed, 4 Apr 2012 17:09:29 +0000 (18:09 +0100)
facebook.tgz
facebook/facebook.php
nsfw.tgz
wppost.tgz
wppost/wppost.php

index 1d1919d2fba7d910ac7a80e77690b270f93c5446..5853b9168ced9f21294dc32e805e94970a3cfdb4 100644 (file)
Binary files a/facebook.tgz and b/facebook.tgz differ
index 5ffb0a2226edf66017b8f528aa5e697b8833a1ab..1849cef33485b0fe6136ab1d78f3d953fadda24f 100755 (executable)
  *   e.g. the app_access_token
  */
 
-define('FACEBOOK_MAXPOSTLEN', 420);
+// Size of maximum post length increased
+// see http://www.facebook.com/schrep/posts/203969696349811
+// define('FACEBOOK_MAXPOSTLEN', 420);
+define('FACEBOOK_MAXPOSTLEN', 63206);
 define('FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL', 259200); // 3 days
 define('FACEBOOK_DEFAULT_POLL_INTERVAL', 60); // given in minutes
 define('FACEBOOK_MIN_POLL_INTERVAL', 5);
 
+
 function facebook_install() {
        register_hook('post_local',       'addon/facebook/facebook.php', 'facebook_post_local');
        register_hook('notifier_normal',  'addon/facebook/facebook.php', 'facebook_post_hook');
@@ -846,6 +850,7 @@ function facebook_post_hook(&$a,&$b) {
                                if($b['verb'] == ACTIVITY_DISLIKE)
                                        $msg = trim(strip_tags(bbcode($msg)));
 
+                               // Old code
                                /*$search_str = $a->get_baseurl() . '/search';
 
                                if(preg_match("/\[url=(.*?)\](.*?)\[\/url\]/is",$msg,$matches)) {
@@ -877,23 +882,47 @@ function facebook_post_hook(&$a,&$b) {
 
                                $msg = trim(strip_tags(bbcode($msg)));*/
 
-                               // Test
+                               // New code
 
-                               // Looking for images
+                               // Looking for the first image
+                               $image = '';
                                if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
                                        $image = $matches[3];
 
-                               if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
-                                       $image = $matches[1];
+                               if ($image != '')
+                                       if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
+                                               $image = $matches[1];
+
+                               // Checking for a bookmark element
+                               $body = $b['body'];
+                               if (strpos($body, "[bookmark") !== false) {
+                                       // splitting the text in two parts:
+                                       // before and after the bookmark
+                                       $pos = strpos($body, "[bookmark");
+                                       $body1 = substr($body, 0, $pos);
+                                       $body2 = substr($body, $pos);
+
+                                       // Removing the bookmark and all quotes after the bookmark
+                                       // they are mostly only the content after the bookmark.
+                                       $body2 = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'',$body2);
+                                       $body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
+                                       $body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
+
+                                       $body = $body1.$body2;
+                               }
+
+                               // At first convert the text to html
+                               $html = bbcode($body);
 
-                               $html = bbcode($b['body']);
-                               $msg = trim($b['title']." \n".html2plain($html, 0, true));
+                               // Then convert it to plain text
+                               $msg = trim($b['title']." \n\n".html2plain($html, 0, true));
                                $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
 
-                               $toolong = false;
+                               // Removing multiple newlines
+                               while (strpos($msg, "\n\n\n") !== false)
+                                       $msg = str_replace("\n\n\n", "\n\n", $msg);
 
                                // add any attachments as text urls
-
                                $arr = explode(',',$b['attach']);
 
                                if(count($arr)) {
@@ -907,19 +936,28 @@ function facebook_post_hook(&$a,&$b) {
                                        }
                                }
 
-                               // To-Do: look for bookmark-bbcode and handle it with priority
+                               $link = '';
+                               $linkname = '';
+                               // look for bookmark-bbcode and handle it with priority
+                               if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) {
+                                       $link = $matches[1];
+                                       $linkname = $matches[2];
+                               }
 
-                               $links = collecturls($html);
-                               if (sizeof($links) > 0) {
-                                       reset($links);
-                                       $link = current($links);
-                                       /*if (strlen($msg."\n".$link) <= FACEBOOK_MAXPOSTLEN)
-                                               $msg .= "\n".$link;
-                                       else
-                                               $toolong = true;*/
+                               // If there is no bookmark element then take the first link
+                               if ($link == '') {
+                                       $links = collecturls($html);
+                                       if (sizeof($links) > 0) {
+                                               reset($links);
+                                               $link = current($links);
+                                       }
                                }
 
-                               if ((strlen($msg) > FACEBOOK_MAXPOSTLEN) or $toolong) {
+                               // Remove trailing and leading spaces
+                               $msg = trim($msg);
+
+                               // Since facebook increased the maxpostlen massively this never should happen again :)
+                               if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
                                        $shortlink = "";
                                        require_once('library/slinky.php');
 
@@ -936,7 +974,19 @@ function facebook_post_hook(&$a,&$b) {
                                        $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
                                        $msg .= '... ' . $shortlink;
                                }
-                               if(! strlen($msg))
+
+                               // Fallback - if message is empty
+                               if(!strlen($msg))
+                                       $msg = $link;
+
+                               if(!strlen($msg))
+                                       $msg = $image;
+
+                               if(!strlen($msg))
+                                       $msg = $linkname;
+
+                               // If there is nothing to post then exit
+                               if(!strlen($msg))
                                        return;
 
                                logger('Facebook post: msg=' . $msg, LOGGER_DATA);
@@ -1240,8 +1290,14 @@ function fb_consume_stream($uid,$j,$wall = false) {
                        // don't store post if we don't have a contact
 
                        if(! x($datarray,'contact-id')) {
-                               logger('no contact: post ignored');
-                               continue;
+                               if (get_config('facebook', 'pages')) {
+                                       // If no user is found then post it under the own id.
+                                       // Definitely a quickhack
+                                       $datarray['contact-id'] = $self[0]['id'];
+                               } else {
+                                       logger('no contact: post ignored');
+                                       continue;
+                               }
                        }
 
                        $datarray['verb'] = ACTIVITY_POST;
index b7caadbd4a6f614c3e12d380785068951c2e6372..6897cb89077e099b1266efead83947f5f48971be 100755 (executable)
Binary files a/nsfw.tgz and b/nsfw.tgz differ
index 221943355a96060e6f4205754d05e37a9ec5c826..b07bf6e0d9ac6965afcc92cf5dfaa0204063937d 100755 (executable)
Binary files a/wppost.tgz and b/wppost.tgz differ
index 264a342c0c0733c5934b218bb0f9646984e3257b..28e4e36946af907978a89f16acf6045e1470f60d 100755 (executable)
@@ -168,6 +168,24 @@ function wppost_send(&$a,&$b) {
        if($wp_username && $wp_password && $wp_blog) {
 
                require_once('include/bbcode.php');
+               require_once('include/html2plain.php');
+
+               // If the title is empty then try to guess
+               if ($b['title'] == '') {
+                       // Take the description from the bookmark
+                       if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
+                               $b['title'] = $matches[2];
+
+                       // If no bookmark is found then take the first line
+                       if ($b['title'] == '') {
+                               $title = html2plain(bbcode($b['body']), 0, true);
+                               $pos = strpos($title, "\n");
+                               if (($pos == 0) or ($pos > 60))
+                                       $pos = 60;
+
+                               $b['title'] = substr($title, 0, $pos);
+                       }
+               }
 
                $title = '<title>' . (($b['title']) ? $b['title'] : t('Post from Friendica')) . '</title>';
                $post = $title . bbcode($b['body']);