+function fb_queue_hook(&$a,&$b) {
+
+ $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
+ dbesc(NETWORK_FACEBOOK)
+ );
+ if(! count($qi))
+ return;
+
+ require_once('include/queue_fn.php');
+
+ foreach($qi as $x) {
+ if($x['network'] !== NETWORK_FACEBOOK)
+ continue;
+
+ logger('facebook_queue: run');
+
+ $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
+ WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
+ intval($x['cid'])
+ );
+ if(! count($r))
+ continue;
+
+ $user = $r[0];
+
+ $appid = get_config('facebook', 'appid' );
+ $secret = get_config('facebook', 'appsecret' );
+
+ if($appid && $secret) {
+ $fb_post = intval(get_pconfig($user['uid'],'facebook','post'));
+ $fb_token = get_pconfig($user['uid'],'facebook','access_token');
+
+ if($fb_post && $fb_token) {
+ logger('facebook_queue: able to post');
+ require_once('library/facebook.php');
+
+ $z = unserialize($x['content']);
+ $item = $z['item'];
+ $j = post_url($z['url'],$z['post']);
+
+ $retj = json_decode($j);
+ if($retj->id) {
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc('fb::' . $retj->id),
+ intval($item)
+ );
+ logger('facebook_queue: success: ' . $j);
+ remove_queue_item($x['id']);
+ }
+ else {
+ logger('facebook_queue: failed: ' . $j);
+ update_queue_time($x['id']);
+ }
+ }
+ }
+ }
+}
+
+function fb_consume_all($uid) {
+
+ require_once('include/items.php');
+
+ $access_token = get_pconfig($uid,'facebook','access_token');
+ if(! $access_token)
+ return;
+ $s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
+ if($s) {
+ $j = json_decode($s);
+ logger('fb_consume_stream: wall: ' . print_r($j,true), LOGGER_DATA);
+ fb_consume_stream($uid,$j,true);
+ }
+ $s = fetch_url('https://graph.facebook.com/me/home?access_token=' . $access_token);
+ if($s) {
+ $j = json_decode($s);
+ logger('fb_consume_stream: feed: ' . print_r($j,true), LOGGER_DATA);
+ fb_consume_stream($uid,$j,false);
+ }
+
+}
+
+function fb_consume_stream($uid,$j,$wall = false) {
+ $a = get_app();
+
+ $no_linking = get_pconfig($uid,'facebook','no_linking');
+ if($no_linking)
+ return;
+
+ $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
+ intval($uid)
+ );
+
+ $user = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval($uid)
+ );
+ if(count($user))
+ $my_local_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
+
+
+ $self_id = get_pconfig($uid,'facebook','self_id');
+ if(! count($j->data) || (! strlen($self_id)))
+ return;
+
+ foreach($j->data as $entry) {
+ logger('fb_consume: entry: ' . print_r($entry,true), LOGGER_DATA);
+ $datarray = array();
+
+ $r = q("SELECT * FROM `item` WHERE ( `uri` = '%s' OR `extid` = '%s') AND `uid` = %d LIMIT 1",
+ dbesc('fb::' . $entry->id),
+ dbesc('fb::' . $entry->id),
+ intval($uid)
+ );
+ if(count($r)) {
+ $post_exists = true;
+ $orig_post = $r[0];
+ $top_item = $r[0]['id'];
+ }
+ else {
+ $post_exists = false;
+ $orig_post = null;
+ }
+
+ if(! $orig_post) {
+ $datarray['gravity'] = 0;
+ $datarray['uid'] = $uid;
+ $datarray['wall'] = (($wall) ? 1 : 0);
+ $datarray['uri'] = $datarray['parent-uri'] = 'fb::' . $entry->id;
+ $from = $entry->from;
+ if($from->id == $self_id)
+ $datarray['contact-id'] = $self[0]['id'];
+ else {
+ $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
+ dbesc($from->id),
+ intval($uid)
+ );
+ if(count($r))
+ $datarray['contact-id'] = $r[0]['id'];
+ }
+
+ // don't store post if we don't have a contact
+
+ if(! x($datarray,'contact-id')) {
+ logger('no contact: post ignored');
+ continue;
+ }
+
+ $datarray['verb'] = ACTIVITY_POST;
+ if($wall) {
+ $datarray['owner-name'] = $self[0]['name'];
+ $datarray['owner-link'] = $self[0]['url'];
+ $datarray['owner-avatar'] = $self[0]['thumb'];
+ }
+ $datarray['author-name'] = $from->name;
+ $datarray['author-link'] = 'http://facebook.com/profile.php?id=' . $from->id;
+ $datarray['author-avatar'] = 'https://graph.facebook.com/' . $from->id . '/picture';
+ $datarray['plink'] = $datarray['author-link'] . '&v=wall&story_fbid=' . substr($entry->id,strpos($entry->id,'_') + 1);
+
+ $datarray['body'] = $entry->message;
+ if($entry->picture)
+ $datarray['body'] .= "\n\n" . '[img]' . $entry->picture . '[/img]';
+ if($entry->link)
+ $datarray['body'] .= "\n" . linkify($entry->link);
+ if($entry->name)
+ $datarray['body'] .= "\n" . $entry->name;
+ if($entry->caption)
+ $datarray['body'] .= "\n" . $entry->caption;
+ if($entry->description)
+ $datarray['body'] .= "\n" . $entry->description;
+ $datarray['created'] = datetime_convert('UTC','UTC',$entry->created_time);
+ $datarray['edited'] = datetime_convert('UTC','UTC',$entry->updated_time);
+ if($entry->privacy && $entry->privacy->value !== 'EVERYONE')
+ $datarray['private'] = 1;
+ $top_item = item_store($datarray);
+ $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+ intval($top_item),
+ intval($uid)
+ );
+ if(count($r)) {
+ $orig_post = $r[0];
+ logger('fb: new top level item posted');
+ }
+ }
+
+ if(isset($entry->likes) && isset($entry->likes->data))
+ $likers = $entry->likes->data;
+ else
+ $likers = null;
+
+ if(isset($entry->comments) && isset($entry->comments->data))
+ $comments = $entry->comments->data;
+ else
+ $comments = null;
+
+ if(is_array($likers)) {
+ foreach($likers as $likes) {
+
+ if(! $orig_post)
+ continue;
+
+ // If we posted the like locally, it will be found with our url, not the FB url.
+
+ $second_url = (($likes->id == $self_id) ? $self[0]['url'] : 'http://facebook.com/profile.php?id=' . $likes->id);
+
+ $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `verb` = '%s'
+ AND ( `author-link` = '%s' OR `author-link` = '%s' ) LIMIT 1",
+ dbesc($orig_post['uri']),
+ intval($uid),
+ dbesc(ACTIVITY_LIKE),
+ dbesc('http://facebook.com/profile.php?id=' . $likes->id),
+ dbesc($second_url)
+ );
+
+ if(count($r))
+ continue;
+
+ $likedata = array();
+ $likedata['parent'] = $top_item;
+ $likedata['verb'] = ACTIVITY_LIKE;
+ $likedata['gravity'] = 3;
+ $likedata['uid'] = $uid;
+ $likedata['wall'] = (($wall) ? 1 : 0);
+ $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
+ $likedata['parent-uri'] = $orig_post['uri'];
+ if($likes->id == $self_id)
+ $likedata['contact-id'] = $self[0]['id'];
+ else {
+ $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
+ dbesc($likes->id),
+ intval($uid)
+ );
+ if(count($r))
+ $likedata['contact-id'] = $r[0]['id'];
+ }
+ if(! x($likedata,'contact-id'))
+ $likedata['contact-id'] = $orig_post['contact-id'];
+
+ $likedata['verb'] = ACTIVITY_LIKE;
+ $likedata['author-name'] = $likes->name;
+ $likedata['author-link'] = 'http://facebook.com/profile.php?id=' . $likes->id;
+ $likedata['author-avatar'] = 'https://graph.facebook.com/' . $likes->id . '/picture';