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;
63 * @return string title
66 function prepare($args)
68 parent::prepare($args);
69 $this->code = $this->trimmed('code');
71 if (empty($this->code)) {
72 common_ensure_session();
73 if (array_key_exists('invitecode', $_SESSION)) {
74 $this->code = $_SESSION['invitecode'];
78 if (common_config('site', 'inviteonly') && empty($this->code)) {
79 $this->clientError(_('Sorry, only invited people can register.'));
83 if (!empty($this->code)) {
84 $this->invite = Invitation::staticGet('code', $this->code);
85 if (empty($this->invite)) {
86 $this->clientError(_('Sorry, invalid invitation code.'));
89 // Store this in case we need it
90 common_ensure_session();
91 $_SESSION['invitecode'] = $this->code;
100 * @return string title
105 if ($this->registered) {
106 return _('Registration successful');
108 return _('Register');
113 * Handle input, produce output
115 * Switches on request method; either shows the form or handles its input.
117 * Checks if registration is closed and shows an error if so.
119 * Checks if only OpenID is allowed and redirects to openidlogin if so.
121 * @param array $args $_REQUEST data
126 function handle($args)
128 parent::handle($args);
130 if (common_config('site', 'closed')) {
131 $this->clientError(_('Registration not allowed.'));
132 } else if (common_config('site', 'openidonly')) {
133 common_redirect(common_local_url('openidlogin'));
134 } else if (common_logged_in()) {
135 $this->clientError(_('Already logged in.'));
136 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
137 $this->tryRegister();
144 * Try to register a user
146 * Validates the input and tries to save a new user and profile
147 * record. On success, shows an instructions page.
152 function tryRegister()
154 if (Event::handle('StartRegistrationTry', array($this))) {
155 $token = $this->trimmed('token');
156 if (!$token || $token != common_session_token()) {
157 $this->showForm(_('There was a problem with your session token. '.
158 'Try again, please.'));
162 $nickname = $this->trimmed('nickname');
163 $email = $this->trimmed('email');
164 $fullname = $this->trimmed('fullname');
165 $homepage = $this->trimmed('homepage');
166 $bio = $this->trimmed('bio');
167 $location = $this->trimmed('location');
169 // We don't trim these... whitespace is OK in a password!
170 $password = $this->arg('password');
171 $confirm = $this->arg('confirm');
173 // invitation code, if any
174 $code = $this->trimmed('code');
177 $invite = Invitation::staticGet($code);
180 if (common_config('site', 'inviteonly') && !($code && $invite)) {
181 $this->clientError(_('Sorry, only invited people can register.'));
186 $nickname = common_canonical_nickname($nickname);
187 $email = common_canonical_email($email);
189 if (!$this->boolean('license')) {
190 $this->showForm(_('You can\'t register if you don\'t '.
191 'agree to the license.'));
192 } else if ($email && !Validate::email($email, true)) {
193 $this->showForm(_('Not a valid email address.'));
194 } else if (!Validate::string($nickname, array('min_length' => 1,
196 'format' => NICKNAME_FMT))) {
197 $this->showForm(_('Nickname must have only lowercase letters '.
198 'and numbers and no spaces.'));
199 } else if ($this->nicknameExists($nickname)) {
200 $this->showForm(_('Nickname already in use. Try another one.'));
201 } else if (!User::allowed_nickname($nickname)) {
202 $this->showForm(_('Not a valid nickname.'));
203 } else if ($this->emailExists($email)) {
204 $this->showForm(_('Email address already exists.'));
205 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
206 !Validate::uri($homepage,
207 array('allowed_schemes' =>
208 array('http', 'https')))) {
209 $this->showForm(_('Homepage is not a valid URL.'));
211 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
212 $this->showForm(_('Full name is too long (max 255 chars).'));
214 } else if (Profile::bioTooLong($bio)) {
215 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
218 } else if (!is_null($location) && mb_strlen($location) > 255) {
219 $this->showForm(_('Location is too long (max 255 chars).'));
221 } else if (strlen($password) < 6) {
222 $this->showForm(_('Password must be 6 or more characters.'));
224 } else if ($password != $confirm) {
225 $this->showForm(_('Passwords don\'t match.'));
226 } else if ($user = User::register(array('nickname' => $nickname,
227 'password' => $password,
229 'fullname' => $fullname,
230 'homepage' => $homepage,
232 'location' => $location,
235 $this->showForm(_('Invalid username or password.'));
239 if (!common_set_user($user)) {
240 $this->serverError(_('Error setting user.'));
243 // this is a real login
244 common_real_login(true);
245 if ($this->boolean('rememberme')) {
246 common_debug('Adding rememberme cookie for ' . $nickname);
247 common_rememberme($user);
250 Event::handle('EndRegistrationTry', array($this));
252 // Re-init language env in case it changed (not yet, but soon)
253 common_init_language();
254 $this->showSuccess();
256 $this->showForm(_('Invalid username or password.'));
262 * Does the given nickname already exist?
264 * Checks a canonical nickname against the database.
266 * @param string $nickname nickname to check
268 * @return boolean true if the nickname already exists
271 function nicknameExists($nickname)
273 $user = User::staticGet('nickname', $nickname);
274 return ($user !== false);
278 * Does the given email address already exist?
280 * Checks a canonical email address against the database.
282 * @param string $email email address to check
284 * @return boolean true if the address already exists
287 function emailExists($email)
289 $email = common_canonical_email($email);
290 if (!$email || strlen($email) == 0) {
293 $user = User::staticGet('email', $email);
294 return ($user !== false);
297 // overrrided to add entry-title class
298 function showPageTitle() {
299 if (Event::handle('StartShowPageTitle', array($this))) {
300 $this->element('h1', array('class' => 'entry-title'), $this->title());
304 // overrided to add hentry, and content-inner class
305 function showContentBlock()
307 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
308 $this->showPageTitle();
309 $this->showPageNoticeBlock();
310 $this->elementStart('div', array('id' => 'content_inner',
311 'class' => 'entry-content'));
312 // show the actual content (forms, lists, whatever)
313 $this->showContent();
314 $this->elementEnd('div');
315 $this->elementEnd('div');
319 * Instructions or a notice for the page
321 * Shows the error, if any, or instructions for registration.
326 function showPageNotice()
328 if ($this->registered) {
330 } else if ($this->error) {
331 $this->element('p', 'error', $this->error);
333 if (common_config('openid', 'enabled')) {
335 common_markup_to_html(_('With this form you can create '.
337 'You can then post notices and '.
338 'link up to friends and colleagues. '.
339 '(Have an [OpenID](http://openid.net/)? ' .
340 'Try our [OpenID registration]'.
341 '(%%action.openidlogin%%)!)'));
344 common_markup_to_html(_('With this form you can create '.
346 'You can then post notices and '.
347 'link up to friends and colleagues.'));
350 $this->elementStart('div', 'instructions');
352 $this->elementEnd('div');
357 * Wrapper for showing a page
359 * Stores an error and shows the page
361 * @param string $error Error, if any
366 function showForm($error=null)
368 $this->error = $error;
373 * Show the page content
375 * Either shows the registration form or, if registration was successful,
376 * instructions for using the site.
381 function showContent()
383 if ($this->registered) {
384 $this->showSuccessContent();
386 $this->showFormContent();
391 * Show the registration form
396 function showFormContent()
398 $code = $this->trimmed('code');
403 $invite = Invitation::staticGet($code);
406 if (common_config('site', 'inviteonly') && !($code && $invite)) {
407 $this->clientError(_('Sorry, only invited people can register.'));
411 $this->elementStart('form', array('method' => 'post',
412 'id' => 'form_register',
413 'class' => 'form_settings',
414 'action' => common_local_url('register')));
415 $this->elementStart('fieldset');
416 $this->element('legend', null, 'Account settings');
417 $this->hidden('token', common_session_token());
420 $this->hidden('code', $this->code);
423 $this->elementStart('ul', 'form_data');
424 if (Event::handle('StartRegistrationFormData', array($this))) {
425 $this->elementStart('li');
426 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
427 _('1-64 lowercase letters or numbers, '.
428 'no punctuation or spaces. Required.'));
429 $this->elementEnd('li');
430 $this->elementStart('li');
431 $this->password('password', _('Password'),
432 _('6 or more characters. Required.'));
433 $this->elementEnd('li');
434 $this->elementStart('li');
435 $this->password('confirm', _('Confirm'),
436 _('Same as password above. Required.'));
437 $this->elementEnd('li');
438 $this->elementStart('li');
439 if ($this->invite && $this->invite->address_type == 'email') {
440 $this->input('email', _('Email'), $this->invite->address,
441 _('Used only for updates, announcements, '.
442 'and password recovery'));
444 $this->input('email', _('Email'), $this->trimmed('email'),
445 _('Used only for updates, announcements, '.
446 'and password recovery'));
448 $this->elementEnd('li');
449 $this->elementStart('li');
450 $this->input('fullname', _('Full name'),
451 $this->trimmed('fullname'),
452 _('Longer name, preferably your "real" name'));
453 $this->elementEnd('li');
454 $this->elementStart('li');
455 $this->input('homepage', _('Homepage'),
456 $this->trimmed('homepage'),
457 _('URL of your homepage, blog, '.
458 'or profile on another site'));
459 $this->elementEnd('li');
460 $this->elementStart('li');
461 $maxBio = Profile::maxBio();
463 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
466 $bioInstr = _('Describe yourself and your interests');
468 $this->textarea('bio', _('Bio'),
469 $this->trimmed('bio'),
471 $this->elementEnd('li');
472 $this->elementStart('li');
473 $this->input('location', _('Location'),
474 $this->trimmed('location'),
475 _('Where you are, like "City, '.
476 'State (or Region), Country"'));
477 $this->elementEnd('li');
478 Event::handle('EndRegistrationFormData', array($this));
479 $this->elementStart('li', array('id' => 'settings_rememberme'));
480 $this->checkbox('rememberme', _('Remember me'),
481 $this->boolean('rememberme'),
482 _('Automatically login in the future; '.
483 'not for shared computers!'));
484 $this->elementEnd('li');
485 $attrs = array('type' => 'checkbox',
487 'class' => 'checkbox',
490 if ($this->boolean('license')) {
491 $attrs['checked'] = 'checked';
493 $this->elementStart('li');
494 $this->element('input', $attrs);
495 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
496 $this->text(_('My text and files are available under '));
497 $this->element('a', array('href' => common_config('license', 'url')),
498 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
499 $this->text(_(' except this private data: password, '.
500 'email address, IM address, and phone number.'));
501 $this->elementEnd('label');
502 $this->elementEnd('li');
504 $this->elementEnd('ul');
505 $this->submit('submit', _('Register'));
506 $this->elementEnd('fieldset');
507 $this->elementEnd('form');
511 * Show some information about registering for the site
513 * Save the registration flag, run showPage
518 function showSuccess()
520 $this->registered = true;
525 * Show some information about registering for the site
527 * Gives some information and options for new registrees.
532 function showSuccessContent()
534 $nickname = $this->arg('nickname');
536 $profileurl = common_local_url('showstream',
537 array('nickname' => $nickname));
539 $this->elementStart('div', 'success');
540 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
541 'From here, you may want to...'. "\n\n" .
542 '* Go to [your profile](%s) '.
543 'and post your first message.' . "\n" .
544 '* Add a [Jabber/GTalk address]'.
545 '(%%%%action.imsettings%%%%) '.
546 'so you can send notices '.
547 'through instant messages.' . "\n" .
548 '* [Search for people](%%%%action.peoplesearch%%%%) '.
549 'that you may know or '.
550 'that share your interests. ' . "\n" .
551 '* Update your [profile settings]'.
552 '(%%%%action.profilesettings%%%%)'.
553 ' to tell others more about you. ' . "\n" .
554 '* Read over the [online docs](%%%%doc.help%%%%)'.
555 ' for features you may have missed. ' . "\n\n" .
556 'Thanks for signing up and we hope '.
557 'you enjoy using this service.'),
558 $nickname, $profileurl);
560 $this->raw(common_markup_to_html($instr));
562 $have_email = $this->trimmed('email');
564 $emailinstr = _('(You should receive a message by email '.
565 'momentarily, with ' .
566 'instructions on how to confirm '.
567 'your email address.)');
568 $this->raw(common_markup_to_html($emailinstr));
570 $this->elementEnd('div');
574 * Show the login group nav menu
579 function showLocalNav()
581 $nav = new LoginGroupNav($this);