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 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 = null; // Facebook application ID
51 public $secret = null; // Facebook application secret
52 public $facebook = null; // Facebook application instance
53 public $dir = null; // Facebook plugin dir
56 * Initializer for this plugin
58 * Gets an instance of the Facebook API client object
60 * @return boolean hook value; true means continue processing, false means stop.
64 $this->facebook = Facebookclient::getFacebook(
73 * Load related modules when needed
75 * @param string $cls Name of the class to be loaded
77 * @return boolean hook value; true means continue processing, false means stop.
79 function onAutoload($cls)
82 $dir = dirname(__FILE__);
84 //common_debug("class = " . $cls);
88 case 'Facebook': // Facebook PHP SDK
89 include_once $dir . '/extlib/facebook.php';
91 case 'FacebookloginAction':
92 case 'FacebookfinishloginAction':
93 case 'FacebookadminpanelAction':
94 case 'FacebooksettingsAction':
95 case 'FacebookdeauthorizeAction':
96 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
98 case 'Facebookclient':
99 case 'FacebookQueueHandler':
100 include_once $dir . '/lib/' . strtolower($cls) . '.php';
102 case 'Notice_to_item':
103 include_once $dir . '/classes/' . $cls . '.php';
112 * Database schema setup
114 * We maintain a table mapping StatusNet notices to Facebook items
119 * @return boolean hook value; true means continue processing, false means stop.
121 function onCheckSchema()
123 $schema = Schema::get();
124 $schema->ensureTable('notice_to_item', Notice_to_item::schemaDef());
129 * Does this $action need the Facebook JavaScripts?
131 function needsScripts($action)
133 static $needy = array(
134 'FacebookloginAction',
135 'FacebookfinishloginAction',
136 'FacebookadminpanelAction',
137 'FacebooksettingsAction'
140 if (in_array(get_class($action), $needy)) {
148 * Map URLs to actions
150 * @param Net_URL_Mapper $m path-to-action mapper
152 * @return boolean hook value; true means continue processing, false means stop.
154 function onRouterInitialized($m)
156 // Always add the admin panel route
157 $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
159 // Only add these routes if an application has been setup on
160 // Facebook for the plugin to use.
161 if ($this->hasApplication()) {
164 'main/facebooklogin',
165 array('action' => 'facebooklogin')
168 'main/facebookfinishlogin',
169 array('action' => 'facebookfinishlogin')
173 array('action' => 'facebooksettings')
176 'facebook/deauthorize',
177 array('action' => 'facebookdeauthorize')
186 * Add a login tab for Facebook, but only if there's a Facebook
187 * application defined for the plugin to use.
189 * @param Action &action the current action
193 function onEndLoginGroupNav(&$action)
195 $action_name = $action->trimmed('action');
197 if ($this->hasApplication()) {
200 common_local_url('facebooklogin'),
201 _m('MENU', 'Facebook'),
202 // TRANS: Tooltip for menu item "Facebook".
203 _m('Login or register using Facebook'),
204 'facebooklogin' === $action_name
212 * If the plugin's installed, this should be accessible to admins
214 function onAdminPanelCheck($name, &$isOK)
216 if ($name == 'facebook') {
225 * Add a Facebook tab to the admin panels
227 * @param Widget $nav Admin panel nav
229 * @return boolean hook value
231 function onEndAdminPanelNav($nav)
233 if (AdminPanelAction::canAdmin('facebook')) {
235 $action_name = $nav->action->trimmed('action');
238 common_local_url('facebookadminpanel'),
240 _m('MENU','Facebook'),
241 // TRANS: Tooltip for menu item "Facebook".
242 _m('Facebook integration configuration'),
243 $action_name == 'facebookadminpanel',
244 'nav_facebook_admin_panel'
252 * Add a tab for user-level Facebook settings if the user
253 * has a link to Facebook
255 * @param Action &action the current action
259 function onEndConnectSettingsNav(&$action)
261 if ($this->hasApplication()) {
262 $action_name = $action->trimmed('action');
264 // CurrentUserDesignAction stores the current user in $cur
265 $user = $action->getCurrentUser();
270 $flink = Foreign_link::getByUserID(
276 if (!empty($flink)) {
279 common_local_url('facebooksettings'),
280 // TRANS: Menu item tab.
281 _m('MENU','Facebook'),
282 // TRANS: Tooltip for menu item "Facebook".
283 _m('Facebook settings'),
284 $action_name === 'facebooksettings'
293 * Is there a Facebook application for the plugin to use?
295 * Checks to see if a Facebook application ID and secret
296 * have been configured and a valid Facebook API client
300 function hasApplication()
302 if (!empty($this->facebook)) {
304 $appId = $this->facebook->getAppId();
305 $secret = $this->facebook->getApiSecret();
307 if (!empty($appId) && !empty($secret)) {
317 * Output a Facebook div for the Facebook JavaSsript SDK to use
319 * @param Action $action the current action
322 function onStartShowHeader($action)
324 // output <div id="fb-root"></div> as close to <body> as possible
325 $action->element('div', array('id' => 'fb-root'));
330 * Load the Facebook JavaScript SDK on pages that need them.
332 * @param Action $action the current action
335 function onEndShowScripts($action)
337 if ($this->needsScripts($action)) {
339 $action->script('https://connect.facebook.net/en_US/all.js');
341 $script = <<<ENDOFSCRIPT
342 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
344 $('#facebook_button').bind('click', function(event) {
346 event.preventDefault();
348 FB.login(function(response) {
349 if (response.session && response.perms) {
350 window.location.href = '%3\$s';
352 // NOP (user cancelled login)
354 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
358 $action->inlineScript(
360 json_encode($this->facebook->getAppId()),
361 json_encode($this->facebook->getSession()),
362 common_local_url('facebookfinishlogin')
369 * Log the user out of Facebook, per the Facebook authentication guide
371 * @param Action action the current action
373 function onEndLogout($action)
375 if ($this->hasApplication()) {
376 $session = $this->facebook->getSession();
382 $fbuid = $this->facebook->getUser();
383 $fbuser = $this->facebook->api('/me');
384 } catch (FacebookApiException $e) {
385 common_log(LOG_ERROR, $e, __FILE__);
389 if (!empty($fbuser)) {
391 $logoutUrl = $this->facebook->getLogoutUrl(
392 array('next' => common_local_url('public'))
398 "Logging user out of Facebook (fbuid = %s)",
403 common_debug("LOGOUT URL = $logoutUrl");
404 common_redirect($logoutUrl, 303);
411 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
412 * and render XFBML tags
414 * @param Action $action the current action
415 * @param array $attrs array of attributes for the HTML tag
419 function onStartHtmlElement($action, $attrs) {
421 if ($this->needsScripts($action)) {
422 $attrs = array_merge(
424 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
432 * Add a Facebook queue item for each notice
434 * @param Notice $notice the notice
435 * @param array &$transports the list of transports (queues)
437 * @return boolean hook return
439 function onStartEnqueueNotice($notice, &$transports)
441 if (self::hasApplication() && $notice->isLocal()) {
442 array_push($transports, 'facebook');
448 * Register Facebook notice queue handler
450 * @param QueueManager $manager
452 * @return boolean hook return
454 function onEndInitializeQueueManager($manager)
456 if (self::hasApplication()) {
457 $manager->connect('facebook', 'FacebookQueueHandler');
463 * Use SSL for Facebook stuff
465 * @param string $action name
466 * @param boolean $ssl outval to force SSL
467 * @return mixed hook return value
469 function onSensitiveAction($action, &$ssl)
472 'facebookadminpanel',
475 'facebookfinishlogin'
478 if (in_array($action, $sensitive)) {
487 * If a notice gets deleted, remove the Notice_to_item mapping and
488 * delete the item on Facebook
490 * @param User $user The user doing the deleting
491 * @param Notice $notice The notice getting deleted
493 * @return boolean hook value
495 function onStartDeleteOwnNotice(User $user, Notice $notice)
497 $client = new Facebookclient($notice);
498 $client->streamRemove();
504 * Notify remote users when their notices get favorited.
506 * @param Profile or User $profile of local user doing the faving
507 * @param Notice $notice being favored
508 * @return hook return value
510 function onEndFavorNotice(Profile $profile, Notice $notice)
512 $client = new Facebookclient($notice);
519 * Notify remote users when their notices get de-favorited.
521 * @param Profile $profile Profile person doing the de-faving
522 * @param Notice $notice Notice being favored
524 * @return hook return value
526 function onEndDisfavorNotice(Profile $profile, Notice $notice)
528 $client = new Facebookclient($notice);
535 * Add version info for this plugin
537 * @param array &$versions plugin version descriptions
539 function onPluginVersion(&$versions)
542 'name' => 'Facebook Bridge',
543 'version' => STATUSNET_VERSION,
544 'author' => 'Craig Andrews, Zach Copley',
545 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
547 _m('A plugin for integrating StatusNet with Facebook.')