3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to add a StatusNet Facebook application
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 define("FACEBOOK_CONNECT_SERVICE", 3);
36 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
39 * Facebook plugin to add a StatusNet Facebook application
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class FacebookPlugin extends Plugin
52 * Add Facebook app actions to the router table
54 * Hook for RouterInitialized event.
56 * @param Net_URL_Mapper &$m path-to-action mapper
58 * @return boolean hook return
61 function onRouterInitialized($m)
66 $m->connect('facebook/app', array('action' => 'facebookhome'));
67 $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
68 $m->connect('facebook/app/settings.php',
69 array('action' => 'facebooksettings'));
70 $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
71 $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
73 // Facebook Connect stuff
75 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
76 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
77 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
78 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
84 * Automatically load the actions and libraries used by the Facebook app
86 * @param Class $cls the class
88 * @return boolean hook return
92 function onAutoload($cls)
95 case 'FacebookAction':
96 case 'FacebookhomeAction':
97 case 'FacebookinviteAction':
98 case 'FacebookremoveAction':
99 case 'FacebooksettingsAction':
100 include_once INSTALLDIR . '/plugins/Facebook/' .
101 strtolower(mb_substr($cls, 0, -6)) . '.php';
103 case 'FBConnectAuthAction':
104 case 'FBConnectLoginAction':
105 case 'FBConnectSettingsAction':
106 case 'FBC_XDReceiverAction':
107 include_once INSTALLDIR . '/plugins/Facebook/' .
108 mb_substr($cls, 0, -6) . '.php';
110 case 'FBCLoginGroupNav':
111 include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
113 case 'FBCSettingsNav':
114 include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
122 * Override normal HTML output to force the content type to
123 * text/html and add in xmlns:fb
125 * @param Action $action the current action
130 function onStartShowHTML($action)
133 if ($this->reqFbScripts($action)) {
135 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
136 // Facebook Connect. These browser cannot use Facebook's
137 // DOM parsing routines unless the mime type of the page is
138 // text/html even though Facebook Connect uses XHTML. This is
139 // A bug in Facebook Connect, and this is a temporary solution
140 // until they fix their JavaScript libs.
142 header('Content-Type: text/html');
144 $action->extraHeaders();
146 $action->startXML('html');
148 $language = $action->getLanguage();
150 $action->elementStart('html',
151 array('xmlns' => 'http://www.w3.org/1999/xhtml',
152 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
153 'xml:lang' => $language,
154 'lang' => $language));
165 * Add in the Facebook Connect JavaScript stuff
167 * Note: this script needs to appear in the <body>
169 * @param Action $action the current action
175 function onEndShowScripts($action)
177 if ($this->reqFbScripts($action)) {
179 $apikey = common_config('facebook', 'apikey');
180 $plugin_path = common_path('plugins/Facebook');
182 $login_url = common_local_url('FBConnectAuth');
183 $logout_url = common_local_url('logout');
185 // XXX: Facebook says we don't need this FB_RequireFeatures(),
186 // but we actually do, for IE and Safari. Gar.
188 $js = '<script type="text/javascript">';
189 $js .= ' $(document).ready(function () {';
190 $js .= ' FB_RequireFeatures(';
191 $js .= ' ["XFBML"], function() {';
192 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
197 $js .= ' function goto_login() {';
198 $js .= ' window.location = "%2$s";';
201 // The below function alters the logout link so that it logs the user out
202 // of Facebook Connect as well as the site. However, for some pages
203 // (FB Connect Settings) we need to output the FB Connect scripts (to
204 // show an existing FB connection even if the user isn't authenticated
205 // with Facebook connect) but NOT alter the logout link. And the only
206 // way to reliably do that is with the FB Connect .js libs. Crazy.
208 $js .= ' FB.ensureInit(function() {';
209 $js .= ' FB.Connect.ifUserConnected(';
210 $js .= ' function() { ';
211 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
212 $js .= ' $(\'#nav_logout a\').click(function() {';
213 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
214 $js .= ' return false;';
217 $js .= ' function() {';
218 $js .= ' return false;';
224 $js = sprintf($js, $apikey, $login_url, $logout_url);
226 // Compress the bugger down a bit
228 $js = str_replace(' ', '', $js);
230 $action->raw(" $js"); // leading two spaces to make it line up
236 * Add in an additional Facebook Connect script that's supposed to
237 * appear as close as possible to </body>
239 * @param Action $action the current action
245 function onEndShowFooter($action)
247 if ($this->reqFbScripts($action)) {
248 $action->script('http://static.ak.connect.facebook.com' .
249 '/js/api_lib/v0.4/FeatureLoader.js.php');
254 * Output Facebook Connect specific CSS link
256 * @param Action $action the current action
262 function onEndShowStatusNetStyles($action)
264 if ($this->reqFbScripts($action)) {
265 $action->cssLink('plugins/Facebook/FBConnect.css');
270 * Does the Action we're plugged into require the FB Scripts? We only
271 * want to output FB namespace, scripts, CSS, etc. on the pages that
274 * @param Action $action the current action
276 * @return boolean true
279 function reqFbScripts($action)
282 // If you're logged in w/FB Connect, you always need the FB stuff
284 $fbuid = $this->loggedIn();
286 if (!empty($fbuid)) {
290 // List of actions that require FB stuff
292 $needy = array('FBConnectLoginAction',
293 'FBConnectauthAction',
294 'FBConnectSettingsAction');
296 if (in_array(get_class($action), $needy)) {
305 * Is the user currently logged in with FB Connect?
307 * @return mixed $fbuid the Facebook ID of the logged in user, or null
312 $user = common_current_user();
316 $flink = Foreign_link::getByUserId($user->id,
317 FACEBOOK_CONNECT_SERVICE);
320 if (!empty($flink)) {
324 $facebook = getFacebook();
325 $fbuid = $facebook->get_loggedin_user();
327 } catch (Exception $e) {
328 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
329 'Problem getting Facebook user: ' .
343 * Add in a Facebook Connect avatar to the primary nav menu
345 * @param Action $action the current action
351 function onStartPrimaryNav($action)
353 $user = common_current_user();
355 $connect = 'FBConnectSettings';
356 if (common_config('xmpp', 'enabled')) {
357 $connect = 'imsettings';
358 } else if (common_config('sms', 'enabled')) {
359 $connect = 'smssettings';
360 } else if (common_config('twitter', 'enabled')) {
361 $connect = 'twittersettings';
366 $fbuid = $this->loggedIn();
368 if (!empty($fbuid)) {
370 /* Default FB silhouette pic for FB users who haven't
371 uploaded a profile pic yet. */
374 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
376 $url = $this->getProfilePicURL($fbuid);
378 $action->elementStart('li', array('id' => 'nav_fb'));
380 $action->element('img', array('id' => 'fbc_profile-pic',
381 'src' => (!empty($url)) ? $url : $silhouetteUrl,
382 'alt' => 'Facebook Connect User',
383 'width' => '16'), '');
385 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
386 $action->element('img', array('id' => 'fb_favicon',
389 $action->elementEnd('li');
398 * Alter the local nav menu to have a Facebook Connect login and
401 * @param Action $action the current action
407 function onStartShowLocalNavBlock($action)
409 $action_name = get_class($action);
411 $login_actions = array('LoginAction', 'RegisterAction',
412 'OpenidloginAction', 'FBConnectLoginAction');
414 if (in_array($action_name, $login_actions)) {
415 $nav = new FBCLoginGroupNav($action);
420 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
421 'TwittersettingsAction', 'FBConnectSettingsAction');
423 if (in_array($action_name, $connect_actions)) {
424 $nav = new FBCSettingsNav($action);
433 * Have the logout process do some Facebook Connect cookie cleanup
435 * @param Action $action the current action
440 function onStartLogout($action)
443 $fbuid = $this->loggedIn();
445 if (!empty($fbuid)) {
447 $facebook = getFacebook();
448 $facebook->expire_session();
449 } catch (Exception $e) {
450 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
451 'Could\'t logout of Facebook: ' .
460 * Get the URL of the user's Facebook avatar
462 * @param int $fbuid the Facebook user ID
464 * @return string $url the url for the user's Facebook avatar
467 function getProfilePicURL($fbuid)
469 $facebook = getFacebook();
474 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
476 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
478 if (!empty($result)) {
479 $url = $result[0]['pic_square'];
482 } catch (Exception $e) {
483 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
484 "Facebook client failure requesting profile pic!");
491 * Add a Facebook queue item for each notice
493 * @param Notice $notice the notice
494 * @param array &$transports the list of transports (queues)
496 * @return boolean hook return
499 function onStartEnqueueNotice($notice, &$transports)
501 array_push($transports, 'facebook');
506 * broadcast the message when not using queuehandler
508 * @param Notice &$notice the notice
509 * @param array $queue destination queue
511 * @return boolean hook return
514 function onUnqueueHandleNotice(&$notice, $queue)
516 if (($queue == 'facebook') && ($this->_isLocal($notice))) {
517 facebookBroadcastNotice($notice);
524 * Determine whether the notice was locally created
526 * @param Notice $notice the notice
528 * @return boolean locality
531 function _isLocal($notice)
533 return ($notice->is_local == Notice::LOCAL_PUBLIC ||
534 $notice->is_local == Notice::LOCAL_NONPUBLIC);
538 * Add Facebook queuehandler to the list of daemons to start
540 * @param array $daemons the list fo daemons to run
542 * @return boolean hook return
546 function onGetValidDaemons($daemons)
548 array_push($daemons, INSTALLDIR .
549 '/plugins/Facebook/facebookqueuehandler.php');