3 * Laconica, the distributed open-source microblogging tool
5 * Plugin to enable Facebook Connect
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.
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.
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/>.
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/
30 require_once INSTALLDIR . '/plugins/FBConnect/FBConnectPlugin.php';
32 class FBConnectauthAction extends Action
36 var $fb_fields = null;
38 function prepare($args) {
39 parent::prepare($args);
43 $this->fbuid = getFacebook()->get_loggedin_user();
45 if ($this->fbuid > 0) {
46 $this->fb_fields = $this->getFacebookFields($this->fbuid,
47 array('first_name', 'last_name', 'name'));
49 common_debug("No Facebook User found.");
52 } catch (Exception $e) {
53 common_log(LOG_WARNING, 'Problem getting Facebook uid: ' .
60 function handle($args)
62 parent::handle($args);
64 if (common_is_real_login()) {
65 $this->clientError(_('Already logged in.'));
66 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
68 $token = $this->trimmed('token');
69 if (!$token || $token != common_session_token()) {
70 $this->showForm(_('There was a problem with your session token. Try again, please.'));
73 if ($this->arg('create')) {
74 if (!$this->boolean('license')) {
75 $this->showForm(_('You can\'t register if you don\'t agree to the license.'),
76 $this->trimmed('newname'));
79 $this->createNewUser();
80 } else if ($this->arg('connect')) {
83 common_debug(print_r($this->args, true), __FILE__);
84 $this->showForm(_('Something weird happened.'),
85 $this->trimmed('newname'));
92 function showPageNotice()
95 $this->element('div', array('class' => 'error'), $this->error);
97 $this->element('div', 'instructions',
98 sprintf(_('This is the first time you\'ve logged into %s so we must connect your Facebook to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name')));
104 return _('Facebook Account Setup');
107 function showForm($error=null, $username=null)
109 $this->error = $error;
110 $this->username = $username;
120 function showContent()
122 if (!empty($this->message_text)) {
123 $this->element('p', null, $this->message);
127 $this->elementStart('form', array('method' => 'post',
128 'id' => 'form_settings_facebook_connect',
129 'class' => 'form_settings',
130 'action' => common_local_url('FBConnectAuth')));
131 $this->elementStart('fieldset', array('id' => 'settings_facebook_connect_options'));
132 $this->element('legend', null, _('Connection options'));
133 $this->elementStart('ul', 'form_data');
134 $this->elementStart('li');
135 $this->element('input', array('type' => 'checkbox',
137 'class' => 'checkbox',
140 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
141 $this->text(_('My text and files are available under '));
142 $this->element('a', array('href' => common_config('license', 'url')),
143 common_config('license', 'title'));
144 $this->text(_(' except this private data: password, email address, IM address, phone number.'));
145 $this->elementEnd('label');
146 $this->elementEnd('li');
147 $this->elementEnd('ul');
149 $this->elementStart('fieldset');
150 $this->hidden('token', common_session_token());
151 $this->element('legend', null,
152 _('Create new account'));
153 $this->element('p', null,
154 _('Create a new user with this nickname.'));
155 $this->elementStart('ul', 'form_data');
156 $this->elementStart('li');
157 $this->input('newname', _('New nickname'),
158 ($this->username) ? $this->username : '',
159 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
160 $this->elementEnd('li');
161 $this->elementEnd('ul');
162 $this->submit('create', _('Create'));
163 $this->elementEnd('fieldset');
165 $this->elementStart('fieldset');
166 $this->element('legend', null,
167 _('Connect existing account'));
168 $this->element('p', null,
169 _('If you already have an account, login with your username and password to connect it to your Facebook.'));
170 $this->elementStart('ul', 'form_data');
171 $this->elementStart('li');
172 $this->input('nickname', _('Existing nickname'));
173 $this->elementEnd('li');
174 $this->elementStart('li');
175 $this->password('password', _('Password'));
176 $this->elementEnd('li');
177 $this->elementEnd('ul');
178 $this->submit('connect', _('Connect'));
179 $this->elementEnd('fieldset');
181 $this->elementEnd('fieldset');
182 $this->elementEnd('form');
185 function message($msg)
187 $this->message_text = $msg;
191 function createNewUser()
194 if (common_config('site', 'closed')) {
195 $this->clientError(_('Registration not allowed.'));
201 if (common_config('site', 'inviteonly')) {
202 $code = $_SESSION['invitecode'];
204 $this->clientError(_('Registration not allowed.'));
208 $invite = Invitation::staticGet($code);
210 if (empty($invite)) {
211 $this->clientError(_('Not a valid invitation code.'));
216 $nickname = $this->trimmed('newname');
218 if (!Validate::string($nickname, array('min_length' => 1,
220 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
221 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
225 if (!User::allowed_nickname($nickname)) {
226 $this->showForm(_('Nickname not allowed.'));
230 if (User::staticGet('nickname', $nickname)) {
231 $this->showForm(_('Nickname already in use. Try another one.'));
235 $fullname = trim($this->fb_fields['firstname'] .
236 ' ' . $this->fb_fields['lastname']);
238 $args = array('nickname' => $nickname, 'fullname' => $fullname);
240 if (!empty($invite)) {
241 $args['code'] = $invite->code;
244 $user = User::register($args);
246 $result = $this->flinkUser($user->id, $this->fbuid);
249 $this->serverError(_('Error connecting user to Facebook.'));
253 common_set_user($user);
254 common_real_login(true);
256 common_debug("Registered new user $user->id from Facebook user $this->fbuid");
258 common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
262 function connectUser()
264 $nickname = $this->trimmed('nickname');
265 $password = $this->trimmed('password');
267 if (!common_check_user($nickname, $password)) {
268 $this->showForm(_('Invalid username or password.'));
272 $user = User::staticGet('nickname', $nickname);
275 common_debug("Legit user to connect to Facebook: $nickname");
278 $result = $this->flinkUser($user->id, $this->fbuid);
281 $this->serverError(_('Error connecting user to Facebook.'));
285 common_debug("Connected Facebook user $this->fbuid to local user $user->id");
287 common_set_user($user);
288 common_real_login(true);
290 $this->goHome($user->nickname);
295 common_debug("Trying Facebook Login...");
297 $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_CONNECT_SERVICE);
300 $user = $flink->getUser();
304 common_debug("Logged in Facebook user $flink->foreign_id as user $user->id ($user->nickname)");
306 common_set_user($user);
307 common_real_login(true);
308 $this->goHome($user->nickname);
313 common_debug("No flink found for fbuid: $this->fbuid");
315 $this->showForm(null, $this->bestNewNickname());
319 function goHome($nickname)
321 $url = common_get_returnto();
323 // We don't have to return to it again
324 common_set_returnto(null);
326 $url = common_local_url('all',
331 common_redirect($url, 303);
334 function flinkUser($user_id, $fbuid)
336 $flink = new Foreign_link();
337 $flink->user_id = $user_id;
338 $flink->foreign_id = $fbuid;
339 $flink->service = FACEBOOK_CONNECT_SERVICE;
340 $flink->created = common_sql_now();
342 $flink_id = $flink->insert();
347 function bestNewNickname()
349 if (!empty($this->fb_fields['name'])) {
350 $nickname = $this->nicknamize($this->fb_fields['name']);
351 if ($this->isNewNickname($nickname)) {
358 $fullname = trim($this->fb_fields['firstname'] .
359 ' ' . $this->fb_fields['lastname']);
361 if (!empty($fullname)) {
362 $fullname = $this->nicknamize($fullname);
363 if ($this->isNewNickname($fullname)) {
371 // Given a string, try to make it work as a nickname
373 function nicknamize($str)
375 $str = preg_replace('/\W/', '', $str);
376 return strtolower($str);
379 function isNewNickname($str)
381 if (!Validate::string($str, array('min_length' => 1,
383 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
386 if (!User::allowed_nickname($str)) {
389 if (User::staticGet('nickname', $str)) {
395 // XXX: Consider moving this to lib/facebookutil.php
396 function getFacebookFields($fb_uid, $fields) {
399 $facebook = getFacebook();
401 $infos = $facebook->api_client->users_getInfo($fb_uid, $fields);
406 return reset($infos);
408 } catch (Exception $e) {
409 common_log(LOG_WARNING, "Facebook client failure when requesting " .
410 join(",", $fields) . " on uid " . $fb_uid .
411 " : ". $e->getMessage());