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 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectLogin.php';
35 require_once INSTALLDIR . '/lib/facebookutil.php';
38 * Plugin to enable Facebook Connect
42 * @author Zach Copley <zach@controlyourself.ca>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://laconi.ca/
47 class FBConnectPlugin extends Plugin
50 function __construct()
52 parent::__construct();
55 // Hook in new actions
56 function onRouterInitialized(&$m) {
57 $m->connect('main/facebookconnect', array('action' => 'fbconnectlogin'));
61 function onStartShowHTML($action)
64 // XXX: This is probably a bad place to do general processing
65 // so maybe I need to make some new events? Maybe in
68 $name = get_class($action);
70 // Avoid a redirect loop
71 if (!in_array($name, array('FBConnectloginAction', 'ClientErrorAction'))) {
73 $this->checkFacebookUser($action);
77 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
78 $_SERVER['HTTP_ACCEPT'] : null;
80 // XXX: allow content negotiation for RDF, RSS, or XRDS
82 $cp = common_accept_to_prefs($httpaccept);
83 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
85 $type = common_negotiate_type($cp, $sp);
88 throw new ClientException(_('This page is not available in a '.
89 'media type you accept'), 406);
93 header('Content-Type: '.$type);
95 $action->extraHeaders();
97 $action->startXML('html',
98 '-//W3C//DTD XHTML 1.0 Strict//EN',
99 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
101 $language = $action->getLanguage();
103 $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
104 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
105 'xml:lang' => $language,
106 'lang' => $language));
112 function onEndShowLaconicaScripts($action)
115 $action->element('script',
116 array('type' => 'text/javascript',
117 'src' => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
120 $apikey = common_config('facebook', 'apikey');
121 $plugin_path = common_path('plugins/FBConnect');
123 $login_url = common_get_returnto();
126 // We don't have to return to it again
127 common_set_returnto(null);
129 $url = common_local_url('public');
132 $logout_url = common_local_url('logout');
134 $html = sprintf('<script type="text/javascript">FB.init("%s", "%s/xd_receiver.htm");
136 function goto_login() {
137 window.location = "%s";
140 function goto_logout() {
141 window.location = "%s";
144 </script>', $apikey, $plugin_path, $login_url, $logout_url);
150 function onStartPrimaryNav($action)
152 $user = common_current_user();
155 $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
156 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
157 $action->menuItem(common_local_url('profilesettings'),
158 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
159 if (common_config('xmpp', 'enabled')) {
160 $action->menuItem(common_local_url('imsettings'),
161 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
163 $action->menuItem(common_local_url('smssettings'),
164 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
166 $action->menuItem(common_local_url('invite'),
168 sprintf(_('Invite friends and colleagues to join you on %s'),
169 common_config('site', 'name')),
170 false, 'nav_invitecontact');
172 // Need to override the Logout link to make it do FB stuff
174 $logout_url = common_local_url('logout');
175 $title = _('Logout from the site');
178 $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
179 'onclick="FB.Connect.logout(function() { goto_logout() })">%s</a></li>',
180 $logout_url, $title, $text);
186 if (!common_config('site', 'closed')) {
187 $action->menuItem(common_local_url('register'),
188 _('Register'), _('Create an account'), false, 'nav_register');
190 $action->menuItem(common_local_url('openidlogin'),
191 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
192 $action->menuItem(common_local_url('login'),
193 _('Login'), _('Login to the site'), false, 'nav_login');
196 $action->menuItem(common_local_url('doc', array('title' => 'help')),
197 _('Help'), _('Help me!'), false, 'nav_help');
198 $action->menuItem(common_local_url('peoplesearch'),
199 _('Search'), _('Search for people or text'), false, 'nav_search');
201 // Tack on "Connect with Facebook" button
203 // XXX: Maybe this looks bad and should not go here. Where should it go?
206 $action->elementStart('li');
207 $action->element('fb:login-button', array('onlogin' => 'goto_login()',
208 'length' => 'long'));
209 $action->elementEnd('li');
215 function checkFacebookUser() {
217 $user = common_current_user();
225 $facebook = getFacebook();
226 $fbuid = $facebook->get_loggedin_user();
228 // If you're a Facebook user and you're logged in do nothing
230 // If you're a Facebook user and you're not logged in
231 // redirect to Facebook connect login page because that means you have clicked
232 // the 'connect with Facebook' button and have cookies
236 if ($facebook->api_client->users_isAppUser($fbuid) ||
237 $facebook->api_client->added) {
239 // user should be connected...
241 common_debug("Facebook user found: $fbuid");
244 common_debug("Facebook user is logged in.");
248 common_debug("Facebook user is NOT logged in.");
249 common_redirect(common_local_url('fbconnectlogin'), 303);
253 common_debug("No Facebook connect user found.");
257 } catch (Exception $e) {
258 common_debug('Expired FB session.');