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('panel/facebook', array('action' => 'facebookadminpanel'));
183 'main/facebooklogin',
184 array('action' => 'facebooklogin')
187 'main/facebookfinishlogin',
188 array('action' => 'facebookfinishlogin')
192 array('action' => 'facebooksettings')
195 'facebook/deauthorize',
196 array('action' => 'facebookdeauthorize')
203 * Add a login tab for Facebook, but only if there's a Facebook
204 * application defined for the plugin to use.
206 * @param Action $action the current action
210 function onEndLoginGroupNav($action)
212 $action_name = $action->trimmed('action');
214 if ($this->hasApplication()) {
217 common_local_url('facebooklogin'),
218 _m('MENU', 'Facebook'),
219 // TRANS: Tooltip for menu item "Facebook".
220 _m('Login or register using Facebook'),
221 'facebooklogin' === $action_name
229 * If the plugin's installed, this should be accessible to admins
231 function onAdminPanelCheck($name, &$isOK)
233 if ($name == 'facebook') {
242 * Add a Facebook tab to the admin panels
244 * @param Widget $nav Admin panel nav
246 * @return boolean hook value
248 function onEndAdminPanelNav($nav)
250 if (AdminPanelAction::canAdmin('facebook')) {
252 $action_name = $nav->action->trimmed('action');
255 common_local_url('facebookadminpanel'),
257 _m('MENU','Facebook'),
258 // TRANS: Tooltip for menu item "Facebook".
259 _m('Facebook integration configuration'),
260 $action_name == 'facebookadminpanel',
261 'nav_facebook_admin_panel'
269 * Add a tab for user-level Facebook settings if the user
270 * has a link to Facebook
272 * @param Action $action the current action
276 function onEndConnectSettingsNav($action)
278 if ($this->hasApplication()) {
279 $action_name = $action->trimmed('action');
281 // CurrentUserDesignAction stores the current user in $cur
282 $user = $action->getCurrentUser();
287 $flink = Foreign_link::getByUserID(
293 if (!empty($flink)) {
296 common_local_url('facebooksettings'),
297 // TRANS: Menu item tab.
298 _m('MENU','Facebook'),
299 // TRANS: Tooltip for menu item "Facebook".
300 _m('Facebook settings'),
301 $action_name === 'facebooksettings'
310 * Is there a Facebook application for the plugin to use?
312 * Checks to see if a Facebook application ID and secret
313 * have been configured and a valid Facebook API client
317 function hasApplication()
319 if (!empty($this->facebook)) {
321 $appId = $this->facebook->getAppId();
322 $secret = $this->facebook->getApiSecret();
324 if (!empty($appId) && !empty($secret)) {
334 * Output a Facebook div for the Facebook JavaSsript SDK to use
336 * @param Action $action the current action
339 function onStartShowHeader($action)
341 // output <div id="fb-root"></div> as close to <body> as possible
342 $action->element('div', array('id' => 'fb-root'));
347 * Load the Facebook JavaScript SDK on pages that need them.
349 * @param Action $action the current action
352 function onEndShowScripts($action)
354 if ($this->needsScripts($action)) {
356 $action->script('https://connect.facebook.net/en_US/all.js');
358 $script = <<<ENDOFSCRIPT
359 FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
361 $('#facebook_button').bind('click', function(event) {
363 event.preventDefault();
365 FB.login(function(response) {
366 if (response.session && response.perms) {
367 window.location.href = '%3\$s';
369 // NOP (user cancelled login)
371 }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
375 $action->inlineScript(
377 json_encode($this->facebook->getAppId()),
378 json_encode($this->facebook->getSession()),
379 common_local_url('facebookfinishlogin')
386 * Log the user out of Facebook, per the Facebook authentication guide
388 * @param Action action the current action
390 function onEndLogout($action)
392 if ($this->hasApplication()) {
393 $session = $this->facebook->getSession();
399 $fbuid = $this->facebook->getUser();
400 $fbuser = $this->facebook->api('/me');
401 } catch (FacebookApiException $e) {
402 common_log(LOG_ERROR, $e, __FILE__);
406 if (!empty($fbuser)) {
408 $logoutUrl = $this->facebook->getLogoutUrl(
409 array('next' => common_local_url('public'))
415 "Logging user out of Facebook (fbuid = %s)",
420 common_debug("LOGOUT URL = $logoutUrl");
421 common_redirect($logoutUrl, 303);
428 * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse
429 * and render XFBML tags
431 * @param Action $action the current action
432 * @param array $attrs array of attributes for the HTML tag
436 function onStartHtmlElement($action, $attrs) {
438 if ($this->needsScripts($action)) {
439 $attrs = array_merge(
441 array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')
449 * Add a Facebook queue item for each notice
451 * @param Notice $notice the notice
452 * @param array &$transports the list of transports (queues)
454 * @return boolean hook return
456 function onStartEnqueueNotice($notice, &$transports)
458 if (self::hasApplication() && $notice->isLocal()) {
459 array_push($transports, 'facebook');
465 * Register Facebook notice queue handler
467 * @param QueueManager $manager
469 * @return boolean hook return
471 function onEndInitializeQueueManager($manager)
473 if (self::hasApplication()) {
474 $manager->connect('facebook', 'FacebookQueueHandler');
480 * Use SSL for Facebook stuff
482 * @param string $action name
483 * @param boolean $ssl outval to force SSL
484 * @return mixed hook return value
486 function onSensitiveAction($action, &$ssl)
489 'facebookadminpanel',
492 'facebookfinishlogin'
495 if (in_array($action, $sensitive)) {
504 * If a notice gets deleted, remove the Notice_to_item mapping and
505 * delete the item on Facebook
507 * @param User $user The user doing the deleting
508 * @param Notice $notice The notice getting deleted
510 * @return boolean hook value
512 function onStartDeleteOwnNotice(User $user, Notice $notice)
514 $client = new Facebookclient($notice);
515 $client->streamRemove();
521 * Notify remote users when their notices get favorited.
523 * @param Profile or User $profile of local user doing the faving
524 * @param Notice $notice being favored
525 * @return hook return value
527 function onEndFavorNotice(Profile $profile, Notice $notice)
529 $client = new Facebookclient($notice);
536 * Notify remote users when their notices get de-favorited.
538 * @param Profile $profile Profile person doing the de-faving
539 * @param Notice $notice Notice being favored
541 * @return hook return value
543 function onEndDisfavorNotice(Profile $profile, Notice $notice)
545 $client = new Facebookclient($notice);
552 * Add version info for this plugin
554 * @param array &$versions plugin version descriptions
556 function onPluginVersion(&$versions)
559 'name' => 'Facebook Bridge',
560 'version' => STATUSNET_VERSION,
561 'author' => 'Craig Andrews, Zach Copley',
562 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
564 _m('A plugin for integrating StatusNet with Facebook.')