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.'));
201 $nickname = common_canonical_nickname($nickname);
202 $email = common_canonical_email($email);
204 if (!$this->boolean('license')) {
205 $this->showForm(_('You can\'t register if you don\'t '.
206 'agree to the license.'));
207 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
208 $this->showForm(_('Not a valid email address.'));
209 } else if (!Validate::string($nickname, array('min_length' => 1,
211 'format' => NICKNAME_FMT))) {
212 $this->showForm(_('Nickname must have only lowercase letters '.
213 'and numbers and no spaces.'));
214 } else if ($this->nicknameExists($nickname)) {
215 $this->showForm(_('Nickname already in use. Try another one.'));
216 } else if (!User::allowed_nickname($nickname)) {
217 $this->showForm(_('Not a valid nickname.'));
218 } else if ($this->emailExists($email)) {
219 $this->showForm(_('Email address already exists.'));
220 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
221 !Validate::uri($homepage,
222 array('allowed_schemes' =>
223 array('http', 'https')))) {
224 $this->showForm(_('Homepage is not a valid URL.'));
226 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
227 $this->showForm(_('Full name is too long (max 255 chars).'));
229 } else if (Profile::bioTooLong($bio)) {
230 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
233 } else if (!is_null($location) && mb_strlen($location) > 255) {
234 $this->showForm(_('Location is too long (max 255 chars).'));
236 } else if (strlen($password) < 6) {
237 $this->showForm(_('Password must be 6 or more characters.'));
239 } else if ($password != $confirm) {
240 $this->showForm(_('Passwords don\'t match.'));
241 } else if ($user = User::register(array('nickname' => $nickname,
242 'password' => $password,
244 'fullname' => $fullname,
245 'homepage' => $homepage,
247 'location' => $location,
250 $this->showForm(_('Invalid username or password.'));
254 if (!common_set_user($user)) {
255 $this->serverError(_('Error setting user.'));
258 // this is a real login
259 common_real_login(true);
260 if ($this->boolean('rememberme')) {
261 common_debug('Adding rememberme cookie for ' . $nickname);
262 common_rememberme($user);
265 Event::handle('EndRegistrationTry', array($this));
267 // Re-init language env in case it changed (not yet, but soon)
268 common_init_language();
270 $this->showSuccess();
272 $this->showForm(_('Invalid username or password.'));
278 * Does the given nickname already exist?
280 * Checks a canonical nickname against the database.
282 * @param string $nickname nickname to check
284 * @return boolean true if the nickname already exists
287 function nicknameExists($nickname)
289 $user = User::staticGet('nickname', $nickname);
290 return is_object($user);
294 * Does the given email address already exist?
296 * Checks a canonical email address against the database.
298 * @param string $email email address to check
300 * @return boolean true if the address already exists
303 function emailExists($email)
305 $email = common_canonical_email($email);
306 if (!$email || strlen($email) == 0) {
309 $user = User::staticGet('email', $email);
310 return is_object($user);
313 // overrrided to add entry-title class
314 function showPageTitle() {
315 if (Event::handle('StartShowPageTitle', array($this))) {
316 $this->element('h1', array('class' => 'entry-title'), $this->title());
320 // overrided to add hentry, and content-inner class
321 function showContentBlock()
323 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
324 $this->showPageTitle();
325 $this->showPageNoticeBlock();
326 $this->elementStart('div', array('id' => 'content_inner',
327 'class' => 'entry-content'));
328 // show the actual content (forms, lists, whatever)
329 $this->showContent();
330 $this->elementEnd('div');
331 $this->elementEnd('div');
335 * Instructions or a notice for the page
337 * Shows the error, if any, or instructions for registration.
342 function showPageNotice()
344 if ($this->registered) {
346 } else if ($this->error) {
347 $this->element('p', 'error', $this->error);
350 common_markup_to_html(_('With this form you can create '.
352 'You can then post notices and '.
353 'link up to friends and colleagues. '));
355 $this->elementStart('div', 'instructions');
357 $this->elementEnd('div');
362 * Wrapper for showing a page
364 * Stores an error and shows the page
366 * @param string $error Error, if any
371 function showForm($error=null)
373 $this->error = $error;
378 * Show the page content
380 * Either shows the registration form or, if registration was successful,
381 * instructions for using the site.
386 function showContent()
388 if ($this->registered) {
389 $this->showSuccessContent();
391 $this->showFormContent();
396 * Show the registration form
401 function showFormContent()
403 $code = $this->trimmed('code');
408 $invite = Invitation::staticGet($code);
411 if (common_config('site', 'inviteonly') && !($code && $invite)) {
412 $this->clientError(_('Sorry, only invited people can register.'));
416 $this->elementStart('form', array('method' => 'post',
417 'id' => 'form_register',
418 'class' => 'form_settings',
419 'action' => common_local_url('register')));
420 $this->elementStart('fieldset');
421 $this->element('legend', null, 'Account settings');
422 $this->hidden('token', common_session_token());
425 $this->hidden('code', $this->code);
428 $this->elementStart('ul', 'form_data');
429 if (Event::handle('StartRegistrationFormData', array($this))) {
430 $this->elementStart('li');
431 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
432 _('1-64 lowercase letters or numbers, '.
433 'no punctuation or spaces. Required.'));
434 $this->elementEnd('li');
435 $this->elementStart('li');
436 $this->password('password', _('Password'),
437 _('6 or more characters. Required.'));
438 $this->elementEnd('li');
439 $this->elementStart('li');
440 $this->password('confirm', _('Confirm'),
441 _('Same as password above. Required.'));
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 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
471 $bioInstr = _('Describe yourself and your interests');
473 $this->textarea('bio', _('Bio'),
474 $this->trimmed('bio'),
476 $this->elementEnd('li');
477 $this->elementStart('li');
478 $this->input('location', _('Location'),
479 $this->trimmed('location'),
480 _('Where you are, like "City, '.
481 'State (or Region), Country"'));
482 $this->elementEnd('li');
483 Event::handle('EndRegistrationFormData', array($this));
484 $this->elementStart('li', array('id' => 'settings_rememberme'));
485 $this->checkbox('rememberme', _('Remember me'),
486 $this->boolean('rememberme'),
487 _('Automatically login in the future; '.
488 'not for shared computers!'));
489 $this->elementEnd('li');
490 $attrs = array('type' => 'checkbox',
492 'class' => 'checkbox',
495 if ($this->boolean('license')) {
496 $attrs['checked'] = 'checked';
498 $this->elementStart('li');
499 $this->element('input', $attrs);
500 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
501 $this->raw($this->licenseCheckbox());
502 $this->elementEnd('label');
503 $this->elementEnd('li');
505 $this->elementEnd('ul');
506 $this->submit('submit', _('Register'));
507 $this->elementEnd('fieldset');
508 $this->elementEnd('form');
511 function licenseCheckbox()
514 switch (common_config('license', 'type')) {
516 // TRANS: Copyright checkbox label in registration dialog, for private sites.
517 $out .= htmlspecialchars(sprintf(
518 _('I understand that content and data of %1$s are private and confidential.'),
519 common_config('site', 'name')));
521 case 'allrightsreserved':
525 if (common_config('license', 'owner')) {
526 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
527 $out .= htmlspecialchars(sprintf(
528 _('My text and files are copyright by %1$s.'),
529 common_config('license', 'owner')));
531 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
532 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
534 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
535 $out .= ' ' . _('All rights reserved.');
537 case 'cc': // fall through
539 // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
540 $message = _('My text and files are available under %s ' .
541 'except this private data: password, ' .
542 'email address, IM address, and phone number.');
543 $link = '<a href="' .
544 htmlspecialchars(common_config('license', 'url')) .
546 htmlspecialchars(common_config('license', 'title')) .
548 $out .= sprintf(htmlspecialchars($message), $link);
554 * Show some information about registering for the site
556 * Save the registration flag, run showPage
561 function showSuccess()
563 $this->registered = true;
568 * Show some information about registering for the site
570 * Gives some information and options for new registrees.
575 function showSuccessContent()
577 $nickname = $this->arg('nickname');
579 $profileurl = common_local_url('showstream',
580 array('nickname' => $nickname));
582 $this->elementStart('div', 'success');
583 $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
584 'From here, you may want to...'. "\n\n" .
585 '* Go to [your profile](%2$s) '.
586 'and post your first message.' . "\n" .
587 '* Add a [Jabber/GTalk address]'.
588 '(%%%%action.imsettings%%%%) '.
589 'so you can send notices '.
590 'through instant messages.' . "\n" .
591 '* [Search for people](%%%%action.peoplesearch%%%%) '.
592 'that you may know or '.
593 'that share your interests. ' . "\n" .
594 '* Update your [profile settings]'.
595 '(%%%%action.profilesettings%%%%)'.
596 ' to tell others more about you. ' . "\n" .
597 '* Read over the [online docs](%%%%doc.help%%%%)'.
598 ' for features you may have missed. ' . "\n\n" .
599 'Thanks for signing up and we hope '.
600 'you enjoy using this service.'),
601 $nickname, $profileurl);
603 $this->raw(common_markup_to_html($instr));
605 $have_email = $this->trimmed('email');
607 $emailinstr = _('(You should receive a message by email '.
608 'momentarily, with ' .
609 'instructions on how to confirm '.
610 'your email address.)');
611 $this->raw(common_markup_to_html($emailinstr));
613 $this->elementEnd('div');
617 * Show the login group nav menu
622 function showLocalNav()
624 $nav = new LoginGroupNav($this);