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 if (empty($this->code)) {
73 common_ensure_session();
74 if (array_key_exists('invitecode', $_SESSION)) {
75 $this->code = $_SESSION['invitecode'];
79 if (common_config('site', 'inviteonly') && empty($this->code)) {
80 $this->clientError(_('Sorry, only invited people can register.'));
84 if (!empty($this->code)) {
85 $this->invite = Invitation::staticGet('code', $this->code);
86 if (empty($this->invite)) {
87 $this->clientError(_('Sorry, invalid invitation code.'));
90 // Store this in case we need it
91 common_ensure_session();
92 $_SESSION['invitecode'] = $this->code;
101 * @return string title
105 if ($this->registered) {
106 return _('Registration successful');
108 return _('Register');
113 * Handle input, produce output
115 * Switches on request method; either shows the form or handles its input.
117 * Checks if registration is closed and shows an error if so.
119 * @param array $args $_REQUEST data
123 function handle($args)
125 parent::handle($args);
127 if (common_config('site', 'closed')) {
128 $this->clientError(_('Registration not allowed.'));
129 } else if (common_logged_in()) {
130 $this->clientError(_('Already logged in.'));
131 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
132 $this->tryRegister();
138 function showScripts()
140 parent::showScripts();
141 $this->autofocus('nickname');
145 * Try to register a user
147 * Validates the input and tries to save a new user and profile
148 * record. On success, shows an instructions page.
152 function tryRegister()
154 if (Event::handle('StartRegistrationTry', array($this))) {
155 $token = $this->trimmed('token');
156 if (!$token || $token != common_session_token()) {
157 $this->showForm(_('There was a problem with your session token. '.
158 'Try again, please.'));
162 $nickname = $this->trimmed('nickname');
163 $email = $this->trimmed('email');
164 $fullname = $this->trimmed('fullname');
165 $homepage = $this->trimmed('homepage');
166 $bio = $this->trimmed('bio');
167 $location = $this->trimmed('location');
169 // We don't trim these... whitespace is OK in a password!
170 $password = $this->arg('password');
171 $confirm = $this->arg('confirm');
173 // invitation code, if any
174 $code = $this->trimmed('code');
177 $invite = Invitation::staticGet($code);
180 if (common_config('site', 'inviteonly') && !($code && $invite)) {
181 $this->clientError(_('Sorry, only invited people can register.'));
187 $nickname = Nickname::normalize($nickname);
188 } catch (NicknameException $e) {
189 $this->showForm($e->getMessage());
191 $email = common_canonical_email($email);
193 if (!$this->boolean('license')) {
194 $this->showForm(_('You cannot register if you don\'t '.
195 'agree to the license.'));
196 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
197 $this->showForm(_('Not a valid email address.'));
198 } else if ($this->nicknameExists($nickname)) {
199 $this->showForm(_('Nickname already in use. Try another one.'));
200 } else if (!User::allowed_nickname($nickname)) {
201 $this->showForm(_('Not a valid nickname.'));
202 } else if ($this->emailExists($email)) {
203 $this->showForm(_('Email address already exists.'));
204 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
205 !Validate::uri($homepage,
206 array('allowed_schemes' =>
207 array('http', 'https')))) {
208 $this->showForm(_('Homepage is not a valid URL.'));
210 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
211 $this->showForm(_('Full name is too long (maximum 255 characters).'));
213 } else if (Profile::bioTooLong($bio)) {
214 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
215 'Bio is too long (maximum %d characters).',
219 } else if (!is_null($location) && mb_strlen($location) > 255) {
220 $this->showForm(_('Location is too long (maximum 255 characters).'));
222 } else if (strlen($password) < 6) {
223 $this->showForm(_('Password must be 6 or more characters.'));
225 } else if ($password != $confirm) {
226 $this->showForm(_('Passwords don\'t match.'));
227 } else if ($user = User::register(array('nickname' => $nickname,
228 'password' => $password,
230 'fullname' => $fullname,
231 'homepage' => $homepage,
233 'location' => $location,
236 $this->showForm(_('Invalid username or password.'));
240 if (!common_set_user($user)) {
241 $this->serverError(_('Error setting user.'));
244 // this is a real login
245 common_real_login(true);
246 if ($this->boolean('rememberme')) {
247 common_debug('Adding rememberme cookie for ' . $nickname);
248 common_rememberme($user);
251 Event::handle('EndRegistrationTry', array($this));
253 // Re-init language env in case it changed (not yet, but soon)
254 common_init_language();
256 $this->showSuccess();
258 $this->showForm(_('Invalid username or password.'));
264 * Does the given nickname already exist?
266 * Checks a canonical nickname against the database.
268 * @param string $nickname nickname to check
270 * @return boolean true if the nickname already exists
272 function nicknameExists($nickname)
274 $user = User::staticGet('nickname', $nickname);
275 return is_object($user);
279 * Does the given email address already exist?
281 * Checks a canonical email address against the database.
283 * @param string $email email address to check
285 * @return boolean true if the address already exists
287 function emailExists($email)
289 $email = common_canonical_email($email);
290 if (!$email || strlen($email) == 0) {
293 $user = User::staticGet('email', $email);
294 return is_object($user);
297 // overrrided to add entry-title class
298 function showPageTitle() {
299 if (Event::handle('StartShowPageTitle', array($this))) {
300 $this->element('h1', array('class' => 'entry-title'), $this->title());
304 // overrided to add hentry, and content-inner class
305 function showContentBlock()
307 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
308 $this->showPageTitle();
309 $this->showPageNoticeBlock();
310 $this->elementStart('div', array('id' => 'content_inner',
311 'class' => 'entry-content'));
312 // show the actual content (forms, lists, whatever)
313 $this->showContent();
314 $this->elementEnd('div');
315 $this->elementEnd('div');
319 * Instructions or a notice for the page
321 * Shows the error, if any, or instructions for registration.
325 function showPageNotice()
327 if ($this->registered) {
329 } else if ($this->error) {
330 $this->element('p', 'error', $this->error);
333 common_markup_to_html(_('With this form you can create '.
335 'You can then post notices and '.
336 'link up to friends and colleagues.'));
338 $this->elementStart('div', 'instructions');
340 $this->elementEnd('div');
345 * Wrapper for showing a page
347 * Stores an error and shows the page
349 * @param string $error Error, if any
353 function showForm($error=null)
355 $this->error = $error;
360 * Show the page content
362 * Either shows the registration form or, if registration was successful,
363 * instructions for using the site.
367 function showContent()
369 if ($this->registered) {
370 $this->showSuccessContent();
372 $this->showFormContent();
377 * Show the registration form
381 function showFormContent()
383 $code = $this->trimmed('code');
388 $invite = Invitation::staticGet($code);
391 if (common_config('site', 'inviteonly') && !($code && $invite)) {
392 $this->clientError(_('Sorry, only invited people can register.'));
396 $this->elementStart('form', array('method' => 'post',
397 'id' => 'form_register',
398 'class' => 'form_settings',
399 'action' => common_local_url('register')));
400 $this->elementStart('fieldset');
401 $this->element('legend', null, 'Account settings');
402 $this->hidden('token', common_session_token());
405 $this->hidden('code', $this->code);
408 $this->elementStart('ul', 'form_data');
409 if (Event::handle('StartRegistrationFormData', array($this))) {
410 $this->elementStart('li');
411 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
412 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
413 $this->elementEnd('li');
414 $this->elementStart('li');
415 $this->password('password', _('Password'),
416 _('6 or more characters.'));
417 $this->elementEnd('li');
418 $this->elementStart('li');
419 $this->password('confirm', _('Confirm'),
420 _('Same as password above.'));
421 $this->elementEnd('li');
422 $this->elementStart('li');
423 if ($this->invite && $this->invite->address_type == 'email') {
424 $this->input('email', _('Email'), $this->invite->address,
425 _('Used only for updates, announcements, '.
426 'and password recovery.'));
428 $this->input('email', _('Email'), $this->trimmed('email'),
429 _('Used only for updates, announcements, '.
430 'and password recovery.'));
432 $this->elementEnd('li');
433 $this->elementStart('li');
434 $this->input('fullname', _('Full name'),
435 $this->trimmed('fullname'),
436 _('Longer name, preferably your "real" name.'));
437 $this->elementEnd('li');
438 $this->elementStart('li');
439 $this->input('homepage', _('Homepage'),
440 $this->trimmed('homepage'),
441 _('URL of your homepage, blog, '.
442 'or profile on another site.'));
443 $this->elementEnd('li');
444 $this->elementStart('li');
445 $maxBio = Profile::maxBio();
447 // TRANS: Tooltip for field label in form for profile settings. Plural
448 // TRANS: is decided by the number of characters available for the
449 // TRANS: biography (%d).
450 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
451 'Describe yourself and your interests in %d characters',
455 $bioInstr = _('Describe yourself and your interests');
457 $this->textarea('bio', _('Bio'),
458 $this->trimmed('bio'),
460 $this->elementEnd('li');
461 $this->elementStart('li');
462 $this->input('location', _('Location'),
463 $this->trimmed('location'),
464 _('Where you are, like "City, '.
465 'State (or Region), Country".'));
466 $this->elementEnd('li');
467 Event::handle('EndRegistrationFormData', array($this));
468 $this->elementStart('li', array('id' => 'settings_rememberme'));
469 $this->checkbox('rememberme', _('Remember me'),
470 $this->boolean('rememberme'),
471 _('Automatically login in the future; '.
472 'not for shared computers!'));
473 $this->elementEnd('li');
474 $attrs = array('type' => 'checkbox',
476 'class' => 'checkbox',
479 if ($this->boolean('license')) {
480 $attrs['checked'] = 'checked';
482 $this->elementStart('li');
483 $this->element('input', $attrs);
484 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
485 $this->raw($this->licenseCheckbox());
486 $this->elementEnd('label');
487 $this->elementEnd('li');
489 $this->elementEnd('ul');
490 $this->submit('submit', _('Register'));
491 $this->elementEnd('fieldset');
492 $this->elementEnd('form');
495 function licenseCheckbox()
498 switch (common_config('license', 'type')) {
500 // TRANS: Copyright checkbox label in registration dialog, for private sites.
501 // TRANS: %1$s is the StatusNet sitename.
502 $out .= htmlspecialchars(sprintf(
503 _('I understand that content and data of %1$s are private and confidential.'),
504 common_config('site', 'name')));
506 case 'allrightsreserved':
510 if (common_config('license', 'owner')) {
511 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
512 $out .= htmlspecialchars(sprintf(
513 _('My text and files are copyright by %1$s.'),
514 common_config('license', 'owner')));
516 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
517 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
519 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
520 $out .= ' ' . _('All rights reserved.');
522 case 'cc': // fall through
524 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
525 $message = _('My text and files are available under %s ' .
526 'except this private data: password, ' .
527 'email address, IM address, and phone number.');
528 $link = '<a href="' .
529 htmlspecialchars(common_config('license', 'url')) .
531 htmlspecialchars(common_config('license', 'title')) .
533 $out .= sprintf(htmlspecialchars($message), $link);
539 * Show some information about registering for the site
541 * Save the registration flag, run showPage
545 function showSuccess()
547 $this->registered = true;
552 * Show some information about registering for the site
554 * Gives some information and options for new registrees.
558 function showSuccessContent()
560 $nickname = $this->arg('nickname');
562 $profileurl = common_local_url('showstream',
563 array('nickname' => $nickname));
565 $this->elementStart('div', 'success');
566 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
567 'From here, you may want to...'. "\n\n" .
568 '* Go to [your profile](%2$s) '.
569 'and post your first message.' . "\n" .
570 '* Add a [Jabber/GTalk address]'.
571 '(%%%%action.imsettings%%%%) '.
572 'so you can send notices '.
573 'through instant messages.' . "\n" .
574 '* [Search for people](%%%%action.peoplesearch%%%%) '.
575 'that you may know or '.
576 'that share your interests. ' . "\n" .
577 '* Update your [profile settings]'.
578 '(%%%%action.profilesettings%%%%)'.
579 ' to tell others more about you. ' . "\n" .
580 '* Read over the [online docs](%%%%doc.help%%%%)'.
581 ' for features you may have missed. ' . "\n\n" .
582 'Thanks for signing up and we hope '.
583 'you enjoy using this service.'),
584 $nickname, $profileurl);
586 $this->raw(common_markup_to_html($instr));
588 $have_email = $this->trimmed('email');
590 $emailinstr = _('(You should receive a message by email '.
591 'momentarily, with ' .
592 'instructions on how to confirm '.
593 'your email address.)');
594 $this->raw(common_markup_to_html($emailinstr));
596 $this->elementEnd('div');
600 * Show the login group nav menu
604 function showLocalNav()
606 $nav = new LoginGroupNav($this);