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_facebook_account_connect',
129 'class' => 'form_settings',
130 'action' => common_local_url('FBConnectAuth')));
131 $this->elementStart('fieldset');
132 $this->hidden('token', common_session_token());
133 $this->element('legend', null,
134 _('Create new account'));
135 $this->element('p', null,
136 _('Create a new user with this nickname.'));
137 $this->elementStart('ul', 'form_data');
138 $this->elementStart('li');
139 $this->input('newname', _('New nickname'),
140 ($this->username) ? $this->username : '',
141 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
142 $this->elementEnd('li');
143 $this->elementStart('li');
144 $this->element('input', array('type' => 'checkbox',
146 'class' => 'checkbox',
149 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
150 $this->text(_('My text and files are available under '));
151 $this->element('a', array('href' => common_config('license', 'url')),
152 common_config('license', 'title'));
153 $this->text(_(' except this private data: password, email address, IM address, phone number.'));
154 $this->elementEnd('label');
155 $this->elementEnd('li');
156 $this->elementEnd('ul');
157 $this->submit('create', _('Create'));
158 $this->elementEnd('fieldset');
160 $this->elementStart('fieldset');
161 $this->element('legend', null,
162 _('Connect existing account'));
163 $this->element('p', null,
164 _('If you already have an account, login with your username and password to connect it to your Facebook.'));
166 $this->elementStart('ul', 'form_data');
167 $this->elementStart('li');
168 $this->input('nickname', _('Existing nickname'));
169 $this->elementEnd('li');
170 $this->elementStart('li');
171 $this->password('password', _('Password'));
172 $this->elementEnd('li');
173 $this->elementEnd('ul');
174 $this->submit('connect', _('Connect'));
175 $this->elementEnd('fieldset');
176 $this->elementEnd('form');
179 function message($msg)
181 $this->message_text = $msg;
185 function createNewUser()
188 if (common_config('site', 'closed')) {
189 $this->clientError(_('Registration not allowed.'));
195 if (common_config('site', 'inviteonly')) {
196 $code = $_SESSION['invitecode'];
198 $this->clientError(_('Registration not allowed.'));
202 $invite = Invitation::staticGet($code);
204 if (empty($invite)) {
205 $this->clientError(_('Not a valid invitation code.'));
210 $nickname = $this->trimmed('newname');
212 if (!Validate::string($nickname, array('min_length' => 1,
214 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
215 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
219 if (!User::allowed_nickname($nickname)) {
220 $this->showForm(_('Nickname not allowed.'));
224 if (User::staticGet('nickname', $nickname)) {
225 $this->showForm(_('Nickname already in use. Try another one.'));
229 $fullname = trim($this->fb_fields['firstname'] .
230 ' ' . $this->fb_fields['lastname']);
232 $args = array('nickname' => $nickname, 'fullname' => $fullname);
234 if (!empty($invite)) {
235 $args['code'] = $invite->code;
238 $user = User::register($args);
240 $result = $this->flinkUser($user->id, $this->fbuid);
243 $this->serverError(_('Error connecting user to Facebook.'));
247 common_set_user($user);
248 common_real_login(true);
250 common_debug("Registered new user $user->id from Facebook user $this->fbuid");
252 common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
256 function connectUser()
258 $nickname = $this->trimmed('nickname');
259 $password = $this->trimmed('password');
261 if (!common_check_user($nickname, $password)) {
262 $this->showForm(_('Invalid username or password.'));
266 $user = User::staticGet('nickname', $nickname);
269 common_debug("Legit user to connect to Facebook: $nickname");
272 $result = $this->flinkUser($user->id, $this->fbuid);
275 $this->serverError(_('Error connecting user to Facebook.'));
279 common_debug("Connected Facebook user $this->fbuid to local user $user->id");
281 common_set_user($user);
282 common_real_login(true);
284 $this->goHome($user->nickname);
289 common_debug("Trying Facebook Login...");
291 $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_CONNECT_SERVICE);
294 $user = $flink->getUser();
298 common_debug("Logged in Facebook user $flink->foreign_id as user $user->id ($user->nickname)");
300 common_set_user($user);
301 common_real_login(true);
302 $this->goHome($user->nickname);
307 common_debug("No flink found for fbuid: $this->fbuid");
309 $this->showForm(null, $this->bestNewNickname());
313 function goHome($nickname)
315 $url = common_get_returnto();
317 // We don't have to return to it again
318 common_set_returnto(null);
320 $url = common_local_url('all',
325 common_redirect($url, 303);
328 function flinkUser($user_id, $fbuid)
330 $flink = new Foreign_link();
331 $flink->user_id = $user_id;
332 $flink->foreign_id = $fbuid;
333 $flink->service = FACEBOOK_CONNECT_SERVICE;
334 $flink->created = common_sql_now();
336 $flink_id = $flink->insert();
341 function bestNewNickname()
343 if (!empty($this->fb_fields['name'])) {
344 $nickname = $this->nicknamize($this->fb_fields['name']);
345 if ($this->isNewNickname($nickname)) {
352 $fullname = trim($this->fb_fields['firstname'] .
353 ' ' . $this->fb_fields['lastname']);
355 if (!empty($fullname)) {
356 $fullname = $this->nicknamize($fullname);
357 if ($this->isNewNickname($fullname)) {
365 // Given a string, try to make it work as a nickname
367 function nicknamize($str)
369 $str = preg_replace('/\W/', '', $str);
370 return strtolower($str);
373 function isNewNickname($str)
375 if (!Validate::string($str, array('min_length' => 1,
377 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
380 if (!User::allowed_nickname($str)) {
383 if (User::staticGet('nickname', $str)) {
389 // XXX: Consider moving this to lib/facebookutil.php
390 function getFacebookFields($fb_uid, $fields) {
393 $facebook = getFacebook();
395 $infos = $facebook->api_client->users_getInfo($fb_uid, $fields);
400 return reset($infos);
402 } catch (Exception $e) {
403 common_log(LOG_WARNING, "Facebook client failure when requesting " .
404 join(",", $fields) . " on uid " . $fb_uid .
405 " : ". $e->getMessage());