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, '.
434 'no punctuation or spaces. Required.'));
435 $this->elementEnd('li');
436 $this->elementStart('li');
437 $this->password('password', _('Password'),
438 _('6 or more characters. Required.'));
439 $this->elementEnd('li');
440 $this->elementStart('li');
441 $this->password('confirm', _('Confirm'),
442 _('Same as password above. Required.'));
443 $this->elementEnd('li');
444 $this->elementStart('li');
445 if ($this->invite && $this->invite->address_type == 'email') {
446 $this->input('email', _('Email'), $this->invite->address,
447 _('Used only for updates, announcements, '.
448 'and password recovery'));
450 $this->input('email', _('Email'), $this->trimmed('email'),
451 _('Used only for updates, announcements, '.
452 'and password recovery'));
454 $this->elementEnd('li');
455 $this->elementStart('li');
456 $this->input('fullname', _('Full name'),
457 $this->trimmed('fullname'),
458 _('Longer name, preferably your "real" name'));
459 $this->elementEnd('li');
460 $this->elementStart('li');
461 $this->input('homepage', _('Homepage'),
462 $this->trimmed('homepage'),
463 _('URL of your homepage, blog, '.
464 'or profile on another site'));
465 $this->elementEnd('li');
466 $this->elementStart('li');
467 $maxBio = Profile::maxBio();
469 // TRANS: Tooltip for field label in form for profile settings. Plural
470 // TRANS: is decided by the number of characters available for the
471 // TRANS: biography (%d).
472 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
473 'Describe yourself and your interests in %d characters',
477 $bioInstr = _('Describe yourself and your interests');
479 $this->textarea('bio', _('Bio'),
480 $this->trimmed('bio'),
482 $this->elementEnd('li');
483 $this->elementStart('li');
484 $this->input('location', _('Location'),
485 $this->trimmed('location'),
486 _('Where you are, like "City, '.
487 'State (or Region), Country"'));
488 $this->elementEnd('li');
489 Event::handle('EndRegistrationFormData', array($this));
490 $this->elementStart('li', array('id' => 'settings_rememberme'));
491 $this->checkbox('rememberme', _('Remember me'),
492 $this->boolean('rememberme'),
493 _('Automatically login in the future; '.
494 'not for shared computers!'));
495 $this->elementEnd('li');
496 $attrs = array('type' => 'checkbox',
498 'class' => 'checkbox',
501 if ($this->boolean('license')) {
502 $attrs['checked'] = 'checked';
504 $this->elementStart('li');
505 $this->element('input', $attrs);
506 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
507 $this->raw($this->licenseCheckbox());
508 $this->elementEnd('label');
509 $this->elementEnd('li');
511 $this->elementEnd('ul');
512 $this->submit('submit', _('Register'));
513 $this->elementEnd('fieldset');
514 $this->elementEnd('form');
517 function licenseCheckbox()
520 switch (common_config('license', 'type')) {
522 // TRANS: Copyright checkbox label in registration dialog, for private sites.
523 $out .= htmlspecialchars(sprintf(
524 _('I understand that content and data of %1$s are private and confidential.'),
525 common_config('site', 'name')));
527 case 'allrightsreserved':
531 if (common_config('license', 'owner')) {
532 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
533 $out .= htmlspecialchars(sprintf(
534 _('My text and files are copyright by %1$s.'),
535 common_config('license', 'owner')));
537 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
538 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
540 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
541 $out .= ' ' . _('All rights reserved.');
543 case 'cc': // fall through
545 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
546 $message = _('My text and files are available under %s ' .
547 'except this private data: password, ' .
548 'email address, IM address, and phone number.');
549 $link = '<a href="' .
550 htmlspecialchars(common_config('license', 'url')) .
552 htmlspecialchars(common_config('license', 'title')) .
554 $out .= sprintf(htmlspecialchars($message), $link);
560 * Show some information about registering for the site
562 * Save the registration flag, run showPage
567 function showSuccess()
569 $this->registered = true;
574 * Show some information about registering for the site
576 * Gives some information and options for new registrees.
581 function showSuccessContent()
583 $nickname = $this->arg('nickname');
585 $profileurl = common_local_url('showstream',
586 array('nickname' => $nickname));
588 $this->elementStart('div', 'success');
589 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
590 'From here, you may want to...'. "\n\n" .
591 '* Go to [your profile](%2$s) '.
592 'and post your first message.' . "\n" .
593 '* Add a [Jabber/GTalk address]'.
594 '(%%%%action.imsettings%%%%) '.
595 'so you can send notices '.
596 'through instant messages.' . "\n" .
597 '* [Search for people](%%%%action.peoplesearch%%%%) '.
598 'that you may know or '.
599 'that share your interests. ' . "\n" .
600 '* Update your [profile settings]'.
601 '(%%%%action.profilesettings%%%%)'.
602 ' to tell others more about you. ' . "\n" .
603 '* Read over the [online docs](%%%%doc.help%%%%)'.
604 ' for features you may have missed. ' . "\n\n" .
605 'Thanks for signing up and we hope '.
606 'you enjoy using this service.'),
607 $nickname, $profileurl);
609 $this->raw(common_markup_to_html($instr));
611 $have_email = $this->trimmed('email');
613 $emailinstr = _('(You should receive a message by email '.
614 'momentarily, with ' .
615 'instructions on how to confirm '.
616 'your email address.)');
617 $this->raw(common_markup_to_html($emailinstr));
619 $this->elementEnd('div');
623 * Show the login group nav menu
628 function showLocalNav()
630 $nav = new LoginGroupNav($this);