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;
61 * @return string title
66 if ($this->registered) {
67 return _('Registration successful');
74 * Handle input, produce output
76 * Switches on request method; either shows the form or handles its input.
78 * Checks if registration is closed and shows an error if so.
80 * @param array $args $_REQUEST data
85 function handle($args)
87 parent::handle($args);
89 if (common_config('site', 'closed')) {
90 $this->clientError(_('Registration not allowed.'));
91 } else if (common_logged_in()) {
92 $this->clientError(_('Already logged in.'));
93 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
101 * Try to register a user
103 * Validates the input and tries to save a new user and profile
104 * record. On success, shows an instructions page.
109 function tryRegister()
111 if (Event::handle('StartRegistrationTry', array($this))) {
112 $token = $this->trimmed('token');
113 if (!$token || $token != common_session_token()) {
114 $this->showForm(_('There was a problem with your session token. '.
115 'Try again, please.'));
119 $nickname = $this->trimmed('nickname');
120 $email = $this->trimmed('email');
121 $fullname = $this->trimmed('fullname');
122 $homepage = $this->trimmed('homepage');
123 $bio = $this->trimmed('bio');
124 $location = $this->trimmed('location');
126 // We don't trim these... whitespace is OK in a password!
127 $password = $this->arg('password');
128 $confirm = $this->arg('confirm');
130 // invitation code, if any
131 $code = $this->trimmed('code');
134 $invite = Invitation::staticGet($code);
137 if (common_config('site', 'inviteonly') && !($code && $invite)) {
138 $this->clientError(_('Sorry, only invited people can register.'));
143 $nickname = common_canonical_nickname($nickname);
144 $email = common_canonical_email($email);
146 if (!$this->boolean('license')) {
147 $this->showForm(_('You can\'t register if you don\'t '.
148 'agree to the license.'));
149 } else if ($email && !Validate::email($email, true)) {
150 $this->showForm(_('Not a valid email address.'));
151 } else if (!Validate::string($nickname, array('min_length' => 1,
153 'format' => NICKNAME_FMT))) {
154 $this->showForm(_('Nickname must have only lowercase letters '.
155 'and numbers and no spaces.'));
156 } else if ($this->nicknameExists($nickname)) {
157 $this->showForm(_('Nickname already in use. Try another one.'));
158 } else if (!User::allowed_nickname($nickname)) {
159 $this->showForm(_('Not a valid nickname.'));
160 } else if ($this->emailExists($email)) {
161 $this->showForm(_('Email address already exists.'));
162 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
163 !Validate::uri($homepage,
164 array('allowed_schemes' =>
165 array('http', 'https')))) {
166 $this->showForm(_('Homepage is not a valid URL.'));
168 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
169 $this->showForm(_('Full name is too long (max 255 chars).'));
171 } else if (!is_null($bio) && mb_strlen($bio) > 140) {
172 $this->showForm(_('Bio is too long (max 140 chars).'));
174 } else if (!is_null($location) && mb_strlen($location) > 255) {
175 $this->showForm(_('Location is too long (max 255 chars).'));
177 } else if (strlen($password) < 6) {
178 $this->showForm(_('Password must be 6 or more characters.'));
180 } else if ($password != $confirm) {
181 $this->showForm(_('Passwords don\'t match.'));
182 } else if ($user = User::register(array('nickname' => $nickname,
183 'password' => $password,
185 'fullname' => $fullname,
186 'homepage' => $homepage,
188 'location' => $location,
191 $this->showForm(_('Invalid username or password.'));
195 if (!common_set_user($user)) {
196 $this->serverError(_('Error setting user.'));
199 // this is a real login
200 common_real_login(true);
201 if ($this->boolean('rememberme')) {
202 common_debug('Adding rememberme cookie for ' . $nickname);
203 common_rememberme($user);
206 Event::handle('EndRegistrationTry', array($this));
208 // Re-init language env in case it changed (not yet, but soon)
209 common_init_language();
210 $this->showSuccess();
212 $this->showForm(_('Invalid username or password.'));
218 * Does the given nickname already exist?
220 * Checks a canonical nickname against the database.
222 * @param string $nickname nickname to check
224 * @return boolean true if the nickname already exists
227 function nicknameExists($nickname)
229 $user = User::staticGet('nickname', $nickname);
230 return ($user !== false);
234 * Does the given email address already exist?
236 * Checks a canonical email address against the database.
238 * @param string $email email address to check
240 * @return boolean true if the address already exists
243 function emailExists($email)
245 $email = common_canonical_email($email);
246 if (!$email || strlen($email) == 0) {
249 $user = User::staticGet('email', $email);
250 return ($user !== false);
253 // overrrided to add entry-title class
254 function showPageTitle() {
255 if (Event::handle('StartShowPageTitle', array($this))) {
256 $this->element('h1', array('class' => 'entry-title'), $this->title());
260 // overrided to add hentry, and content-inner class
261 function showContentBlock()
263 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
264 $this->showPageTitle();
265 $this->showPageNoticeBlock();
266 $this->elementStart('div', array('id' => 'content_inner',
267 'class' => 'entry-content'));
268 // show the actual content (forms, lists, whatever)
269 $this->showContent();
270 $this->elementEnd('div');
271 $this->elementEnd('div');
275 * Instructions or a notice for the page
277 * Shows the error, if any, or instructions for registration.
282 function showPageNotice()
284 if ($this->registered) {
286 } else if ($this->error) {
287 $this->element('p', 'error', $this->error);
290 common_markup_to_html(_('With this form you can create '.
292 'You can then post notices and '.
293 'link up to friends and colleagues. '.
294 '(Have an [OpenID](http://openid.net/)? ' .
295 'Try our [OpenID registration]'.
296 '(%%action.openidlogin%%)!)'));
298 $this->elementStart('div', 'instructions');
300 $this->elementEnd('div');
305 * Wrapper for showing a page
307 * Stores an error and shows the page
309 * @param string $error Error, if any
314 function showForm($error=null)
316 $this->error = $error;
321 * Show the page content
323 * Either shows the registration form or, if registration was successful,
324 * instructions for using the site.
329 function showContent()
331 if ($this->registered) {
332 $this->showSuccessContent();
334 $this->showFormContent();
339 * Show the registration form
344 function showFormContent()
346 $code = $this->trimmed('code');
349 $invite = Invitation::staticGet($code);
352 if (common_config('site', 'inviteonly') && !($code && $invite)) {
353 $this->clientError(_('Sorry, only invited people can register.'));
357 $this->elementStart('form', array('method' => 'post',
358 'id' => 'form_register',
359 'class' => 'form_settings',
360 'action' => common_local_url('register')));
361 $this->elementStart('fieldset');
362 $this->element('legend', null, 'Account settings');
363 $this->hidden('token', common_session_token());
366 $this->hidden('code', $code);
369 $this->elementStart('ul', 'form_data');
370 if (Event::handle('StartRegistrationFormData', array($this))) {
371 $this->elementStart('li');
372 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
373 _('1-64 lowercase letters or numbers, '.
374 'no punctuation or spaces. Required.'));
375 $this->elementEnd('li');
376 $this->elementStart('li');
377 $this->password('password', _('Password'),
378 _('6 or more characters. Required.'));
379 $this->elementEnd('li');
380 $this->elementStart('li');
381 $this->password('confirm', _('Confirm'),
382 _('Same as password above. Required.'));
383 $this->elementEnd('li');
384 $this->elementStart('li');
385 if ($invite && $invite->address_type == 'email') {
386 $this->input('email', _('Email'), $invite->address,
387 _('Used only for updates, announcements, '.
388 'and password recovery'));
390 $this->input('email', _('Email'), $this->trimmed('email'),
391 _('Used only for updates, announcements, '.
392 'and password recovery'));
394 $this->elementEnd('li');
395 $this->elementStart('li');
396 $this->input('fullname', _('Full name'),
397 $this->trimmed('fullname'),
398 _('Longer name, preferably your "real" name'));
399 $this->elementEnd('li');
400 $this->elementStart('li');
401 $this->input('homepage', _('Homepage'),
402 $this->trimmed('homepage'),
403 _('URL of your homepage, blog, '.
404 'or profile on another site'));
405 $this->elementEnd('li');
406 $this->elementStart('li');
407 $this->textarea('bio', _('Bio'),
408 $this->trimmed('bio'),
409 _('Describe yourself and your '.
410 'interests in 140 chars'));
411 $this->elementEnd('li');
412 $this->elementStart('li');
413 $this->input('location', _('Location'),
414 $this->trimmed('location'),
415 _('Where you are, like "City, '.
416 'State (or Region), Country"'));
417 $this->elementEnd('li');
418 Event::handle('EndRegistrationFormData', array($this));
419 $this->elementStart('li', array('id' => 'settings_rememberme'));
420 $this->checkbox('rememberme', _('Remember me'),
421 $this->boolean('rememberme'),
422 _('Automatically login in the future; '.
423 'not for shared computers!'));
424 $this->elementEnd('li');
425 $attrs = array('type' => 'checkbox',
427 'class' => 'checkbox',
430 if ($this->boolean('license')) {
431 $attrs['checked'] = 'checked';
433 $this->elementStart('li');
434 $this->element('input', $attrs);
435 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
436 $this->text(_('My text and files are available under '));
437 $this->element('a', array('href' => common_config('license', 'url')),
438 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
439 $this->text(_(' except this private data: password, '.
440 'email address, IM address, and phone number.'));
441 $this->elementEnd('label');
442 $this->elementEnd('li');
444 $this->elementEnd('ul');
445 $this->submit('submit', _('Register'));
446 $this->elementEnd('fieldset');
447 $this->elementEnd('form');
451 * Show some information about registering for the site
453 * Save the registration flag, run showPage
458 function showSuccess()
460 $this->registered = true;
465 * Show some information about registering for the site
467 * Gives some information and options for new registrees.
472 function showSuccessContent()
474 $nickname = $this->arg('nickname');
476 $profileurl = common_local_url('showstream',
477 array('nickname' => $nickname));
479 $this->elementStart('div', 'success');
480 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
481 'From here, you may want to...'. "\n\n" .
482 '* Go to [your profile](%s) '.
483 'and post your first message.' . "\n" .
484 '* Add a [Jabber/GTalk address]'.
485 '(%%%%action.imsettings%%%%) '.
486 'so you can send notices '.
487 'through instant messages.' . "\n" .
488 '* [Search for people](%%%%action.peoplesearch%%%%) '.
489 'that you may know or '.
490 'that share your interests. ' . "\n" .
491 '* Update your [profile settings]'.
492 '(%%%%action.profilesettings%%%%)'.
493 ' to tell others more about you. ' . "\n" .
494 '* Read over the [online docs](%%%%doc.help%%%%)'.
495 ' for features you may have missed. ' . "\n\n" .
496 'Thanks for signing up and we hope '.
497 'you enjoy using this service.'),
498 $nickname, $profileurl);
500 $this->raw(common_markup_to_html($instr));
502 $have_email = $this->trimmed('email');
504 $emailinstr = _('(You should receive a message by email '.
505 'momentarily, with ' .
506 'instructions on how to confirm '.
507 'your email address.)');
508 $this->raw(common_markup_to_html($emailinstr));
510 $this->elementEnd('div');
514 * Show the login group nav menu
519 function showLocalNav()
521 $nav = new LoginGroupNav($this);