4 * This module still needs a lot of work, but is functional today.
5 * Please review this section if you upgrade because things will change.
7 * 1. register an API key from developer.facebook.com
8 * a. We'd be very happy if you include "Friendika" in the application name
9 * to increase name recognition.
10 * b. The url should be your site URL with a trailing slash
11 * c. Set the following values in your .htconfig.php file
12 * $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
13 * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
14 * Replace with the settings Facebook gives you.
15 * 2. Enable the facebook plugin by including it in .htconfig.php - e.g.
16 * $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
17 * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook)
18 * and click 'Install Facebook posting'.
19 * 4. This will ask you to login to Facebook and grant permission to the
20 * plugin to do its stuff. Allow it to do so.
21 * 5. You're done. To turn it off visit your site's /facebook page again and
22 * 'Remove Facebook posting'.
24 * Turn logging on (see the github Friendika wiki page 'Settings') and
25 * repeat these steps if you have trouble.
26 * Vidoes and embeds will not be posted if there is no other content. Links
27 * and images will be converted to text and long posts truncated - with a link
28 * to view the full post. Posts with permission settings and comments will
29 * not be posted to Facebook.
33 define('FACEBOOK_MAXPOSTLEN', 420);
35 /* declare the facebook_module function so that /facebook url requests will land here */
37 function facebook_module() {}
41 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
43 function facebook_init(&$a) {
49 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
56 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
57 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
61 logger('facebook_init: Error: ' . $error);
63 if($auth_code && $uid) {
65 $appid = get_config('facebook','appid');
66 $appsecret = get_config('facebook', 'appsecret');
68 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
69 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
70 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
71 . '&code=' . $auth_code);
73 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
75 if(strpos($x,'access_token=') !== false) {
76 $token = str_replace('access_token=', '', $x);
77 if(strpos($token,'&') !== false)
78 $token = substr($token,0,strpos($token,'&'));
79 set_pconfig($uid,'facebook','access_token',$token);
80 set_pconfig($uid,'facebook','post','true');
83 // todo: is this a browser session or a server session? where do we go?
88 function facebook_content(&$a) {
91 notice( t('Permission denied.') . EOL);
95 if($a->argc > 1 && $a->argv[1] === 'remove') {
96 del_pconfig(local_user(),'facebook','post');
97 notice( t('Facebook disabled') . EOL);
100 $appid = get_config('facebook','appid');
103 notify( t('Facebook API key is missing.') . EOL);
107 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
111 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
112 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook posting') . '</a><br />';
114 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook posting') . '</a><br />';
120 function facebook_install() {
121 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
125 function facebook_uninstall() {
126 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
130 function facebook_post_hook(&$a,&$b) {
133 * Post to Facebook stream
136 logger('Facebook post');
138 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
141 $appid = get_config('facebook', 'appid' );
142 $secret = get_config('facebook', 'appsecret' );
144 if($appid && $secret) {
146 $fb_post = get_pconfig(local_user(),'facebook','post');
147 $fb_token = get_pconfig(local_user(),'facebook','access_token');
149 if($fb_post && $fb_token) {
150 require_once('library/facebook.php');
151 require_once('include/bbcode.php');
155 logger('Facebook post2: msg=' . $msg, LOGGER_DATA);
157 // make links readable before we strip the code
159 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
161 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
163 $msg = trim(strip_tags(bbcode($msg)));
165 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
167 require_once('addon/twitter/slinky.php');
169 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
170 $slinky = new Slinky( $posturl );
171 // setup a cascade of shortening services
172 // try to get a short link from these services
173 // in the order ur1.ca, trim, id.gd, tinyurl
174 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
175 $shortlink = $slinky->short();
176 // the new message will be shortened such that "... $shortlink"
177 // will fit into the character limit
178 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
179 $msg .= '... ' . $shortlink;
184 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
186 $postvars = array('access_token' => $fb_token, 'message' => $msg);
188 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
190 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);