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