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/
44 class RegisterAction extends Action
47 * Has there been an error?
56 var $registered = false;
59 * Are we processing an invite?
69 * @return string title
72 function prepare($args)
74 parent::prepare($args);
75 $this->code = $this->trimmed('code');
77 // @todo this check should really be in index.php for all sensitive actions
78 $ssl = common_config('site', 'ssl');
79 if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
80 common_redirect(common_local_url('register'));
84 if (empty($this->code)) {
85 common_ensure_session();
86 if (array_key_exists('invitecode', $_SESSION)) {
87 $this->code = $_SESSION['invitecode'];
91 if (common_config('site', 'inviteonly') && empty($this->code)) {
92 $this->clientError(_('Sorry, only invited people can register.'));
96 if (!empty($this->code)) {
97 $this->invite = Invitation::staticGet('code', $this->code);
98 if (empty($this->invite)) {
99 $this->clientError(_('Sorry, invalid invitation code.'));
102 // Store this in case we need it
103 common_ensure_session();
104 $_SESSION['invitecode'] = $this->code;
113 * @return string title
118 if ($this->registered) {
119 return _('Registration successful');
121 return _('Register');
126 * Handle input, produce output
128 * Switches on request method; either shows the form or handles its input.
130 * Checks if registration is closed and shows an error if so.
132 * @param array $args $_REQUEST data
137 function handle($args)
139 parent::handle($args);
141 if (common_config('site', 'closed')) {
142 $this->clientError(_('Registration not allowed.'));
143 } else if (common_logged_in()) {
144 $this->clientError(_('Already logged in.'));
145 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
146 $this->tryRegister();
152 function showScripts()
154 parent::showScripts();
155 $this->autofocus('nickname');
159 * Try to register a user
161 * Validates the input and tries to save a new user and profile
162 * record. On success, shows an instructions page.
167 function tryRegister()
169 if (Event::handle('StartRegistrationTry', array($this))) {
170 $token = $this->trimmed('token');
171 if (!$token || $token != common_session_token()) {
172 $this->showForm(_('There was a problem with your session token. '.
173 'Try again, please.'));
177 $nickname = $this->trimmed('nickname');
178 $email = $this->trimmed('email');
179 $fullname = $this->trimmed('fullname');
180 $homepage = $this->trimmed('homepage');
181 $bio = $this->trimmed('bio');
182 $location = $this->trimmed('location');
184 // We don't trim these... whitespace is OK in a password!
185 $password = $this->arg('password');
186 $confirm = $this->arg('confirm');
188 // invitation code, if any
189 $code = $this->trimmed('code');
192 $invite = Invitation::staticGet($code);
195 if (common_config('site', 'inviteonly') && !($code && $invite)) {
196 $this->clientError(_('Sorry, only invited people can register.'));
201 $nickname = common_canonical_nickname($nickname);
202 $email = common_canonical_email($email);
204 if (!$this->boolean('license')) {
205 $this->showForm(_('You can\'t register if you don\'t '.
206 'agree to the license.'));
207 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
208 $this->showForm(_('Not a valid email address.'));
209 } else if (!Validate::string($nickname, array('min_length' => 1,
211 'format' => NICKNAME_FMT))) {
212 $this->showForm(_('Nickname must have only lowercase letters '.
213 'and numbers and no spaces.'));
214 } else if ($this->nicknameExists($nickname)) {
215 $this->showForm(_('Nickname already in use. Try another one.'));
216 } else if (!User::allowed_nickname($nickname)) {
217 $this->showForm(_('Not a valid nickname.'));
218 } else if ($this->emailExists($email)) {
219 $this->showForm(_('Email address already exists.'));
220 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
221 !Validate::uri($homepage,
222 array('allowed_schemes' =>
223 array('http', 'https')))) {
224 $this->showForm(_('Homepage is not a valid URL.'));
226 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
227 $this->showForm(_('Full name is too long (maximum 255 characters).'));
229 } else if (Profile::bioTooLong($bio)) {
230 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
231 'Bio is too long (maximum %d characters).',
235 } else if (!is_null($location) && mb_strlen($location) > 255) {
236 $this->showForm(_('Location is too long (maximum 255 characters).'));
238 } else if (strlen($password) < 6) {
239 $this->showForm(_('Password must be 6 or more characters.'));
241 } else if ($password != $confirm) {
242 $this->showForm(_('Passwords don\'t match.'));
243 } else if ($user = User::register(array('nickname' => $nickname,
244 'password' => $password,
246 'fullname' => $fullname,
247 'homepage' => $homepage,
249 'location' => $location,
252 $this->showForm(_('Invalid username or password.'));
256 if (!common_set_user($user)) {
257 $this->serverError(_('Error setting user.'));
260 // this is a real login
261 common_real_login(true);
262 if ($this->boolean('rememberme')) {
263 common_debug('Adding rememberme cookie for ' . $nickname);
264 common_rememberme($user);
267 Event::handle('EndRegistrationTry', array($this));
269 // Re-init language env in case it changed (not yet, but soon)
270 common_init_language();
272 $this->showSuccess();
274 $this->showForm(_('Invalid username or password.'));
280 * Does the given nickname already exist?
282 * Checks a canonical nickname against the database.
284 * @param string $nickname nickname to check
286 * @return boolean true if the nickname already exists
289 function nicknameExists($nickname)
291 $user = User::staticGet('nickname', $nickname);
292 return is_object($user);
296 * Does the given email address already exist?
298 * Checks a canonical email address against the database.
300 * @param string $email email address to check
302 * @return boolean true if the address already exists
305 function emailExists($email)
307 $email = common_canonical_email($email);
308 if (!$email || strlen($email) == 0) {
311 $user = User::staticGet('email', $email);
312 return is_object($user);
315 // overrrided to add entry-title class
316 function showPageTitle() {
317 if (Event::handle('StartShowPageTitle', array($this))) {
318 $this->element('h1', array('class' => 'entry-title'), $this->title());
322 // overrided to add hentry, and content-inner class
323 function showContentBlock()
325 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
326 $this->showPageTitle();
327 $this->showPageNoticeBlock();
328 $this->elementStart('div', array('id' => 'content_inner',
329 'class' => 'entry-content'));
330 // show the actual content (forms, lists, whatever)
331 $this->showContent();
332 $this->elementEnd('div');
333 $this->elementEnd('div');
337 * Instructions or a notice for the page
339 * Shows the error, if any, or instructions for registration.
344 function showPageNotice()
346 if ($this->registered) {
348 } else if ($this->error) {
349 $this->element('p', 'error', $this->error);
352 common_markup_to_html(_('With this form you can create '.
354 'You can then post notices and '.
355 'link up to friends and colleagues. '));
357 $this->elementStart('div', 'instructions');
359 $this->elementEnd('div');
364 * Wrapper for showing a page
366 * Stores an error and shows the page
368 * @param string $error Error, if any
373 function showForm($error=null)
375 $this->error = $error;
380 * Show the page content
382 * Either shows the registration form or, if registration was successful,
383 * instructions for using the site.
388 function showContent()
390 if ($this->registered) {
391 $this->showSuccessContent();
393 $this->showFormContent();
398 * 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 $this->clientError(_('Sorry, only invited people can register.'));
418 $this->elementStart('form', array('method' => 'post',
419 'id' => 'form_register',
420 'class' => 'form_settings',
421 'action' => common_local_url('register')));
422 $this->elementStart('fieldset');
423 $this->element('legend', null, 'Account settings');
424 $this->hidden('token', common_session_token());
427 $this->hidden('code', $this->code);
430 $this->elementStart('ul', 'form_data');
431 if (Event::handle('StartRegistrationFormData', array($this))) {
432 $this->elementStart('li');
433 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
434 _('1-64 lowercase letters or numbers, '.
435 'no punctuation or spaces. Required.'));
436 $this->elementEnd('li');
437 $this->elementStart('li');
438 $this->password('password', _('Password'),
439 _('6 or more characters. Required.'));
440 $this->elementEnd('li');
441 $this->elementStart('li');
442 $this->password('confirm', _('Confirm'),
443 _('Same as password above. Required.'));
444 $this->elementEnd('li');
445 $this->elementStart('li');
446 if ($this->invite && $this->invite->address_type == 'email') {
447 $this->input('email', _('Email'), $this->invite->address,
448 _('Used only for updates, announcements, '.
449 'and password recovery'));
451 $this->input('email', _('Email'), $this->trimmed('email'),
452 _('Used only for updates, announcements, '.
453 'and password recovery'));
455 $this->elementEnd('li');
456 $this->elementStart('li');
457 $this->input('fullname', _('Full name'),
458 $this->trimmed('fullname'),
459 _('Longer name, preferably your "real" name'));
460 $this->elementEnd('li');
461 $this->elementStart('li');
462 $this->input('homepage', _('Homepage'),
463 $this->trimmed('homepage'),
464 _('URL of your homepage, blog, '.
465 'or profile on another site'));
466 $this->elementEnd('li');
467 $this->elementStart('li');
468 $maxBio = Profile::maxBio();
470 // TRANS: Tooltip for field label in form for profile settings. Plural
471 // TRANS: is decided by the number of characters available for the
472 // TRANS: biography (%d).
473 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
474 'Describe yourself and your interests in %d characters',
478 $bioInstr = _('Describe yourself and your interests');
480 $this->textarea('bio', _('Bio'),
481 $this->trimmed('bio'),
483 $this->elementEnd('li');
484 $this->elementStart('li');
485 $this->input('location', _('Location'),
486 $this->trimmed('location'),
487 _('Where you are, like "City, '.
488 'State (or Region), Country"'));
489 $this->elementEnd('li');
490 Event::handle('EndRegistrationFormData', array($this));
491 $this->elementStart('li', array('id' => 'settings_rememberme'));
492 $this->checkbox('rememberme', _('Remember me'),
493 $this->boolean('rememberme'),
494 _('Automatically login in the future; '.
495 'not for shared computers!'));
496 $this->elementEnd('li');
497 $attrs = array('type' => 'checkbox',
499 'class' => 'checkbox',
502 if ($this->boolean('license')) {
503 $attrs['checked'] = 'checked';
505 $this->elementStart('li');
506 $this->element('input', $attrs);
507 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
508 $this->raw($this->licenseCheckbox());
509 $this->elementEnd('label');
510 $this->elementEnd('li');
512 $this->elementEnd('ul');
513 $this->submit('submit', _('Register'));
514 $this->elementEnd('fieldset');
515 $this->elementEnd('form');
518 function licenseCheckbox()
521 switch (common_config('license', 'type')) {
523 // TRANS: Copyright checkbox label in registration dialog, for private sites.
524 $out .= htmlspecialchars(sprintf(
525 _('I understand that content and data of %1$s are private and confidential.'),
526 common_config('site', 'name')));
528 case 'allrightsreserved':
532 if (common_config('license', 'owner')) {
533 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
534 $out .= htmlspecialchars(sprintf(
535 _('My text and files are copyright by %1$s.'),
536 common_config('license', 'owner')));
538 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
539 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
541 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
542 $out .= ' ' . _('All rights reserved.');
544 case 'cc': // fall through
546 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
547 $message = _('My text and files are available under %s ' .
548 'except this private data: password, ' .
549 'email address, IM address, and phone number.');
550 $link = '<a href="' .
551 htmlspecialchars(common_config('license', 'url')) .
553 htmlspecialchars(common_config('license', 'title')) .
555 $out .= sprintf(htmlspecialchars($message), $link);
561 * Show some information about registering for the site
563 * Save the registration flag, run showPage
568 function showSuccess()
570 $this->registered = true;
575 * Show some information about registering for the site
577 * Gives some information and options for new registrees.
582 function showSuccessContent()
584 $nickname = $this->arg('nickname');
586 $profileurl = common_local_url('showstream',
587 array('nickname' => $nickname));
589 $this->elementStart('div', 'success');
590 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
591 'From here, you may want to...'. "\n\n" .
592 '* Go to [your profile](%2$s) '.
593 'and post your first message.' . "\n" .
594 '* Add a [Jabber/GTalk address]'.
595 '(%%%%action.imsettings%%%%) '.
596 'so you can send notices '.
597 'through instant messages.' . "\n" .
598 '* [Search for people](%%%%action.peoplesearch%%%%) '.
599 'that you may know or '.
600 'that share your interests. ' . "\n" .
601 '* Update your [profile settings]'.
602 '(%%%%action.profilesettings%%%%)'.
603 ' to tell others more about you. ' . "\n" .
604 '* Read over the [online docs](%%%%doc.help%%%%)'.
605 ' for features you may have missed. ' . "\n\n" .
606 'Thanks for signing up and we hope '.
607 'you enjoy using this service.'),
608 $nickname, $profileurl);
610 $this->raw(common_markup_to_html($instr));
612 $have_email = $this->trimmed('email');
614 $emailinstr = _('(You should receive a message by email '.
615 'momentarily, with ' .
616 'instructions on how to confirm '.
617 'your email address.)');
618 $this->raw(common_markup_to_html($emailinstr));
620 $this->elementEnd('div');
624 * Show the login group nav menu
629 function showLocalNav()
631 $nav = new LoginGroupNav($this);