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