]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
ae24e7a7316aee51a09f0f110e79bfb56b47149b
[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('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 onStartShowHeader($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             $html = sprintf('<script type="text/javascript">
119                                 $(document).ready(function () {
120                                     FB_RequireFeatures(
121                                         ["XFBML"],
122                                             function() {
123                                                 FB.init("%s", "../xd_receiver.html");
124                                             }
125                                         ); });
126
127                                 function goto_login() {
128                                     window.location = "%s";
129                                 }
130
131                                 function goto_logout() {
132                                     window.location = "%s";
133                                 }
134                               </script>', $apikey,
135                                   $login_url, $logout_url);
136
137             $action->raw($html);
138         }
139
140     }
141
142     // Note: this script needs to appear as close as possible to </body>
143
144     function onEndShowFooter($action)
145     {
146         if ($this->reqFbScripts($action)) {
147             $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
148         }
149     }
150
151     function onEndShowStatusNetStyles($action)
152     {
153
154         if ($this->reqFbScripts($action)) {
155             $action->cssLink('plugins/FBConnect/FBConnectPlugin.css');
156         }
157     }
158
159     /**
160      * Does the Action we're plugged into require the FB Scripts?  We only
161      * want to output FB namespace, scripts, CSS, etc. on the pages that
162      * really need them.
163      *
164      * @param Action the action in question
165      *
166      * @return boolean true
167      */
168
169     function reqFbScripts($action) {
170
171         // If you're logged in w/FB Connect, you always need the FB stuff
172
173         $fbuid = $this->loggedIn();
174
175         if (!empty($fbuid)) {
176             return true;
177         }
178
179         // List of actions that require FB stuff
180
181         $needy = array('FBConnectLoginAction',
182                        'FBConnectauthAction',
183                        'FBConnectSettingsAction');
184
185         if (in_array(get_class($action), $needy)) {
186             return true;
187         }
188
189         return false;
190
191     }
192
193     /**
194      * Is the user currently logged in with FB Connect?
195      *
196      * @return mixed $fbuid the Facebook ID of the logged in user, or null
197      */
198
199     function loggedIn()
200     {
201         $user = common_current_user();
202
203         if (!empty($user)) {
204
205             $flink = Foreign_link::getByUserId($user->id,
206                 FACEBOOK_CONNECT_SERVICE);
207             $fbuid = 0;
208
209             if (!empty($flink)) {
210
211                 try {
212
213                     $facebook = getFacebook();
214                     $fbuid    = $facebook->get_loggedin_user();
215
216                 } catch (Exception $e) {
217                     common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
218                         'Problem getting Facebook user: ' .
219                             $e->getMessage());
220                 }
221
222                 if ($fbuid > 0) {
223                     return $fbuid;
224                 }
225             }
226         }
227
228         return null;
229     }
230
231     function onStartPrimaryNav($action)
232     {
233
234         $user = common_current_user();
235
236         if (!empty($user)) {
237
238             $fbuid = $this->loggedIn();
239
240             if (!empty($fbuid)) {
241
242                 /* Default FB silhouette pic for FB users who haven't
243                    uploaded a profile pic yet. */
244
245                 $silhouetteUrl =
246                     'http://static.ak.fbcdn.net/pics/q_silhouette.gif';
247
248                 $url = $this->getProfilePicURL($fbuid);
249
250                 $action->elementStart('li', array('id' => 'nav_fb'));
251
252                 $action->element('img', array('id' => 'fbc_profile-pic',
253                     'src' => (!empty($url)) ? $url : $silhouetteUrl,
254                     'alt' => 'Facebook Connect User',
255                     'width' => '16'), '');
256
257                 $iconurl =  common_path('plugins/FBConnect/fbfavicon.ico');
258                 $action->element('img', array('id' => 'fb_favicon',
259                     'src' => $iconurl));
260
261                 $action->elementEnd('li');
262
263             }
264
265             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
266                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
267             $action->menuItem(common_local_url('profilesettings'),
268                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
269             if (common_config('xmpp', 'enabled')) {
270                 $action->menuItem(common_local_url('imsettings'),
271                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
272             } else {
273              $action->menuItem(common_local_url('smssettings'),
274                  _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
275             }
276             if (common_config('invite', 'enabled')) {
277                 $action->menuItem(common_local_url('invite'),
278                     _('Invite'),
279                     sprintf(_('Invite friends and colleagues to join you on %s'),
280                     common_config('site', 'name')),
281                     false, 'nav_invitecontact');
282             }
283
284             // Need to override the Logout link to make it do FB stuff
285             if (!empty($fbuid)) {
286
287                 $logout_url = common_local_url('logout');
288                 $title =  _('Logout from the site');
289                 $text = _('Logout');
290
291                 $html = sprintf('<li id="nav_logout"><a href="#" title="%s" ' .
292                     'onclick="FB.Connect.logoutAndRedirect(\'%s\');">%s</a></li>',
293                     $title, $logout_url, $text);
294
295                 $action->raw($html);
296
297              } else {
298                  $action->menuItem(common_local_url('logout'),
299                      _('Logout'), _('Logout from the site'), false, 'nav_logout');
300              }
301          }
302          else {
303              if (!common_config('site', 'closed')) {
304                  $action->menuItem(common_local_url('register'),
305                      _('Register'), _('Create an account'), false, 'nav_register');
306              }
307              $action->menuItem(common_local_url('login'),
308                  _('Login'), _('Login to the site'), false, 'nav_login');
309          }
310
311          $action->menuItem(common_local_url('doc', array('title' => 'help')),
312              _('Help'), _('Help me!'), false, 'nav_help');
313          $action->menuItem(common_local_url('peoplesearch'),
314              _('Search'), _('Search for people or text'), false, 'nav_search');
315
316         return false;
317     }
318
319     function onStartShowLocalNavBlock($action)
320     {
321         $action_name   = get_class($action);
322
323         $login_actions = array('LoginAction', 'RegisterAction',
324             'OpenidloginAction', 'FBConnectLoginAction');
325
326         if (in_array($action_name, $login_actions)) {
327             $nav = new FBCLoginGroupNav($action);
328             $nav->show();
329             return false;
330         }
331
332         $connect_actions = array('SmssettingsAction', 'ImsettingsAction',
333             'TwittersettingsAction', 'FBConnectSettingsAction');
334
335         if (in_array($action_name, $connect_actions)) {
336             $nav = new FBCSettingsNav($action);
337             $nav->show();
338             return false;
339         }
340
341         return true;
342     }
343
344     function onStartLogout($action)
345 {
346         $action->logout();
347         $fbuid = $this->loggedIn();
348
349         if (!empty($fbuid)) {
350             try {
351                 $facebook = getFacebook();
352                 $facebook->expire_session();
353             } catch (Exception $e) {
354                 common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
355                            'Could\'t logout of Facebook: ' .
356                            $e->getMessage());
357             }
358         }
359
360         return true;
361     }
362
363     function getProfilePicURL($fbuid)
364     {
365
366         $facebook = getFacebook();
367         $url      = null;
368
369         try {
370
371             $fqry = 'SELECT pic_square FROM user WHERE uid = %s';
372
373             $result = $facebook->api_client->fql_query(sprintf($fqry, $fbuid));
374
375             if (!empty($result)) {
376                 $url = $result[0]['pic_square'];
377             }
378
379         } catch (Exception $e) {
380             common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
381                        "Facebook client failure requesting profile pic!");
382         }
383
384        return $url;
385
386     }
387
388 }