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
68 $this->code = $this->trimmed('code');
70 if (empty($this->code)) {
71 common_ensure_session();
72 if (!empty($_SESSION['invitecode'])) {
73 $this->code = $_SESSION['invitecode'];
77 if (common_config('site', 'inviteonly') && empty($this->code)) {
78 $this->clientError(_('Sorry, only invited people can register.'));
82 if (!empty($this->code)) {
83 $this->invite = Invitation::staticGet($code);
84 if (empty($this->invite)) {
85 $this->clientError(_('Sorry, invalid invitation code.'));
88 // Store this in case we need it
89 common_ensure_session();
90 $_SESSION['invitecode'] = $this->code;
99 * @return string title
104 if ($this->registered) {
105 return _('Registration successful');
107 return _('Register');
112 * Handle input, produce output
114 * Switches on request method; either shows the form or handles its input.
116 * Checks if registration is closed and shows an error if so.
118 * @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();
139 * Try to register a user
141 * Validates the input and tries to save a new user and profile
142 * record. On success, shows an instructions page.
147 function tryRegister()
149 if (Event::handle('StartRegistrationTry', array($this))) {
150 $token = $this->trimmed('token');
151 if (!$token || $token != common_session_token()) {
152 $this->showForm(_('There was a problem with your session token. '.
153 'Try again, please.'));
157 $nickname = $this->trimmed('nickname');
158 $email = $this->trimmed('email');
159 $fullname = $this->trimmed('fullname');
160 $homepage = $this->trimmed('homepage');
161 $bio = $this->trimmed('bio');
162 $location = $this->trimmed('location');
164 // We don't trim these... whitespace is OK in a password!
165 $password = $this->arg('password');
166 $confirm = $this->arg('confirm');
168 // invitation code, if any
169 $code = $this->trimmed('code');
172 $invite = Invitation::staticGet($code);
175 if (common_config('site', 'inviteonly') && !($code && $invite)) {
176 $this->clientError(_('Sorry, only invited people can register.'));
181 $nickname = common_canonical_nickname($nickname);
182 $email = common_canonical_email($email);
184 if (!$this->boolean('license')) {
185 $this->showForm(_('You can\'t register if you don\'t '.
186 'agree to the license.'));
187 } else if ($email && !Validate::email($email, true)) {
188 $this->showForm(_('Not a valid email address.'));
189 } else if (!Validate::string($nickname, array('min_length' => 1,
191 'format' => NICKNAME_FMT))) {
192 $this->showForm(_('Nickname must have only lowercase letters '.
193 'and numbers and no spaces.'));
194 } else if ($this->nicknameExists($nickname)) {
195 $this->showForm(_('Nickname already in use. Try another one.'));
196 } else if (!User::allowed_nickname($nickname)) {
197 $this->showForm(_('Not a valid nickname.'));
198 } else if ($this->emailExists($email)) {
199 $this->showForm(_('Email address already exists.'));
200 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
201 !Validate::uri($homepage,
202 array('allowed_schemes' =>
203 array('http', 'https')))) {
204 $this->showForm(_('Homepage is not a valid URL.'));
206 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
207 $this->showForm(_('Full name is too long (max 255 chars).'));
209 } else if (!is_null($bio) && mb_strlen($bio) > 140) {
210 $this->showForm(_('Bio is too long (max 140 chars).'));
212 } else if (!is_null($location) && mb_strlen($location) > 255) {
213 $this->showForm(_('Location is too long (max 255 chars).'));
215 } else if (strlen($password) < 6) {
216 $this->showForm(_('Password must be 6 or more characters.'));
218 } else if ($password != $confirm) {
219 $this->showForm(_('Passwords don\'t match.'));
220 } else if ($user = User::register(array('nickname' => $nickname,
221 'password' => $password,
223 'fullname' => $fullname,
224 'homepage' => $homepage,
226 'location' => $location,
229 $this->showForm(_('Invalid username or password.'));
233 if (!common_set_user($user)) {
234 $this->serverError(_('Error setting user.'));
237 // this is a real login
238 common_real_login(true);
239 if ($this->boolean('rememberme')) {
240 common_debug('Adding rememberme cookie for ' . $nickname);
241 common_rememberme($user);
244 Event::handle('EndRegistrationTry', array($this));
246 // Re-init language env in case it changed (not yet, but soon)
247 common_init_language();
248 $this->showSuccess();
250 $this->showForm(_('Invalid username or password.'));
256 * Does the given nickname already exist?
258 * Checks a canonical nickname against the database.
260 * @param string $nickname nickname to check
262 * @return boolean true if the nickname already exists
265 function nicknameExists($nickname)
267 $user = User::staticGet('nickname', $nickname);
268 return ($user !== false);
272 * Does the given email address already exist?
274 * Checks a canonical email address against the database.
276 * @param string $email email address to check
278 * @return boolean true if the address already exists
281 function emailExists($email)
283 $email = common_canonical_email($email);
284 if (!$email || strlen($email) == 0) {
287 $user = User::staticGet('email', $email);
288 return ($user !== false);
291 // overrrided to add entry-title class
292 function showPageTitle() {
293 if (Event::handle('StartShowPageTitle', array($this))) {
294 $this->element('h1', array('class' => 'entry-title'), $this->title());
298 // overrided to add hentry, and content-inner class
299 function showContentBlock()
301 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
302 $this->showPageTitle();
303 $this->showPageNoticeBlock();
304 $this->elementStart('div', array('id' => 'content_inner',
305 'class' => 'entry-content'));
306 // show the actual content (forms, lists, whatever)
307 $this->showContent();
308 $this->elementEnd('div');
309 $this->elementEnd('div');
313 * Instructions or a notice for the page
315 * Shows the error, if any, or instructions for registration.
320 function showPageNotice()
322 if ($this->registered) {
324 } else if ($this->error) {
325 $this->element('p', 'error', $this->error);
328 common_markup_to_html(_('With this form you can create '.
330 'You can then post notices and '.
331 'link up to friends and colleagues. '.
332 '(Have an [OpenID](http://openid.net/)? ' .
333 'Try our [OpenID registration]'.
334 '(%%action.openidlogin%%)!)'));
336 $this->elementStart('div', 'instructions');
338 $this->elementEnd('div');
343 * Wrapper for showing a page
345 * Stores an error and shows the page
347 * @param string $error Error, if any
352 function showForm($error=null)
354 $this->error = $error;
359 * Show the page content
361 * Either shows the registration form or, if registration was successful,
362 * instructions for using the site.
367 function showContent()
369 if ($this->registered) {
370 $this->showSuccessContent();
372 $this->showFormContent();
377 * Show the registration form
382 function showFormContent()
384 $this->elementStart('form', array('method' => 'post',
385 'id' => 'form_register',
386 'class' => 'form_settings',
387 'action' => common_local_url('register')));
388 $this->elementStart('fieldset');
389 $this->element('legend', null, 'Account settings');
390 $this->hidden('token', common_session_token());
393 $this->hidden('code', $this->code);
396 $this->elementStart('ul', 'form_data');
397 if (Event::handle('StartRegistrationFormData', array($this))) {
398 $this->elementStart('li');
399 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
400 _('1-64 lowercase letters or numbers, '.
401 'no punctuation or spaces. Required.'));
402 $this->elementEnd('li');
403 $this->elementStart('li');
404 $this->password('password', _('Password'),
405 _('6 or more characters. Required.'));
406 $this->elementEnd('li');
407 $this->elementStart('li');
408 $this->password('confirm', _('Confirm'),
409 _('Same as password above. Required.'));
410 $this->elementEnd('li');
411 $this->elementStart('li');
412 if ($this->invite && $this->invite->address_type == 'email') {
413 $this->input('email', _('Email'), $this->invite->address,
414 _('Used only for updates, announcements, '.
415 'and password recovery'));
417 $this->input('email', _('Email'), $this->trimmed('email'),
418 _('Used only for updates, announcements, '.
419 'and password recovery'));
421 $this->elementEnd('li');
422 $this->elementStart('li');
423 $this->input('fullname', _('Full name'),
424 $this->trimmed('fullname'),
425 _('Longer name, preferably your "real" name'));
426 $this->elementEnd('li');
427 $this->elementStart('li');
428 $this->input('homepage', _('Homepage'),
429 $this->trimmed('homepage'),
430 _('URL of your homepage, blog, '.
431 'or profile on another site'));
432 $this->elementEnd('li');
433 $this->elementStart('li');
434 $this->textarea('bio', _('Bio'),
435 $this->trimmed('bio'),
436 _('Describe yourself and your '.
437 'interests in 140 chars'));
438 $this->elementEnd('li');
439 $this->elementStart('li');
440 $this->input('location', _('Location'),
441 $this->trimmed('location'),
442 _('Where you are, like "City, '.
443 'State (or Region), Country"'));
444 $this->elementEnd('li');
445 Event::handle('EndRegistrationFormData', array($this));
446 $this->elementStart('li', array('id' => 'settings_rememberme'));
447 $this->checkbox('rememberme', _('Remember me'),
448 $this->boolean('rememberme'),
449 _('Automatically login in the future; '.
450 'not for shared computers!'));
451 $this->elementEnd('li');
452 $attrs = array('type' => 'checkbox',
454 'class' => 'checkbox',
457 if ($this->boolean('license')) {
458 $attrs['checked'] = 'checked';
460 $this->elementStart('li');
461 $this->element('input', $attrs);
462 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
463 $this->text(_('My text and files are available under '));
464 $this->element('a', array('href' => common_config('license', 'url')),
465 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
466 $this->text(_(' except this private data: password, '.
467 'email address, IM address, and phone number.'));
468 $this->elementEnd('label');
469 $this->elementEnd('li');
471 $this->elementEnd('ul');
472 $this->submit('submit', _('Register'));
473 $this->elementEnd('fieldset');
474 $this->elementEnd('form');
478 * Show some information about registering for the site
480 * Save the registration flag, run showPage
485 function showSuccess()
487 $this->registered = true;
492 * Show some information about registering for the site
494 * Gives some information and options for new registrees.
499 function showSuccessContent()
501 $nickname = $this->arg('nickname');
503 $profileurl = common_local_url('showstream',
504 array('nickname' => $nickname));
506 $this->elementStart('div', 'success');
507 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
508 'From here, you may want to...'. "\n\n" .
509 '* Go to [your profile](%s) '.
510 'and post your first message.' . "\n" .
511 '* Add a [Jabber/GTalk address]'.
512 '(%%%%action.imsettings%%%%) '.
513 'so you can send notices '.
514 'through instant messages.' . "\n" .
515 '* [Search for people](%%%%action.peoplesearch%%%%) '.
516 'that you may know or '.
517 'that share your interests. ' . "\n" .
518 '* Update your [profile settings]'.
519 '(%%%%action.profilesettings%%%%)'.
520 ' to tell others more about you. ' . "\n" .
521 '* Read over the [online docs](%%%%doc.help%%%%)'.
522 ' for features you may have missed. ' . "\n\n" .
523 'Thanks for signing up and we hope '.
524 'you enjoy using this service.'),
525 $nickname, $profileurl);
527 $this->raw(common_markup_to_html($instr));
529 $have_email = $this->trimmed('email');
531 $emailinstr = _('(You should receive a message by email '.
532 'momentarily, with ' .
533 'instructions on how to confirm '.
534 'your email address.)');
535 $this->raw(common_markup_to_html($emailinstr));
537 $this->elementEnd('div');
541 * Show the login group nav menu
546 function showLocalNav()
548 $nav = new LoginGroupNav($this);