We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

register.php 26.4 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5 6 7 8 9
 * Register a new user account
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
10 11 12
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Evan Prodromou's avatar
Evan Prodromou committed
13
 *
Evan Prodromou's avatar
Evan Prodromou committed
14 15 16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
18
 *
Evan Prodromou's avatar
Evan Prodromou committed
19 20
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 22
 *
 * @category  Login
23
 * @package   StatusNet
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/
Evan Prodromou's avatar
Evan Prodromou committed
28 29
 */

30
if (!defined('STATUSNET') && !defined('LACONICA')) {
31 32 33 34 35 36 37
    exit(1);
}

/**
 * An action for registering a new user account
 *
 * @category Login
38
 * @package  StatusNet
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/
42
 */
43 44
class RegisterAction extends Action
{
45 46 47 48 49 50 51 52 53 54
    /**
     * Has there been an error?
     */
    var $error = null;

    /**
     * Have we registered?
     */
    var $registered = false;

55 56 57 58 59
    /**
     * Are we processing an invite?
     */
    var $invite = null;

60 61 62 63 64 65 66
    /**
     * Prepare page to run
     *
     *
     * @param $args
     * @return string title
     */
Evan Prodromou's avatar
Evan Prodromou committed
67
    function prepare($args)
68
    {
Evan Prodromou's avatar
Evan Prodromou committed
69
        parent::prepare($args);
70 71 72 73
        $this->code = $this->trimmed('code');

        if (empty($this->code)) {
            common_ensure_session();
Evan Prodromou's avatar
Evan Prodromou committed
74
            if (array_key_exists('invitecode', $_SESSION)) {
75 76 77 78 79
                $this->code = $_SESSION['invitecode'];
            }
        }

        if (common_config('site', 'inviteonly') && empty($this->code)) {
80
            // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
81
            $this->clientError(_('Sorry, only invited people can register.'));
82 83 84 85
            return false;
        }

        if (!empty($this->code)) {
Evan Prodromou's avatar
Evan Prodromou committed
86
            $this->invite = Invitation::staticGet('code', $this->code);
87
            if (empty($this->invite)) {
88
            // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
89
                $this->clientError(_('Sorry, invalid invitation code.'));
90 91 92 93 94 95 96 97 98 99
                return false;
            }
            // Store this in case we need it
            common_ensure_session();
            $_SESSION['invitecode'] = $this->code;
        }

        return true;
    }

100 101 102 103 104 105 106 107
    /**
     * Title of the page
     *
     * @return string title
     */
    function title()
    {
        if ($this->registered) {
108
            // TRANS: Title for registration page after a succesful registration.
109 110
            return _('Registration successful');
        } else {
111 112
            // TRANS: Title for registration page.
            return _m('TITLE','Register');
113 114 115 116 117 118 119 120 121 122 123 124 125 126
        }
    }

    /**
     * Handle input, produce output
     *
     * Switches on request method; either shows the form or handles its input.
     *
     * Checks if registration is closed and shows an error if so.
     *
     * @param array $args $_REQUEST data
     *
     * @return void
     */
127 128
    function handle($args)
    {
129 130 131
        parent::handle($args);

        if (common_config('site', 'closed')) {
132
            // TRANS: Client error displayed when trying to register to a closed site.
133
            $this->clientError(_('Registration not allowed.'));
134
        } else if (common_logged_in()) {
135
            // TRANS: Client error displayed when trying to register while already logged in.
136
            $this->clientError(_('Already logged in.'));
137
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
138
            $this->tryRegister();
139
        } else {
140
            $this->showForm();
141 142 143
        }
    }

144 145 146 147 148 149
    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('nickname');
    }

150 151 152 153 154 155 156 157 158
    /**
     * Try to register a user
     *
     * Validates the input and tries to save a new user and profile
     * record. On success, shows an instructions page.
     *
     * @return void
     */
    function tryRegister()
