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 (!is_null($bio) && mb_strlen($bio) > 140) {
211 $this->showForm(_('Bio is too long (max 140 chars).'));
213 } else if (!is_null($location) && mb_strlen($location) > 255) {
214 $this->showForm(_('Location is too long (max 255 chars).'));
216 } else if (strlen($password) < 6) {
217 $this->showForm(_('Password must be 6 or more characters.'));
219 } else if ($password != $confirm) {
220 $this->showForm(_('Passwords don\'t match.'));
221 } else if ($user = User::register(array('nickname' => $nickname,
222 'password' => $password,
224 'fullname' => $fullname,
225 'homepage' => $homepage,
227 'location' => $location,
230 $this->showForm(_('Invalid username or password.'));
234 if (!common_set_user($user)) {
235 $this->serverError(_('Error setting user.'));
238 // this is a real login
239 common_real_login(true);
240 if ($this->boolean('rememberme')) {
241 common_debug('Adding rememberme cookie for ' . $nickname);
242 common_rememberme($user);
245 Event::handle('EndRegistrationTry', array($this));
247 // Re-init language env in case it changed (not yet, but soon)
248 common_init_language();
249 $this->showSuccess();
251 $this->showForm(_('Invalid username or password.'));
257 * Does the given nickname already exist?
259 * Checks a canonical nickname against the database.
261 * @param string $nickname nickname to check
263 * @return boolean true if the nickname already exists
266 function nicknameExists($nickname)
268 $user = User::staticGet('nickname', $nickname);
269 return ($user !== false);
273 * Does the given email address already exist?
275 * Checks a canonical email address against the database.
277 * @param string $email email address to check
279 * @return boolean true if the address already exists
282 function emailExists($email)
284 $email = common_canonical_email($email);
285 if (!$email || strlen($email) == 0) {
288 $user = User::staticGet('email', $email);
289 return ($user !== false);
292 // overrrided to add entry-title class
293 function showPageTitle() {
294 if (Event::handle('StartShowPageTitle', array($this))) {
295 $this->element('h1', array('class' => 'entry-title'), $this->title());
299 // overrided to add hentry, and content-inner class
300 function showContentBlock()
302 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
303 $this->showPageTitle();
304 $this->showPageNoticeBlock();
305 $this->elementStart('div', array('id' => 'content_inner',
306 'class' => 'entry-content'));
307 // show the actual content (forms, lists, whatever)
308 $this->showContent();
309 $this->elementEnd('div');
310 $this->elementEnd('div');
314 * Instructions or a notice for the page
316 * Shows the error, if any, or instructions for registration.
321 function showPageNotice()
323 if ($this->registered) {
325 } else if ($this->error) {
326 $this->element('p', 'error', $this->error);
329 common_markup_to_html(_('With this form you can create '.
331 'You can then post notices and '.
332 'link up to friends and colleagues. '.
333 '(Have an [OpenID](http://openid.net/)? ' .
334 'Try our [OpenID registration]'.
335 '(%%action.openidlogin%%)!)'));
337 $this->elementStart('div', 'instructions');
339 $this->elementEnd('div');
344 * Wrapper for showing a page
346 * Stores an error and shows the page
348 * @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.
368 function showContent()
370 if ($this->registered) {
371 $this->showSuccessContent();
373 $this->showFormContent();
378 * Show the registration form
383 function showFormContent()
385 $code = $this->trimmed('code');
390 $invite = Invitation::staticGet($code);
393 if (common_config('site', 'inviteonly') && !($code && $invite)) {
394 $this->clientError(_('Sorry, only invited people can register.'));
398 $this->elementStart('form', array('method' => 'post',
399 'id' => 'form_register',
400 'class' => 'form_settings',
401 'action' => common_local_url('register')));
402 $this->elementStart('fieldset');
403 $this->element('legend', null, 'Account settings');
404 $this->hidden('token', common_session_token());
407 $this->hidden('code', $this->code);
410 $this->elementStart('ul', 'form_data');
411 if (Event::handle('StartRegistrationFormData', array($this))) {
412 $this->elementStart('li');
413 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
414 _('1-64 lowercase letters or numbers, '.
415 'no punctuation or spaces. Required.'));
416 $this->elementEnd('li');
417 $this->elementStart('li');
418 $this->password('password', _('Password'),
419 _('6 or more characters. Required.'));
420 $this->elementEnd('li');
421 $this->elementStart('li');
422 $this->password('confirm', _('Confirm'),
423 _('Same as password above. Required.'));
424 $this->elementEnd('li');
425 $this->elementStart('li');
426 if ($this->invite && $this->invite->address_type == 'email') {
427 $this->input('email', _('Email'), $this->invite->address,
428 _('Used only for updates, announcements, '.
429 'and password recovery'));
431 $this->input('email', _('Email'), $this->trimmed('email'),
432 _('Used only for updates, announcements, '.
433 'and password recovery'));
435 $this->elementEnd('li');
436 $this->elementStart('li');
437 $this->input('fullname', _('Full name'),
438 $this->trimmed('fullname'),
439 _('Longer name, preferably your "real" name'));
440 $this->elementEnd('li');
441 $this->elementStart('li');
442 $this->input('homepage', _('Homepage'),
443 $this->trimmed('homepage'),
444 _('URL of your homepage, blog, '.
445 'or profile on another site'));
446 $this->elementEnd('li');
447 $this->elementStart('li');
448 $this->textarea('bio', _('Bio'),
449 $this->trimmed('bio'),
450 _('Describe yourself and your '.
451 'interests in 140 chars'));
452 $this->elementEnd('li');
453 $this->elementStart('li');
454 $this->input('location', _('Location'),
455 $this->trimmed('location'),
456 _('Where you are, like "City, '.
457 'State (or Region), Country"'));
458 $this->elementEnd('li');
459 Event::handle('EndRegistrationFormData', array($this));
460 $this->elementStart('li', array('id' => 'settings_rememberme'));
461 $this->checkbox('rememberme', _('Remember me'),
462 $this->boolean('rememberme'),
463 _('Automatically login in the future; '.
464 'not for shared computers!'));
465 $this->elementEnd('li');
466 $attrs = array('type' => 'checkbox',
468 'class' => 'checkbox',
471 if ($this->boolean('license')) {
472 $attrs['checked'] = 'checked';
474 $this->elementStart('li');
475 $this->element('input', $attrs);
476 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
477 $this->text(_('My text and files are available under '));
478 $this->element('a', array('href' => common_config('license', 'url')),
479 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
480 $this->text(_(' except this private data: password, '.
481 'email address, IM address, and phone number.'));
482 $this->elementEnd('label');
483 $this->elementEnd('li');
485 $this->elementEnd('ul');
486 $this->submit('submit', _('Register'));
487 $this->elementEnd('fieldset');
488 $this->elementEnd('form');
492 * Show some information about registering for the site
494 * Save the registration flag, run showPage
499 function showSuccess()
501 $this->registered = true;
506 * Show some information about registering for the site
508 * Gives some information and options for new registrees.
513 function showSuccessContent()
515 $nickname = $this->arg('nickname');
517 $profileurl = common_local_url('showstream',
518 array('nickname' => $nickname));
520 $this->elementStart('div', 'success');
521 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
522 'From here, you may want to...'. "\n\n" .
523 '* Go to [your profile](%s) '.
524 'and post your first message.' . "\n" .
525 '* Add a [Jabber/GTalk address]'.
526 '(%%%%action.imsettings%%%%) '.
527 'so you can send notices '.
528 'through instant messages.' . "\n" .
529 '* [Search for people](%%%%action.peoplesearch%%%%) '.
530 'that you may know or '.
531 'that share your interests. ' . "\n" .
532 '* Update your [profile settings]'.
533 '(%%%%action.profilesettings%%%%)'.
534 ' to tell others more about you. ' . "\n" .
535 '* Read over the [online docs](%%%%doc.help%%%%)'.
536 ' for features you may have missed. ' . "\n\n" .
537 'Thanks for signing up and we hope '.
538 'you enjoy using this service.'),
539 $nickname, $profileurl);
541 $this->raw(common_markup_to_html($instr));
543 $have_email = $this->trimmed('email');
545 $emailinstr = _('(You should receive a message by email '.
546 'momentarily, with ' .
547 'instructions on how to confirm '.
548 'your email address.)');
549 $this->raw(common_markup_to_html($emailinstr));
551 $this->elementEnd('div');
555 * Show the login group nav menu
560 function showLocalNav()
562 $nav = new LoginGroupNav($this);