3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class RegisterAction extends Action
24 function handle($args)
26 parent::handle($args);
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();
39 function try_register()
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.'));
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');
54 # We don't trim these... whitespace is OK in a password!
56 $password = $this->arg('password');
57 $confirm = $this->arg('confirm');
59 # invitation code, if any
61 $code = $this->trimmed('code');
64 $invite = Invitation::staticGet($code);
67 if (common_config('site', 'inviteonly') && !($code && $invite)) {
68 $this->client_error(_('Sorry, only invited people can register.'));
74 $nickname = common_canonical_nickname($nickname);
75 $email = common_canonical_email($email);
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,
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.'));
95 } else if (!is_null($fullname) && strlen($fullname) > 255) {
96 $this->show_form(_('Full name is too long (max 255 chars).'));
98 } else if (!is_null($bio) && strlen($bio) > 140) {
99 $this->show_form(_('Bio is too long (max 140 chars).'));
101 } else if (!is_null($location) && strlen($location) > 255) {
102 $this->show_form(_('Location is too long (max 255 chars).'));
104 } else if (strlen($password) < 6) {
105 $this->show_form(_('Password must be 6 or more characters.'));
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))) {
113 $this->show_form(_('Invalid username or password.'));
117 if (!common_set_user($user)) {
118 common_server_error(_('Error setting user.'));
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);
127 # Re-init language env in case it changed (not yet, but soon)
128 common_init_language();
129 $this->show_success();
131 $this->show_form(_('Invalid username or password.'));
135 # checks if *CANONICAL* nickname exists
137 function nickname_exists($nickname)
139 $user = User::staticGet('nickname', $nickname);
140 return ($user !== false);
143 # checks if *CANONICAL* email exists
145 function email_exists($email)
147 $email = common_canonical_email($email);
148 if (!$email || strlen($email) == 0) {
151 $user = User::staticGet('email', $email);
152 return ($user !== false);
155 function show_top($error=null)
158 common_element('p', 'error', $error);
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%%)!)'));
165 common_element_start('div', 'instructions');
167 common_element_end('div');
171 function show_form($error=null)
175 $code = $this->trimmed('code');
178 $invite = Invitation::staticGet($code);
181 if (common_config('site', 'inviteonly') && !($code && $invite)) {
182 $this->client_error(_('Sorry, only invited people can register.'));
186 common_show_header(_('Register'), null, $error, array($this, 'show_top'));
187 common_element_start('form', array('method' => 'post',
189 'action' => common_local_url('register')));
191 common_hidden('token', common_session_token());
194 common_hidden('code', $code);
197 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
198 _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
199 common_password('password', _('Password'),
200 _('6 or more characters. Required.'));
201 common_password('confirm', _('Confirm'),
202 _('Same as password above. Required.'));
203 if ($invite && $invite->address_type == 'email') {
204 common_input('email', _('Email'), $invite->address,
205 _('Used only for updates, announcements, and password recovery'));
207 common_input('email', _('Email'), $this->trimmed('email'),
208 _('Used only for updates, announcements, and password recovery'));
210 common_input('fullname', _('Full name'),
211 $this->trimmed('fullname'),
212 _('Longer name, preferably your "real" name'));
213 common_input('homepage', _('Homepage'),
214 $this->trimmed('homepage'),
215 _('URL of your homepage, blog, or profile on another site'));
216 common_textarea('bio', _('Bio'),
217 $this->trimmed('bio'),
218 _('Describe yourself and your interests in 140 chars'));
219 common_input('location', _('Location'),
220 $this->trimmed('location'),
221 _('Where you are, like "City, State (or Region), Country"'));
222 common_checkbox('rememberme', _('Remember me'),
223 $this->boolean('rememberme'),
224 _('Automatically login in the future; not for shared computers!'));
225 common_element_start('p');
226 $attrs = array('type' => 'checkbox',
230 if ($this->boolean('license')) {
231 $attrs['checked'] = 'checked';
233 common_element('input', $attrs);
234 common_text(_('My text and files are available under '));
235 common_element('a', array('href' => $config['license']['url']),
236 $config['license']['title']);
237 common_text(_(' except this private data: password, email address, IM address, phone number.'));
238 common_element_end('p');
239 common_submit('submit', _('Register'));
240 common_element_end('form');
241 common_show_footer();
244 function show_success()
246 $nickname = $this->arg('nickname');
247 common_show_header(_('Registration successful'));
248 common_element_start('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 common_raw(common_markup_to_html($instr));
258 $have_email = $this->trimmed('email');
260 $emailinstr = _('(You should receive a message by email momentarily, with ' .
261 'instructions on how to confirm your email address.)');
262 common_raw(common_markup_to_html($emailinstr));
264 common_element_end('div');
265 common_show_footer();