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