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