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-2010 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 canvas application
40 * and allow registration and authentication via Facebook Connect
44 * @author Zach Copley <zach@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
49 class FacebookPlugin extends Plugin
52 const VERSION = STATUSNET_VERSION;
55 * Initializer for the plugin.
60 // Allow the key and secret to be passed in
61 // Control panel will override
63 if (isset($this->apikey)) {
64 $key = common_config('facebook', 'apikey');
66 Config::save('facebook', 'apikey', $this->apikey);
70 if (isset($this->secret)) {
71 $secret = common_config('facebook', 'secret');
83 * Check to see if there is an API key and secret defined
84 * for Facebook integration.
86 * @return boolean result
89 static function hasKeys()
91 $apiKey = common_config('facebook', 'apikey');
92 $apiSecret = common_config('facebook', 'secret');
94 if (!empty($apiKey) && !empty($apiSecret)) {
102 * Add Facebook app actions to the router table
104 * Hook for RouterInitialized event.
106 * @param Net_URL_Mapper &$m path-to-action mapper
108 * @return boolean hook return
111 function onStartInitializeRouter($m)
113 $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
115 if (self::hasKeys()) {
117 // Facebook App stuff
119 $m->connect('facebook/app', array('action' => 'facebookhome'));
120 $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
121 $m->connect('facebook/app/settings.php',
122 array('action' => 'facebooksettings'));
123 $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
124 $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
126 // Facebook Connect stuff
128 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
129 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
130 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
131 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
138 * Automatically load the actions and libraries used by the Facebook app
140 * @param Class $cls the class
142 * @return boolean hook return
146 function onAutoload($cls)
149 case 'FacebookAction':
150 case 'FacebookhomeAction':
151 case 'FacebookinviteAction':
152 case 'FacebookremoveAction':
153 case 'FacebooksettingsAction':
154 case 'FacebookadminpanelAction':
155 include_once INSTALLDIR . '/plugins/Facebook/' .
156 strtolower(mb_substr($cls, 0, -6)) . '.php';
158 case 'FBConnectAuthAction':
159 case 'FBConnectLoginAction':
160 case 'FBConnectSettingsAction':
161 case 'FBC_XDReceiverAction':
162 include_once INSTALLDIR . '/plugins/Facebook/' .
163 mb_substr($cls, 0, -6) . '.php';
165 case 'FBCLoginGroupNav':
166 include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
168 case 'FBCSettingsNav':
169 include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
171 case 'FacebookQueueHandler':
172 include_once INSTALLDIR . '/plugins/Facebook/facebookqueuehandler.php';
180 * Add a Facebook tab to the admin panels
182 * @param Widget $nav Admin panel nav
184 * @return boolean hook value
187 function onEndAdminPanelNav($nav)
189 if (AdminPanelAction::canAdmin('facebook')) {
191 $action_name = $nav->action->trimmed('action');
194 common_local_url('facebookadminpanel'),
196 _m('Facebook integration configuration'),
197 $action_name == 'facebookadminpanel',
198 'nav_facebook_admin_panel'
206 * Override normal HTML output to force the content type to
207 * text/html and add in xmlns:fb
209 * @param Action $action the current action
214 function onStartShowHTML($action)
217 if ($this->reqFbScripts($action)) {
219 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
220 // Facebook Connect. These browser cannot use Facebook's
221 // DOM parsing routines unless the mime type of the page is
222 // text/html even though Facebook Connect uses XHTML. This is
223 // A bug in Facebook Connect, and this is a temporary solution
224 // until they fix their JavaScript libs.
226 header('Content-Type: text/html');
228 $action->extraHeaders();
230 $action->startXML('html');
232 $language = $action->getLanguage();
234 $action->elementStart('html',
235 array('xmlns' => 'http://www.w3.org/1999/xhtml',
236 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
237 'xml:lang' => $language,
238 'lang' => $language));
249 * Add in the Facebook Connect JavaScript stuff
251 * Note: this script needs to appear in the <body>
253 * @param Action $action the current action
259 function onEndShowScripts($action)
261 if ($this->reqFbScripts($action)) {
263 $apikey = common_config('facebook', 'apikey');
264 $plugin_path = 'plugins/Facebook';
266 $login_url = common_local_url('FBConnectAuth');
267 $logout_url = common_local_url('logout');
269 // XXX: Facebook says we don't need this FB_RequireFeatures(),
270 // but we actually do, for IE and Safari. Gar.
272 $js .= ' $(document).ready(function () {';
273 $js .= ' FB_RequireFeatures(';
274 $js .= ' ["XFBML"], function() {';
275 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
280 $js .= ' function goto_login() {';
281 $js .= ' window.location = "%2$s";';
284 // The below function alters the logout link so that it logs the user out
285 // of Facebook Connect as well as the site. However, for some pages
286 // (FB Connect Settings) we need to output the FB Connect scripts (to
287 // show an existing FB connection even if the user isn't authenticated
288 // with Facebook connect) but NOT alter the logout link. And the only
289 // way to reliably do that is with the FB Connect .js libs. Crazy.
291 $js .= ' FB.ensureInit(function() {';
292 $js .= ' FB.Connect.ifUserConnected(';
293 $js .= ' function() { ';
294 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
295 $js .= ' $(\'#nav_logout a\').click(function() {';
296 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
297 $js .= ' return false;';
300 $js .= ' function() {';
301 $js .= ' return false;';
306 $js = sprintf($js, $apikey, $login_url, $logout_url);
308 // Compress the bugger down a bit
310 $js = str_replace(' ', '', $js);
312 $action->inlineScript($js);
318 * Add in an additional Facebook Connect script that's supposed to
319 * appear as close as possible to </body>
321 * @param Action $action the current action
327 function onEndShowFooter($action)
329 if ($this->reqFbScripts($action)) {
330 $action->script('http://static.ak.connect.facebook.com' .
331 '/js/api_lib/v0.4/FeatureLoader.js.php');
336 * Output Facebook Connect specific CSS link
338 * @param Action $action the current action
344 function onEndShowStatusNetStyles($action)
346 if ($this->reqFbScripts($action)) {
347 $action->cssLink('plugins/Facebook/FBConnect.css');
352 * Does the Action we're plugged into require the FB Scripts? We only
353 * want to output FB namespace, scripts, CSS, etc. on the pages that
356 * @param Action $action the current action
358 * @return boolean true
361 function reqFbScripts($action)
363 if (!self::hasKeys()) {
367 // If you're logged in w/FB Connect, you always need the FB stuff
369 $fbuid = $this->loggedIn();
371 if (!empty($fbuid)) {
375 // List of actions that require FB stuff
377 $needy = array('FBConnectLoginAction',
378 'FBConnectauthAction',
379 'FBConnectSettingsAction');
381 if (in_array(get_class($action), $needy)) {
390 * Is the user currently logged in with FB Connect?
392 * @return mixed $fbuid the Facebook ID of the logged in user, or null
397 $user = common_current_user();
401 $flink = Foreign_link::getByUserId($user->id,
402 FACEBOOK_CONNECT_SERVICE);
405 if (!empty($flink)) {
409 $facebook = getFacebook();
410 $fbuid = $facebook->get_loggedin_user();
412 } catch (Exception $e) {
413 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
414 'Problem getting Facebook user: ' .
428 * Add in a Facebook Connect avatar to the primary nav menu
430 * @param Action $action the current action
436 function onStartPrimaryNav($action)
438 if (self::hasKeys()) {
439 $user = common_current_user();
442 $fbuid = $this->loggedIn();
444 if (!empty($fbuid)) {
446 /* Default FB silhouette pic for FB users who haven't
447 uploaded a profile pic yet. */
450 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
452 $url = $this->getProfilePicURL($fbuid);
454 $action->elementStart('li', array('id' => 'nav_fb'));
456 $action->element('img', array('id' => 'fbc_profile-pic',
457 'src' => (!empty($url)) ? $url : $silhouetteUrl,
458 'alt' => 'Facebook Connect User',
459 'width' => '16'), '');
461 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
462 $action->element('img', array('id' => 'fb_favicon',
465 $action->elementEnd('li');
474 * Add a login tab for Facebook Connect
476 * @param Action &action the current action
481 function onEndLoginGroupNav(&$action)
483 if (self::hasKeys()) {
485 $action_name = $action->trimmed('action');
487 $action->menuItem(common_local_url('FBConnectLogin'),
489 _m('Login or register using Facebook'),
490 'FBConnectLogin' === $action_name);
496 * Add a tab for managing Facebook Connect settings
498 * @param Action &action the current action
503 function onEndConnectSettingsNav(&$action)
505 if (self::hasKeys()) {
507 $action_name = $action->trimmed('action');
509 $action->menuItem(common_local_url('FBConnectSettings'),
511 _m('Facebook Connect Settings'),
512 $action_name === 'FBConnectSettings');
518 * Have the logout process do some Facebook Connect cookie cleanup
520 * @param Action $action the current action
525 function onStartLogout($action)
527 if (self::hasKeys()) {
530 $fbuid = $this->loggedIn();
532 if (!empty($fbuid)) {
534 $facebook = getFacebook();
535 $facebook->expire_session();
536 } catch (Exception $e) {
537 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
538 'Could\'t logout of Facebook: ' .
547 * Get the URL of the user's Facebook avatar
549 * @param int $fbuid the Facebook user ID
551 * @return string $url the url for the user's Facebook avatar
554 function getProfilePicURL($fbuid)
556 $facebook = getFacebook();
561 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
563 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
565 if (!empty($result)) {
566 $url = $result[0]['pic_square'];
569 } catch (Exception $e) {
570 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
571 "Facebook client failure requesting profile pic!");
578 * Add a Facebook queue item for each notice
580 * @param Notice $notice the notice
581 * @param array &$transports the list of transports (queues)
583 * @return boolean hook return
586 function onStartEnqueueNotice($notice, &$transports)
588 if (self::hasKeys()) {
589 array_push($transports, 'facebook');
595 * Register Facebook notice queue handler
597 * @param QueueManager $manager
599 * @return boolean hook return
601 function onEndInitializeQueueManager($manager)
603 if (self::hasKeys()) {
604 $manager->connect('facebook', 'FacebookQueueHandler');
609 function onPluginVersion(&$versions)
612 'name' => 'Facebook',
613 'version' => self::VERSION,
614 'author' => 'Zach Copley',
615 'homepage' => 'http://status.net/wiki/Plugin:Facebook',
616 'rawdescription' => _m(
617 'The Facebook plugin allows you to integrate ' .
618 'your StatusNet instance with ' .
619 '<a href="http://facebook.com/">Facebook</a> ' .
620 'and Facebook Connect.'