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 * Add Facebook app actions to the router table
85 * Hook for RouterInitialized event.
87 * @param Net_URL_Mapper &$m path-to-action mapper
89 * @return boolean hook return
92 function onStartInitializeRouter($m)
97 $m->connect('facebook/app', array('action' => 'facebookhome'));
98 $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
99 $m->connect('facebook/app/settings.php',
100 array('action' => 'facebooksettings'));
101 $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
102 $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
103 $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
105 // Facebook Connect stuff
107 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
108 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
109 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
110 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
116 * Automatically load the actions and libraries used by the Facebook app
118 * @param Class $cls the class
120 * @return boolean hook return
124 function onAutoload($cls)
127 case 'FacebookAction':
128 case 'FacebookhomeAction':
129 case 'FacebookinviteAction':
130 case 'FacebookremoveAction':
131 case 'FacebooksettingsAction':
132 case 'FacebookadminpanelAction':
133 include_once INSTALLDIR . '/plugins/Facebook/' .
134 strtolower(mb_substr($cls, 0, -6)) . '.php';
136 case 'FBConnectAuthAction':
137 case 'FBConnectLoginAction':
138 case 'FBConnectSettingsAction':
139 case 'FBC_XDReceiverAction':
140 include_once INSTALLDIR . '/plugins/Facebook/' .
141 mb_substr($cls, 0, -6) . '.php';
143 case 'FBCLoginGroupNav':
144 include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
146 case 'FBCSettingsNav':
147 include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
149 case 'FacebookQueueHandler':
150 include_once INSTALLDIR . '/plugins/Facebook/facebookqueuehandler.php';
158 * Add a Facebook tab to the admin panels
160 * @param Widget $nav Admin panel nav
162 * @return boolean hook value
165 function onEndAdminPanelNav($nav)
167 if (AdminPanelAction::canAdmin('facebook')) {
169 $action_name = $nav->action->trimmed('action');
172 common_local_url('facebookadminpanel'),
174 _m('Facebook integration configuration'),
175 $action_name == 'facebookadminpanel',
176 'nav_facebook_admin_panel'
184 * Override normal HTML output to force the content type to
185 * text/html and add in xmlns:fb
187 * @param Action $action the current action
192 function onStartShowHTML($action)
195 if ($this->reqFbScripts($action)) {
197 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
198 // Facebook Connect. These browser cannot use Facebook's
199 // DOM parsing routines unless the mime type of the page is
200 // text/html even though Facebook Connect uses XHTML. This is
201 // A bug in Facebook Connect, and this is a temporary solution
202 // until they fix their JavaScript libs.
204 header('Content-Type: text/html');
206 $action->extraHeaders();
208 $action->startXML('html');
210 $language = $action->getLanguage();
212 $action->elementStart('html',
213 array('xmlns' => 'http://www.w3.org/1999/xhtml',
214 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
215 'xml:lang' => $language,
216 'lang' => $language));
227 * Add in the Facebook Connect JavaScript stuff
229 * Note: this script needs to appear in the <body>
231 * @param Action $action the current action
237 function onEndShowScripts($action)
239 if ($this->reqFbScripts($action)) {
241 $apikey = common_config('facebook', 'apikey');
242 $plugin_path = common_path('plugins/Facebook');
244 $login_url = common_local_url('FBConnectAuth');
245 $logout_url = common_local_url('logout');
247 // XXX: Facebook says we don't need this FB_RequireFeatures(),
248 // but we actually do, for IE and Safari. Gar.
250 $js .= ' $(document).ready(function () {';
251 $js .= ' FB_RequireFeatures(';
252 $js .= ' ["XFBML"], function() {';
253 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
258 $js .= ' function goto_login() {';
259 $js .= ' window.location = "%2$s";';
262 // The below function alters the logout link so that it logs the user out
263 // of Facebook Connect as well as the site. However, for some pages
264 // (FB Connect Settings) we need to output the FB Connect scripts (to
265 // show an existing FB connection even if the user isn't authenticated
266 // with Facebook connect) but NOT alter the logout link. And the only
267 // way to reliably do that is with the FB Connect .js libs. Crazy.
269 $js .= ' FB.ensureInit(function() {';
270 $js .= ' FB.Connect.ifUserConnected(';
271 $js .= ' function() { ';
272 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
273 $js .= ' $(\'#nav_logout a\').click(function() {';
274 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
275 $js .= ' return false;';
278 $js .= ' function() {';
279 $js .= ' return false;';
284 $js = sprintf($js, $apikey, $login_url, $logout_url);
286 // Compress the bugger down a bit
288 $js = str_replace(' ', '', $js);
290 $action->inlineScript($js);
296 * Add in an additional Facebook Connect script that's supposed to
297 * appear as close as possible to </body>
299 * @param Action $action the current action
305 function onEndShowFooter($action)
307 if ($this->reqFbScripts($action)) {
308 $action->script('http://static.ak.connect.facebook.com' .
309 '/js/api_lib/v0.4/FeatureLoader.js.php');
314 * Output Facebook Connect specific CSS link
316 * @param Action $action the current action
322 function onEndShowStatusNetStyles($action)
324 if ($this->reqFbScripts($action)) {
325 $action->cssLink('plugins/Facebook/FBConnect.css');
330 * Does the Action we're plugged into require the FB Scripts? We only
331 * want to output FB namespace, scripts, CSS, etc. on the pages that
334 * @param Action $action the current action
336 * @return boolean true
339 function reqFbScripts($action)
342 // If you're logged in w/FB Connect, you always need the FB stuff
344 $fbuid = $this->loggedIn();
346 if (!empty($fbuid)) {
350 // List of actions that require FB stuff
352 $needy = array('FBConnectLoginAction',
353 'FBConnectauthAction',
354 'FBConnectSettingsAction');
356 if (in_array(get_class($action), $needy)) {
365 * Is the user currently logged in with FB Connect?
367 * @return mixed $fbuid the Facebook ID of the logged in user, or null
372 $user = common_current_user();
376 $flink = Foreign_link::getByUserId($user->id,
377 FACEBOOK_CONNECT_SERVICE);
380 if (!empty($flink)) {
384 $facebook = getFacebook();
385 $fbuid = $facebook->get_loggedin_user();
387 } catch (Exception $e) {
388 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
389 'Problem getting Facebook user: ' .
403 * Add in a Facebook Connect avatar to the primary nav menu
405 * @param Action $action the current action
411 function onStartPrimaryNav($action)
413 $user = common_current_user();
415 $connect = 'FBConnectSettings';
416 if (common_config('xmpp', 'enabled')) {
417 $connect = 'imsettings';
418 } else if (common_config('sms', 'enabled')) {
419 $connect = 'smssettings';
424 $fbuid = $this->loggedIn();
426 if (!empty($fbuid)) {
428 /* Default FB silhouette pic for FB users who haven't
429 uploaded a profile pic yet. */
432 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
434 $url = $this->getProfilePicURL($fbuid);
436 $action->elementStart('li', array('id' => 'nav_fb'));
438 $action->element('img', array('id' => 'fbc_profile-pic',
439 'src' => (!empty($url)) ? $url : $silhouetteUrl,
440 'alt' => 'Facebook Connect User',
441 'width' => '16'), '');
443 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
444 $action->element('img', array('id' => 'fb_favicon',
447 $action->elementEnd('li');
456 * Add a login tab for Facebook Connect
458 * @param Action &action the current action
463 function onEndLoginGroupNav(&$action)
466 $action_name = $action->trimmed('action');
468 $action->menuItem(common_local_url('FBConnectLogin'),
470 _m('Login or register using Facebook'),
471 'FBConnectLogin' === $action_name);
477 * Add a tab for managing Facebook Connect settings
479 * @param Action &action the current action
484 function onEndConnectSettingsNav(&$action)
486 $action_name = $action->trimmed('action');
488 $action->menuItem(common_local_url('FBConnectSettings'),
490 _m('Facebook Connect Settings'),
491 $action_name === 'FBConnectSettings');
497 * Have the logout process do some Facebook Connect cookie cleanup
499 * @param Action $action the current action
504 function onStartLogout($action)
507 $fbuid = $this->loggedIn();
509 if (!empty($fbuid)) {
511 $facebook = getFacebook();
512 $facebook->expire_session();
513 } catch (Exception $e) {
514 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
515 'Could\'t logout of Facebook: ' .
524 * Get the URL of the user's Facebook avatar
526 * @param int $fbuid the Facebook user ID
528 * @return string $url the url for the user's Facebook avatar
531 function getProfilePicURL($fbuid)
533 $facebook = getFacebook();
538 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
540 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
542 if (!empty($result)) {
543 $url = $result[0]['pic_square'];
546 } catch (Exception $e) {
547 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
548 "Facebook client failure requesting profile pic!");
555 * Add a Facebook queue item for each notice
557 * @param Notice $notice the notice
558 * @param array &$transports the list of transports (queues)
560 * @return boolean hook return
563 function onStartEnqueueNotice($notice, &$transports)
565 array_push($transports, 'facebook');
570 * Register Facebook notice queue handler
572 * @param QueueManager $manager
574 * @return boolean hook return
576 function onEndInitializeQueueManager($manager)
578 $manager->connect('facebook', 'FacebookQueueHandler');
582 function onPluginVersion(&$versions)
585 'name' => 'Facebook',
586 'version' => self::VERSION,
587 'author' => 'Zach Copley',
588 'homepage' => 'http://status.net/wiki/Plugin:Facebook',
589 'rawdescription' => _m(
590 'The Facebook plugin allows you to integrate ' .
591 'your StatusNet instance with ' .
592 '<a href="http://facebook.com/">Facebook</a> ' .
593 'and Facebook Connect.'