3 * Name: Facebook Connector
5 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 * Installing the Friendica/Facebook connector
11 * 1. register an API key for your site from developer.facebook.com
12 * a. We'd be very happy if you include "Friendica" in the application name
13 * to increase name recognition. The Friendica icons are also present
14 * in the images directory and may be uploaded as a Facebook app icon.
15 * Use images/friendica-16.jpg for the Icon and images/friendica-128.jpg for the Logo.
16 * b. The url should be your site URL with a trailing slash.
17 * Friendica is a software application and does not require a Privacy Policy
18 * or Terms of Service, though your installation of it might. Facebook may require
19 * that you provide a Privacy Policy, which we find ironic.
20 * c. Set the following values in your .htconfig.php file
21 * $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
22 * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
23 * Replace with the settings Facebook gives you.
24 * d. Navigate to Set Web->Site URL & Domain -> Website Settings. Set
25 * Site URL to yoursubdomain.yourdomain.com. Set Site Domain to your
27 * 2. Enable the facebook plugin by including it in .htconfig.php - e.g.
28 * $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
29 * 3. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
30 * and click 'Install Facebook Connector'.
31 * 4. This will ask you to login to Facebook and grant permission to the
32 * plugin to do its stuff. Allow it to do so.
33 * 5. You're done. To turn it off visit the Plugin Settings page again and
34 * 'Remove Facebook posting'.
36 * Vidoes and embeds will not be posted if there is no other content. Links
37 * and images will be converted to a format suitable for the Facebook API and
38 * long posts truncated - with a link to view the full post.
40 * Facebook contacts will not be able to view private photos, as they are not able to
41 * authenticate to your site to establish identity. We will address this
42 * in a future release.
45 define('FACEBOOK_MAXPOSTLEN', 420);
48 function facebook_install() {
49 register_hook('post_local', 'addon/facebook/facebook.php', 'facebook_post_local');
50 register_hook('notifier_normal', 'addon/facebook/facebook.php', 'facebook_post_hook');
51 register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
52 register_hook('connector_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
53 register_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron');
54 register_hook('queue_predeliver', 'addon/facebook/facebook.php', 'fb_queue_hook');
58 function facebook_uninstall() {
59 unregister_hook('post_local', 'addon/facebook/facebook.php', 'facebook_post_local');
60 unregister_hook('notifier_normal', 'addon/facebook/facebook.php', 'facebook_post_hook');
61 unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
62 unregister_hook('connector_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
63 unregister_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron');
64 unregister_hook('queue_predeliver', 'addon/facebook/facebook.php', 'fb_queue_hook');
67 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
68 unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
72 /* declare the facebook_module function so that /facebook url requests will land here */
74 function facebook_module() {}
78 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
80 function facebook_init(&$a) {
86 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
93 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
94 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
98 logger('facebook_init: Error: ' . $error);
100 if($auth_code && $uid) {
102 $appid = get_config('facebook','appid');
103 $appsecret = get_config('facebook', 'appsecret');
105 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
106 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
107 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
108 . '&code=' . $auth_code);
110 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
112 if(strpos($x,'access_token=') !== false) {
113 $token = str_replace('access_token=', '', $x);
114 if(strpos($token,'&') !== false)
115 $token = substr($token,0,strpos($token,'&'));
116 set_pconfig($uid,'facebook','access_token',$token);
117 set_pconfig($uid,'facebook','post','1');
118 if(get_pconfig($uid,'facebook','no_linking') === false)
119 set_pconfig($uid,'facebook','no_linking',1);
121 fb_get_friends($uid);
122 fb_consume_all($uid);
131 function fb_get_self($uid) {
132 $access_token = get_pconfig($uid,'facebook','access_token');
135 $s = fetch_url('https://graph.facebook.com/me/?access_token=' . $access_token);
137 $j = json_decode($s);
138 set_pconfig($uid,'facebook','self_id',(string) $j->id);
144 function fb_get_friends($uid) {
146 $r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
152 $access_token = get_pconfig($uid,'facebook','access_token');
154 $no_linking = get_pconfig($uid,'facebook','no_linking');
160 $s = fetch_url('https://graph.facebook.com/me/friends?access_token=' . $access_token);
162 logger('facebook: fb_get_friends: ' . $s, LOGGER_DATA);
163 $j = json_decode($s);
164 logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
167 foreach($j->data as $person) {
168 $s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
170 $jp = json_decode($s);
171 logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
173 // always use numeric link for consistency
175 $jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
177 // check if we already have a contact
179 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
186 // check that we have all the photos, this has been known to fail on occasion
188 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
189 require_once("Photo.php");
191 $photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
193 $r = q("UPDATE `contact` SET `photo` = '%s',
199 WHERE `id` = %d LIMIT 1
204 dbesc(datetime_convert()),
205 dbesc(datetime_convert()),
206 dbesc(datetime_convert()),
214 // create contact record
215 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
216 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
217 `writable`, `blocked`, `readonly`, `pending` )
218 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
220 dbesc(datetime_convert()),
222 dbesc(normalise_link($jp->link)),
226 dbesc('facebook ' . $jp->id),
228 dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
229 dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
230 dbesc(NETWORK_FACEBOOK),
231 intval(CONTACT_IS_FRIEND),
237 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
247 $contact_id = $r[0]['id'];
249 require_once("Photo.php");
251 $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
253 $r = q("UPDATE `contact` SET `photo` = '%s',
259 WHERE `id` = %d LIMIT 1
264 dbesc(datetime_convert()),
265 dbesc(datetime_convert()),
266 dbesc(datetime_convert()),
275 // This is the POST method to the facebook settings page
276 // Content is posted to Facebook in the function facebook_post_hook()
278 function facebook_post(&$a) {
283 $value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
284 set_pconfig($uid,'facebook','post_by_default', $value);
286 $no_linking = get_pconfig($uid,'facebook','no_linking');
288 $no_wall = ((x($_POST,'facebook_no_wall')) ? intval($_POST['facebook_no_wall']) : 0);
289 set_pconfig($uid,'facebook','no_wall',$no_wall);
291 $private_wall = ((x($_POST,'facebook_private_wall')) ? intval($_POST['facebook_private_wall']) : 0);
292 set_pconfig($uid,'facebook','private_wall',$private_wall);
295 set_pconfig($uid,'facebook','blocked_apps',escape_tags(trim($_POST['blocked_apps'])));
297 $linkvalue = ((x($_POST,'facebook_linking')) ? intval($_POST['facebook_linking']) : 0);
298 set_pconfig($uid,'facebook','no_linking', (($linkvalue) ? 0 : 1));
300 // FB linkage was allowed but has just been turned off - remove all FB contacts and posts
302 if((! intval($no_linking)) && (! intval($linkvalue))) {
303 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s' ",
305 dbesc(NETWORK_FACEBOOK)
308 require_once('include/Contact.php');
310 contact_remove($rr['id']);
313 elseif(intval($no_linking) && intval($linkvalue)) {
314 // FB linkage is now allowed - import stuff.
316 fb_get_friends($uid);
317 fb_consume_all($uid);
320 info( t('Settings updated.') . EOL);
326 // Facebook settings form
328 function facebook_content(&$a) {
331 notice( t('Permission denied.') . EOL);
335 if($a->argc > 1 && $a->argv[1] === 'remove') {
336 del_pconfig(local_user(),'facebook','post');
337 info( t('Facebook disabled') . EOL);
340 if($a->argc > 1 && $a->argv[1] === 'friends') {
341 fb_get_friends(local_user());
342 info( t('Updating contacts') . EOL);
346 $fb_installed = get_pconfig(local_user(),'facebook','post');
348 $appid = get_config('facebook','appid');
351 notice( t('Facebook API key is missing.') . EOL);
355 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'
356 . $a->get_baseurl() . '/addon/facebook/facebook.css' . '" media="all" />' . "\r\n";
358 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
360 if(! $fb_installed) {
361 $o .= '<div id="facebook-enable-wrapper">';
363 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
364 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook connector for this account.') . '</a>';
369 $o .= '<div id="facebook-disable-wrapper">';
371 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook connector') . '</a></div>';
373 $o .= '<div id="facebook-enable-wrapper">';
375 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
376 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
379 $o .= '<div id="facebook-post-default-form">';
380 $o .= '<form action="facebook" method="post" >';
381 $post_by_default = get_pconfig(local_user(),'facebook','post_by_default');
382 $checked = (($post_by_default) ? ' checked="checked" ' : '');
383 $o .= '<input type="checkbox" name="post_by_default" value="1"' . $checked . '/>' . ' ' . t('Post to Facebook by default') . EOL;
385 $no_linking = get_pconfig(local_user(),'facebook','no_linking');
386 $checked = (($no_linking) ? '' : ' checked="checked" ');
387 $o .= '<input type="checkbox" name="facebook_linking" value="1"' . $checked . '/>' . ' ' . t('Link all your Facebook friends and conversations on this website') . EOL ;
389 $o .= '<p>' . t('Facebook conversations consist of your <em>profile wall</em> and your friend <em>stream</em>.');
390 $o .= ' ' . t('On this website, your Facebook friend stream is only visible to you.');
391 $o .= ' ' . t('The following settings determine the privacy of your Facebook profile wall on this website.') . '</p>';
393 $private_wall = get_pconfig(local_user(),'facebook','private_wall');
394 $checked = (($private_wall) ? ' checked="checked" ' : '');
395 $o .= '<input type="checkbox" name="facebook_private_wall" value="1"' . $checked . '/>' . ' ' . t('On this website your Facebook profile wall conversations will only be visible to you') . EOL ;
398 $no_wall = get_pconfig(local_user(),'facebook','no_wall');
399 $checked = (($no_wall) ? ' checked="checked" ' : '');
400 $o .= '<input type="checkbox" name="facebook_no_wall" value="1"' . $checked . '/>' . ' ' . t('Do not import your Facebook profile wall conversations') . EOL ;
402 $o .= '<p>' . t('If you choose to link conversations and leave both of these boxes unchecked, your Facebook profile wall will be merged with your profile wall on this website and your privacy settings on this website will be used to determine who may see the conversations.') . '</p>';
405 $blocked_apps = get_pconfig(local_user(),'facebook','blocked_apps');
407 $o .= '<div><label id="blocked-apps-label" for="blocked-apps">' . t('Comma separated applications to ignore') . ' </label></div>';
408 $o .= '<div><textarea id="blocked-apps" name="blocked_apps" >' . htmlspecialchars($blocked_apps) . '</textarea></div>';
410 $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
418 function facebook_cron($a,$b) {
420 $last = get_config('facebook','last_poll');
422 $poll_interval = intval(get_config('facebook','poll_interval'));
424 $poll_interval = 3600;
427 $next = $last + $poll_interval;
432 logger('facebook_cron');
435 // Find the FB users on this site and randomize in case one of them
436 // uses an obscene amount of memory. It may kill this queue run
437 // but hopefully we'll get a few others through on each run.
439 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'facebook' AND `k` = 'post' AND `v` = '1' ORDER BY RAND() ");
442 if(get_pconfig($rr['uid'],'facebook','no_linking'))
444 $ab = intval(get_config('system','account_abandon_days'));
446 $z = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY LIMIT 1",
454 // check for new friends once a day
455 $last_friend_check = get_pconfig($rr['uid'],'facebook','friend_check');
456 if($last_friend_check)
457 $next_friend_check = $last_friend_check + 86400;
458 if($next_friend_check <= time()) {
459 fb_get_friends($rr['uid']);
460 set_pconfig($rr['uid'],'facebook','friend_check',time());
462 fb_consume_all($rr['uid']);
466 set_config('facebook','last_poll', time());
472 function facebook_plugin_settings(&$a,&$b) {
474 $b .= '<div class="settings-block">';
475 $b .= '<h3>' . t('Facebook') . '</h3>';
476 $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
481 function facebook_jot_nets(&$a,&$b) {
485 $fb_post = get_pconfig(local_user(),'facebook','post');
486 if(intval($fb_post) == 1) {
487 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
488 $selected = ((intval($fb_defpost) == 1) ? ' checked="checked" ' : '');
489 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . ' value="1" /> '
490 . t('Post to Facebook') . '</div>';
495 function facebook_post_hook(&$a,&$b) {
498 if($b['deleted'] || ($b['created'] !== $b['edited']))
502 * Post to Facebook stream
505 require_once('include/group.php');
507 logger('Facebook post');
512 $toplevel = (($b['id'] == $b['parent']) ? true : false);
515 $linking = ((get_pconfig($b['uid'],'facebook','no_linking')) ? 0 : 1);
517 if((! $toplevel) && ($linking)) {
518 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
519 intval($b['parent']),
522 if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
523 $reply = substr($r[0]['uri'],4);
524 elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
525 $reply = substr($r[0]['extid'],4);
528 logger('facebook reply id=' . $reply);
531 if(strstr($b['postopts'],'facebook') || ($b['private']) || ($reply)) {
533 if($b['private'] && $reply === false) {
534 $allow_people = expand_acl($b['allow_cid']);
535 $allow_groups = expand_groups(expand_acl($b['allow_gid']));
536 $deny_people = expand_acl($b['deny_cid']);
537 $deny_groups = expand_groups(expand_acl($b['deny_gid']));
539 $recipients = array_unique(array_merge($allow_people,$allow_groups));
540 $deny = array_unique(array_merge($deny_people,$deny_groups));
542 $allow_str = dbesc(implode(', ',$recipients));
544 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'");
545 $allow_arr = array();
548 $allow_arr[] = $rr['notify'];
551 $deny_str = dbesc(implode(', ',$deny));
553 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $deny_str ) AND `network` = 'face'");
557 $deny_arr[] = $rr['notify'];
560 if(count($deny_arr) && (! count($allow_arr))) {
562 // One or more FB folks were denied access but nobody on FB was specifically allowed access.
563 // This might cause the post to be open to public on Facebook, but only to selected members
564 // on another network. Since this could potentially leak a post to somebody who was denied,
565 // we will skip posting it to Facebook with a slightly vague but relevant message that will
566 // hopefully lead somebody to this code comment for a better explanation of what went wrong.
568 notice( t('Post to Facebook cancelled because of multi-network access permission conflict.') . EOL);
573 // if it's a private message but no Facebook members are allowed or denied, skip Facebook post
575 if((! count($allow_arr)) && (! count($deny_arr)))
579 if($b['verb'] == ACTIVITY_LIKE)
583 $appid = get_config('facebook', 'appid' );
584 $secret = get_config('facebook', 'appsecret' );
586 if($appid && $secret) {
588 logger('facebook: have appid+secret');
590 $fb_token = get_pconfig($b['uid'],'facebook','access_token');
593 // post to facebook if it's a public post and we've ticked the 'post to Facebook' box,
594 // or it's a private message with facebook participants
595 // or it's a reply or likes action to an existing facebook post
597 if($fb_token && ($toplevel || $b['private'] || $reply)) {
598 logger('facebook: able to post');
599 require_once('library/facebook.php');
600 require_once('include/bbcode.php');
604 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
606 // make links readable before we strip the code
608 // unless it's a dislike - just send the text as a comment
610 if($b['verb'] == ACTIVITY_DISLIKE)
611 $msg = trim(strip_tags(bbcode($msg)));
613 $search_str = $a->get_baseurl() . '/search';
615 if(preg_match("/\[url=(.*?)\](.*?)\[\/url\]/is",$msg,$matches)) {
617 // don't use hashtags for message link
619 if(strpos($matches[2],$search_str) === false) {
621 if(substr($matches[2],0,5) != '[img]')
622 $linkname = $matches[2];
626 $msg = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/is",'$2 $1',$msg);
628 if(preg_match("/\[img\](.*?)\[\/img\]/is",$msg,$matches))
629 $image = $matches[1];
631 $msg = preg_replace("/\[img\](.*?)\[\/img\]/is", t('Image: ') . '$1', $msg);
633 if((strpos($link,z_root()) !== false) && (! $image))
634 $image = $a->get_baseurl() . '/images/friendica-64.jpg';
636 $msg = trim(strip_tags(bbcode($msg)));
637 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
639 // add any attachments as text urls
641 $arr = explode(',',$b['attach']);
645 foreach($arr as $r) {
647 $cnt = preg_match('|\[attach\]href=\"(.*?)\" size=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
654 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
656 require_once('library/slinky.php');
658 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
659 $slinky = new Slinky( $display_url );
660 // setup a cascade of shortening services
661 // try to get a short link from these services
662 // in the order ur1.ca, trim, id.gd, tinyurl
663 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
664 $shortlink = $slinky->short();
665 // the new message will be shortened such that "... $shortlink"
666 // will fit into the character limit
667 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
668 $msg .= '... ' . $shortlink;
673 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
676 $postvars = array('access_token' => $fb_token);
680 'access_token' => $fb_token,
684 $postvars['picture'] = $image;
686 $postvars['link'] = $link;
688 $postvars['name'] = $linkname;
691 if(($b['private']) && (! $b['parent'])) {
692 $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
693 if(count($allow_arr))
694 $postvars['privacy'] .= ',"allow": "' . implode(',',$allow_arr) . '"';
696 $postvars['privacy'] .= ',"deny": "' . implode(',',$deny_arr) . '"';
697 $postvars['privacy'] .= '}';
702 $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
705 $url = 'https://graph.facebook.com/me/feed';
707 $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' . $b['plink'] . '"}';
710 logger('facebook: post to ' . $url);
711 logger('facebook: postvars: ' . print_r($postvars,true));
713 // "test_mode" prevents anything from actually being posted.
714 // Otherwise, let's do it.
716 if(! get_config('facebook','test_mode')) {
717 $x = post_url($url, $postvars);
719 $retj = json_decode($x);
721 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
722 dbesc('fb::' . $retj->id),
728 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $postvars));
729 require_once('include/queue_fn.php');
730 add_to_queue($a->contact,NETWORK_FACEBOOK,$s);
731 notice( t('Facebook post failed. Queued for retry.') . EOL);
735 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
743 function facebook_post_local(&$a,&$b) {
745 // Figure out if Facebook posting is enabled for this post and file it in 'postopts'
746 // where we will discover it during background delivery.
748 // This can only be triggered by a local user posting to their own wall.
750 if((local_user()) && (local_user() == $b['uid'])) {
752 $fb_post = intval(get_pconfig(local_user(),'facebook','post'));
753 $fb_enable = (($fb_post && x($_REQUEST,'facebook_enable')) ? intval($_REQUEST['facebook_enable']) : 0);
755 // if API is used, default to the chosen settings
756 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default')))
762 if(strlen($b['postopts']))
763 $b['postopts'] .= ',';
764 $b['postopts'] .= 'facebook';
769 function fb_queue_hook(&$a,&$b) {
771 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
772 dbesc(NETWORK_FACEBOOK)
777 require_once('include/queue_fn.php');
780 if($x['network'] !== NETWORK_FACEBOOK)
783 logger('facebook_queue: run');
785 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
786 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
794 $appid = get_config('facebook', 'appid' );
795 $secret = get_config('facebook', 'appsecret' );
797 if($appid && $secret) {
798 $fb_post = intval(get_pconfig($user['uid'],'facebook','post'));
799 $fb_token = get_pconfig($user['uid'],'facebook','access_token');
801 if($fb_post && $fb_token) {
802 logger('facebook_queue: able to post');
803 require_once('library/facebook.php');
805 $z = unserialize($x['content']);
807 $j = post_url($z['url'],$z['post']);
809 $retj = json_decode($j);
811 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
812 dbesc('fb::' . $retj->id),
815 logger('facebook_queue: success: ' . $j);
816 remove_queue_item($x['id']);
819 logger('facebook_queue: failed: ' . $j);
820 update_queue_time($x['id']);
827 function fb_consume_all($uid) {
829 require_once('include/items.php');
831 $access_token = get_pconfig($uid,'facebook','access_token');
835 if(! get_pconfig($uid,'facebook','no_wall')) {
836 $private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
837 $s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
839 $j = json_decode($s);
840 logger('fb_consume_stream: wall: ' . print_r($j,true), LOGGER_DATA);
841 fb_consume_stream($uid,$j,($private_wall) ? false : true);
844 $s = fetch_url('https://graph.facebook.com/me/home?access_token=' . $access_token);
846 $j = json_decode($s);
847 logger('fb_consume_stream: feed: ' . print_r($j,true), LOGGER_DATA);
848 fb_consume_stream($uid,$j,false);
853 function fb_consume_stream($uid,$j,$wall = false) {
858 $user = q("SELECT `nickname`, `blockwall` FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
864 $my_local_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
866 $no_linking = get_pconfig($uid,'facebook','no_linking');
870 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
874 $blocked_apps = get_pconfig($uid,'facebook','blocked_apps');
875 $blocked_apps_arr = explode(',',$blocked_apps);
877 $self_id = get_pconfig($uid,'facebook','self_id');
878 if(! count($j->data) || (! strlen($self_id)))
881 foreach($j->data as $entry) {
882 logger('fb_consume: entry: ' . print_r($entry,true), LOGGER_DATA);
885 $r = q("SELECT * FROM `item` WHERE ( `uri` = '%s' OR `extid` = '%s') AND `uid` = %d LIMIT 1",
886 dbesc('fb::' . $entry->id),
887 dbesc('fb::' . $entry->id),
893 $top_item = $r[0]['id'];
896 $post_exists = false;
901 $datarray['gravity'] = 0;
902 $datarray['uid'] = $uid;
903 $datarray['wall'] = (($wall) ? 1 : 0);
904 $datarray['uri'] = $datarray['parent-uri'] = 'fb::' . $entry->id;
905 $from = $entry->from;
906 if($from->id == $self_id)
907 $datarray['contact-id'] = $self[0]['id'];
909 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
914 $datarray['contact-id'] = $r[0]['id'];
917 // don't store post if we don't have a contact
919 if(! x($datarray,'contact-id')) {
920 logger('no contact: post ignored');
924 $datarray['verb'] = ACTIVITY_POST;
926 $datarray['owner-name'] = $self[0]['name'];
927 $datarray['owner-link'] = $self[0]['url'];
928 $datarray['owner-avatar'] = $self[0]['thumb'];
930 if(isset($entry->application) && isset($entry->application->name) && strlen($entry->application->name))
931 $datarray['app'] = strip_tags($entry->application->name);
933 $datarray['app'] = 'facebook';
935 $found_blocked = false;
937 if(count($blocked_apps_arr)) {
938 foreach($blocked_apps_arr as $bad_appl) {
939 if(strlen(trim($bad_appl)) && (stristr($datarray['app'],trim($bad_appl)))) {
940 $found_blocked = true;
946 logger('facebook: blocking application: ' . $datarray['app']);
950 $datarray['author-name'] = $from->name;
951 $datarray['author-link'] = 'http://facebook.com/profile.php?id=' . $from->id;
952 $datarray['author-avatar'] = 'https://graph.facebook.com/' . $from->id . '/picture';
953 $datarray['plink'] = $datarray['author-link'] . '&v=wall&story_fbid=' . substr($entry->id,strpos($entry->id,'_') + 1);
955 $datarray['body'] = escape_tags($entry->message);
957 if($entry->picture && $entry->link) {
958 $datarray['body'] .= "\n\n" . '[url=' . $entry->link . '][img]' . $entry->picture . '[/img][/url]';
962 $datarray['body'] .= "\n\n" . '[img]' . $entry->picture . '[/img]';
964 $datarray['body'] .= "\n" . '[url=' . $entry->link . ']' . t('link') . '[/url]';
967 $datarray['body'] .= "\n" . $entry->name;
969 $datarray['body'] .= "\n" . $entry->caption;
970 if($entry->description)
971 $datarray['body'] .= "\n" . $entry->description;
972 $datarray['created'] = datetime_convert('UTC','UTC',$entry->created_time);
973 $datarray['edited'] = datetime_convert('UTC','UTC',$entry->updated_time);
975 // If the entry has a privacy policy, we cannot assume who can or cannot see it,
976 // as the identities are from a foreign system. Mark it as private to the owner.
978 if($entry->privacy && $entry->privacy->value !== 'EVERYONE') {
979 $datarray['private'] = 1;
980 $datarray['allow_cid'] = '<' . $uid . '>';
983 $top_item = item_store($datarray);
984 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
990 logger('fb: new top level item posted');
994 if(isset($entry->likes) && isset($entry->likes->data))
995 $likers = $entry->likes->data;
999 if(isset($entry->comments) && isset($entry->comments->data))
1000 $comments = $entry->comments->data;
1004 if(is_array($likers)) {
1005 foreach($likers as $likes) {
1010 // If we posted the like locally, it will be found with our url, not the FB url.
1012 $second_url = (($likes->id == $self_id) ? $self[0]['url'] : 'http://facebook.com/profile.php?id=' . $likes->id);
1014 $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `verb` = '%s'
1015 AND ( `author-link` = '%s' OR `author-link` = '%s' ) LIMIT 1",
1016 dbesc($orig_post['uri']),
1018 dbesc(ACTIVITY_LIKE),
1019 dbesc('http://facebook.com/profile.php?id=' . $likes->id),
1026 $likedata = array();
1027 $likedata['parent'] = $top_item;
1028 $likedata['verb'] = ACTIVITY_LIKE;
1029 $likedata['gravity'] = 3;
1030 $likedata['uid'] = $uid;
1031 $likedata['wall'] = (($wall) ? 1 : 0);
1032 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
1033 $likedata['parent-uri'] = $orig_post['uri'];
1034 if($likes->id == $self_id)
1035 $likedata['contact-id'] = $self[0]['id'];
1037 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1042 $likedata['contact-id'] = $r[0]['id'];
1044 if(! x($likedata,'contact-id'))
1045 $likedata['contact-id'] = $orig_post['contact-id'];
1047 $likedata['app'] = 'facebook';
1048 $likedata['verb'] = ACTIVITY_LIKE;
1049 $likedata['author-name'] = $likes->name;
1050 $likedata['author-link'] = 'http://facebook.com/profile.php?id=' . $likes->id;
1051 $likedata['author-avatar'] = 'https://graph.facebook.com/' . $likes->id . '/picture';
1053 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
1054 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
1055 $post_type = t('status');
1056 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
1057 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
1059 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
1060 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
1061 '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
1063 $item = item_store($likedata);
1066 if(is_array($comments)) {
1067 foreach($comments as $cmnt) {
1072 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND ( `uri` = '%s' OR `extid` = '%s' ) LIMIT 1",
1074 dbesc('fb::' . $cmnt->id),
1075 dbesc('fb::' . $cmnt->id)
1080 $cmntdata = array();
1081 $cmntdata['parent'] = $top_item;
1082 $cmntdata['verb'] = ACTIVITY_POST;
1083 $cmntdata['gravity'] = 6;
1084 $cmntdata['uid'] = $uid;
1085 $cmntdata['wall'] = (($wall) ? 1 : 0);
1086 $cmntdata['uri'] = 'fb::' . $cmnt->id;
1087 $cmntdata['parent-uri'] = $orig_post['uri'];
1088 if($cmnt->from->id == $self_id) {
1089 $cmntdata['contact-id'] = $self[0]['id'];
1092 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d LIMIT 1",
1093 dbesc($cmnt->from->id),
1097 $cmntdata['contact-id'] = $r[0]['id'];
1098 if($r[0]['blocked'] || $r[0]['readonly'])
1102 if(! x($cmntdata,'contact-id'))
1103 $cmntdata['contact-id'] = $orig_post['contact-id'];
1105 $cmntdata['app'] = 'facebook';
1106 $cmntdata['created'] = datetime_convert('UTC','UTC',$cmnt->created_time);
1107 $cmntdata['edited'] = datetime_convert('UTC','UTC',$cmnt->created_time);
1108 $cmntdata['verb'] = ACTIVITY_POST;
1109 $cmntdata['author-name'] = $cmnt->from->name;
1110 $cmntdata['author-link'] = 'http://facebook.com/profile.php?id=' . $cmnt->from->id;
1111 $cmntdata['author-avatar'] = 'https://graph.facebook.com/' . $cmnt->from->id . '/picture';
1112 $cmntdata['body'] = $cmnt->message;
1113 $item = item_store($cmntdata);