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 /* 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'] : '');
40 if($auth_code && $uid) {
42 $appid = get_config('facebook','appid');
43 $appsecret = get_config('facebook', 'appsecret');
45 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
46 . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
47 . urlencode($a->get_baseurl() . '/facebook/' . $nick)
48 . '&code=' . $auth_code);
49 if(strpos($x,'access_token=') !== false) {
50 $token = str_replace('access_token=', '', $x);
51 if(strpos($token,'&') !== false)
52 $token = substr('$token,0,strpos($token,'&'));
53 set_pconfig($uid,'facebook','access_token',$token);
56 // todo: is this a browser session or a server session? where do we go?
61 function facebook_content(&$a) {
62 $o = "facebook module loaded";
66 function facebook_install() {
67 register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
71 function facebook_uninstall() {
72 unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
76 function facebook_post_hook(&$a,&$b) {
79 * Post to Facebook stream
82 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
84 $appid = get_config('facebook', 'appid' );
85 $secret = get_config('facebook', 'appsecret' );
87 if($appid && $secret) {
89 $fb_post = get_pconfig(local_user(),'facebook','post');
90 $fb_token = get_pconfig(local_user(),'facebook','access_token');
92 if($fb_post && $fb_token) {
93 require_once('library/facebook.php');
94 require_once('include/bbcode.php');
97 // make links readable before we strip the code
99 $msg = preg_replace('\[url\=(.?*)\](.?*)\[\/url\]/is','$2 ($1)',$msg);
101 $msg = preg_replace('\[img\](.?*)\[\/img\]/is', t('Image: ') . '$1',$msg);
103 $msg = trim(strip_tags(bbcode($b['body'])));
104 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
106 require_once('addon/twitter/slinky.php');
108 $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
109 $slinky = new Slinky( $posturl );
110 // setup a cascade of shortening services
111 // try to get a short link from these services
112 // in the order ur1.ca, trim, id.gd, tinyurl
113 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
114 $shortlink = $slinky->short();
115 // the new message will be shortened such that "... $shortlink"
116 // will fit into the character limit
117 $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
118 $msg .= '... ' . $shortlink;
126 $facebook = new Facebook(array(
132 $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($b['body']), 'cb' => ''));
134 catch (FacebookApiException $e) {
135 notice( t('Facebook status update failed.') . EOL);