<?php
/**
* Name: Facebook Connector
- * Version: 1.1
+ * Version: 1.2
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
* Tobias Hößl <https://github.com/CatoTH/>
*/
/**
* Installing the Friendica/Facebook connector
*
- * 1. register an API key for your site from developer.facebook.com
- * a. We'd be very happy if you include "Friendica" in the application name
- * to increase name recognition. The Friendica icons are also present
- * in the images directory and may be uploaded as a Facebook app icon.
- * Use images/friendica-16.jpg for the Icon and images/friendica-128.jpg for the Logo.
- * b. The url should be your site URL with a trailing slash.
- * Friendica is a software application and does not require a Privacy Policy
- * or Terms of Service, though your installation of it might. Facebook may require
- * that you provide a Privacy Policy, which we find ironic.
- * c. Set the following values in your .htconfig.php file
- * $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
- * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
- * Replace with the settings Facebook gives you.
- * d. Navigate to Set Web->Site URL & Domain -> Website Settings. Set
- * Site URL to yoursubdomain.yourdomain.com. Set Site Domain to your
- * yourdomain.com.
- * 2. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
- * and click 'Install Facebook Connector'.
- * 3. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
- * and click 'Install Facebook Connector'.
- * 4. This will ask you to login to Facebook and grant permission to the
- * plugin to do its stuff. Allow it to do so.
- * 5. Optional step: If you want to use Facebook Real Time Updates (so new messages
- * and new contacts are added ~1min after they are postet / added on FB), go to
- * Settings -> plugins -> facebook and press the "Activate Real-Time Updates"-button.
- * 6. You're done. To turn it off visit the Plugin Settings page again and
- * 'Remove Facebook posting'.
+ * 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
// 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 $a
+ * @return mixed
+ */
function facebook_init(&$a) {
if (x($_REQUEST, "realtime_cb") && x($_REQUEST, "realtime_cb")) {
}
+/**
+ * @param $uid
+ * @return mixed
+ */
function fb_get_self($uid) {
$access_token = get_pconfig($uid,'facebook','access_token');
if(! $access_token)
}
}
-function fb_get_friends_sync_new($uid, $access_token, $person) {
- $link = 'http://facebook.com/profile.php?id=' . $person->id;
-
- $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
- intval($uid),
- dbesc($link)
- );
-
- if (count($r) == 0) {
- logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
-
- fb_get_friends_sync_full($uid, $access_token, $person);
- }
-}
-
-function fb_get_friends_sync_full($uid, $access_token, $person) {
- $s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
- if($s) {
- $jp = json_decode($s);
- logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
-
- // always use numeric link for consistency
-
- $jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
-
- // If its a page then set the first name from the username
- if (!$jp->first_name and $jp->username)
- $jp->first_name = $jp->username;
-
- // check if we already have a contact
+/**
+ * @param $uid
+ * @param $access_token
+ * @param $persons
+ */
+function fb_get_friends_sync_new($uid, $access_token, $persons) {
+ $persons_todo = array();
+ foreach ($persons as $person) {
+ $link = 'http://facebook.com/profile.php?id=' . $person->id;
+
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+ intval($uid),
+ dbesc($link)
+ );
+
+ if (count($r) == 0) {
+ logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
+ $persons_todo[] = $person;
+ }
- $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
- intval($uid),
- dbesc($jp->link)
- );
+ if (count($persons_todo) > 0) fb_get_friends_sync_full($uid, $access_token, $persons_todo);
+ }
+}
- if(count($r)) {
+/**
+ * @param $uid
+ * @param $contact
+ * @return mixed
+ */
+function fb_get_friends_sync_parsecontact($uid, $contact) {
+ $contact->link = 'http://facebook.com/profile.php?id=' . $contact->id;
+
+ // If its a page then set the first name from the username
+ if (!$contact->first_name and $contact->username)
+ $contact->first_name = $contact->username;
+
+ // check if we already have a contact
+
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+ intval($uid),
+ dbesc($contact->link)
+ );
+
+ if(count($r)) {
+
+ // 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'])) {
+ require_once("Photo.php");
+
+ $photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
+
+ $r = q("UPDATE `contact` SET `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `name-date` = '%s',
+ `uri-date` = '%s',
+ `avatar-date` = '%s'
+ WHERE `id` = %d LIMIT 1
+ ",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($r[0]['id'])
+ );
+ }
+ return;
+ }
+ else {
+
+ // create contact record
+ $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
+ `name`, `nick`, `photo`, `network`, `rel`, `priority`,
+ `writable`, `blocked`, `readonly`, `pending` )
+ VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
+ intval($uid),
+ dbesc(datetime_convert()),
+ dbesc($contact->link),
+ dbesc(normalise_link($contact->link)),
+ dbesc(''),
+ dbesc(''),
+ dbesc($contact->id),
+ dbesc('facebook ' . $contact->id),
+ dbesc($contact->name),
+ dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
+ dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
+ dbesc(NETWORK_FACEBOOK),
+ intval(CONTACT_IS_FRIEND),
+ intval(1),
+ intval(1)
+ );
+ }
- // 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'])) {
- require_once("Photo.php");
-
- $photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
-
- $r = q("UPDATE `contact` SET `photo` = '%s',
- `thumb` = '%s',
- `micro` = '%s',
- `name-date` = '%s',
- `uri-date` = '%s',
- `avatar-date` = '%s'
- WHERE `id` = %d LIMIT 1
- ",
- dbesc($photos[0]),
- dbesc($photos[1]),
- dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- intval($r[0]['id'])
- );
- }
- return;
- }
- else {
+ $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($contact->link),
+ intval($uid)
+ );
- // create contact record
- $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
- `name`, `nick`, `photo`, `network`, `rel`, `priority`,
- `writable`, `blocked`, `readonly`, `pending` )
- VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
- intval($uid),
- dbesc(datetime_convert()),
- dbesc($jp->link),
- dbesc(normalise_link($jp->link)),
- dbesc(''),
- dbesc(''),
- dbesc($jp->id),
- dbesc('facebook ' . $jp->id),
- dbesc($jp->name),
- dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
- dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
- dbesc(NETWORK_FACEBOOK),
- intval(CONTACT_IS_FRIEND),
- intval(1),
- intval(1)
- );
- }
+ if(! count($r)) {
+ return;
+ }
- $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
- dbesc($jp->link),
- intval($uid)
- );
+ $contact = $r[0];
+ $contact_id = $r[0]['id'];
+
+ require_once("Photo.php");
+
+ $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
+
+ $r = q("UPDATE `contact` SET `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `name-date` = '%s',
+ `uri-date` = '%s',
+ `avatar-date` = '%s'
+ WHERE `id` = %d LIMIT 1
+ ",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($contact_id)
+ );
+}
- if(! count($r)) {
- return;
- }
+/**
+ * @param $uid
+ * @param $access_token
+ * @param $persons
+ * @return mixed
+ */
+function fb_get_friends_sync_full($uid, $access_token, $persons) {
+ if (count($persons) == 0) return;
+ $nums = Ceil(count($persons) / 50);
+ for ($i = 0; $i < $nums; $i++) {
+ $batch_request = array();
+ for ($j = $i * 50; $j < ($i+1) * 50 && $j < count($persons); $j++) $batch_request[] = array('method'=>'GET', 'relative_url'=>$persons[$j]->id);
+ $s = post_url('https://graph.facebook.com/', array('access_token' => $access_token, 'batch' => json_encode($batch_request)));
+ 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));
+ }
+ }
+ }
+}
- $contact = $r[0];
- $contact_id = $r[0]['id'];
-
- require_once("Photo.php");
-
- $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
-
- $r = q("UPDATE `contact` SET `photo` = '%s',
- `thumb` = '%s',
- `micro` = '%s',
- `name-date` = '%s',
- `uri-date` = '%s',
- `avatar-date` = '%s'
- WHERE `id` = %d LIMIT 1
- ",
- dbesc($photos[0]),
- dbesc($photos[1]),
- dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- intval($contact_id)
- );
- }
-}
// if $fullsync is true, only new contacts are searched for
+/**
+ * @param $uid
+ * @param bool $fullsync
+ * @return mixed
+ */
function fb_get_friends($uid, $fullsync = true) {
$r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
if(! $j->data)
return;
- foreach($j->data as $person)
- if ($fullsync)
- fb_get_friends_sync_full($uid, $access_token, $person);
- else
- fb_get_friends_sync_new($uid, $access_token, $person);
+
+ $persons_todo = array();
+ foreach($j->data as $person) $persons_todo[] = $person;
+
+ if ($fullsync)
+ fb_get_friends_sync_full($uid, $access_token, $persons_todo);
+ else
+ fb_get_friends_sync_new($uid, $access_token, $persons_todo);
}
}
// This is the POST method to the facebook settings page
// Content is posted to Facebook in the function facebook_post_hook()
+/**
+ * @param $a
+ * @return mixed
+ */
function facebook_post(&$a) {
$uid = local_user();
// Facebook settings form
+/**
+ * @param $a
+ * @return string
+ */
function facebook_content(&$a) {
if(! local_user()) {
if (get_pconfig(local_user(),'facebook','post')) {
$access_token = get_pconfig(local_user(),'facebook','access_token');
if ($access_token) {
- $private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
+ $private_wall = intval(get_pconfig(local_user(),'facebook','private_wall'));
$s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
if($s) {
$j = json_decode($s);
}
-
+/**
+ * @param $a
+ * @param $b
+ * @return mixed
+ */
function facebook_cron($a,$b) {
-//del_config('facebook', 'realtime_active');
-//del_config('facebook', 'realtime_err_mailsent');
-//del_config('facebook', 'cb_verify_token');
$last = get_config('facebook','last_poll');
}
-
+/**
+ * @param $a
+ * @param $b
+ */
function facebook_plugin_settings(&$a,&$b) {
$b .= '<div class="settings-block">';
}
+/**
+ * @param $a
+ * @param $o
+ */
function facebook_plugin_admin(&$a, &$o){
}
}
-function facebook_plugin_admin_post(&$a){
+/**
+ * @param $a
+ * @param $o
+ */
+function facebook_plugin_admin_post(&$a, &$o){
check_form_security_token_redirectOnErr('/admin/plugins/facebook', 'fbsave');
if (x($_REQUEST,'fb_save_keys')) {
}
}
+/**
+ * @param $a
+ * @param $b
+ * @return mixed
+ */
function facebook_jot_nets(&$a,&$b) {
if(! local_user())
return;
}
+/**
+ * @param $a
+ * @param $b
+ * @return mixed
+ */
function facebook_post_hook(&$a,&$b) {
}
}
+/**
+ * @param $app
+ * @param $data
+ */
function facebook_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';
}
}
+/**
+ * @param $a
+ * @param $b
+ * @return mixed
+ */
function facebook_post_local(&$a,&$b) {
// Figure out if Facebook posting is enabled for this post and file it in 'postopts'
}
+/**
+ * @param $a
+ * @param $b
+ * @return mixed
+ */
function fb_queue_hook(&$a,&$b) {
$qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
}
}
+/**
+ * @param $access_token
+ * @param $since
+ * @return stdClass
+ */
function fb_get_timeline($access_token, &$since) {
+ $entries = new stdClass();
$entries->data = array();
$newest = 0;
return($entries);
}
+/**
+ * @param $uid
+ * @return mixed
+ */
function fb_consume_all($uid) {
require_once('include/items.php');
// Get the last date
$lastdate = get_pconfig($uid,'facebook','lastdate');
// fetch all items since the last date
- $j = fb_get_timeline($access_token, &$lastdate);
+ $j = fb_get_timeline($access_token, $lastdate);
if (isset($j->data)) {
logger('fb_consume_stream: feed: ' . print_r($j,true), LOGGER_DATA);
fb_consume_stream($uid,$j,false);
logger('fb_consume_stream: feed: got no data from Facebook: ' . print_r($j,true), LOGGER_NORMAL);
}
+/**
+ * @param $uid
+ * @param $link
+ * @return string
+ */
function fb_get_photo($uid,$link) {
$access_token = get_pconfig($uid,'facebook','access_token');
if(! $access_token || (! stristr($link,'facebook.com/photo.php')))
$ret = preg_match('/fbid=([0-9]*)/',$link,$match);
if($ret)
$photo_id = $match[1];
+ else
+ return "";
$x = fetch_url('https://graph.facebook.com/' . $photo_id . '?access_token=' . $access_token);
$j = json_decode($x);
if($j->picture)
// return "\n" . '[url=' . $link . ']' . t('link') . '[/url]';
}
+/**
+ * @param $uid
+ * @param $j
+ * @param bool $wall
+ * @return mixed
+ */
function fb_consume_stream($uid,$j,$wall = false) {
$a = get_app();
'to_email' => $user[0]['email'],
'uid' => $user[0]['uid'],
'item' => $cmntdata,
- 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $item,
+ 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $item,
'source_name' => $cmntdata['author-name'],
'source_link' => $cmntdata['author-link'],
'source_photo' => $cmntdata['author-avatar'],
}
+/**
+ * @return bool|mixed|string
+ */
function fb_get_app_access_token() {
$acc_token = get_config('facebook','app_access_token');
}
}
+/**
+ *
+ */
function facebook_subscription_del_users() {
$a = get_app();
$access_token = fb_get_app_access_token();
if (!facebook_check_realtime_active()) del_config('facebook', 'realtime_active');
}
+/**
+ * @param bool $second_try
+ */
function facebook_subscription_add_users($second_try = false) {
$a = get_app();
$access_token = fb_get_app_access_token();
};
}
+/**
+ * @return null
+ */
function facebook_subscriptions_get() {
$access_token = fb_get_app_access_token();
}
+/**
+ * @return bool
+ */
function facebook_check_realtime_active() {
$ret = facebook_subscriptions_get();
if (is_null($ret)) return false;
// DELETE-request to $url
if(! function_exists('facebook_delete_url')) {
-function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
+ /**
+ * @param $url
+ * @param null $headers
+ * @param int $redirects
+ * @param int $timeout
+ * @return bool|string
+ */
+ function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
$a = get_app();
$ch = curl_init($url);
if(($redirects > 8) || (! $ch))
$url_parsed = @parse_url($url);
if (isset($url_parsed)) {
$redirects++;
- return delete_url($url,$headers,$redirects,$timeout);
+ return facebook_delete_url($url,$headers,$redirects,$timeout);
}
}
$a->set_curl_code($http_code);