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