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
25 function handle($args)
27 parent::handle($args);
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();
40 function try_register()
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.'));
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');
56 # We don't trim these... whitespace is OK in a password!
58 $password = $this->arg('password');
59 $confirm = $this->arg('confirm');
61 # invitation code, if any
63 $code = $this->trimmed('code');
66 $invite = Invitation::staticGet($code);
69 if (common_config('site', 'inviteonly') && !($code && $invite)) {
70 $this->client_error(_('Sorry, only invited people can register.'));
76 $nickname = common_canonical_nickname($nickname);
77 $email = common_canonical_email($email);
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,
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.'));
97 } else if (!is_null($fullname) && strlen($fullname) > 255) {
98 $this->show_form(_('Full name is too long (max 255 chars).'));
100 } else if (!is_null($bio) && strlen($bio) > 140) {
101 $this->show_form(_('Bio is too long (max 140 chars).'));
103 } else if (!is_null($location) && strlen($location) > 255) {
104 $this->show_form(_('Location is too long (max 255 chars).'));
106 } else if (strlen($password) < 6) {
107 $this->show_form(_('Password must be 6 or more characters.'));
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))) {
115 $this->show_form(_('Invalid username or password.'));
119 if (!common_set_user($user)) {
120 common_server_error(_('Error setting user.'));
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);
129 # Re-init language env in case it changed (not yet, but soon)
130 common_init_language();
131 $this->show_success();
133 $this->show_form(_('Invalid username or password.'));
137 # checks if *CANONICAL* nickname exists
139 function nickname_exists($nickname)
141 $user = User::staticGet('nickname', $nickname);
142 return ($user !== false);
145 # checks if *CANONICAL* email exists
147 function email_exists($email)
149 $email = common_canonical_email($email);
150 if (!$email || strlen($email) == 0) {
153 $user = User::staticGet('email', $email);
154 return ($user !== false);
157 function show_top($error=null)
160 common_element('p', 'error', $error);
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%%)!)'));
167 common_element_start('div', 'instructions');
169 common_element_end('div');
173 function show_form($error=null)
177 $code = $this->trimmed('code');
180 $invite = Invitation::staticGet($code);
183 if (common_config('site', 'inviteonly') && !($code && $invite)) {
184 $this->client_error(_('Sorry, only invited people can register.'));
188 common_show_header(_('Register'), null, $error, array($this, 'show_top'));
189 common_element_start('form', array('method' => 'post',
191 'action' => common_local_url('register')));
193 common_hidden('token', common_session_token());
196 common_hidden('code', $code);
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'));
209 common_input('email', _('Email'), $this->trimmed('email'),
210 _('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',
232 if ($this->boolean('license')) {
233 $attrs['checked'] = 'checked';
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();
246 function show_success()
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');
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));
266 common_element_end('div');
267 common_show_footer();