3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to enable Facebook Connect
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 define("FACEBOOK_CONNECT_SERVICE", 3);
36 require_once INSTALLDIR . '/lib/facebookutil.php';
37 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectAuth.php';
38 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectLogin.php';
39 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectSettings.php';
40 require_once INSTALLDIR . '/plugins/FBConnect/FBCLoginGroupNav.php';
41 require_once INSTALLDIR . '/plugins/FBConnect/FBCSettingsNav.php';
42 require_once INSTALLDIR . '/plugins/FBConnect/FBC_XDReceiver.php';
45 * Plugin to enable Facebook Connect
49 * @author Zach Copley <zach@status.net>
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51 * @link http://status.net/
54 class FBConnectPlugin extends Plugin
56 function __construct()
58 parent::__construct();
61 // Hook in new actions
62 function onRouterInitialized(&$m) {
63 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
64 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
65 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
66 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
70 function onStartShowHTML($action)
73 if ($this->reqFbScripts($action)) {
75 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
76 // Facebook Connect. These browser cannot use Facebook's
77 // DOM parsing routines unless the mime type of the page is
78 // text/html even though Facebook Connect uses XHTML. This is
79 // A bug in Facebook Connect, and this is a temporary solution
80 // until they fix their JavaScript libs.
81 header('Content-Type: text/html');
83 $action->extraHeaders();
85 $action->startXML('html');
87 $language = $action->getLanguage();
89 $action->elementStart('html',
90 array('xmlns' => 'http://www.w3.org/1999/xhtml',
91 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
92 'xml:lang' => $language,
93 'lang' => $language));
103 // Note: this script needs to appear in the <body>
105 function onStartShowHeader($action)
107 if ($this->reqFbScripts($action)) {
109 $apikey = common_config('facebook', 'apikey');
110 $plugin_path = common_path('plugins/FBConnect');
112 $login_url = common_local_url('FBConnectAuth');
113 $logout_url = common_local_url('logout');
115 // XXX: Facebook says we don't need this FB_RequireFeatures(),
116 // but we actually do, for IE and Safari. Gar.
118 $html = sprintf('<script type="text/javascript">
119 $(document).ready(function () {
123 FB.init("%s", "../xd_receiver.html");
127 function goto_login() {
128 window.location = "%s";
131 function goto_logout() {
132 window.location = "%s";
135 $login_url, $logout_url);
142 // Note: this script needs to appear as close as possible to </body>
144 function onEndShowFooter($action)
146 if ($this->reqFbScripts($action)) {
147 $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
151 function onEndShowStatusNetStyles($action)
154 if ($this->reqFbScripts($action)) {
155 $action->cssLink('plugins/FBConnect/FBConnectPlugin.css');
160 * Does the Action we're plugged into require the FB Scripts? We only
161 * want to output FB namespace, scripts, CSS, etc. on the pages that
164 * @param Action the action in question
166 * @return boolean true
169 function reqFbScripts($action) {
171 // If you're logged in w/FB Connect, you always need the FB stuff
173 $fbuid = $this->loggedIn();
175 if (!empty($fbuid)) {
179 // List of actions that require FB stuff
181 $needy = array('FBConnectLoginAction',
182 'FBConnectauthAction',
183 'FBConnectSettingsAction');
185 if (in_array(get_class($action), $needy)) {
194 * Is the user currently logged in with FB Connect?
196 * @return mixed $fbuid the Facebook ID of the logged in user, or null
201 $user = common_current_user();
205 $flink = Foreign_link::getByUserId($user->id,
206 FACEBOOK_CONNECT_SERVICE);
209 if (!empty($flink)) {
213 $facebook = getFacebook();
214 $fbuid = $facebook->get_loggedin_user();
216 } catch (Exception $e) {
217 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
218 'Problem getting Facebook user: ' .
231 function onStartPrimaryNav($action)
234 $user = common_current_user();
235 $connect = 'FBConnectSettings';
236 if (common_config('xmpp', 'enabled')) {
237 $connect = 'imsettings';
238 } else if (common_config('sms', 'enabled')) {
239 $connect = 'smssettings';
240 } else if (common_config('twitter', 'enabled')) {
241 $connect = 'twittersettings';
246 $fbuid = $this->loggedIn();
248 if (!empty($fbuid)) {
250 /* Default FB silhouette pic for FB users who haven't
251 uploaded a profile pic yet. */
254 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
256 $url = $this->getProfilePicURL($fbuid);
258 $action->elementStart('li', array('id' => 'nav_fb'));
260 $action->element('img', array('id' => 'fbc_profile-pic',
261 'src' => (!empty($url)) ? $url : $silhouetteUrl,
262 'alt' => 'Facebook Connect User',
263 'width' => '16'), '');
265 $iconurl = common_path('plugins/FBConnect/fbfavicon.ico');
266 $action->element('img', array('id' => 'fb_favicon',
269 $action->elementEnd('li');
273 $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
274 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
275 $action->menuItem(common_local_url('profilesettings'),
276 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
277 $action->menuItem(common_local_url($connect),
278 _('Connect'), _('Connect to services'), false, 'nav_connect');
279 if (common_config('invite', 'enabled')) {
280 $action->menuItem(common_local_url('invite'),
282 sprintf(_('Invite friends and colleagues to join you on %s'),
283 common_config('site', 'name')),
284 false, 'nav_invitecontact');
287 // Need to override the Logout link to make it do FB stuff
288 if (!empty($fbuid)) {
290 $logout_url = common_local_url('logout');
291 $title = _('Logout from the site');
294 $html = sprintf('<li id="nav_logout"><a href="#" title="%s" ' .
295 'onclick="FB.Connect.logoutAndRedirect(\'%s\');">%s</a></li>',
296 $title, $logout_url, $text);
301 $action->menuItem(common_local_url('logout'),
302 _('Logout'), _('Logout from the site'), false, 'nav_logout');
306 if (!common_config('site', 'openidonly')) {
307 if (!common_config('site', 'closed')) {
308 $action->menuItem(common_local_url('register'),
309 _('Register'), _('Create an account'), false, 'nav_register');
311 $action->menuItem(common_local_url('login'),
312 _('Login'), _('Login to the site'), false, 'nav_login');
314 $this->menuItem(common_local_url('openidlogin'),
315 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
319 $action->menuItem(common_local_url('doc', array('title' => 'help')),
320 _('Help'), _('Help me!'), false, 'nav_help');
321 if ($user || !common_config('site', 'private')) {
322 $action->menuItem(common_local_url('peoplesearch'),
323 _('Search'), _('Search for people or text'), false, 'nav_search');
326 // We are replacing the primary nav entirely; give other
327 // plugins a chance to handle it here.
329 Event::handle('EndPrimaryNav', array($action));
334 function onStartShowLocalNavBlock($action)
336 $action_name = get_class($action);
338 $login_actions = array('LoginAction', 'RegisterAction',
339 'OpenidloginAction', 'FBConnectLoginAction');
341 if (in_array($action_name, $login_actions)) {
342 $nav = new FBCLoginGroupNav($action);
347 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
348 'TwittersettingsAction', 'FBConnectSettingsAction');
350 if (in_array($action_name, $connect_actions)) {
351 $nav = new FBCSettingsNav($action);
359 function onStartLogout($action)
362 $fbuid = $this->loggedIn();
364 if (!empty($fbuid)) {
366 $facebook = getFacebook();
367 $facebook->expire_session();
368 } catch (Exception $e) {
369 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
370 'Could\'t logout of Facebook: ' .
378 function getProfilePicURL($fbuid)
381 $facebook = getFacebook();
386 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
388 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
390 if (!empty($result)) {
391 $url = $result[0]['pic_square'];
394 } catch (Exception $e) {
395 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
396 "Facebook client failure requesting profile pic!");