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();
144 * Try to register a user
146 * Validates the input and tries to save a new user and profile
147 * record. On success, shows an instructions page.
152 function tryRegister()
154 if (Event::handle('StartRegistrationTry', array($this))) {
155 $token = $this->trimmed('token');
156 if (!$token || $token != common_session_token()) {
157 $this->showForm(_('There was a problem with your session token. '.
158 'Try again, please.'));
162 $nickname = $this->trimmed('nickname');
163 $email = $this->trimmed('email');
164 $fullname = $this->trimmed('fullname');
165 $homepage = $this->trimmed('homepage');
166 $bio = $this->trimmed('bio');
167 $location = $this->trimmed('location');
169 // We don't trim these... whitespace is OK in a password!
170 $password = $this->arg('password');
171 $confirm = $this->arg('confirm');
173 // invitation code, if any
174 $code = $this->trimmed('code');
177 $invite = Invitation::staticGet($code);
180 if (common_config('site', 'inviteonly') && !($code && $invite)) {
181 $this->clientError(_('Sorry, only invited people can register.'));
186 $nickname = common_canonical_nickname($nickname);
187 $email = common_canonical_email($email);
189 if (!$this->boolean('license')) {
190 $this->showForm(_('You can\'t register if you don\'t '.
191 'agree to the license.'));
192 } else if ($email && !Validate::email($email, true)) {
193 $this->showForm(_('Not a valid email address.'));
194 } else if (!Validate::string($nickname, array('min_length' => 1,
196 'format' => NICKNAME_FMT))) {
197 $this->showForm(_('Nickname must have only lowercase letters '.
198 'and numbers and no spaces.'));
199 } else if ($this->nicknameExists($nickname)) {
200 $this->showForm(_('Nickname already in use. Try another one.'));
201 } else if (!User::allowed_nickname($nickname)) {
202 $this->showForm(_('Not a valid nickname.'));
203 } else if ($this->emailExists($email)) {
204 $this->showForm(_('Email address already exists.'));
205 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
206 !Validate::uri($homepage,
207 array('allowed_schemes' =>
208 array('http', 'https')))) {
209 $this->showForm(_('Homepage is not a valid URL.'));
211 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
212 $this->showForm(_('Full name is too long (max 255 chars).'));
214 } else if (!is_null($bio) && mb_strlen($bio) > 140) {
215 $this->showForm(_('Bio is too long (max 140 chars).'));
217 } else if (!is_null($location) && mb_strlen($location) > 255) {
218 $this->showForm(_('Location is too long (max 255 chars).'));
220 } else if (strlen($password) < 6) {
221 $this->showForm(_('Password must be 6 or more characters.'));
223 } else if ($password != $confirm) {
224 $this->showForm(_('Passwords don\'t match.'));
225 } else if ($user = User::register(array('nickname' => $nickname,
226 'password' => $password,
228 'fullname' => $fullname,
229 'homepage' => $homepage,
231 'location' => $location,
234 $this->showForm(_('Invalid username or password.'));
238 if (!common_set_user($user)) {
239 $this->serverError(_('Error setting user.'));
242 // this is a real login
243 common_real_login(true);
244 if ($this->boolean('rememberme')) {
245 common_debug('Adding rememberme cookie for ' . $nickname);
246 common_rememberme($user);
249 Event::handle('EndRegistrationTry', array($this));
251 // Re-init language env in case it changed (not yet, but soon)
252 common_init_language();
253 $this->showSuccess();
255 $this->showForm(_('Invalid username or password.'));
261 * Does the given nickname already exist?
263 * Checks a canonical nickname against the database.
265 * @param string $nickname nickname to check
267 * @return boolean true if the nickname already exists
270 function nicknameExists($nickname)
272 $user = User::staticGet('nickname', $nickname);
273 return ($user !== false);
277 * Does the given email address already exist?
279 * Checks a canonical email address against the database.
281 * @param string $email email address to check
283 * @return boolean true if the address already exists
286 function emailExists($email)
288 $email = common_canonical_email($email);
289 if (!$email || strlen($email) == 0) {
292 $user = User::staticGet('email', $email);
293 return ($user !== false);
296 // overrrided to add entry-title class
297 function showPageTitle() {
298 if (Event::handle('StartShowPageTitle', array($this))) {
299 $this->element('h1', array('class' => 'entry-title'), $this->title());
303 // overrided to add hentry, and content-inner class
304 function showContentBlock()
306 $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
307 $this->showPageTitle();
308 $this->showPageNoticeBlock();
309 $this->elementStart('div', array('id' => 'content_inner',
310 'class' => 'entry-content'));
311 // show the actual content (forms, lists, whatever)
312 $this->showContent();
313 $this->elementEnd('div');
314 $this->elementEnd('div');
318 * Instructions or a notice for the page
320 * Shows the error, if any, or instructions for registration.
325 function showPageNotice()
327 if ($this->registered) {
329 } else if ($this->error) {
330 $this->element('p', 'error', $this->error);
332 if (common_config('openid', 'enabled')) {
334 common_markup_to_html(_('With this form you can create '.
336 'You can then post notices and '.
337 'link up to friends and colleagues. '.
338 '(Have an [OpenID](http://openid.net/)? ' .
339 'Try our [OpenID registration]'.
340 '(%%action.openidlogin%%)!)'));
343 common_markup_to_html(_('With this form you can create '.
345 'You can then post notices and '.
346 'link up to friends and colleagues.'));
349 $this->elementStart('div', 'instructions');
351 $this->elementEnd('div');
356 * Wrapper for showing a page
358 * Stores an error and shows the page
360 * @param string $error Error, if any
365 function showForm($error=null)
367 $this->error = $error;
372 * Show the page content
374 * Either shows the registration form or, if registration was successful,
375 * instructions for using the site.
380 function showContent()
382 if ($this->registered) {
383 $this->showSuccessContent();
385 $this->showFormContent();
390 * Show the registration form
395 function showFormContent()
397 $code = $this->trimmed('code');
402 $invite = Invitation::staticGet($code);
405 if (common_config('site', 'inviteonly') && !($code && $invite)) {
406 $this->clientError(_('Sorry, only invited people can register.'));
410 $this->elementStart('form', array('method' => 'post',
411 'id' => 'form_register',
412 'class' => 'form_settings',
413 'action' => common_local_url('register')));
414 $this->elementStart('fieldset');
415 $this->element('legend', null, 'Account settings');
416 $this->hidden('token', common_session_token());
419 $this->hidden('code', $this->code);
422 $this->elementStart('ul', 'form_data');
423 if (Event::handle('StartRegistrationFormData', array($this))) {
424 $this->elementStart('li');
425 $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
426 _('1-64 lowercase letters or numbers, '.
427 'no punctuation or spaces. Required.'));
428 $this->elementEnd('li');
429 $this->elementStart('li');
430 $this->password('password', _('Password'),
431 _('6 or more characters. Required.'));
432 $this->elementEnd('li');
433 $this->elementStart('li');
434 $this->password('confirm', _('Confirm'),
435 _('Same as password above. Required.'));
436 $this->elementEnd('li');
437 $this->elementStart('li');
438 if ($this->invite && $this->invite->address_type == 'email') {
439 $this->input('email', _('Email'), $this->invite->address,
440 _('Used only for updates, announcements, '.
441 'and password recovery'));
443 $this->input('email', _('Email'), $this->trimmed('email'),
444 _('Used only for updates, announcements, '.
445 'and password recovery'));
447 $this->elementEnd('li');
448 $this->elementStart('li');
449 $this->input('fullname', _('Full name'),
450 $this->trimmed('fullname'),
451 _('Longer name, preferably your "real" name'));
452 $this->elementEnd('li');
453 $this->elementStart('li');
454 $this->input('homepage', _('Homepage'),
455 $this->trimmed('homepage'),
456 _('URL of your homepage, blog, '.
457 'or profile on another site'));
458 $this->elementEnd('li');
459 $this->elementStart('li');
460 $this->textarea('bio', _('Bio'),
461 $this->trimmed('bio'),
462 _('Describe yourself and your '.
463 'interests in 140 chars'));
464 $this->elementEnd('li');
465 $this->elementStart('li');
466 $this->input('location', _('Location'),
467 $this->trimmed('location'),
468 _('Where you are, like "City, '.
469 'State (or Region), Country"'));
470 $this->elementEnd('li');
471 Event::handle('EndRegistrationFormData', array($this));
472 $this->elementStart('li', array('id' => 'settings_rememberme'));
473 $this->checkbox('rememberme', _('Remember me'),
474 $this->boolean('rememberme'),
475 _('Automatically login in the future; '.
476 'not for shared computers!'));
477 $this->elementEnd('li');
478 $attrs = array('type' => 'checkbox',
480 'class' => 'checkbox',
483 if ($this->boolean('license')) {
484 $attrs['checked'] = 'checked';
486 $this->elementStart('li');
487 $this->element('input', $attrs);
488 $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
489 $this->text(_('My text and files are available under '));
490 $this->element('a', array('href' => common_config('license', 'url')),
491 common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
492 $this->text(_(' except this private data: password, '.
493 'email address, IM address, and phone number.'));
494 $this->elementEnd('label');
495 $this->elementEnd('li');
497 $this->elementEnd('ul');
498 $this->submit('submit', _('Register'));
499 $this->elementEnd('fieldset');
500 $this->elementEnd('form');
504 * Show some information about registering for the site
506 * Save the registration flag, run showPage
511 function showSuccess()
513 $this->registered = true;
518 * Show some information about registering for the site
520 * Gives some information and options for new registrees.
525 function showSuccessContent()
527 $nickname = $this->arg('nickname');
529 $profileurl = common_local_url('showstream',
530 array('nickname' => $nickname));
532 $this->elementStart('div', 'success');
533 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
534 'From here, you may want to...'. "\n\n" .
535 '* Go to [your profile](%s) '.
536 'and post your first message.' . "\n" .
537 '* Add a [Jabber/GTalk address]'.
538 '(%%%%action.imsettings%%%%) '.
539 'so you can send notices '.
540 'through instant messages.' . "\n" .
541 '* [Search for people](%%%%action.peoplesearch%%%%) '.
542 'that you may know or '.
543 'that share your interests. ' . "\n" .
544 '* Update your [profile settings]'.
545 '(%%%%action.profilesettings%%%%)'.
546 ' to tell others more about you. ' . "\n" .
547 '* Read over the [online docs](%%%%doc.help%%%%)'.
548 ' for features you may have missed. ' . "\n\n" .
549 'Thanks for signing up and we hope '.
550 'you enjoy using this service.'),
551 $nickname, $profileurl);
553 $this->raw(common_markup_to_html($instr));
555 $have_email = $this->trimmed('email');
557 $emailinstr = _('(You should receive a message by email '.
558 'momentarily, with ' .
559 'instructions on how to confirm '.
560 'your email address.)');
561 $this->raw(common_markup_to_html($emailinstr));
563 $this->elementEnd('div');
567 * Show the login group nav menu
572 function showLocalNav()
574 $nav = new LoginGroupNav($this);