3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A plugin for integrating Facebook with StatusNet. Includes single-sign-on
7 * and publishing notices to Facebook using Facebook's Graph API.
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * @author Zach Copley <zach@status.net>
27 * @copyright 2010 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET')) {
36 define("FACEBOOK_SERVICE", 2);
39 * Main class for Facebook plugin
43 * @author Zach Copley <zach@status.net>
44 * @copyright 2010 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46 * @link http://status.net/
48 class FacebookSSOPlugin extends Plugin
50 public $appId = null; // Facebook application ID
51 public $apikey = null; // Facebook API key (for deprecated "Old REST API")
52 public $secret = null; // Facebook application secret
53 public $facebook = null; // Facebook application instance
54 public $dir = null; // Facebook SSO plugin dir
57 * Initializer for this plugin
59 * Gets an instance of the Facebook API client object
61 * @return boolean hook value; true means continue processing, false means stop.
65 $this->facebook = Facebookclient::getFacebook(
75 * Load related modules when needed
77 * @param string $cls Name of the class to be loaded
79 * @return boolean hook value; true means continue processing, false means stop.
81 function onAutoload($cls)
84 $dir = dirname(__FILE__);
86 //common_debug("class = " . $cls);
90 case 'Facebook': // Facebook PHP SDK
91 include_once $dir . '/extlib/facebook.php';
93 case 'FacebookloginAction':
94 case 'FacebookfinishloginAction':
95 case 'FacebookadminpanelAction':
96 case 'FacebooksettingsAction':
97 case 'FacebookdeauthorizeAction':
98 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
100 case 'Facebookclient':
101 case 'FacebookQueueHandler':
102 include_once $dir . '/lib/' . strtolower($cls) . '.php';
111 * Does this $action need the Facebook JavaScripts?
113 function needsScripts($action)
115 static $needy = array(
116 'FacebookloginAction',
117 'FacebookfinishloginAction',
118 'FacebookadminpanelAction',
119 'FacebooksettingsAction'
122 if (in_array(get_class($action), $needy)) {
130 * Map URLs to actions
132 * @param Net_URL_Mapper $m path-to-action mapper
134 * @return boolean hook value; true means continue processing, false means stop.
136 function onRouterInitialized($m)
138 // Always add the admin panel route
139 $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
141 // Only add these routes if an application has been setup on
142 // Facebook for the plugin to use.
143 if ($this->hasApplication()) {
146 'main/facebooklogin',
147 array('action' => 'facebooklogin')
150 'main/facebookfinishlogin',
151 array('action' => 'facebookfinishlogin')
155 array('action' => 'facebooksettings')
158 'facebook/deauthorize',
159 array('action' => 'facebookdeauthorize')
168 * Add a login tab for Facebook, but only if there's a Facebook
169 * application defined for the plugin to use.
171 * @param Action &action the current action
175 function onEndLoginGroupNav(&$action)
177 $action_name = $action->trimmed('action');
179 if ($this->hasApplication()) {
182 common_local_url('facebooklogin'),
183 _m('MENU', 'Facebook'),
184 // TRANS: Tooltip for menu item "Facebook".
185 _m('Login or register using Facebook'),
186 'facebooklogin' === $action_name
194 * Add a Facebook tab to the admin panels
196 * @param Widget $nav Admin panel nav
198 * @return boolean hook value
200 function onEndAdminPanelNav($nav)
202 if (AdminPanelAction::canAdmin('facebook')) {
204 $action_name = $nav->action->trimmed('action');
207 common_local_url('facebookadminpanel'),
209 _m('MENU','Facebook'),
210 // TRANS: Tooltip for menu item "Facebook".
211 _m('Facebook integration configuration'),
212 $action_name == 'facebookadminpanel',
213 'nav_facebook_admin_panel'
221 * Add a tab for user-level Facebook settings
223 * @param Action &action the current action
227 function onEndConnectSettingsNav(&$action)
229 if ($this->hasApplication()) {
230 $action_name = $action->trimmed('action');
233 common_local_url('facebooksettings'),
234 // TRANS: Menu item tab.
235 _m('MENU','Facebook'),
236 // TRANS: Tooltip for menu item "Facebook".
237 _m('Facebook settings'),
238 $action_name === 'facebooksettings'
246 * Is there a Facebook application for the plugin to use?
248 * Checks to see if a Facebook application ID and secret
249 * have been configured and a valid Facebook API client
253 function hasApplication()
255 if (!empty($this->facebook)) {
257 $appId = $this->facebook->getAppId();
258 $secret = $this->facebook->getApiSecret();
260 if (!empty($appId) && !empty($secret)) {
270 * Output a Facebook div for the Facebook JavaSsript SDK to use
272 * @param Action $action the current action
275 function onStartShowHeader($action)
277 // output <div id="fb-root"></div> as close to <body> as possible
278 $action->element('div', array('id' => 'fb-root'));
283 * Load the Facebook JavaScript SDK on pages that need them.
285 * @param Action $action the current action
288 function onEndShowScripts($action)
290 if ($this->needsScripts($action)) {
292 $action->script('https://connect.facebook.net/en_US/all.js');
294 $script = <<<ENDOFSCRIPT
295 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
297 $('#facebook_button').bind('click', function(event) {
299 event.preventDefault();
301 FB.login(function(response) {
302 if (response.session && response.perms) {
303 window.location.href = '%3\$s';
305 // NOP (user cancelled login)
307 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
311 $action->inlineScript(
313 json_encode($this->facebook->getAppId()),
314 json_encode($this->facebook->getSession()),
315 common_local_url('facebookfinishlogin')
322 * Log the user out of Facebook, per the Facebook authentication guide
324 * @param Action action the current action
326 function onEndLogout($action)
328 if ($this->hasApplication()) {
329 $session = $this->facebook->getSession();
335 $fbuid = $this->facebook->getUser();
336 $fbuser = $this->facebook->api('/me');
337 } catch (FacebookApiException $e) {
338 common_log(LOG_ERROR, $e, __FILE__);
342 if (!empty($fbuser)) {
344 $logoutUrl = $this->facebook->getLogoutUrl(
345 array('next' => common_local_url('public'))
351 "Logging user out of Facebook (fbuid = %s)",
356 common_debug("LOGOUT URL = $logoutUrl");
357 common_redirect($logoutUrl, 303);
364 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
365 * and render XFBML tags
367 * @param Action $action the current action
368 * @param array $attrs array of attributes for the HTML tag
372 function onStartHtmlElement($action, $attrs) {
374 if ($this->needsScripts($action)) {
375 $attrs = array_merge(
377 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
385 * Add a Facebook queue item for each notice
387 * @param Notice $notice the notice
388 * @param array &$transports the list of transports (queues)
390 * @return boolean hook return
392 function onStartEnqueueNotice($notice, &$transports)
394 if (self::hasApplication() && $notice->isLocal()) {
395 array_push($transports, 'facebook');
401 * Register Facebook notice queue handler
403 * @param QueueManager $manager
405 * @return boolean hook return
407 function onEndInitializeQueueManager($manager)
409 if (self::hasApplication()) {
410 $manager->connect('facebook', 'FacebookQueueHandler');
416 * Use SSL for Facebook stuff
418 * @param string $action name
419 * @param boolean $ssl outval to force SSL
420 * @return mixed hook return value
422 function onSensitiveAction($action, &$ssl)
425 'facebookadminpanel',
428 'facebookfinishlogin'
431 if (in_array($action, $sensitive)) {
440 * Add version info for this plugin
442 * @param array &$versions plugin version descriptions
444 function onPluginVersion(&$versions)
447 'name' => 'Facebook Single-Sign-On',
448 'version' => STATUSNET_VERSION,
449 'author' => 'Craig Andrews, Zach Copley',
450 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO',
452 _m('A plugin for integrating StatusNet with Facebook.')