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.
6 * If you have issues upgrading, remove facebook from the addon list,
7 * view a page on your site, then add it back to the list. This will reset
8 * all of the plugin 'hooks'.
10 * 1. register an API key from developer.facebook.com
11 * a. We'd be very happy if you include "Friendika" in the application name
12 * to increase name recognition.
13 * b. The url should be your site URL with a trailing slash
14 * c. Set the following values in your .htconfig.php file
15 * $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
16 * $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
17 * Replace with the settings Facebook gives you.
18 * 2. Enable the facebook plugin by including it in .htconfig.php - e.g.
19 * $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
20 * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook)
21 * and click 'Install Facebook posting'.
22 * 4. This will ask you to login to Facebook and grant permission to the
23 * plugin to do its stuff. Allow it to do so.
24 * 5. You're done. To turn it off visit your site's /facebook page again and
25 * 'Remove Facebook posting'.
27 * Turn logging on (see the github Friendika wiki page 'Settings') and
28 * repeat these steps if you have trouble.
29 * Vidoes and embeds will not be posted if there is no other content. Links
30 * and images will be converted to text and long posts truncated - with a link
31 * to view the full post. Posts with permission settings and comments will
32 * not be posted to Facebook.
36 define('FACEBOOK_MAXPOSTLEN', 420);
38 /* declare the facebook_module function so that /facebook url requests will land here */
40 function facebook_module() {}
44 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
46 function facebook_init(&$a) {
52 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
59 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
60 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
64 logger('facebook_init: Error: ' . $error);
66 if($auth_code && $uid) {
68 $appid = get_config('facebook','appid');
69 $appsecret = get_config('facebook', 'appsecret');
71 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
72 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
73 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
74 . '&code=' . $auth_code);
76 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
78 if(strpos($x,'access_token=') !== false) {
79 $token = str_replace('access_token=', '', $x);
80 if(strpos($token,'&') !== false)
81 $token = substr($token,0,strpos($token,'&'));
82 set_pconfig($uid,'facebook','access_token',$token);
83 set_pconfig($uid,'facebook','post','1');
86 // todo: is this a browser session or a server session? where do we go?
91 function facebook_content(&$a) {
94 notice( t('Permission denied.') . EOL);
98 if($a->argc > 1 && $a->argv[1] === 'remove') {
99 del_pconfig(local_user(),'facebook','post');
100 notice( t('Facebook disabled') . EOL);
103 $appid = get_config('facebook','appid');
106 notify( t('Facebook API key is missing.') . EOL);
110 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
114 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
115 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a><br /><br />';
117 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a><br />';
123 function facebook_install() {
124 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
125 register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
126 register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
130 function facebook_uninstall() {
131 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
132 unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets');
133 unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
137 function facebook_plugin_settings(&$a,&$b) {
139 $b .= '<h3>' . t('Facebook') . '</h3>';
140 $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
144 function facebook_jot_nets(&$a,&$b) {
148 $fb_post = get_pconfig(local_user(),'facebook','post');
149 if(intval($fb_post) == 1) {
150 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
151 $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : '');
152 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> '
153 . t('Post to Facebook') . '</div>';
158 function facebook_post_hook(&$a,&$b) {
161 * Post to Facebook stream
164 logger('Facebook post');
166 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
169 $appid = get_config('facebook', 'appid' );
170 $secret = get_config('facebook', 'appsecret' );
172 if($appid && $secret) {
174 logger('facebook: have appid+secret');
176 $fb_post = intval(get_pconfig(local_user(),'facebook','post'));
177 $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
178 $fb_token = get_pconfig(local_user(),'facebook','access_token');
180 logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG);
181 if($fb_post && $fb_token && $fb_enable) {
182 logger('facebook: able to post');
183 require_once('library/facebook.php');
184 require_once('include/bbcode.php');
188 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
190 // make links readable before we strip the code
192 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
194 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
196 $msg = trim(strip_tags(bbcode($msg)));
197 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
199 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
201 require_once('addon/twitter/slinky.php');
203 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
204 $slinky = new Slinky( $posturl );
205 // setup a cascade of shortening services
206 // try to get a short link from these services
207 // in the order ur1.ca, trim, id.gd, tinyurl
208 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
209 $shortlink = $slinky->short();
210 // the new message will be shortened such that "... $shortlink"
211 // will fit into the character limit
212 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
213 $msg .= '... ' . $shortlink;
218 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
220 $postvars = array('access_token' => $fb_token, 'message' => $msg);
222 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
224 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);