]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Register a new user account
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
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/
28  */
29
30 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
31
32 /**
33  * An action for registering a new user account
34  *
35  * @category Login
36  * @package  StatusNet
37  * @author   Evan Prodromou <evan@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39  * @link     http://status.net/
40  */
41 class RegisterAction extends Action
42 {
43     /**
44      * Has there been an error?
45      */
46     var $error = null;
47
48     /**
49      * Have we registered?
50      */
51     var $registered = false;
52
53     /**
54      * Are we processing an invite?
55      */
56     var $invite = null;
57
58     /**
59      * Prepare page to run
60      *
61      *
62      * @param $args
63      * @return string title
64      */
65     protected function prepare(array $args=array())
66     {
67         parent::prepare($args);
68         $this->code = $this->trimmed('code');
69
70         if (empty($this->code)) {
71             common_ensure_session();
72             if (array_key_exists('invitecode', $_SESSION)) {
73                 $this->code = $_SESSION['invitecode'];
74             }
75         }
76
77         if (common_config('site', 'inviteonly') && empty($this->code)) {
78             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
79             $this->clientError(_('Sorry, only invited people can register.'));
80         }
81
82         if (!empty($this->code)) {
83             $this->invite = Invitation::getKV('code', $this->code);
84             if (!$this->invite instanceof Invitation) {
85             // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
86                 $this->clientError(_('Sorry, invalid invitation code.'));
87             }
88             // Store this in case we need it
89             common_ensure_session();
90             $_SESSION['invitecode'] = $this->code;
91         }
92
93         return true;
94     }
95
96     /**
97      * Title of the page
98      *
99      * @return string title
100      */
101     function title()
102     {
103         if ($this->registered) {
104             // TRANS: Title for registration page after a succesful registration.
105             return _('Registration successful');
106         } else {
107             // TRANS: Title for registration page.
108             return _m('TITLE','Register');
109         }
110     }
111
112     /**
113      * Handle input, produce output
114      *
115      * Switches on request method; either shows the form or handles its input.
116      *
117      * Checks if registration is closed and shows an error if so.
118      *
119      * @param array $args $_REQUEST data
120      *
121      * @return void
122      */
123     function handle($args)
124     {
125         parent::handle($args);
126
127         if (common_config('site', 'closed')) {
128             // TRANS: Client error displayed when trying to register to a closed site.
129             $this->clientError(_('Registration not allowed.'));
130         } else if (common_logged_in()) {
131             // TRANS: Client error displayed when trying to register while already logged in.
132             $this->clientError(_('Already logged in.'));
133         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
134             $this->tryRegister();
135         } else {
136             $this->showForm();
137         }
138     }
139
140     function showScripts()
141     {
142         parent::showScripts();
143         $this->autofocus('nickname');
144     }
145
146     /**
147      * Try to register a user
148      *
149      * Validates the input and tries to save a new user and profile
150      * record. On success, shows an instructions page.
151      *
152      * @return void
153      */
154     function tryRegister()
155     {
156         if (Event::handle('StartRegistrationTry', array($this))) {
157             $token = $this->trimmed('token');
158             if (!$token || $token != common_session_token()) {
159                 // TRANS: Client error displayed when the session token does not match or is not given.
160                 $this->showForm(_('There was a problem with your session token. '.
161                                   'Try again, please.'));
162                 return;
163             }
164
165             $nickname = $this->trimmed('nickname');
166             $email    = $this->trimmed('email');
167             $fullname = $this->trimmed('fullname');
168             $homepage = $this->trimmed('homepage');
169             $bio      = $this->trimmed('bio');
170             $location = $this->trimmed('location');
171
172             // We don't trim these... whitespace is OK in a password!
173             $password = $this->arg('password');
174             $confirm  = $this->arg('confirm');
175
176             // invitation code, if any
177             $code = $this->trimmed('code');
178
179             if ($code) {
180                 $invite = Invitation::getKV($code);
181             }
182
183             if (common_config('site', 'inviteonly') && !($code && $invite)) {
184                 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
185                 $this->clientError(_('Sorry, only invited people can register.'));
186             }
187
188             // Input scrubbing
189             try {
190                 $nickname = Nickname::normalize($nickname, true);
191             } catch (NicknameException $e) {
192                 $this->showForm($e->getMessage());
193                 return;
194             }
195             $email    = common_canonical_email($email);
196
197             if (!$this->boolean('license')) {
198                 // TRANS: Form validation error displayed when trying to register without agreeing to the site license.
199                 $this->showForm(_('You cannot register if you do not '.
200                                   'agree to the license.'));
201             } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
202                 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
203                 $this->showForm(_('Not a valid email address.'));
204             } else if ($this->emailExists($email)) {
205                 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
206                 $this->showForm(_('Email address already exists.'));
207             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
208                        !common_valid_http_url($homepage)) {
209                 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
210                 $this->showForm(_('Homepage is not a valid URL.'));
211             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
212                 // TRANS: Form validation error displayed when trying to register with a too long full name.
213                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
214             } else if (Profile::bioTooLong($bio)) {
215                 // TRANS: Form validation error on registration page when providing too long a bio text.
216                 // TRANS: %d is the maximum number of characters for bio; used for plural.
217                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
218                                            'Bio is too long (maximum %d characters).',
219                                            Profile::maxBio()),
220                                         Profile::maxBio()));
221             } else if (!is_null($location) && mb_strlen($location) > 255) {
222                 // TRANS: Form validation error displayed when trying to register with a too long location.
223                 $this->showForm(_('Location is too long (maximum 255 characters).'));
224             } else if (strlen($password) < 6) {
225                 // TRANS: Form validation error displayed when trying to register with too short a password.
226                 $this->showForm(_('Password must be 6 or more characters.'));
227             } else if ($password != $confirm) {
228                 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
229                 $this->showForm(_('Passwords do not match.'));
230             } else {
231                 try {
232                     $user = User::register(array('nickname' => $nickname,
233                                                     'password' => $password,
234                                                     'email' => $email,
235                                                     'fullname' => $fullname,
236                                                     'homepage' => $homepage,
237                                                     'bio' => $bio,
238                                                     'location' => $location,
239                                                     'code' => $code));
240                     // success!
241                     if (!common_set_user($user)) {
242                         // TRANS: Server error displayed when saving fails during user registration.
243                         $this->serverError(_('Error setting user.'));
244                     }
245                     // this is a real login
246                     common_real_login(true);
247                     if ($this->boolean('rememberme')) {
248                         common_debug('Adding rememberme cookie for ' . $nickname);
249                         common_rememberme($user);
250                     }
251
252                     // Re-init language env in case it changed (not yet, but soon)
253                     common_init_language();
254
255                     Event::handle('EndRegistrationTry', array($this));
256
257                     $this->showSuccess();
258                 } catch (Exception $e) {
259                     // TRANS: Form validation error displayed when trying to register with an invalid username or password.
260                     $this->showForm($e->getMessage());
261                 }
262             }
263         }
264     }
265
266     /**
267      * Does the given email address already exist?
268      *
269      * Checks a canonical email address against the database.
270      *
271      * @param string $email email address to check
272      *
273      * @return boolean true if the address already exists
274      */
275     function emailExists($email)
276     {
277         $email = common_canonical_email($email);
278         if (!$email || strlen($email) == 0) {
279             return false;
280         }
281         $user = User::getKV('email', $email);
282         return is_object($user);
283     }
284
285     // overrrided to add entry-title class
286     function showPageTitle() {
287         if (Event::handle('StartShowPageTitle', array($this))) {
288             $this->element('h1', array('class' => 'entry-title'), $this->title());
289         }
290     }
291
292     // overrided to add h-entry, and content-inner class
293     function showContentBlock()
294     {
295         $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
296         $this->showPageTitle();
297         $this->showPageNoticeBlock();
298         $this->elementStart('div', array('id' => 'content_inner',
299                                          'class' => 'e-content'));
300         // show the actual content (forms, lists, whatever)
301         $this->showContent();
302         $this->elementEnd('div');
303         $this->elementEnd('div');
304     }
305
306     /**
307      * Instructions or a notice for the page
308      *
309      * Shows the error, if any, or instructions for registration.
310      *
311      * @return void
312      */
313     function showPageNotice()
314     {
315         if ($this->registered) {
316             return;
317         } else if ($this->error) {
318             $this->element('p', 'error', $this->error);
319         } else {
320             $instr =
321               // TRANS: Page notice on registration page.
322               common_markup_to_html(_('With this form you can create '.
323                                       'a new account. ' .
324                                       'You can then post notices and '.
325                                       'link up to friends and colleagues.'));
326
327             $this->elementStart('div', 'instructions');
328             $this->raw($instr);
329             $this->elementEnd('div');
330         }
331     }
332
333     /**
334      * Wrapper for showing a page
335      *
336      * Stores an error and shows the page
337      *
338      * @param string $error Error, if any
339      *
340      * @return void
341      */
342     function showForm($error=null)
343     {
344         $this->error = $error;
345         $this->showPage();
346     }
347
348     /**
349      * Show the page content
350      *
351      * Either shows the registration form or, if registration was successful,
352      * instructions for using the site.
353      *
354      * @return void
355      */
356     function showContent()
357     {
358         if ($this->registered) {
359             $this->showSuccessContent();
360         } else {
361             $this->showFormContent();
362         }
363     }
364
365     /**
366      * Show the registration form
367      *
368      * @return void
369      */
370     function showFormContent()
371     {
372         $code = $this->trimmed('code');
373
374         $invite = null;
375
376         if ($code) {
377             $invite = Invitation::getKV($code);
378         }
379
380         if (common_config('site', 'inviteonly') && !($code && $invite)) {
381             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
382             $this->clientError(_('Sorry, only invited people can register.'));
383         }
384
385         $this->elementStart('form', array('method' => 'post',
386                                           'id' => 'form_register',
387                                           'class' => 'form_settings',
388                                           'action' => common_local_url('register')));
389         $this->elementStart('fieldset');
390         // TRANS: Fieldset legend on accout registration page.
391         $this->element('legend', null, 'Account settings');
392         $this->hidden('token', common_session_token());
393
394         if ($this->code) {
395             $this->hidden('code', $this->code);
396         }
397
398         $this->elementStart('ul', 'form_data');
399         if (Event::handle('StartRegistrationFormData', array($this))) {
400             $this->elementStart('li');
401             // TRANS: Field label on account registration page.
402             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
403                          // TRANS: Field title on account registration page.
404                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
405             $this->elementEnd('li');
406             $this->elementStart('li');
407             // TRANS: Field label on account registration page.
408             $this->password('password', _('Password'),
409                             // TRANS: Field title on account registration page.
410                             _('6 or more characters.'));
411             $this->elementEnd('li');
412             $this->elementStart('li');
413             // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
414             $this->password('confirm', _m('PASSWORD','Confirm'),
415                          // TRANS: Field title on account registration page.
416                          _('Same as password above.'));
417             $this->elementEnd('li');
418             $this->elementStart('li');
419             if ($this->invite && $this->invite->address_type == 'email') {
420                 // TRANS: Field label on account registration page.
421                 $this->input('email', _m('LABEL','Email'), $this->invite->address,
422                              // TRANS: Field title on account registration page.
423                              _('Used only for updates, announcements, '.
424                                'and password recovery.'));
425             } else {
426                 // TRANS: Field label on account registration page.
427                 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
428                              // TRANS: Field title on account registration page.
429                              _('Used only for updates, announcements, '.
430                                'and password recovery.'));
431             }
432             $this->elementEnd('li');
433             $this->elementStart('li');
434             // TRANS: Field label on account registration page.
435             $this->input('fullname', _('Full name'),
436                          $this->trimmed('fullname'),
437                      // TRANS: Field title on account registration page.
438                      _('Longer name, preferably your "real" name.'));
439             $this->elementEnd('li');
440             $this->elementStart('li');
441             // TRANS: Field label on account registration page.
442             $this->input('homepage', _('Homepage'),
443                          $this->trimmed('homepage'),
444                          // TRANS: Field title on account registration page.
445                          _('URL of your homepage, blog, '.
446                            'or profile on another site.'));
447             $this->elementEnd('li');
448             $this->elementStart('li');
449             $maxBio = Profile::maxBio();
450             if ($maxBio > 0) {
451                 // TRANS: Text area title in form for account registration. Plural
452                 // TRANS: is decided by the number of characters available for the
453                 // TRANS: biography (%d).
454                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
455                                        'Describe yourself and your interests in %d characters.',
456                                        $maxBio),
457                                     $maxBio);
458             } else {
459                 // TRANS: Text area title on account registration page.
460                 $bioInstr = _('Describe yourself and your interests.');
461             }
462             // TRANS: Text area label on account registration page.
463             $this->textarea('bio', _('Bio'),
464                             $this->trimmed('bio'),
465                             $bioInstr);
466             $this->elementEnd('li');
467             $this->elementStart('li');
468             // TRANS: Field label on account registration page.
469             $this->input('location', _('Location'),
470                          $this->trimmed('location'),
471                          // TRANS: Field title on account registration page.
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             // TRANS: Checkbox label on account registration page.
478             $this->checkbox('rememberme', _('Remember me'),
479                             $this->boolean('rememberme'),
480                             // TRANS: Checkbox title on account registration page.
481                             _('Automatically login in the future; '.
482                               'not for shared computers!'));
483             $this->elementEnd('li');
484             $attrs = array('type' => 'checkbox',
485                            'id' => 'license',
486                            'class' => 'checkbox',
487                            'name' => 'license',
488                            'value' => 'true');
489             if ($this->boolean('license')) {
490                 $attrs['checked'] = 'checked';
491             }
492             $this->elementStart('li');
493             $this->element('input', $attrs);
494             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
495             $this->raw($this->licenseCheckbox());
496             $this->elementEnd('label');
497             $this->elementEnd('li');
498         }
499         $this->elementEnd('ul');
500         // TRANS: Button text to register a user on account registration page.
501         $this->submit('submit', _m('BUTTON','Register'));
502         $this->elementEnd('fieldset');
503         $this->elementEnd('form');
504     }
505
506     function licenseCheckbox()
507     {
508         $out = '';
509         switch (common_config('license', 'type')) {
510         case 'private':
511             $out .= htmlspecialchars(sprintf(
512                 // TRANS: Copyright checkbox label in registration dialog, for private sites.
513                 // TRANS: %1$s is the StatusNet sitename.
514                 _('I understand that content and data of %1$s are private and confidential.'),
515                 common_config('site', 'name')));
516             // fall through
517         case 'allrightsreserved':
518             if ($out != '') {
519                 $out .= ' ';
520             }
521             if (common_config('license', 'owner')) {
522                 $out .= htmlspecialchars(sprintf(
523                     // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
524                     // TRANS: %1$s is the license owner.
525                     _('My text and files are copyright by %1$s.'),
526                     common_config('license', 'owner')));
527             } else {
528                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
529                 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
530             }
531             // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
532             $out .= ' ' . _('All rights reserved.');
533             break;
534         case 'cc': // fall through
535         default:
536             // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
537             $message = _('My text and files are available under %s ' .
538                          'except this private data: password, ' .
539                          'email address, IM address, and phone number.');
540             $link = '<a href="' .
541                     htmlspecialchars(common_config('license', 'url')) .
542                     '">' .
543                     htmlspecialchars(common_config('license', 'title')) .
544                     '</a>';
545             $out .= sprintf(htmlspecialchars($message), $link);
546         }
547         return $out;
548     }
549
550     /**
551      * Show some information about registering for the site
552      *
553      * Save the registration flag, run showPage
554      *
555      * @return void
556      */
557     function showSuccess()
558     {
559         $this->registered = true;
560         $this->showPage();
561     }
562
563     /**
564      * Show some information about registering for the site
565      *
566      * Gives some information and options for new registrees.
567      *
568      * @return void
569      */
570     function showSuccessContent()
571     {
572         if (Event::handle('StartRegisterSuccess', array($this))) {
573             $nickname = $this->arg('nickname');
574
575             $profileurl = common_local_url('showstream',
576                                            array('nickname' => $nickname));
577
578             $this->elementStart('div', 'success');
579             // TRANS: Text displayed after successful account registration.
580             // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
581             // TRANS: This message contains Markdown links in the form [link text](link)
582             // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
583             $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
584                                'From here, you may want to...'. "\n\n" .
585                                '* Go to [your profile](%2$s) '.
586                                'and post your first message.' .  "\n" .
587                                '* Add a [Jabber/GTalk address]'.
588                                '(%%%%action.imsettings%%%%) '.
589                                'so you can send notices '.
590                                'through instant messages.' . "\n" .
591                                '* [Search for people](%%%%action.peoplesearch%%%%) '.
592                                'that you may know or '.
593                                'that share your interests. ' . "\n" .
594                                '* Update your [profile settings]'.
595                                '(%%%%action.profilesettings%%%%)'.
596                                ' to tell others more about you. ' . "\n" .
597                                '* Read over the [online docs](%%%%doc.help%%%%)'.
598                                ' for features you may have missed. ' . "\n\n" .
599                                'Thanks for signing up and we hope '.
600                                'you enjoy using this service.'),
601                              $nickname, $profileurl);
602
603             $this->raw(common_markup_to_html($instr));
604
605             $have_email = $this->trimmed('email');
606             if ($have_email) {
607                 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
608                 $emailinstr = _('(You should receive a message by email '.
609                                 'momentarily, with ' .
610                                 'instructions on how to confirm '.
611                                 'your email address.)');
612                 $this->raw(common_markup_to_html($emailinstr));
613             }
614             $this->elementEnd('div');
615
616             Event::handle('EndRegisterSuccess', array($this));
617         }
618     }
619
620     /**
621      * Show the login group nav menu
622      *
623      * @return void
624      */
625     function showLocalNav()
626     {
627         if (common_logged_in()) {
628             parent::showLocalNav();
629         } else {
630             $nav = new LoginGroupNav($this);
631             $nav->show();
632         }
633     }
634
635     /**
636      * Show a bit of login context
637      *
638      * @return nothing
639      */
640     function showProfileBlock()
641     {
642         if (common_logged_in()) {
643             parent::showProfileBlock();
644         }
645     }
646 }