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