* Name: Facebook Connector
* Version: 1.3
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
- * Tobias Hößl <https://github.com/CatoTH/>
+ * Author: Tobias Hößl <https://github.com/CatoTH/>
+ * Status: Unsupported
*/
/**
if (x($_REQUEST, "realtime_cb") && x($_REQUEST, "realtime_cb")) {
logger("facebook_init: Facebook Real-Time callback called", LOGGER_DEBUG);
-
+
if (x($_REQUEST, "hub_verify_token")) {
// this is the verification callback while registering for real time updates
-
+
$verify_token = get_config('facebook', 'cb_verify_token');
if ($verify_token != $_REQUEST["hub_verify_token"]) {
logger('facebook_init: Wrong Facebook Callback Verifier - expected ' . $verify_token . ', got ' . $_REQUEST["hub_verify_token"]);
return;
}
-
+
if (x($_REQUEST, "hub_challenge")) {
logger('facebook_init: Answering Challenge: ' . $_REQUEST["hub_challenge"], LOGGER_DATA);
echo $_REQUEST["hub_challenge"];
die();
}
}
-
+
require_once('include/items.php');
-
+
// this is a status update
$content = file_get_contents("php://input");
if (is_numeric($content)) $content = file_get_contents("php://input");
$js = json_decode($content);
logger(print_r($js, true), LOGGER_DATA);
-
+
if (!isset($js->object) || $js->object != "user" || !isset($js->entry)) {
logger('facebook_init: Could not parse Real-Time Update data', LOGGER_DEBUG);
return;
}
-
+
$affected_users = array("feed" => array(), "friends" => array());
-
+
foreach ($js->entry as $entry) {
$fbuser = $entry->uid;
foreach ($entry->changed_fields as $field) {
continue;
}
if (in_array($fbuser, $affected_users[$field])) continue;
-
+
$r = q("SELECT `uid` FROM `pconfig` WHERE `cat` = 'facebook' AND `k` = 'self_id' AND `v` = '%s' LIMIT 1", dbesc($fbuser));
if(! count($r))
continue;
$uid = $r[0]['uid'];
-
+
$access_token = get_pconfig($uid,'facebook','access_token');
if(! $access_token)
return;
-
+
switch ($field) {
case "feed":
logger('facebook_init: FB-User ' . $fbuser . ' / feed', LOGGER_DEBUG);
-
+
if(! get_pconfig($uid,'facebook','no_wall')) {
$private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
$s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
}
}
}
-
+
break;
case "friends":
logger('facebook_init: FB-User ' . $fbuser . ' / friends', LOGGER_DEBUG);
-
+
fb_get_friends($uid, false);
set_pconfig($uid,'facebook','friend_check',time());
break;
}
}
-
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)
$x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
. $appid . '&client_secret=' . $appsecret . '&redirect_uri='
- . urlencode($a->get_baseurl() . '/facebook/' . $nick)
+ . urlencode($a->get_baseurl() . '/facebook/' . $nick)
. '&code=' . $auth_code);
logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
if(count($r)) {
+ // update profile photos once every two weeks as we have no notification of when they change.
+
+ $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
+
// check that we have all the photos, this has been known to fail on occasion
- if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
+ if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
require_once("Photo.php");
$photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
`name-date` = '%s',
`uri-date` = '%s',
`avatar-date` = '%s'
- WHERE `id` = %d LIMIT 1
+ WHERE `id` = %d
",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($contact->id),
dbesc('facebook ' . $contact->id),
dbesc($contact->name),
- dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
+ dbesc(($contact->nickname) ? $contact->nickname : mb_convert_case($contact->first_name, MB_CASE_LOWER, "UTF-8")),
dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
dbesc(NETWORK_FACEBOOK),
intval(CONTACT_IS_FRIEND),
$contact_id = $r[0]['id'];
+ $g = q("select def_gid from user where uid = %d limit 1",
+ intval($uid)
+ );
+ if($g && intval($g[0]['def_gid'])) {
+ require_once('include/group.php');
+ group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
+ }
+
require_once("Photo.php");
$photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
`name-date` = '%s',
`uri-date` = '%s',
`avatar-date` = '%s'
- WHERE `id` = %d LIMIT 1
+ WHERE `id` = %d
",
dbesc($photos[0]),
dbesc($photos[1]),
if($s) {
$results = json_decode($s);
logger('fb_get_friends: info: ' . print_r($results,true), LOGGER_DATA);
- foreach ($results as $contact) {
- if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
- else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
- }
+ if(count($results)) {
+ foreach ($results as $contact) {
+ if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
+ else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
+ }
+ }
}
}
}
$access_token = get_pconfig($uid,'facebook','access_token');
$no_linking = get_pconfig($uid,'facebook','no_linking');
+
if($no_linking)
return;
return;
$s = fetch_url('https://graph.facebook.com/me/friends?access_token=' . $access_token);
if($s) {
- logger('facebook: fb_get_friends: ' . $s, LOGGER_DATA);
+ logger('facebook: fb_gwet_friends: ' . $s, LOGGER_DATA);
$j = json_decode($s);
logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
if(! $j->data)
}
// This is the POST method to the facebook settings page
-// Content is posted to Facebook in the function facebook_post_hook()
+// Content is posted to Facebook in the function facebook_post_hook()
/**
* @param App $a
if($uid){
- $fb_limited = get_config('facebook','restrict');
+ $fb_limited = get_config('facebook','crestrict');
$value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
$private_wall = ((x($_POST,'facebook_private_wall')) ? intval($_POST['facebook_private_wall']) : 0);
set_pconfig($uid,'facebook','private_wall',$private_wall);
-
+
set_pconfig($uid,'facebook','blocked_apps',escape_tags(trim($_POST['blocked_apps'])));
if($linkvalue == 0)
set_pconfig($uid,'facebook','no_linking', 1);
}
- else
+ else
set_pconfig($uid,'facebook','no_linking', (($linkvalue) ? 0 : 1));
// FB linkage was allowed but has just been turned off - remove all FB contacts and posts
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 disabled') . EOL);
$o .= '<div><label id="blocked-apps-label" for="blocked-apps">' . t('Comma separated applications to ignore') . ' </label></div>';
$o .= '<div><textarea id="blocked-apps" name="blocked_apps" >' . htmlspecialchars($blocked_apps) . '</textarea></div>';
- $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
+ $o .= '<input type="submit" name="submit" value="' . t('Save Settings') . '" /></form></div>';
}
return $o;
$o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("fbsave") . '">';
-
+
$o .= '<h4>' . t('Facebook API Key') . '</h4>';
-
+
$appid = get_config('facebook', 'appid' );
$appsecret = get_config('facebook', 'appsecret' );
$poll_interval = get_config('facebook', 'poll_interval' );
$sync_comments = get_config('facebook', 'sync_comments' );
if (!$poll_interval) $poll_interval = FACEBOOK_DEFAULT_POLL_INTERVAL;
-
+
$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.<br><br>');
-
+
$working_connection = false;
if ($appid && $appsecret) {
$subs = facebook_subscriptions_get();
elseif (is_array($subs)) {
$o .= t('The given API Key seems to work correctly.') . '<br>';
$working_connection = true;
- } else $o .= t('The correctness of the API Key could not be detected. Somthing strange\'s going on.') . '<br>';
+ } else $o .= t('The correctness of the API Key could not be detected. Something strange\'s going on.') . '<br>';
}
-
+
$o .= '<label for="fb_appid">' . t('App-ID / API-Key') . '</label><input id="fb_appid" name="appid" type="text" value="' . escape_tags($appid ? $appid : "") . '"><br style="clear: both;">';
$o .= '<label for="fb_appsecret">' . t('Application secret') . '</label><input id="fb_appsecret" name="appsecret" type="text" value="' . escape_tags($appsecret ? $appsecret : "") . '"><br style="clear: both;">';
$o .= '<label for="fb_poll_interval">' . sprintf(t('Polling Interval in minutes (minimum %1$s minutes)'), FACEBOOK_MIN_POLL_INTERVAL) . '</label><input name="poll_interval" id="fb_poll_interval" type="number" min="' . FACEBOOK_MIN_POLL_INTERVAL . '" value="' . $poll_interval . '"><br style="clear: both;">';
if($fb_token && ($toplevel || $b['private'] || $reply)) {
logger('facebook: able to post');
require_once('library/facebook.php');
- require_once('include/bbcode.php');
+ require_once('include/bbcode.php');
$msg = $b['body'];
if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
$image = $matches[1];
+ // When saved into the database the content is sent through htmlspecialchars
+ // That means that we have to decode all image-urls
+ $image = htmlspecialchars_decode($image);
+
// Checking for a bookmark element
$body = $b['body'];
if (strpos($body, "[bookmark") !== false) {
}
// At first convert the text to html
- $html = bbcode($body);
+ $html = bbcode($body, false, false);
// Then convert it to plain text
$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
// Fallback - if message is empty
if(!strlen($msg))
- $msg = $link;
+ $msg = $linkname;
if(!strlen($msg))
- $msg = $image;
+ $msg = $link;
if(!strlen($msg))
- $msg = $linkname;
+ $msg = $image;
// If there is nothing to post then exit
if(!strlen($msg))
logger('Facebook post: msg=' . $msg, LOGGER_DATA);
- if($likes) {
+ if($likes) {
$postvars = array('access_token' => $fb_token);
}
else {
+ // message, picture, link, name, caption, description, source, place, tags
$postvars = array(
- 'access_token' => $fb_token,
+ 'access_token' => $fb_token,
'message' => $msg
);
- if(isset($image)) {
+ if(trim($image) != "") {
$postvars['picture'] = $image;
- //$postvars['type'] = "photo";
}
- if(isset($link)) {
+ if(trim($link) != "") {
$postvars['link'] = $link;
- //$postvars['type'] = "link";
+
+ // The following doesn't work - why?
+ if ((stristr($link,'youtube')) || (stristr($link,'youtu.be')) || (stristr($link,'vimeo'))) {
+ $postvars['source'] = $link;
+ }
}
- if(isset($linkname))
+ if(trim($linkname) != "")
$postvars['name'] = $linkname;
}
if($reply) {
$url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
- } else if (($link != "") or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
+ } else if (($link != "") or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
$url = 'https://graph.facebook.com/me/feed';
if($b['plink'])
$postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' . $b['plink'] . '"}';
} else {
// if its only a message and a subject and the message is larger than 500 characters then post it as note
$postvars = array(
- 'access_token' => $fb_token,
- 'message' => bbcode($b['body']),
+ 'access_token' => $fb_token,
+ 'message' => bbcode($b['body'], false, false),
'subject' => $b['title'],
);
$url = 'https://graph.facebook.com/me/notes';
$retj = json_decode($x);
if($retj->id) {
- q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
dbesc('fb::' . $retj->id),
intval($b['id'])
);
add_to_queue($a->contact,NETWORK_FACEBOOK,$s);
notice( t('Facebook post failed. Queued for retry.') . EOL);
}
-
+
if (isset($retj->error) && $retj->error->type == "OAuthException" && $retj->error->code == 190) {
logger('Facebook session has expired due to changed password.', LOGGER_DEBUG);
-
+
$last_notification = get_pconfig($b['uid'], 'facebook', 'session_expired_mailsent');
if (!$last_notification || $last_notification < (time() - FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL)) {
require_once('include/enotify.php');
-
+
$r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($b['uid']) );
notification(array(
'uid' => $b['uid'],
'source_link' => $a->config["system"]["url"],
'source_photo' => $a->config["system"]["url"] . '/images/person-80.jpg',
));
-
+
set_pconfig($b['uid'], 'facebook', 'session_expired_mailsent', time());
} else logger('Facebook: No notification, as the last one was sent on ' . $last_notification, LOGGER_DEBUG);
}
$fb_enable = (($fb_post && x($_REQUEST,'facebook_enable')) ? intval($_REQUEST['facebook_enable']) : 0);
// if API is used, default to the chosen settings
- if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default')))
- $fb_enable = 1;
+ // but allow a specific override
+
+ if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default'))) {
+ if(! x($_REQUEST,'facebook_enable'))
+ $fb_enable = 1;
+ }
if(! $fb_enable)
return;
$retj = json_decode($j);
if($retj->id) {
- q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
dbesc('fb::' . $retj->id),
intval($item)
);
- logger('facebook_queue: success: ' . $j);
+ logger('facebook_queue: success: ' . $j);
remove_queue_item($x['id']);
}
else {
// oembed display a picture of the video as well
if ($entry->type != "video") {
if(isset($entry->picture) && isset($entry->link)) {
- $datarray['body'] .= "\n" . '[url=' . $entry->link . '][img]'.$entry->picture.'[/img][/url]';
+ $datarray['body'] .= "\n" . '[url=' . $entry->link . '][img]'.$entry->picture.'[/img][/url]';
}
else {
if(isset($entry->picture))