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);
35 define('FACEBOOKPLUGIN_VERSION', '0.9');
37 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
40 * Facebook plugin to add a StatusNet Facebook application
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
53 * Add Facebook app actions to the router table
55 * Hook for RouterInitialized event.
57 * @param Net_URL_Mapper &$m path-to-action mapper
59 * @return boolean hook return
62 function onStartInitializeRouter($m)
67 $m->connect('facebook/app', array('action' => 'facebookhome'));
68 $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
69 $m->connect('facebook/app/settings.php',
70 array('action' => 'facebooksettings'));
71 $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
72 $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
74 // Facebook Connect stuff
76 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
77 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
78 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
79 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
85 * Automatically load the actions and libraries used by the Facebook app
87 * @param Class $cls the class
89 * @return boolean hook return
93 function onAutoload($cls)
96 case 'FacebookAction':
97 case 'FacebookhomeAction':
98 case 'FacebookinviteAction':
99 case 'FacebookremoveAction':
100 case 'FacebooksettingsAction':
101 include_once INSTALLDIR . '/plugins/Facebook/' .
102 strtolower(mb_substr($cls, 0, -6)) . '.php';
104 case 'FBConnectAuthAction':
105 case 'FBConnectLoginAction':
106 case 'FBConnectSettingsAction':
107 case 'FBC_XDReceiverAction':
108 include_once INSTALLDIR . '/plugins/Facebook/' .
109 mb_substr($cls, 0, -6) . '.php';
111 case 'FBCLoginGroupNav':
112 include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
114 case 'FBCSettingsNav':
115 include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
117 case 'FacebookQueueHandler':
118 include_once INSTALLDIR . '/plugins/Facebook/facebookqueuehandler.php';
126 * Override normal HTML output to force the content type to
127 * text/html and add in xmlns:fb
129 * @param Action $action the current action
134 function onStartShowHTML($action)
137 if ($this->reqFbScripts($action)) {
139 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
140 // Facebook Connect. These browser cannot use Facebook's
141 // DOM parsing routines unless the mime type of the page is
142 // text/html even though Facebook Connect uses XHTML. This is
143 // A bug in Facebook Connect, and this is a temporary solution
144 // until they fix their JavaScript libs.
146 header('Content-Type: text/html');
148 $action->extraHeaders();
150 $action->startXML('html');
152 $language = $action->getLanguage();
154 $action->elementStart('html',
155 array('xmlns' => 'http://www.w3.org/1999/xhtml',
156 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
157 'xml:lang' => $language,
158 'lang' => $language));
169 * Add in the Facebook Connect JavaScript stuff
171 * Note: this script needs to appear in the <body>
173 * @param Action $action the current action
179 function onEndShowScripts($action)
181 if ($this->reqFbScripts($action)) {
183 $apikey = common_config('facebook', 'apikey');
184 $plugin_path = common_path('plugins/Facebook');
186 $login_url = common_local_url('FBConnectAuth');
187 $logout_url = common_local_url('logout');
189 // XXX: Facebook says we don't need this FB_RequireFeatures(),
190 // but we actually do, for IE and Safari. Gar.
192 $js .= ' $(document).ready(function () {';
193 $js .= ' FB_RequireFeatures(';
194 $js .= ' ["XFBML"], function() {';
195 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
200 $js .= ' function goto_login() {';
201 $js .= ' window.location = "%2$s";';
204 // The below function alters the logout link so that it logs the user out
205 // of Facebook Connect as well as the site. However, for some pages
206 // (FB Connect Settings) we need to output the FB Connect scripts (to
207 // show an existing FB connection even if the user isn't authenticated
208 // with Facebook connect) but NOT alter the logout link. And the only
209 // way to reliably do that is with the FB Connect .js libs. Crazy.
211 $js .= ' FB.ensureInit(function() {';
212 $js .= ' FB.Connect.ifUserConnected(';
213 $js .= ' function() { ';
214 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
215 $js .= ' $(\'#nav_logout a\').click(function() {';
216 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
217 $js .= ' return false;';
220 $js .= ' function() {';
221 $js .= ' return false;';
226 $js = sprintf($js, $apikey, $login_url, $logout_url);
228 // Compress the bugger down a bit
230 $js = str_replace(' ', '', $js);
232 $action->inlineScript($js);
238 * Add in an additional Facebook Connect script that's supposed to
239 * appear as close as possible to </body>
241 * @param Action $action the current action
247 function onEndShowFooter($action)
249 if ($this->reqFbScripts($action)) {
250 $action->script('http://static.ak.connect.facebook.com' .
251 '/js/api_lib/v0.4/FeatureLoader.js.php');
256 * Output Facebook Connect specific CSS link
258 * @param Action $action the current action
264 function onEndShowStatusNetStyles($action)
266 if ($this->reqFbScripts($action)) {
267 $action->cssLink('plugins/Facebook/FBConnect.css');
272 * Does the Action we're plugged into require the FB Scripts? We only
273 * want to output FB namespace, scripts, CSS, etc. on the pages that
276 * @param Action $action the current action
278 * @return boolean true
281 function reqFbScripts($action)
284 // If you're logged in w/FB Connect, you always need the FB stuff
286 $fbuid = $this->loggedIn();
288 if (!empty($fbuid)) {
292 // List of actions that require FB stuff
294 $needy = array('FBConnectLoginAction',
295 'FBConnectauthAction',
296 'FBConnectSettingsAction');
298 if (in_array(get_class($action), $needy)) {
307 * Is the user currently logged in with FB Connect?
309 * @return mixed $fbuid the Facebook ID of the logged in user, or null
314 $user = common_current_user();
318 $flink = Foreign_link::getByUserId($user->id,
319 FACEBOOK_CONNECT_SERVICE);
322 if (!empty($flink)) {
326 $facebook = getFacebook();
327 $fbuid = $facebook->get_loggedin_user();
329 } catch (Exception $e) {
330 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
331 'Problem getting Facebook user: ' .
345 * Add in a Facebook Connect avatar to the primary nav menu
347 * @param Action $action the current action
353 function onStartPrimaryNav($action)
355 $user = common_current_user();
357 $connect = 'FBConnectSettings';
358 if (common_config('xmpp', 'enabled')) {
359 $connect = 'imsettings';
360 } else if (common_config('sms', 'enabled')) {
361 $connect = 'smssettings';
362 } else if (common_config('twitter', 'enabled')) {
363 $connect = 'twittersettings';
368 $fbuid = $this->loggedIn();
370 if (!empty($fbuid)) {
372 /* Default FB silhouette pic for FB users who haven't
373 uploaded a profile pic yet. */
376 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
378 $url = $this->getProfilePicURL($fbuid);
380 $action->elementStart('li', array('id' => 'nav_fb'));
382 $action->element('img', array('id' => 'fbc_profile-pic',
383 'src' => (!empty($url)) ? $url : $silhouetteUrl,
384 'alt' => 'Facebook Connect User',
385 'width' => '16'), '');
387 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
388 $action->element('img', array('id' => 'fb_favicon',
391 $action->elementEnd('li');
400 * Add a login tab for Facebook Connect
402 * @param Action &action the current action
407 function onEndLoginGroupNav(&$action)
410 $action_name = $action->trimmed('action');
412 $action->menuItem(common_local_url('FBConnectLogin'),
414 _m('Login or register using Facebook'),
415 'FBConnectLogin' === $action_name);
421 * Add a tab for managing Facebook Connect settings
423 * @param Action &action the current action
428 function onEndConnectSettingsNav(&$action)
430 $action_name = $action->trimmed('action');
432 $action->menuItem(common_local_url('FBConnectSettings'),
434 _m('Facebook Connect Settings'),
435 $action_name === 'FBConnectSettings');
441 * Have the logout process do some Facebook Connect cookie cleanup
443 * @param Action $action the current action
448 function onStartLogout($action)
451 $fbuid = $this->loggedIn();
453 if (!empty($fbuid)) {
455 $facebook = getFacebook();
456 $facebook->expire_session();
457 } catch (Exception $e) {
458 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
459 'Could\'t logout of Facebook: ' .
468 * Get the URL of the user's Facebook avatar
470 * @param int $fbuid the Facebook user ID
472 * @return string $url the url for the user's Facebook avatar
475 function getProfilePicURL($fbuid)
477 $facebook = getFacebook();
482 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
484 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
486 if (!empty($result)) {
487 $url = $result[0]['pic_square'];
490 } catch (Exception $e) {
491 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
492 "Facebook client failure requesting profile pic!");
499 * Add a Facebook queue item for each notice
501 * @param Notice $notice the notice
502 * @param array &$transports the list of transports (queues)
504 * @return boolean hook return
507 function onStartEnqueueNotice($notice, &$transports)
509 array_push($transports, 'facebook');
514 * Register Facebook notice queue handler
516 * @param QueueManager $manager
518 * @return boolean hook return
520 function onEndInitializeQueueManager($manager)
522 $manager->connect('facebook', 'FacebookQueueHandler');
526 function onPluginVersion(&$versions)
528 $versions[] = array('name' => 'Facebook',
529 'version' => FACEBOOKPLUGIN_VERSION,
530 'author' => 'Zach Copley',
531 'homepage' => 'http://status.net/wiki/Plugin:Facebook',
533 _m('The Facebook plugin allows you to integrate ' .
534 'your StatusNet instance with ' .
535 '<a href="http://facebook.com/">Facebook</a> ' .
536 'and Facebook Connect.'));