159
    {
160 161 162 163
        if (Event::handle('StartRegistrationTry', array($this))) {
            $token = $this->trimmed('token');
            if (!$token || $token != common_session_token()) {
                $this->showForm(_('There was a problem with your session token. '.
164
                                  'Try again, please.'));
165 166
                return;
            }
167

168 169 170 171 172 173
            $nickname = $this->trimmed('nickname');
            $email    = $this->trimmed('email');
            $fullname = $this->trimmed('fullname');
            $homepage = $this->trimmed('homepage');
            $bio      = $this->trimmed('bio');
            $location = $this->trimmed('location');
174

175
            // We don't trim these... whitespace is OK in a password!
176 177
            $password = $this->arg('password');
            $confirm  = $this->arg('confirm');
178

179 180
            // invitation code, if any
            $code = $this->trimmed('code');
Evan Prodromou's avatar
Evan Prodromou committed
181

182 183 184
            if ($code) {
                $invite = Invitation::staticGet($code);
            }
185

186
            if (common_config('site', 'inviteonly') && !($code && $invite)) {
187
                // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
188
                $this->clientError(_('Sorry, only invited people can register.'));
189 190
                return;
            }
191 192

            // Input scrubbing
193 194 195 196 197
            try {
                $nickname = Nickname::normalize($nickname);
            } catch (NicknameException $e) {
                $this->showForm($e->getMessage());
            }
198 199 200
            $email    = common_canonical_email($email);

            if (!$this->boolean('license')) {
201 202
                // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
                $this->showForm(_('You cannot register if you do not '.
203
                                  'agree to the license.'));
204
            } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
205
                // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
206 207
                $this->showForm(_('Not a valid email address.'));
            } else if ($this->nicknameExists($nickname)) {
208
                // TRANS: Form validation error displayed when trying to register with an existing nickname.
209 210
                $this->showForm(_('Nickname already in use. Try another one.'));
            } else if (!User::allowed_nickname($nickname)) {
211
                // TRANS: Form validation error displayed when trying to register with an invalid nickname.
212 213 214 215
                $this->showForm(_('Not a valid nickname.'));
            } else if ($this->emailExists($email)) {
                $this->showForm(_('Email address already exists.'));
            } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
216 217 218
                       !Validate::uri($homepage,
                                      array('allowed_schemes' =>
                                            array('http', 'https')))) {
219
                // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
220
                $this->showForm(_('Homepage is not a valid URL.'));
221
                return;
222
            } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
223
                // TRANS: Form validation error displayed when trying to register with a too long full name.
224
                $this->showForm(_('Full name is too long (maximum 255 characters).'));
225
                return;
226
            } else if (Profile::bioTooLong($bio)) {
227 228
                // TRANS: Form validation error on registration page when providing too long a bio text.
                // TRANS: %d is the maximum number of characters for bio; used for plural.
229 230 231
                $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
                                           'Bio is too long (maximum %d characters).',
                                           Profile::maxBio()),
232
                                        Profile::maxBio()));
233 234
                return;
            } else if (!is_null($location) && mb_strlen($location) > 255) {
235
                // TRANS: Form validation error displayed when trying to register with a too long location.
236
                $this->showForm(_('Location is too long (maximum 255 characters).'));
237 238
                return;
            } else if (strlen($password) < 6) {
239
                // TRANS: Form validation error displayed when trying to register with too short a password.
240 241 242
                $this->showForm(_('Password must be 6 or more characters.'));
                return;
            } else if ($password != $confirm) {
243 244
                // TRANS: Form validation error displayed when trying to register with non-matching passwords.
                $this->showForm(_('Passwords do not match.'));
245
            } else if ($user = User::register(array('nickname' => $nickname,
246 247 248 249 250 251 252
                                                    'password' => $password,
                                                    'email' => $email,
                                                    'fullname' => $fullname,
                                                    'homepage' => $homepage,
                                                    'bio' => $bio,
                                                    'location' => $location,
                                                    'code' => $code))) {
253
                if (!$user) {
254
                    // TRANS: Form validation error displayed when trying to register with an invalid username or password.
255 256 257 258 259
                    $this->showForm(_('Invalid username or password.'));
                    return;
                }
                // success!
                if (!common_set_user($user)) {
260
                    // TRANS: Server error displayed when saving fails during user registration.
261 262 263 264 265 266 267 268 269 270 271 272 273 274
                    $this->serverError(_('Error setting user.'));
                    return;
                }
                // this is a real login
                common_real_login(true);
                if ($this->boolean('rememberme')) {
                    common_debug('Adding rememberme cookie for ' . $nickname);
                    common_rememberme($user);
                }

                Event::handle('EndRegistrationTry', array($this));

                // Re-init language env in case it changed (not yet, but soon)
                common_init_language();
275

276 277
                $this->showSuccess();
            } else {
278
                // TRANS: Form validation error displayed when trying to register with an invalid username or password.
279
                $this->showForm(_('Invalid username or password.'));
280 281 282 283
            }
        }
    }

