3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2008-2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
35 * An action for registering a new user account
39 * @author Evan Prodromou <evan@controlyourself.ca>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://laconi.ca/
44 class RegisterAction extends Action
47 * Has there been an error?
56 var $registered = false;
63 * @return string title
66 function prepare($args)
68 parent::prepare($args);
69 $this->code = $this->trimmed('code');
71 if (empty($this->code)) {
72 common_ensure_session();
73 if (array_key_exists('invitecode', $_SESSION)) {
74 $this->code = $_SESSION['invitecode'];
78 if (common_config('site', 'inviteonly') && empty($this->code)) {
79 $this->clientError(_('Sorry, only invited people can register.'));
83 if (!empty($this->code)) {
84 $this->invite = Invitation::staticGet('code', $this->code);
85 if (empty($this->invite)) {
86 $this->clientError(_('Sorry, invalid invitation code.'));
89 // Store this in case we need it
90 common_ensure_session();
91 $_SESSION['invitecode'] = $this->code;
100 * @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
124 function handle($args)
126 parent::handle($args);
128 if (common_config('site', 'closed')) {
129 $this->clientError(_('Registration not allowed.'));
130 } else if (common_logged_in()) {
131 $this->clientError(_('Already logged in.'));
132 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
133 $this->tryRegister();
140 * Try to register a user
142 * Validates the input and tries to save a new user and profile
143 * record. On success, shows an instructions page.
148 function tryRegister()
150 if (Event::handle('StartRegistrationTry', array($this))) {
151 $token = $this->trimmed('token');
152 if (!$token || $token != common_session_token()) {
153 $this->showForm(_('There was a problem with your session token. '.
154 'Try again, please.'));
158 $nickname = $this->trimmed('nickname');
159 $email = $this->trimmed('email');
160 $fullname = $this->trimmed('fullname');
161 $homepage = $this->trimmed('homepage');
162 $bio = $this->trimmed('bio');
163 $location = $this->trimmed('location');
165 // We don't trim these... whitespace is OK in a password!
166 $password = $this->arg('password');
167 $confirm = $this->arg('confirm');
169 // invitation code, if any
170 $code = $this->trimmed('code');
173 $invite = Invitation::staticGet($code);
176 if (common_config('site', 'inviteonly') && !($code && $invite)) {
177 $this->clientError(_('Sorry, only invited people can register.'));
182 $nickname = common_canonical_nickname($nickname);
183 $email = common_canonical_email($email);
185 if (!$this->boolean('license')) {
186 $this->showForm(_('You can\'t register if you don\'t '.
187 'agree to the license.'));
188 } else if ($email && !Validate::email($email, true)) {
189 $this->showForm(_('Not a valid email address.'));
190 } else if (!Validate::string($nickname, array('min_length' => 1,
192 'format' => NICKNAME_FMT))) {
193 $this->showForm(_('Nickname must have only lowercase letters '.
194 'and numbers and no spaces.'));
195 } else if ($this->nicknameExists($nickname)) {
196 $this->showForm(_('Nickname already in use. Try another one.'));
197 } else if (!User::allowed_nickname($nickname)) {
198 $this->showForm(_('Not a valid nickname.'));
199 } else if ($this->emailExists($email)) {
200 $this->showForm(_('Email address already exists.'));
201 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
202 !Validate::uri($homepage,
203 array('allowed_schemes' =>
204 array('http', 'https')))) {
205 $this->showForm(_('Homepage is not a valid URL.'));
207 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
208 $this->showForm(_('Full name is too long (max 255 chars).'));
210 } else if (Profile::bioTooLong($bio)) {
211 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
214 } else if (!is_null($location) && mb_strlen($location) > 255) {
215 $this->showForm(_('Location is too long (max 255 chars).'));
217 } else if (strlen($password) < 6) {
218 $this->showForm(_('Password must be 6 or more characters.'));
220 } else if ($password != $confirm) {
221 $this->showForm(_('Passwords don\'t match.'));
222 } else if ($user = User::register(array('nickname' => $nickname,
223 'password' => $password,
225 'fullname' => $fullname,
226 'homepage' => $homepage,
228 'location' => $location,
231 $this->showForm(_('Invalid username or password.'));
235 if (!common_set_user($user)) {
236 $this->serverError(_('Error setting user.'));
239 // this is a real login
240 common_real_login(true);
241 if ($this->boolean('rememberme')) {
242 common_debug('Adding rememberme cookie for ' . $nickname);
243 common_rememberme($user);
246 Event::handle('EndRegistrationTry', array($this));
248 // Re-init language env in case it changed (not yet, but soon)
249 common_init_language();
250 $this->showSuccess();
252 $this->showForm(_('Invalid username or password.'));
258 * Does the given nickname already exist?
260 * Checks a canonical nickname against the database.
262 * @param string $nickname nickname to check
264 * @return boolean true if the nickname already exists
267 function nicknameExists($nickname)
269 $user = User::staticGet('nickname', $nickname);
270 return ($user !== false);
274 * Does the given email address already exist?
276 * Checks a canonical email address against the database.
278 * @param string $email email address to check
280 * @return boolean true if the address already exists
283 function emailExists($email)
285 $email = common_canonical_email($email);
286 if (!$email || strlen($email) == 0) {
289 $user = User::staticGet('email', $email);
290 return ($user !== false);
293 // overrrided to add entry-title class
294 function showPageTitle() {
295 if (Event::handle('StartShowPageTitle', array($this))) {
296 $this->element('h1', array('class' => 'entry-title'), $this->title());
300 // overrided to add hentry, and content-inner class
301 function showContentBlock()
303 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
304 $this->showPageTitle();
305 $this->showPageNoticeBlock();
306 $this->elementStart('div', array('id' => 'content_inner',
307 'class' => 'entry-content'));
308 // show the actual content (forms, lists, whatever)
309 $this->showContent();
310 $this->elementEnd('div');
311 $this->elementEnd('div');
315 * Instructions or a notice for the page
317 * Shows the error, if any, or instructions for registration.
322 function showPageNotice()
324 if ($this->registered) {
326 } else if ($this->error) {
327 $this->element('p', 'error', $this->error);
330 common_markup_to_html(_('With this form you can create '.
332 'You can then post notices and '.
333 'link up to friends and colleagues. '.
334 '(Have an [OpenID](http://openid.net/)? ' .
335 'Try our [OpenID registration]'.
336 '(%%action.openidlogin%%)!)'));
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
354 function showForm($error=null)
356 $this->error = $error;
361 * Show the page content
363 * Either shows the registration form or, if registration was successful,
364 * instructions for using the site.
369 function showContent()
371 if ($this->registered) {
372 $this->showSuccessContent();
374 $this->showFormContent();
379 * Show the registration form
384 function showFormContent()
386 $code = $this->trimmed('code');
391 $invite = Invitation::staticGet($code);
394 if (common_config('site', 'inviteonly') && !($code && $invite)) {
395 $this->clientError(_('Sorry, only invited people can register.'));
399 $this->elementStart('form', array('method' => 'post',
400 'id' => 'form_register',
401 'class' => 'form_settings',
402 'action' => common_local_url('register')));
403 $this->elementStart('fieldset');
404 $this->element('legend', null, 'Account settings');
405 $this->hidden('token', common_session_token());
408 $this->hidden('code', $this->code);
411 $this->elementStart('ul', 'form_data');
412 if (Event::handle('StartRegistrationFormData', array($this))) {
413 $this->elementStart('li');
414 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
415 _('1-64 lowercase letters or numbers, '.
416 'no punctuation or spaces. Required.'));
417 $this->elementEnd('li');
418 $this->elementStart('li');
419 $this->password('password', _('Password'),
420 _('6 or more characters. Required.'));
421 $this->elementEnd('li');
422 $this->elementStart('li');
423 $this->password('confirm', _('Confirm'),
424 _('Same as password above. Required.'));
425 $this->elementEnd('li');
426 $this->elementStart('li');
427 if ($this->invite && $this->invite->address_type == 'email') {
428 $this->input('email', _('Email'), $this->invite->address,
429 _('Used only for updates, announcements, '.
430 'and password recovery'));
432 $this->input('email', _('Email'), $this->trimmed('email'),
433 _('Used only for updates, announcements, '.
434 'and password recovery'));
436 $this->elementEnd('li');
437 $this->elementStart('li');
438 $this->input('fullname', _('Full name'),
439 $this->trimmed('fullname'),
440 _('Longer name, preferably your "real" name'));
441 $this->elementEnd('li');
442 $this->elementStart('li');
443 $this->input('homepage', _('Homepage'),
444 $this->trimmed('homepage'),
445 _('URL of your homepage, blog, '.
446 'or profile on another site'));
447 $this->elementEnd('li');
448 $this->elementStart('li');
449 $maxBio = Profile::maxBio();
451 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
454 $bioInstr = _('Describe yourself and your interests');
456 $this->textarea('bio', _('Bio'),
457 $this->trimmed('bio'),
459 $this->elementEnd('li');
460 $this->elementStart('li');
461 $this->input('location', _('Location'),
462 $this->trimmed('location'),
463 _('Where you are, like "City, '.
464 'State (or Region), Country"'));
465 $this->elementEnd('li');
466 Event::handle('EndRegistrationFormData', array($this));
467 $this->elementStart('li', array('id' => 'settings_rememberme'));
468 $this->checkbox('rememberme', _('Remember me'),
469 $this->boolean('rememberme'),
470 _('Automatically login in the future; '.
471 'not for shared computers!'));
472 $this->elementEnd('li');
473 $attrs = array('type' => 'checkbox',
475 'class' => 'checkbox',
478 if ($this->boolean('license')) {
479 $attrs['checked'] = 'checked';
481 $this->elementStart('li');
482 $this->element('input', $attrs);
483 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
484 $this->text(_('My text and files are available under '));
485 $this->element('a', array('href' => common_config('license', 'url')),
486 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
487 $this->text(_(' except this private data: password, '.
488 'email address, IM address, and phone number.'));
489 $this->elementEnd('label');
490 $this->elementEnd('li');
492 $this->elementEnd('ul');
493 $this->submit('submit', _('Register'));
494 $this->elementEnd('fieldset');
495 $this->elementEnd('form');
499 * Show some information about registering for the site
501 * Save the registration flag, run showPage
506 function showSuccess()
508 $this->registered = true;
513 * Show some information about registering for the site
515 * Gives some information and options for new registrees.
520 function showSuccessContent()
522 $nickname = $this->arg('nickname');
524 $profileurl = common_local_url('showstream',
525 array('nickname' => $nickname));
527 $this->elementStart('div', 'success');
528 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
529 'From here, you may want to...'. "\n\n" .
530 '* Go to [your profile](%s) '.
531 'and post your first message.' . "\n" .
532 '* Add a [Jabber/GTalk address]'.
533 '(%%%%action.imsettings%%%%) '.
534 'so you can send notices '.
535 'through instant messages.' . "\n" .
536 '* [Search for people](%%%%action.peoplesearch%%%%) '.
537 'that you may know or '.
538 'that share your interests. ' . "\n" .
539 '* Update your [profile settings]'.
540 '(%%%%action.profilesettings%%%%)'.
541 ' to tell others more about you. ' . "\n" .
542 '* Read over the [online docs](%%%%doc.help%%%%)'.
543 ' for features you may have missed. ' . "\n\n" .
544 'Thanks for signing up and we hope '.
545 'you enjoy using this service.'),
546 $nickname, $profileurl);
548 $this->raw(common_markup_to_html($instr));
550 $have_email = $this->trimmed('email');
552 $emailinstr = _('(You should receive a message by email '.
553 'momentarily, with ' .
554 'instructions on how to confirm '.
555 'your email address.)');
556 $this->raw(common_markup_to_html($emailinstr));
558 $this->elementEnd('div');
562 * Show the login group nav menu
567 function showLocalNav()
569 $nav = new LoginGroupNav($this);