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
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') {
135 $this->tryRegister();
136 } catch (ClientException $e) {
137 $this->showForm($e->getMessage());
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::getKV($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.'));
194 $nickname = Nickname::normalize($nickname, true);
195 } catch (NicknameException $e) {
196 $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->emailExists($email)) {
209 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
210 $this->showForm(_('Email address already exists.'));
211 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
212 !common_valid_http_url($homepage)) {
213 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
214 $this->showForm(_('Homepage is not a valid URL.'));
215 } else if (Profile::bioTooLong($bio)) {
216 // TRANS: Form validation error on registration page when providing too long a bio text.
217 // TRANS: %d is the maximum number of characters for bio; used for plural.
218 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
219 'Bio is too long (maximum %d characters).',
222 } else if (strlen($password) < 6) {
223 // TRANS: Form validation error displayed when trying to register with too short a password.
224 $this->showForm(_('Password must be 6 or more characters.'));
225 } else if ($password != $confirm) {
226 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
227 $this->showForm(_('Passwords do not match.'));
230 $user = User::register(array('nickname' => $nickname,
231 'password' => $password,
233 'fullname' => $fullname,
234 'homepage' => $homepage,
236 'location' => $location,
239 if (!common_set_user($user)) {
240 // TRANS: Server error displayed when saving fails during user registration.
241 $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 // Re-init language env in case it changed (not yet, but soon)
251 common_init_language();
253 Event::handle('EndRegistrationTry', array($this));
255 $this->showSuccess();
256 } catch (Exception $e) {
257 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
258 $this->showForm($e->getMessage());
265 * Does the given email address already exist?
267 * Checks a canonical email address against the database.
269 * @param string $email email address to check
271 * @return boolean true if the address already exists
273 function emailExists($email)
275 $email = common_canonical_email($email);
276 if (!$email || strlen($email) == 0) {
279 $user = User::getKV('email', $email);
280 return is_object($user);
283 // overrrided to add entry-title class
284 function showPageTitle() {
285 if (Event::handle('StartShowPageTitle', array($this))) {
286 $this->element('h1', array('class' => 'entry-title'), $this->title());
290 // overrided to add h-entry, and content-inner class
291 function showContentBlock()
293 $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
294 $this->showPageTitle();
295 $this->showPageNoticeBlock();
296 $this->elementStart('div', array('id' => 'content_inner',
297 'class' => 'e-content'));
298 // show the actual content (forms, lists, whatever)
299 $this->showContent();
300 $this->elementEnd('div');
301 $this->elementEnd('div');
305 * Instructions or a notice for the page
307 * Shows the error, if any, or instructions for registration.
311 function showPageNotice()
313 if ($this->registered) {
315 } else if ($this->error) {
316 $this->element('p', 'error', $this->error);
319 // TRANS: Page notice on registration page.
320 common_markup_to_html(_('With this form you can create '.
322 'You can then post notices and '.
323 'link up to friends and colleagues.'));
325 $this->elementStart('div', 'instructions');
327 $this->elementEnd('div');
332 * Wrapper for showing a page
334 * Stores an error and shows the page
336 * @param string $error Error, if any
340 function showForm($error=null)
342 $this->error = $error;
347 * Show the page content
349 * Either shows the registration form or, if registration was successful,
350 * instructions for using the site.
354 function showContent()
356 if ($this->registered) {
357 $this->showSuccessContent();
359 $this->showFormContent();
364 * Show the registration form
368 function showFormContent()
370 $code = $this->trimmed('code');
375 $invite = Invitation::getKV($code);
378 if (common_config('site', 'inviteonly') && !($code && $invite)) {
379 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
380 $this->clientError(_('Sorry, only invited people can register.'));
383 $this->elementStart('form', array('method' => 'post',
384 'id' => 'form_register',
385 'class' => 'form_settings',
386 'action' => common_local_url('register')));
387 $this->elementStart('fieldset');
388 // TRANS: Fieldset legend on accout registration page.
389 $this->element('legend', null, 'Account settings');
390 $this->hidden('token', common_session_token());
393 $this->hidden('code', $this->code);
396 $this->elementStart('ul', 'form_data');
397 if (Event::handle('StartRegistrationFormData', array($this))) {
398 $this->elementStart('li');
399 // TRANS: Field label on account registration page.
400 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
401 // TRANS: Field title on account registration page.
402 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
403 $this->elementEnd('li');
404 $this->elementStart('li');
405 // TRANS: Field label on account registration page.
406 $this->password('password', _('Password'),
407 // TRANS: Field title on account registration page.
408 _('6 or more characters.'));
409 $this->elementEnd('li');
410 $this->elementStart('li');
411 // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
412 $this->password('confirm', _m('PASSWORD','Confirm'),
413 // TRANS: Field title on account registration page.
414 _('Same as password above.'));
415 $this->elementEnd('li');
416 $this->elementStart('li');
417 if ($this->invite && $this->invite->address_type == 'email') {
418 // TRANS: Field label on account registration page.
419 $this->input('email', _m('LABEL','Email'), $this->invite->address,
420 // TRANS: Field title on account registration page.
421 _('Used only for updates, announcements, '.
422 'and password recovery.'));
424 // TRANS: Field label on account registration page.
425 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
426 // TRANS: Field title on account registration page.
427 _('Used only for updates, announcements, '.
428 'and password recovery.'));
430 $this->elementEnd('li');
431 $this->elementStart('li');
432 // TRANS: Field label on account registration page.
433 $this->input('fullname', _('Full name'),
434 $this->trimmed('fullname'),
435 // TRANS: Field title on account registration page.
436 _('Longer name, preferably your "real" name.'));
437 $this->elementEnd('li');
438 $this->elementStart('li');
439 // TRANS: Field label on account registration page.
440 $this->input('homepage', _('Homepage'),
441 $this->trimmed('homepage'),
442 // TRANS: Field title on account registration page.
443 _('URL of your homepage, blog, '.
444 'or profile on another site.'));
445 $this->elementEnd('li');
446 $this->elementStart('li');
447 $maxBio = Profile::maxBio();
449 // TRANS: Text area title in form for account registration. Plural
450 // TRANS: is decided by the number of characters available for the
451 // TRANS: biography (%d).
452 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
453 'Describe yourself and your interests in %d characters.',
457 // TRANS: Text area title on account registration page.
458 $bioInstr = _('Describe yourself and your interests.');
460 // TRANS: Text area label on account registration page.
461 $this->textarea('bio', _('Bio'),
462 $this->trimmed('bio'),
464 $this->elementEnd('li');
465 $this->elementStart('li');
466 // TRANS: Field label on account registration page.
467 $this->input('location', _('Location'),
468 $this->trimmed('location'),
469 // TRANS: Field title on account registration page.
470 _('Where you are, like "City, '.
471 'State (or Region), Country".'));
472 $this->elementEnd('li');
473 Event::handle('EndRegistrationFormData', array($this));
474 $this->elementStart('li', array('id' => 'settings_rememberme'));
475 // TRANS: Checkbox label on account registration page.
476 $this->checkbox('rememberme', _('Remember me'),
477 $this->boolean('rememberme'),
478 // TRANS: Checkbox title on account registration page.
479 _('Automatically login in the future; '.
480 'not for shared computers!'));
481 $this->elementEnd('li');
482 $attrs = array('type' => 'checkbox',
484 'class' => 'checkbox',
486 'required' => 'true',
488 if ($this->boolean('license')) {
489 $attrs['checked'] = 'checked';
491 $this->elementStart('li');
492 $this->element('input', $attrs);
493 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
494 $this->raw($this->licenseCheckbox());
495 $this->elementEnd('label');
496 $this->elementEnd('li');
498 $this->elementEnd('ul');
499 // TRANS: Button text to register a user on account registration page.
500 $this->submit('submit', _m('BUTTON','Register'));
501 $this->elementEnd('fieldset');
502 $this->elementEnd('form');
505 function licenseCheckbox()
508 switch (common_config('license', 'type')) {
510 $out .= htmlspecialchars(sprintf(
511 // TRANS: Copyright checkbox label in registration dialog, for private sites.
512 // TRANS: %1$s is the StatusNet sitename.
513 _('I understand that content and data of %1$s are private and confidential.'),
514 common_config('site', 'name')));
516 case 'allrightsreserved':
520 if (common_config('license', 'owner')) {
521 $out .= htmlspecialchars(sprintf(
522 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
523 // TRANS: %1$s is the license owner.
524 _('My text and files are copyright by %1$s.'),
525 common_config('license', 'owner')));
527 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
528 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
530 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
531 $out .= ' ' . _('All rights reserved.');
533 case 'cc': // fall through
535 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
536 $message = _('My text and files are available under %s ' .
537 'except this private data: password, ' .
538 'email address, IM address, and phone number.');
539 $link = '<a href="' .
540 htmlspecialchars(common_config('license', 'url')) .
542 htmlspecialchars(common_config('license', 'title')) .
544 $out .= sprintf(htmlspecialchars($message), $link);
550 * Show some information about registering for the site
552 * Save the registration flag, run showPage
556 function showSuccess()
558 $this->registered = true;
563 * Show some information about registering for the site
565 * Gives some information and options for new registrees.
569 function showSuccessContent()
571 if (Event::handle('StartRegisterSuccess', array($this))) {
572 $nickname = $this->arg('nickname');
574 $profileurl = common_local_url('showstream',
575 array('nickname' => $nickname));
577 $this->elementStart('div', 'success');
578 // TRANS: Text displayed after successful account registration.
579 // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
580 // TRANS: This message contains Markdown links in the form [link text](link)
581 // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
582 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
583 'From here, you may want to...'. "\n\n" .
584 '* Go to [your profile](%2$s) '.
585 'and post your first message.' . "\n" .
586 '* Add a [Jabber/GTalk address]'.
587 '(%%%%action.imsettings%%%%) '.
588 'so you can send notices '.
589 'through instant messages.' . "\n" .
590 '* [Search for people](%%%%action.peoplesearch%%%%) '.
591 'that you may know or '.
592 'that share your interests. ' . "\n" .
593 '* Update your [profile settings]'.
594 '(%%%%action.profilesettings%%%%)'.
595 ' to tell others more about you. ' . "\n" .
596 '* Read over the [online docs](%%%%doc.help%%%%)'.
597 ' for features you may have missed. ' . "\n\n" .
598 'Thanks for signing up and we hope '.
599 'you enjoy using this service.'),
600 $nickname, $profileurl);
602 $this->raw(common_markup_to_html($instr));
604 $have_email = $this->trimmed('email');
606 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
607 $emailinstr = _('(You should receive a message by email '.
608 'momentarily, with ' .
609 'instructions on how to confirm '.
610 'your email address.)');
611 $this->raw(common_markup_to_html($emailinstr));
613 $this->elementEnd('div');
615 Event::handle('EndRegisterSuccess', array($this));
620 * Show the login group nav menu
624 function showLocalNav()
626 if (common_logged_in()) {
627 parent::showLocalNav();
629 $nav = new LoginGroupNav($this);
635 * Show a bit of login context
639 function showProfileBlock()
641 if (common_logged_in()) {
642 parent::showProfileBlock();