3 * Name: Facebook Post Connector
5 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
6 * Author: Tobias Hößl <https://github.com/CatoTH/>
11 * Installing the Friendica/Facebook connector
13 * Detailed instructions how to use this plugin can be found at
14 * https://github.com/friendica/friendica/wiki/How-to:-Friendica%E2%80%99s-Facebook-connector
16 * Vidoes and embeds will not be posted if there is no other content. Links
17 * and images will be converted to a format suitable for the Facebook API and
18 * long posts truncated - with a link to view the full post.
20 * Facebook contacts will not be able to view private photos, as they are not able to
21 * authenticate to your site to establish identity. We will address this
22 * in a future release.
26 require_once('include/security.php');
28 function fbpost_install() {
29 register_hook('post_local', 'addon/fbpost/fbpost.php', 'fbpost_post_local');
30 register_hook('notifier_normal', 'addon/fbpost/fbpost.php', 'fbpost_post_hook');
31 register_hook('jot_networks', 'addon/fbpost/fbpost.php', 'fbpost_jot_nets');
32 register_hook('connector_settings', 'addon/fbpost/fbpost.php', 'fbpost_plugin_settings');
33 register_hook('enotify', 'addon/fbpost/fbpost.php', 'fbpost_enotify');
34 register_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
38 function fbpost_uninstall() {
39 unregister_hook('post_local', 'addon/fbpost/fbpost.php', 'fbpost_post_local');
40 unregister_hook('notifier_normal', 'addon/fbpost/fbpost.php', 'fbpost_post_hook');
41 unregister_hook('jot_networks', 'addon/fbpost/fbpost.php', 'fbpost_jot_nets');
42 unregister_hook('connector_settings', 'addon/fbpost/fbpost.php', 'fbpost_plugin_settings');
43 unregister_hook('enotify', 'addon/fbpost/fbpost.php', 'fbpost_enotify');
44 unregister_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
50 /* declare the fbpost_module function so that /fbpost url requests will land here */
52 function fbpost_module() {}
56 // If a->argv[1] is a nickname, this is a callback from Facebook oauth requests.
57 // If $_REQUEST["realtime_cb"] is set, this is a callback from the Real-Time Updates API
62 function fbpost_init(&$a) {
70 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
73 if(!(isset($r) && count($r)))
77 $auth_code = (x($_GET, 'code') ? $_GET['code'] : '');
78 $error = (x($_GET, 'error_description') ? $_GET['error_description'] : '');
82 logger('fbpost_init: Error: ' . $error);
84 if($auth_code && $uid) {
86 $appid = get_config('facebook','appid');
87 $appsecret = get_config('facebook', 'appsecret');
89 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
90 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
91 . urlencode($a->get_baseurl() . '/fbpost/' . $nick)
92 . '&code=' . $auth_code);
94 logger('fbpost_init: returned access token: ' . $x, LOGGER_DATA);
96 if(strpos($x,'access_token=') !== false) {
97 $token = str_replace('access_token=', '', $x);
98 if(strpos($token,'&') !== false)
99 $token = substr($token,0,strpos($token,'&'));
100 set_pconfig($uid,'facebook','access_token',$token);
101 set_pconfig($uid,'facebook','post','1');
102 fbpost_get_self($uid);
113 function fbpost_get_self($uid) {
114 $access_token = get_pconfig($uid,'facebook','access_token');
117 $s = fetch_url('https://graph.facebook.com/me/?access_token=' . $access_token);
119 $j = json_decode($s);
120 set_pconfig($uid,'facebook','self_id',(string) $j->id);
125 // This is the POST method to the facebook settings page
126 // Content is posted to Facebook in the function facebook_post_hook()
131 function fbpost_post(&$a) {
137 $fb_limited = get_config('facebook','crestrict');
140 $value = ((x($_POST,'post_by_default')) ? intval($_POST['post_by_default']) : 0);
141 set_pconfig($uid,'facebook','post_by_default', $value);
144 info( t('Settings updated.') . EOL);
150 // Facebook settings form
156 function fbpost_content(&$a) {
159 notice( t('Permission denied.') . EOL);
164 if(! service_class_allows(local_user(),'facebook_connect')) {
165 notice( t('Permission denied.') . EOL);
166 return upgrade_bool_message();
170 if($a->argc > 1 && $a->argv[1] === 'remove') {
171 del_pconfig(local_user(),'facebook','post');
172 info( t('Facebook Post disabled') . EOL);
177 $fb_installed = false;
178 if (get_pconfig(local_user(),'facebook','post')) {
179 $access_token = get_pconfig(local_user(),'facebook','access_token');
181 $s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
183 $j = json_decode($s);
184 if (isset($j->data)) $fb_installed = true;
189 $appid = get_config('facebook','appid');
192 notice( t('Facebook API key is missing.') . EOL);
196 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'
197 . $a->get_baseurl() . '/addon/fbpost/fbpost.css' . '" media="all" />' . "\r\n";
199 $o .= '<h3>' . t('Facebook Post') . '</h3>';
201 if(! $fb_installed) {
202 $o .= '<div id="fbpost-enable-wrapper">';
204 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
205 . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=publish_stream,offline_access">' . t('Install Facebook Post connector for this account.') . '</a>';
210 $o .= '<div id="fbpost-disable-wrapper">';
212 $o .= '<a href="' . $a->get_baseurl() . '/fbpost/remove' . '">' . t('Remove Facebook Post connector') . '</a></div>';
214 $o .= '<div id="fbpost-enable-wrapper">';
216 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
217 . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=publish_stream,offline_access">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
220 $o .= '<div id="fbpost-post-default-form">';
221 $o .= '<form action="fbpost" method="post" >';
222 $post_by_default = get_pconfig(local_user(),'facebook','post_by_default');
223 $checked = (($post_by_default) ? ' checked="checked" ' : '');
224 $o .= '<input type="checkbox" name="post_by_default" value="1"' . $checked . '/>' . ' ' . t('Post to Facebook by default') . EOL;
226 $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
234 * @param null|object $b
236 function fbpost_plugin_settings(&$a,&$b) {
238 $b .= '<div class="settings-block">';
239 $b .= '<h3>' . t('Facebook') . '</h3>';
240 $b .= '<a href="fbpost">' . t('Facebook Post Settings') . '</a><br />';
248 * @param null|object $o
250 function fbpost_plugin_admin(&$a, &$o){
253 $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("fbsave") . '">';
255 $o .= '<h4>' . t('Facebook API Key') . '</h4>';
257 $appid = get_config('facebook', 'appid' );
258 $appsecret = get_config('facebook', 'appsecret' );
260 $ret1 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appid' LIMIT 1");
261 $ret2 = q("SELECT `v` FROM `config` WHERE `cat` = 'facebook' AND `k` = 'appsecret' LIMIT 1");
262 if ((count($ret1) > 0 && $ret1[0]['v'] != $appid) || (count($ret2) > 0 && $ret2[0]['v'] != $appsecret)) $o .= t('Error: it appears that you have specified the App-ID and -Secret in your .htconfig.php file. As long as they are specified there, they cannot be set using this form.<br><br>');
264 $o .= '<label for="fb_appid">' . t('App-ID / API-Key') . '</label><input id="fb_appid" name="appid" type="text" value="' . escape_tags($appid ? $appid : "") . '"><br style="clear: both;">';
265 $o .= '<label for="fb_appsecret">' . t('Application secret') . '</label><input id="fb_appsecret" name="appsecret" type="text" value="' . escape_tags($appsecret ? $appsecret : "") . '"><br style="clear: both;">';
267 $o .= '<input type="submit" name="fb_save_keys" value="' . t('Save') . '">';
275 function fbpost_plugin_admin_post(&$a){
276 check_form_security_token_redirectOnErr('/admin/plugins/fbpost', 'fbsave');
278 if (x($_REQUEST,'fb_save_keys')) {
279 set_config('facebook', 'appid', $_REQUEST['appid']);
280 set_config('facebook', 'appsecret', $_REQUEST['appsecret']);
282 info(t('The new values have been saved.'));
292 function fbpost_jot_nets(&$a,&$b) {
296 $fb_post = get_pconfig(local_user(),'facebook','post');
297 if(intval($fb_post) == 1) {
298 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
299 $selected = ((intval($fb_defpost) == 1) ? ' checked="checked" ' : '');
300 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . ' value="1" /> '
301 . t('Post to Facebook') . '</div>';
311 function fbpost_post_hook(&$a,&$b) {
314 if($b['deleted'] || ($b['created'] !== $b['edited']))
318 * Post to Facebook stream
321 require_once('include/group.php');
322 require_once('include/html2plain.php');
324 logger('Facebook post');
330 $allow_arr = array();
332 $toplevel = (($b['id'] == $b['parent']) ? true : false);
335 $linking = ((get_pconfig($b['uid'],'facebook','no_linking')) ? 0 : 1);
337 if((! $toplevel) && ($linking)) {
338 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
339 intval($b['parent']),
342 if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
343 $reply = substr($r[0]['uri'],4);
344 elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
345 $reply = substr($r[0]['extid'],4);
349 $u = q("SELECT * FROM user where uid = %d limit 1",
355 // only accept comments from the item owner. Other contacts are unknown to FB.
357 if(! link_compare($b['author-link'], $a->get_baseurl() . '/profile/' . $u[0]['nickname']))
361 logger('facebook reply id=' . $reply);
364 if(strstr($b['postopts'],'facebook') || ($b['private']) || ($reply)) {
366 if($b['private'] && $reply === false) {
367 $allow_people = expand_acl($b['allow_cid']);
368 $allow_groups = expand_groups(expand_acl($b['allow_gid']));
369 $deny_people = expand_acl($b['deny_cid']);
370 $deny_groups = expand_groups(expand_acl($b['deny_gid']));
372 $recipients = array_unique(array_merge($allow_people,$allow_groups));
373 $deny = array_unique(array_merge($deny_people,$deny_groups));
375 $allow_str = dbesc(implode(', ',$recipients));
377 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'");
380 $allow_arr[] = $rr['notify'];
383 $deny_str = dbesc(implode(', ',$deny));
385 $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $deny_str ) AND `network` = 'face'");
388 $deny_arr[] = $rr['notify'];
391 if(count($deny_arr) && (! count($allow_arr))) {
393 // One or more FB folks were denied access but nobody on FB was specifically allowed access.
394 // This might cause the post to be open to public on Facebook, but only to selected members
395 // on another network. Since this could potentially leak a post to somebody who was denied,
396 // we will skip posting it to Facebook with a slightly vague but relevant message that will
397 // hopefully lead somebody to this code comment for a better explanation of what went wrong.
399 notice( t('Post to Facebook cancelled because of multi-network access permission conflict.') . EOL);
404 // if it's a private message but no Facebook members are allowed or denied, skip Facebook post
406 if((! count($allow_arr)) && (! count($deny_arr)))
410 if($b['verb'] == ACTIVITY_LIKE)
414 $appid = get_config('facebook', 'appid' );
415 $secret = get_config('facebook', 'appsecret' );
417 if($appid && $secret) {
419 logger('facebook: have appid+secret');
421 $fb_token = get_pconfig($b['uid'],'facebook','access_token');
424 // post to facebook if it's a public post and we've ticked the 'post to Facebook' box,
425 // or it's a private message with facebook participants
426 // or it's a reply or likes action to an existing facebook post
428 if($fb_token && ($toplevel || $b['private'] || $reply)) {
429 logger('facebook: able to post');
430 require_once('library/facebook.php');
431 require_once('include/bbcode.php');
435 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
437 // make links readable before we strip the code
439 // unless it's a dislike - just send the text as a comment
441 // if($b['verb'] == ACTIVITY_DISLIKE)
442 // $msg = trim(strip_tags(bbcode($msg)));
445 /*$search_str = $a->get_baseurl() . '/search';
447 if(preg_match("/\[url=(.*?)\](.*?)\[\/url\]/is",$msg,$matches)) {
449 // don't use hashtags for message link
451 if(strpos($matches[2],$search_str) === false) {
453 if(substr($matches[2],0,5) != '[img]')
454 $linkname = $matches[2];
458 // strip tag links to avoid link clutter, this really should be
459 // configurable because we're losing information
461 $msg = preg_replace("/\#\[url=(.*?)\](.*?)\[\/url\]/is",'#$2',$msg);
463 // provide the link separately for normal links
464 $msg = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/is",'$2 $1',$msg);
466 if(preg_match("/\[img\](.*?)\[\/img\]/is",$msg,$matches))
467 $image = $matches[1];
469 $msg = preg_replace("/\[img\](.*?)\[\/img\]/is", t('Image: ') . '$1', $msg);
471 if((strpos($link,z_root()) !== false) && (! $image))
472 $image = $a->get_baseurl() . '/images/friendica-64.jpg';
474 $msg = trim(strip_tags(bbcode($msg)));*/
478 // Looking for the first image
480 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
481 $image = $matches[3];
484 if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
485 $image = $matches[1];
487 // When saved into the database the content is sent through htmlspecialchars
488 // That means that we have to decode all image-urls
489 $image = htmlspecialchars_decode($image);
491 // Checking for a bookmark element
493 if (strpos($body, "[bookmark") !== false) {
494 // splitting the text in two parts:
495 // before and after the bookmark
496 $pos = strpos($body, "[bookmark");
497 $body1 = substr($body, 0, $pos);
498 $body2 = substr($body, $pos);
500 // Removing the bookmark and all quotes after the bookmark
501 // they are mostly only the content after the bookmark.
502 $body2 = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'',$body2);
503 $body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
504 $body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
506 $body = $body1.$body2;
509 // At first convert the text to html
510 $html = bbcode($body, false, false);
512 // Then convert it to plain text
513 $msg = trim($b['title']." \n\n".html2plain($html, 0, true));
514 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
516 // Removing multiple newlines
517 while (strpos($msg, "\n\n\n") !== false)
518 $msg = str_replace("\n\n\n", "\n\n", $msg);
520 // add any attachments as text urls
521 $arr = explode(',',$b['attach']);
525 foreach($arr as $r) {
527 $cnt = preg_match('|\[attach\]href=\"(.*?)\" size=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
529 $msg .= "\n".$matches[1];
536 // look for bookmark-bbcode and handle it with priority
537 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) {
539 $linkname = $matches[2];
542 // If there is no bookmark element then take the first link
544 $links = collecturls($html);
545 if (sizeof($links) > 0) {
547 $link = current($links);
551 // Remove trailing and leading spaces
555 // Fallback - if message is empty
565 // If there is nothing to post then exit
569 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
572 $postvars = array('access_token' => $fb_token);
575 // message, picture, link, name, caption, description, source, place, tags
577 'access_token' => $fb_token,
580 if(trim($image) != "") {
581 $postvars['picture'] = $image;
583 if(trim($link) != "") {
584 $postvars['link'] = $link;
586 // The following doesn't work - why?
587 if ((stristr($link,'youtube')) || (stristr($link,'youtu.be')) || (stristr($link,'vimeo'))) {
588 $postvars['source'] = $link;
591 if(trim($linkname) != "")
592 $postvars['name'] = $linkname;
595 if(($b['private']) && ($toplevel)) {
596 $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
597 if(count($allow_arr))
598 $postvars['privacy'] .= ',"allow": "' . implode(',',$allow_arr) . '"';
600 $postvars['privacy'] .= ',"deny": "' . implode(',',$deny_arr) . '"';
601 $postvars['privacy'] .= '}';
606 $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
607 } else if (($link != "") or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
608 $url = 'https://graph.facebook.com/me/feed';
610 $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' . $b['plink'] . '"}';
612 // if its only a message and a subject and the message is larger than 500 characters then post it as note
614 'access_token' => $fb_token,
615 'message' => bbcode($b['body'], false, false),
616 'subject' => $b['title'],
618 $url = 'https://graph.facebook.com/me/notes';
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);
629 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
631 $retj = json_decode($x);
633 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
634 dbesc('fb::' . $retj->id),
640 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $postvars));
641 require_once('include/queue_fn.php');
642 add_to_queue($a->contact,NETWORK_FACEBOOK,$s);
643 notice( t('Facebook post failed. Queued for retry.') . EOL);
646 if (isset($retj->error) && $retj->error->type == "OAuthException" && $retj->error->code == 190) {
647 logger('Facebook session has expired due to changed password.', LOGGER_DEBUG);
649 $last_notification = get_pconfig($b['uid'], 'facebook', 'session_expired_mailsent');
650 if (!$last_notification || $last_notification < (time() - FACEBOOK_SESSION_ERR_NOTIFICATION_INTERVAL)) {
651 require_once('include/enotify.php');
653 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($b['uid']) );
656 'type' => NOTIFY_SYSTEM,
657 'system_type' => 'facebook_connection_invalid',
658 'language' => $r[0]['language'],
659 'to_name' => $r[0]['username'],
660 'to_email' => $r[0]['email'],
661 'source_name' => t('Administrator'),
662 'source_link' => $a->config["system"]["url"],
663 'source_photo' => $a->config["system"]["url"] . '/images/person-80.jpg',
666 set_pconfig($b['uid'], 'facebook', 'session_expired_mailsent', time());
667 } else logger('Facebook: No notification, as the last one was sent on ' . $last_notification, LOGGER_DEBUG);
678 * @param object $data
680 function fbpost_enotify(&$app, &$data) {
681 if (x($data, 'params') && $data['params']['type'] == NOTIFY_SYSTEM && x($data['params'], 'system_type') && $data['params']['system_type'] == 'facebook_connection_invalid') {
682 $data['itemlink'] = '/facebook';
683 $data['epreamble'] = $data['preamble'] = t('Your Facebook connection became invalid. Please Re-authenticate.');
684 $data['subject'] = t('Facebook connection became invalid');
685 $data['body'] = sprintf( t("Hi %1\$s,\n\nThe connection between your accounts on %2\$s and Facebook became invalid. This usually happens after you change your Facebook-password. To enable the connection again, you have to %3\$sre-authenticate the Facebook-connector%4\$s."), $data['params']['to_name'], "[url=" . $app->config["system"]["url"] . "]" . $app->config["sitename"] . "[/url]", "[url=" . $app->config["system"]["url"] . "/facebook]", "[/url]");
693 function fbpost_post_local(&$a,&$b) {
695 // Figure out if Facebook posting is enabled for this post and file it in 'postopts'
696 // where we will discover it during background delivery.
698 // This can only be triggered by a local user posting to their own wall.
700 if((local_user()) && (local_user() == $b['uid'])) {
702 $fb_post = intval(get_pconfig(local_user(),'facebook','post'));
703 $fb_enable = (($fb_post && x($_REQUEST,'facebook_enable')) ? intval($_REQUEST['facebook_enable']) : 0);
705 // if API is used, default to the chosen settings
706 // but allow a specific override
708 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default'))) {
709 if(! x($_REQUEST,'facebook_enable'))
716 if(strlen($b['postopts']))
717 $b['postopts'] .= ',';
718 $b['postopts'] .= 'facebook';
727 function fbpost_queue_hook(&$a,&$b) {
729 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
730 dbesc(NETWORK_FACEBOOK)
735 require_once('include/queue_fn.php');
738 if($x['network'] !== NETWORK_FACEBOOK)
741 logger('facebook_queue: run');
743 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
744 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
752 $appid = get_config('facebook', 'appid' );
753 $secret = get_config('facebook', 'appsecret' );
755 if($appid && $secret) {
756 $fb_post = intval(get_pconfig($user['uid'],'facebook','post'));
757 $fb_token = get_pconfig($user['uid'],'facebook','access_token');
759 if($fb_post && $fb_token) {
760 logger('facebook_queue: able to post');
761 require_once('library/facebook.php');
763 $z = unserialize($x['content']);
765 $j = post_url($z['url'],$z['post']);
767 $retj = json_decode($j);
769 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
770 dbesc('fb::' . $retj->id),
773 logger('facebook_queue: success: ' . $j);
774 remove_queue_item($x['id']);
777 logger('facebook_queue: failed: ' . $j);
778 update_queue_time($x['id']);
787 * @return bool|string
789 function fbpost_get_app_access_token() {
791 $acc_token = get_config('facebook','app_access_token');
793 if ($acc_token !== false) return $acc_token;
795 $appid = get_config('facebook','appid');
796 $appsecret = get_config('facebook', 'appsecret');
798 if ($appid === false || $appsecret === false) {
799 logger('fb_get_app_access_token: appid and/or appsecret not set', LOGGER_DEBUG);
802 logger('https://graph.facebook.com/oauth/access_token?client_id=' . $appid . '&client_secret=' . $appsecret . '&grant_type=client_credentials', LOGGER_DATA);
803 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id=' . $appid . '&client_secret=' . $appsecret . '&grant_type=client_credentials');
805 if(strpos($x,'access_token=') !== false) {
806 logger('fb_get_app_access_token: returned access token: ' . $x, LOGGER_DATA);
808 $token = str_replace('access_token=', '', $x);
809 if(strpos($token,'&') !== false)
810 $token = substr($token,0,strpos($token,'&'));
813 logger('fb_get_app_access_token: empty token: ' . $x, LOGGER_DEBUG);
816 set_config('facebook','app_access_token',$token);
819 logger('fb_get_app_access_token: response did not contain an access_token: ' . $x, LOGGER_DATA);