]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
beware of shadows
[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_logged_in()) {
28                         common_user_error(_('Already logged in.'));
29                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30                         $this->try_register();
31                 } else {
32                         $this->show_form();
33                 }
34         }
35
36         function try_register() {
37                 $nickname = $this->trimmed('nickname');
38                 $email = $this->trimmed('email');
39                 $fullname = $this->trimmed('fullname');
40                 $homepage = $this->trimmed('homepage');
41                 $bio = $this->trimmed('bio');
42                 $location = $this->trimmed('location');
43
44                 # We don't trim these... whitespace is OK in a password!
45
46                 $password = $this->arg('password');
47                 $confirm = $this->arg('confirm');
48
49                 # Input scrubbing
50
51                 $nickname = common_canonical_nickname($nickname);
52                 $email = common_canonical_email($email);
53
54                 if (!$this->boolean('license')) {
55                         $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
56                 } else if ($email && !Validate::email($email, true)) {
57                         $this->show_form(_('Not a valid email address.'));
58                 } else if (!Validate::string($nickname, array('min_length' => 1,
59                                                                                                           'max_length' => 64,
60                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
61                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
62                 } else if ($this->nickname_exists($nickname)) {
63                         $this->show_form(_('Nickname already in use. Try another one.'));
64                 } else if (!User::allowed_nickname($nickname)) {
65                         $this->show_form(_('Not a valid nickname.'));
66                 } else if ($this->email_exists($email)) {
67                         $this->show_form(_('Email address already exists.'));
68                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
69                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
70                         $this->show_form(_('Homepage is not a valid URL.'));
71                         return;
72                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
73                         $this->show_form(_('Full name is too long (max 255 chars).'));
74                         return;
75                 } else if (!is_null($bio) && strlen($bio) > 140) {
76                         $this->show_form(_('Bio is too long (max 140 chars).'));
77                         return;
78                 } else if (!is_null($location) && strlen($location) > 255) {
79                         $this->show_form(_('Location is too long (max 255 chars).'));
80                         return;
81                 } else if ($password != $confirm) {
82                         $this->show_form(_('Passwords don\'t match.'));
83                 } else if ($user = $this->register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location)) {
84                         if (!$user) {
85                                 $this->show_form(_('Invalid username or password.'));
86                                 return;
87                         }
88                         # success!
89                         if (!common_set_user($user)) {
90                                 common_server_error(_('Error setting user.'));
91                                 return;
92                         }
93                         # this is a real login
94                         common_real_login(true);
95                         if ($this->boolean('rememberme')) {
96                                 common_debug('Adding rememberme cookie for ' . $nickname);
97                                 common_rememberme($user);
98                         }
99                         $this->show_success();
100                 } else {
101                         $this->show_form(_('Invalid username or password.'));
102                 }
103         }
104
105         # checks if *CANONICAL* nickname exists
106
107         function nickname_exists($nickname) {
108                 $user = User::staticGet('nickname', $nickname);
109                 return ($user !== false);
110         }
111
112         # checks if *CANONICAL* email exists
113
114         function email_exists($email) {
115                 $email = common_canonical_email($email);
116                 $user = User::staticGet('email', $email);
117                 return ($user !== false);
118         }
119
120         function register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location) {
121
122                 $profile = new Profile();
123
124                 $profile->query('BEGIN');
125
126                 $profile->nickname = $nickname;
127                 $profile->profileurl = common_profile_url($nickname);
128                 if ($fullname) {
129                         $profile->fullname = $fullname;
130                 }
131                 if ($homepage) {
132                         $profile->homepage = $homepage;
133                 }
134                 if ($bio) {
135                         $profile->bio = $bio;
136                 }
137                 if ($location) {
138                         $profile->location = $location;
139                 }
140                 $profile->created = DB_DataObject_Cast::dateTime(); # current time
141                 
142                 $id = $profile->insert();
143
144                 if (!$id) {
145                         common_log_db_error($profile, 'INSERT', __FILE__);
146                     return FALSE;
147                 }
148                 $user = new User();
149                 $user->id = $id;
150                 $user->nickname = $nickname;
151                 $user->password = common_munge_password($password, $id);
152                 $user->created =  DB_DataObject_Cast::dateTime(); # current time
153                 $user->uri = common_user_uri($user);
154
155                 $result = $user->insert();
156
157                 if (!$result) {
158                         common_log_db_error($user, 'INSERT', __FILE__);
159                         return FALSE;
160                 }
161
162                 if ($email) {
163
164                         $confirm = new Confirm_address();
165                         $confirm->code = common_confirmation_code(128);
166                         $confirm->user_id = $user->id;
167                         $confirm->address = $email;
168                         $confirm->address_type = 'email';
169
170                         $result = $confirm->insert();
171                         if (!$result) {
172                                 common_log_db_error($confirm, 'INSERT', __FILE__);
173                                 return FALSE;
174                         }
175                 }
176
177                 $profile->query('COMMIT');
178
179                 if ($email) {
180                         mail_confirm_address($confirm->code,
181                                                                  $profile->nickname,
182                                                                  $email);
183                 }
184
185                 return $user;
186         }
187
188         function show_top($error=NULL) {
189                 if ($error) {
190                         common_element('p', 'error', $error);
191                 } else {
192                         common_element('div', 'instructions',
193                                                    _('You can create a new account to start posting notices.'));
194                 }
195         }
196
197         function show_form($error=NULL) {
198                 global $config;
199
200                 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
201                 common_element_start('form', array('method' => 'post',
202                                                                                    'id' => 'login',
203                                                                                    'action' => common_local_url('register')));
204                 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
205                                          _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
206                 common_password('password', _('Password'),
207                                                 _('6 or more characters. Required.'));
208                 common_password('confirm', _('Confirm'),
209                                                 _('Same as password above. Required.'));
210                 common_input('email', _('Email'), $this->trimmed('email'),
211                                          _('Used only for updates, announcements, and password recovery'));
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                 $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 }