3 * Laconica, the distributed open-source microblogging tool
5 * Register a new user account
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 Evan Prodromou <evan@controlyourself.ca>
25 * @copyright 2008-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 if (!defined('LACONICA')) {
35 * An action for registering a new user account
39 * @author Evan Prodromou <evan@controlyourself.ca>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://laconi.ca/
44 class RegisterAction extends Action
47 * Has there been an error?
56 var $registered = false;
61 * @return string title
66 if ($this->registered) {
67 return _('Registration successful');
74 * Handle input, produce output
76 * Switches on request method; either shows the form or handles its input.
78 * Checks if registration is closed and shows an error if so.
80 * @param array $args $_REQUEST data
85 function handle($args)
87 parent::handle($args);
89 if (common_config('site', 'closed')) {
90 $this->clientError(_('Registration not allowed.'));
91 } else if (common_logged_in()) {
92 $this->clientError(_('Already logged in.'));
93 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
101 * Try to register a user
103 * Validates the input and tries to save a new user and profile
104 * record. On success, shows an instructions page.
109 function tryRegister()
111 $token = $this->trimmed('token');
112 if (!$token || $token != common_session_token()) {
113 $this->showForm(_('There was a problem with your session token. '.
114 'Try again, please.'));
118 $nickname = $this->trimmed('nickname');
119 $email = $this->trimmed('email');
120 $fullname = $this->trimmed('fullname');
121 $homepage = $this->trimmed('homepage');
122 $bio = $this->trimmed('bio');
123 $location = $this->trimmed('location');
125 // We don't trim these... whitespace is OK in a password!
127 $password = $this->arg('password');
128 $confirm = $this->arg('confirm');
130 // invitation code, if any
132 $code = $this->trimmed('code');
135 $invite = Invitation::staticGet($code);
138 if (common_config('site', 'inviteonly') && !($code && $invite)) {
139 $this->clientError(_('Sorry, only invited people can register.'));
145 $nickname = common_canonical_nickname($nickname);
146 $email = common_canonical_email($email);
148 if (!$this->boolean('license')) {
149 $this->showForm(_('You can\'t register if you don\'t '.
150 'agree to the license.'));
151 } else if ($email && !Validate::email($email, true)) {
152 $this->showForm(_('Not a valid email address.'));
153 } else if (!Validate::string($nickname, array('min_length' => 1,
155 'format' => NICKNAME_FMT))) {
156 $this->showForm(_('Nickname must have only lowercase letters '.
157 'and numbers and no spaces.'));
158 } else if ($this->nicknameExists($nickname)) {
159 $this->showForm(_('Nickname already in use. Try another one.'));
160 } else if (!User::allowed_nickname($nickname)) {
161 $this->showForm(_('Not a valid nickname.'));
162 } else if ($this->emailExists($email)) {
163 $this->showForm(_('Email address already exists.'));
164 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
165 !Validate::uri($homepage,
166 array('allowed_schemes' =>
167 array('http', 'https')))) {
168 $this->showForm(_('Homepage is not a valid URL.'));
170 } else if (!is_null($fullname) && strlen($fullname) > 255) {
171 $this->showForm(_('Full name is too long (max 255 chars).'));
173 } else if (!is_null($bio) && strlen($bio) > 140) {
174 $this->showForm(_('Bio is too long (max 140 chars).'));
176 } else if (!is_null($location) && strlen($location) > 255) {
177 $this->showForm(_('Location is too long (max 255 chars).'));
179 } else if (strlen($password) < 6) {
180 $this->showForm(_('Password must be 6 or more characters.'));
182 } else if ($password != $confirm) {
183 $this->showForm(_('Passwords don\'t match.'));
184 } else if ($user = User::register(array('nickname' => $nickname,
185 'password' => $password,
187 'fullname' => $fullname,
188 'homepage' => $homepage,
190 'location' => $location,
193 $this->showForm(_('Invalid username or password.'));
197 if (!common_set_user($user)) {
198 $this->serverError(_('Error setting user.'));
201 // this is a real login
202 common_real_login(true);
203 if ($this->boolean('rememberme')) {
204 common_debug('Adding rememberme cookie for ' . $nickname);
205 common_rememberme($user);
207 // Re-init language env in case it changed (not yet, but soon)
208 common_init_language();
209 $this->showSuccess();
211 $this->showForm(_('Invalid username or password.'));
216 * Does the given nickname already exist?
218 * Checks a canonical nickname against the database.
220 * @param string $nickname nickname to check
222 * @return boolean true if the nickname already exists
225 function nicknameExists($nickname)
227 $user = User::staticGet('nickname', $nickname);
228 return ($user !== false);
232 * Does the given email address already exist?
234 * Checks a canonical email address against the database.
236 * @param string $email email address to check
238 * @return boolean true if the address already exists
241 function emailExists($email)
243 $email = common_canonical_email($email);
244 if (!$email || strlen($email) == 0) {
247 $user = User::staticGet('email', $email);
248 return ($user !== false);
251 // overrrided to add entry-title class
252 function showPageTitle() {
253 $this->element('h1', array('class' => 'entry-title'), $this->title());
256 // overrided to add hentry, and content-inner class
257 function showContentBlock()
259 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
260 $this->showPageTitle();
261 $this->showPageNoticeBlock();
262 $this->elementStart('div', array('id' => 'content_inner',
263 'class' => 'entry-content'));
264 // show the actual content (forms, lists, whatever)
265 $this->showContent();
266 $this->elementEnd('div');
267 $this->elementEnd('div');
271 * Instructions or a notice for the page
273 * Shows the error, if any, or instructions for registration.
278 function showPageNotice()
280 if ($this->registered) {
282 } else if ($this->error) {
283 $this->element('p', 'error', $this->error);
286 common_markup_to_html(_('With this form you can create '.
288 'You can then post notices and '.
289 'link up to friends and colleagues. '.
290 '(Have an [OpenID](http://openid.net/)? ' .
291 'Try our [OpenID registration]'.
292 '(%%action.openidlogin%%)!)'));
294 $this->elementStart('div', 'instructions');
296 $this->elementEnd('div');
301 * Wrapper for showing a page
303 * Stores an error and shows the page
305 * @param string $error Error, if any
310 function showForm($error=null)
312 $this->error = $error;
317 * Show the page content
319 * Either shows the registration form or, if registration was successful,
320 * instructions for using the site.
325 function showContent()
327 if ($this->registered) {
328 $this->showSuccessContent();
330 $this->showFormContent();
335 * Show the registration form
340 function showFormContent()
342 $code = $this->trimmed('code');
345 $invite = Invitation::staticGet($code);
348 if (common_config('site', 'inviteonly') && !($code && $invite)) {
349 $this->clientError(_('Sorry, only invited people can register.'));
353 $this->elementStart('form', array('method' => 'post',
354 'id' => 'form_register',
355 'class' => 'form_settings',
356 'action' => common_local_url('register')));
357 $this->elementStart('fieldset');
358 $this->element('legend', null, 'Account settings');
359 $this->hidden('token', common_session_token());
362 $this->hidden('code', $code);
365 $this->elementStart('ul', 'form_data');
366 $this->elementStart('li');
367 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
368 _('1-64 lowercase letters or numbers, '.
369 'no punctuation or spaces. Required.'));
370 $this->elementEnd('li');
371 $this->elementStart('li');
372 $this->password('password', _('Password'),
373 _('6 or more characters. Required.'));
374 $this->elementEnd('li');
375 $this->elementStart('li');
376 $this->password('confirm', _('Confirm'),
377 _('Same as password above. Required.'));
378 $this->elementEnd('li');
379 $this->elementStart('li');
380 if ($invite && $invite->address_type == 'email') {
381 $this->input('email', _('Email'), $invite->address,
382 _('Used only for updates, announcements, '.
383 'and password recovery'));
385 $this->input('email', _('Email'), $this->trimmed('email'),
386 _('Used only for updates, announcements, '.
387 'and password recovery'));
389 $this->elementEnd('li');
390 $this->elementStart('li');
391 $this->input('fullname', _('Full name'),
392 $this->trimmed('fullname'),
393 _('Longer name, preferably your "real" name'));
394 $this->elementEnd('li');
395 $this->elementStart('li');
396 $this->input('homepage', _('Homepage'),
397 $this->trimmed('homepage'),
398 _('URL of your homepage, blog, '.
399 'or profile on another site'));
400 $this->elementEnd('li');
401 $this->elementStart('li');
402 $this->textarea('bio', _('Bio'),
403 $this->trimmed('bio'),
404 _('Describe yourself and your '.
405 'interests in 140 chars'));
406 $this->elementEnd('li');
407 $this->elementStart('li');
408 $this->input('location', _('Location'),
409 $this->trimmed('location'),
410 _('Where you are, like "City, '.
411 'State (or Region), Country"'));
412 $this->elementEnd('li');
413 $this->elementStart('li', array('id' => 'settings_rememberme'));
414 $this->checkbox('rememberme', _('Remember me'),
415 $this->boolean('rememberme'),
416 _('Automatically login in the future; '.
417 'not for shared computers!'));
418 $this->elementEnd('li');
419 $attrs = array('type' => 'checkbox',
421 'class' => 'checkbox',
424 if ($this->boolean('license')) {
425 $attrs['checked'] = 'checked';
427 $this->elementStart('li');
428 $this->element('input', $attrs);
429 $this->text(_('My text and files are available under '));
430 $this->element('a', array('href' => common_config('license', 'url')),
431 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
432 $this->text(_(' except this private data: password, '.
433 'email address, IM address, phone number.'));
434 $this->elementEnd('li');
435 $this->elementEnd('ul');
436 $this->submit('submit', _('Register'));
437 $this->elementEnd('fieldset');
438 $this->elementEnd('form');
442 * Show some information about registering for the site
444 * Save the registration flag, run showPage
449 function showSuccess()
451 $this->registered = true;
456 * Show some information about registering for the site
458 * Gives some information and options for new registrees.
463 function showSuccessContent()
465 $nickname = $this->arg('nickname');
467 $profileurl = common_local_url('showstream',
468 array('nickname' => $nickname));
470 $this->elementStart('div', 'success');
471 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
472 'From here, you may want to...'. "\n\n" .
473 '* Go to [your profile](%s) '.
474 'and post your first message.' . "\n" .
475 '* Add a [Jabber/GTalk address]'.
476 '(%%%%action.imsettings%%%%) '.
477 'so you can send notices '.
478 'through instant messages.' . "\n" .
479 '* [Search for people](%%%%action.peoplesearch%%%%) '.
480 'that you may know or '.
481 'that share your interests. ' . "\n" .
482 '* Update your [profile settings]'.
483 '(%%%%action.profilesettings%%%%)'.
484 ' to tell others more about you. ' . "\n" .
485 '* Read over the [online docs](%%%%doc.help%%%%)'.
486 ' for features you may have missed. ' . "\n\n" .
487 'Thanks for signing up and we hope '.
488 'you enjoy using this service.'),
489 $nickname, $profileurl);
491 $this->raw(common_markup_to_html($instr));
493 $have_email = $this->trimmed('email');
495 $emailinstr = _('(You should receive a message by email '.
496 'momentarily, with ' .
497 'instructions on how to confirm '.
498 'your email address.)');
499 $this->raw(common_markup_to_html($emailinstr));
501 $this->elementEnd('div');
505 * Show the login group nav menu
510 function showLocalNav()
512 $nav = new LoginGroupNav($this);