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/FBConnectLogin.php';
31 require_once INSTALLDIR . '/lib/facebookutil.php';
33 class FBConnectloginAction extends Action
37 var $fb_fields = null;
39 function prepare($args) {
40 parent::prepare($args);
42 $this->fbuid = getFacebook()->get_loggedin_user();
43 $this->fb_fields = $this->getFacebookFields($this->fbuid,
44 array('first_name', 'last_name', 'name'));
49 function handle($args)
51 parent::handle($args);
53 if (common_is_real_login()) {
54 $this->clientError(_('Already logged in.'));
55 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
56 $token = $this->trimmed('token');
57 if (!$token || $token != common_session_token()) {
58 $this->showForm(_('There was a problem with your session token. Try again, please.'));
61 if ($this->arg('create')) {
62 if (!$this->boolean('license')) {
63 $this->showForm(_('You can\'t register if you don\'t agree to the license.'),
64 $this->trimmed('newname'));
67 $this->createNewUser();
68 } else if ($this->arg('connect')) {
71 common_debug(print_r($this->args, true), __FILE__);
72 $this->showForm(_('Something weird happened.'),
73 $this->trimmed('newname'));
80 function showPageNotice()
83 $this->element('div', array('class' => 'error'), $this->error);
85 $this->element('div', 'instructions',
86 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')));
92 return _('Facebook Account Setup');
95 function showForm($error=null, $username=null)
97 $this->error = $error;
98 $this->username = $username;
108 function showContent()
110 if (!empty($this->message_text)) {
111 $this->element('p', null, $this->message);
115 $this->elementStart('form', array('method' => 'post',
116 'id' => 'account_connect',
117 'action' => common_local_url('fbconnectlogin')));
118 $this->hidden('token', common_session_token());
119 $this->element('h2', null,
120 _('Create new account'));
121 $this->element('p', null,
122 _('Create a new user with this nickname.'));
123 $this->input('newname', _('New nickname'),
124 ($this->username) ? $this->username : '',
125 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
126 $this->elementStart('p');
127 $this->element('input', array('type' => 'checkbox',
131 $this->text(_('My text and files are available under '));
132 $this->element('a', array('href' => common_config('license', 'url')),
133 common_config('license', 'title'));
134 $this->text(_(' except this private data: password, email address, IM address, phone number.'));
135 $this->elementEnd('p');
136 $this->submit('create', _('Create'));
137 $this->element('h2', null,
138 _('Connect existing account'));
139 $this->element('p', null,
140 _('If you already have an account, login with your username and password to connect it to your Facebook.'));
141 $this->input('nickname', _('Existing nickname'));
142 $this->password('password', _('Password'));
143 $this->submit('connect', _('Connect'));
144 $this->elementEnd('form');
147 function message($msg)
149 $this->message_text = $msg;
153 function createNewUser()
156 if (common_config('site', 'closed')) {
157 $this->clientError(_('Registration not allowed.'));
163 if (common_config('site', 'inviteonly')) {
164 $code = $_SESSION['invitecode'];
166 $this->clientError(_('Registration not allowed.'));
170 $invite = Invitation::staticGet($code);
172 if (empty($invite)) {
173 $this->clientError(_('Not a valid invitation code.'));
178 $nickname = $this->trimmed('newname');
180 if (!Validate::string($nickname, array('min_length' => 1,
182 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
183 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
187 if (!User::allowed_nickname($nickname)) {
188 $this->showForm(_('Nickname not allowed.'));
192 if (User::staticGet('nickname', $nickname)) {
193 $this->showForm(_('Nickname already in use. Try another one.'));
197 $fullname = trim($this->fb_fields['firstname'] .
198 ' ' . $this->fb_fields['lastname']);
200 $args = array('nickname' => $nickname, 'fullname' => $fullname);
202 if (!empty($invite)) {
203 $args['code'] = $invite->code;
206 $user = User::register($args);
208 $result = $this->flinkUser($user->id, $this->fbuid);
211 $this->serverError(_('Error connecting user to Facebook.'));
215 common_set_user($user);
216 common_real_login(true);
218 common_debug("Registered new user $user->id from Facebook user $this->fbuid");
220 common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
224 function connectUser()
226 $nickname = $this->trimmed('nickname');
227 $password = $this->trimmed('password');
229 if (!common_check_user($nickname, $password)) {
230 $this->showForm(_('Invalid username or password.'));
234 $user = User::staticGet('nickname', $nickname);
237 common_debug("Legit user to connect to Facebook: $nickname");
240 $result = $this->flinkUser($user->id, $this->fbuid);
243 $this->serverError(_('Error connecting user to Facebook.'));
247 common_debug("Connected Facebook user $this->fbuid to local user $user->id");
249 common_set_user($user);
250 common_real_login(true);
252 $this->goHome($user->nickname);
257 common_debug("Trying Facebook Login...");
259 $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
262 $user = $flink->getUser();
266 common_debug("Logged in Facebook user $flink->foreign_id as user $user->id ($user->nickname)");
268 common_set_user($user);
269 common_real_login(true);
270 $this->goHome($user->nickname);
274 $this->showForm(null, $this->bestNewNickname());
278 function goHome($nickname)
280 $url = common_get_returnto();
282 // We don't have to return to it again
283 common_set_returnto(null);
285 $url = common_local_url('all',
290 common_redirect($url, 303);
293 function flinkUser($user_id, $fbuid)
295 $flink = new Foreign_link();
296 $flink->user_id = $user_id;
297 $flink->foreign_id = $fbuid;
298 $flink->service = FACEBOOK_SERVICE;
299 $flink->created = common_sql_now();
301 $flink_id = $flink->insert();
306 function bestNewNickname()
308 if (!empty($this->fb_fields['name'])) {
309 $nickname = $this->nicknamize($this->fb_fields['name']);
310 if ($this->isNewNickname($nickname)) {
317 $fullname = trim($this->fb_fields['firstname'] .
318 ' ' . $this->fb_fields['lastname']);
320 if (!empty($fullname)) {
321 $fullname = $this->nicknamize($fullname);
322 if ($this->isNewNickname($fullname)) {
330 // Given a string, try to make it work as a nickname
332 function nicknamize($str)
334 $str = preg_replace('/\W/', '', $str);
335 return strtolower($str);
338 function isNewNickname($str)
340 if (!Validate::string($str, array('min_length' => 1,
342 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
345 if (!User::allowed_nickname($str)) {
348 if (User::staticGet('nickname', $str)) {
354 // XXX: Consider moving this to lib/facebookutil.php
355 function getFacebookFields($fb_uid, $fields) {
357 $infos = getFacebook()->api_client->users_getInfo($fb_uid, $fields);
362 return reset($infos);
364 } catch (Exception $e) {
365 error_log("Failure in the api when requesting " . join(",", $fields)
366 ." on uid " . $fb_uid . " : ". $e->getMessage());