+ if($likes) {
+ $postvars = array('access_token' => $fb_token);
+ }
+ else {
+ $postvars = array(
+ 'access_token' => $fb_token,
+ 'message' => $msg
+ );
+ if(isset($image))
+ $postvars['picture'] = $image;
+ if(isset($link))
+ $postvars['link'] = $link;
+ if(isset($linkname))
+ $postvars['name'] = $linkname;
+ }
+
+ if(($b['private']) && (! $b['parent'])) {
+ $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
+ if(count($allow_arr))
+ $postvars['privacy'] .= ',"allow": "' . implode(',',$allow_arr) . '"';
+ if(count($deny_arr))
+ $postvars['privacy'] .= ',"deny": "' . implode(',',$deny_arr) . '"';
+ $postvars['privacy'] .= '}';
+
+ }
+
+ if($reply) {
+ $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
+ }
+ else {
+ $url = 'https://graph.facebook.com/me/feed';
+ if($b['plink'])
+ $postvars['actions'] = '{"name": "' . t('View on Friendika') . '", "link": "' . $b['plink'] . '"}';
+ }
+
+ logger('facebook: post to ' . $url);
+ logger('facebook: postvars: ' . print_r($postvars,true));
+
+ // "test_mode" prevents anything from actually being posted.
+ // Otherwise, let's do it.
+
+ if(! get_config('facebook','test_mode')) {
+ $x = post_url($url, $postvars);
+
+ $retj = json_decode($x);
+ if($retj->id) {
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc('fb::' . $retj->id),
+ intval($b['id'])
+ );
+ }
+ else {
+ if(! $likes) {
+ $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $postvars));
+ q("INSERT INTO `queue` ( `network`, `cid`, `created`, `last`, `content`)
+ VALUES ( '%s', %d, '%s', '%s', '%s') ",
+ dbesc(NETWORK_FACEBOOK),
+ intval($a->contact),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc($s)
+ );
+
+ notice( t('Facebook post failed. Queued for retry.') . EOL);
+ }
+ }
+
+ logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
+ }
+ }
+ }
+ }
+}
+
+
+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;