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/
43 class RegisterAction extends Action
46 * Has there been an error?
53 var $registered = false;
56 * Are we processing an invite?
65 * @return string title
67 function prepare($args)
69 parent::prepare($args);
70 $this->code = $this->trimmed('code');
72 // @todo this check should really be in index.php for all sensitive actions
73 $ssl = common_config('site', 'ssl');
74 if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
75 common_redirect(common_local_url('register'));
79 if (empty($this->code)) {
80 common_ensure_session();
81 if (array_key_exists('invitecode', $_SESSION)) {
82 $this->code = $_SESSION['invitecode'];
86 if (common_config('site', 'inviteonly') && empty($this->code)) {
87 $this->clientError(_('Sorry, only invited people can register.'));
91 if (!empty($this->code)) {
92 $this->invite = Invitation::staticGet('code', $this->code);
93 if (empty($this->invite)) {
94 $this->clientError(_('Sorry, invalid invitation code.'));
97 // Store this in case we need it
98 common_ensure_session();
99 $_SESSION['invitecode'] = $this->code;
108 * @return string title
112 if ($this->registered) {
113 return _('Registration successful');
115 return _('Register');
120 * Handle input, produce output
122 * Switches on request method; either shows the form or handles its input.
124 * Checks if registration is closed and shows an error if so.
126 * @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.
159 function tryRegister()
161 if (Event::handle('StartRegistrationTry', array($this))) {
162 $token = $this->trimmed('token');
163 if (!$token || $token != common_session_token()) {
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::staticGet($code);
187 if (common_config('site', 'inviteonly') && !($code && $invite)) {
188 $this->clientError(_('Sorry, only invited people can register.'));
194 $nickname = Nickname::normalize($nickname);
195 } catch (NicknameException $e) {
196 $this->showForm($e->getMessage());
198 $email = common_canonical_email($email);
200 if (!$this->boolean('license')) {
201 $this->showForm(_('You cannot register if you don\'t '.
202 'agree to the license.'));
203 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
204 $this->showForm(_('Not a valid email address.'));
205 } else if ($this->nicknameExists($nickname)) {
206 $this->showForm(_('Nickname already in use. Try another one.'));
207 } else if (!User::allowed_nickname($nickname)) {
208 $this->showForm(_('Not a valid nickname.'));
209 } else if ($this->emailExists($email)) {
210 $this->showForm(_('Email address already exists.'));
211 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
212 !Validate::uri($homepage,
213 array('allowed_schemes' =>
214 array('http', 'https')))) {
215 $this->showForm(_('Homepage is not a valid URL.'));
217 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
218 $this->showForm(_('Full name is too long (maximum 255 characters).'));
220 } else if (Profile::bioTooLong($bio)) {
221 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
222 'Bio is too long (maximum %d characters).',
226 } else if (!is_null($location) && mb_strlen($location) > 255) {
227 $this->showForm(_('Location is too long (maximum 255 characters).'));
229 } else if (strlen($password) < 6) {
230 $this->showForm(_('Password must be 6 or more characters.'));
232 } else if ($password != $confirm) {
233 $this->showForm(_('Passwords don\'t match.'));
234 } else if ($user = User::register(array('nickname' => $nickname,
235 'password' => $password,
237 'fullname' => $fullname,
238 'homepage' => $homepage,
240 'location' => $location,
243 $this->showForm(_('Invalid username or password.'));
247 if (!common_set_user($user)) {
248 $this->serverError(_('Error setting user.'));
251 // this is a real login
252 common_real_login(true);
253 if ($this->boolean('rememberme')) {
254 common_debug('Adding rememberme cookie for ' . $nickname);
255 common_rememberme($user);
258 Event::handle('EndRegistrationTry', array($this));
260 // Re-init language env in case it changed (not yet, but soon)
261 common_init_language();
263 $this->showSuccess();
265 $this->showForm(_('Invalid username or password.'));
271 * Does the given nickname already exist?
273 * Checks a canonical nickname against the database.
275 * @param string $nickname nickname to check
277 * @return boolean true if the nickname already exists
279 function nicknameExists($nickname)
281 $user = User::staticGet('nickname', $nickname);
282 return is_object($user);
286 * Does the given email address already exist?
288 * Checks a canonical email address against the database.
290 * @param string $email email address to check
292 * @return boolean true if the address already exists
294 function emailExists($email)
296 $email = common_canonical_email($email);
297 if (!$email || strlen($email) == 0) {
300 $user = User::staticGet('email', $email);
301 return is_object($user);
304 // overrrided to add entry-title class
305 function showPageTitle() {
306 if (Event::handle('StartShowPageTitle', array($this))) {
307 $this->element('h1', array('class' => 'entry-title'), $this->title());
311 // overrided to add hentry, and content-inner class
312 function showContentBlock()
314 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
315 $this->showPageTitle();
316 $this->showPageNoticeBlock();
317 $this->elementStart('div', array('id' => 'content_inner',
318 'class' => 'entry-content'));
319 // show the actual content (forms, lists, whatever)
320 $this->showContent();
321 $this->elementEnd('div');
322 $this->elementEnd('div');
326 * Instructions or a notice for the page
328 * Shows the error, if any, or instructions for registration.
332 function showPageNotice()
334 if ($this->registered) {
336 } else if ($this->error) {
337 $this->element('p', 'error', $this->error);
340 common_markup_to_html(_('With this form you can create '.
342 'You can then post notices and '.
343 'link up to friends and colleagues.'));
345 $this->elementStart('div', 'instructions');
347 $this->elementEnd('div');
352 * Wrapper for showing a page
354 * Stores an error and shows the page
356 * @param string $error Error, if any
360 function showForm($error=null)
362 $this->error = $error;
367 * Show the page content
369 * Either shows the registration form or, if registration was successful,
370 * instructions for using the site.
374 function showContent()
376 if ($this->registered) {
377 $this->showSuccessContent();
379 $this->showFormContent();
384 * Show the registration form
388 function showFormContent()
390 $code = $this->trimmed('code');
395 $invite = Invitation::staticGet($code);
398 if (common_config('site', 'inviteonly') && !($code && $invite)) {
399 $this->clientError(_('Sorry, only invited people can register.'));
403 $this->elementStart('form', array('method' => 'post',
404 'id' => 'form_register',
405 'class' => 'form_settings',
406 'action' => common_local_url('register')));
407 $this->elementStart('fieldset');
408 $this->element('legend', null, 'Account settings');
409 $this->hidden('token', common_session_token());
412 $this->hidden('code', $this->code);
415 $this->elementStart('ul', 'form_data');
416 if (Event::handle('StartRegistrationFormData', array($this))) {
417 $this->elementStart('li');
418 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
419 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
420 $this->elementEnd('li');
421 $this->elementStart('li');
422 $this->password('password', _('Password'),
423 _('6 or more characters.'));
424 $this->elementEnd('li');
425 $this->elementStart('li');
426 $this->password('confirm', _('Confirm'),
427 _('Same as password above.'));
428 $this->elementEnd('li');
429 $this->elementStart('li');
430 if ($this->invite && $this->invite->address_type == 'email') {
431 $this->input('email', _('Email'), $this->invite->address,
432 _('Used only for updates, announcements, '.
433 'and password recovery.'));
435 $this->input('email', _('Email'), $this->trimmed('email'),
436 _('Used only for updates, announcements, '.
437 'and password recovery.'));
439 $this->elementEnd('li');
440 $this->elementStart('li');
441 $this->input('fullname', _('Full name'),
442 $this->trimmed('fullname'),
443 _('Longer name, preferably your "real" name.'));
444 $this->elementEnd('li');
445 $this->elementStart('li');
446 $this->input('homepage', _('Homepage'),
447 $this->trimmed('homepage'),
448 _('URL of your homepage, blog, '.
449 'or profile on another site.'));
450 $this->elementEnd('li');
451 $this->elementStart('li');
452 $maxBio = Profile::maxBio();
454 // TRANS: Tooltip for field label in form for profile settings. Plural
455 // TRANS: is decided by the number of characters available for the
456 // TRANS: biography (%d).
457 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
458 'Describe yourself and your interests in %d characters',
462 $bioInstr = _('Describe yourself and your interests');
464 $this->textarea('bio', _('Bio'),
465 $this->trimmed('bio'),
467 $this->elementEnd('li');
468 $this->elementStart('li');
469 $this->input('location', _('Location'),
470 $this->trimmed('location'),
471 _('Where you are, like "City, '.
472 'State (or Region), Country".'));
473 $this->elementEnd('li');
474 Event::handle('EndRegistrationFormData', array($this));
475 $this->elementStart('li', array('id' => 'settings_rememberme'));
476 $this->checkbox('rememberme', _('Remember me'),
477 $this->boolean('rememberme'),
478 _('Automatically login in the future; '.
479 'not for shared computers!'));
480 $this->elementEnd('li');
481 $attrs = array('type' => 'checkbox',
483 'class' => 'checkbox',
486 if ($this->boolean('license')) {
487 $attrs['checked'] = 'checked';
489 $this->elementStart('li');
490 $this->element('input', $attrs);
491 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
492 $this->raw($this->licenseCheckbox());
493 $this->elementEnd('label');
494 $this->elementEnd('li');
496 $this->elementEnd('ul');
497 $this->submit('submit', _('Register'));
498 $this->elementEnd('fieldset');
499 $this->elementEnd('form');
502 function licenseCheckbox()
505 switch (common_config('license', 'type')) {
507 // TRANS: Copyright checkbox label in registration dialog, for private sites.
508 // TRANS: %1$s is the StatusNet sitename.
509 $out .= htmlspecialchars(sprintf(
510 _('I understand that content and data of %1$s are private and confidential.'),
511 common_config('site', 'name')));
513 case 'allrightsreserved':
517 if (common_config('license', 'owner')) {
518 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
519 $out .= htmlspecialchars(sprintf(
520 _('My text and files are copyright by %1$s.'),
521 common_config('license', 'owner')));
523 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
524 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
526 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
527 $out .= ' ' . _('All rights reserved.');
529 case 'cc': // fall through
531 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
532 $message = _('My text and files are available under %s ' .
533 'except this private data: password, ' .
534 'email address, IM address, and phone number.');
535 $link = '<a href="' .
536 htmlspecialchars(common_config('license', 'url')) .
538 htmlspecialchars(common_config('license', 'title')) .
540 $out .= sprintf(htmlspecialchars($message), $link);
546 * Show some information about registering for the site
548 * Save the registration flag, run showPage
552 function showSuccess()
554 $this->registered = true;
559 * Show some information about registering for the site
561 * Gives some information and options for new registrees.
565 function showSuccessContent()
567 $nickname = $this->arg('nickname');
569 $profileurl = common_local_url('showstream',
570 array('nickname' => $nickname));
572 $this->elementStart('div', 'success');
573 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
574 'From here, you may want to...'. "\n\n" .
575 '* Go to [your profile](%2$s) '.
576 'and post your first message.' . "\n" .
577 '* Add a [Jabber/GTalk address]'.
578 '(%%%%action.imsettings%%%%) '.
579 'so you can send notices '.
580 'through instant messages.' . "\n" .
581 '* [Search for people](%%%%action.peoplesearch%%%%) '.
582 'that you may know or '.
583 'that share your interests. ' . "\n" .
584 '* Update your [profile settings]'.
585 '(%%%%action.profilesettings%%%%)'.
586 ' to tell others more about you. ' . "\n" .
587 '* Read over the [online docs](%%%%doc.help%%%%)'.
588 ' for features you may have missed. ' . "\n\n" .
589 'Thanks for signing up and we hope '.
590 'you enjoy using this service.'),
591 $nickname, $profileurl);
593 $this->raw(common_markup_to_html($instr));
595 $have_email = $this->trimmed('email');
597 $emailinstr = _('(You should receive a message by email '.
598 'momentarily, with ' .
599 'instructions on how to confirm '.
600 'your email address.)');
601 $this->raw(common_markup_to_html($emailinstr));
603 $this->elementEnd('div');
607 * Show the login group nav menu
611 function showLocalNav()
613 $nav = new LoginGroupNav($this);