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