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;
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 * Checks if only OpenID is allowed and redirects to openidlogin if so.
121 * @param array $args $_REQUEST data
126 function handle($args)
128 parent::handle($args);
130 if (common_config('site', 'closed')) {
131 $this->clientError(_('Registration not allowed.'));
132 } else if (common_config('site', 'openidonly')) {
133 common_redirect(common_local_url('openidlogin'));
134 } else if (common_logged_in()) {
135 $this->clientError(_('Already logged in.'));
136 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
137 $this->tryRegister();
143 function showScripts()
145 parent::showScripts();
146 $this->autofocus('nickname');
150 * Try to register a user
152 * Validates the input and tries to save a new user and profile
153 * record. On success, shows an instructions page.
158 function tryRegister()
160 if (Event::handle('StartRegistrationTry', array($this))) {
161 $token = $this->trimmed('token');
162 if (!$token || $token != common_session_token()) {
163 $this->showForm(_('There was a problem with your session token. '.
164 'Try again, please.'));
168 $nickname = $this->trimmed('nickname');
169 $email = $this->trimmed('email');
170 $fullname = $this->trimmed('fullname');
171 $homepage = $this->trimmed('homepage');
172 $bio = $this->trimmed('bio');
173 $location = $this->trimmed('location');
175 // We don't trim these... whitespace is OK in a password!
176 $password = $this->arg('password');
177 $confirm = $this->arg('confirm');
179 // invitation code, if any
180 $code = $this->trimmed('code');
183 $invite = Invitation::staticGet($code);
186 if (common_config('site', 'inviteonly') && !($code && $invite)) {
187 $this->clientError(_('Sorry, only invited people can register.'));
192 $nickname = common_canonical_nickname($nickname);
193 $email = common_canonical_email($email);
195 if (!$this->boolean('license')) {
196 $this->showForm(_('You can\'t register if you don\'t '.
197 'agree to the license.'));
198 } else if ($email && !Validate::email($email, true)) {
199 $this->showForm(_('Not a valid email address.'));
200 } else if (!Validate::string($nickname, array('min_length' => 1,
202 'format' => NICKNAME_FMT))) {
203 $this->showForm(_('Nickname must have only lowercase letters '.
204 'and numbers and no spaces.'));
205 } else if ($this->nicknameExists($nickname)) {
206 $this->showForm(_('Nickname already in use. Try another one.'));
207 } else if (!User::allowed_nickname($nickname)) {
208 $this->showForm(_('Not a valid nickname.'));
209 } else if ($this->emailExists($email)) {
210 $this->showForm(_('Email address already exists.'));
211 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
212 !Validate::uri($homepage,
213 array('allowed_schemes' =>
214 array('http', 'https')))) {
215 $this->showForm(_('Homepage is not a valid URL.'));
217 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
218 $this->showForm(_('Full name is too long (max 255 chars).'));
220 } else if (!is_null($bio) && mb_strlen($bio) > 140) {
221 $this->showForm(_('Bio is too long (max 140 chars).'));
223 } else if (!is_null($location) && mb_strlen($location) > 255) {
224 $this->showForm(_('Location is too long (max 255 chars).'));
226 } else if (strlen($password) < 6) {
227 $this->showForm(_('Password must be 6 or more characters.'));
229 } else if ($password != $confirm) {
230 $this->showForm(_('Passwords don\'t match.'));
231 } else if ($user = User::register(array('nickname' => $nickname,
232 'password' => $password,
234 'fullname' => $fullname,
235 'homepage' => $homepage,
237 'location' => $location,
240 $this->showForm(_('Invalid username or password.'));
244 if (!common_set_user($user)) {
245 $this->serverError(_('Error setting user.'));
248 // this is a real login
249 common_real_login(true);
250 if ($this->boolean('rememberme')) {
251 common_debug('Adding rememberme cookie for ' . $nickname);
252 common_rememberme($user);
255 Event::handle('EndRegistrationTry', array($this));
257 // Re-init language env in case it changed (not yet, but soon)
258 common_init_language();
259 $this->showSuccess();
261 $this->showForm(_('Invalid username or password.'));
267 * Does the given nickname already exist?
269 * Checks a canonical nickname against the database.
271 * @param string $nickname nickname to check
273 * @return boolean true if the nickname already exists
276 function nicknameExists($nickname)
278 $user = User::staticGet('nickname', $nickname);
279 return ($user !== false);
283 * Does the given email address already exist?
285 * Checks a canonical email address against the database.
287 * @param string $email email address to check
289 * @return boolean true if the address already exists
292 function emailExists($email)
294 $email = common_canonical_email($email);
295 if (!$email || strlen($email) == 0) {
298 $user = User::staticGet('email', $email);
299 return ($user !== false);
302 // overrrided to add entry-title class
303 function showPageTitle() {
304 if (Event::handle('StartShowPageTitle', array($this))) {
305 $this->element('h1', array('class' => 'entry-title'), $this->title());
309 // overrided to add hentry, and content-inner class
310 function showContentBlock()
312 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
313 $this->showPageTitle();
314 $this->showPageNoticeBlock();
315 $this->elementStart('div', array('id' => 'content_inner',
316 'class' => 'entry-content'));
317 // show the actual content (forms, lists, whatever)
318 $this->showContent();
319 $this->elementEnd('div');
320 $this->elementEnd('div');
324 * Instructions or a notice for the page
326 * Shows the error, if any, or instructions for registration.
331 function showPageNotice()
333 if ($this->registered) {
335 } else if ($this->error) {
336 $this->element('p', 'error', $this->error);
338 if (common_config('openid', 'enabled')) {
340 common_markup_to_html(_('With this form you can create '.
342 'You can then post notices and '.
343 'link up to friends and colleagues. '.
344 '(Have an [OpenID](http://openid.net/)? ' .
345 'Try our [OpenID registration]'.
346 '(%%action.openidlogin%%)!)'));
349 common_markup_to_html(_('With this form you can create '.
351 'You can then post notices and '.
352 'link up to friends and colleagues.'));
355 $this->elementStart('div', 'instructions');
357 $this->elementEnd('div');
362 * Wrapper for showing a page
364 * Stores an error and shows the page
366 * @param string $error Error, if any
371 function showForm($error=null)
373 $this->error = $error;
378 * Show the page content
380 * Either shows the registration form or, if registration was successful,
381 * instructions for using the site.
386 function showContent()
388 if ($this->registered) {
389 $this->showSuccessContent();
391 $this->showFormContent();
396 * Show the registration form
401 function showFormContent()
403 $code = $this->trimmed('code');
408 $invite = Invitation::staticGet($code);
411 if (common_config('site', 'inviteonly') && !($code && $invite)) {
412 $this->clientError(_('Sorry, only invited people can register.'));
416 $this->elementStart('form', array('method' => 'post',
417 'id' => 'form_register',
418 'class' => 'form_settings',
419 'action' => common_local_url('register')));
420 $this->elementStart('fieldset');
421 $this->element('legend', null, 'Account settings');
422 $this->hidden('token', common_session_token());
425 $this->hidden('code', $this->code);
428 $this->elementStart('ul', 'form_data');
429 if (Event::handle('StartRegistrationFormData', array($this))) {
430 $this->elementStart('li');
431 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
432 _('1-64 lowercase letters or numbers, '.
433 'no punctuation or spaces. Required.'));
434 $this->elementEnd('li');
435 $this->elementStart('li');
436 $this->password('password', _('Password'),
437 _('6 or more characters. Required.'));
438 $this->elementEnd('li');
439 $this->elementStart('li');
440 $this->password('confirm', _('Confirm'),
441 _('Same as password above. Required.'));
442 $this->elementEnd('li');
443 $this->elementStart('li');
444 if ($this->invite && $this->invite->address_type == 'email') {
445 $this->input('email', _('Email'), $this->invite->address,
446 _('Used only for updates, announcements, '.
447 'and password recovery'));
449 $this->input('email', _('Email'), $this->trimmed('email'),
450 _('Used only for updates, announcements, '.
451 'and password recovery'));
453 $this->elementEnd('li');
454 $this->elementStart('li');
455 $this->input('fullname', _('Full name'),
456 $this->trimmed('fullname'),
457 _('Longer name, preferably your "real" name'));
458 $this->elementEnd('li');
459 $this->elementStart('li');
460 $this->input('homepage', _('Homepage'),
461 $this->trimmed('homepage'),
462 _('URL of your homepage, blog, '.
463 'or profile on another site'));
464 $this->elementEnd('li');
465 $this->elementStart('li');
466 $this->textarea('bio', _('Bio'),
467 $this->trimmed('bio'),
468 _('Describe yourself and your '.
469 'interests in 140 chars'));
470 $this->elementEnd('li');
471 $this->elementStart('li');
472 $this->input('location', _('Location'),
473 $this->trimmed('location'),
474 _('Where you are, like "City, '.
475 'State (or Region), Country"'));
476 $this->elementEnd('li');
477 Event::handle('EndRegistrationFormData', array($this));
478 $this->elementStart('li', array('id' => 'settings_rememberme'));
479 $this->checkbox('rememberme', _('Remember me'),
480 $this->boolean('rememberme'),
481 _('Automatically login in the future; '.
482 'not for shared computers!'));
483 $this->elementEnd('li');
484 $attrs = array('type' => 'checkbox',
486 'class' => 'checkbox',
489 if ($this->boolean('license')) {
490 $attrs['checked'] = 'checked';
492 $this->elementStart('li');
493 $this->element('input', $attrs);
494 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
495 $this->text(_('My text and files are available under '));
496 $this->element('a', array('href' => common_config('license', 'url')),
497 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
498 $this->text(_(' except this private data: password, '.
499 'email address, IM address, and phone number.'));
500 $this->elementEnd('label');
501 $this->elementEnd('li');
503 $this->elementEnd('ul');
504 $this->submit('submit', _('Register'));
505 $this->elementEnd('fieldset');
506 $this->elementEnd('form');
510 * Show some information about registering for the site
512 * Save the registration flag, run showPage
517 function showSuccess()
519 $this->registered = true;
524 * Show some information about registering for the site
526 * Gives some information and options for new registrees.
531 function showSuccessContent()
533 $nickname = $this->arg('nickname');
535 $profileurl = common_local_url('showstream',
536 array('nickname' => $nickname));
538 $this->elementStart('div', 'success');
539 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
540 'From here, you may want to...'. "\n\n" .
541 '* Go to [your profile](%s) '.
542 'and post your first message.' . "\n" .
543 '* Add a [Jabber/GTalk address]'.
544 '(%%%%action.imsettings%%%%) '.
545 'so you can send notices '.
546 'through instant messages.' . "\n" .
547 '* [Search for people](%%%%action.peoplesearch%%%%) '.
548 'that you may know or '.
549 'that share your interests. ' . "\n" .
550 '* Update your [profile settings]'.
551 '(%%%%action.profilesettings%%%%)'.
552 ' to tell others more about you. ' . "\n" .
553 '* Read over the [online docs](%%%%doc.help%%%%)'.
554 ' for features you may have missed. ' . "\n\n" .
555 'Thanks for signing up and we hope '.
556 'you enjoy using this service.'),
557 $nickname, $profileurl);
559 $this->raw(common_markup_to_html($instr));
561 $have_email = $this->trimmed('email');
563 $emailinstr = _('(You should receive a message by email '.
564 'momentarily, with ' .
565 'instructions on how to confirm '.
566 'your email address.)');
567 $this->raw(common_markup_to_html($emailinstr));
569 $this->elementEnd('div');
573 * Show the login group nav menu
578 function showLocalNav()
580 $nav = new LoginGroupNav($this);