]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
Merge branch '0.8.x' into fbconnect
[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 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectLogin.php';
35 require_once INSTALLDIR . '/lib/facebookutil.php';
36
37 /**
38  * Plugin to enable Facebook Connect
39  *
40  * @category Plugin
41  * @package  Laconica
42  * @author   Zach Copley <zach@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
45  */
46
47 class FBConnectPlugin extends Plugin
48 {
49
50     function __construct()
51     {
52         parent::__construct();
53     }
54
55     // Hook in new actions
56     function onRouterInitialized(&$m) {
57         $m->connect('main/facebookconnect', array('action' => 'fbconnectlogin'));
58      }
59
60     // Add in xmlns:fb
61     function onStartShowHTML($action)
62     {
63
64         // XXX: This is probably a bad place to do general processing
65         // so maybe I need to make some new events?  Maybe in
66         // Action::prepare?
67
68         $name = get_class($action);
69
70         common_debug("action: $name");
71
72         // Avoid a redirect loop
73         if ($name != 'FBConnectloginAction') {
74
75             $this->checkFacebookUser($action);
76
77         }
78
79         $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
80         $_SERVER['HTTP_ACCEPT'] : null;
81
82         // XXX: allow content negotiation for RDF, RSS, or XRDS
83
84         $cp = common_accept_to_prefs($httpaccept);
85         $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
86
87         $type = common_negotiate_type($cp, $sp);
88
89         if (!$type) {
90             throw new ClientException(_('This page is not available in a '.
91                                          'media type you accept'), 406);
92         }
93
94
95         header('Content-Type: '.$type);
96
97         $action->extraHeaders();
98
99         $action->startXML('html',
100                          '-//W3C//DTD XHTML 1.0 Strict//EN',
101                          'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
102
103         $language = $action->getLanguage();
104
105         $action->elementStart('html', array('xmlns'  => 'http://www.w3.org/1999/xhtml',
106                                             'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
107                                             'xml:lang' => $language,
108                                             'lang'     => $language));
109
110         return false;
111
112     }
113
114     function onEndShowLaconicaScripts($action)
115     {
116
117         $action->element('script',
118             array('type' => 'text/javascript',
119                   'src'  => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
120                   ' ');
121
122         $apikey = common_config('facebook', 'apikey');
123         $plugin_path = common_path('plugins/FBConnect');
124
125         $login_url = common_get_returnto() || common_local_url('public');
126
127         $html = sprintf('<script type="text/javascript">FB.init("%s", "%s/xd_receiver.htm");
128
129                             function refresh_page() {
130                                 window.location = "%s";
131                             }
132
133                          </script>', $apikey, $plugin_path, $login_url);
134
135
136         $action->raw($html);
137     }
138
139     function onStartPrimaryNav($action)
140     {
141         $user = common_current_user();
142
143         if ($user) {
144              $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
145                              _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
146              $action->menuItem(common_local_url('profilesettings'),
147                              _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
148              if (common_config('xmpp', 'enabled')) {
149                  $action->menuItem(common_local_url('imsettings'),
150                                  _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
151              } else {
152                  $action->menuItem(common_local_url('smssettings'),
153                                  _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
154              }
155              $action->menuItem(common_local_url('invite'),
156                               _('Invite'),
157                               sprintf(_('Invite friends and colleagues to join you on %s'),
158                               common_config('site', 'name')),
159                               false, 'nav_invitecontact');
160
161              // Need to override the Logout link to make it do FB stuff
162
163              $logout_url = common_local_url('logout');
164              $title =  _('Logout from the site');
165              $text = _('Logout');
166
167              $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
168                  'onclick="FB.Connect.logoutAndRedirect(\'%s\')">%s</a></li>',
169                     $logout_url, $title, $logout_url, $text);
170
171              $action->raw($html);
172
173          }
174          else {
175              if (!common_config('site', 'closed')) {
176                  $action->menuItem(common_local_url('register'),
177                                  _('Register'), _('Create an account'), false, 'nav_register');
178              }
179              $action->menuItem(common_local_url('openidlogin'),
180                              _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
181              $action->menuItem(common_local_url('login'),
182                              _('Login'), _('Login to the site'), false, 'nav_login');
183          }
184
185          $action->menuItem(common_local_url('doc', array('title' => 'help')),
186                          _('Help'), _('Help me!'), false, 'nav_help');
187          $action->menuItem(common_local_url('peoplesearch'),
188                          _('Search'), _('Search for people or text'), false, 'nav_search');
189
190         // Tack on "Connect with Facebook" button
191
192         // XXX: Maybe this looks bad and should not go here.  Where should it go?
193
194         if (!$user) {
195              $action->elementStart('li');
196              $action->element('fb:login-button', array('onlogin' => 'refresh_page()',
197                  'length' => 'long'));
198              $action->elementEnd('li');
199         }
200
201         return false;
202     }
203
204     function checkFacebookUser() {
205
206         try {
207
208             $facebook = getFacebook();
209             $fbuid = $facebook->get_loggedin_user();
210             $user = common_current_user();
211
212             // If you're a Facebook user and you're logged in do nothing
213
214             // If you're a Facebook user and you're not logged in
215             // redirect to Facebook connect login page because that means you have clicked
216             // the 'connect with Facebook' button and have cookies
217
218             if ($fbuid > 0) {
219
220                 if ($facebook->api_client->users_isAppUser($fbuid) ||
221                     $facebook->api_client->added) {
222
223                     // user should be connected...
224
225                     common_debug("Facebook user found: $fbuid");
226
227                     if ($user) {
228                         common_debug("Facebook user is logged in.");
229                         return;
230
231                     } else {
232                         common_debug("Facebook user is NOT logged in.");
233                         common_redirect(common_local_url('fbconnectlogin'), 303);
234                     }
235
236                 } else {
237                     common_debug("No Facebook connect user found.");
238                 }
239             }
240
241         } catch (Exception $e) {
242             common_debug('Expired FB session.');
243         }
244
245     }
246
247     function onStartLogout($action)
248     {
249         common_debug("onEndLogout()");
250
251         common_set_user(null);
252         common_real_login(false); // not logged in
253         common_forgetme(); // don't log back in!
254
255         try {
256
257             $facebook = getFacebook();
258             $fbuid = $facebook->get_loggedin_user();
259
260             // XXX: ARGGGH this doesn't work right!
261
262             if ($fbuid) {
263                 $facebook->expire_session();
264                 $facebook->logout(common_local_url('public'));
265               }
266
267         } catch (Exception $e) {
268             common_debug('Problem expiring FB session');
269         }
270
271         common_debug("logged out.");
272
273         return false;
274     }
275
276 }
277
278