284 285 286 287 288 289 290 291 292 293
    /**
     * Does the given nickname already exist?
     *
     * Checks a canonical nickname against the database.
     *
     * @param string $nickname nickname to check
     *
     * @return boolean true if the nickname already exists
     */
    function nicknameExists($nickname)
294
    {
295
        $user = User::staticGet('nickname', $nickname);
296
        return is_object($user);
297 298
    }

299 300 301 302 303 304 305 306 307 308
    /**
     * Does the given email address already exist?
     *
     * Checks a canonical email address against the database.
     *
     * @param string $email email address to check
     *
     * @return boolean true if the address already exists
     */
    function emailExists($email)
309
    {
310 311 312 313 314
        $email = common_canonical_email($email);
        if (!$email || strlen($email) == 0) {
            return false;
        }
        $user = User::staticGet('email', $email);
315
        return is_object($user);
316 317
    }

318 319
    // overrrided to add entry-title class
    function showPageTitle() {
320 321 322
        if (Event::handle('StartShowPageTitle', array($this))) {
            $this->element('h1', array('class' => 'entry-title'), $this->title());
        }
323
    }
324

325 326
    // overrided to add hentry, and content-inner class
    function showContentBlock()
327 328 329 330 331 332 333 334 335 336 337
    {
        $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
        $this->showPageTitle();
        $this->showPageNoticeBlock();
        $this->elementStart('div', array('id' => 'content_inner',
                                         'class' => 'entry-content'));
        // show the actual content (forms, lists, whatever)
        $this->showContent();
        $this->elementEnd('div');
        $this->elementEnd('div');
    }
338

339 340 341 342 343 344 345 346
    /**
     * Instructions or a notice for the page
     *
     * Shows the error, if any, or instructions for registration.
     *
     * @return void
     */
    function showPageNotice()
347
    {
348 349 350 351
        if ($this->registered) {
            return;
        } else if ($this->error) {
            $this->element('p', 'error', $this->error);
352
        } else {
353
            $instr =
354
              // TRANS: Page notice on registration page.
355
              common_markup_to_html(_('With this form you can create '.
356
                                      'a new account. ' .
357
                                      'You can then post notices and '.
358
                                      'link up to friends and colleagues.'));
359

360 361 362
            $this->elementStart('div', 'instructions');
            $this->raw($instr);
            $this->elementEnd('div');
363 364 365
        }
    }

366 367 368 369 370 371 372 373 374 375
    /**
     * Wrapper for showing a page
     *
     * Stores an error and shows the page
     *
     * @param string $error Error, if any
     *
     * @return void
     */
    function showForm($error=null)
