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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
33 * An action for registering a new user account
37 * @author Evan Prodromou <evan@status.net>
38 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39 * @link http://status.net/
41 class RegisterAction extends Action
44 * Has there been an error?
51 var $registered = false;
54 * Are we processing an invite?
63 * @return string title
65 protected function prepare(array $args=array())
67 parent::prepare($args);
68 $this->code = $this->trimmed('code');
70 if (empty($this->code)) {
71 common_ensure_session();
72 if (array_key_exists('invitecode', $_SESSION)) {
73 $this->code = $_SESSION['invitecode'];
77 if (common_config('site', 'inviteonly') && empty($this->code)) {
78 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
79 $this->clientError(_('Sorry, only invited people can register.'));
82 if (!empty($this->code)) {
83 $this->invite = Invitation::getKV('code', $this->code);
84 if (!$this->invite instanceof Invitation) {
85 // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
86 $this->clientError(_('Sorry, invalid invitation code.'));
88 // Store this in case we need it
89 common_ensure_session();
90 $_SESSION['invitecode'] = $this->code;
99 * @return string title
103 if ($this->registered) {
104 // TRANS: Title for registration page after a succesful registration.
105 return _('Registration successful');
107 // TRANS: Title for registration page.
108 return _m('TITLE','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 * @param array $args $_REQUEST data
123 function handle($args)
125 parent::handle($args);
127 if (common_config('site', 'closed')) {
128 // TRANS: Client error displayed when trying to register to a closed site.
129 $this->clientError(_('Registration not allowed.'));
130 } else if (common_logged_in()) {
131 // TRANS: Client error displayed when trying to register while already logged in.
132 $this->clientError(_('Already logged in.'));
133 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
134 $this->tryRegister();
140 function showScripts()
142 parent::showScripts();
143 $this->autofocus('nickname');
147 * Try to register a user
149 * Validates the input and tries to save a new user and profile
150 * record. On success, shows an instructions page.
154 function tryRegister()
156 if (Event::handle('StartRegistrationTry', array($this))) {
157 $token = $this->trimmed('token');
158 if (!$token || $token != common_session_token()) {
159 // TRANS: Client error displayed when the session token does not match or is not given.
160 $this->showForm(_('There was a problem with your session token. '.
161 'Try again, please.'));
165 $nickname = $this->trimmed('nickname');
166 $email = $this->trimmed('email');
167 $fullname = $this->trimmed('fullname');
168 $homepage = $this->trimmed('homepage');
169 $bio = $this->trimmed('bio');
170 $location = $this->trimmed('location');
172 // We don't trim these... whitespace is OK in a password!
173 $password = $this->arg('password');
174 $confirm = $this->arg('confirm');
176 // invitation code, if any
177 $code = $this->trimmed('code');
180 $invite = Invitation::getKV($code);
183 if (common_config('site', 'inviteonly') && !($code && $invite)) {
184 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
185 $this->clientError(_('Sorry, only invited people can register.'));
190 $nickname = Nickname::normalize($nickname, true);
191 } catch (NicknameException $e) {
192 $this->showForm($e->getMessage());
195 $email = common_canonical_email($email);
197 if (!$this->boolean('license')) {
198 // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
199 $this->showForm(_('You cannot register if you do not '.
200 'agree to the license.'));
201 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
202 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
203 $this->showForm(_('Not a valid email address.'));
204 } else if ($this->emailExists($email)) {
205 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
206 $this->showForm(_('Email address already exists.'));
207 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
208 !common_valid_http_url($homepage)) {
209 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
210 $this->showForm(_('Homepage is not a valid URL.'));
211 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
212 // TRANS: Form validation error displayed when trying to register with a too long full name.
213 $this->showForm(_('Full name is too long (maximum 255 characters).'));
214 } else if (Profile::bioTooLong($bio)) {
215 // TRANS: Form validation error on registration page when providing too long a bio text.
216 // TRANS: %d is the maximum number of characters for bio; used for plural.
217 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
218 'Bio is too long (maximum %d characters).',
221 } else if (!is_null($location) && mb_strlen($location) > 255) {
222 // TRANS: Form validation error displayed when trying to register with a too long location.
223 $this->showForm(_('Location is too long (maximum 255 characters).'));
224 } else if (strlen($password) < 6) {
225 // TRANS: Form validation error displayed when trying to register with too short a password.
226 $this->showForm(_('Password must be 6 or more characters.'));
227 } else if ($password != $confirm) {
228 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
229 $this->showForm(_('Passwords do not match.'));
232 $user = User::register(array('nickname' => $nickname,
233 'password' => $password,
235 'fullname' => $fullname,
236 'homepage' => $homepage,
238 'location' => $location,
241 if (!common_set_user($user)) {
242 // TRANS: Server error displayed when saving fails during user registration.
243 $this->serverError(_('Error setting user.'));
245 // this is a real login
246 common_real_login(true);
247 if ($this->boolean('rememberme')) {
248 common_debug('Adding rememberme cookie for ' . $nickname);
249 common_rememberme($user);
252 // Re-init language env in case it changed (not yet, but soon)
253 common_init_language();
255 Event::handle('EndRegistrationTry', array($this));
257 $this->showSuccess();
258 } catch (Exception $e) {
259 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
260 $this->showForm($e->getMessage());
267 * Does the given email address already exist?
269 * Checks a canonical email address against the database.
271 * @param string $email email address to check
273 * @return boolean true if the address already exists
275 function emailExists($email)
277 $email = common_canonical_email($email);
278 if (!$email || strlen($email) == 0) {
281 $user = User::getKV('email', $email);
282 return is_object($user);
285 // overrrided to add entry-title class
286 function showPageTitle() {
287 if (Event::handle('StartShowPageTitle', array($this))) {
288 $this->element('h1', array('class' => 'entry-title'), $this->title());
292 // overrided to add h-entry, and content-inner class
293 function showContentBlock()
295 $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
296 $this->showPageTitle();
297 $this->showPageNoticeBlock();
298 $this->elementStart('div', array('id' => 'content_inner',
299 'class' => 'e-content'));
300 // show the actual content (forms, lists, whatever)
301 $this->showContent();
302 $this->elementEnd('div');
303 $this->elementEnd('div');
307 * Instructions or a notice for the page
309 * Shows the error, if any, or instructions for registration.
313 function showPageNotice()
315 if ($this->registered) {
317 } else if ($this->error) {
318 $this->element('p', 'error', $this->error);
321 // TRANS: Page notice on registration page.
322 common_markup_to_html(_('With this form you can create '.
324 'You can then post notices and '.
325 'link up to friends and colleagues.'));
327 $this->elementStart('div', 'instructions');
329 $this->elementEnd('div');
334 * Wrapper for showing a page
336 * Stores an error and shows the page
338 * @param string $error Error, if any
342 function showForm($error=null)
344 $this->error = $error;
349 * Show the page content
351 * Either shows the registration form or, if registration was successful,
352 * instructions for using the site.
356 function showContent()
358 if ($this->registered) {
359 $this->showSuccessContent();
361 $this->showFormContent();
366 * Show the registration form
370 function showFormContent()
372 $code = $this->trimmed('code');
377 $invite = Invitation::getKV($code);
380 if (common_config('site', 'inviteonly') && !($code && $invite)) {
381 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
382 $this->clientError(_('Sorry, only invited people can register.'));
385 $this->elementStart('form', array('method' => 'post',
386 'id' => 'form_register',
387 'class' => 'form_settings',
388 'action' => common_local_url('register')));
389 $this->elementStart('fieldset');
390 // TRANS: Fieldset legend on accout registration page.
391 $this->element('legend', null, 'Account settings');
392 $this->hidden('token', common_session_token());
395 $this->hidden('code', $this->code);
398 $this->elementStart('ul', 'form_data');
399 if (Event::handle('StartRegistrationFormData', array($this))) {
400 $this->elementStart('li');
401 // TRANS: Field label on account registration page.
402 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
403 // TRANS: Field title on account registration page.
404 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
405 $this->elementEnd('li');
406 $this->elementStart('li');
407 // TRANS: Field label on account registration page.
408 $this->password('password', _('Password'),
409 // TRANS: Field title on account registration page.
410 _('6 or more characters.'));
411 $this->elementEnd('li');
412 $this->elementStart('li');
413 // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
414 $this->password('confirm', _m('PASSWORD','Confirm'),
415 // TRANS: Field title on account registration page.
416 _('Same as password above.'));
417 $this->elementEnd('li');
418 $this->elementStart('li');
419 if ($this->invite && $this->invite->address_type == 'email') {
420 // TRANS: Field label on account registration page.
421 $this->input('email', _m('LABEL','Email'), $this->invite->address,
422 // TRANS: Field title on account registration page.
423 _('Used only for updates, announcements, '.
424 'and password recovery.'));
426 // TRANS: Field label on account registration page.
427 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
428 // TRANS: Field title on account registration page.
429 _('Used only for updates, announcements, '.
430 'and password recovery.'));
432 $this->elementEnd('li');
433 $this->elementStart('li');
434 // TRANS: Field label on account registration page.
435 $this->input('fullname', _('Full name'),
436 $this->trimmed('fullname'),
437 // TRANS: Field title on account registration page.
438 _('Longer name, preferably your "real" name.'));
439 $this->elementEnd('li');
440 $this->elementStart('li');
441 // TRANS: Field label on account registration page.
442 $this->input('homepage', _('Homepage'),
443 $this->trimmed('homepage'),
444 // TRANS: Field title on account registration page.
445 _('URL of your homepage, blog, '.
446 'or profile on another site.'));
447 $this->elementEnd('li');
448 $this->elementStart('li');
449 $maxBio = Profile::maxBio();
451 // TRANS: Text area title in form for account registration. Plural
452 // TRANS: is decided by the number of characters available for the
453 // TRANS: biography (%d).
454 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
455 'Describe yourself and your interests in %d characters.',
459 // TRANS: Text area title on account registration page.
460 $bioInstr = _('Describe yourself and your interests.');
462 // TRANS: Text area label on account registration page.
463 $this->textarea('bio', _('Bio'),
464 $this->trimmed('bio'),
466 $this->elementEnd('li');
467 $this->elementStart('li');
468 // TRANS: Field label on account registration page.
469 $this->input('location', _('Location'),
470 $this->trimmed('location'),
471 // TRANS: Field title on account registration page.
472 _('Where you are, like "City, '.
473 'State (or Region), Country".'));
474 $this->elementEnd('li');
475 Event::handle('EndRegistrationFormData', array($this));
476 $this->elementStart('li', array('id' => 'settings_rememberme'));
477 // TRANS: Checkbox label on account registration page.
478 $this->checkbox('rememberme', _('Remember me'),
479 $this->boolean('rememberme'),
480 // TRANS: Checkbox title on account registration page.
481 _('Automatically login in the future; '.
482 'not for shared computers!'));
483 $this->elementEnd('li');
484 $attrs = array('type' => 'checkbox',
486 'class' => 'checkbox',
489 if ($this->boolean('license')) {
490 $attrs['checked'] = 'checked';
492 $this->elementStart('li');
493 $this->element('input', $attrs);
494 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
495 $this->raw($this->licenseCheckbox());
496 $this->elementEnd('label');
497 $this->elementEnd('li');
499 $this->elementEnd('ul');
500 // TRANS: Button text to register a user on account registration page.
501 $this->submit('submit', _m('BUTTON','Register'));
502 $this->elementEnd('fieldset');
503 $this->elementEnd('form');
506 function licenseCheckbox()
509 switch (common_config('license', 'type')) {
511 $out .= htmlspecialchars(sprintf(
512 // TRANS: Copyright checkbox label in registration dialog, for private sites.
513 // TRANS: %1$s is the StatusNet sitename.
514 _('I understand that content and data of %1$s are private and confidential.'),
515 common_config('site', 'name')));
517 case 'allrightsreserved':
521 if (common_config('license', 'owner')) {
522 $out .= htmlspecialchars(sprintf(
523 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
524 // TRANS: %1$s is the license owner.
525 _('My text and files are copyright by %1$s.'),
526 common_config('license', 'owner')));
528 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
529 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
531 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
532 $out .= ' ' . _('All rights reserved.');
534 case 'cc': // fall through
536 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
537 $message = _('My text and files are available under %s ' .
538 'except this private data: password, ' .
539 'email address, IM address, and phone number.');
540 $link = '<a href="' .
541 htmlspecialchars(common_config('license', 'url')) .
543 htmlspecialchars(common_config('license', 'title')) .
545 $out .= sprintf(htmlspecialchars($message), $link);
551 * Show some information about registering for the site
553 * Save the registration flag, run showPage
557 function showSuccess()
559 $this->registered = true;
564 * Show some information about registering for the site
566 * Gives some information and options for new registrees.
570 function showSuccessContent()
572 if (Event::handle('StartRegisterSuccess', array($this))) {
573 $nickname = $this->arg('nickname');
575 $profileurl = common_local_url('showstream',
576 array('nickname' => $nickname));
578 $this->elementStart('div', 'success');
579 // TRANS: Text displayed after successful account registration.
580 // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
581 // TRANS: This message contains Markdown links in the form [link text](link)
582 // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
583 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
584 'From here, you may want to...'. "\n\n" .
585 '* Go to [your profile](%2$s) '.
586 'and post your first message.' . "\n" .
587 '* Add a [Jabber/GTalk address]'.
588 '(%%%%action.imsettings%%%%) '.
589 'so you can send notices '.
590 'through instant messages.' . "\n" .
591 '* [Search for people](%%%%action.peoplesearch%%%%) '.
592 'that you may know or '.
593 'that share your interests. ' . "\n" .
594 '* Update your [profile settings]'.
595 '(%%%%action.profilesettings%%%%)'.
596 ' to tell others more about you. ' . "\n" .
597 '* Read over the [online docs](%%%%doc.help%%%%)'.
598 ' for features you may have missed. ' . "\n\n" .
599 'Thanks for signing up and we hope '.
600 'you enjoy using this service.'),
601 $nickname, $profileurl);
603 $this->raw(common_markup_to_html($instr));
605 $have_email = $this->trimmed('email');
607 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
608 $emailinstr = _('(You should receive a message by email '.
609 'momentarily, with ' .
610 'instructions on how to confirm '.
611 'your email address.)');
612 $this->raw(common_markup_to_html($emailinstr));
614 $this->elementEnd('div');
616 Event::handle('EndRegisterSuccess', array($this));
621 * Show the login group nav menu
625 function showLocalNav()
627 if (common_logged_in()) {
628 parent::showLocalNav();
630 $nav = new LoginGroupNav($this);
636 * Show a bit of login context
640 function showProfileBlock()
642 if (common_logged_in()) {
643 parent::showProfileBlock();