]> git.mxchange.org Git - friendica-addons.git/blobdiff - facebook/facebook.php
Merge commit 'upstream/master'
[friendica-addons.git] / facebook / facebook.php
index 9f873284e54a90e00699f35a1ab6bd56fca70302..1afa996df08aab3163ce906f024acef65093bba3 100755 (executable)
  * - Implement a configuration option to set the polling interval system-wide
  */
 
-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
 
-
 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');
@@ -828,6 +830,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)) {
@@ -859,23 +862,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;
+                               }
 
-                               $html = bbcode($b['body']);
-                               $msg = trim($b['title']." \n".html2plain($html, 0, true));
+                               // At first convert the text to html
+                               $html = bbcode($body);
+
+                               // 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)) {
@@ -889,19 +916,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');
 
@@ -918,7 +954,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 = $linkname;
+
+                               if(!strlen($msg))
+                                       $msg = $link;
+
+                               if(!strlen($msg))
+                                       $msg = $image;
+
+                               // If there is nothing to post then exit
+                               if(!strlen($msg))
                                        return;
 
                                logger('Facebook post: msg=' . $msg, LOGGER_DATA);