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 . '/lib/facebookutil.php';
35 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectAuth.php';
36 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectLogin.php';
37 require_once INSTALLDIR . '/plugins/FBConnect/FBCLoginGroupNav.php';
40 * Plugin to enable Facebook Connect
44 * @author Zach Copley <zach@controlyourself.ca>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://laconi.ca/
49 class FBConnectPlugin extends Plugin
52 function __construct()
54 parent::__construct();
57 // Hook in new actions
58 function onRouterInitialized(&$m) {
60 common_debug("onRouterIntialized()");
62 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
63 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
67 function onStartShowHTML($action)
70 // XXX: This is probably a bad place to do general processing
71 // so maybe I need to make some new events? Maybe in
74 $name = get_class($action);
76 common_debug("onStartShowHTML: action = $name");
78 // Avoid a redirect loop
79 if (!in_array($name, array('FBConnectAuthAction', 'ClientErrorAction'))) {
81 $this->checkFacebookUser($action);
85 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
86 $_SERVER['HTTP_ACCEPT'] : null;
88 // XXX: allow content negotiation for RDF, RSS, or XRDS
90 $cp = common_accept_to_prefs($httpaccept);
91 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
93 $type = common_negotiate_type($cp, $sp);
96 throw new ClientException(_('This page is not available in a '.
97 'media type you accept'), 406);
101 header('Content-Type: '.$type);
103 $action->extraHeaders();
105 $action->startXML('html',
106 '-//W3C//DTD XHTML 1.0 Strict//EN',
107 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
109 $language = $action->getLanguage();
111 $action->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
112 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
113 'xml:lang' => $language,
114 'lang' => $language));
120 function onEndShowLaconicaScripts($action)
123 $action->element('script',
124 array('type' => 'text/javascript',
125 'src' => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
128 $apikey = common_config('facebook', 'apikey');
129 $plugin_path = common_path('plugins/FBConnect');
131 $login_url = common_get_returnto();
134 // We don't have to return to it again
135 common_set_returnto(null);
137 $url = common_local_url('public');
140 $logout_url = common_local_url('logout');
142 $html = sprintf('<script type="text/javascript">FB.init("%s", "%s/xd_receiver.htm");
144 function goto_login() {
145 window.location = "%s";
148 function goto_logout() {
149 window.location = "%s";
152 </script>', $apikey, $plugin_path, $login_url, $logout_url);
158 function onStartPrimaryNav($action)
160 $user = common_current_user();
163 $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
164 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
165 $action->menuItem(common_local_url('profilesettings'),
166 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
167 if (common_config('xmpp', 'enabled')) {
168 $action->menuItem(common_local_url('imsettings'),
169 _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
171 $action->menuItem(common_local_url('smssettings'),
172 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
174 $action->menuItem(common_local_url('invite'),
176 sprintf(_('Invite friends and colleagues to join you on %s'),
177 common_config('site', 'name')),
178 false, 'nav_invitecontact');
180 // Need to override the Logout link to make it do FB stuff
182 $logout_url = common_local_url('logout');
183 $title = _('Logout from the site');
186 $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
187 'onclick="FB.Connect.logout(function() { goto_logout() })">%s</a></li>',
188 $logout_url, $title, $text);
194 if (!common_config('site', 'closed')) {
195 $action->menuItem(common_local_url('register'),
196 _('Register'), _('Create an account'), false, 'nav_register');
198 $action->menuItem(common_local_url('openidlogin'),
199 _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
200 $action->menuItem(common_local_url('login'),
201 _('Login'), _('Login to the site'), false, 'nav_login');
204 $action->menuItem(common_local_url('doc', array('title' => 'help')),
205 _('Help'), _('Help me!'), false, 'nav_help');
206 $action->menuItem(common_local_url('peoplesearch'),
207 _('Search'), _('Search for people or text'), false, 'nav_search');
209 // Tack on "Connect with Facebook" button
211 // XXX: Maybe this looks bad and should not go here. Where should it go?
214 $action->elementStart('li');
215 $action->element('fb:login-button', array('onlogin' => 'goto_login()',
216 'length' => 'long'));
217 $action->elementEnd('li');
223 function checkFacebookUser() {
225 $user = common_current_user();
233 $facebook = getFacebook();
234 $fbuid = $facebook->get_loggedin_user();
236 // If you're a Facebook user and you're logged in do nothing
238 // If you're a Facebook user and you're not logged in
239 // redirect to Facebook connect login page because that means you have clicked
240 // the 'connect with Facebook' button and have cookies
244 if ($facebook->api_client->users_isAppUser($fbuid) ||
245 $facebook->api_client->added) {
247 // user should be connected...
249 common_debug("Facebook user found: $fbuid");
252 common_debug("Facebook user is logged in.");
256 common_debug("Facebook user is NOT logged in.");
257 common_redirect(common_local_url('FBConnectAuth'), 303);
261 common_debug("No Facebook connect user found.");
265 } catch (Exception $e) {
266 common_debug('Expired FB session.');
271 function onStartShowLocalNavBlock($action)
273 $action_name = get_class($action);
275 common_debug($action_name);
277 $login_actions = array('LoginAction', 'RegisterAction',
278 'OpenidloginAction', 'FacebookStart');
280 if (in_array($action_name, $login_actions)) {
282 common_debug("LoginAction found!");
284 $nav = new FBCLoginGroupNav($action);