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