376
    {
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        $this->error = $error;
        $this->showPage();
    }

    /**
     * Show the page content
     *
     * Either shows the registration form or, if registration was successful,
     * instructions for using the site.
     *
     * @return void
     */
    function showContent()
    {
        if ($this->registered) {
            $this->showSuccessContent();
        } else {
            $this->showFormContent();
        }
    }

    /**
     * Show the registration form
     *
     * @return void
     */
    function showFormContent()
    {
405 406
        $code = $this->trimmed('code');

Evan Prodromou's avatar
Evan Prodromou committed
407 408
        $invite = null;

409 410 411 412 413
        if ($code) {
            $invite = Invitation::staticGet($code);
        }

        if (common_config('site', 'inviteonly') && !($code && $invite)) {
414
            // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
415
            $this->clientError(_('Sorry, only invited people can register.'));
416 417 418
            return;
        }

419
        $this->elementStart('form', array('method' => 'post',
420 421 422
                                          'id' => 'form_register',
                                          'class' => 'form_settings',
                                          'action' => common_local_url('register')));
423
        $this->elementStart('fieldset');
424
        // TRANS: Fieldset legend on accout registration page.
425
        $this->element('legend', null, 'Account settings');
426
        $this->hidden('token', common_session_token());
427

428 429
        if ($this->code) {
            $this->hidden('code', $this->code);
430 431
        }

sarven's avatar
sarven committed
432
        $this->elementStart('ul', 'form_data');
433 434
        if (Event::handle('StartRegistrationFormData', array($this))) {
            $this->elementStart('li');
435
            // TRANS: Field label on account registration page.
436
            $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
437
                         // TRANS: Field title on account registration page.
438
                         _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
439 440
            $this->elementEnd('li');
            $this->elementStart('li');
441
            // TRANS: Field label on account registration page.
442
            $this->password('password', _('Password'),
443
                            // TRANS: Field title on account registration page.
444
                            _('6 or more characters.'));
445 446
            $this->elementEnd('li');
            $this->elementStart('li');
447 448 449 450
            // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
            $this->password('confirm', _m('PASSWORD','Confirm'),
                         // TRANS: Field title on account registration page.
                         _('Same as password above.'));
451 452
            $this->elementEnd('li');
            $this->elementStart('li');
453
            if ($this->invite && $this->invite->address_type == 'email') {
454 455 456
                // TRANS: Field label on account registration page.
                $this->input('email', _m('LABEL','Email'), $this->invite->address,
                             // TRANS: Field title on account registration page.
457
                             _('Used only for updates, announcements, '.
458
                               'and password recovery.'));
459
            } else {
460 461 462
                // TRANS: Field label on account registration page.
                $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
                             // TRANS: Field title on account registration page.
463
                             _('Used only for updates, announcements, '.
464
                               'and password recovery.'));
465 466 467
            }
            $this->elementEnd('li');
            $this->elementStart('li');
468
            // TRANS: Field label on account registration page.
469
            $this->input('fullname', _('Full name'),
470
                         $this->trimmed('fullname'),
471 472
                     // TRANS: Field title on account registration page.
                     _('Longer name, preferably your "real" name.'));
473 474
            $this->elementEnd('li');
            $this->elementStart('li');
475
            // TRANS: Field label on account registration page.
476
            $this->input('homepage', _('Homepage'),
477
                         $this->trimmed('homepage'),
478
                         // TRANS: Field title on account registration page.
479
                         _('URL of your homepage, blog, '.
480
                           'or profile on another site.'));
481 482
            $this->elementEnd('li');
            $this->elementStart('li');
483 484
            $maxBio = Profile::maxBio();
            if ($maxBio > 0) {
485
                // TRANS: Text area title in form for account registration. Plural
486 487
                // TRANS: is decided by the number of characters available for the
                // TRANS: biography (%d).
488 489
                $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
                                       'Describe yourself and your interests in %d characters.',
490
                                       $maxBio),
491 492
                                    $maxBio);
            } else {
493 494
                // TRANS: Text area title on account registration page.
                $bioInstr = _('Describe yourself and your interests.');
495
            }
496
            // TRANS: Text area label on account registration page.
497
            $this->textarea('bio', _('Bio'),
498
                            $this->trimmed('bio'),
499
                            $bioInstr);
500 501
            $this->elementEnd('li');
            $this->elementStart('li');
502
            // TRANS: Field label on account registration page.
503
            $this->input('location', _('Location'),
504
                         $this->trimmed('location'),
505
                         // TRANS: Field title on account registration page.
506
                         _('Where you are, like "City, '.
507
                           'State (or Region), Country".'));
508 509 510
            $this->elementEnd('li');
            Event::handle('EndRegistrationFormData', array($this));
            $this->elementStart('li', array('id' => 'settings_rememberme'));
511
            // TRANS: Checkbox label on account registration page.
512
            $this->checkbox('rememberme', _('Remember me'),
513
                            $this->boolean('rememberme'),
514
                            // TRANS: Checkbox title on account registration page.
515 516
                            _('Automatically login in the future; '.
                              'not for shared computers!'));
517 518
            $this->elementEnd('li');
            $attrs = array('type' => 'checkbox',
519 520 521 522
                           'id' => 'license',
                           'class' => 'checkbox',
                           'name' => 'license',
                           'value' => 'true');
523 524 525 526 527 528
            if ($this->boolean('license')) {
                $attrs['checked'] = 'checked';
            }
            $this->elementStart('li');
            $this->element('input', $attrs);
            $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
529 530 531 532 533
            $this->raw($this->licenseCheckbox());
            $this->elementEnd('label');
            $this->elementEnd('li');
        }
        $this->elementEnd('ul');
534
        // TRANS: Botton text to register a user on account registration page.
