]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
- Reworked login / logout workflow
[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
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
57     function __construct()
58     {
59         parent::__construct();
60     }
61
62     // Hook in new actions
63     function onRouterInitialized(&$m) {
64         $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
65         $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
66         $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
67      }
68
69     // Add in xmlns:fb
70     function onStartShowHTML($action)
71     {
72         $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
73         $_SERVER['HTTP_ACCEPT'] : null;
74
75         // XXX: allow content negotiation for RDF, RSS, or XRDS
76
77         $cp = common_accept_to_prefs($httpaccept);
78         $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
79
80         $type = common_negotiate_type($cp, $sp);
81
82         if (!$type) {
83             throw new ClientException(_('This page is not available in a '.
84                                          'media type you accept'), 406);
85         }
86
87
88         header('Content-Type: '.$type);
89
90         $action->extraHeaders();
91
92         $action->startXML('html',
93                          '-//W3C//DTD XHTML 1.0 Strict//EN',
94                          'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
95
96         $language = $action->getLanguage();
97
98         $action->elementStart('html', array('xmlns'  => 'http://www.w3.org/1999/xhtml',
99                                             'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
100                                             'xml:lang' => $language,
101                                             'lang'     => $language));
102
103         return false;
104
105     }
106
107     function onEndShowLaconicaScripts($action)
108     {
109
110         $action->element('script',
111             array('type' => 'text/javascript',
112                   'src'  => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
113                   ' ');
114
115         $apikey = common_config('facebook', 'apikey');
116         $plugin_path = common_path('plugins/FBConnect');
117
118         $login_url = common_local_url('FBConnectAuth');
119         $logout_url = common_local_url('logout');
120
121         $html = sprintf('<script type="text/javascript">FB.init("%s", "%s/xd_receiver.htm");
122
123                             function goto_login() {
124                                 window.location = "%s";
125                             }
126
127                             function goto_logout() {
128                                 window.location = "%s";
129                             }
130
131                          </script>', $apikey, $plugin_path, $login_url, $logout_url);
132
133
134         $action->raw($html);
135     }
136
137     function onStartPrimaryNav($action)
138     {
139         $user = common_current_user();
140
141         if ($user) {
142
143             $flink = Foreign_link::getByUserId($user->id, FACEBOOK_CONNECT_SERVICE);
144
145             if ($flink) {
146
147                 $facebook = getFacebook();
148
149                 if ($facebook->api_client->users_isAppUser($flink->foreign_id) ||
150                     $facebook->api_client->added) {
151
152                     // XXX: We need to replace this with a proper mini-icon and only after
153                     // checing the FB Connect JavaScript lib method to see what the Connect
154                     // status is. Checking Connect status looks to be impossible with the
155                     // PHP client.
156
157                     $action->elementStart('li');
158                     $action->elementStart('fb:profile-pic', array('uid' => $flink->foreign_id,
159                         'facebook-logo' => 'true',
160                         'linked' => 'false',
161                         'width' => 32,
162                         'height' => 32));
163                     $action->elementEnd('fb:profile-pic');
164                     $action->elementEnd('li');
165                 }
166
167             }
168             
169             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
170                          _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
171             $action->menuItem(common_local_url('profilesettings'),
172                          _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
173             if (common_config('xmpp', 'enabled')) {
174              $action->menuItem(common_local_url('imsettings'),
175                              _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
176             } else {
177              $action->menuItem(common_local_url('smssettings'),
178                              _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
179             }
180             $action->menuItem(common_local_url('invite'),
181                           _('Invite'),
182                           sprintf(_('Invite friends and colleagues to join you on %s'),
183                           common_config('site', 'name')),
184                           false, 'nav_invitecontact');
185
186              // Need to override the Logout link to make it do FB stuff
187
188              $logout_url = common_local_url('logout');
189              $title =  _('Logout from the site');
190              $text = _('Logout');
191
192              $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
193                  'onclick="FB.Connect.logout(function() { goto_logout() })">%s</a></li>',
194                     $logout_url, $title, $text);
195
196              $action->raw($html);
197
198          }
199          else {
200              if (!common_config('site', 'closed')) {
201                  $action->menuItem(common_local_url('register'),
202                                  _('Register'), _('Create an account'), false, 'nav_register');
203              }
204              $action->menuItem(common_local_url('openidlogin'),
205                              _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
206              $action->menuItem(common_local_url('login'),
207                              _('Login'), _('Login to the site'), false, 'nav_login');
208          }
209
210          $action->menuItem(common_local_url('doc', array('title' => 'help')),
211                          _('Help'), _('Help me!'), false, 'nav_help');
212          $action->menuItem(common_local_url('peoplesearch'),
213                          _('Search'), _('Search for people or text'), false, 'nav_search');
214
215         return false;
216     }
217
218     function onStartShowLocalNavBlock($action)
219     {
220         $action_name = get_class($action);
221
222         $login_actions = array('LoginAction', 'RegisterAction',
223             'OpenidloginAction', 'FacebookStart');
224
225         if (in_array($action_name, $login_actions)) {
226             $nav = new FBCLoginGroupNav($action);
227             $nav->show();
228             return false;
229         }
230
231         $connect_actions = array('SmssettingsAction',
232             'TwittersettingsAction', 'FBConnectSettingsAction');
233
234         if (in_array($action_name, $connect_actions)) {
235             $nav = new FBCSettingsNav($action);
236             $nav->show();
237             return false;
238         }
239
240         return true;
241     }
242
243 }
244
245