]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Register a new user account
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Login
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * An action for registering a new user account
36  *
37  * @category Login
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
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         if (empty($this->code)) {
78             common_ensure_session();
79             if (array_key_exists('invitecode', $_SESSION)) {
80                 $this->code = $_SESSION['invitecode'];
81             }
82         }
83
84         if (common_config('site', 'inviteonly') && empty($this->code)) {
85             $this->clientError(_('Sorry, only invited people can register.'));
86             return false;
87         }
88
89         if (!empty($this->code)) {
90             $this->invite = Invitation::staticGet('code', $this->code);
91             if (empty($this->invite)) {
92                 $this->clientError(_('Sorry, invalid invitation code.'));
93                 return false;
94             }
95             // Store this in case we need it
96             common_ensure_session();
97             $_SESSION['invitecode'] = $this->code;
98         }
99
100         return true;
101     }
102
103     /**
104      * Title of the page
105      *
106      * @return string title
107      */
108
109     function title()
110     {
111         if ($this->registered) {
112             return _('Registration successful');
113         } else {
114             return _('Register');
115         }
116     }
117
118     /**
119      * Handle input, produce output
120      *
121      * Switches on request method; either shows the form or handles its input.
122      *
123      * Checks if registration is closed and shows an error if so.
124      *
125      * @param array $args $_REQUEST data
126      *
127      * @return void
128      */
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
160     function tryRegister()
161     {
162         if (Event::handle('StartRegistrationTry', array($this))) {
163             $token = $this->trimmed('token');
164             if (!$token || $token != common_session_token()) {
165                 $this->showForm(_('There was a problem with your session token. '.
166                                   'Try again, please.'));
167                 return;
168             }
169
170             $nickname = $this->trimmed('nickname');
171             $email    = $this->trimmed('email');
172             $fullname = $this->trimmed('fullname');
173             $homepage = $this->trimmed('homepage');
174             $bio      = $this->trimmed('bio');
175             $location = $this->trimmed('location');
176
177             // We don't trim these... whitespace is OK in a password!
178             $password = $this->arg('password');
179             $confirm  = $this->arg('confirm');
180
181             // invitation code, if any
182             $code = $this->trimmed('code');
183
184             if ($code) {
185                 $invite = Invitation::staticGet($code);
186             }
187
188             if (common_config('site', 'inviteonly') && !($code && $invite)) {
189                 $this->clientError(_('Sorry, only invited people can register.'));
190                 return;
191             }
192
193             // Input scrubbing
194             $nickname = common_canonical_nickname($nickname);
195             $email    = common_canonical_email($email);
196
197             if (!$this->boolean('license')) {
198                 $this->showForm(_('You can\'t register if you don\'t '.
199                                   'agree to the license.'));
200             } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
201                 $this->showForm(_('Not a valid email address.'));
202             } else if (!Validate::string($nickname, array('min_length' => 1,
203                                                           'max_length' => 64,
204                                                           'format' => NICKNAME_FMT))) {
205                 $this->showForm(_('Nickname must have only lowercase letters '.
206                                   'and numbers and no spaces.'));
207             } else if ($this->nicknameExists($nickname)) {
208                 $this->showForm(_('Nickname already in use. Try another one.'));
209             } else if (!User::allowed_nickname($nickname)) {
210                 $this->showForm(_('Not a valid nickname.'));
211             } else if ($this->emailExists($email)) {
212                 $this->showForm(_('Email address already exists.'));
213             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
214                        !Validate::uri($homepage,
215                                       array('allowed_schemes' =>
216                                             array('http', 'https')))) {
217                 $this->showForm(_('Homepage is not a valid URL.'));
218                 return;
219             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
220                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
221                 return;
222             } else if (Profile::bioTooLong($bio)) {
223                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
224                                            'Bio is too long (maximum %d characters).',
225                                            Profile::maxBio()),
226                                         Profile::maxBio()));
227                 return;
228             } else if (!is_null($location) && mb_strlen($location) > 255) {
229                 $this->showForm(_('Location is too long (maximum 255 characters).'));
230                 return;
231             } else if (strlen($password) < 6) {
232                 $this->showForm(_('Password must be 6 or more characters.'));
233                 return;
234             } else if ($password != $confirm) {
235                 $this->showForm(_('Passwords don\'t match.'));
236             } else if ($user = User::register(array('nickname' => $nickname,
237                                                     'password' => $password,
238                                                     'email' => $email,
239                                                     'fullname' => $fullname,
240                                                     'homepage' => $homepage,
241                                                     'bio' => $bio,
242                                                     'location' => $location,
243                                                     'code' => $code))) {
244                 if (!$user) {
245                     $this->showForm(_('Invalid username or password.'));
246                     return;
247                 }
248                 // success!
249                 if (!common_set_user($user)) {
250                     $this->serverError(_('Error setting user.'));
251                     return;
252                 }
253                 // this is a real login
254                 common_real_login(true);
255                 if ($this->boolean('rememberme')) {
256                     common_debug('Adding rememberme cookie for ' . $nickname);
257                     common_rememberme($user);
258                 }
259
260                 Event::handle('EndRegistrationTry', array($this));
261
262                 // Re-init language env in case it changed (not yet, but soon)
263                 common_init_language();
264
265                 $this->showSuccess();
266             } else {
267                 $this->showForm(_('Invalid username or password.'));
268             }
269         }
270     }
271
272     /**
273      * Does the given nickname already exist?
274      *
275      * Checks a canonical nickname against the database.
276      *
277      * @param string $nickname nickname to check
278      *
279      * @return boolean true if the nickname already exists
280      */
281
282     function nicknameExists($nickname)
283     {
284         $user = User::staticGet('nickname', $nickname);
285         return is_object($user);
286     }
287
288     /**
289      * Does the given email address already exist?
290      *
291      * Checks a canonical email address against the database.
292      *
293      * @param string $email email address to check
294      *
295      * @return boolean true if the address already exists
296      */
297
298     function emailExists($email)
299     {
300         $email = common_canonical_email($email);
301         if (!$email || strlen($email) == 0) {
302             return false;
303         }
304         $user = User::staticGet('email', $email);
305         return is_object($user);
306     }
307
308     // overrrided to add entry-title class
309     function showPageTitle() {
310         if (Event::handle('StartShowPageTitle', array($this))) {
311             $this->element('h1', array('class' => 'entry-title'), $this->title());
312         }
313     }
314
315     // overrided to add hentry, and content-inner class
316     function showContentBlock()
317     {
318         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
319         $this->showPageTitle();
320         $this->showPageNoticeBlock();
321         $this->elementStart('div', array('id' => 'content_inner',
322                                          'class' => 'entry-content'));
323         // show the actual content (forms, lists, whatever)
324         $this->showContent();
325         $this->elementEnd('div');
326         $this->elementEnd('div');
327     }
328
329     /**
330      * Instructions or a notice for the page
331      *
332      * Shows the error, if any, or instructions for registration.
333      *
334      * @return void
335      */
336
337     function showPageNotice()
338     {
339         if ($this->registered) {
340             return;
341         } else if ($this->error) {
342             $this->element('p', 'error', $this->error);
343         } else {
344             $instr =
345               common_markup_to_html(_('With this form you can create '.
346                                       'a new account. ' .
347                                       'You can then post notices and '.
348                                       'link up to friends and colleagues. '));
349
350             $this->elementStart('div', 'instructions');
351             $this->raw($instr);
352             $this->elementEnd('div');
353         }
354     }
355
356     /**
357      * Wrapper for showing a page
358      *
359      * Stores an error and shows the page
360      *
361      * @param string $error Error, if any
362      *
363      * @return void
364      */
365
366     function showForm($error=null)
367     {
368         $this->error = $error;
369         $this->showPage();
370     }
371
372     /**
373      * Show the page content
374      *
375      * Either shows the registration form or, if registration was successful,
376      * instructions for using the site.
377      *
378      * @return void
379      */
380
381     function showContent()
382     {
383         if ($this->registered) {
384             $this->showSuccessContent();
385         } else {
386             $this->showFormContent();
387         }
388     }
389
390     /**
391      * Show the registration form
392      *
393      * @return void
394      */
395
396     function showFormContent()
397     {
398         $code = $this->trimmed('code');
399
400         $invite = null;
401
402         if ($code) {
403             $invite = Invitation::staticGet($code);
404         }
405
406         if (common_config('site', 'inviteonly') && !($code && $invite)) {
407             $this->clientError(_('Sorry, only invited people can register.'));
408             return;
409         }
410
411         $this->elementStart('form', array('method' => 'post',
412                                           'id' => 'form_register',
413                                           'class' => 'form_settings',
414                                           'action' => common_local_url('register')));
415         $this->elementStart('fieldset');
416         $this->element('legend', null, 'Account settings');
417         $this->hidden('token', common_session_token());
418
419         if ($this->code) {
420             $this->hidden('code', $this->code);
421         }
422
423         $this->elementStart('ul', 'form_data');
424         if (Event::handle('StartRegistrationFormData', array($this))) {
425             $this->elementStart('li');
426             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
427                          _('1-64 lowercase letters or numbers, '.
428                            'no punctuation or spaces. Required.'));
429             $this->elementEnd('li');
430             $this->elementStart('li');
431             $this->password('password', _('Password'),
432                             _('6 or more characters. Required.'));
433             $this->elementEnd('li');
434             $this->elementStart('li');
435             $this->password('confirm', _('Confirm'),
436                             _('Same as password above. Required.'));
437             $this->elementEnd('li');
438             $this->elementStart('li');
439             if ($this->invite && $this->invite->address_type == 'email') {
440                 $this->input('email', _('Email'), $this->invite->address,
441                              _('Used only for updates, announcements, '.
442                                'and password recovery'));
443             } else {
444                 $this->input('email', _('Email'), $this->trimmed('email'),
445                              _('Used only for updates, announcements, '.
446                                'and password recovery'));
447             }
448             $this->elementEnd('li');
449             $this->elementStart('li');
450             $this->input('fullname', _('Full name'),
451                          $this->trimmed('fullname'),
452                          _('Longer name, preferably your "real" name'));
453             $this->elementEnd('li');
454             $this->elementStart('li');
455             $this->input('homepage', _('Homepage'),
456                          $this->trimmed('homepage'),
457                          _('URL of your homepage, blog, '.
458                            'or profile on another site'));
459             $this->elementEnd('li');
460             $this->elementStart('li');
461             $maxBio = Profile::maxBio();
462             if ($maxBio > 0) {
463                 // TRANS: Tooltip for field label in form for profile settings. Plural
464                 // TRANS: is decided by the number of characters available for the
465                 // TRANS: biography (%d).
466                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
467                                        'Describe yourself and your interests in %d characters',
468                                        $maxBio),
469                                     $maxBio);
470             } else {
471                 $bioInstr = _('Describe yourself and your interests');
472             }
473             $this->textarea('bio', _('Bio'),
474                             $this->trimmed('bio'),
475                             $bioInstr);
476             $this->elementEnd('li');
477             $this->elementStart('li');
478             $this->input('location', _('Location'),
479                          $this->trimmed('location'),
480                          _('Where you are, like "City, '.
481                            'State (or Region), Country"'));
482             $this->elementEnd('li');
483             Event::handle('EndRegistrationFormData', array($this));
484             $this->elementStart('li', array('id' => 'settings_rememberme'));
485             $this->checkbox('rememberme', _('Remember me'),
486                             $this->boolean('rememberme'),
487                             _('Automatically login in the future; '.
488                               'not for shared computers!'));
489             $this->elementEnd('li');
490             $attrs = array('type' => 'checkbox',
491                            'id' => 'license',
492                            'class' => 'checkbox',
493                            'name' => 'license',
494                            'value' => 'true');
495             if ($this->boolean('license')) {
496                 $attrs['checked'] = 'checked';
497             }
498             $this->elementStart('li');
499             $this->element('input', $attrs);
500             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
501             $this->raw($this->licenseCheckbox());
502             $this->elementEnd('label');
503             $this->elementEnd('li');
504         }
505         $this->elementEnd('ul');
506         $this->submit('submit', _('Register'));
507         $this->elementEnd('fieldset');
508         $this->elementEnd('form');
509     }
510
511     function licenseCheckbox()
512     {
513         $out = '';
514         switch (common_config('license', 'type')) {
515         case 'private':
516             // TRANS: Copyright checkbox label in registration dialog, for private sites.
517             $out .= htmlspecialchars(sprintf(
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                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
527                 $out .= htmlspecialchars(sprintf(
528                     _('My text and files are copyright by %1$s.'),
529                     common_config('license', 'owner')));
530             } else {
531                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
532                 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
533             }
534             // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
535             $out .= ' ' . _('All rights reserved.');
536             break;
537         case 'cc': // fall through
538         default:
539             // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
540             $message = _('My text and files are available under %s ' .
541                          'except this private data: password, ' .
542                          'email address, IM address, and phone number.');
543             $link = '<a href="' .
544                     htmlspecialchars(common_config('license', 'url')) .
545                     '">' .
546                     htmlspecialchars(common_config('license', 'title')) .
547                     '</a>';
548             $out .= sprintf(htmlspecialchars($message), $link);
549         }
550         return $out;
551     }
552
553     /**
554      * Show some information about registering for the site
555      *
556      * Save the registration flag, run showPage
557      *
558      * @return void
559      */
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
575     function showSuccessContent()
576     {
577         $nickname = $this->arg('nickname');
578
579         $profileurl = common_local_url('showstream',
580                                        array('nickname' => $nickname));
581
582         $this->elementStart('div', 'success');
583         $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
584                            'From here, you may want to...'. "\n\n" .
585                            '* Go to [your profile](%2$s) '.
586                            'and post your first message.' .  "\n" .
587                            '* Add a [Jabber/GTalk address]'.
588                            '(%%%%action.imsettings%%%%) '.
589                            'so you can send notices '.
590                            'through instant messages.' . "\n" .
591                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
592                            'that you may know or '.
593                            'that share your interests. ' . "\n" .
594                            '* Update your [profile settings]'.
595                            '(%%%%action.profilesettings%%%%)'.
596                            ' to tell others more about you. ' . "\n" .
597                            '* Read over the [online docs](%%%%doc.help%%%%)'.
598                            ' for features you may have missed. ' . "\n\n" .
599                            'Thanks for signing up and we hope '.
600                            'you enjoy using this service.'),
601                          $nickname, $profileurl);
602
603         $this->raw(common_markup_to_html($instr));
604
605         $have_email = $this->trimmed('email');
606         if ($have_email) {
607             $emailinstr = _('(You should receive a message by email '.
608                             'momentarily, with ' .
609                             'instructions on how to confirm '.
610                             'your email address.)');
611             $this->raw(common_markup_to_html($emailinstr));
612         }
613         $this->elementEnd('div');
614     }
615
616     /**
617      * Show the login group nav menu
618      *
619      * @return void
620      */
621
622     function showLocalNav()
623     {
624         $nav = new LoginGroupNav($this);
625         $nav->show();
626     }
627 }
628