4 * This module needs a lot of work.
6 * - setting/storing preferences
7 * - documentation on how to obtain FB API keys for your site
8 * - ensuring a valid FB login session
9 * - requesting permissions within the FB login session to post on your behalf until permission revoked.
13 define('FACEBOOK_MAXPOSTLEN', 420);
15 /* declare the facebook_module function so that /facebook url requests will land here */
17 function facebook_module() {}
21 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
23 function facebook_init(&$a) {
29 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
36 $auth_code = (($_GET['code']) ? $_GET['code'] : '');
37 $error = (($_GET['error_description']) ? $_GET['error_description'] : '');
41 logger('facebook_init: Error: ' . $error);
43 if($auth_code && $uid) {
45 $appid = get_config('facebook','appid');
46 $appsecret = get_config('facebook', 'appsecret');
48 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
49 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
50 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
51 . '&code=' . $auth_code);
53 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
55 if(strpos($x,'access_token=') !== false) {
56 $token = str_replace('access_token=', '', $x);
57 if(strpos($token,'&') !== false)
58 $token = substr($token,0,strpos($token,'&'));
59 set_pconfig($uid,'facebook','access_token',$token);
60 set_pconfig($uid,'facebook','post','true');
63 // todo: is this a browser session or a server session? where do we go?
68 function facebook_content(&$a) {
71 notice( t('Permission denied.') . EOL);
75 if($a->argc > 1 && $a->argv[1] === 'remove') {
76 del_pconfig(local_user(),'facebook','post');
77 notice( t('Facebook disabled') . EOL);
80 $appid = get_config('facebook','appid');
83 notify( t('Facebook API key is missing.') . EOL);
87 $o .= '<h3>' . t('Facebook Connect') . '</h3>';
91 $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri='
92 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook posting') . '</a><br />';
94 $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook posting') . '</a><br />';
100 function facebook_install() {
101 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
105 function facebook_uninstall() {
106 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
110 function facebook_post_hook(&$a,&$b) {
113 * Post to Facebook stream
116 logger('Facebook post');
118 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
121 $appid = get_config('facebook', 'appid' );
122 $secret = get_config('facebook', 'appsecret' );
124 if($appid && $secret) {
126 $fb_post = get_pconfig(local_user(),'facebook','post');
127 $fb_token = get_pconfig(local_user(),'facebook','access_token');
129 if($fb_post && $fb_token) {
130 require_once('library/facebook.php');
131 require_once('include/bbcode.php');
135 logger('Facebook post2: msg=' . $msg, LOGGER_DATA);
137 // make links readable before we strip the code
139 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
141 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
143 $msg = trim(strip_tags(bbcode($msg)));
145 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
147 require_once('addon/twitter/slinky.php');
149 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
150 $slinky = new Slinky( $posturl );
151 // setup a cascade of shortening services
152 // try to get a short link from these services
153 // in the order ur1.ca, trim, id.gd, tinyurl
154 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
155 $shortlink = $slinky->short();
156 // the new message will be shortened such that "... $shortlink"
157 // will fit into the character limit
158 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
159 $msg .= '... ' . $shortlink;
164 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
166 $postvars = array('access_token' => $fb_token, 'message' => $msg);
168 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
170 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);