535
        $this->submit('submit', _m('BUTTON','Register'));
536 537 538 539 540 541 542 543 544 545
        $this->elementEnd('fieldset');
        $this->elementEnd('form');
    }

    function licenseCheckbox()
    {
        $out = '';
        switch (common_config('license', 'type')) {
        case 'private':
            $out .= htmlspecialchars(sprintf(
546 547
                // TRANS: Copyright checkbox label in registration dialog, for private sites.
                // TRANS: %1$s is the StatusNet sitename.
548 549 550 551 552 553 554 555 556
                _('I understand that content and data of %1$s are private and confidential.'),
                common_config('site', 'name')));
            // fall through
        case 'allrightsreserved':
            if ($out != '') {
                $out .= ' ';
            }
            if (common_config('license', 'owner')) {
                $out .= htmlspecialchars(sprintf(
557 558
                    // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
                    // TRANS: %1$s is the license owner.
559 560 561 562 563 564 565 566 567 568 569 570
                    _('My text and files are copyright by %1$s.'),
                    common_config('license', 'owner')));
            } else {
                // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
                $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
            }
            // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
            $out .= ' ' . _('All rights reserved.');
            break;
        case 'cc': // fall through
        default:
            // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
571 572 573 574 575 576 577 578
            $message = _('My text and files are available under %s ' .
                         'except this private data: password, ' .
                         'email address, IM address, and phone number.');
            $link = '<a href="' .
                    htmlspecialchars(common_config('license', 'url')) .
                    '">' .
                    htmlspecialchars(common_config('license', 'title')) .
                    '</a>';
579
            $out .= sprintf(htmlspecialchars($message), $link);
580
        }
581
        return $out;
582 583
    }

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    /**
     * Show some information about registering for the site
     *
     * Save the registration flag, run showPage
     *
     * @return void
     */
    function showSuccess()
    {
        $this->registered = true;
        $this->showPage();
    }

    /**
     * Show some information about registering for the site
     *
     * Gives some information and options for new registrees.
     *
     * @return void
     */
    function showSuccessContent()
605
    {
606
        $nickname = $this->arg('nickname');
607 608 609 610

        $profileurl = common_local_url('showstream',
                                       array('nickname' => $nickname));

611
        $this->elementStart('div', 'success');
612 613 614 615
        // TRANS: Text displayed after successful account registration.
        // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
        // TRANS: This message contains Markdown links in the form [link text](link)
        // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
616
        $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
617
                           'From here, you may want to...'. "\n\n" .
618
                           '* Go to [your profile](%2$s) '.
619 620 621 622 623
                           'and post your first message.' .  "\n" .
                           '* Add a [Jabber/GTalk address]'.
                           '(%%%%action.imsettings%%%%) '.
                           'so you can send notices '.
                           'through instant messages.' . "\n" .
624
                           '* [Search for people](%%%%action.peoplesearch%%%%) '.
625 626 627 628 629 630 631 632 633 634 635
                           'that you may know or '.
                           'that share your interests. ' . "\n" .
                           '* Update your [profile settings]'.
                           '(%%%%action.profilesettings%%%%)'.
                           ' to tell others more about you. ' . "\n" .
                           '* Read over the [online docs](%%%%doc.help%%%%)'.
                           ' for features you may have missed. ' . "\n\n" .
                           'Thanks for signing up and we hope '.
                           'you enjoy using this service.'),
                         $nickname, $profileurl);

636
        $this->raw(common_markup_to_html($instr));
637

638 639
        $have_email = $this->trimmed('email');
        if ($have_email) {
640
            // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
641 642 643 644
            $emailinstr = _('(You should receive a message by email '.
                            'momentarily, with ' .
                            'instructions on how to confirm '.
                            'your email address.)');
645
            $this->raw(common_markup_to_html($emailinstr));
646
        }
647
        $this->elementEnd('div');
648
    }
Evan Prodromou's avatar
Evan Prodromou committed
649

650 651 652 653 654 655 656 657 658 659
    /**
     * Show the login group nav menu
     *
     * @return void
     */
    function showLocalNav()
    {
        $nav = new LoginGroupNav($this);
        $nav->show();
    }
660 661 662 663

    function showNoticeForm()
    {
    }
664 665 666 667

    function showProfileBlock()
    {
    }
Evan Prodromou's avatar
Evan Prodromou committed
668
}