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