3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!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@controlyourself.ca>
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51 * @link http://laconi.ca/
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',
86 '-//W3C//DTD XHTML 1.0 Strict//EN',
87 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
89 $language = $action->getLanguage();
91 $action->elementStart('html',
92 array('xmlns' => 'http://www.w3.org/1999/xhtml',
93 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
94 'xml:lang' => $language,
95 'lang' => $language));
105 // Note: this script needs to appear in the <body>
107 function onStartShowHeader($action)
109 if ($this->reqFbScripts($action)) {
111 $apikey = common_config('facebook', 'apikey');
112 $plugin_path = common_path('plugins/FBConnect');
114 $login_url = common_local_url('FBConnectAuth');
115 $logout_url = common_local_url('logout');
117 // XXX: Facebook says we don't need this FB_RequireFeatures(),
118 // but we actually do, for IE and Safari. Gar.
120 $html = sprintf('<script type="text/javascript">
121 window.onload = function () {
125 FB.init("%s", "../xd_receiver.html",
126 {"doNotUseCachedConnectState":true });
131 function goto_login() {
132 window.location = "%s";
135 function goto_logout() {
136 window.location = "%s";
139 $login_url, $logout_url);
146 // Note: this script needs to appear as close as possible to </body>
148 function onEndShowFooter($action)
150 if ($this->reqFbScripts($action)) {
152 $action->element('script',
153 array('type' => 'text/javascript',
154 'src' => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
159 function onEndShowLaconicaStyles($action)
162 if ($this->reqFbScripts($action)) {
164 $action->element('link', array('rel' => 'stylesheet',
165 'type' => 'text/css',
166 'href' => common_path('plugins/FBConnect/FBConnectPlugin.css')));
171 * Does the Action we're plugged into require the FB Scripts? We only
172 * want to output FB namespace, scripts, CSS, etc. on the pages that
175 * @param Action the action in question
177 * @return boolean true
180 function reqFbScripts($action) {
182 // If you're logged in w/FB Connect, you always need the FB stuff
184 $fbuid = $this->loggedIn();
186 if (!empty($fbuid)) {
190 // List of actions that require FB stuff
192 $needy = array('FBConnectLoginAction',
193 'FBConnectauthAction',
194 'FBConnectSettingsAction');
196 if (in_array(get_class($action), $needy)) {
205 * Is the user currently logged in with FB Connect?
207 * @return mixed $fbuid the Facebook ID of the logged in user, or null
212 $user = common_current_user();
216 $flink = Foreign_link::getByUserId($user->id,
217 FACEBOOK_CONNECT_SERVICE);
220 if (!empty($flink)) {
224 $facebook = getFacebook();
225 $fbuid = $facebook->api_client->users_getLoggedInUser();
227 } catch (Exception $e) {
228 common_log(LOG_WARNING,
229 'Problem getting Facebook user: ' .
242 function onStartPrimaryNav($action)
245 $user = common_current_user();
249 $fbuid = $this->loggedIn();
251 if (!empty($fbuid)) {
253 /* Default FB silhouette pic for FB users who haven't
254 uploaded a profile pic yet. */
257 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
259 $url = $this->getProfilePicURL($fbuid);
261 $action->elementStart('li', array('id' => 'nav_fb'));
263 $action->element('img', array('id' => 'fbc_profile-pic',
264 'src' => (!empty($url)) ? $url : $silhouetteUrl,
265 'alt' => 'Facebook Connect User',
266 'width' => '16'), '');
268 $iconurl = common_path('plugins/FBConnect/fbfavicon.ico');
269 $action->element('img', array('id' => 'fb_favicon',
272 $action->elementEnd('li');
276 $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
277 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
278 $action->menuItem(common_local_url('profilesettings'),
279 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
280 if (common_config('xmpp', 'enabled')) {
281 $action->menuItem(common_local_url('imsettings'),
282 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
284 $action->menuItem(common_local_url('smssettings'),
285 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
287 if (common_config('invite', 'enabled')) {
288 $action->menuItem(common_local_url('invite'),
290 sprintf(_('Invite friends and colleagues to join you on %s'),
291 common_config('site', 'name')),
292 false, 'nav_invitecontact');
295 // Need to override the Logout link to make it do FB stuff
296 if (!empty($fbuid)) {
298 $logout_url = common_local_url('logout');
299 $title = _('Logout from the site');
302 $html = sprintf('<li id="nav_logout"><a href="#" title="%s" ' .
303 'onclick="FB.Connect.logoutAndRedirect(\'%s\');">%s</a></li>',
304 $title, $logout_url, $text);
309 $action->menuItem(common_local_url('logout'),
310 _('Logout'), _('Logout from the site'), false, 'nav_logout');
314 if (!common_config('site', 'closed')) {
315 $action->menuItem(common_local_url('register'),
316 _('Register'), _('Create an account'), false, 'nav_register');
318 $action->menuItem(common_local_url('login'),
319 _('Login'), _('Login to the site'), false, 'nav_login');
322 $action->menuItem(common_local_url('doc', array('title' => 'help')),
323 _('Help'), _('Help me!'), false, 'nav_help');
324 $action->menuItem(common_local_url('peoplesearch'),
325 _('Search'), _('Search for people or text'), false, 'nav_search');
330 function onStartShowLocalNavBlock($action)
332 $action_name = get_class($action);
334 $login_actions = array('LoginAction', 'RegisterAction',
335 'OpenidloginAction', 'FBConnectLoginAction');
337 if (in_array($action_name, $login_actions)) {
338 $nav = new FBCLoginGroupNav($action);
343 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
344 'TwittersettingsAction', 'FBConnectSettingsAction');
346 if (in_array($action_name, $connect_actions)) {
347 $nav = new FBCSettingsNav($action);
355 function onStartLogout($action)
358 $fbuid = $this->loggedIn();
360 if (!empty($fbuid)) {
362 $facebook = getFacebook();
363 $facebook->expire_session();
364 } catch (Exception $e) {
365 common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
373 function getProfilePicURL($fbuid)
376 $facebook = getFacebook();
381 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
383 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
385 if (!empty($result)) {
386 $url = $result[0]['pic_square'];
389 } catch (Exception $e) {
390 common_log(LOG_WARNING, "Facebook client failure requesting profile pic!");