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 protected function prepare(array $args=array())
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.'));
84 if (!empty($this->code)) {
85 $this->invite = Invitation::getKV('code', $this->code);
86 if (!$this->invite instanceof Invitation) {
87 // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
88 $this->clientError(_('Sorry, invalid invitation code.'));
90 // Store this in case we need it
91 common_ensure_session();
92 $_SESSION['invitecode'] = $this->code;
101 * @return string title
105 if ($this->registered) {
106 // TRANS: Title for registration page after a succesful registration.
107 return _('Registration successful');
109 // TRANS: Title for registration page.
110 return _m('TITLE','Register');
115 * Handle input, produce output
117 * Switches on request method; either shows the form or handles its input.
119 * Checks if registration is closed and shows an error if so.
121 * @param array $args $_REQUEST data
125 function handle(array $args=array())
127 parent::handle($args);
129 if (common_config('site', 'closed')) {
130 // TRANS: Client error displayed when trying to register to a closed site.
131 $this->clientError(_('Registration not allowed.'));
132 } else if (common_logged_in()) {
133 // TRANS: Client error displayed when trying to register while already logged in.
134 $this->clientError(_('Already logged in.'));
135 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
136 $this->tryRegister();
142 function showScripts()
144 parent::showScripts();
145 $this->autofocus('nickname');
149 * Try to register a user
151 * Validates the input and tries to save a new user and profile
152 * record. On success, shows an instructions page.
156 function tryRegister()
158 if (Event::handle('StartRegistrationTry', array($this))) {
159 $token = $this->trimmed('token');
160 if (!$token || $token != common_session_token()) {
161 // TRANS: Client error displayed when the session token does not match or is not given.
162 $this->showForm(_('There was a problem with your session token. '.
163 'Try again, please.'));
167 $nickname = $this->trimmed('nickname');
168 $email = $this->trimmed('email');
169 $fullname = $this->trimmed('fullname');
170 $homepage = $this->trimmed('homepage');
171 $bio = $this->trimmed('bio');
172 $location = $this->trimmed('location');
174 // We don't trim these... whitespace is OK in a password!
175 $password = $this->arg('password');
176 $confirm = $this->arg('confirm');
178 // invitation code, if any
179 $code = $this->trimmed('code');
182 $invite = Invitation::getKV($code);
185 if (common_config('site', 'inviteonly') && !($code && $invite)) {
186 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
187 $this->clientError(_('Sorry, only invited people can register.'));
192 $nickname = Nickname::normalize($nickname, true);
193 } catch (NicknameException $e) {
194 $this->showForm($e->getMessage());
197 $email = common_canonical_email($email);
199 if (!$this->boolean('license')) {
200 // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
201 $this->showForm(_('You cannot register if you do not '.
202 'agree to the license.'));
203 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
204 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
205 $this->showForm(_('Not a valid email address.'));
206 } else if ($this->emailExists($email)) {
207 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
208 $this->showForm(_('Email address already exists.'));
209 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
210 !common_valid_http_url($homepage)) {
211 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
212 $this->showForm(_('Homepage is not a valid URL.'));
213 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
214 // TRANS: Form validation error displayed when trying to register with a too long full name.
215 $this->showForm(_('Full name is too long (maximum 255 characters).'));
216 } else if (Profile::bioTooLong($bio)) {
217 // TRANS: Form validation error on registration page when providing too long a bio text.
218 // TRANS: %d is the maximum number of characters for bio; used for plural.
219 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
220 'Bio is too long (maximum %d characters).',
223 } else if (!is_null($location) && mb_strlen($location) > 255) {
224 // TRANS: Form validation error displayed when trying to register with a too long location.
225 $this->showForm(_('Location is too long (maximum 255 characters).'));
226 } else if (strlen($password) < 6) {
227 // TRANS: Form validation error displayed when trying to register with too short a password.
228 $this->showForm(_('Password must be 6 or more characters.'));
229 } else if ($password != $confirm) {
230 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
231 $this->showForm(_('Passwords do not match.'));
232 } else if ($user = User::register(array('nickname' => $nickname,
233 'password' => $password,
235 'fullname' => $fullname,
236 'homepage' => $homepage,
238 'location' => $location,
240 if (!($user instanceof User)) {
241 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
242 $this->showForm(_('Invalid username or password.'));
246 if (!common_set_user($user)) {
247 // TRANS: Server error displayed when saving fails during user registration.
248 $this->serverError(_('Error setting user.'));
250 // this is a real login
251 common_real_login(true);
252 if ($this->boolean('rememberme')) {
253 common_debug('Adding rememberme cookie for ' . $nickname);
254 common_rememberme($user);
257 // Re-init language env in case it changed (not yet, but soon)
258 common_init_language();
260 Event::handle('EndRegistrationTry', array($this));
262 $this->showSuccess();
264 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
265 $this->showForm(_('Invalid username or password.'));
271 * Does the given email address already exist?
273 * Checks a canonical email address against the database.
275 * @param string $email email address to check
277 * @return boolean true if the address already exists
279 function emailExists($email)
281 $email = common_canonical_email($email);
282 if (!$email || strlen($email) == 0) {
285 $user = User::getKV('email', $email);
286 return is_object($user);
289 // overrrided to add entry-title class
290 function showPageTitle() {
291 if (Event::handle('StartShowPageTitle', array($this))) {
292 $this->element('h1', array('class' => 'entry-title'), $this->title());
296 // overrided to add h-entry, and content-inner class
297 function showContentBlock()
299 $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
300 $this->showPageTitle();
301 $this->showPageNoticeBlock();
302 $this->elementStart('div', array('id' => 'content_inner',
303 'class' => 'e-content'));
304 // show the actual content (forms, lists, whatever)
305 $this->showContent();
306 $this->elementEnd('div');
307 $this->elementEnd('div');
311 * Instructions or a notice for the page
313 * Shows the error, if any, or instructions for registration.
317 function showPageNotice()
319 if ($this->registered) {
321 } else if ($this->error) {
322 $this->element('p', 'error', $this->error);
325 // TRANS: Page notice on registration page.
326 common_markup_to_html(_('With this form you can create '.
328 'You can then post notices and '.
329 'link up to friends and colleagues.'));
331 $this->elementStart('div', 'instructions');
333 $this->elementEnd('div');
338 * Wrapper for showing a page
340 * Stores an error and shows the page
342 * @param string $error Error, if any
346 function showForm($error=null)
348 $this->error = $error;
353 * Show the page content
355 * Either shows the registration form or, if registration was successful,
356 * instructions for using the site.
360 function showContent()
362 if ($this->registered) {
363 $this->showSuccessContent();
365 $this->showFormContent();
370 * Show the registration form
374 function showFormContent()
376 $code = $this->trimmed('code');
381 $invite = Invitation::getKV($code);
384 if (common_config('site', 'inviteonly') && !($code && $invite)) {
385 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
386 $this->clientError(_('Sorry, only invited people can register.'));
389 $this->elementStart('form', array('method' => 'post',
390 'id' => 'form_register',
391 'class' => 'form_settings',
392 'action' => common_local_url('register')));
393 $this->elementStart('fieldset');
394 // TRANS: Fieldset legend on accout registration page.
395 $this->element('legend', null, 'Account settings');
396 $this->hidden('token', common_session_token());
399 $this->hidden('code', $this->code);
402 $this->elementStart('ul', 'form_data');
403 if (Event::handle('StartRegistrationFormData', array($this))) {
404 $this->elementStart('li');
405 // TRANS: Field label on account registration page.
406 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
407 // TRANS: Field title on account registration page.
408 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
409 $this->elementEnd('li');
410 $this->elementStart('li');
411 // TRANS: Field label on account registration page.
412 $this->password('password', _('Password'),
413 // TRANS: Field title on account registration page.
414 _('6 or more characters.'));
415 $this->elementEnd('li');
416 $this->elementStart('li');
417 // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
418 $this->password('confirm', _m('PASSWORD','Confirm'),
419 // TRANS: Field title on account registration page.
420 _('Same as password above.'));
421 $this->elementEnd('li');
422 $this->elementStart('li');
423 if ($this->invite && $this->invite->address_type == 'email') {
424 // TRANS: Field label on account registration page.
425 $this->input('email', _m('LABEL','Email'), $this->invite->address,
426 // TRANS: Field title on account registration page.
427 _('Used only for updates, announcements, '.
428 'and password recovery.'));
430 // TRANS: Field label on account registration page.
431 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
432 // TRANS: Field title on account registration page.
433 _('Used only for updates, announcements, '.
434 'and password recovery.'));
436 $this->elementEnd('li');
437 $this->elementStart('li');
438 // TRANS: Field label on account registration page.
439 $this->input('fullname', _('Full name'),
440 $this->trimmed('fullname'),
441 // TRANS: Field title on account registration page.
442 _('Longer name, preferably your "real" name.'));
443 $this->elementEnd('li');
444 $this->elementStart('li');
445 // TRANS: Field label on account registration page.
446 $this->input('homepage', _('Homepage'),
447 $this->trimmed('homepage'),
448 // TRANS: Field title on account registration page.
449 _('URL of your homepage, blog, '.
450 'or profile on another site.'));
451 $this->elementEnd('li');
452 $this->elementStart('li');
453 $maxBio = Profile::maxBio();
455 // TRANS: Text area title in form for account registration. Plural
456 // TRANS: is decided by the number of characters available for the
457 // TRANS: biography (%d).
458 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
459 'Describe yourself and your interests in %d characters.',
463 // TRANS: Text area title on account registration page.
464 $bioInstr = _('Describe yourself and your interests.');
466 // TRANS: Text area label on account registration page.
467 $this->textarea('bio', _('Bio'),
468 $this->trimmed('bio'),
470 $this->elementEnd('li');
471 $this->elementStart('li');
472 // TRANS: Field label on account registration page.
473 $this->input('location', _('Location'),
474 $this->trimmed('location'),
475 // TRANS: Field title on account registration page.
476 _('Where you are, like "City, '.
477 'State (or Region), Country".'));
478 $this->elementEnd('li');
479 Event::handle('EndRegistrationFormData', array($this));
480 $this->elementStart('li', array('id' => 'settings_rememberme'));
481 // TRANS: Checkbox label on account registration page.
482 $this->checkbox('rememberme', _('Remember me'),
483 $this->boolean('rememberme'),
484 // TRANS: Checkbox title on account registration page.
485 _('Automatically login in the future; '.
486 'not for shared computers!'));
487 $this->elementEnd('li');
488 $attrs = array('type' => 'checkbox',
490 'class' => 'checkbox',
493 if ($this->boolean('license')) {
494 $attrs['checked'] = 'checked';
496 $this->elementStart('li');
497 $this->element('input', $attrs);
498 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
499 $this->raw($this->licenseCheckbox());
500 $this->elementEnd('label');
501 $this->elementEnd('li');
503 $this->elementEnd('ul');
504 // TRANS: Button text to register a user on account registration page.
505 $this->submit('submit', _m('BUTTON','Register'));
506 $this->elementEnd('fieldset');
507 $this->elementEnd('form');
510 function licenseCheckbox()
513 switch (common_config('license', 'type')) {
515 $out .= htmlspecialchars(sprintf(
516 // TRANS: Copyright checkbox label in registration dialog, for private sites.
517 // TRANS: %1$s is the StatusNet sitename.
518 _('I understand that content and data of %1$s are private and confidential.'),
519 common_config('site', 'name')));
521 case 'allrightsreserved':
525 if (common_config('license', 'owner')) {
526 $out .= htmlspecialchars(sprintf(
527 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
528 // TRANS: %1$s is the license owner.
529 _('My text and files are copyright by %1$s.'),
530 common_config('license', 'owner')));
532 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
533 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
535 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
536 $out .= ' ' . _('All rights reserved.');
538 case 'cc': // fall through
540 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
541 $message = _('My text and files are available under %s ' .
542 'except this private data: password, ' .
543 'email address, IM address, and phone number.');
544 $link = '<a href="' .
545 htmlspecialchars(common_config('license', 'url')) .
547 htmlspecialchars(common_config('license', 'title')) .
549 $out .= sprintf(htmlspecialchars($message), $link);
555 * Show some information about registering for the site
557 * Save the registration flag, run showPage
561 function showSuccess()
563 $this->registered = true;
568 * Show some information about registering for the site
570 * Gives some information and options for new registrees.
574 function showSuccessContent()
576 if (Event::handle('StartRegisterSuccess', array($this))) {
577 $nickname = $this->arg('nickname');
579 $profileurl = common_local_url('showstream',
580 array('nickname' => $nickname));
582 $this->elementStart('div', 'success');
583 // TRANS: Text displayed after successful account registration.
584 // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
585 // TRANS: This message contains Markdown links in the form [link text](link)
586 // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
587 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
588 'From here, you may want to...'. "\n\n" .
589 '* Go to [your profile](%2$s) '.
590 'and post your first message.' . "\n" .
591 '* Add a [Jabber/GTalk address]'.
592 '(%%%%action.imsettings%%%%) '.
593 'so you can send notices '.
594 'through instant messages.' . "\n" .
595 '* [Search for people](%%%%action.peoplesearch%%%%) '.
596 'that you may know or '.
597 'that share your interests. ' . "\n" .
598 '* Update your [profile settings]'.
599 '(%%%%action.profilesettings%%%%)'.
600 ' to tell others more about you. ' . "\n" .
601 '* Read over the [online docs](%%%%doc.help%%%%)'.
602 ' for features you may have missed. ' . "\n\n" .
603 'Thanks for signing up and we hope '.
604 'you enjoy using this service.'),
605 $nickname, $profileurl);
607 $this->raw(common_markup_to_html($instr));
609 $have_email = $this->trimmed('email');
611 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
612 $emailinstr = _('(You should receive a message by email '.
613 'momentarily, with ' .
614 'instructions on how to confirm '.
615 'your email address.)');
616 $this->raw(common_markup_to_html($emailinstr));
618 $this->elementEnd('div');
620 Event::handle('EndRegisterSuccess', array($this));
625 * Show the login group nav menu
629 function showLocalNav()
631 if (common_logged_in()) {
632 parent::showLocalNav();
634 $nav = new LoginGroupNav($this);
640 * Show a bit of login context
644 function showProfileBlock()
646 if (common_logged_in()) {
647 parent::showProfileBlock();