3 * StatusNet, 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@status.net>
25 * @copyright 2008-2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * An action for registering a new user account
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
43 class RegisterAction extends Action
46 * Has there been an error?
53 var $registered = false;
56 * Are we processing an invite?
65 * @return string title
67 function prepare($args)
69 parent::prepare($args);
70 $this->code = $this->trimmed('code');
72 if (empty($this->code)) {
73 common_ensure_session();
74 if (array_key_exists('invitecode', $_SESSION)) {
75 $this->code = $_SESSION['invitecode'];
79 if (common_config('site', 'inviteonly') && empty($this->code)) {
80 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
81 $this->clientError(_('Sorry, only invited people can register.'));
85 if (!empty($this->code)) {
86 $this->invite = Invitation::staticGet('code', $this->code);
87 if (empty($this->invite)) {
88 // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
89 $this->clientError(_('Sorry, invalid invitation code.'));
92 // Store this in case we need it
93 common_ensure_session();
94 $_SESSION['invitecode'] = $this->code;
103 * @return string title
107 if ($this->registered) {
108 // TRANS: Title for registration page after a succesful registration.
109 return _('Registration successful');
111 // TRANS: Title for registration page.
112 return _m('TITLE','Register');
117 * Handle input, produce output
119 * Switches on request method; either shows the form or handles its input.
121 * Checks if registration is closed and shows an error if so.
123 * @param array $args $_REQUEST data
127 function handle($args)
129 parent::handle($args);
131 if (common_config('site', 'closed')) {
132 // TRANS: Client error displayed when trying to register to a closed site.
133 $this->clientError(_('Registration not allowed.'));
134 } else if (common_logged_in()) {
135 // TRANS: Client error displayed when trying to register while already logged in.
136 $this->clientError(_('Already logged in.'));
137 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
138 $this->tryRegister();
144 function showScripts()
146 parent::showScripts();
147 $this->autofocus('nickname');
151 * Try to register a user
153 * Validates the input and tries to save a new user and profile
154 * record. On success, shows an instructions page.
158 function tryRegister()
160 if (Event::handle('StartRegistrationTry', array($this))) {
161 $token = $this->trimmed('token');
162 if (!$token || $token != common_session_token()) {
163 // TRANS: Client error displayed when the session token does not match or is not given.
164 $this->showForm(_('There was a problem with your session token. '.
165 'Try again, please.'));
169 $nickname = $this->trimmed('nickname');
170 $email = $this->trimmed('email');
171 $fullname = $this->trimmed('fullname');
172 $homepage = $this->trimmed('homepage');
173 $bio = $this->trimmed('bio');
174 $location = $this->trimmed('location');
176 // We don't trim these... whitespace is OK in a password!
177 $password = $this->arg('password');
178 $confirm = $this->arg('confirm');
180 // invitation code, if any
181 $code = $this->trimmed('code');
184 $invite = Invitation::staticGet($code);
187 if (common_config('site', 'inviteonly') && !($code && $invite)) {
188 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
189 $this->clientError(_('Sorry, only invited people can register.'));
195 $nickname = Nickname::normalize($nickname);
196 } catch (NicknameException $e) {
197 $this->showForm($e->getMessage());
199 $email = common_canonical_email($email);
201 if (!$this->boolean('license')) {
202 // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
203 $this->showForm(_('You cannot register if you do not '.
204 'agree to the license.'));
205 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
206 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
207 $this->showForm(_('Not a valid email address.'));
208 } else if ($this->nicknameExists($nickname)) {
209 // TRANS: Form validation error displayed when trying to register with an existing nickname.
210 $this->showForm(_('Nickname already in use. Try another one.'));
211 } else if (!User::allowed_nickname($nickname)) {
212 // TRANS: Form validation error displayed when trying to register with an invalid nickname.
213 $this->showForm(_('Not a valid nickname.'));
214 } else if ($this->emailExists($email)) {
215 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
216 $this->showForm(_('Email address already exists.'));
217 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
218 !Validate::uri($homepage,
219 array('allowed_schemes' =>
220 array('http', 'https')))) {
221 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
222 $this->showForm(_('Homepage is not a valid URL.'));
224 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
225 // TRANS: Form validation error displayed when trying to register with a too long full name.
226 $this->showForm(_('Full name is too long (maximum 255 characters).'));
228 } else if (Profile::bioTooLong($bio)) {
229 // TRANS: Form validation error on registration page when providing too long a bio text.
230 // TRANS: %d is the maximum number of characters for bio; used for plural.
231 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
232 'Bio is too long (maximum %d characters).',
236 } else if (!is_null($location) && mb_strlen($location) > 255) {
237 // TRANS: Form validation error displayed when trying to register with a too long location.
238 $this->showForm(_('Location is too long (maximum 255 characters).'));
240 } else if (strlen($password) < 6) {
241 // TRANS: Form validation error displayed when trying to register with too short a password.
242 $this->showForm(_('Password must be 6 or more characters.'));
244 } else if ($password != $confirm) {
245 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
246 $this->showForm(_('Passwords do not match.'));
247 } else if ($user = User::register(array('nickname' => $nickname,
248 'password' => $password,
250 'fullname' => $fullname,
251 'homepage' => $homepage,
253 'location' => $location,
256 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
257 $this->showForm(_('Invalid username or password.'));
261 if (!common_set_user($user)) {
262 // TRANS: Server error displayed when saving fails during user registration.
263 $this->serverError(_('Error setting user.'));
266 // this is a real login
267 common_real_login(true);
268 if ($this->boolean('rememberme')) {
269 common_debug('Adding rememberme cookie for ' . $nickname);
270 common_rememberme($user);
273 // Re-init language env in case it changed (not yet, but soon)
274 common_init_language();
276 Event::handle('EndRegistrationTry', array($this));
278 $this->showSuccess();
280 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
281 $this->showForm(_('Invalid username or password.'));
287 * Does the given nickname already exist?
289 * Checks a canonical nickname against the database.
291 * @param string $nickname nickname to check
293 * @return boolean true if the nickname already exists
295 function nicknameExists($nickname)
297 $user = User::staticGet('nickname', $nickname);
298 return is_object($user);
302 * Does the given email address already exist?
304 * Checks a canonical email address against the database.
306 * @param string $email email address to check
308 * @return boolean true if the address already exists
310 function emailExists($email)
312 $email = common_canonical_email($email);
313 if (!$email || strlen($email) == 0) {
316 $user = User::staticGet('email', $email);
317 return is_object($user);
320 // overrrided to add entry-title class
321 function showPageTitle() {
322 if (Event::handle('StartShowPageTitle', array($this))) {
323 $this->element('h1', array('class' => 'entry-title'), $this->title());
327 // overrided to add hentry, and content-inner class
328 function showContentBlock()
330 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
331 $this->showPageTitle();
332 $this->showPageNoticeBlock();
333 $this->elementStart('div', array('id' => 'content_inner',
334 'class' => 'entry-content'));
335 // show the actual content (forms, lists, whatever)
336 $this->showContent();
337 $this->elementEnd('div');
338 $this->elementEnd('div');
342 * Instructions or a notice for the page
344 * Shows the error, if any, or instructions for registration.
348 function showPageNotice()
350 if ($this->registered) {
352 } else if ($this->error) {
353 $this->element('p', 'error', $this->error);
356 // TRANS: Page notice on registration page.
357 common_markup_to_html(_('With this form you can create '.
359 'You can then post notices and '.
360 'link up to friends and colleagues.'));
362 $this->elementStart('div', 'instructions');
364 $this->elementEnd('div');
369 * Wrapper for showing a page
371 * Stores an error and shows the page
373 * @param string $error Error, if any
377 function showForm($error=null)
379 $this->error = $error;
384 * Show the page content
386 * Either shows the registration form or, if registration was successful,
387 * instructions for using the site.
391 function showContent()
393 if ($this->registered) {
394 $this->showSuccessContent();
396 $this->showFormContent();
401 * Show the registration form
405 function showFormContent()
407 $code = $this->trimmed('code');
412 $invite = Invitation::staticGet($code);
415 if (common_config('site', 'inviteonly') && !($code && $invite)) {
416 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
417 $this->clientError(_('Sorry, only invited people can register.'));
421 $this->elementStart('form', array('method' => 'post',
422 'id' => 'form_register',
423 'class' => 'form_settings',
424 'action' => common_local_url('register')));
425 $this->elementStart('fieldset');
426 // TRANS: Fieldset legend on accout registration page.
427 $this->element('legend', null, 'Account settings');
428 $this->hidden('token', common_session_token());
431 $this->hidden('code', $this->code);
434 $this->elementStart('ul', 'form_data');
435 if (Event::handle('StartRegistrationFormData', array($this))) {
436 $this->elementStart('li');
437 // TRANS: Field label on account registration page.
438 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
439 // TRANS: Field title on account registration page.
440 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
441 $this->elementEnd('li');
442 $this->elementStart('li');
443 // TRANS: Field label on account registration page.
444 $this->password('password', _('Password'),
445 // TRANS: Field title on account registration page.
446 _('6 or more characters.'));
447 $this->elementEnd('li');
448 $this->elementStart('li');
449 // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
450 $this->password('confirm', _m('PASSWORD','Confirm'),
451 // TRANS: Field title on account registration page.
452 _('Same as password above.'));
453 $this->elementEnd('li');
454 $this->elementStart('li');
455 if ($this->invite && $this->invite->address_type == 'email') {
456 // TRANS: Field label on account registration page.
457 $this->input('email', _m('LABEL','Email'), $this->invite->address,
458 // TRANS: Field title on account registration page.
459 _('Used only for updates, announcements, '.
460 'and password recovery.'));
462 // TRANS: Field label on account registration page.
463 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
464 // TRANS: Field title on account registration page.
465 _('Used only for updates, announcements, '.
466 'and password recovery.'));
468 $this->elementEnd('li');
469 $this->elementStart('li');
470 // TRANS: Field label on account registration page.
471 $this->input('fullname', _('Full name'),
472 $this->trimmed('fullname'),
473 // TRANS: Field title on account registration page.
474 _('Longer name, preferably your "real" name.'));
475 $this->elementEnd('li');
476 $this->elementStart('li');
477 // TRANS: Field label on account registration page.
478 $this->input('homepage', _('Homepage'),
479 $this->trimmed('homepage'),
480 // TRANS: Field title on account registration page.
481 _('URL of your homepage, blog, '.
482 'or profile on another site.'));
483 $this->elementEnd('li');
484 $this->elementStart('li');
485 $maxBio = Profile::maxBio();
487 // TRANS: Text area title in form for account registration. Plural
488 // TRANS: is decided by the number of characters available for the
489 // TRANS: biography (%d).
490 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
491 'Describe yourself and your interests in %d characters.',
495 // TRANS: Text area title on account registration page.
496 $bioInstr = _('Describe yourself and your interests.');
498 // TRANS: Text area label on account registration page.
499 $this->textarea('bio', _('Bio'),
500 $this->trimmed('bio'),
502 $this->elementEnd('li');
503 $this->elementStart('li');
504 // TRANS: Field label on account registration page.
505 $this->input('location', _('Location'),
506 $this->trimmed('location'),
507 // TRANS: Field title on account registration page.
508 _('Where you are, like "City, '.
509 'State (or Region), Country".'));
510 $this->elementEnd('li');
511 Event::handle('EndRegistrationFormData', array($this));
512 $this->elementStart('li', array('id' => 'settings_rememberme'));
513 // TRANS: Checkbox label on account registration page.
514 $this->checkbox('rememberme', _('Remember me'),
515 $this->boolean('rememberme'),
516 // TRANS: Checkbox title on account registration page.
517 _('Automatically login in the future; '.
518 'not for shared computers!'));
519 $this->elementEnd('li');
520 $attrs = array('type' => 'checkbox',
522 'class' => 'checkbox',
525 if ($this->boolean('license')) {
526 $attrs['checked'] = 'checked';
528 $this->elementStart('li');
529 $this->element('input', $attrs);
530 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
531 $this->raw($this->licenseCheckbox());
532 $this->elementEnd('label');
533 $this->elementEnd('li');
535 $this->elementEnd('ul');
536 // TRANS: Button text to register a user on account registration page.
537 $this->submit('submit', _m('BUTTON','Register'));
538 $this->elementEnd('fieldset');
539 $this->elementEnd('form');
542 function licenseCheckbox()
545 switch (common_config('license', 'type')) {
547 $out .= htmlspecialchars(sprintf(
548 // TRANS: Copyright checkbox label in registration dialog, for private sites.
549 // TRANS: %1$s is the StatusNet sitename.
550 _('I understand that content and data of %1$s are private and confidential.'),
551 common_config('site', 'name')));
553 case 'allrightsreserved':
557 if (common_config('license', 'owner')) {
558 $out .= htmlspecialchars(sprintf(
559 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
560 // TRANS: %1$s is the license owner.
561 _('My text and files are copyright by %1$s.'),
562 common_config('license', 'owner')));
564 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
565 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
567 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
568 $out .= ' ' . _('All rights reserved.');
570 case 'cc': // fall through
572 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
573 $message = _('My text and files are available under %s ' .
574 'except this private data: password, ' .
575 'email address, IM address, and phone number.');
576 $link = '<a href="' .
577 htmlspecialchars(common_config('license', 'url')) .
579 htmlspecialchars(common_config('license', 'title')) .
581 $out .= sprintf(htmlspecialchars($message), $link);
587 * Show some information about registering for the site
589 * Save the registration flag, run showPage
593 function showSuccess()
595 $this->registered = true;
600 * Show some information about registering for the site
602 * Gives some information and options for new registrees.
606 function showSuccessContent()
608 if (Event::handle('StartRegisterSuccess', array($this))) {
609 $nickname = $this->arg('nickname');
611 $profileurl = common_local_url('showstream',
612 array('nickname' => $nickname));
614 $this->elementStart('div', 'success');
615 // TRANS: Text displayed after successful account registration.
616 // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
617 // TRANS: This message contains Markdown links in the form [link text](link)
618 // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
619 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
620 'From here, you may want to...'. "\n\n" .
621 '* Go to [your profile](%2$s) '.
622 'and post your first message.' . "\n" .
623 '* Add a [Jabber/GTalk address]'.
624 '(%%%%action.imsettings%%%%) '.
625 'so you can send notices '.
626 'through instant messages.' . "\n" .
627 '* [Search for people](%%%%action.peoplesearch%%%%) '.
628 'that you may know or '.
629 'that share your interests. ' . "\n" .
630 '* Update your [profile settings]'.
631 '(%%%%action.profilesettings%%%%)'.
632 ' to tell others more about you. ' . "\n" .
633 '* Read over the [online docs](%%%%doc.help%%%%)'.
634 ' for features you may have missed. ' . "\n\n" .
635 'Thanks for signing up and we hope '.
636 'you enjoy using this service.'),
637 $nickname, $profileurl);
639 $this->raw(common_markup_to_html($instr));
641 $have_email = $this->trimmed('email');
643 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
644 $emailinstr = _('(You should receive a message by email '.
645 'momentarily, with ' .
646 'instructions on how to confirm '.
647 'your email address.)');
648 $this->raw(common_markup_to_html($emailinstr));
650 $this->elementEnd('div');
652 Event::handle('EndRegisterSuccess', array($this));
657 * Show the login group nav menu
661 function showLocalNav()
663 if (common_logged_in()) {
664 parent::showLocalNav();
666 $nav = new LoginGroupNav($this);
672 * Show a bit of login context
676 function showProfileBlock()
678 if (common_logged_in()) {
679 parent::showProfileBlock();