]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FBConnect/FBConnectPlugin.php
Added configurable options for attachments: supported mimetypes and quotas for uploads.
[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  * Plugin to enable Facebook Connect
45  *
46  * @category Plugin
47  * @package  Laconica
48  * @author   Zach Copley <zach@controlyourself.ca>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://laconi.ca/
51  */
52
53 class FBConnectPlugin extends Plugin
54 {
55     function __construct()
56     {
57         parent::__construct();
58     }
59
60     // Hook in new actions
61     function onRouterInitialized(&$m) {
62         $m->connect('main/facebookconnect', array('action' => 'FBConnectAuth'));
63         $m->connect('main/facebooklogin', array('action' => 'FBConnectLogin'));
64         $m->connect('settings/facebook', array('action' => 'FBConnectSettings'));
65      }
66
67     // Add in xmlns:fb
68     function onStartShowHTML($action)
69     {
70         $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
71         $_SERVER['HTTP_ACCEPT'] : null;
72
73         // XXX: allow content negotiation for RDF, RSS, or XRDS
74
75         $cp = common_accept_to_prefs($httpaccept);
76         $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
77
78         $type = common_negotiate_type($cp, $sp);
79
80         if (!$type) {
81             throw new ClientException(_('This page is not available in a '.
82                                          'media type you accept'), 406);
83         }
84
85         header('Content-Type: '.$type);
86
87         $action->extraHeaders();
88
89         $action->startXML('html',
90             '-//W3C//DTD XHTML 1.0 Strict//EN',
91             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
92
93         $language = $action->getLanguage();
94
95         $action->elementStart('html',
96             array('xmlns'  => 'http://www.w3.org/1999/xhtml',
97                   'xmlns:fb' => 'http://www.facebook.com/2008/fbml',
98                   'xml:lang' => $language,
99                   'lang'     => $language));
100
101         return false;
102     }
103
104     function onEndShowLaconicaScripts($action)
105     {
106         $action->element('script',
107             array('type' => 'text/javascript',
108                   'src'  => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
109                   ' ');
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         $html = sprintf('<script type="text/javascript">FB.init("%s", "%s/xd_receiver.htm");
118
119                             function goto_login() {
120                                 window.location = "%s";
121                             }
122
123                             function goto_logout() {
124                                 window.location = "%s";
125                             }
126
127                          </script>', $apikey, $plugin_path, $login_url, $logout_url);
128
129
130         $action->raw($html);
131     }
132
133     function onEndShowLaconicaStyles($action)
134     {
135         $action->element('link', array('rel' => 'stylesheet',
136             'type' => 'text/css',
137             'href' => common_path('plugins/FBConnect/FBConnectPlugin.css')));
138     }
139
140     function onStartPrimaryNav($action)
141     {
142         $user = common_current_user();
143
144         if ($user) {
145
146             $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
147                 _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
148             $action->menuItem(common_local_url('profilesettings'),
149                 _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
150             if (common_config('xmpp', 'enabled')) {
151                 $action->menuItem(common_local_url('imsettings'),
152                     _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
153             } else {
154              $action->menuItem(common_local_url('smssettings'),
155                  _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
156             }
157             $action->menuItem(common_local_url('invite'),
158                 _('Invite'),
159                 sprintf(_('Invite friends and colleagues to join you on %s'),
160                 common_config('site', 'name')),
161                 false, 'nav_invitecontact');
162
163             $flink = Foreign_link::getByUserId($user->id, FACEBOOK_CONNECT_SERVICE);
164             $fbuid = 0;
165
166             if ($flink) {
167
168                 try {
169
170                     $facebook = getFacebook();
171                     $fbuid = getFacebook()->get_loggedin_user();
172
173                 } catch (Exception $e) {
174                     common_log(LOG_WARNING,
175                         'Problem getting Facebook client: ' .
176                             $e->getMessage());
177                 }
178
179                 // Display Facebook Logged in indicator w/Facebook favicon
180
181                 if ($fbuid > 0) {
182
183                     $action->elementStart('li', array('id' => 'nav_fb'));
184                     $action->elementStart('fb:profile-pic', array('uid' => $flink->foreign_id,
185                         'linked' => 'false',
186                         'width' => 16,
187                         'height' => 16));
188                     $action->elementEnd('fb:profile-pic');
189
190                     $iconurl =  common_path('/plugins/FBConnect/fbfavicon.ico');
191                     $action->element('img', array('src' => $iconurl));
192
193                     $action->elementEnd('li');
194
195                 }
196             }
197
198             // Need to override the Logout link to make it do FB stuff
199
200             if ($fbuid > 0) {
201
202                 $logout_url = common_local_url('logout');
203                 $title =  _('Logout from the site');
204                 $text = _('Logout');
205
206                 $html = sprintf('<li id="nav_logout"><a href="%s" title="%s" ' .
207                     'onclick="FB.Connect.logout(function() { goto_logout() })">%s</a></li>',
208                     $logout_url, $title, $text);
209
210                 $action->raw($html);
211
212              } else {
213                  $action->menuItem(common_local_url('logout'),
214                      _('Logout'), _('Logout from the site'), false, 'nav_logout');
215              }
216          }
217          else {
218              if (!common_config('site', 'closed')) {
219                  $action->menuItem(common_local_url('register'),
220                      _('Register'), _('Create an account'), false, 'nav_register');
221              }
222              $action->menuItem(common_local_url('openidlogin'),
223                  _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
224              $action->menuItem(common_local_url('login'),
225                  _('Login'), _('Login to the site'), false, 'nav_login');
226          }
227
228          $action->menuItem(common_local_url('doc', array('title' => 'help')),
229              _('Help'), _('Help me!'), false, 'nav_help');
230          $action->menuItem(common_local_url('peoplesearch'),
231              _('Search'), _('Search for people or text'), false, 'nav_search');
232
233         return false;
234     }
235
236     function onStartShowLocalNavBlock($action)
237     {
238         $action_name = get_class($action);
239
240         $login_actions = array('LoginAction', 'RegisterAction',
241             'OpenidloginAction', 'FBConnectLoginAction');
242
243         if (in_array($action_name, $login_actions)) {
244             $nav = new FBCLoginGroupNav($action);
245             $nav->show();
246             return false;
247         }
248
249         $connect_actions = array('SmssettingsAction',
250             'TwittersettingsAction', 'FBConnectSettingsAction');
251
252         if (in_array($action_name, $connect_actions)) {
253             $nav = new FBCSettingsNav($action);
254             $nav->show();
255             return false;
256         }
257
258         return true;
259     }
260
261     function onEndLogout($action)
262     {
263         try {
264
265             $facebook = getFacebook();
266             $fbuid = $facebook->get_loggedin_user();
267
268             if ($fbuid > 0) {
269                 $facebook->logout(common_local_url('public'));
270             }
271
272         } catch (Exception $e) {
273             common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
274                 $e->getMessage());
275         }
276     }
277
278 }