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 if (empty($this->code)) {
78 common_ensure_session();
79 if (array_key_exists('invitecode', $_SESSION)) {
80 $this->code = $_SESSION['invitecode'];
84 if (common_config('site', 'inviteonly') && empty($this->code)) {
85 $this->clientError(_('Sorry, only invited people can register.'));
89 if (!empty($this->code)) {
90 $this->invite = Invitation::staticGet('code', $this->code);
91 if (empty($this->invite)) {
92 $this->clientError(_('Sorry, invalid invitation code.'));
95 // Store this in case we need it
96 common_ensure_session();
97 $_SESSION['invitecode'] = $this->code;
106 * @return string title
111 if ($this->registered) {
112 return _('Registration successful');
114 return _('Register');
119 * Handle input, produce output
121 * Switches on request method; either shows the form or handles its input.
123 * Checks if registration is closed and shows an error if so.
125 * @param array $args $_REQUEST data
130 function handle($args)
132 parent::handle($args);
134 if (common_config('site', 'closed')) {
135 $this->clientError(_('Registration not allowed.'));
136 } else if (common_logged_in()) {
137 $this->clientError(_('Already logged in.'));
138 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
139 $this->tryRegister();
145 function showScripts()
147 parent::showScripts();
148 $this->autofocus('nickname');
152 * Try to register a user
154 * Validates the input and tries to save a new user and profile
155 * record. On success, shows an instructions page.
160 function tryRegister()
162 if (Event::handle('StartRegistrationTry', array($this))) {
163 $token = $this->trimmed('token');
164 if (!$token || $token != common_session_token()) {
165 $this->showForm(_('There was a problem with your session token. '.
166 'Try again, please.'));
170 $nickname = $this->trimmed('nickname');
171 $email = $this->trimmed('email');
172 $fullname = $this->trimmed('fullname');
173 $homepage = $this->trimmed('homepage');
174 $bio = $this->trimmed('bio');
175 $location = $this->trimmed('location');
177 // We don't trim these... whitespace is OK in a password!
178 $password = $this->arg('password');
179 $confirm = $this->arg('confirm');
181 // invitation code, if any
182 $code = $this->trimmed('code');
185 $invite = Invitation::staticGet($code);
188 if (common_config('site', 'inviteonly') && !($code && $invite)) {
189 $this->clientError(_('Sorry, only invited people can register.'));
195 $nickname = Nickname::normalize($nickname);
196 } catch (NicknameException $e) {
197 $this->showForm($e->getMessage());
199 $email = common_canonical_email($email);
201 if (!$this->boolean('license')) {
202 $this->showForm(_('You can\'t register if you don\'t '.
203 'agree to the license.'));
204 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
205 $this->showForm(_('Not a valid email address.'));
206 } else if ($this->nicknameExists($nickname)) {
207 $this->showForm(_('Nickname already in use. Try another one.'));
208 } else if (!User::allowed_nickname($nickname)) {
209 $this->showForm(_('Not a valid nickname.'));
210 } else if ($this->emailExists($email)) {
211 $this->showForm(_('Email address already exists.'));
212 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
213 !Validate::uri($homepage,
214 array('allowed_schemes' =>
215 array('http', 'https')))) {
216 $this->showForm(_('Homepage is not a valid URL.'));
218 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
219 $this->showForm(_('Full name is too long (maximum 255 characters).'));
221 } else if (Profile::bioTooLong($bio)) {
222 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
223 'Bio is too long (maximum %d characters).',
227 } else if (!is_null($location) && mb_strlen($location) > 255) {
228 $this->showForm(_('Location is too long (maximum 255 characters).'));
230 } else if (strlen($password) < 6) {
231 $this->showForm(_('Password must be 6 or more characters.'));
233 } else if ($password != $confirm) {
234 $this->showForm(_('Passwords don\'t match.'));
235 } else if ($user = User::register(array('nickname' => $nickname,
236 'password' => $password,
238 'fullname' => $fullname,
239 'homepage' => $homepage,
241 'location' => $location,
244 $this->showForm(_('Invalid username or password.'));
248 if (!common_set_user($user)) {
249 $this->serverError(_('Error setting user.'));
252 // this is a real login
253 common_real_login(true);
254 if ($this->boolean('rememberme')) {
255 common_debug('Adding rememberme cookie for ' . $nickname);
256 common_rememberme($user);
259 Event::handle('EndRegistrationTry', array($this));
261 // Re-init language env in case it changed (not yet, but soon)
262 common_init_language();
264 $this->showSuccess();
266 $this->showForm(_('Invalid username or password.'));
272 * Does the given nickname already exist?
274 * Checks a canonical nickname against the database.
276 * @param string $nickname nickname to check
278 * @return boolean true if the nickname already exists
281 function nicknameExists($nickname)
283 $user = User::staticGet('nickname', $nickname);
284 return is_object($user);
288 * Does the given email address already exist?
290 * Checks a canonical email address against the database.
292 * @param string $email email address to check
294 * @return boolean true if the address already exists
297 function emailExists($email)
299 $email = common_canonical_email($email);
300 if (!$email || strlen($email) == 0) {
303 $user = User::staticGet('email', $email);
304 return is_object($user);
307 // overrrided to add entry-title class
308 function showPageTitle() {
309 if (Event::handle('StartShowPageTitle', array($this))) {
310 $this->element('h1', array('class' => 'entry-title'), $this->title());
314 // overrided to add hentry, and content-inner class
315 function showContentBlock()
317 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
318 $this->showPageTitle();
319 $this->showPageNoticeBlock();
320 $this->elementStart('div', array('id' => 'content_inner',
321 'class' => 'entry-content'));
322 // show the actual content (forms, lists, whatever)
323 $this->showContent();
324 $this->elementEnd('div');
325 $this->elementEnd('div');
329 * Instructions or a notice for the page
331 * Shows the error, if any, or instructions for registration.
336 function showPageNotice()
338 if ($this->registered) {
340 } else if ($this->error) {
341 $this->element('p', 'error', $this->error);
344 common_markup_to_html(_('With this form you can create '.
346 'You can then post notices and '.
347 'link up to friends and colleagues. '));
349 $this->elementStart('div', 'instructions');
351 $this->elementEnd('div');
356 * Wrapper for showing a page
358 * Stores an error and shows the page
360 * @param string $error Error, if any
365 function showForm($error=null)
367 $this->error = $error;
372 * Show the page content
374 * Either shows the registration form or, if registration was successful,
375 * instructions for using the site.
380 function showContent()
382 if ($this->registered) {
383 $this->showSuccessContent();
385 $this->showFormContent();
390 * Show the registration form
395 function showFormContent()
397 $code = $this->trimmed('code');
402 $invite = Invitation::staticGet($code);
405 if (common_config('site', 'inviteonly') && !($code && $invite)) {
406 $this->clientError(_('Sorry, only invited people can register.'));
410 $this->elementStart('form', array('method' => 'post',
411 'id' => 'form_register',
412 'class' => 'form_settings',
413 'action' => common_local_url('register')));
414 $this->elementStart('fieldset');
415 $this->element('legend', null, 'Account settings');
416 $this->hidden('token', common_session_token());
419 $this->hidden('code', $this->code);
422 $this->elementStart('ul', 'form_data');
423 if (Event::handle('StartRegistrationFormData', array($this))) {
424 $this->elementStart('li');
425 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
426 _('1-64 lowercase letters or numbers, '.
427 'no punctuation or spaces. Required.'));
428 $this->elementEnd('li');
429 $this->elementStart('li');
430 $this->password('password', _('Password'),
431 _('6 or more characters. Required.'));
432 $this->elementEnd('li');
433 $this->elementStart('li');
434 $this->password('confirm', _('Confirm'),
435 _('Same as password above. Required.'));
436 $this->elementEnd('li');
437 $this->elementStart('li');
438 if ($this->invite && $this->invite->address_type == 'email') {
439 $this->input('email', _('Email'), $this->invite->address,
440 _('Used only for updates, announcements, '.
441 'and password recovery'));
443 $this->input('email', _('Email'), $this->trimmed('email'),
444 _('Used only for updates, announcements, '.
445 'and password recovery'));
447 $this->elementEnd('li');
448 $this->elementStart('li');
449 $this->input('fullname', _('Full name'),
450 $this->trimmed('fullname'),
451 _('Longer name, preferably your "real" name'));
452 $this->elementEnd('li');
453 $this->elementStart('li');
454 $this->input('homepage', _('Homepage'),
455 $this->trimmed('homepage'),
456 _('URL of your homepage, blog, '.
457 'or profile on another site'));
458 $this->elementEnd('li');
459 $this->elementStart('li');
460 $maxBio = Profile::maxBio();
462 // TRANS: Tooltip for field label in form for profile settings. Plural
463 // TRANS: is decided by the number of characters available for the
464 // TRANS: biography (%d).
465 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
466 'Describe yourself and your interests in %d characters',
470 $bioInstr = _('Describe yourself and your interests');
472 $this->textarea('bio', _('Bio'),
473 $this->trimmed('bio'),
475 $this->elementEnd('li');
476 $this->elementStart('li');
477 $this->input('location', _('Location'),
478 $this->trimmed('location'),
479 _('Where you are, like "City, '.
480 'State (or Region), Country"'));
481 $this->elementEnd('li');
482 Event::handle('EndRegistrationFormData', array($this));
483 $this->elementStart('li', array('id' => 'settings_rememberme'));
484 $this->checkbox('rememberme', _('Remember me'),
485 $this->boolean('rememberme'),
486 _('Automatically login in the future; '.
487 'not for shared computers!'));
488 $this->elementEnd('li');
489 $attrs = array('type' => 'checkbox',
491 'class' => 'checkbox',
494 if ($this->boolean('license')) {
495 $attrs['checked'] = 'checked';
497 $this->elementStart('li');
498 $this->element('input', $attrs);
499 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
500 $this->raw($this->licenseCheckbox());
501 $this->elementEnd('label');
502 $this->elementEnd('li');
504 $this->elementEnd('ul');
505 $this->submit('submit', _('Register'));
506 $this->elementEnd('fieldset');
507 $this->elementEnd('form');
510 function licenseCheckbox()
513 switch (common_config('license', 'type')) {
515 // TRANS: Copyright checkbox label in registration dialog, for private sites.
516 $out .= htmlspecialchars(sprintf(
517 _('I understand that content and data of %1$s are private and confidential.'),
518 common_config('site', 'name')));
520 case 'allrightsreserved':
524 if (common_config('license', 'owner')) {
525 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
526 $out .= htmlspecialchars(sprintf(
527 _('My text and files are copyright by %1$s.'),
528 common_config('license', 'owner')));
530 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
531 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
533 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
534 $out .= ' ' . _('All rights reserved.');
536 case 'cc': // fall through
538 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
539 $message = _('My text and files are available under %s ' .
540 'except this private data: password, ' .
541 'email address, IM address, and phone number.');
542 $link = '<a href="' .
543 htmlspecialchars(common_config('license', 'url')) .
545 htmlspecialchars(common_config('license', 'title')) .
547 $out .= sprintf(htmlspecialchars($message), $link);
553 * Show some information about registering for the site
555 * Save the registration flag, run showPage
560 function showSuccess()
562 $this->registered = true;
567 * Show some information about registering for the site
569 * Gives some information and options for new registrees.
574 function showSuccessContent()
576 $nickname = $this->arg('nickname');
578 $profileurl = common_local_url('showstream',
579 array('nickname' => $nickname));
581 $this->elementStart('div', 'success');
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 $emailinstr = _('(You should receive a message by email '.
607 'momentarily, with ' .
608 'instructions on how to confirm '.
609 'your email address.)');
610 $this->raw(common_markup_to_html($emailinstr));
612 $this->elementEnd('div');
616 * Show the login group nav menu
621 function showLocalNav()
623 $nav = new LoginGroupNav($this);