]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
Merge branch '0.8.x' into invite-enabled
[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         // XXX: Horrible hack to make Safari, FF2, and Chrome work with
73         // Facebook Connect. These browser cannot use Facebook's
74         // DOM parsing routines unless the mime type of the page is
75         // text/html even though Facebook Connect uses XHTML.  This is
76         // A bug in Facebook Connect, and this is a temporary solution
77         // until they fix their JavaScript libs.
78         header('Content-Type: text/html');
79
80         $action->extraHeaders();
81
82         $action->startXML('html',
83             '-//W3C//DTD XHTML 1.0 Strict//EN',
84             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
85
86         $language = $action->getLanguage();
87
88         $action->elementStart('html',
89             array('xmlns'  => 'http://www.w3.org/1999/xhtml',
90                   'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
91                   'xml:lang' => $language,
92                   'lang'     => $language));
93
94         return false;
95     }
96
97     // Note: this script needs to appear in the <body>
98
99     function onStartShowHeader($action)
100     {
101         $apikey = common_config('facebook', 'apikey');
102         $plugin_path = common_path('plugins/FBConnect');
103
104         $login_url = common_local_url('FBConnectAuth');
105         $logout_url = common_local_url('logout');
106
107         // XXX: Facebook says we don't need this FB_RequireFeatures(),
108         // but we actually do, for IE and Safari. Gar.
109
110         $html = sprintf('<script type="text/javascript">
111                             window.onload = function () {
112                                 FB_RequireFeatures(
113                                     ["XFBML"],
114                                         function() {
115                                             FB.Facebook.init("%s", "../xd_receiver.html");
116                                         }
117                                     ); }
118
119                             function goto_login() {
120                                 window.location = "%s";
121                             }
122
123                             function goto_logout() {
124                                 window.location = "%s";
125                             }
126                           </script>', $apikey,
127                               $login_url, $logout_url);
128
129         $action->raw($html);
130     }
131
132     // Note: this script needs to appear as close as possible to </body>
133
134     function onEndShowFooter($action)
135     {
136
137         $action->element('script',
138             array('type' => 'text/javascript',
139                   'src'  => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
140                   '');
141     }
142
143     function onEndShowLaconicaStyles($action)
144     {
145         $action->element('link', array('rel' => 'stylesheet',
146             'type' => 'text/css',
147             'href' => common_path('plugins/FBConnect/FBConnectPlugin.css')));
148     }
149
150     function onStartPrimaryNav($action)
151     {
152         $user = common_current_user();
153
154         if ($user) {
155
156             $flink = Foreign_link::getByUserId($user->id,
157                 FACEBOOK_CONNECT_SERVICE);
158             $fbuid = 0;
159
160             if ($flink) {
161
162                 try {
163
164                     $facebook = getFacebook();
165                     $fbuid = getFacebook()->get_loggedin_user();
166
167                 } catch (Exception $e) {
168                     common_log(LOG_WARNING,
169                         'Problem getting Facebook client: ' .
170                             $e->getMessage());
171                 }
172
173                 // Display Facebook Logged in indicator w/Facebook favicon
174
175                 if ($fbuid > 0) {
176
177                     $action->elementStart('li', array('id' => 'nav_fb'));
178                     $action->elementStart('fb:profile-pic', array('uid' => $flink->foreign_id,
179                         'linked' => 'false',
180                         'width' => 16,
181                         'height' => 16));
182                     $action->elementEnd('fb:profile-pic');
183
184                     $iconurl =  common_path('/plugins/FBConnect/fbfavicon.ico');
185                     $action->element('img', array('src' => $iconurl));
186
187                     $action->elementEnd('li');
188
189                 }
190             }
191
192             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
193                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
194             $action->menuItem(common_local_url('profilesettings'),
195                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
196             if (common_config('xmpp', 'enabled')) {
197                 $action->menuItem(common_local_url('imsettings'),
198                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
199             } else {
200              $action->menuItem(common_local_url('smssettings'),
201                  _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
202             }
203             if (common_config('invite', 'enabled')) {
204                 $action->menuItem(common_local_url('invite'),
205                     _('Invite'),
206                     sprintf(_('Invite friends and colleagues to join you on %s'),
207                     common_config('site', 'name')),
208                     false, 'nav_invitecontact');
209             }
210
211             // Need to override the Logout link to make it do FB stuff
212             if ($flink && $fbuid > 0) {
213
214                 $logout_url = common_local_url('logout');
215                 $title =  _('Logout from the site');
216                 $text = _('Logout');
217
218                 $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
219                     'onclick="FB.Connect.logout(function() { goto_logout() })">%s</a></li>',
220                     $logout_url, $title, $text);
221
222                 $action->raw($html);
223
224              } else {
225                  $action->menuItem(common_local_url('logout'),
226                      _('Logout'), _('Logout from the site'), false, 'nav_logout');
227              }
228          }
229          else {
230              if (!common_config('site', 'closed')) {
231                  $action->menuItem(common_local_url('register'),
232                      _('Register'), _('Create an account'), false, 'nav_register');
233              }
234              $action->menuItem(common_local_url('openidlogin'),
235                  _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
236              $action->menuItem(common_local_url('login'),
237                  _('Login'), _('Login to the site'), false, 'nav_login');
238          }
239
240          $action->menuItem(common_local_url('doc', array('title' => 'help')),
241              _('Help'), _('Help me!'), false, 'nav_help');
242          $action->menuItem(common_local_url('peoplesearch'),
243              _('Search'), _('Search for people or text'), false, 'nav_search');
244
245         return false;
246     }
247
248     function onStartShowLocalNavBlock($action)
249     {
250         $action_name = get_class($action);
251
252         $login_actions = array('LoginAction', 'RegisterAction',
253             'OpenidloginAction', 'FBConnectLoginAction');
254
255         if (in_array($action_name, $login_actions)) {
256             $nav = new FBCLoginGroupNav($action);
257             $nav->show();
258             return false;
259         }
260
261         $connect_actions = array('SmssettingsAction',
262             'TwittersettingsAction', 'FBConnectSettingsAction');
263
264         if (in_array($action_name, $connect_actions)) {
265             $nav = new FBCSettingsNav($action);
266             $nav->show();
267             return false;
268         }
269
270         return true;
271     }
272
273     function onStartLogout($action)
274     {
275         $user = common_current_user();
276
277         $flink = Foreign_link::getByUserId($user->id, FACEBOOK_CONNECT_SERVICE);
278
279         $action->logout();
280
281         if ($flink) {
282
283             $facebook = getFacebook();
284
285             try {
286                 $fbuid = $facebook->get_loggedin_user();
287
288                 if ($fbuid > 0) {
289                     $facebook->logout(common_local_url('public'));
290                 }
291
292             } catch (Exception $e) {
293                 common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
294                     $e->getMessage());
295             }
296         }
297
298         return true;
299     }
300
301 }