* Detailed instructions how to use this plugin can be found at
* https://github.com/friendica/friendica/wiki/How-to:-Friendica%E2%80%99s-Facebook-connector
*
- * Vidoes and embeds will not be posted if there is no other content. Links
- * and images will be converted to a format suitable for the Facebook API and
- * long posts truncated - with a link to view the full post.
+ * Vidoes and embeds will not be posted if there is no other content. Links
+ * and images will be converted to a format suitable for the Facebook API and
+ * long posts truncated - with a link to view the full post.
*
* Facebook contacts will not be able to view private photos, as they are not able to
- * authenticate to your site to establish identity. We will address this
+ * authenticate to your site to establish identity. We will address this
* in a future release.
*/
$o .= '<h3>' . t('Facebook Post') . '</h3>';
- if(! $fb_installed) {
+ if(! $fb_installed) {
$o .= '<div id="fbpost-enable-wrapper">';
+ //read_stream,publish_stream,manage_pages,photo_upload,user_groups,offline_access
+
$o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
- . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=read_stream,publish_stream,manage_pages,photo_upload,user_groups,offline_access">' . t('Install Facebook Post connector for this account.') . '</a>';
+ . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,create_note,share_item,video_upload,status_update">' . t('Install Facebook Post connector for this account.') . '</a>';
$o .= '</div>';
}
$o .= '<div id="fbpost-enable-wrapper">';
$o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
- . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=read_stream,publish_stream,manage_pages,photo_upload,user_groups,offline_access">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
+ . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,create_note,share_item,video_upload,status_update">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
$o .= '</div>';
$o .= '<div id="fbpost-post-default-form">';
$o .= "</select>";
- $o .= '<p><input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
+ $o .= '<p><input type="submit" name="submit" value="' . t('Save Settings') . '" /></form></div>';
}
function fbpost_plugin_settings(&$a,&$b) {
$b .= '<div class="settings-block">';
- $b .= '<h3>' . t('Facebook') . '</h3>';
- $b .= '<a href="fbpost">' . t('Facebook Post Settings') . '</a><br />';
+ //$b .= '<h3>' . t('Facebook Post Settings') . '</h3>';
+ $b .= '<a href="fbpost"><h3>' . t('Facebook Post Settings') . '</a></h3>';
$b .= '</div>';
}
require_once("include/bbcode.php");
require_once("include/html2plain.php");
+ $b['body'] = bb_CleanPictureLinks($b['body']);
+
// Looking for the first image
$image = '';
if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
intval($b['parent']),
intval($b['uid'])
);
+ //$r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ // dbesc($b['parent-uri']),
+ // intval($b['uid'])
+ //);
+
+ // is it a reply to a facebook post?
+ // A reply to a toplevel post is only allowed for "real" facebook posts
if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
$reply = substr($r[0]['uri'],4);
- elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
+ elseif(count($r) && (substr($r[0]['extid'],0,4) === 'fb::') AND ($r[0]['id'] != $r[0]['parent']))
$reply = substr($r[0]['extid'],4);
else
return;
return;
}
- if($b['verb'] == ACTIVITY_LIKE)
+ if($b['verb'] == ACTIVITY_LIKE) {
$likes = true;
+ logger('fbpost_post_hook: liking '.print_r($b, true), LOGGER_DEBUG);
+ }
$appid = get_config('facebook', 'appid' );
logger('fbpost_post_hook: original msg=' . $msg, LOGGER_DATA);
+ // To-Do: if it is a reply, then only do a simple bbcode2plain conversion
$msgarr = fbpost_createmsg($b);
$msg = $msgarr["msg"];
$link = $msgarr["link"];
$retj = json_decode($x);
if($retj->id) {
- q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ // Only set the extid when it isn't the toplevel post
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d AND `parent` != %d",
dbesc('fb::' . $retj->id),
+ intval($b['id']),
intval($b['id'])
);
- }
- else {
- if(! $likes) {
+ } else {
+ // Sometimes posts are accepted from facebook although it telling an error
+ // This leads to endless comment flooding.
+
+ // If it is a special kind of failure the post was receiced
+ // Although facebook said it wasn't received ...
+ if (!$likes AND (($retj->error->type != "OAuthException") OR ($retj->error->code != 2)) AND ($x <> "")) {
+ $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
+ if (count($r))
+ $a->contact = $r[0]["id"];
+
$s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $postvars));
require_once('include/queue_fn.php');
add_to_queue($a->contact,NETWORK_FACEBOOK,$s);
+ logger('fbpost_post_hook: Post failed, requeued.', LOGGER_DEBUG);
notice( t('Facebook post failed. Queued for retry.') . EOL);
}
*/
function fbpost_enotify(&$app, &$data) {
if (x($data, 'params') && $data['params']['type'] == NOTIFY_SYSTEM && x($data['params'], 'system_type') && $data['params']['system_type'] == 'facebook_connection_invalid') {
- $data['itemlink'] = '/facebook';
+ $data['itemlink'] = '/fbpost';
$data['epreamble'] = $data['preamble'] = t('Your Facebook connection became invalid. Please Re-authenticate.');
$data['subject'] = t('Facebook connection became invalid');
- $data['body'] = sprintf( t("Hi %1\$s,\n\nThe connection between your accounts on %2\$s and Facebook became invalid. This usually happens after you change your Facebook-password. To enable the connection again, you have to %3\$sre-authenticate the Facebook-connector%4\$s."), $data['params']['to_name'], "[url=" . $app->config["system"]["url"] . "]" . $app->config["sitename"] . "[/url]", "[url=" . $app->config["system"]["url"] . "/facebook]", "[/url]");
+ $data['body'] = sprintf( t("Hi %1\$s,\n\nThe connection between your accounts on %2\$s and Facebook became invalid. This usually happens after you change your Facebook-password. To enable the connection again, you have to %3\$sre-authenticate the Facebook-connector%4\$s."), $data['params']['to_name'], "[url=" . $app->config["system"]["url"] . "]" . $app->config["sitename"] . "[/url]", "[url=" . $app->config["system"]["url"] . "/fbpost]", "[/url]");
}
}
if($x['network'] !== NETWORK_FACEBOOK)
continue;
- logger('facebook_queue: run');
+ logger('fbpost_queue_hook: run');
$r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
intval($x['cid'])
);
- if(! count($r))
+ if(! count($r)) {
+ logger('fbpost_queue_hook: no user found for entry '.print_r($x, true));
+ update_queue_time($x['id']);
continue;
+ }
$user = $r[0];
$fb_token = get_pconfig($user['uid'],'facebook','access_token');
if($fb_post && $fb_token) {
- logger('facebook_queue: able to post');
+ logger('fbpost_queue_hook: able to post');
require_once('library/facebook.php');
$z = unserialize($x['content']);
$retj = json_decode($j);
if($retj->id) {
- q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ // Only set the extid when it isn't the toplevel post
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d AND `parent` != %d",
dbesc('fb::' . $retj->id),
+ intval($item),
intval($item)
);
- logger('facebook_queue: success: ' . $j);
+ logger('fbpost_queue_hook: success: ' . $j);
remove_queue_item($x['id']);
+ } else {
+ logger('fbpost_queue_hook: failed: ' . $j);
+
+ // If it is a special kind of failure the post was receiced
+ // Although facebook said it wasn't received ...
+ $ret = json_decode($j);
+ if (($ret->error->type != "OAuthException") OR ($ret->error->code != 2) AND ($j <> ""))
+ update_queue_time($x['id']);
+ else
+ logger('fbpost_queue_hook: Not requeued, since it seems to be received');
}
- else {
- logger('facebook_queue: failed: ' . $j);
- update_queue_time($x['id']);
- }
+ } else {
+ logger('fbpost_queue_hook: No fb_post or fb_token.');
+ update_queue_time($x['id']);
}
+ } else {
+ logger('fbpost_queue_hook: No appid or secret.');
+ update_queue_time($x['id']);
}
}
}
}
function fbpost_fetchwall($a, $uid) {
+ require_once("include/oembed.php");
+
$access_token = get_pconfig($uid,'facebook','access_token');
$post_to_page = get_pconfig($uid,'facebook','post_to_page');
$lastcreated = get_pconfig($uid,'facebook','last_created');
$_REQUEST["body"] = (isset($item->message) ? escape_tags($item->message) : '');
- if(isset($item->name) and isset($item->link))
- $_REQUEST["body"] .= "\n\n[bookmark=".$item->link."]".$item->name."[/bookmark]";
- elseif (isset($item->name))
- $_REQUEST["body"] .= "\n\n[b]" . $item->name."[/b]";
+ $content = "";
+ $type = "";
- /*if(isset($item->caption)) {
- if(!isset($item->name) and isset($item->link))
- $_REQUEST["body"] .= "\n\n[bookmark=".$item->link."]".$item->caption."[/bookmark]";
- //else
- // $_REQUEST["body"] .= "[i]" . $item->caption."[/i]\n";
- }
-
- if(!isset($item->caption) and !isset($item->name)) {
- if (isset($item->link))
- $_REQUEST["body"] .= "\n[url]".$item->link."[/url]\n";
- else
- $_REQUEST["body"] .= "\n";
- }*/
+ if(isset($item->name) and isset($item->link)) {
+ $oembed_data = oembed_fetch_url($item->link);
+ $type = $oembed_data->type;
+ $content = "[bookmark=".$item->link."]".$item->name."[/bookmark]";
+ } elseif (isset($item->name))
+ $content .= "[b]".$item->name."[/b]";
$quote = "";
if(isset($item->description) and ($item->type != "photo"))
if(isset($item->caption) and ($item->type == "photo"))
$quote = $item->caption;
- //if (isset($item->properties))
- // foreach ($item->properties as $property)
- // $quote .= "\n".$property->name.": [url=".$property->href."]".$property->text."[/url]";
-
- if ($quote)
- $_REQUEST["body"] .= "\n[quote]".$quote."[/quote]";
-
// Only import the picture when the message is no video
// oembed display a picture of the video as well
- if ($item->type != "video") {
+ //if ($item->type != "video") {
//if (($item->type != "video") and ($item->type != "photo")) {
+ if (($type == "") OR ($type == "link")) {
+
+ $type = $item->type;
+
if(isset($item->picture) && isset($item->link))
- $_REQUEST["body"] .= "\n".'[url='.$item->link.'][img]'.fpost_cleanpicture($item->picture).'[/img][/url]';
+ $content .= "\n".'[url='.$item->link.'][img]'.fpost_cleanpicture($item->picture).'[/img][/url]';
else {
if (isset($item->picture))
- $_REQUEST["body"] .= "\n".'[img]'.fpost_cleanpicture($item->picture).'[/img]';
+ $content .= "\n".'[img]'.fpost_cleanpicture($item->picture).'[/img]';
// if just a link, it may be a wall photo - check
if(isset($item->link))
- $_REQUEST["body"] .= fbpost_get_photo($uid,$item->link);
+ $content .= fbpost_get_photo($uid,$item->link);
}
}
- /*if (($datarray['app'] == "Events") and isset($item->actions))
- foreach ($item->actions as $action)
- if ($action->name == "View")
- $_REQUEST["body"] .= " [url=".$action->link."]".$item->story."[/url]";
- */
-
- if(trim($_REQUEST["body"]) == '') {
+ if(trim($_REQUEST["body"].$content.$quote) == '') {
logger('facebook: empty body '.$item->id.' '.print_r($item, true));
continue;
}
+ if ($content)
+ $_REQUEST["body"] .= "\n\n";
+
+ if ($type)
+ $_REQUEST["body"] .= "[class=type-".$type."]";
+
+ if ($content)
+ $_REQUEST["body"] .= trim($content);
+
+ if ($quote)
+ $_REQUEST["body"] .= "\n[quote]".$quote."[/quote]";
+
+ if ($type)
+ $_REQUEST["body"] .= "[/class]";
+
$_REQUEST["body"] = trim($_REQUEST["body"]);
if (isset($item->place)) {