4 * Installing the Friendika/Facebook connector
6 * 1. register an API key for your site from developer.facebook.com
7 * a. We'd be very happy if you include "Friendika" in the application name
8 * to increase name recognition. The Friendika icons are also present
9 * in the images directory and may be uploaded as a Facebook app icon.
10 * Use images/friendika-16.jpg for the Icon and images/friendika-128.jpg for the Logo.
11 * b. The url should be your site URL with a trailing slash.
12 * You may use http://portal.friendika.com/privacy as the privacy policy
13 * URL unless your site has different requirements, and
14 * http://portal.friendika.com as the Terms of Service URL unless
15 * you have different requirements. (Friendika is a software application
16 * and does not require Terms of Service, though your installation of it might).
17 * c. Set the following values in your .htconfig.php file
18 * $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
19 * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
20 * Replace with the settings Facebook gives you.
21 * 2. Enable the facebook plugin by including it in .htconfig.php - e.g.
22 * $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
23 * 3. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
24 * and click 'Install Facebook Connector'.
25 * 4. This will ask you to login to Facebook and grant permission to the
26 * plugin to do its stuff. Allow it to do so.
27 * 5. You're done. To turn it off visit the Plugin Settings page again and
28 * 'Remove Facebook posting'.
30 * Vidoes and embeds will not be posted if there is no other content. Links
31 * and images will be converted to a format suitable for the Facebook API and
32 * long posts truncated - with a link to view the full post.
34 * Facebook contacts will not be able to view private photos, as they are not able to
35 * authenticate to your site to establish identity. We will address this
36 * in a future release.
39 define('FACEBOOK_MAXPOSTLEN', 420);
41 /* declare the facebook_module function so that /facebook url requests will land here */
43 function facebook_module() {}
47 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
49 function facebook_init(&$a) {
55 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
62 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
63 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
67 logger('facebook_init: Error: ' . $error);
69 if($auth_code && $uid) {
71 $appid = get_config('facebook','appid');
72 $appsecret = get_config('facebook', 'appsecret');
74 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
75 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
76 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
77 . '&code=' . $auth_code);
79 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
81 if(strpos($x,'access_token=') !== false) {
82 $token = str_replace('access_token=', '', $x);
83 if(strpos($token,'&') !== false)
84 $token = substr($token,0,strpos($token,'&'));
85 set_pconfig($uid,'facebook','access_token',$token);
86 set_pconfig($uid,'facebook','post','1');
93 // todo: is this a browser session or a server session? where do we go?
99 function fb_get_self($uid) {
100 $access_token = get_pconfig($uid,'facebook','access_token');
103 $s = fetch_url('https://graph.facebook.com/me/?access_token=' . $access_token);
105 $j = json_decode($s);
106 set_pconfig($uid,'facebook','self_id',(string) $j->id);
112 function fb_get_friends($uid) {
114 $access_token = get_pconfig($uid,'facebook','access_token');
116 $no_linking = get_pconfig($uid,'facebook','no_linking');
122 $s = fetch_url('https://graph.facebook.com/me/friends?access_token=' . $access_token);
124 logger('facebook: fb_get_friends: ' . $s, LOGGER_DATA);
125 $j = json_decode($s);
126 logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
127 foreach($j->data as $person) {
128 $s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
130 $jp = json_decode($s);
131 logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
133 // always use numeric link for consistency
135 $jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
137 // check if we already have a contact
139 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
146 // check that we have all the photos, this has been known to fail on occasion
148 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
149 require_once("Photo.php");
151 $photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
153 $r = q("UPDATE `contact` SET `photo` = '%s',
159 WHERE `id` = %d LIMIT 1
164 dbesc(datetime_convert()),
165 dbesc(datetime_convert()),
166 dbesc(datetime_convert()),
174 // create contact record
175 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`,
176 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
177 `writable`, `blocked`, `readonly`, `pending` )
178 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
180 dbesc(datetime_convert()),
185 dbesc('facebook ' . $jp->id),
187 dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
188 dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
189 dbesc(NETWORK_FACEBOOK),
196 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
206 $contact_id = $r[0]['id'];
208 require_once("Photo.php");
210 $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
212 $r = q("UPDATE `contact` SET `photo` = '%s',
218 WHERE `id` = %d LIMIT 1
223 dbesc(datetime_convert()),
224 dbesc(datetime_convert()),
225 dbesc(datetime_convert()),
235 function facebook_post(&$a) {
240 $value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
241 set_pconfig($uid,'facebook','post_by_default', $value);
243 $no_linking = get_pconfig($uid,'facebook','no_linking');
245 $linkvalue = ((x($_POST,'facebook_linking')) ? intval($_POST['facebook_linking']) : 0);
246 set_pconfig($uid,'facebook','no_linking', (($linkvalue) ? 0 : 1));
248 // FB linkage was allowed but has just been turned off - remove all FB contacts and posts
250 if((! intval($no_linking)) && (! intval($linkvalue))) {
251 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s' ",
253 dbesc(NETWORK_FACEBOOK)
256 require_once('include/Contact.php');
258 contact_remove($rr['id']);
261 elseif(intval($no_linking) && intval($linkvalue)) {
262 // FB linkage is now allowed - import stuff.
264 fb_get_friends($uid);
265 fb_consume_all($uid);
268 info( t('Settings updated.') . EOL);
274 function facebook_content(&$a) {
277 notice( t('Permission denied.') . EOL);
281 if($a->argc > 1 && $a->argv[1] === 'remove') {
282 del_pconfig(local_user(),'facebook','post');
283 info( t('Facebook disabled') . EOL);
286 if($a->argc > 1 && $a->argv[1] === 'friends') {
287 fb_get_friends(local_user());
288 info( t('Updating contacts') . EOL);
292 $fb_installed = get_pconfig(local_user(),'facebook','post');
294 $appid = get_config('facebook','appid');
297 notice( t('Facebook API key is missing.') . EOL);
301 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'
302 . $a->get_baseurl() . '/addon/facebook/facebook.css' . '" media="all" />' . "\r\n";
304 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
306 if(! $fb_installed) {
307 $o .= '<div id="facebook-enable-wrapper">';
309 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
310 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook connector for this account.') . '</a>';
315 $o .= '<div id="facebook-disable-wrapper">';
317 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook connector') . '</a></div>';
319 $o .= '<div id="facebook-post-default-form">';
320 $o .= '<form action="facebook" method="post" >';
321 $post_by_default = get_pconfig(local_user(),'facebook','post_by_default');
322 $checked = (($post_by_default) ? ' checked="checked" ' : '');
323 $o .= '<input type="checkbox" name="post_by_default" value="1"' . $checked . '/>' . ' ' . t('Post to Facebook by default') . '<br />';
325 $no_linking = get_pconfig(local_user(),'facebook','no_linking');
326 $checked = (($no_linking) ? '' : ' checked="checked" ');
327 $o .= '<input type="checkbox" name="facebook_linking" value="1"' . $checked . '/>' . ' ' . t('Link all your Facebook friends and conversations') . '<br />';
331 $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
337 function facebook_install() {
338 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
339 register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
340 register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
341 register_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron');
345 function facebook_uninstall() {
346 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
347 unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
348 unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
349 unregister_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron');
353 function facebook_cron($a,$b) {
355 $last = get_config('facebook','last_poll');
357 $poll_interval = intval(get_config('facebook','poll_interval'));
359 $poll_interval = 3600;
362 $next = $last + $poll_interval;
367 logger('facebook_cron');
369 set_config('facebook','last_poll', time());
371 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'facebook' AND `k` = 'post' AND `v` = '1' ");
374 // check for new friends once a day
375 $last_friend_check = get_pconfig($rr['uid'],'facebook','friend_check');
376 if($last_friend_check)
377 $next_friend_check = $last_friend_check + 86400;
378 if($next_friend_check <= time()) {
379 fb_get_friends($rr['uid']);
380 set_pconfig($rr['uid'],'facebook','friend_check',time());
382 fb_consume_all($rr['uid']);
389 function facebook_plugin_settings(&$a,&$b) {
391 $b .= '<div class="settings-block">';
392 $b .= '<h3>' . t('Facebook') . '</h3>';
393 $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
398 function facebook_jot_nets(&$a,&$b) {
402 $fb_post = get_pconfig(local_user(),'facebook','post');
403 if(intval($fb_post) == 1) {
404 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
405 $selected = ((intval($fb_defpost) == 1) ? ' checked="checked" ' : '');
406 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> '
407 . t('Post to Facebook') . '</div>';
412 function facebook_post_hook(&$a,&$b) {
415 * Post to Facebook stream
418 require_once('include/group.php');
420 logger('Facebook post');
425 if((local_user()) && (local_user() == $b['uid'])) {
428 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
429 intval($b['parent']),
432 if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
433 $reply = substr($r[0]['uri'],4);
434 elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
435 $reply = substr($r[0]['extid'],4);
438 logger('facebook reply id=' . $reply);
441 if($b['private'] && $reply == false) {
442 $allow_people = expand_acl($b['allow_cid']);
443 $allow_groups = expand_groups(expand_acl($b['allow_gid']));
444 $deny_people = expand_acl($b['deny_cid']);
445 $deny_groups = expand_groups(expand_acl($b['deny_gid']));
447 $recipients = array_unique(array_merge($allow_people,$allow_groups));
448 $deny = array_unique(array_merge($deny_people,$deny_groups));
450 $allow_str = dbesc(implode(', ',$recipients));
452 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'");
453 $allow_arr = array();
456 $allow_arr[] = $rr['notify'];
459 $deny_str = dbesc(implode(', ',$deny));
461 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $deny_str ) AND `network` = 'face'");
465 $deny_arr[] = $rr['notify'];
468 if(count($deny_arr) && (! count($allow_arr))) {
470 // One or more FB folks were denied access but nobody on FB was specifically allowed access.
471 // This might cause the post to be open to public on Facebook, but only to selected members
472 // on another network. Since this could potentially leak a post to somebody who was denied,
473 // we will skip posting it to Facebook with a slightly vague but relevant message that will
474 // hopefully lead somebody to this code comment for a better explanation of what went wrong.
476 notice( t('Post to Facebook cancelled because of multi-network access permission conflict.') . EOL);
481 // if it's a private message but no Facebook members are allowed or denied, skip Facebook post
483 if((! count($allow_arr)) && (! count($deny_arr)))
487 if($b['verb'] == ACTIVITY_LIKE)
491 $appid = get_config('facebook', 'appid' );
492 $secret = get_config('facebook', 'appsecret' );
494 if($appid && $secret) {
496 logger('facebook: have appid+secret');
498 $fb_post = intval(get_pconfig(local_user(),'facebook','post'));
499 $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
500 $fb_token = get_pconfig(local_user(),'facebook','access_token');
502 logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG);
504 // post to facebook if it's a public post and we've ticked the 'post to Facebook' box,
505 // or it's a private message with facebook participants
506 // or it's a reply or likes action to an existing facebook post
508 if($fb_post && $fb_token && ($fb_enable || $b['private'] || $reply)) {
509 logger('facebook: able to post');
510 require_once('library/facebook.php');
511 require_once('include/bbcode.php');
515 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
517 // make links readable before we strip the code
519 // unless it's a dislike - just send the text as a comment
521 if($b['verb'] == ACTIVITY_DISLIKE)
522 $msg = trim(strip_tags(bbcode($msg)));
524 $search_str = $a->get_baseurl() . '/search';
526 if(preg_match("/\[url=(.+?)\](.+?)\[\/url\]/is",$msg,$matches)) {
528 // don't use hashtags for message link
530 if(strpos($matches[2],$search_str) === false) {
532 if(substr($matches[2],0,5) != '[img]')
533 $linkname = $matches[2];
537 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 $1',$msg);
539 if(preg_match("/\[img\](.+?)\[\/img\]/is",$msg,$matches))
540 $image = $matches[1];
542 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1', $msg);
544 if((strpos($link,$a->get_baseurl()) !== false) && (! $image))
545 $image = $a->get_baseurl() . '/images/friendika-64.jpg';
547 $msg = trim(strip_tags(bbcode($msg)));
548 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
550 // add any attachments as text urls
552 $arr = explode(',',$b['attach']);
556 foreach($arr as $r) {
558 $cnt = preg_match('|\[attach\]href=\"(.+?)\" size=\"(.+?)\" type=\"(.+?)\" title=\"(.+?)\"\[\/attach\]|',$r,$matches);
565 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
567 require_once('library/slinky.php');
569 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
570 $slinky = new Slinky( $display_url );
571 // setup a cascade of shortening services
572 // try to get a short link from these services
573 // in the order ur1.ca, trim, id.gd, tinyurl
574 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
575 $shortlink = $slinky->short();
576 // the new message will be shortened such that "... $shortlink"
577 // will fit into the character limit
578 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
579 $msg .= '... ' . $shortlink;
584 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
587 $postvars = array('access_token' => $fb_token);
591 'access_token' => $fb_token,
595 $postvars['picture'] = $image;
597 $postvars['link'] = $link;
599 $postvars['name'] = $linkname;
602 if(($b['private']) && (! $b['parent'])) {
603 $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
604 if(count($allow_arr))
605 $postvars['privacy'] .= ',"allow": "' . implode(',',$allow_arr) . '"';
607 $postvars['privacy'] .= ',"deny": "' . implode(',',$deny_arr) . '"';
608 $postvars['privacy'] .= '}';
613 $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
616 $url = 'https://graph.facebook.com/me/feed';
618 $postvars['actions'] = '{"name": "' . t('View on Friendika') . '", "link": "' . $b['plink'] . '"}';
621 logger('facebook: post to ' . $url);
622 logger('facebook: postvars: ' . print_r($postvars,true));
624 // "test_mode" prevents anything from actually being posted.
625 // Otherwise, let's do it.
627 if(! get_config('facebook','test_mode'))
628 $x = post_url($url, $postvars);
630 $retj = json_decode($x);
632 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
633 dbesc('fb::' . $retj->id),
638 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
646 function fb_consume_all($uid) {
648 require_once('include/items.php');
650 $access_token = get_pconfig($uid,'facebook','access_token');
653 $s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
655 $j = json_decode($s);
656 logger('fb_consume_stream: wall: ' . print_r($j,true), LOGGER_DATA);
657 fb_consume_stream($uid,$j,true);
659 $s = fetch_url('https://graph.facebook.com/me/home?access_token=' . $access_token);
661 $j = json_decode($s);
662 logger('fb_consume_stream: feed: ' . print_r($j,true), LOGGER_DATA);
663 fb_consume_stream($uid,$j,false);
668 function fb_consume_stream($uid,$j,$wall = false) {
671 $no_linking = get_pconfig($uid,'facebook','no_linking');
675 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
679 $user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
683 $my_local_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
686 $self_id = get_pconfig($uid,'facebook','self_id');
687 if(! count($j->data) || (! strlen($self_id)))
690 foreach($j->data as $entry) {
691 logger('fb_consume: entry: ' . print_r($entry,true), LOGGER_DATA);
694 $r = q("SELECT * FROM `item` WHERE ( `uri` = '%s' OR `extid` = '%s') AND `uid` = %d LIMIT 1",
695 dbesc('fb::' . $entry->id),
696 dbesc('fb::' . $entry->id),
702 $top_item = $r[0]['id'];
705 $post_exists = false;
710 $datarray['gravity'] = 0;
711 $datarray['uid'] = $uid;
712 $datarray['wall'] = (($wall) ? 1 : 0);
713 $datarray['uri'] = $datarray['parent-uri'] = 'fb::' . $entry->id;
714 $from = $entry->from;
715 if($from->id == $self_id)
716 $datarray['contact-id'] = $self[0]['id'];
718 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
723 $datarray['contact-id'] = $r[0]['id'];
726 // don't store post if we don't have a contact
728 if(! x($datarray,'contact-id'))
731 $datarray['verb'] = ACTIVITY_POST;
733 $datarray['owner-name'] = $self[0]['name'];
734 $datarray['owner-link'] = $self[0]['url'];
735 $datarray['owner-avatar'] = $self[0]['thumb'];
737 $datarray['author-name'] = $from->name;
738 $datarray['author-link'] = 'http://facebook.com/profile.php?id=' . $from->id;
739 $datarray['author-avatar'] = 'https://graph.facebook.com/' . $from->id . '/picture';
740 $datarray['plink'] = $datarray['author-link'] . '&v=wall&story_fbid=' . substr($entry->id,strpos($entry->id,'_') + 1);
742 $datarray['body'] = $entry->message;
744 $datarray['body'] .= "\n\n" . '[img]' . $entry->picture . '[/img]';
746 $datarray['body'] .= "\n" . linkify($entry->link);
748 $datarray['body'] .= "\n" . $entry->name;
750 $datarray['body'] .= "\n" . $entry->caption;
751 if($entry->description)
752 $datarray['body'] .= "\n" . $entry->description;
753 $datarray['created'] = datetime_convert('UTC','UTC',$entry->created_time);
754 $datarray['edited'] = datetime_convert('UTC','UTC',$entry->updated_time);
755 if($entry->privacy && $entry->privacy->value !== 'EVERYONE')
756 $datarray['private'] = 1;
757 $top_item = item_store($datarray);
758 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
766 if(isset($entry->likes) && isset($entry->likes->data))
767 $likers = $entry->likes->data;
771 if(isset($entry->comments) && isset($entry->comments->data))
772 $comments = $entry->comments->data;
776 if(is_array($likers)) {
777 foreach($likers as $likes) {
782 // If we posted the like locally, it will be found with our url, not the FB url.
784 $second_url = (($likes->id == $self_id) ? $self[0]['url'] : 'http://facebook.com/profile.php?id=' . $likes->id);
786 $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `verb` = '%s'
787 AND ( `author-link` = '%s' OR `author-link` = '%s' ) LIMIT 1",
788 dbesc($orig_post['uri']),
790 dbesc(ACTIVITY_LIKE),
791 dbesc('http://facebook.com/profile.php?id=' . $likes->id),
799 $likedata['parent'] = $top_item;
800 $likedata['verb'] = ACTIVITY_LIKE;
801 $likedata['gravity'] = 3;
802 $likedata['uid'] = $uid;
803 $likedata['wall'] = (($wall) ? 1 : 0);
804 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
805 $likedata['parent-uri'] = $orig_post['uri'];
806 if($likes->id == $self_id)
807 $likedata['contact-id'] = $self[0]['id'];
809 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
814 $likedata['contact-id'] = $r[0]['id'];
816 if(! x($likedata,'contact-id'))
817 $likedata['contact-id'] = $orig_post['contact-id'];
819 $likedata['verb'] = ACTIVITY_LIKE;
820 $likedata['author-name'] = $likes->name;
821 $likedata['author-link'] = 'http://facebook.com/profile.php?id=' . $likes->id;
822 $likedata['author-avatar'] = 'https://graph.facebook.com/' . $likes->id . '/picture';
824 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
825 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
826 $post_type = t('status');
827 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
828 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
830 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
831 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
832 '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . $orig_post['plink'] . '">') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
834 $item = item_store($likedata);
837 if(is_array($comments)) {
838 foreach($comments as $cmnt) {
843 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND ( `uri` = '%s' OR `extid` = '%s' ) LIMIT 1",
845 dbesc('fb::' . $cmnt->id),
846 dbesc('fb::' . $cmnt->id)
852 $cmntdata['parent'] = $top_item;
853 $cmntdata['verb'] = ACTIVITY_POST;
854 $cmntdata['gravity'] = 6;
855 $cmntdata['uid'] = $uid;
856 $cmntdata['wall'] = (($wall) ? 1 : 0);
857 $cmntdata['uri'] = 'fb::' . $cmnt->id;
858 $cmntdata['parent-uri'] = $orig_post['uri'];
859 if($cmnt->from->id == $self_id) {
860 $cmntdata['contact-id'] = $self[0]['id'];
863 $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d LIMIT 1",
864 dbesc($cmnt->from->id),
868 $cmntdata['contact-id'] = $r[0]['id'];
869 if($r[0]['blocked'] || $r[0]['readonly'])
873 if(! x($cmntdata,'contact-id'))
874 $cmntdata['contact-id'] = $orig_post['contact-id'];
876 $cmntdata['created'] = datetime_convert('UTC','UTC',$cmnt->created_time);
877 $cmntdata['edited'] = datetime_convert('UTC','UTC',$cmnt->created_time);
878 $cmntdata['verb'] = ACTIVITY_POST;
879 $cmntdata['author-name'] = $cmnt->from->name;
880 $cmntdata['author-link'] = 'http://facebook.com/profile.php?id=' . $cmnt->from->id;
881 $cmntdata['author-avatar'] = 'https://graph.facebook.com/' . $cmnt->from->id . '/picture';
882 $cmntdata['body'] = $cmnt->message;
883 $item = item_store($cmntdata);