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.'));
202 $nickname = Nickname::normalize($nickname);
203 } catch (NicknameException $e) {
204 $this->showForm($e->getMessage());
206 $email = common_canonical_email($email);
208 if (!$this->boolean('license')) {
209 $this->showForm(_('You can\'t register if you don\'t '.
210 'agree to the license.'));
211 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
212 $this->showForm(_('Not a valid email address.'));
213 } else if ($this->nicknameExists($nickname)) {
214 $this->showForm(_('Nickname already in use. Try another one.'));
215 } else if (!User::allowed_nickname($nickname)) {
216 $this->showForm(_('Not a valid nickname.'));
217 } else if ($this->emailExists($email)) {
218 $this->showForm(_('Email address already exists.'));
219 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
220 !Validate::uri($homepage,
221 array('allowed_schemes' =>
222 array('http', 'https')))) {
223 $this->showForm(_('Homepage is not a valid URL.'));
225 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
226 $this->showForm(_('Full name is too long (maximum 255 characters).'));
228 } else if (Profile::bioTooLong($bio)) {
229 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
230 'Bio is too long (maximum %d characters).',
234 } else if (!is_null($location) && mb_strlen($location) > 255) {
235 $this->showForm(_('Location is too long (maximum 255 characters).'));
237 } else if (strlen($password) < 6) {
238 $this->showForm(_('Password must be 6 or more characters.'));
240 } else if ($password != $confirm) {
241 $this->showForm(_('Passwords don\'t match.'));
242 } else if ($user = User::register(array('nickname' => $nickname,
243 'password' => $password,
245 'fullname' => $fullname,
246 'homepage' => $homepage,
248 'location' => $location,
251 $this->showForm(_('Invalid username or password.'));
255 if (!common_set_user($user)) {
256 $this->serverError(_('Error setting user.'));
259 // this is a real login
260 common_real_login(true);
261 if ($this->boolean('rememberme')) {
262 common_debug('Adding rememberme cookie for ' . $nickname);
263 common_rememberme($user);
266 Event::handle('EndRegistrationTry', array($this));
268 // Re-init language env in case it changed (not yet, but soon)
269 common_init_language();
271 $this->showSuccess();
273 $this->showForm(_('Invalid username or password.'));
279 * Does the given nickname already exist?
281 * Checks a canonical nickname against the database.
283 * @param string $nickname nickname to check
285 * @return boolean true if the nickname already exists
288 function nicknameExists($nickname)
290 $user = User::staticGet('nickname', $nickname);
291 return is_object($user);
295 * Does the given email address already exist?
297 * Checks a canonical email address against the database.
299 * @param string $email email address to check
301 * @return boolean true if the address already exists
304 function emailExists($email)
306 $email = common_canonical_email($email);
307 if (!$email || strlen($email) == 0) {
310 $user = User::staticGet('email', $email);
311 return is_object($user);
314 // overrrided to add entry-title class
315 function showPageTitle() {
316 if (Event::handle('StartShowPageTitle', array($this))) {
317 $this->element('h1', array('class' => 'entry-title'), $this->title());
321 // overrided to add hentry, and content-inner class
322 function showContentBlock()
324 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
325 $this->showPageTitle();
326 $this->showPageNoticeBlock();
327 $this->elementStart('div', array('id' => 'content_inner',
328 'class' => 'entry-content'));
329 // show the actual content (forms, lists, whatever)
330 $this->showContent();
331 $this->elementEnd('div');
332 $this->elementEnd('div');
336 * Instructions or a notice for the page
338 * Shows the error, if any, or instructions for registration.
343 function showPageNotice()
345 if ($this->registered) {
347 } else if ($this->error) {
348 $this->element('p', 'error', $this->error);
351 common_markup_to_html(_('With this form you can create '.
353 'You can then post notices and '.
354 'link up to friends and colleagues. '));
356 $this->elementStart('div', 'instructions');
358 $this->elementEnd('div');
363 * Wrapper for showing a page
365 * Stores an error and shows the page
367 * @param string $error Error, if any
372 function showForm($error=null)
374 $this->error = $error;
379 * Show the page content
381 * Either shows the registration form or, if registration was successful,
382 * instructions for using the site.
387 function showContent()
389 if ($this->registered) {
390 $this->showSuccessContent();
392 $this->showFormContent();
397 * Show the registration form
402 function showFormContent()
404 $code = $this->trimmed('code');
409 $invite = Invitation::staticGet($code);
412 if (common_config('site', 'inviteonly') && !($code && $invite)) {
413 $this->clientError(_('Sorry, only invited people can register.'));
417 $this->elementStart('form', array('method' => 'post',
418 'id' => 'form_register',
419 'class' => 'form_settings',
420 'action' => common_local_url('register')));
421 $this->elementStart('fieldset');
422 $this->element('legend', null, 'Account settings');
423 $this->hidden('token', common_session_token());
426 $this->hidden('code', $this->code);
429 $this->elementStart('ul', 'form_data');
430 if (Event::handle('StartRegistrationFormData', array($this))) {
431 $this->elementStart('li');
432 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
433 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
434 $this->elementEnd('li');
435 $this->elementStart('li');
436 $this->password('password', _('Password'),
437 _('6 or more characters.'));
438 $this->elementEnd('li');
439 $this->elementStart('li');
440 $this->password('confirm', _('Confirm'),
441 _('Same as password above.'));
442 $this->elementEnd('li');
443 $this->elementStart('li');
444 if ($this->invite && $this->invite->address_type == 'email') {
445 $this->input('email', _('Email'), $this->invite->address,
446 _('Used only for updates, announcements, '.
447 'and password recovery'));
449 $this->input('email', _('Email'), $this->trimmed('email'),
450 _('Used only for updates, announcements, '.
451 'and password recovery'));
453 $this->elementEnd('li');
454 $this->elementStart('li');
455 $this->input('fullname', _('Full name'),
456 $this->trimmed('fullname'),
457 _('Longer name, preferably your "real" name'));
458 $this->elementEnd('li');
459 $this->elementStart('li');
460 $this->input('homepage', _('Homepage'),
461 $this->trimmed('homepage'),
462 _('URL of your homepage, blog, '.
463 'or profile on another site'));
464 $this->elementEnd('li');
465 $this->elementStart('li');
466 $maxBio = Profile::maxBio();
468 // TRANS: Tooltip for field label in form for profile settings. Plural
469 // TRANS: is decided by the number of characters available for the
470 // TRANS: biography (%d).
471 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
472 'Describe yourself and your interests in %d characters',
476 $bioInstr = _('Describe yourself and your interests');
478 $this->textarea('bio', _('Bio'),
479 $this->trimmed('bio'),
481 $this->elementEnd('li');
482 $this->elementStart('li');
483 $this->input('location', _('Location'),
484 $this->trimmed('location'),
485 _('Where you are, like "City, '.
486 'State (or Region), Country"'));
487 $this->elementEnd('li');
488 Event::handle('EndRegistrationFormData', array($this));
489 $this->elementStart('li', array('id' => 'settings_rememberme'));
490 $this->checkbox('rememberme', _('Remember me'),
491 $this->boolean('rememberme'),
492 _('Automatically login in the future; '.
493 'not for shared computers!'));
494 $this->elementEnd('li');
495 $attrs = array('type' => 'checkbox',
497 'class' => 'checkbox',
500 if ($this->boolean('license')) {
501 $attrs['checked'] = 'checked';
503 $this->elementStart('li');
504 $this->element('input', $attrs);
505 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
506 $this->raw($this->licenseCheckbox());
507 $this->elementEnd('label');
508 $this->elementEnd('li');
510 $this->elementEnd('ul');
511 $this->submit('submit', _('Register'));
512 $this->elementEnd('fieldset');
513 $this->elementEnd('form');
516 function licenseCheckbox()
519 switch (common_config('license', 'type')) {
521 // TRANS: Copyright checkbox label in registration dialog, for private sites.
522 $out .= htmlspecialchars(sprintf(
523 _('I understand that content and data of %1$s are private and confidential.'),
524 common_config('site', 'name')));
526 case 'allrightsreserved':
530 if (common_config('license', 'owner')) {
531 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
532 $out .= htmlspecialchars(sprintf(
533 _('My text and files are copyright by %1$s.'),
534 common_config('license', 'owner')));
536 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
537 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
539 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
540 $out .= ' ' . _('All rights reserved.');
542 case 'cc': // fall through
544 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
545 $message = _('My text and files are available under %s ' .
546 'except this private data: password, ' .
547 'email address, IM address, and phone number.');
548 $link = '<a href="' .
549 htmlspecialchars(common_config('license', 'url')) .
551 htmlspecialchars(common_config('license', 'title')) .
553 $out .= sprintf(htmlspecialchars($message), $link);
559 * Show some information about registering for the site
561 * Save the registration flag, run showPage
566 function showSuccess()
568 $this->registered = true;
573 * Show some information about registering for the site
575 * Gives some information and options for new registrees.
580 function showSuccessContent()
582 $nickname = $this->arg('nickname');
584 $profileurl = common_local_url('showstream',
585 array('nickname' => $nickname));
587 $this->elementStart('div', 'success');
588 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
589 'From here, you may want to...'. "\n\n" .
590 '* Go to [your profile](%2$s) '.
591 'and post your first message.' . "\n" .
592 '* Add a [Jabber/GTalk address]'.
593 '(%%%%action.imsettings%%%%) '.
594 'so you can send notices '.
595 'through instant messages.' . "\n" .
596 '* [Search for people](%%%%action.peoplesearch%%%%) '.
597 'that you may know or '.
598 'that share your interests. ' . "\n" .
599 '* Update your [profile settings]'.
600 '(%%%%action.profilesettings%%%%)'.
601 ' to tell others more about you. ' . "\n" .
602 '* Read over the [online docs](%%%%doc.help%%%%)'.
603 ' for features you may have missed. ' . "\n\n" .
604 'Thanks for signing up and we hope '.
605 'you enjoy using this service.'),
606 $nickname, $profileurl);
608 $this->raw(common_markup_to_html($instr));
610 $have_email = $this->trimmed('email');
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');
622 * Show the login group nav menu
627 function showLocalNav()
629 $nav = new LoginGroupNav($this);