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