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 2011 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 Bridge 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 FacebookBridgePlugin extends Plugin
50 public $appId; // Facebook application ID
51 public $secret; // Facebook application secret
53 public $facebook = null; // Facebook application instance
54 public $dir = null; // Facebook 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.
66 // Allow the id and key to be passed in
67 // Control panel will override
69 if (isset($this->appId)) {
70 $appId = common_config('facebook', 'appid');
80 if (isset($this->secret)) {
81 $secret = common_config('facebook', 'secret');
83 Config::save('facebook', 'secret', $this->secret);
87 $this->facebook = Facebookclient::getFacebook(
96 * Load related modules when needed
98 * @param string $cls Name of the class to be loaded
100 * @return boolean hook value; true means continue processing, false means stop.
102 function onAutoload($cls)
105 $dir = dirname(__FILE__);
107 //common_debug("class = " . $cls);
111 case 'Facebook': // Facebook PHP SDK
112 include_once $dir . '/extlib/facebook.php';
114 case 'FacebookloginAction':
115 case 'FacebookfinishloginAction':
116 case 'FacebookadminpanelAction':
117 case 'FacebooksettingsAction':
118 case 'FacebookdeauthorizeAction':
119 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
121 case 'Facebookclient':
122 case 'FacebookQueueHandler':
123 include_once $dir . '/lib/' . strtolower($cls) . '.php';
125 case 'Notice_to_item':
126 include_once $dir . '/classes/' . $cls . '.php';
135 * Database schema setup
137 * We maintain a table mapping StatusNet notices to Facebook items
142 * @return boolean hook value; true means continue processing, false means stop.
144 function onCheckSchema()
146 $schema = Schema::get();
147 $schema->ensureTable('notice_to_item', Notice_to_item::schemaDef());
152 * Does this $action need the Facebook JavaScripts?
154 function needsScripts($action)
156 static $needy = array(
157 'FacebookloginAction',
158 'FacebookfinishloginAction',
159 'FacebookadminpanelAction',
160 'FacebooksettingsAction'
163 if (in_array(get_class($action), $needy)) {
171 * Map URLs to actions
173 * @param Net_URL_Mapper $m path-to-action mapper
175 * @return boolean hook value; true means continue processing, false means stop.
177 function onRouterInitialized($m)
179 // Always add the admin panel route
180 $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
182 // Only add these routes if an application has been setup on
183 // Facebook for the plugin to use.
184 if ($this->hasApplication()) {
187 'main/facebooklogin',
188 array('action' => 'facebooklogin')
191 'main/facebookfinishlogin',
192 array('action' => 'facebookfinishlogin')
196 array('action' => 'facebooksettings')
199 'facebook/deauthorize',
200 array('action' => 'facebookdeauthorize')
209 * Add a login tab for Facebook, but only if there's a Facebook
210 * application defined for the plugin to use.
212 * @param Action $action the current action
216 function onEndLoginGroupNav($action)
218 $action_name = $action->trimmed('action');
220 if ($this->hasApplication()) {
223 common_local_url('facebooklogin'),
224 _m('MENU', 'Facebook'),
225 // TRANS: Tooltip for menu item "Facebook".
226 _m('Login or register using Facebook'),
227 'facebooklogin' === $action_name
235 * If the plugin's installed, this should be accessible to admins
237 function onAdminPanelCheck($name, &$isOK)
239 if ($name == 'facebook') {
248 * Add a Facebook tab to the admin panels
250 * @param Widget $nav Admin panel nav
252 * @return boolean hook value
254 function onEndAdminPanelNav($nav)
256 if (AdminPanelAction::canAdmin('facebook')) {
258 $action_name = $nav->action->trimmed('action');
261 common_local_url('facebookadminpanel'),
263 _m('MENU','Facebook'),
264 // TRANS: Tooltip for menu item "Facebook".
265 _m('Facebook integration configuration'),
266 $action_name == 'facebookadminpanel',
267 'nav_facebook_admin_panel'
275 * Add a tab for user-level Facebook settings if the user
276 * has a link to Facebook
278 * @param Action $action the current action
282 function onEndConnectSettingsNav($action)
284 if ($this->hasApplication()) {
285 $action_name = $action->trimmed('action');
287 // CurrentUserDesignAction stores the current user in $cur
288 $user = $action->getCurrentUser();
293 $flink = Foreign_link::getByUserID(
299 if (!empty($flink)) {
302 common_local_url('facebooksettings'),
303 // TRANS: Menu item tab.
304 _m('MENU','Facebook'),
305 // TRANS: Tooltip for menu item "Facebook".
306 _m('Facebook settings'),
307 $action_name === 'facebooksettings'
316 * Is there a Facebook application for the plugin to use?
318 * Checks to see if a Facebook application ID and secret
319 * have been configured and a valid Facebook API client
323 function hasApplication()
325 if (!empty($this->facebook)) {
327 $appId = $this->facebook->getAppId();
328 $secret = $this->facebook->getApiSecret();
330 if (!empty($appId) && !empty($secret)) {
340 * Output a Facebook div for the Facebook JavaSsript SDK to use
342 * @param Action $action the current action
345 function onStartShowHeader($action)
347 // output <div id="fb-root"></div> as close to <body> as possible
348 $action->element('div', array('id' => 'fb-root'));
353 * Load the Facebook JavaScript SDK on pages that need them.
355 * @param Action $action the current action
358 function onEndShowScripts($action)
360 if ($this->needsScripts($action)) {
362 $action->script('https://connect.facebook.net/en_US/all.js');
364 $script = <<<ENDOFSCRIPT
365 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
367 $('#facebook_button').bind('click', function(event) {
369 event.preventDefault();
371 FB.login(function(response) {
372 if (response.session && response.perms) {
373 window.location.href = '%3\$s';
375 // NOP (user cancelled login)
377 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
381 $action->inlineScript(
383 json_encode($this->facebook->getAppId()),
384 json_encode($this->facebook->getSession()),
385 common_local_url('facebookfinishlogin')
392 * Log the user out of Facebook, per the Facebook authentication guide
394 * @param Action action the current action
396 function onEndLogout($action)
398 if ($this->hasApplication()) {
399 $session = $this->facebook->getSession();
405 $fbuid = $this->facebook->getUser();
406 $fbuser = $this->facebook->api('/me');
407 } catch (FacebookApiException $e) {
408 common_log(LOG_ERROR, $e, __FILE__);
412 if (!empty($fbuser)) {
414 $logoutUrl = $this->facebook->getLogoutUrl(
415 array('next' => common_local_url('public'))
421 "Logging user out of Facebook (fbuid = %s)",
426 common_debug("LOGOUT URL = $logoutUrl");
427 common_redirect($logoutUrl, 303);
434 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
435 * and render XFBML tags
437 * @param Action $action the current action
438 * @param array $attrs array of attributes for the HTML tag
442 function onStartHtmlElement($action, $attrs) {
444 if ($this->needsScripts($action)) {
445 $attrs = array_merge(
447 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
455 * Add a Facebook queue item for each notice
457 * @param Notice $notice the notice
458 * @param array &$transports the list of transports (queues)
460 * @return boolean hook return
462 function onStartEnqueueNotice($notice, &$transports)
464 if (self::hasApplication() && $notice->isLocal()) {
465 array_push($transports, 'facebook');
471 * Register Facebook notice queue handler
473 * @param QueueManager $manager
475 * @return boolean hook return
477 function onEndInitializeQueueManager($manager)
479 if (self::hasApplication()) {
480 $manager->connect('facebook', 'FacebookQueueHandler');
486 * Use SSL for Facebook stuff
488 * @param string $action name
489 * @param boolean $ssl outval to force SSL
490 * @return mixed hook return value
492 function onSensitiveAction($action, &$ssl)
495 'facebookadminpanel',
498 'facebookfinishlogin'
501 if (in_array($action, $sensitive)) {
510 * If a notice gets deleted, remove the Notice_to_item mapping and
511 * delete the item on Facebook
513 * @param User $user The user doing the deleting
514 * @param Notice $notice The notice getting deleted
516 * @return boolean hook value
518 function onStartDeleteOwnNotice(User $user, Notice $notice)
520 $client = new Facebookclient($notice);
521 $client->streamRemove();
527 * Notify remote users when their notices get favorited.
529 * @param Profile or User $profile of local user doing the faving
530 * @param Notice $notice being favored
531 * @return hook return value
533 function onEndFavorNotice(Profile $profile, Notice $notice)
535 $client = new Facebookclient($notice);
542 * Notify remote users when their notices get de-favorited.
544 * @param Profile $profile Profile person doing the de-faving
545 * @param Notice $notice Notice being favored
547 * @return hook return value
549 function onEndDisfavorNotice(Profile $profile, Notice $notice)
551 $client = new Facebookclient($notice);
558 * Add version info for this plugin
560 * @param array &$versions plugin version descriptions
562 function onPluginVersion(&$versions)
565 'name' => 'Facebook Bridge',
566 'version' => STATUSNET_VERSION,
567 'author' => 'Craig Andrews, Zach Copley',
568 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
570 _m('A plugin for integrating StatusNet with Facebook.')