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 // CurrentUserDesignAction stores the current user in $cur
281 $user = $action->getCurrentUser();
286 $flink = Foreign_link::getByUserID(
292 if (!empty($flink)) {
295 common_local_url('facebooksettings'),
296 // TRANS: Menu item for "Facebook" in user settings.
297 _m('MENU','Facebook'),
298 // TRANS: Menu title for "Facebook" in user settings.
299 _m('Facebook settings.'),
300 $action_name === 'facebooksettings'
307 * Is there a Facebook application for the plugin to use?
309 * Checks to see if a Facebook application ID and secret
310 * have been configured and a valid Facebook API client
314 function hasApplication()
316 if (!empty($this->facebook)) {
318 $appId = $this->facebook->getAppId();
319 $secret = $this->facebook->getApiSecret();
321 if (!empty($appId) && !empty($secret)) {
330 * Output a Facebook div for the Facebook JavaSsript SDK to use
332 * @param Action $action the current action
335 function onStartShowHeader($action)
337 // output <div id="fb-root"></div> as close to <body> as possible
338 $action->element('div', array('id' => 'fb-root'));
343 * Load the Facebook JavaScript SDK on pages that need them.
345 * @param Action $action the current action
348 function onEndShowScripts($action)
350 if ($this->needsScripts($action)) {
352 $action->script('https://connect.facebook.net/en_US/all.js');
354 $script = <<<ENDOFSCRIPT
355 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
357 $('#facebook_button').bind('click', function(event) {
359 event.preventDefault();
361 FB.login(function(response) {
362 if (response.session && response.perms) {
363 window.location.href = '%3\$s';
365 // NOP (user cancelled login)
367 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
371 $action->inlineScript(
373 json_encode($this->facebook->getAppId()),
374 json_encode($this->facebook->getSession()),
375 common_local_url('facebookfinishlogin')
382 * Log the user out of Facebook, per the Facebook authentication guide
384 * @param Action action the current action
386 function onEndLogout($action)
388 if ($this->hasApplication()) {
389 $session = $this->facebook->getSession();
395 $fbuid = $this->facebook->getUser();
396 $fbuser = $this->facebook->api('/me');
397 } catch (FacebookApiException $e) {
398 common_log(LOG_ERROR, $e, __FILE__);
402 if (!empty($fbuser)) {
404 $logoutUrl = $this->facebook->getLogoutUrl(
405 array('next' => common_local_url('public'))
411 "Logging user out of Facebook (fbuid = %s)",
416 common_debug("LOGOUT URL = $logoutUrl");
417 common_redirect($logoutUrl, 303);
423 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
424 * and render XFBML tags
426 * @param Action $action the current action
427 * @param array $attrs array of attributes for the HTML tag
431 function onStartHtmlElement($action, $attrs) {
433 if ($this->needsScripts($action)) {
434 $attrs = array_merge(
436 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
444 * Add a Facebook queue item for each notice
446 * @param Notice $notice the notice
447 * @param array &$transports the list of transports (queues)
449 * @return boolean hook return
451 function onStartEnqueueNotice($notice, &$transports)
453 if (self::hasApplication() && $notice->isLocal() && $notice->inScope(null)) {
454 array_push($transports, 'facebook');
460 * Register Facebook notice queue handler
462 * @param QueueManager $manager
464 * @return boolean hook return
466 function onEndInitializeQueueManager($manager)
468 if (self::hasApplication()) {
469 $manager->connect('facebook', 'FacebookQueueHandler');
475 * Use SSL for Facebook stuff
477 * @param string $action name
478 * @param boolean $ssl outval to force SSL
479 * @return mixed hook return value
481 function onSensitiveAction($action, &$ssl)
484 'facebookadminpanel',
487 'facebookfinishlogin'
490 if (in_array($action, $sensitive)) {
499 * If a notice gets deleted, remove the Notice_to_item mapping and
500 * delete the item on Facebook
502 * @param User $user The user doing the deleting
503 * @param Notice $notice The notice getting deleted
505 * @return boolean hook value
507 function onStartDeleteOwnNotice(User $user, Notice $notice)
509 $client = new Facebookclient($notice);
510 $client->streamRemove();
516 * Notify remote users when their notices get favorited.
518 * @param Profile or User $profile of local user doing the faving
519 * @param Notice $notice being favored
520 * @return hook return value
522 function onEndFavorNotice(Profile $profile, Notice $notice)
524 $client = new Facebookclient($notice, $profile);
531 * Notify remote users when their notices get de-favorited.
533 * @param Profile $profile Profile person doing the de-faving
534 * @param Notice $notice Notice being favored
536 * @return hook return value
538 function onEndDisfavorNotice(Profile $profile, Notice $notice)
540 $client = new Facebookclient($notice, $profile);
547 * Add version info for this plugin
549 * @param array &$versions plugin version descriptions
551 function onPluginVersion(&$versions)
554 'name' => 'Facebook Bridge',
555 'version' => STATUSNET_VERSION,
556 'author' => 'Craig Andrews, Zach Copley',
557 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
559 // TRANS: Plugin description.
560 _m('A plugin for integrating StatusNet with Facebook.')