]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/FacebookPlugin.php
Merge branch 'inblob' of git@gitorious.org:~evan/statusnet/evans-mainline into inblob
[quix0rs-gnu-social.git] / plugins / Facebook / FacebookPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to add a StatusNet Facebook application
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 define("FACEBOOK_CONNECT_SERVICE", 3);
35 define('FACEBOOKPLUGIN_VERSION', '0.9');
36
37 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
38
39 /**
40  * Facebook plugin to add a StatusNet Facebook application
41  *
42  * @category Plugin
43  * @package  StatusNet
44  * @author   Zach Copley <zach@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class FacebookPlugin extends Plugin
50 {
51
52     /**
53      * Add Facebook app actions to the router table
54      *
55      * Hook for RouterInitialized event.
56      *
57      * @param Net_URL_Mapper &$m path-to-action mapper
58      *
59      * @return boolean hook return
60      */
61
62     function onStartInitializeRouter($m)
63     {
64
65         // Facebook App stuff
66
67         $m->connect('facebook/app', array('action' => 'facebookhome'));
68         $m->connect('facebook/app/index.php', array('action' => 'facebookhome'));
69         $m->connect('facebook/app/settings.php',
70                     array('action' => 'facebooksettings'));
71         $m->connect('facebook/app/invite.php', array('action' => 'facebookinvite'));
72         $m->connect('facebook/app/remove', array('action' => 'facebookremove'));
73
74         // Facebook Connect stuff
75
76         $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
77         $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
78         $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
79         $m->connect('xd_receiver.html', array('action' => 'FBC_XDReceiver'));
80
81         return true;
82     }
83
84     /**
85      * Automatically load the actions and libraries used by the Facebook app
86      *
87      * @param Class $cls the class
88      *
89      * @return boolean hook return
90      *
91      */
92
93     function onAutoload($cls)
94     {
95         switch ($cls) {
96         case 'FacebookAction':
97         case 'FacebookhomeAction':
98         case 'FacebookinviteAction':
99         case 'FacebookremoveAction':
100         case 'FacebooksettingsAction':
101             include_once INSTALLDIR . '/plugins/Facebook/' .
102               strtolower(mb_substr($cls, 0, -6)) . '.php';
103             return false;
104         case 'FBConnectAuthAction':
105         case 'FBConnectLoginAction':
106         case 'FBConnectSettingsAction':
107         case 'FBC_XDReceiverAction':
108             include_once INSTALLDIR . '/plugins/Facebook/' .
109               mb_substr($cls, 0, -6) . '.php';
110             return false;
111         case 'FBCLoginGroupNav':
112             include_once INSTALLDIR . '/plugins/Facebook/FBCLoginGroupNav.php';
113             return false;
114         case 'FBCSettingsNav':
115             include_once INSTALLDIR . '/plugins/Facebook/FBCSettingsNav.php';
116             return false;
117         default:
118             return true;
119         }
120     }
121
122     /**
123      * Override normal HTML output to force the content type to
124      * text/html and add in xmlns:fb
125      *
126      * @param Action $action the current action
127      *
128      * @return void
129      */
130
131     function onStartShowHTML($action)
132     {
133
134         if ($this->reqFbScripts($action)) {
135
136             // XXX: Horrible hack to make Safari, FF2, and Chrome work with
137             // Facebook Connect. These browser cannot use Facebook's
138             // DOM parsing routines unless the mime type of the page is
139             // text/html even though Facebook Connect uses XHTML.  This is
140             // A bug in Facebook Connect, and this is a temporary solution
141             // until they fix their JavaScript libs.
142
143             header('Content-Type: text/html');
144
145             $action->extraHeaders();
146
147             $action->startXML('html');
148
149             $language = $action->getLanguage();
150
151             $action->elementStart('html',
152                 array('xmlns'  => 'http://www.w3.org/1999/xhtml',
153                       'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
154                       'xml:lang' => $language,
155                       'lang'     => $language));
156
157             return false;
158
159         } else {
160
161             return true;
162         }
163     }
164
165     /**
166      * Add in the Facebook Connect JavaScript stuff
167      *
168      * Note: this script needs to appear in the <body>
169      *
170      * @param Action $action the current action
171      *
172      * @return void
173      *
174      */
175
176     function onEndShowScripts($action)
177     {
178         if ($this->reqFbScripts($action)) {
179
180             $apikey      = common_config('facebook', 'apikey');
181             $plugin_path = common_path('plugins/Facebook');
182
183             $login_url  = common_local_url('FBConnectAuth');
184             $logout_url = common_local_url('logout');
185
186             // XXX: Facebook says we don't need this FB_RequireFeatures(),
187             // but we actually do, for IE and Safari. Gar.
188
189             $js .= '    $(document).ready(function () {';
190             $js .= '         FB_RequireFeatures(';
191             $js .= '             ["XFBML"], function() {';
192             $js .= '                 FB.init("%1$s", "../xd_receiver.html");';
193             $js .= '             }';
194             $js .= '         );';
195             $js .= '    });';
196
197             $js .= '    function goto_login() {';
198             $js .= '        window.location = "%2$s";';
199             $js .= '    }';
200
201             // The below function alters the logout link so that it logs the user out
202             // of Facebook Connect as well as the site.  However, for some pages
203             // (FB Connect Settings) we need to output the FB Connect scripts (to
204             // show an existing FB connection even if the user isn't authenticated
205             // with Facebook connect) but NOT alter the logout link. And the only
206             // way to reliably do that is with the FB Connect .js libs.  Crazy.
207
208             $js .= '    FB.ensureInit(function() {';
209             $js .= '        FB.Connect.ifUserConnected(';
210             $js .= '            function() { ';
211             $js .= '                $(\'#nav_logout a\').attr(\'href\', \'#\');';
212             $js .= '                $(\'#nav_logout a\').click(function() {';
213             $js .= '                   FB.Connect.logoutAndRedirect(\'%3$s\');';
214             $js .= '                   return false;';
215             $js .= '                })';
216             $js .= '            },';
217             $js .= '            function() {';
218             $js .= '                return false;';
219             $js .= '            }';
220             $js .= '        );';
221             $js .= '     });';
222
223             $js = sprintf($js, $apikey, $login_url, $logout_url);
224
225             // Compress the bugger down a bit
226
227             $js = str_replace('  ', '', $js);
228
229             $action->inlineScript($js);
230         }
231
232     }
233
234     /**
235      * Add in an additional Facebook Connect script that's supposed to
236      * appear as close as possible to </body>
237      *
238      * @param Action $action the current action
239      *
240      * @return void
241      *
242      */
243
244     function onEndShowFooter($action)
245     {
246         if ($this->reqFbScripts($action)) {
247             $action->script('http://static.ak.connect.facebook.com' .
248                             '/js/api_lib/v0.4/FeatureLoader.js.php');
249         }
250     }
251
252     /**
253      * Output Facebook Connect specific CSS link
254      *
255      * @param Action $action the current action
256      *
257      * @return void
258      *
259      */
260
261     function onEndShowStatusNetStyles($action)
262     {
263         if ($this->reqFbScripts($action)) {
264             $action->cssLink('plugins/Facebook/FBConnect.css');
265         }
266     }
267
268     /**
269      * Does the Action we're plugged into require the FB Scripts?  We only
270      * want to output FB namespace, scripts, CSS, etc. on the pages that
271      * really need them.
272      *
273      * @param Action $action the current action
274      *
275      * @return boolean true
276      */
277
278     function reqFbScripts($action)
279     {
280
281         // If you're logged in w/FB Connect, you always need the FB stuff
282
283         $fbuid = $this->loggedIn();
284
285         if (!empty($fbuid)) {
286             return true;
287         }
288
289         // List of actions that require FB stuff
290
291         $needy = array('FBConnectLoginAction',
292                        'FBConnectauthAction',
293                        'FBConnectSettingsAction');
294
295         if (in_array(get_class($action), $needy)) {
296             return true;
297         }
298
299         return false;
300
301     }
302
303     /**
304      * Is the user currently logged in with FB Connect?
305      *
306      * @return mixed $fbuid the Facebook ID of the logged in user, or null
307      */
308
309     function loggedIn()
310     {
311         $user = common_current_user();
312
313         if (!empty($user)) {
314
315             $flink = Foreign_link::getByUserId($user->id,
316                 FACEBOOK_CONNECT_SERVICE);
317             $fbuid = 0;
318
319             if (!empty($flink)) {
320
321                 try {
322
323                     $facebook = getFacebook();
324                     $fbuid    = $facebook->get_loggedin_user();
325
326                 } catch (Exception $e) {
327                     common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
328                         'Problem getting Facebook user: ' .
329                             $e->getMessage());
330                 }
331
332                 if ($fbuid > 0) {
333                     return $fbuid;
334                 }
335             }
336         }
337
338         return null;
339     }
340
341     /**
342      * Add in a Facebook Connect avatar to the primary nav menu
343      *
344      * @param Action $action the current action
345      *
346      * @return void
347      *
348      */
349
350     function onStartPrimaryNav($action)
351     {
352         $user = common_current_user();
353
354         $connect = 'FBConnectSettings';
355         if (common_config('xmpp', 'enabled')) {
356             $connect = 'imsettings';
357         } else if (common_config('sms', 'enabled')) {
358             $connect = 'smssettings';
359         } else if (common_config('twitter', 'enabled')) {
360             $connect = 'twittersettings';
361         }
362
363         if (!empty($user)) {
364
365             $fbuid = $this->loggedIn();
366
367             if (!empty($fbuid)) {
368
369                 /* Default FB silhouette pic for FB users who haven't
370                    uploaded a profile pic yet. */
371
372                 $silhouetteUrl =
373                     'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
374
375                 $url = $this->getProfilePicURL($fbuid);
376
377                 $action->elementStart('li', array('id' => 'nav_fb'));
378
379                 $action->element('img', array('id' => 'fbc_profile-pic',
380                     'src' => (!empty($url)) ? $url : $silhouetteUrl,
381                     'alt' => 'Facebook Connect User',
382                     'width' => '16'), '');
383
384                 $iconurl =  common_path('plugins/Facebook/fbfavicon.ico');
385                 $action->element('img', array('id' => 'fb_favicon',
386                     'src' => $iconurl));
387
388                 $action->elementEnd('li');
389
390             }
391         }
392
393         return true;
394     }
395
396     /*
397      * Add a login tab for Facebook Connect
398      *
399      * @param Action &action the current action
400      *
401      * @return void
402      */
403
404     function onEndLoginGroupNav(&$action)
405     {
406
407         $action_name = $action->trimmed('action');
408
409         $action->menuItem(common_local_url('FBConnectLogin'),
410                                            _m('Facebook'),
411                                            _m('Login or register using Facebook'),
412                                            'FBConnectLogin' === $action_name);
413
414         return true;
415     }
416
417     /*
418      * Add a tab for managing Facebook Connect settings
419      *
420      * @param Action &action the current action
421      *
422      * @return void
423      */
424
425     function onEndConnectSettingsNav(&$action)
426     {
427         $action_name = $action->trimmed('action');
428
429         $action->menuItem(common_local_url('FBConnectSettings'),
430                           _m('Facebook'),
431                           _m('Facebook Connect Settings'),
432                           $action_name === 'FBConnectSettings');
433
434         return true;
435     }
436
437     /**
438      * Have the logout process do some Facebook Connect cookie cleanup
439      *
440      * @param Action $action the current action
441      *
442      * @return void
443      */
444
445     function onStartLogout($action)
446     {
447         $action->logout();
448         $fbuid = $this->loggedIn();
449
450         if (!empty($fbuid)) {
451             try {
452                 $facebook = getFacebook();
453                 $facebook->expire_session();
454             } catch (Exception $e) {
455                 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
456                            'Could\'t logout of Facebook: ' .
457                            $e->getMessage());
458             }
459         }
460
461         return true;
462     }
463
464     /**
465      * Get the URL of the user's Facebook avatar
466      *
467      * @param int $fbuid the Facebook user ID
468      *
469      * @return string $url the url for the user's Facebook avatar
470      */
471
472     function getProfilePicURL($fbuid)
473     {
474         $facebook = getFacebook();
475         $url      = null;
476
477         try {
478
479             $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
480
481             $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
482
483             if (!empty($result)) {
484                 $url = $result[0]['pic_square'];
485             }
486
487         } catch (Exception $e) {
488             common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
489                        "Facebook client failure requesting profile pic!");
490         }
491
492         return $url;
493     }
494
495     /**
496      * Add a Facebook queue item for each notice
497      *
498      * @param Notice $notice      the notice
499      * @param array  &$transports the list of transports (queues)
500      *
501      * @return boolean hook return
502      */
503
504     function onStartEnqueueNotice($notice, &$transports)
505     {
506         array_push($transports, 'facebook');
507         return true;
508     }
509
510     /**
511      * broadcast the message when not using queuehandler
512      *
513      * @param Notice &$notice the notice
514      * @param array  $queue   destination queue
515      *
516      * @return boolean hook return
517      */
518
519     function onUnqueueHandleNotice(&$notice, $queue)
520     {
521         if (($queue == 'facebook') && ($this->_isLocal($notice))) {
522             facebookBroadcastNotice($notice);
523             return false;
524         }
525         return true;
526     }
527
528     /**
529      * Determine whether the notice was locally created
530      *
531      * @param Notice $notice the notice
532      *
533      * @return boolean locality
534      */
535
536     function _isLocal($notice)
537     {
538         return ($notice->is_local == Notice::LOCAL_PUBLIC ||
539                 $notice->is_local == Notice::LOCAL_NONPUBLIC);
540     }
541
542     /**
543      * Add Facebook queuehandler to the list of daemons to start
544      *
545      * @param array $daemons the list fo daemons to run
546      *
547      * @return boolean hook return
548      *
549      */
550
551     function onGetValidDaemons($daemons)
552     {
553         array_push($daemons, INSTALLDIR .
554                    '/plugins/Facebook/facebookqueuehandler.php');
555         return true;
556     }
557
558     function onPluginVersion(&$versions)
559     {
560         $versions[] = array('name' => 'Facebook',
561                             'version' => FACEBOOKPLUGIN_VERSION,
562                             'author' => 'Zach Copley',
563                             'homepage' => 'http://status.net/wiki/Plugin:Facebook',
564                             'rawdescription' =>
565                             _m('The Facebook plugin allows you to integrate ' .
566                                'your StatusNet instance with ' .
567                                '<a href="http://facebook.com/">Facebook</a> ' .
568                                'and Facebook Connect.'));
569         return true;
570     }
571
572 }