]> 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
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, '.
434                            'no punctuation or spaces. Required.'));
435             $this->elementEnd('li');
436             $this->elementStart('li');
437             $this->password('password', _('Password'),
438                             _('6 or more characters. Required.'));
439             $this->elementEnd('li');
440             $this->elementStart('li');
441             $this->password('confirm', _('Confirm'),
442                             _('Same as password above. Required.'));
443             $this->elementEnd('li');
444             $this->elementStart('li');
445             if ($this->invite && $this->invite->address_type == 'email') {
446                 $this->input('email', _('Email'), $this->invite->address,
447                              _('Used only for updates, announcements, '.
448                                'and password recovery'));
449             } else {
450                 $this->input('email', _('Email'), $this->trimmed('email'),
451                              _('Used only for updates, announcements, '.
452                                'and password recovery'));
453             }
454             $this->elementEnd('li');
455             $this->elementStart('li');
456             $this->input('fullname', _('Full name'),
457                          $this->trimmed('fullname'),
458                          _('Longer name, preferably your "real" name'));
459             $this->elementEnd('li');
460             $this->elementStart('li');
461             $this->input('homepage', _('Homepage'),
462                          $this->trimmed('homepage'),
463                          _('URL of your homepage, blog, '.
464                            'or profile on another site'));
465             $this->elementEnd('li');
466             $this->elementStart('li');
467             $maxBio = Profile::maxBio();
468             if ($maxBio > 0) {
469                 // TRANS: Tooltip for field label in form for profile settings. Plural
470                 // TRANS: is decided by the number of characters available for the
471                 // TRANS: biography (%d).
472                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
473                                        'Describe yourself and your interests in %d characters',
474                                        $maxBio),
475                                     $maxBio);
476             } else {
477                 $bioInstr = _('Describe yourself and your interests');
478             }
479             $this->textarea('bio', _('Bio'),
480                             $this->trimmed('bio'),
481                             $bioInstr);
482             $this->elementEnd('li');
483             $this->elementStart('li');
484             $this->input('location', _('Location'),
485                          $this->trimmed('location'),
486                          _('Where you are, like "City, '.
487                            'State (or Region), Country"'));
488             $this->elementEnd('li');
489             Event::handle('EndRegistrationFormData', array($this));
490             $this->elementStart('li', array('id' => 'settings_rememberme'));
491             $this->checkbox('rememberme', _('Remember me'),
492                             $this->boolean('rememberme'),
493                             _('Automatically login in the future; '.
494                               'not for shared computers!'));
495             $this->elementEnd('li');
496             $attrs = array('type' => 'checkbox',
497                            'id' => 'license',
498                            'class' => 'checkbox',
499                            'name' => 'license',
500                            'value' => 'true');
501             if ($this->boolean('license')) {
502                 $attrs['checked'] = 'checked';
503             }
504             $this->elementStart('li');
505             $this->element('input', $attrs);
506             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
507             $this->raw($this->licenseCheckbox());
508             $this->elementEnd('label');
509             $this->elementEnd('li');
510         }
511         $this->elementEnd('ul');
512         $this->submit('submit', _('Register'));
513         $this->elementEnd('fieldset');
514         $this->elementEnd('form');
515     }
516
517     function licenseCheckbox()
518     {
519         $out = '';
520         switch (common_config('license', 'type')) {
521         case 'private':
522             // TRANS: Copyright checkbox label in registration dialog, for private sites.
523             $out .= htmlspecialchars(sprintf(
524                 _('I understand that content and data of %1$s are private and confidential.'),
525                 common_config('site', 'name')));
526             // fall through
527         case 'allrightsreserved':
528             if ($out != '') {
529                 $out .= ' ';
530             }
531             if (common_config('license', 'owner')) {
532                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
533                 $out .= htmlspecialchars(sprintf(
534                     _('My text and files are copyright by %1$s.'),
535                     common_config('license', 'owner')));
536             } else {
537                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
538                 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
539             }
540             // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
541             $out .= ' ' . _('All rights reserved.');
542             break;
543         case 'cc': // fall through
544         default:
545             // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
546             $message = _('My text and files are available under %s ' .
547                          'except this private data: password, ' .
548                          'email address, IM address, and phone number.');
549             $link = '<a href="' .
550                     htmlspecialchars(common_config('license', 'url')) .
551                     '">' .
552                     htmlspecialchars(common_config('license', 'title')) .
553                     '</a>';
554             $out .= sprintf(htmlspecialchars($message), $link);
555         }
556         return $out;
557     }
558
559     /**
560      * Show some information about registering for the site
561      *
562      * Save the registration flag, run showPage
563      *
564      * @return void
565      */
566
567     function showSuccess()
568     {
569         $this->registered = true;
570         $this->showPage();
571     }
572
573     /**
574      * Show some information about registering for the site
575      *
576      * Gives some information and options for new registrees.
577      *
578      * @return void
579      */
580
581     function showSuccessContent()
582     {
583         $nickname = $this->arg('nickname');
584
585         $profileurl = common_local_url('showstream',
586                                        array('nickname' => $nickname));
587
588         $this->elementStart('div', 'success');
589         $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
590                            'From here, you may want to...'. "\n\n" .
591                            '* Go to [your profile](%2$s) '.
592                            'and post your first message.' .  "\n" .
593                            '* Add a [Jabber/GTalk address]'.
594                            '(%%%%action.imsettings%%%%) '.
595                            'so you can send notices '.
596                            'through instant messages.' . "\n" .
597                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
598                            'that you may know or '.
599                            'that share your interests. ' . "\n" .
600                            '* Update your [profile settings]'.
601                            '(%%%%action.profilesettings%%%%)'.
602                            ' to tell others more about you. ' . "\n" .
603                            '* Read over the [online docs](%%%%doc.help%%%%)'.
604                            ' for features you may have missed. ' . "\n\n" .
605                            'Thanks for signing up and we hope '.
606                            'you enjoy using this service.'),
607                          $nickname, $profileurl);
608
609         $this->raw(common_markup_to_html($instr));
610
611         $have_email = $this->trimmed('email');
612         if ($have_email) {
613             $emailinstr = _('(You should receive a message by email '.
614                             'momentarily, with ' .
615                             'instructions on how to confirm '.
616                             'your email address.)');
617             $this->raw(common_markup_to_html($emailinstr));
618         }
619         $this->elementEnd('div');
620     }
621
622     /**
623      * Show the login group nav menu
624      *
625      * @return void
626      */
627
628     function showLocalNav()
629     {
630         $nav = new LoginGroupNav($this);
631         $nav->show();
632     }
633 }
634