From e688a2608a1593a8f6d71b278aff3bbf62399179 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Thu, 29 Mar 2012 20:04:54 +0200 Subject: [PATCH 1/7] The "bookmark" element is now supported --- facebook/facebook.php | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index 20690eddc..a15e741db 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -833,7 +833,10 @@ function facebook_post_hook(&$a,&$b) { if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches)) $image = $matches[1]; - $html = bbcode($b['body']); + // Replace bookmark with url + $body = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$b['body']); + + $html = bbcode($body); $msg = trim($b['title']." \n".html2plain($html, 0, true)); $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8'); @@ -854,16 +857,21 @@ function facebook_post_hook(&$a,&$b) { } } - // To-Do: look for bookmark-bbcode and handle it with priority + $link = ''; + // look for bookmark-bbcode and handle it with priority + if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) + $link = $matches[1]; - $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 ($link == '') { + $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 ((strlen($msg) > FACEBOOK_MAXPOSTLEN) or $toolong) { From 5e9405e9dcb4fe18b744f1f52fbea5b875c0e09e Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 31 Mar 2012 16:34:28 +0200 Subject: [PATCH 2/7] Maximum limit for facebook is now max higher than 400. --- facebook/facebook.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index a15e741db..7282315d5 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -53,7 +53,10 @@ * - 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); function facebook_install() { @@ -891,7 +894,8 @@ function facebook_post_hook(&$a,&$b) { $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4); $msg .= '... ' . $shortlink; } - if(! strlen($msg)) + + if(!strlen($msg) and !strlen($link) and !strlen($image)) return; logger('Facebook post: msg=' . $msg, LOGGER_DATA); From 97bbcad0f598ced002540f7a04bef7e38d2240c4 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 31 Mar 2012 17:42:17 +0200 Subject: [PATCH 3/7] Facebook: Removing useless content when posting to facebook. Now content on facebook really looks great. --- facebook/facebook.php | 48 +++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index 7282315d5..a3d8a5b29 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -796,6 +796,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)) { @@ -827,26 +828,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]; - // Replace bookmark with url - $body = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'[url=$1]$2[/url]',$b['body']); + // 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); - $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)) { @@ -865,19 +887,19 @@ function facebook_post_hook(&$a,&$b) { if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) $link = $matches[1]; + // 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."\n".$link) <= FACEBOOK_MAXPOSTLEN) - $msg .= "\n".$link; - else - $toolong = true;*/ + $msg .= "\n".$link;*/ } } - if ((strlen($msg) > FACEBOOK_MAXPOSTLEN) or $toolong) { + // Since facebook increased the maxpostlen massively this never should happen again :) + if (strlen($msg) > FACEBOOK_MAXPOSTLEN) { $shortlink = ""; require_once('library/slinky.php'); From 79f5e9d0837290658b0119eafd81c420c09150fc Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 1 Apr 2012 13:44:39 +0200 Subject: [PATCH 4/7] Handling of empty messages improved. They weren't posted. --- facebook/facebook.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index a3d8a5b29..bc492f683 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -883,9 +883,12 @@ function facebook_post_hook(&$a,&$b) { } $link = ''; + $linkname = ''; // look for bookmark-bbcode and handle it with priority - if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) + if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) { $link = $matches[1]; + $linkname = $matches[2]; + } // If there is no bookmark element then take the first link if ($link == '') { @@ -893,11 +896,12 @@ function facebook_post_hook(&$a,&$b) { if (sizeof($links) > 0) { reset($links); $link = current($links); - /*if (strlen($msg."\n".$link) <= FACEBOOK_MAXPOSTLEN) - $msg .= "\n".$link;*/ } } + // 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 = ""; @@ -917,7 +921,18 @@ function facebook_post_hook(&$a,&$b) { $msg .= '... ' . $shortlink; } - if(!strlen($msg) and !strlen($link) and !strlen($image)) + // 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); From 26ad8dc79aed27a84a8576d25cee6d09f4d89ad1 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 1 Apr 2012 16:30:25 +0200 Subject: [PATCH 5/7] facebook: If the body is empty then the link is taken. --- facebook/facebook.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index 1afa996df..64eb3ee24 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -956,15 +956,15 @@ function facebook_post_hook(&$a,&$b) { } // Fallback - if message is empty - if(!strlen($msg)) - $msg = $linkname; - 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; From 483043eb1b009b451bd16cfcdf01e1d759ee9fb8 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 1 Apr 2012 19:20:40 +0200 Subject: [PATCH 6/7] wppost: Mechanism for guessing the title added --- wppost/wppost.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/wppost/wppost.php b/wppost/wppost.php index 264a342c0..28e4e3694 100755 --- a/wppost/wppost.php +++ b/wppost/wppost.php @@ -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 = '' . (($b['title']) ? $b['title'] : t('Post from Friendica')) . ''; $post = $title . bbcode($b['body']); From 9ff72061f21056760ce06bbe36527aa3637bf4e3 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Mon, 2 Apr 2012 22:37:12 +0200 Subject: [PATCH 7/7] Facebook: Design issues in the export --- facebook/facebook.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/facebook/facebook.php b/facebook/facebook.php index 64eb3ee24..58ce025bb 100755 --- a/facebook/facebook.php +++ b/facebook/facebook.php @@ -1270,8 +1270,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;