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