]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
base class is_readonly() now returns false by default
[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                 # Everyone is subscribed to themself
163
164                 $subscription = new Subscription();
165                 $subscription->subscriber = $user->id;
166                 $subscription->subscribed = $user->id;
167                 $subscription->created = $user->created;
168                 
169                 $result = $subscription->insert();
170                 
171                 if (!$result) {
172                         common_log_db_error($subscription, 'INSERT', __FILE__);
173                         return FALSE;
174                 }
175                 
176                 if ($email) {
177
178                         $confirm = new Confirm_address();
179                         $confirm->code = common_confirmation_code(128);
180                         $confirm->user_id = $user->id;
181                         $confirm->address = $email;
182                         $confirm->address_type = 'email';
183
184                         $result = $confirm->insert();
185                         if (!$result) {
186                                 common_log_db_error($confirm, 'INSERT', __FILE__);
187                                 return FALSE;
188                         }
189                 }
190
191                 $profile->query('COMMIT');
192
193                 if ($email) {
194                         mail_confirm_address($confirm->code,
195                                                                  $profile->nickname,
196                                                                  $email);
197                 }
198
199                 return $user;
200         }
201
202         function show_top($error=NULL) {
203                 if ($error) {
204                         common_element('p', 'error', $error);
205                 } else {
206                         common_element('div', 'instructions',
207                                                    _('You can create a new account to start posting notices.'));
208                 }
209         }
210
211         function show_form($error=NULL) {
212                 global $config;
213
214                 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
215                 common_element_start('form', array('method' => 'post',
216                                                                                    'id' => 'login',
217                                                                                    'action' => common_local_url('register')));
218                 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
219                                          _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
220                 common_password('password', _('Password'),
221                                                 _('6 or more characters. Required.'));
222                 common_password('confirm', _('Confirm'),
223                                                 _('Same as password above. Required.'));
224                 common_input('email', _('Email'), $this->trimmed('email'),
225                                          _('Used only for updates, announcements, and password recovery'));
226                 common_input('fullname', _('Full name'),
227                                          $this->trimmed('fullname'),
228                                           _('Longer name, preferably your "real" name'));
229                 common_input('homepage', _('Homepage'),
230                                          $this->trimmed('homepage'),
231                                          _('URL of your homepage, blog, or profile on another site'));
232                 common_textarea('bio', _('Bio'),
233                                                 $this->trimmed('bio'),
234                                                  _('Describe yourself and your interests in 140 chars'));
235                 common_input('location', _('Location'),
236                                          $this->trimmed('location'),
237                                          _('Where you are, like "City, State (or Region), Country"'));
238                 common_checkbox('rememberme', _('Remember me'), 
239                                                 $this->boolean('rememberme'),
240                                 _('Automatically login in the future; not for shared computers!'));
241                 common_element_start('p');
242                 $attrs = array('type' => 'checkbox',
243                                            'id' => 'license',
244                                            'name' => 'license',
245                                            'value' => 'true');
246                 if ($this->boolean('license')) {
247                         $attrs['checked'] = 'checked';
248                 }
249                 common_element('input', $attrs);
250             common_text(_('My text and files are available under '));
251                 common_element('a', array(href => $config['license']['url']),
252                                            $config['license']['title']);
253                 common_text(_(' except this private data: password, email address, IM address, phone number.'));
254                 common_element_end('p');
255                 common_submit('submit', _('Register'));
256                 common_element_end('form');
257                 common_show_footer();
258         }
259                                                 
260         function show_success() {
261                 $nickname = $this->arg('nickname');
262                 common_show_header(_('Registration successful'));
263                 common_element_start('div', 'success');
264                 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. From here, you may want to...'. "\n\n" .
265                                                    '* Go to [your profile](%s) and post your first message.' .  "\n" .
266                                                    '* Add a [Jabber/GTalk address](%%%%action.imsettings%%%%) so you can send notices through instant messages.' . "\n" .
267                                                    '* [Search for people](%%%%action.peoplesearch%%%%) that you may know or that share your interests. ' . "\n" .
268                                                    '* Update your [profile settings](%%%%action.profilesettings%%%%) to tell others more about you. ' . "\n" .
269                                                    '* Read over the [online docs](%%%%doc.help%%%%) for features you may have missed. ' . "\n\n" .
270                                                    'Thanks for signing up and we hope you enjoy using this service.'),
271                                                  $nickname, common_local_url('showstream', array('nickname' => $nickname)));
272                 common_raw(common_markup_to_html($instr));
273                 $have_email = $this->trimmed('email');
274                 if ($have_email) {
275                         $emailinstr = _('(You should receive a message by email momentarily, with ' .
276                                                         'instructions on how to confirm your email address.)');
277                         common_raw(common_markup_to_html($emailinstr));
278                 }
279                 common_element_end('div');
280                 common_show_footer();
281         }
282                                                 
283 }