]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
CSRF protection in imsettings.php
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class RegisterAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26
27                 if (common_config('site', 'closed')) {
28                         common_user_error(_('Registration not allowed.'));
29                 } else if (common_logged_in()) {
30                         common_user_error(_('Already logged in.'));
31                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32                         $this->try_register();
33                 } else {
34                         $this->show_form();
35                 }
36         }
37
38         function try_register() {
39                 $nickname = $this->trimmed('nickname');
40                 $email = $this->trimmed('email');
41                 $fullname = $this->trimmed('fullname');
42                 $homepage = $this->trimmed('homepage');
43                 $bio = $this->trimmed('bio');
44                 $location = $this->trimmed('location');
45
46                 # We don't trim these... whitespace is OK in a password!
47
48                 $password = $this->arg('password');
49                 $confirm = $this->arg('confirm');
50
51                 # Input scrubbing
52
53                 $nickname = common_canonical_nickname($nickname);
54                 $email = common_canonical_email($email);
55
56                 if (!$this->boolean('license')) {
57                         $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
58                 } else if ($email && !Validate::email($email, true)) {
59                         $this->show_form(_('Not a valid email address.'));
60                 } else if (!Validate::string($nickname, array('min_length' => 1,
61                                                                                                           'max_length' => 64,
62                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
63                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
64                 } else if ($this->nickname_exists($nickname)) {
65                         $this->show_form(_('Nickname already in use. Try another one.'));
66                 } else if (!User::allowed_nickname($nickname)) {
67                         $this->show_form(_('Not a valid nickname.'));
68                 } else if ($this->email_exists($email)) {
69                         $this->show_form(_('Email address already exists.'));
70                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
71                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
72                         $this->show_form(_('Homepage is not a valid URL.'));
73                         return;
74                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
75                         $this->show_form(_('Full name is too long (max 255 chars).'));
76                         return;
77                 } else if (!is_null($bio) && strlen($bio) > 140) {
78                         $this->show_form(_('Bio is too long (max 140 chars).'));
79                         return;
80                 } else if (!is_null($location) && strlen($location) > 255) {
81                         $this->show_form(_('Location is too long (max 255 chars).'));
82                         return;
83                 } else if ($password != $confirm) {
84                         $this->show_form(_('Passwords don\'t match.'));
85                 } else if ($user = User::register(array('nickname' => $nickname, 'password' => $password, 'email' => $email,
86                                                                                                 'fullname' => $fullname, 'homepage' => $homepage, 'bio' => $bio, 
87                                                                                                 'location' => $location))) {
88                         if (!$user) {
89                                 $this->show_form(_('Invalid username or password.'));
90                                 return;
91                         }
92                         # success!
93                         if (!common_set_user($user)) {
94                                 common_server_error(_('Error setting user.'));
95                                 return;
96                         }
97                         # this is a real login
98                         common_real_login(true);
99                         if ($this->boolean('rememberme')) {
100                                 common_debug('Adding rememberme cookie for ' . $nickname);
101                                 common_rememberme($user);
102                         }
103                         # Re-init language env in case it changed (not yet, but soon)
104                         common_init_language();
105                         $this->show_success();
106                 } else {
107                         $this->show_form(_('Invalid username or password.'));
108                 }
109         }
110
111         # checks if *CANONICAL* nickname exists
112
113         function nickname_exists($nickname) {
114                 $user = User::staticGet('nickname', $nickname);
115                 return ($user !== false);
116         }
117
118         # checks if *CANONICAL* email exists
119
120         function email_exists($email) {
121                 $email = common_canonical_email($email);
122                 $user = User::staticGet('email', $email);
123                 return ($user !== false);
124         }
125
126         function show_top($error=NULL) {
127                 if ($error) {
128                         common_element('p', 'error', $error);
129                 } else {
130                         common_element('div', 'instructions',
131                                                    _('You can create a new account to start posting notices.'));
132                 }
133         }
134
135         function show_form($error=NULL) {
136                 global $config;
137
138                 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
139                 common_element_start('form', array('method' => 'post',
140                                                                                    'id' => 'login',
141                                                                                    'action' => common_local_url('register')));
142                 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
143                                          _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
144                 common_password('password', _('Password'),
145                                                 _('6 or more characters. Required.'));
146                 common_password('confirm', _('Confirm'),
147                                                 _('Same as password above. Required.'));
148                 common_input('email', _('Email'), $this->trimmed('email'),
149                                          _('Used only for updates, announcements, and password recovery'));
150                 common_input('fullname', _('Full name'),
151                                          $this->trimmed('fullname'),
152                                           _('Longer name, preferably your "real" name'));
153                 common_input('homepage', _('Homepage'),
154                                          $this->trimmed('homepage'),
155                                          _('URL of your homepage, blog, or profile on another site'));
156                 common_textarea('bio', _('Bio'),
157                                                 $this->trimmed('bio'),
158                                                  _('Describe yourself and your interests in 140 chars'));
159                 common_input('location', _('Location'),
160                                          $this->trimmed('location'),
161                                          _('Where you are, like "City, State (or Region), Country"'));
162                 common_checkbox('rememberme', _('Remember me'), 
163                                                 $this->boolean('rememberme'),
164                                 _('Automatically login in the future; not for shared computers!'));
165                 common_element_start('p');
166                 $attrs = array('type' => 'checkbox',
167                                            'id' => 'license',
168                                            'name' => 'license',
169                                            'value' => 'true');
170                 if ($this->boolean('license')) {
171                         $attrs['checked'] = 'checked';
172                 }
173                 common_element('input', $attrs);
174             common_text(_('My text and files are available under '));
175                 common_element('a', array(href => $config['license']['url']),
176                                            $config['license']['title']);
177                 common_text(_(' except this private data: password, email address, IM address, phone number.'));
178                 common_element_end('p');
179                 common_submit('submit', _('Register'));
180                 common_element_end('form');
181                 common_show_footer();
182         }
183                                                 
184         function show_success() {
185                 $nickname = $this->arg('nickname');
186                 common_show_header(_('Registration successful'));
187                 common_element_start('div', 'success');
188                 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. From here, you may want to...'. "\n\n" .
189                                                    '* Go to [your profile](%s) and post your first message.' .  "\n" .
190                                                    '* Add a [Jabber/GTalk address](%%%%action.imsettings%%%%) so you can send notices through instant messages.' . "\n" .
191                                                    '* [Search for people](%%%%action.peoplesearch%%%%) that you may know or that share your interests. ' . "\n" .
192                                                    '* Update your [profile settings](%%%%action.profilesettings%%%%) to tell others more about you. ' . "\n" .
193                                                    '* Read over the [online docs](%%%%doc.help%%%%) for features you may have missed. ' . "\n\n" .
194                                                    'Thanks for signing up and we hope you enjoy using this service.'),
195                                                  $nickname, common_local_url('showstream', array('nickname' => $nickname)));
196                 common_raw(common_markup_to_html($instr));
197                 $have_email = $this->trimmed('email');
198                 if ($have_email) {
199                         $emailinstr = _('(You should receive a message by email momentarily, with ' .
200                                                         'instructions on how to confirm your email address.)');
201                         common_raw(common_markup_to_html($emailinstr));
202                 }
203                 common_element_end('div');
204                 common_show_footer();
205         }
206                                                 
207 }