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 onEndShowScripts($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 $js = '<script type="text/javascript">';
119 $js .= ' $(document).ready(function () {';
120 $js .= ' FB_RequireFeatures(';
121 $js .= ' ["XFBML"], function() {';
122 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
127 $js .= ' function goto_login() {';
128 $js .= ' window.location = "%2$s";';
131 // The below function alters the logout link so that it logs the user out
132 // of Facebook Connect as well as the site. However, for some pages
133 // (FB Connect Settings) we need to output the FB Connect scripts (to
134 // show an existing FB connection even if the user isn't authenticated
135 // with Facebook connect) but NOT alter the logout link. And the only
136 // way to reliably do that is with the FB Connect .js libs. Crazy.
138 $js .= ' FB.ensureInit(function() {';
139 $js .= ' FB.Connect.ifUserConnected(';
140 $js .= ' function() { ';
141 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
142 $js .= ' $(\'#nav_logout a\').click(function() {';
143 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
144 $js .= ' return false;';
147 $js .= ' function() {';
148 $js .= ' return false;';
154 $js = sprintf($js, $apikey, $login_url, $logout_url);
156 // Compress the bugger down a bit
157 $js = str_replace(' ', '', $js);
159 $action->raw(" $js"); // leading two spaces to make it line up
164 // Note: this script needs to appear as close as possible to </body>
166 function onEndShowFooter($action)
168 if ($this->reqFbScripts($action)) {
169 $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
173 function onEndShowStatusNetStyles($action)
175 if ($this->reqFbScripts($action)) {
176 $action->cssLink('plugins/FBConnect/FBConnectPlugin.css');
181 * Does the Action we're plugged into require the FB Scripts? We only
182 * want to output FB namespace, scripts, CSS, etc. on the pages that
185 * @param Action the action in question
187 * @return boolean true
190 function reqFbScripts($action) {
192 // If you're logged in w/FB Connect, you always need the FB stuff
194 $fbuid = $this->loggedIn();
196 if (!empty($fbuid)) {
200 // List of actions that require FB stuff
202 $needy = array('FBConnectLoginAction',
203 'FBConnectauthAction',
204 'FBConnectSettingsAction');
206 if (in_array(get_class($action), $needy)) {
215 * Is the user currently logged in with FB Connect?
217 * @return mixed $fbuid the Facebook ID of the logged in user, or null
222 $user = common_current_user();
226 $flink = Foreign_link::getByUserId($user->id,
227 FACEBOOK_CONNECT_SERVICE);
230 if (!empty($flink)) {
234 $facebook = getFacebook();
235 $fbuid = $facebook->get_loggedin_user();
237 } catch (Exception $e) {
238 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
239 'Problem getting Facebook user: ' .
252 function onStartPrimaryNav($action)
255 $user = common_current_user();
256 $connect = 'FBConnectSettings';
257 if (common_config('xmpp', 'enabled')) {
258 $connect = 'imsettings';
259 } else if (common_config('sms', 'enabled')) {
260 $connect = 'smssettings';
261 } else if (common_config('twitter', 'enabled')) {
262 $connect = 'twittersettings';
267 $fbuid = $this->loggedIn();
269 if (!empty($fbuid)) {
271 /* Default FB silhouette pic for FB users who haven't
272 uploaded a profile pic yet. */
275 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
277 $url = $this->getProfilePicURL($fbuid);
279 $action->elementStart('li', array('id' => 'nav_fb'));
281 $action->element('img', array('id' => 'fbc_profile-pic',
282 'src' => (!empty($url)) ? $url : $silhouetteUrl,
283 'alt' => 'Facebook Connect User',
284 'width' => '16'), '');
286 $iconurl = common_path('plugins/FBConnect/fbfavicon.ico');
287 $action->element('img', array('id' => 'fb_favicon',
290 $action->elementEnd('li');
298 function onStartShowLocalNavBlock($action)
300 $action_name = get_class($action);
302 $login_actions = array('LoginAction', 'RegisterAction',
303 'OpenidloginAction', 'FBConnectLoginAction');
305 if (in_array($action_name, $login_actions)) {
306 $nav = new FBCLoginGroupNav($action);
311 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
312 'TwittersettingsAction', 'FBConnectSettingsAction');
314 if (in_array($action_name, $connect_actions)) {
315 $nav = new FBCSettingsNav($action);
323 function onStartLogout($action)
326 $fbuid = $this->loggedIn();
328 if (!empty($fbuid)) {
330 $facebook = getFacebook();
331 $facebook->expire_session();
332 } catch (Exception $e) {
333 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
334 'Could\'t logout of Facebook: ' .
342 function getProfilePicURL($fbuid)
345 $facebook = getFacebook();
350 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
352 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
354 if (!empty($result)) {
355 $url = $result[0]['pic_square'];
358 } catch (Exception $e) {
359 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
360 "Facebook client failure requesting profile pic!");