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';
39 * Facebook plugin to add a StatusNet Facebook application
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class FacebookPlugin extends Plugin
52 * Add Facebook app actions to the router table
54 * Hook for RouterInitialized event.
56 * @param Net_URL_Mapper &$m path-to-action mapper
58 * @return boolean hook return
61 function onRouterInitialized(&$m)
66 $m->connect('facebook/app', array('action' => 'facebookhome'));
67 $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
68 $m->connect('facebook/app/settings.php', array('action' => 'facebooksettings'));
69 $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
70 $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
72 // Facebook Connect stuff
74 $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
75 $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
76 $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
77 $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
83 * Automatically load the actions and libraries used by the Facebook app
85 * @param Class $cls the class
87 * @return boolean hook return
90 function onAutoload($cls)
93 case 'FacebookAction':
94 case 'FacebookhomeAction':
95 case 'FacebookinviteAction':
96 case 'FacebookremoveAction':
97 case 'FacebooksettingsAction':
98 include_once INSTALLDIR . '/plugins/Facebook/' .
99 strtolower(mb_substr($cls, 0, -6)) . '.php';
101 case 'FBConnectAuthAction':
102 case 'FBConnectLoginAction':
103 case 'FBConnectSettingsAction':
104 case 'FBC_XDReceiverAction':
105 include_once INSTALLDIR . '/plugins/Facebook/' .
106 mb_substr($cls, 0, -6) . '.php';
108 case 'FBCLoginGroupNav':
109 include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
111 case 'FBCSettingsNav':
112 include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
120 function onStartShowHTML($action)
123 if ($this->reqFbScripts($action)) {
125 // XXX: Horrible hack to make Safari, FF2, and Chrome work with
126 // Facebook Connect. These browser cannot use Facebook's
127 // DOM parsing routines unless the mime type of the page is
128 // text/html even though Facebook Connect uses XHTML. This is
129 // A bug in Facebook Connect, and this is a temporary solution
130 // until they fix their JavaScript libs.
131 header('Content-Type: text/html');
133 $action->extraHeaders();
135 $action->startXML('html');
137 $language = $action->getLanguage();
139 $action->elementStart('html',
140 array('xmlns' => 'http://www.w3.org/1999/xhtml',
141 'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
142 'xml:lang' => $language,
143 'lang' => $language));
153 // Note: this script needs to appear in the <body>
155 function onEndShowScripts($action)
157 if ($this->reqFbScripts($action)) {
159 $apikey = common_config('facebook', 'apikey');
160 $plugin_path = common_path('plugins/Facebook');
162 $login_url = common_local_url('FBConnectAuth');
163 $logout_url = common_local_url('logout');
165 // XXX: Facebook says we don't need this FB_RequireFeatures(),
166 // but we actually do, for IE and Safari. Gar.
168 $js = '<script type="text/javascript">';
169 $js .= ' $(document).ready(function () {';
170 $js .= ' FB_RequireFeatures(';
171 $js .= ' ["XFBML"], function() {';
172 $js .= ' FB.init("%1$s", "../xd_receiver.html");';
177 $js .= ' function goto_login() {';
178 $js .= ' window.location = "%2$s";';
181 // The below function alters the logout link so that it logs the user out
182 // of Facebook Connect as well as the site. However, for some pages
183 // (FB Connect Settings) we need to output the FB Connect scripts (to
184 // show an existing FB connection even if the user isn't authenticated
185 // with Facebook connect) but NOT alter the logout link. And the only
186 // way to reliably do that is with the FB Connect .js libs. Crazy.
188 $js .= ' FB.ensureInit(function() {';
189 $js .= ' FB.Connect.ifUserConnected(';
190 $js .= ' function() { ';
191 $js .= ' $(\'#nav_logout a\').attr(\'href\', \'#\');';
192 $js .= ' $(\'#nav_logout a\').click(function() {';
193 $js .= ' FB.Connect.logoutAndRedirect(\'%3$s\');';
194 $js .= ' return false;';
197 $js .= ' function() {';
198 $js .= ' return false;';
204 $js = sprintf($js, $apikey, $login_url, $logout_url);
206 // Compress the bugger down a bit
207 $js = str_replace(' ', '', $js);
209 $action->raw(" $js"); // leading two spaces to make it line up
214 // Note: this script needs to appear as close as possible to </body>
216 function onEndShowFooter($action)
218 if ($this->reqFbScripts($action)) {
219 $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
223 function onEndShowStatusNetStyles($action)
225 if ($this->reqFbScripts($action)) {
226 $action->cssLink('plugins/Facebook/FBConnect.css');
231 * Does the Action we're plugged into require the FB Scripts? We only
232 * want to output FB namespace, scripts, CSS, etc. on the pages that
235 * @param Action the action in question
237 * @return boolean true
240 function reqFbScripts($action) {
242 // If you're logged in w/FB Connect, you always need the FB stuff
244 $fbuid = $this->loggedIn();
246 if (!empty($fbuid)) {
250 // List of actions that require FB stuff
252 $needy = array('FBConnectLoginAction',
253 'FBConnectauthAction',
254 'FBConnectSettingsAction');
256 if (in_array(get_class($action), $needy)) {
265 * Is the user currently logged in with FB Connect?
267 * @return mixed $fbuid the Facebook ID of the logged in user, or null
272 $user = common_current_user();
276 $flink = Foreign_link::getByUserId($user->id,
277 FACEBOOK_CONNECT_SERVICE);
280 if (!empty($flink)) {
284 $facebook = getFacebook();
285 $fbuid = $facebook->get_loggedin_user();
287 } catch (Exception $e) {
288 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
289 'Problem getting Facebook user: ' .
302 function onStartPrimaryNav($action)
305 $user = common_current_user();
306 $connect = 'FBConnectSettings';
307 if (common_config('xmpp', 'enabled')) {
308 $connect = 'imsettings';
309 } else if (common_config('sms', 'enabled')) {
310 $connect = 'smssettings';
311 } else if (common_config('twitter', 'enabled')) {
312 $connect = 'twittersettings';
317 $fbuid = $this->loggedIn();
319 if (!empty($fbuid)) {
321 /* Default FB silhouette pic for FB users who haven't
322 uploaded a profile pic yet. */
325 'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
327 $url = $this->getProfilePicURL($fbuid);
329 $action->elementStart('li', array('id' => 'nav_fb'));
331 $action->element('img', array('id' => 'fbc_profile-pic',
332 'src' => (!empty($url)) ? $url : $silhouetteUrl,
333 'alt' => 'Facebook Connect User',
334 'width' => '16'), '');
336 $iconurl = common_path('plugins/Facebook/fbfavicon.ico');
337 $action->element('img', array('id' => 'fb_favicon',
340 $action->elementEnd('li');
348 function onStartShowLocalNavBlock($action)
350 $action_name = get_class($action);
352 $login_actions = array('LoginAction', 'RegisterAction',
353 'OpenidloginAction', 'FBConnectLoginAction');
355 if (in_array($action_name, $login_actions)) {
356 $nav = new FBCLoginGroupNav($action);
361 $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
362 'TwittersettingsAction', 'FBConnectSettingsAction');
364 if (in_array($action_name, $connect_actions)) {
365 $nav = new FBCSettingsNav($action);
373 function onStartLogout($action)
376 $fbuid = $this->loggedIn();
378 if (!empty($fbuid)) {
380 $facebook = getFacebook();
381 $facebook->expire_session();
382 } catch (Exception $e) {
383 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
384 'Could\'t logout of Facebook: ' .
392 function getProfilePicURL($fbuid)
395 $facebook = getFacebook();
400 $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
402 $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
404 if (!empty($result)) {
405 $url = $result[0]['pic_square'];
408 } catch (Exception $e) {
409 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
410 "Facebook client failure requesting profile pic!");
418 * Add a Facebook queue item for each notice
420 * @param Notice $notice the notice
421 * @param array &$transports the list of transports (queues)
423 * @return boolean hook return
425 function onStartEnqueueNotice($notice, &$transports)
427 array_push($transports, 'facebook');
432 * broadcast the message when not using queuehandler
434 * @param Notice &$notice the notice
435 * @param array $queue destination queue
437 * @return boolean hook return
439 function onUnqueueHandleNotice(&$notice, $queue)
441 if (($queue == 'facebook') && ($this->_isLocal($notice))) {
442 facebookBroadcastNotice($notice);
449 * Determine whether the notice was locally created
451 * @param Notice $notice
453 * @return boolean locality
455 function _isLocal($notice)
457 return ($notice->is_local == Notice::LOCAL_PUBLIC ||
458 $notice->is_local == Notice::LOCAL_NONPUBLIC);
462 * Add Facebook queuehandler to the list of daemons to start
464 * @param array $daemons the list fo daemons to run
466 * @return boolean hook return
469 function onGetValidDaemons($daemons)
471 array_push($daemons, INSTALLDIR .
472 '/plugins/Facebook/facebookqueuehandler.php');