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)
104 $dir = dirname(__FILE__);
106 //common_debug("class = " . $cls);
110 case 'Facebook': // Facebook PHP SDK
111 include_once $dir . '/extlib/facebook.php';
113 case 'FacebookloginAction':
114 case 'FacebookfinishloginAction':
115 case 'FacebookadminpanelAction':
116 case 'FacebooksettingsAction':
117 case 'FacebookdeauthorizeAction':
118 include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
120 case 'Facebookclient':
121 case 'FacebookQueueHandler':
122 include_once $dir . '/lib/' . strtolower($cls) . '.php';
124 case 'Notice_to_item':
125 include_once $dir . '/classes/' . $cls . '.php';
133 * Database schema setup
135 * We maintain a table mapping StatusNet notices to Facebook items
140 * @return boolean hook value; true means continue processing, false means stop.
142 function onCheckSchema()
144 $schema = Schema::get();
145 $schema->ensureTable('notice_to_item', Notice_to_item::schemaDef());
150 * Does this $action need the Facebook JavaScripts?
152 function needsScripts($action)
154 static $needy = array(
155 'FacebookloginAction',
156 'FacebookfinishloginAction',
157 'FacebookadminpanelAction',
158 'FacebooksettingsAction'
161 if (in_array(get_class($action), $needy)) {
169 * Map URLs to actions
171 * @param Net_URL_Mapper $m path-to-action mapper
173 * @return boolean hook value; true means continue processing, false means stop.
175 function onRouterInitialized($m)
177 // Always add the admin panel route
178 $m->connect('panel/facebook', array('action' => 'facebookadminpanel'));
181 'main/facebooklogin',
182 array('action' => 'facebooklogin')
185 'main/facebookfinishlogin',
186 array('action' => 'facebookfinishlogin')
190 array('action' => 'facebooksettings')
193 'facebook/deauthorize',
194 array('action' => 'facebookdeauthorize')
201 * Add a login tab for Facebook, but only if there's a Facebook
202 * application defined for the plugin to use.
204 * @param Action $action the current action
208 function onEndLoginGroupNav($action)
210 $action_name = $action->trimmed('action');
212 if ($this->hasApplication()) {
215 // TRANS: Menu item for "Facebook" login.
216 common_local_url('facebooklogin'),
217 _m('MENU', 'Facebook'),
218 // TRANS: Menu title for "Facebook" login.
219 _m('Login or register using Facebook.'),
220 'facebooklogin' === $action_name
228 * If the plugin's installed, this should be accessible to admins
230 function onAdminPanelCheck($name, &$isOK)
232 if ($name == 'facebook') {
241 * Add a Facebook tab to the admin panels
243 * @param Widget $nav Admin panel nav
245 * @return boolean hook value
247 function onEndAdminPanelNav($nav)
249 if (AdminPanelAction::canAdmin('facebook')) {
251 $action_name = $nav->action->trimmed('action');
254 common_local_url('facebookadminpanel'),
255 // TRANS: Menu item for "Facebook" in administration panel.
256 _m('MENU','Facebook'),
257 // TRANS: Menu title for "Facebook" in administration panel.
258 _m('Facebook integration configuration.'),
259 $action_name == 'facebookadminpanel',
260 'nav_facebook_admin_panel'
268 * Add a tab for user-level Facebook settings if the user
269 * has a link to Facebook
271 * @param Action $action the current action
275 function onEndConnectSettingsNav($action)
277 if ($this->hasApplication()) {
278 $action_name = $action->trimmed('action');
280 $user = common_current_user();
285 $flink = Foreign_link::getByUserID(
291 if (!empty($flink)) {
294 common_local_url('facebooksettings'),
295 // TRANS: Menu item for "Facebook" in user settings.
296 _m('MENU','Facebook'),
297 // TRANS: Menu title for "Facebook" in user settings.
298 _m('Facebook settings.'),
299 $action_name === 'facebooksettings'
306 * Is there a Facebook application for the plugin to use?
308 * Checks to see if a Facebook application ID and secret
309 * have been configured and a valid Facebook API client
313 function hasApplication()
315 if (!empty($this->facebook)) {
317 $appId = $this->facebook->getAppId();
318 $secret = $this->facebook->getApiSecret();
320 if (!empty($appId) && !empty($secret)) {
329 * Output a Facebook div for the Facebook JavaSsript SDK to use
331 * @param Action $action the current action
334 function onStartShowHeader($action)
336 // output <div id="fb-root"></div> as close to <body> as possible
337 $action->element('div', array('id' => 'fb-root'));
342 * Load the Facebook JavaScript SDK on pages that need them.
344 * @param Action $action the current action
347 function onEndShowScripts($action)
349 if ($this->needsScripts($action)) {
351 $action->script('https://connect.facebook.net/en_US/all.js');
353 $script = <<<ENDOFSCRIPT
354 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
356 $('#facebook_button').bind('click', function(event) {
358 event.preventDefault();
360 FB.login(function(response) {
361 if (response.session && response.perms) {
362 window.location.href = '%3\$s';
364 // NOP (user cancelled login)
366 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
370 $action->inlineScript(
372 json_encode($this->facebook->getAppId()),
373 json_encode($this->facebook->getSession()),
374 common_local_url('facebookfinishlogin')
381 * Log the user out of Facebook, per the Facebook authentication guide
383 * @param Action action the current action
385 function onEndLogout($action)
387 if ($this->hasApplication()) {
388 $session = $this->facebook->getSession();
394 $fbuid = $this->facebook->getUser();
395 $fbuser = $this->facebook->api('/me');
396 } catch (FacebookApiException $e) {
397 common_log(LOG_ERROR, $e, __FILE__);
401 if (!empty($fbuser)) {
403 $logoutUrl = $this->facebook->getLogoutUrl(
404 array('next' => common_local_url('public'))
410 "Logging user out of Facebook (fbuid = %s)",
415 common_debug("LOGOUT URL = $logoutUrl");
416 common_redirect($logoutUrl, 303);
422 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
423 * and render XFBML tags
425 * @param Action $action the current action
426 * @param array $attrs array of attributes for the HTML tag
430 function onStartHtmlElement($action, $attrs) {
432 if ($this->needsScripts($action)) {
433 $attrs = array_merge(
435 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
443 * Add a Facebook queue item for each notice
445 * @param Notice $notice the notice
446 * @param array &$transports the list of transports (queues)
448 * @return boolean hook return
450 function onStartEnqueueNotice($notice, &$transports)
452 if (self::hasApplication() && $notice->isLocal() && $notice->inScope(null)) {
453 array_push($transports, 'facebook');
459 * Register Facebook notice queue handler
461 * @param QueueManager $manager
463 * @return boolean hook return
465 function onEndInitializeQueueManager($manager)
467 if (self::hasApplication()) {
468 $manager->connect('facebook', 'FacebookQueueHandler');
474 * Use SSL for Facebook stuff
476 * @param string $action name
477 * @param boolean $ssl outval to force SSL
478 * @return mixed hook return value
480 function onSensitiveAction($action, &$ssl)
483 'facebookadminpanel',
486 'facebookfinishlogin'
489 if (in_array($action, $sensitive)) {
498 * If a notice gets deleted, remove the Notice_to_item mapping and
499 * delete the item on Facebook
501 * @param User $user The user doing the deleting
502 * @param Notice $notice The notice getting deleted
504 * @return boolean hook value
506 function onStartDeleteOwnNotice(User $user, Notice $notice)
508 $client = new Facebookclient($notice);
509 $client->streamRemove();
515 * Notify remote users when their notices get favorited.
517 * @param Profile or User $profile of local user doing the faving
518 * @param Notice $notice being favored
519 * @return hook return value
521 function onEndFavorNotice(Profile $profile, Notice $notice)
523 $client = new Facebookclient($notice, $profile);
530 * Notify remote users when their notices get de-favorited.
532 * @param Profile $profile Profile person doing the de-faving
533 * @param Notice $notice Notice being favored
535 * @return hook return value
537 function onEndDisfavorNotice(Profile $profile, Notice $notice)
539 $client = new Facebookclient($notice, $profile);
546 * Add version info for this plugin
548 * @param array &$versions plugin version descriptions
550 function onPluginVersion(&$versions)
553 'name' => 'Facebook Bridge',
554 'version' => STATUSNET_VERSION,
555 'author' => 'Craig Andrews, Zach Copley',
556 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
558 // TRANS: Plugin description.
559 _m('A plugin for integrating StatusNet with Facebook.')