]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
generate an etag for shownotice
[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
40                 $token = $this->trimmed('token');
41                 if (!$token || $token != common_session_token()) {
42                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
43                         return;
44                 }
45
46                 $nickname = $this->trimmed('nickname');
47                 $email = $this->trimmed('email');
48                 $fullname = $this->trimmed('fullname');
49                 $homepage = $this->trimmed('homepage');
50                 $bio = $this->trimmed('bio');
51                 $location = $this->trimmed('location');
52
53                 # We don't trim these... whitespace is OK in a password!
54
55                 $password = $this->arg('password');
56                 $confirm = $this->arg('confirm');
57
58                 # invitation code, if any
59
60                 $code = $this->trimmed('code');
61
62                 if ($code) {
63                         $invite = Invitation::staticGet($code);
64                 }
65
66                 if (common_config('site', 'inviteonly') && !($code && $invite)) {
67                         $this->client_error(_('Sorry, only invited people can register.'));
68                         return;
69                 }
70
71                 # Input scrubbing
72
73                 $nickname = common_canonical_nickname($nickname);
74                 $email = common_canonical_email($email);
75
76                 if (!$this->boolean('license')) {
77                         $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
78                 } else if ($email && !Validate::email($email, true)) {
79                         $this->show_form(_('Not a valid email address.'));
80                 } else if (!Validate::string($nickname, array('min_length' => 1,
81                                                                                                           'max_length' => 64,
82                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
83                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
84                 } else if ($this->nickname_exists($nickname)) {
85                         $this->show_form(_('Nickname already in use. Try another one.'));
86                 } else if (!User::allowed_nickname($nickname)) {
87                         $this->show_form(_('Not a valid nickname.'));
88                 } else if ($this->email_exists($email)) {
89                         $this->show_form(_('Email address already exists.'));
90                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
91                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
92                         $this->show_form(_('Homepage is not a valid URL.'));
93                         return;
94                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
95                         $this->show_form(_('Full name is too long (max 255 chars).'));
96                         return;
97                 } else if (!is_null($bio) && strlen($bio) > 140) {
98                         $this->show_form(_('Bio is too long (max 140 chars).'));
99                         return;
100                 } else if (!is_null($location) && strlen($location) > 255) {
101                         $this->show_form(_('Location is too long (max 255 chars).'));
102                         return;
103                 } else if (strlen($password) < 6) {
104                         $this->show_form(_('Password must be 6 or more characters.'));
105                         return;
106                 } else if ($password != $confirm) {
107                         $this->show_form(_('Passwords don\'t match.'));
108                 } else if ($user = User::register(array('nickname' => $nickname, 'password' => $password, 'email' => $email,
109                                                                                                 'fullname' => $fullname, 'homepage' => $homepage, 'bio' => $bio,
110                                                                                                 'location' => $location, 'code' => $code))) {
111                         if (!$user) {
112                                 $this->show_form(_('Invalid username or password.'));
113                                 return;
114                         }
115                         # success!
116                         if (!common_set_user($user)) {
117                                 common_server_error(_('Error setting user.'));
118                                 return;
119                         }
120                         # this is a real login
121                         common_real_login(true);
122                         if ($this->boolean('rememberme')) {
123                                 common_debug('Adding rememberme cookie for ' . $nickname);
124                                 common_rememberme($user);
125                         }
126                         # Re-init language env in case it changed (not yet, but soon)
127                         common_init_language();
128                         $this->show_success();
129                 } else {
130                         $this->show_form(_('Invalid username or password.'));
131                 }
132         }
133
134         # checks if *CANONICAL* nickname exists
135
136         function nickname_exists($nickname) {
137                 $user = User::staticGet('nickname', $nickname);
138                 return ($user !== false);
139         }
140
141         # checks if *CANONICAL* email exists
142
143         function email_exists($email) {
144                 $email = common_canonical_email($email);
145                 if (!$email || strlen($email) == 0) {
146                         return false;
147                 }
148                 $user = User::staticGet('email', $email);
149                 return ($user !== false);
150         }
151
152         function show_top($error=NULL) {
153                 if ($error) {
154                         common_element('p', 'error', $error);
155                 } else {
156                         $instr = common_markup_to_html(_('With this form you can create a new account. ' .
157                                                                                          'You can then post notices and link up to friends and colleagues. '.
158                                                                                          '(Have an [OpenID](http://openid.net/)? ' .
159                                                                                          'Try our [OpenID registration](%%action.openidlogin%%)!)'));
160
161                         common_element_start('div', 'instructions');
162                         common_raw($instr);
163                         common_element_end('div');
164                 }
165         }
166
167         function show_form($error=NULL) {
168                 global $config;
169
170                 $code = $this->trimmed('code');
171
172                 if ($code) {
173                         $invite = Invitation::staticGet($code);
174                 }
175
176                 if (common_config('site', 'inviteonly') && !($code && $invite)) {
177                         $this->client_error(_('Sorry, only invited people can register.'));
178                         return;
179                 }
180
181                 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
182                 common_element_start('form', array('method' => 'post',
183                                                                                    'id' => 'login',
184                                                                                    'action' => common_local_url('register')));
185
186                 common_hidden('token', common_session_token());
187
188                 if ($code) {
189                         common_hidden('code', $code);
190                 }
191
192                 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
193                                          _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
194                 common_password('password', _('Password'),
195                                                 _('6 or more characters. Required.'));
196                 common_password('confirm', _('Confirm'),
197                                                 _('Same as password above. Required.'));
198                 if ($invite && $invite->address_type == 'email') {
199                         common_input('email', _('Email'), $invite->address,
200                                          _('Used only for updates, announcements, and password recovery'));
201                 } else {
202                         common_input('email', _('Email'), $this->trimmed('email'),
203                                                  _('Used only for updates, announcements, and password recovery'));
204                 }
205                 common_input('fullname', _('Full name'),
206                                          $this->trimmed('fullname'),
207                                           _('Longer name, preferably your "real" name'));
208                 common_input('homepage', _('Homepage'),
209                                          $this->trimmed('homepage'),
210                                          _('URL of your homepage, blog, or profile on another site'));
211                 common_textarea('bio', _('Bio'),
212                                                 $this->trimmed('bio'),
213                                                  _('Describe yourself and your interests in 140 chars'));
214                 common_input('location', _('Location'),
215                                          $this->trimmed('location'),
216                                          _('Where you are, like "City, State (or Region), Country"'));
217                 common_checkbox('rememberme', _('Remember me'),
218                                                 $this->boolean('rememberme'),
219                                 _('Automatically login in the future; not for shared computers!'));
220                 common_element_start('p');
221                 $attrs = array('type' => 'checkbox',
222                                            'id' => 'license',
223                                            'name' => 'license',
224                                            'value' => 'true');
225                 if ($this->boolean('license')) {
226                         $attrs['checked'] = 'checked';
227                 }
228                 common_element('input', $attrs);
229             common_text(_('My text and files are available under '));
230                 common_element('a', array('href' => $config['license']['url']),
231                                            $config['license']['title']);
232                 common_text(_(' except this private data: password, email address, IM address, phone number.'));
233                 common_element_end('p');
234                 common_submit('submit', _('Register'));
235                 common_element_end('form');
236                 common_show_footer();
237         }
238
239         function show_success() {
240                 $nickname = $this->arg('nickname');
241                 common_show_header(_('Registration successful'));
242                 common_element_start('div', 'success');
243                 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. From here, you may want to...'. "\n\n" .
244                                                    '* Go to [your profile](%s) and post your first message.' .  "\n" .
245                                                    '* Add a [Jabber/GTalk address](%%%%action.imsettings%%%%) so you can send notices through instant messages.' . "\n" .
246                                                    '* [Search for people](%%%%action.peoplesearch%%%%) that you may know or that share your interests. ' . "\n" .
247                                                    '* Update your [profile settings](%%%%action.profilesettings%%%%) to tell others more about you. ' . "\n" .
248                                                    '* Read over the [online docs](%%%%doc.help%%%%) for features you may have missed. ' . "\n\n" .
249                                                    'Thanks for signing up and we hope you enjoy using this service.'),
250                                                  $nickname, common_local_url('showstream', array('nickname' => $nickname)));
251                 common_raw(common_markup_to_html($instr));
252                 $have_email = $this->trimmed('email');
253                 if ($have_email) {
254                         $emailinstr = _('(You should receive a message by email momentarily, with ' .
255                                                         'instructions on how to confirm your email address.)');
256                         common_raw(common_markup_to_html($emailinstr));
257                 }
258                 common_element_end('div');
259                 common_show_footer();
260         }
261
262 }