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 if (empty($this->code)) {
78 common_ensure_session();
79 if (array_key_exists('invitecode', $_SESSION)) {
80 $this->code = $_SESSION['invitecode'];
84 if (common_config('site', 'inviteonly') && empty($this->code)) {
85 $this->clientError(_('Sorry, only invited people can register.'));
89 if (!empty($this->code)) {
90 $this->invite = Invitation::staticGet('code', $this->code);
91 if (empty($this->invite)) {
92 $this->clientError(_('Sorry, invalid invitation code.'));
95 // Store this in case we need it
96 common_ensure_session();
97 $_SESSION['invitecode'] = $this->code;
106 * @return string title
111 if ($this->registered) {
112 return _('Registration successful');
114 return _('Register');
119 * Handle input, produce output
121 * Switches on request method; either shows the form or handles its input.
123 * Checks if registration is closed and shows an error if so.
125 * @param array $args $_REQUEST data
130 function handle($args)
132 parent::handle($args);
134 if (common_config('site', 'closed')) {
135 $this->clientError(_('Registration not allowed.'));
136 } else if (common_logged_in()) {
137 $this->clientError(_('Already logged in.'));
138 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
139 $this->tryRegister();
145 function showScripts()
147 parent::showScripts();
148 $this->autofocus('nickname');
152 * Try to register a user
154 * Validates the input and tries to save a new user and profile
155 * record. On success, shows an instructions page.
160 function tryRegister()
162 if (Event::handle('StartRegistrationTry', array($this))) {
163 $token = $this->trimmed('token');
164 if (!$token || $token != common_session_token()) {
165 $this->showForm(_('There was a problem with your session token. '.
166 'Try again, please.'));
170 $nickname = $this->trimmed('nickname');
171 $email = $this->trimmed('email');
172 $fullname = $this->trimmed('fullname');
173 $homepage = $this->trimmed('homepage');
174 $bio = $this->trimmed('bio');
175 $location = $this->trimmed('location');
177 // We don't trim these... whitespace is OK in a password!
178 $password = $this->arg('password');
179 $confirm = $this->arg('confirm');
181 // invitation code, if any
182 $code = $this->trimmed('code');
185 $invite = Invitation::staticGet($code);
188 if (common_config('site', 'inviteonly') && !($code && $invite)) {
189 $this->clientError(_('Sorry, only invited people can register.'));
194 $nickname = common_canonical_nickname($nickname);
195 $email = common_canonical_email($email);
197 if (!$this->boolean('license')) {
198 $this->showForm(_('You can\'t register if you don\'t '.
199 'agree to the license.'));
200 } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
201 $this->showForm(_('Not a valid email address.'));
202 } else if (!Validate::string($nickname, array('min_length' => 1,
204 'format' => NICKNAME_FMT))) {
205 $this->showForm(_('Nickname must have only lowercase letters '.
206 'and numbers and no spaces.'));
207 } else if ($this->nicknameExists($nickname)) {
208 $this->showForm(_('Nickname already in use. Try another one.'));
209 } else if (!User::allowed_nickname($nickname)) {
210 $this->showForm(_('Not a valid nickname.'));
211 } else if ($this->emailExists($email)) {
212 $this->showForm(_('Email address already exists.'));
213 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
214 !Validate::uri($homepage,
215 array('allowed_schemes' =>
216 array('http', 'https')))) {
217 $this->showForm(_('Homepage is not a valid URL.'));
219 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
220 $this->showForm(_('Full name is too long (max 255 chars).'));
222 } else if (Profile::bioTooLong($bio)) {
223 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
226 } else if (!is_null($location) && mb_strlen($location) > 255) {
227 $this->showForm(_('Location is too long (max 255 chars).'));
229 } else if (strlen($password) < 6) {
230 $this->showForm(_('Password must be 6 or more characters.'));
232 } else if ($password != $confirm) {
233 $this->showForm(_('Passwords don\'t match.'));
234 } else if ($user = User::register(array('nickname' => $nickname,
235 'password' => $password,
237 'fullname' => $fullname,
238 'homepage' => $homepage,
240 'location' => $location,
243 $this->showForm(_('Invalid username or password.'));
247 if (!common_set_user($user)) {
248 $this->serverError(_('Error setting user.'));
251 // this is a real login
252 common_real_login(true);
253 if ($this->boolean('rememberme')) {
254 common_debug('Adding rememberme cookie for ' . $nickname);
255 common_rememberme($user);
258 Event::handle('EndRegistrationTry', array($this));
260 // Re-init language env in case it changed (not yet, but soon)
261 common_init_language();
262 $this->showSuccess();
264 $this->showForm(_('Invalid username or password.'));
270 * Does the given nickname already exist?
272 * Checks a canonical nickname against the database.
274 * @param string $nickname nickname to check
276 * @return boolean true if the nickname already exists
279 function nicknameExists($nickname)
281 $user = User::staticGet('nickname', $nickname);
282 return ($user !== false);
286 * Does the given email address already exist?
288 * Checks a canonical email address against the database.
290 * @param string $email email address to check
292 * @return boolean true if the address already exists
295 function emailExists($email)
297 $email = common_canonical_email($email);
298 if (!$email || strlen($email) == 0) {
301 $user = User::staticGet('email', $email);
302 return ($user !== false);
305 // overrrided to add entry-title class
306 function showPageTitle() {
307 if (Event::handle('StartShowPageTitle', array($this))) {
308 $this->element('h1', array('class' => 'entry-title'), $this->title());
312 // overrided to add hentry, and content-inner class
313 function showContentBlock()
315 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
316 $this->showPageTitle();
317 $this->showPageNoticeBlock();
318 $this->elementStart('div', array('id' => 'content_inner',
319 'class' => 'entry-content'));
320 // show the actual content (forms, lists, whatever)
321 $this->showContent();
322 $this->elementEnd('div');
323 $this->elementEnd('div');
327 * Instructions or a notice for the page
329 * Shows the error, if any, or instructions for registration.
334 function showPageNotice()
336 if ($this->registered) {
338 } else if ($this->error) {
339 $this->element('p', 'error', $this->error);
342 common_markup_to_html(_('With this form you can create '.
344 'You can then post notices and '.
345 'link up to friends and colleagues. '));
347 $this->elementStart('div', 'instructions');
349 $this->elementEnd('div');
354 * Wrapper for showing a page
356 * Stores an error and shows the page
358 * @param string $error Error, if any
363 function showForm($error=null)
365 $this->error = $error;
370 * Show the page content
372 * Either shows the registration form or, if registration was successful,
373 * instructions for using the site.
378 function showContent()
380 if ($this->registered) {
381 $this->showSuccessContent();
383 $this->showFormContent();
388 * Show the registration form
393 function showFormContent()
395 $code = $this->trimmed('code');
400 $invite = Invitation::staticGet($code);
403 if (common_config('site', 'inviteonly') && !($code && $invite)) {
404 $this->clientError(_('Sorry, only invited people can register.'));
408 $this->elementStart('form', array('method' => 'post',
409 'id' => 'form_register',
410 'class' => 'form_settings',
411 'action' => common_local_url('register')));
412 $this->elementStart('fieldset');
413 $this->element('legend', null, 'Account settings');
414 $this->hidden('token', common_session_token());
417 $this->hidden('code', $this->code);
420 $this->elementStart('ul', 'form_data');
421 if (Event::handle('StartRegistrationFormData', array($this))) {
422 $this->elementStart('li');
423 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
424 _('1-64 lowercase letters or numbers, '.
425 'no punctuation or spaces. Required.'));
426 $this->elementEnd('li');
427 $this->elementStart('li');
428 $this->password('password', _('Password'),
429 _('6 or more characters. Required.'));
430 $this->elementEnd('li');
431 $this->elementStart('li');
432 $this->password('confirm', _('Confirm'),
433 _('Same as password above. Required.'));
434 $this->elementEnd('li');
435 $this->elementStart('li');
436 if ($this->invite && $this->invite->address_type == 'email') {
437 $this->input('email', _('Email'), $this->invite->address,
438 _('Used only for updates, announcements, '.
439 'and password recovery'));
441 $this->input('email', _('Email'), $this->trimmed('email'),
442 _('Used only for updates, announcements, '.
443 'and password recovery'));
445 $this->elementEnd('li');
446 $this->elementStart('li');
447 $this->input('fullname', _('Full name'),
448 $this->trimmed('fullname'),
449 _('Longer name, preferably your "real" name'));
450 $this->elementEnd('li');
451 $this->elementStart('li');
452 $this->input('homepage', _('Homepage'),
453 $this->trimmed('homepage'),
454 _('URL of your homepage, blog, '.
455 'or profile on another site'));
456 $this->elementEnd('li');
457 $this->elementStart('li');
458 $maxBio = Profile::maxBio();
460 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
463 $bioInstr = _('Describe yourself and your interests');
465 $this->textarea('bio', _('Bio'),
466 $this->trimmed('bio'),
468 $this->elementEnd('li');
469 $this->elementStart('li');
470 $this->input('location', _('Location'),
471 $this->trimmed('location'),
472 _('Where you are, like "City, '.
473 'State (or Region), Country"'));
474 $this->elementEnd('li');
475 Event::handle('EndRegistrationFormData', array($this));
476 $this->elementStart('li', array('id' => 'settings_rememberme'));
477 $this->checkbox('rememberme', _('Remember me'),
478 $this->boolean('rememberme'),
479 _('Automatically login in the future; '.
480 'not for shared computers!'));
481 $this->elementEnd('li');
482 $attrs = array('type' => 'checkbox',
484 'class' => 'checkbox',
487 if ($this->boolean('license')) {
488 $attrs['checked'] = 'checked';
490 $this->elementStart('li');
491 $this->element('input', $attrs);
492 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
493 $this->text(_('My text and files are available under '));
494 $this->element('a', array('href' => common_config('license', 'url')),
495 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
496 $this->text(_(' except this private data: password, '.
497 'email address, IM address, and phone number.'));
498 $this->elementEnd('label');
499 $this->elementEnd('li');
501 $this->elementEnd('ul');
502 $this->submit('submit', _('Register'));
503 $this->elementEnd('fieldset');
504 $this->elementEnd('form');
508 * Show some information about registering for the site
510 * Save the registration flag, run showPage
515 function showSuccess()
517 $this->registered = true;
522 * Show some information about registering for the site
524 * Gives some information and options for new registrees.
529 function showSuccessContent()
531 $nickname = $this->arg('nickname');
533 $profileurl = common_local_url('showstream',
534 array('nickname' => $nickname));
536 $this->elementStart('div', 'success');
537 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
538 'From here, you may want to...'. "\n\n" .
539 '* Go to [your profile](%s) '.
540 'and post your first message.' . "\n" .
541 '* Add a [Jabber/GTalk address]'.
542 '(%%%%action.imsettings%%%%) '.
543 'so you can send notices '.
544 'through instant messages.' . "\n" .
545 '* [Search for people](%%%%action.peoplesearch%%%%) '.
546 'that you may know or '.
547 'that share your interests. ' . "\n" .
548 '* Update your [profile settings]'.
549 '(%%%%action.profilesettings%%%%)'.
550 ' to tell others more about you. ' . "\n" .
551 '* Read over the [online docs](%%%%doc.help%%%%)'.
552 ' for features you may have missed. ' . "\n\n" .
553 'Thanks for signing up and we hope '.
554 'you enjoy using this service.'),
555 $nickname, $profileurl);
557 $this->raw(common_markup_to_html($instr));
559 $have_email = $this->trimmed('email');
561 $emailinstr = _('(You should receive a message by email '.
562 'momentarily, with ' .
563 'instructions on how to confirm '.
564 'your email address.)');
565 $this->raw(common_markup_to_html($emailinstr));
567 $this->elementEnd('div');
571 * Show the login group nav menu
576 function showLocalNav()
578 $nav = new LoginGroupNav($this);