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 $this->showForm(_('There was a problem with your session token. '.
164 'Try again, please.'));
168 $nickname = $this->trimmed('nickname');
169 $email = $this->trimmed('email');
170 $fullname = $this->trimmed('fullname');
171 $homepage = $this->trimmed('homepage');
172 $bio = $this->trimmed('bio');
173 $location = $this->trimmed('location');
175 // We don't trim these... whitespace is OK in a password!
176 $password = $this->arg('password');
177 $confirm = $this->arg('confirm');
179 // invitation code, if any
180 $code = $this->trimmed('code');
183 $invite = Invitation::staticGet($code);
186 if (common_config('site', 'inviteonly') && !($code && $invite)) {
187 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
188 $this->clientError(_('Sorry, only invited people can register.'));
194 $nickname = Nickname::normalize($nickname);
195 } catch (NicknameException $e) {
196 $this->showForm($e->getMessage());
198 $email = common_canonical_email($email);
200 if (!$this->boolean('license')) {
201 // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
202 $this->showForm(_('You cannot register if you do not '.
203 'agree to the license.'));
204 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
205 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
206 $this->showForm(_('Not a valid email address.'));
207 } else if ($this->nicknameExists($nickname)) {
208 // TRANS: Form validation error displayed when trying to register with an existing nickname.
209 $this->showForm(_('Nickname already in use. Try another one.'));
210 } else if (!User::allowed_nickname($nickname)) {
211 // TRANS: Form validation error displayed when trying to register with an invalid nickname.
212 $this->showForm(_('Not a valid nickname.'));
213 } else if ($this->emailExists($email)) {
214 $this->showForm(_('Email address already exists.'));
215 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
216 !Validate::uri($homepage,
217 array('allowed_schemes' =>
218 array('http', 'https')))) {
219 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
220 $this->showForm(_('Homepage is not a valid URL.'));
222 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
223 // TRANS: Form validation error displayed when trying to register with a too long full name.
224 $this->showForm(_('Full name is too long (maximum 255 characters).'));
226 } else if (Profile::bioTooLong($bio)) {
227 // TRANS: Form validation error on registration page when providing too long a bio text.
228 // TRANS: %d is the maximum number of characters for bio; used for plural.
229 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
230 'Bio is too long (maximum %d characters).',
234 } else if (!is_null($location) && mb_strlen($location) > 255) {
235 // TRANS: Form validation error displayed when trying to register with a too long location.
236 $this->showForm(_('Location is too long (maximum 255 characters).'));
238 } else if (strlen($password) < 6) {
239 // TRANS: Form validation error displayed when trying to register with too short a password.
240 $this->showForm(_('Password must be 6 or more characters.'));
242 } else if ($password != $confirm) {
243 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
244 $this->showForm(_('Passwords do not match.'));
245 } else if ($user = User::register(array('nickname' => $nickname,
246 'password' => $password,
248 'fullname' => $fullname,
249 'homepage' => $homepage,
251 'location' => $location,
254 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
255 $this->showForm(_('Invalid username or password.'));
259 if (!common_set_user($user)) {
260 // TRANS: Server error displayed when saving fails during user registration.
261 $this->serverError(_('Error setting user.'));
264 // this is a real login
265 common_real_login(true);
266 if ($this->boolean('rememberme')) {
267 common_debug('Adding rememberme cookie for ' . $nickname);
268 common_rememberme($user);
271 Event::handle('EndRegistrationTry', array($this));
273 // Re-init language env in case it changed (not yet, but soon)
274 common_init_language();
276 $this->showSuccess();
278 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
279 $this->showForm(_('Invalid username or password.'));
285 * Does the given nickname already exist?
287 * Checks a canonical nickname against the database.
289 * @param string $nickname nickname to check
291 * @return boolean true if the nickname already exists
293 function nicknameExists($nickname)
295 $user = User::staticGet('nickname', $nickname);
296 return is_object($user);
300 * Does the given email address already exist?
302 * Checks a canonical email address against the database.
304 * @param string $email email address to check
306 * @return boolean true if the address already exists
308 function emailExists($email)
310 $email = common_canonical_email($email);
311 if (!$email || strlen($email) == 0) {
314 $user = User::staticGet('email', $email);
315 return is_object($user);
318 // overrrided to add entry-title class
319 function showPageTitle() {
320 if (Event::handle('StartShowPageTitle', array($this))) {
321 $this->element('h1', array('class' => 'entry-title'), $this->title());
325 // overrided to add hentry, and content-inner class
326 function showContentBlock()
328 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
329 $this->showPageTitle();
330 $this->showPageNoticeBlock();
331 $this->elementStart('div', array('id' => 'content_inner',
332 'class' => 'entry-content'));
333 // show the actual content (forms, lists, whatever)
334 $this->showContent();
335 $this->elementEnd('div');
336 $this->elementEnd('div');
340 * Instructions or a notice for the page
342 * Shows the error, if any, or instructions for registration.
346 function showPageNotice()
348 if ($this->registered) {
350 } else if ($this->error) {
351 $this->element('p', 'error', $this->error);
354 // TRANS: Page notice on registration page.
355 common_markup_to_html(_('With this form you can create '.
357 'You can then post notices and '.
358 'link up to friends and colleagues.'));
360 $this->elementStart('div', 'instructions');
362 $this->elementEnd('div');
367 * Wrapper for showing a page
369 * Stores an error and shows the page
371 * @param string $error Error, if any
375 function showForm($error=null)
377 $this->error = $error;
382 * Show the page content
384 * Either shows the registration form or, if registration was successful,
385 * instructions for using the site.
389 function showContent()
391 if ($this->registered) {
392 $this->showSuccessContent();
394 $this->showFormContent();
399 * Show the registration form
403 function showFormContent()
405 $code = $this->trimmed('code');
410 $invite = Invitation::staticGet($code);
413 if (common_config('site', 'inviteonly') && !($code && $invite)) {
414 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
415 $this->clientError(_('Sorry, only invited people can register.'));
419 $this->elementStart('form', array('method' => 'post',
420 'id' => 'form_register',
421 'class' => 'form_settings',
422 'action' => common_local_url('register')));
423 $this->elementStart('fieldset');
424 // TRANS: Fieldset legend on accout registration page.
425 $this->element('legend', null, 'Account settings');
426 $this->hidden('token', common_session_token());
429 $this->hidden('code', $this->code);
432 $this->elementStart('ul', 'form_data');
433 if (Event::handle('StartRegistrationFormData', array($this))) {
434 $this->elementStart('li');
435 // TRANS: Field label on account registration page.
436 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
437 // TRANS: Field title on account registration page.
438 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
439 $this->elementEnd('li');
440 $this->elementStart('li');
441 // TRANS: Field label on account registration page.
442 $this->password('password', _('Password'),
443 // TRANS: Field title on account registration page.
444 _('6 or more characters.'));
445 $this->elementEnd('li');
446 $this->elementStart('li');
447 // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
448 $this->password('confirm', _m('PASSWORD','Confirm'),
449 // TRANS: Field title on account registration page.
450 _('Same as password above.'));
451 $this->elementEnd('li');
452 $this->elementStart('li');
453 if ($this->invite && $this->invite->address_type == 'email') {
454 // TRANS: Field label on account registration page.
455 $this->input('email', _m('LABEL','Email'), $this->invite->address,
456 // TRANS: Field title on account registration page.
457 _('Used only for updates, announcements, '.
458 'and password recovery.'));
460 // TRANS: Field label on account registration page.
461 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
462 // TRANS: Field title on account registration page.
463 _('Used only for updates, announcements, '.
464 'and password recovery.'));
466 $this->elementEnd('li');
467 $this->elementStart('li');
468 // TRANS: Field label on account registration page.
469 $this->input('fullname', _('Full name'),
470 $this->trimmed('fullname'),
471 // TRANS: Field title on account registration page.
472 _('Longer name, preferably your "real" name.'));
473 $this->elementEnd('li');
474 $this->elementStart('li');
475 // TRANS: Field label on account registration page.
476 $this->input('homepage', _('Homepage'),
477 $this->trimmed('homepage'),
478 // TRANS: Field title on account registration page.
479 _('URL of your homepage, blog, '.
480 'or profile on another site.'));
481 $this->elementEnd('li');
482 $this->elementStart('li');
483 $maxBio = Profile::maxBio();
485 // TRANS: Text area title in form for account registration. Plural
486 // TRANS: is decided by the number of characters available for the
487 // TRANS: biography (%d).
488 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
489 'Describe yourself and your interests in %d characters.',
493 // TRANS: Text area title on account registration page.
494 $bioInstr = _('Describe yourself and your interests.');
496 // TRANS: Text area label on account registration page.
497 $this->textarea('bio', _('Bio'),
498 $this->trimmed('bio'),
500 $this->elementEnd('li');
501 $this->elementStart('li');
502 // TRANS: Field label on account registration page.
503 $this->input('location', _('Location'),
504 $this->trimmed('location'),
505 // TRANS: Field title on account registration page.
506 _('Where you are, like "City, '.
507 'State (or Region), Country".'));
508 $this->elementEnd('li');
509 Event::handle('EndRegistrationFormData', array($this));
510 $this->elementStart('li', array('id' => 'settings_rememberme'));
511 // TRANS: Checkbox label on account registration page.
512 $this->checkbox('rememberme', _('Remember me'),
513 $this->boolean('rememberme'),
514 // TRANS: Checkbox title on account registration page.
515 _('Automatically login in the future; '.
516 'not for shared computers!'));
517 $this->elementEnd('li');
518 $attrs = array('type' => 'checkbox',
520 'class' => 'checkbox',
523 if ($this->boolean('license')) {
524 $attrs['checked'] = 'checked';
526 $this->elementStart('li');
527 $this->element('input', $attrs);
528 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
529 $this->raw($this->licenseCheckbox());
530 $this->elementEnd('label');
531 $this->elementEnd('li');
533 $this->elementEnd('ul');
534 // TRANS: Field label on account registration page.
535 $this->submit('submit', _m('BUTTON','Register'));
536 $this->elementEnd('fieldset');
537 $this->elementEnd('form');
540 function licenseCheckbox()
543 switch (common_config('license', 'type')) {
545 $out .= htmlspecialchars(sprintf(
546 // TRANS: Copyright checkbox label in registration dialog, for private sites.
547 // TRANS: %1$s is the StatusNet sitename.
548 _('I understand that content and data of %1$s are private and confidential.'),
549 common_config('site', 'name')));
551 case 'allrightsreserved':
555 if (common_config('license', 'owner')) {
556 $out .= htmlspecialchars(sprintf(
557 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
558 // TRANS: %1$s is the license owner.
559 _('My text and files are copyright by %1$s.'),
560 common_config('license', 'owner')));
562 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
563 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
565 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
566 $out .= ' ' . _('All rights reserved.');
568 case 'cc': // fall through
570 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
571 $message = _('My text and files are available under %s ' .
572 'except this private data: password, ' .
573 'email address, IM address, and phone number.');
574 $link = '<a href="' .
575 htmlspecialchars(common_config('license', 'url')) .
577 htmlspecialchars(common_config('license', 'title')) .
579 $out .= sprintf(htmlspecialchars($message), $link);
585 * Show some information about registering for the site
587 * Save the registration flag, run showPage
591 function showSuccess()
593 $this->registered = true;
598 * Show some information about registering for the site
600 * Gives some information and options for new registrees.
604 function showSuccessContent()
606 $nickname = $this->arg('nickname');
608 $profileurl = common_local_url('showstream',
609 array('nickname' => $nickname));
611 $this->elementStart('div', 'success');
612 // TRANS: Text displayed after successful account registration.
613 // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
614 // TRANS: This message contains Markdown links in the form [link text](link)
615 // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
616 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
617 'From here, you may want to...'. "\n\n" .
618 '* Go to [your profile](%2$s) '.
619 'and post your first message.' . "\n" .
620 '* Add a [Jabber/GTalk address]'.
621 '(%%%%action.imsettings%%%%) '.
622 'so you can send notices '.
623 'through instant messages.' . "\n" .
624 '* [Search for people](%%%%action.peoplesearch%%%%) '.
625 'that you may know or '.
626 'that share your interests. ' . "\n" .
627 '* Update your [profile settings]'.
628 '(%%%%action.profilesettings%%%%)'.
629 ' to tell others more about you. ' . "\n" .
630 '* Read over the [online docs](%%%%doc.help%%%%)'.
631 ' for features you may have missed. ' . "\n\n" .
632 'Thanks for signing up and we hope '.
633 'you enjoy using this service.'),
634 $nickname, $profileurl);
636 $this->raw(common_markup_to_html($instr));
638 $have_email = $this->trimmed('email');
640 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
641 $emailinstr = _('(You should receive a message by email '.
642 'momentarily, with ' .
643 'instructions on how to confirm '.
644 'your email address.)');
645 $this->raw(common_markup_to_html($emailinstr));
647 $this->elementEnd('div');
651 * Show the login group nav menu
655 function showLocalNav()
657 $nav = new LoginGroupNav($this);
661 function showNoticeForm()
665 function showProfileBlock()