3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to add a StatusNet Facebook application
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')) {
34 define("FACEBOOK_CONNECT_SERVICE", 3);
36 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
37 require_once INSTALLDIR . '/plugins/Facebook/FBConnectAuth.php';
38 require_once INSTALLDIR . '/plugins/Facebook/FBConnectLogin.php';
39 require_once INSTALLDIR . '/plugins/Facebook/FBConnectSettings.php';
40 require_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
41 require_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
42 require_once INSTALLDIR . '/plugins/Facebook/FBC_XDReceiver.php';
45 * Facebook plugin to add a StatusNet Facebook application
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 FacebookPlugin extends Plugin
58 * Add Facebook app actions to the router table
60 * Hook for RouterInitialized event.
62 * @param Net_URL_Mapper &$m path-to-action mapper
64 * @return boolean hook return
67 function onRouterInitialized(&$m)
72 $m->connect('facebook', array('action' => 'facebookhome'));
73 $m->connect('facebook/index.php', array('action' => 'facebookhome'));
74 $m->connect('facebook/settings.php', array('action' => 'facebooksettings'));
75 $m->connect('facebook/invite.php', array('action' => 'facebookinvite'));
76 $m->connect('facebook/remove', array('action' => 'facebookremove'));
78 // Facebook Connect stuff
80 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
81 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
82 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
83 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
89 * Automatically load the actions and libraries used by the Facebook app
91 * @param Class $cls the class
93 * @return boolean hook return
96 function onAutoload($cls)
99 case 'FacebookAction':
100 case 'FacebookhomeAction':
101 case 'FacebookinviteAction':
102 case 'FacebookremoveAction':
103 case 'FacebooksettingsAction':
104 include_once INSTALLDIR . '/plugins/Facebook/' .
105 strtolower(mb_substr($cls, 0, -6)) . '.php';
113 function onStartShowHTML($action)
116 if ($this->reqFbScripts($action)) {
118 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
119 // Facebook Connect. These browser cannot use Facebook's
120 // DOM parsing routines unless the mime type of the page is
121 // text/html even though Facebook Connect uses XHTML. This is
122 // A bug in Facebook Connect, and this is a temporary solution
123 // until they fix their JavaScript libs.
124 header('Content-Type: text/html');
126 $action->extraHeaders();
128 $action->startXML('html');
130 $language = $action->getLanguage();
132 $action->elementStart('html',
133 array('xmlns' => 'http://www.w3.org/1999/xhtml',
134 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
135 'xml:lang' => $language,
136 'lang' => $language));
146 // Note: this script needs to appear in the <body>
148 function onEndShowScripts($action)
150 if ($this->reqFbScripts($action)) {
152 $apikey = common_config('facebook', 'apikey');
153 $plugin_path = common_path('plugins/Facebook');
155 $login_url = common_local_url('FBConnectAuth');
156 $logout_url = common_local_url('logout');
158 // XXX: Facebook says we don't need this FB_RequireFeatures(),
159 // but we actually do, for IE and Safari. Gar.
161 $js = '<script type="text/javascript">';
162 $js .= ' $(document).ready(function () {';
163 $js .= ' FB_RequireFeatures(';
164 $js .= ' ["XFBML"], function() {';
165 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
170 $js .= ' function goto_login() {';
171 $js .= ' window.location = "%2$s";';
174 // The below function alters the logout link so that it logs the user out
175 // of Facebook Connect as well as the site. However, for some pages
176 // (FB Connect Settings) we need to output the FB Connect scripts (to
177 // show an existing FB connection even if the user isn't authenticated
178 // with Facebook connect) but NOT alter the logout link. And the only
179 // way to reliably do that is with the FB Connect .js libs. Crazy.
181 $js .= ' FB.ensureInit(function() {';
182 $js .= ' FB.Connect.ifUserConnected(';
183 $js .= ' function() { ';
184 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
185 $js .= ' $(\'#nav_logout a\').click(function() {';
186 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
187 $js .= ' return false;';
190 $js .= ' function() {';
191 $js .= ' return false;';
197 $js = sprintf($js, $apikey, $login_url, $logout_url);
199 // Compress the bugger down a bit
200 $js = str_replace(' ', '', $js);
202 $action->raw(" $js"); // leading two spaces to make it line up
207 // Note: this script needs to appear as close as possible to </body>
209 function onEndShowFooter($action)
211 if ($this->reqFbScripts($action)) {
212 $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
216 function onEndShowStatusNetStyles($action)
218 if ($this->reqFbScripts($action)) {
219 $action->cssLink('plugins/Facebook/FBConnect.css');
224 * Does the Action we're plugged into require the FB Scripts? We only
225 * want to output FB namespace, scripts, CSS, etc. on the pages that
228 * @param Action the action in question
230 * @return boolean true
233 function reqFbScripts($action) {
235 // If you're logged in w/FB Connect, you always need the FB stuff
237 $fbuid = $this->loggedIn();
239 if (!empty($fbuid)) {
243 // List of actions that require FB stuff
245 $needy = array('FBConnectLoginAction',
246 'FBConnectauthAction',
247 'FBConnectSettingsAction');
249 if (in_array(get_class($action), $needy)) {
258 * Is the user currently logged in with FB Connect?
260 * @return mixed $fbuid the Facebook ID of the logged in user, or null
265 $user = common_current_user();
269 $flink = Foreign_link::getByUserId($user->id,
270 FACEBOOK_CONNECT_SERVICE);
273 if (!empty($flink)) {
277 $facebook = getFacebook();
278 $fbuid = $facebook->get_loggedin_user();
280 } catch (Exception $e) {
281 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
282 'Problem getting Facebook user: ' .
295 function onStartPrimaryNav($action)
298 $user = common_current_user();
299 $connect = 'FBConnectSettings';
300 if (common_config('xmpp', 'enabled')) {
301 $connect = 'imsettings';
302 } else if (common_config('sms', 'enabled')) {
303 $connect = 'smssettings';
304 } else if (common_config('twitter', 'enabled')) {
305 $connect = 'twittersettings';
310 $fbuid = $this->loggedIn();
312 if (!empty($fbuid)) {
314 /* Default FB silhouette pic for FB users who haven't
315 uploaded a profile pic yet. */
318 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
320 $url = $this->getProfilePicURL($fbuid);
322 $action->elementStart('li', array('id' => 'nav_fb'));
324 $action->element('img', array('id' => 'fbc_profile-pic',
325 'src' => (!empty($url)) ? $url : $silhouetteUrl,
326 'alt' => 'Facebook Connect User',
327 'width' => '16'), '');
329 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
330 $action->element('img', array('id' => 'fb_favicon',
333 $action->elementEnd('li');
341 function onStartShowLocalNavBlock($action)
343 $action_name = get_class($action);
345 $login_actions = array('LoginAction', 'RegisterAction',
346 'OpenidloginAction', 'FBConnectLoginAction');
348 if (in_array($action_name, $login_actions)) {
349 $nav = new FBCLoginGroupNav($action);
354 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
355 'TwittersettingsAction', 'FBConnectSettingsAction');
357 if (in_array($action_name, $connect_actions)) {
358 $nav = new FBCSettingsNav($action);
366 function onStartLogout($action)
369 $fbuid = $this->loggedIn();
371 if (!empty($fbuid)) {
373 $facebook = getFacebook();
374 $facebook->expire_session();
375 } catch (Exception $e) {
376 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
377 'Could\'t logout of Facebook: ' .
385 function getProfilePicURL($fbuid)
388 $facebook = getFacebook();
393 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
395 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
397 if (!empty($result)) {
398 $url = $result[0]['pic_square'];
401 } catch (Exception $e) {
402 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
403 "Facebook client failure requesting profile pic!");
411 * Add a Facebook queue item for each notice
413 * @param Notice $notice the notice
414 * @param array &$transports the list of transports (queues)
416 * @return boolean hook return
418 function onStartEnqueueNotice($notice, &$transports)
420 array_push($transports, 'facebook');
425 * broadcast the message when not using queuehandler
427 * @param Notice &$notice the notice
428 * @param array $queue destination queue
430 * @return boolean hook return
432 function onUnqueueHandleNotice(&$notice, $queue)
434 if (($queue == 'facebook') && ($this->_isLocal($notice))) {
435 facebookBroadcastNotice($notice);
442 * Determine whether the notice was locally created
444 * @param Notice $notice
446 * @return boolean locality
448 function _isLocal($notice)
450 return ($notice->is_local == Notice::LOCAL_PUBLIC ||
451 $notice->is_local == Notice::LOCAL_NONPUBLIC);
455 * Add Facebook queuehandler to the list of daemons to start
457 * @param array $daemons the list fo daemons to run
459 * @return boolean hook return
462 function onGetValidDaemons($daemons)
464 array_push($daemons, INSTALLDIR .
465 '/plugins/Facebook/facebookqueuehandler.php');