* Author: Tobias Hößl
*
*/
/**
* Installing the Friendica/Facebook connector
*
* 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.
*
* 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
* in a future release.
*/
require_once('include/security.php');
function fbpost_install() {
register_hook('post_local', 'addon/fbpost/fbpost.php', 'fbpost_post_local');
register_hook('notifier_normal', 'addon/fbpost/fbpost.php', 'fbpost_post_hook');
register_hook('jot_networks', 'addon/fbpost/fbpost.php', 'fbpost_jot_nets');
register_hook('connector_settings', 'addon/fbpost/fbpost.php', 'fbpost_plugin_settings');
register_hook('enotify', 'addon/fbpost/fbpost.php', 'fbpost_enotify');
register_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
}
function fbpost_uninstall() {
unregister_hook('post_local', 'addon/fbpost/fbpost.php', 'fbpost_post_local');
unregister_hook('notifier_normal', 'addon/fbpost/fbpost.php', 'fbpost_post_hook');
unregister_hook('jot_networks', 'addon/fbpost/fbpost.php', 'fbpost_jot_nets');
unregister_hook('connector_settings', 'addon/fbpost/fbpost.php', 'fbpost_plugin_settings');
unregister_hook('enotify', 'addon/fbpost/fbpost.php', 'fbpost_enotify');
unregister_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
}
/* declare the fbpost_module function so that /fbpost url requests will land here */
function fbpost_module() {}
// If a->argv[1] is a nickname, this is a callback from Facebook oauth requests.
// If $_REQUEST["realtime_cb"] is set, this is a callback from the Real-Time Updates API
/**
* @param App $a
*/
function fbpost_init(&$a) {
if($a->argc != 2)
return;
$nick = $a->argv[1];
if(strlen($nick))
$r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
dbesc($nick)
);
if(!(isset($r) && count($r)))
return;
$uid = $r[0]['uid'];
$auth_code = (x($_GET, 'code') ? $_GET['code'] : '');
$error = (x($_GET, 'error_description') ? $_GET['error_description'] : '');
if($error)
logger('fbpost_init: Error: ' . $error);
if($auth_code && $uid) {
$appid = get_config('facebook','appid');
$appsecret = get_config('facebook', 'appsecret');
$x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
. $appid . '&client_secret=' . $appsecret . '&redirect_uri='
. urlencode($a->get_baseurl() . '/fbpost/' . $nick)
. '&code=' . $auth_code);
logger('fbpost_init: returned access token: ' . $x, LOGGER_DATA);
if(strpos($x,'access_token=') !== false) {
$token = str_replace('access_token=', '', $x);
if(strpos($token,'&') !== false)
$token = substr($token,0,strpos($token,'&'));
set_pconfig($uid,'facebook','access_token',$token);
set_pconfig($uid,'facebook','post','1');
fbpost_get_self($uid);
}
}
}
/**
* @param int $uid
*/
function fbpost_get_self($uid) {
$access_token = get_pconfig($uid,'facebook','access_token');
if(! $access_token)
return;
$s = fetch_url('https://graph.facebook.com/me/?access_token=' . $access_token);
if($s) {
$j = json_decode($s);
set_pconfig($uid,'facebook','self_id',(string) $j->id);
}
}
// This is the POST method to the facebook settings page
// Content is posted to Facebook in the function facebook_post_hook()
/**
* @param App $a
*/
function fbpost_post(&$a) {
$uid = local_user();
if($uid){
$fb_limited = get_config('facebook','crestrict');
$value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
set_pconfig($uid,'facebook','post_by_default', $value);
info( t('Settings updated.') . EOL);
}
return;
}
// Facebook settings form
/**
* @param App $a
* @return string
*/
function fbpost_content(&$a) {
if(! local_user()) {
notice( t('Permission denied.') . EOL);
return '';
}
if(! service_class_allows(local_user(),'facebook_connect')) {
notice( t('Permission denied.') . EOL);
return upgrade_bool_message();
}
if($a->argc > 1 && $a->argv[1] === 'remove') {
del_pconfig(local_user(),'facebook','post');
info( t('Facebook Post disabled') . EOL);
}
$o = '';
$fb_installed = false;
if (get_pconfig(local_user(),'facebook','post')) {
$access_token = get_pconfig(local_user(),'facebook','access_token');
if ($access_token) {
$s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
if($s) {
$j = json_decode($s);
if (isset($j->data)) $fb_installed = true;
}
}
}
$appid = get_config('facebook','appid');
if(! $appid) {
notice( t('Facebook API key is missing.') . EOL);
return '';
}
$a->page['htmlhead'] .= '' . "\r\n";
$o .= '
';
$appid = get_config('facebook', 'appid' );
$appsecret = get_config('facebook', 'appsecret' );
$ret1 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appid' LIMIT 1");
$ret2 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appsecret' LIMIT 1");
if ((count($ret1) > 0 && $ret1[0]['v'] != $appid) || (count($ret2) > 0 && $ret2[0]['v'] != $appsecret)) $o .= t('Error: it appears that you have specified the App-ID and -Secret in your .htconfig.php file. As long as they are specified there, they cannot be set using this form.