]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
New domain regexp for WebFinger matching.
[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             try {
135                 $this->tryRegister();
136             } catch (ClientException $e) {
137                 $this->showForm($e->getMessage());
138             }
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::getKV($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             }
191
192             // Input scrubbing
193             try {
194                 $nickname = Nickname::normalize($nickname, true);
195             } catch (NicknameException $e) {
196                 $this->showForm($e->getMessage());
197                 return;
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->emailExists($email)) {
209                 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
210                 $this->showForm(_('Email address already exists.'));
211             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
212                        !common_valid_http_url($homepage)) {
213                 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
214                 $this->showForm(_('Homepage is not a valid URL.'));
215             } else if (Profile::bioTooLong($bio)) {
216                 // TRANS: Form validation error on registration page when providing too long a bio text.
217                 // TRANS: %d is the maximum number of characters for bio; used for plural.
218                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
219                                            'Bio is too long (maximum %d characters).',
220                                            Profile::maxBio()),
221                                         Profile::maxBio()));
222             } else if (strlen($password) < 6) {
223                 // TRANS: Form validation error displayed when trying to register with too short a password.
224                 $this->showForm(_('Password must be 6 or more characters.'));
225             } else if ($password != $confirm) {
226                 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
227                 $this->showForm(_('Passwords do not match.'));
228             } else {
229                 try {
230                     $user = User::register(array('nickname' => $nickname,
231                                                     'password' => $password,
232                                                     'email' => $email,
233                                                     'fullname' => $fullname,
234                                                     'homepage' => $homepage,
235                                                     'bio' => $bio,
236                                                     'location' => $location,
237                                                     'code' => $code));
238                     // success!
239                     if (!common_set_user($user)) {
240                         // TRANS: Server error displayed when saving fails during user registration.
241                         $this->serverError(_('Error setting user.'));
242                     }
243                     // this is a real login
244                     common_real_login(true);
245                     if ($this->boolean('rememberme')) {
246                         common_debug('Adding rememberme cookie for ' . $nickname);
247                         common_rememberme($user);
248                     }
249
250                     // Re-init language env in case it changed (not yet, but soon)
251                     common_init_language();
252
253                     Event::handle('EndRegistrationTry', array($this));
254
255                     $this->showSuccess();
256                 } catch (Exception $e) {
257                     // TRANS: Form validation error displayed when trying to register with an invalid username or password.
258                     $this->showForm($e->getMessage());
259                 }
260             }
261         }
262     }
263
264     /**
265      * Does the given email address already exist?
266      *
267      * Checks a canonical email address against the database.
268      *
269      * @param string $email email address to check
270      *
271      * @return boolean true if the address already exists
272      */
273     function emailExists($email)
274     {
275         $email = common_canonical_email($email);
276         if (!$email || strlen($email) == 0) {
277             return false;
278         }
279         $user = User::getKV('email', $email);
280         return is_object($user);
281     }
282
283     // overrrided to add entry-title class
284     function showPageTitle() {
285         if (Event::handle('StartShowPageTitle', array($this))) {
286             $this->element('h1', array('class' => 'entry-title'), $this->title());
287         }
288     }
289
290     // overrided to add h-entry, and content-inner class
291     function showContentBlock()
292     {
293         $this->elementStart('div', array('id' => 'content', 'class' => 'h-entry'));
294         $this->showPageTitle();
295         $this->showPageNoticeBlock();
296         $this->elementStart('div', array('id' => 'content_inner',
297                                          'class' => 'e-content'));
298         // show the actual content (forms, lists, whatever)
299         $this->showContent();
300         $this->elementEnd('div');
301         $this->elementEnd('div');
302     }
303
304     /**
305      * Instructions or a notice for the page
306      *
307      * Shows the error, if any, or instructions for registration.
308      *
309      * @return void
310      */
311     function showPageNotice()
312     {
313         if ($this->registered) {
314             return;
315         } else if ($this->error) {
316             $this->element('p', 'error', $this->error);
317         } else {
318             $instr =
319               // TRANS: Page notice on registration page.
320               common_markup_to_html(_('With this form you can create '.
321                                       'a new account. ' .
322                                       'You can then post notices and '.
323                                       'link up to friends and colleagues.'));
324
325             $this->elementStart('div', 'instructions');
326             $this->raw($instr);
327             $this->elementEnd('div');
328         }
329     }
330
331     /**
332      * Wrapper for showing a page
333      *
334      * Stores an error and shows the page
335      *
336      * @param string $error Error, if any
337      *
338      * @return void
339      */
340     function showForm($error=null)
341     {
342         $this->error = $error;
343         $this->showPage();
344     }
345
346     /**
347      * Show the page content
348      *
349      * Either shows the registration form or, if registration was successful,
350      * instructions for using the site.
351      *
352      * @return void
353      */
354     function showContent()
355     {
356         if ($this->registered) {
357             $this->showSuccessContent();
358         } else {
359             $this->showFormContent();
360         }
361     }
362
363     /**
364      * Show the registration form
365      *
366      * @return void
367      */
368     function showFormContent()
369     {
370         $code = $this->trimmed('code');
371
372         $invite = null;
373
374         if ($code) {
375             $invite = Invitation::getKV($code);
376         }
377
378         if (common_config('site', 'inviteonly') && !($code && $invite)) {
379             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
380             $this->clientError(_('Sorry, only invited people can register.'));
381         }
382
383         $this->elementStart('form', array('method' => 'post',
384                                           'id' => 'form_register',
385                                           'class' => 'form_settings',
386                                           'action' => common_local_url('register')));
387         $this->elementStart('fieldset');
388         // TRANS: Fieldset legend on accout registration page.
389         $this->element('legend', null, 'Account settings');
390         $this->hidden('token', common_session_token());
391
392         if ($this->code) {
393             $this->hidden('code', $this->code);
394         }
395
396         $this->elementStart('ul', 'form_data');
397         if (Event::handle('StartRegistrationFormData', array($this))) {
398             $this->elementStart('li');
399             // TRANS: Field label on account registration page.
400             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
401                          // TRANS: Field title on account registration page.
402                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
403             $this->elementEnd('li');
404             $this->elementStart('li');
405             // TRANS: Field label on account registration page.
406             $this->password('password', _('Password'),
407                             // TRANS: Field title on account registration page.
408                             _('6 or more characters.'));
409             $this->elementEnd('li');
410             $this->elementStart('li');
411             // TRANS: Field label on account registration page. In this field the password has to be entered a second time.
412             $this->password('confirm', _m('PASSWORD','Confirm'),
413                          // TRANS: Field title on account registration page.
414                          _('Same as password above.'));
415             $this->elementEnd('li');
416             $this->elementStart('li');
417             if ($this->invite && $this->invite->address_type == 'email') {
418                 // TRANS: Field label on account registration page.
419                 $this->input('email', _m('LABEL','Email'), $this->invite->address,
420                              // TRANS: Field title on account registration page.
421                              _('Used only for updates, announcements, '.
422                                'and password recovery.'));
423             } else {
424                 // TRANS: Field label on account registration page.
425                 $this->input('email', _m('LABEL','Email'), $this->trimmed('email'),
426                              // TRANS: Field title on account registration page.
427                              _('Used only for updates, announcements, '.
428                                'and password recovery.'));
429             }
430             $this->elementEnd('li');
431             $this->elementStart('li');
432             // TRANS: Field label on account registration page.
433             $this->input('fullname', _('Full name'),
434                          $this->trimmed('fullname'),
435                      // TRANS: Field title on account registration page.
436                      _('Longer name, preferably your "real" name.'));
437             $this->elementEnd('li');
438             $this->elementStart('li');
439             // TRANS: Field label on account registration page.
440             $this->input('homepage', _('Homepage'),
441                          $this->trimmed('homepage'),
442                          // TRANS: Field title on account registration page.
443                          _('URL of your homepage, blog, '.
444                            'or profile on another site.'));
445             $this->elementEnd('li');
446             $this->elementStart('li');
447             $maxBio = Profile::maxBio();
448             if ($maxBio > 0) {
449                 // TRANS: Text area title in form for account registration. Plural
450                 // TRANS: is decided by the number of characters available for the
451                 // TRANS: biography (%d).
452                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
453                                        'Describe yourself and your interests in %d characters.',
454                                        $maxBio),
455                                     $maxBio);
456             } else {
457                 // TRANS: Text area title on account registration page.
458                 $bioInstr = _('Describe yourself and your interests.');
459             }
460             // TRANS: Text area label on account registration page.
461             $this->textarea('bio', _('Bio'),
462                             $this->trimmed('bio'),
463                             $bioInstr);
464             $this->elementEnd('li');
465             $this->elementStart('li');
466             // TRANS: Field label on account registration page.
467             $this->input('location', _('Location'),
468                          $this->trimmed('location'),
469                          // TRANS: Field title on account registration page.
470                          _('Where you are, like "City, '.
471                            'State (or Region), Country".'));
472             $this->elementEnd('li');
473             Event::handle('EndRegistrationFormData', array($this));
474             $this->elementStart('li', array('id' => 'settings_rememberme'));
475             // TRANS: Checkbox label on account registration page.
476             $this->checkbox('rememberme', _('Remember me'),
477                             $this->boolean('rememberme'),
478                             // TRANS: Checkbox title on account registration page.
479                             _('Automatically login in the future; '.
480                               'not for shared computers!'));
481             $this->elementEnd('li');
482             $attrs = array('type' => 'checkbox',
483                            'id' => 'license',
484                            'class' => 'checkbox',
485                            'name' => 'license',
486                            'required' => 'true',
487                            'value' => 'true');
488             if ($this->boolean('license')) {
489                 $attrs['checked'] = 'checked';
490             }
491             $this->elementStart('li');
492             $this->element('input', $attrs);
493             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
494             $this->raw($this->licenseCheckbox());
495             $this->elementEnd('label');
496             $this->elementEnd('li');
497         }
498         $this->elementEnd('ul');
499         // TRANS: Button text to register a user on account registration page.
500         $this->submit('submit', _m('BUTTON','Register'));
501         $this->elementEnd('fieldset');
502         $this->elementEnd('form');
503     }
504
505     function licenseCheckbox()
506     {
507         $out = '';
508         switch (common_config('license', 'type')) {
509         case 'private':
510             $out .= htmlspecialchars(sprintf(
511                 // TRANS: Copyright checkbox label in registration dialog, for private sites.
512                 // TRANS: %1$s is the StatusNet sitename.
513                 _('I understand that content and data of %1$s are private and confidential.'),
514                 common_config('site', 'name')));
515             // fall through
516         case 'allrightsreserved':
517             if ($out != '') {
518                 $out .= ' ';
519             }
520             if (common_config('license', 'owner')) {
521                 $out .= htmlspecialchars(sprintf(
522                     // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
523                     // TRANS: %1$s is the license owner.
524                     _('My text and files are copyright by %1$s.'),
525                     common_config('license', 'owner')));
526             } else {
527                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
528                 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
529             }
530             // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
531             $out .= ' ' . _('All rights reserved.');
532             break;
533         case 'cc': // fall through
534         default:
535             // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
536             $message = _('My text and files are available under %s ' .
537                          'except this private data: password, ' .
538                          'email address, IM address, and phone number.');
539             $link = '<a href="' .
540                     htmlspecialchars(common_config('license', 'url')) .
541                     '">' .
542                     htmlspecialchars(common_config('license', 'title')) .
543                     '</a>';
544             $out .= sprintf(htmlspecialchars($message), $link);
545         }
546         return $out;
547     }
548
549     /**
550      * Show some information about registering for the site
551      *
552      * Save the registration flag, run showPage
553      *
554      * @return void
555      */
556     function showSuccess()
557     {
558         $this->registered = true;
559         $this->showPage();
560     }
561
562     /**
563      * Show some information about registering for the site
564      *
565      * Gives some information and options for new registrees.
566      *
567      * @return void
568      */
569     function showSuccessContent()
570     {
571         if (Event::handle('StartRegisterSuccess', array($this))) {
572             $nickname = $this->arg('nickname');
573
574             $profileurl = common_local_url('showstream',
575                                            array('nickname' => $nickname));
576
577             $this->elementStart('div', 'success');
578             // TRANS: Text displayed after successful account registration.
579             // TRANS: %1$s is the registered nickname, %2$s is the profile URL.
580             // TRANS: This message contains Markdown links in the form [link text](link)
581             // TRANS: and variables in the form %%%%variable%%%%. Please mind the syntax.
582             $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
583                                'From here, you may want to...'. "\n\n" .
584                                '* Go to [your profile](%2$s) '.
585                                'and post your first message.' .  "\n" .
586                                '* Add a [Jabber/GTalk address]'.
587                                '(%%%%action.imsettings%%%%) '.
588                                'so you can send notices '.
589                                'through instant messages.' . "\n" .
590                                '* [Search for people](%%%%action.peoplesearch%%%%) '.
591                                'that you may know or '.
592                                'that share your interests. ' . "\n" .
593                                '* Update your [profile settings]'.
594                                '(%%%%action.profilesettings%%%%)'.
595                                ' to tell others more about you. ' . "\n" .
596                                '* Read over the [online docs](%%%%doc.help%%%%)'.
597                                ' for features you may have missed. ' . "\n\n" .
598                                'Thanks for signing up and we hope '.
599                                'you enjoy using this service.'),
600                              $nickname, $profileurl);
601
602             $this->raw(common_markup_to_html($instr));
603
604             $have_email = $this->trimmed('email');
605             if ($have_email) {
606                 // TRANS: Instruction text on how to deal with the e-mail address confirmation e-mail.
607                 $emailinstr = _('(You should receive a message by email '.
608                                 'momentarily, with ' .
609                                 'instructions on how to confirm '.
610                                 'your email address.)');
611                 $this->raw(common_markup_to_html($emailinstr));
612             }
613             $this->elementEnd('div');
614
615             Event::handle('EndRegisterSuccess', array($this));
616         }
617     }
618
619     /**
620      * Show the login group nav menu
621      *
622      * @return void
623      */
624     function showLocalNav()
625     {
626         if (common_logged_in()) {
627             parent::showLocalNav();
628         } else {
629             $nav = new LoginGroupNav($this);
630             $nav->show();
631         }
632     }
633
634     /**
635      * Show a bit of login context
636      *
637      * @return nothing
638      */
639     function showProfileBlock()
640     {
641         if (common_logged_in()) {
642             parent::showProfileBlock();
643         }
644     }
645 }