]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
0dacf90122dcfa8653337a98324f1d73d2aee7f3
[quix0rs-gnu-social.git] / plugins / FBConnect / FBConnectPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to enable Facebook Connect
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') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 define("FACEBOOK_CONNECT_SERVICE", 3);
35
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';
43
44 /**
45  * Plugin to enable Facebook Connect
46  *
47  * @category Plugin
48  * @package  StatusNet
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/
52  */
53
54 class FBConnectPlugin extends Plugin
55 {
56     function __construct()
57     {
58         parent::__construct();
59     }
60
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'));
67      }
68
69     // Add in xmlns:fb
70     function onStartShowHTML($action)
71     {
72
73         if ($this->reqFbScripts($action)) {
74
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');
82
83             $action->extraHeaders();
84
85             $action->startXML('html');
86
87             $language = $action->getLanguage();
88
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));
94
95             return false;
96
97         } else {
98
99             return true;
100         }
101     }
102
103     // Note: this script needs to appear in the <body>
104
105     function onEndShowScripts($action)
106     {
107         if ($this->reqFbScripts($action)) {
108
109             $apikey = common_config('facebook', 'apikey');
110             $plugin_path = common_path('plugins/FBConnect');
111
112             $login_url = common_local_url('FBConnectAuth');
113             $logout_url = common_local_url('logout');
114
115             // XXX: Facebook says we don't need this FB_RequireFeatures(),
116             // but we actually do, for IE and Safari. Gar.
117
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");';
123             $js .= '             }';
124             $js .= '         );';
125             $js .= '    });';
126
127             $js .= '    function goto_login() {';
128             $js .= '        window.location = "%2$s";';
129             $js .= '    }';
130
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.
137
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;';
145             $js .= '                })';
146             $js .= '            },';
147             $js .= '            function() {';
148             $js .= '                return false;';
149             $js .= '            }';
150             $js .= '        );';
151             $js .= '     });';
152             $js .= '</script>';
153
154             $js = sprintf($js, $apikey, $login_url, $logout_url);
155
156             // Compress the bugger down a bit
157             $js = str_replace('  ', '', $js);
158
159             $action->raw("  $js");  // leading two spaces to make it line up
160         }
161
162     }
163
164     // Note: this script needs to appear as close as possible to </body>
165
166     function onEndShowFooter($action)
167     {
168         if ($this->reqFbScripts($action)) {
169             $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
170         }
171     }
172
173     function onEndShowStatusNetStyles($action)
174     {
175         if ($this->reqFbScripts($action)) {
176             $action->cssLink('plugins/FBConnect/FBConnectPlugin.css');
177         }
178     }
179
180     /**
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
183      * really need them.
184      *
185      * @param Action the action in question
186      *
187      * @return boolean true
188      */
189
190     function reqFbScripts($action) {
191
192         // If you're logged in w/FB Connect, you always need the FB stuff
193
194         $fbuid = $this->loggedIn();
195
196         if (!empty($fbuid)) {
197             return true;
198         }
199
200         // List of actions that require FB stuff
201
202         $needy = array('FBConnectLoginAction',
203                        'FBConnectauthAction',
204                        'FBConnectSettingsAction');
205
206         if (in_array(get_class($action), $needy)) {
207             return true;
208         }
209
210         return false;
211
212     }
213
214     /**
215      * Is the user currently logged in with FB Connect?
216      *
217      * @return mixed $fbuid the Facebook ID of the logged in user, or null
218      */
219
220     function loggedIn()
221     {
222         $user = common_current_user();
223
224         if (!empty($user)) {
225
226             $flink = Foreign_link::getByUserId($user->id,
227                 FACEBOOK_CONNECT_SERVICE);
228             $fbuid = 0;
229
230             if (!empty($flink)) {
231
232                 try {
233
234                     $facebook = getFacebook();
235                     $fbuid    = $facebook->get_loggedin_user();
236
237                 } catch (Exception $e) {
238                     common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
239                         'Problem getting Facebook user: ' .
240                             $e->getMessage());
241                 }
242
243                 if ($fbuid > 0) {
244                     return $fbuid;
245                 }
246             }
247         }
248
249         return null;
250     }
251
252     function onStartPrimaryNav($action)
253     {
254
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';
263         }
264
265         if (!empty($user)) {
266
267             $fbuid = $this->loggedIn();
268
269             if (!empty($fbuid)) {
270
271                 /* Default FB silhouette pic for FB users who haven't
272                    uploaded a profile pic yet. */
273
274                 $silhouetteUrl =
275                     'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
276
277                 $url = $this->getProfilePicURL($fbuid);
278
279                 $action->elementStart('li', array('id' => 'nav_fb'));
280
281                 $action->element('img', array('id' => 'fbc_profile-pic',
282                     'src' => (!empty($url)) ? $url : $silhouetteUrl,
283                     'alt' => 'Facebook Connect User',
284                     'width' => '16'), '');
285
286                 $iconurl =  common_path('plugins/FBConnect/fbfavicon.ico');
287                 $action->element('img', array('id' => 'fb_favicon',
288                     'src' => $iconurl));
289
290                 $action->elementEnd('li');
291
292             }
293         }
294
295         return true;
296     }
297
298     function onStartShowLocalNavBlock($action)
299     {
300         $action_name   = get_class($action);
301
302         $login_actions = array('LoginAction', 'RegisterAction',
303             'OpenidloginAction', 'FBConnectLoginAction');
304
305         if (in_array($action_name, $login_actions)) {
306             $nav = new FBCLoginGroupNav($action);
307             $nav->show();
308             return false;
309         }
310
311         $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
312             'TwittersettingsAction', 'FBConnectSettingsAction');
313
314         if (in_array($action_name, $connect_actions)) {
315             $nav = new FBCSettingsNav($action);
316             $nav->show();
317             return false;
318         }
319
320         return true;
321     }
322
323     function onStartLogout($action)
324     {
325         $action->logout();
326         $fbuid = $this->loggedIn();
327
328         if (!empty($fbuid)) {
329             try {
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: ' .
335                            $e->getMessage());
336             }
337         }
338
339         return true;
340     }
341
342     function getProfilePicURL($fbuid)
343     {
344
345         $facebook = getFacebook();
346         $url      = null;
347
348         try {
349
350             $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
351
352             $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
353
354             if (!empty($result)) {
355                 $url = $result[0]['pic_square'];
356             }
357
358         } catch (Exception $e) {
359             common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
360                        "Facebook client failure requesting profile pic!");
361         }
362
363        return $url;
364
365     }
366
367 }