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) {
25 parent::handle($args);
27 if (common_config('site', 'closed')) {
28 common_user_error(_('Registration not allowed.'));
29 } else if (common_logged_in()) {
30 common_user_error(_('Already logged in.'));
31 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32 $this->try_register();
38 function try_register() {
40 $token = $this->trimmed('token');
41 if (!$token || $token != common_session_token()) {
42 $this->show_form(_('There was a problem with your session token. Try again, please.'));
46 $nickname = $this->trimmed('nickname');
47 $email = $this->trimmed('email');
48 $fullname = $this->trimmed('fullname');
49 $homepage = $this->trimmed('homepage');
50 $bio = $this->trimmed('bio');
51 $location = $this->trimmed('location');
53 # We don't trim these... whitespace is OK in a password!
55 $password = $this->arg('password');
56 $confirm = $this->arg('confirm');
58 # invitation code, if any
60 $code = $this->trimmed('code');
64 $nickname = common_canonical_nickname($nickname);
65 $email = common_canonical_email($email);
67 if (!$this->boolean('license')) {
68 $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
69 } else if ($email && !Validate::email($email, true)) {
70 $this->show_form(_('Not a valid email address.'));
71 } else if (!Validate::string($nickname, array('min_length' => 1,
73 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
74 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
75 } else if ($this->nickname_exists($nickname)) {
76 $this->show_form(_('Nickname already in use. Try another one.'));
77 } else if (!User::allowed_nickname($nickname)) {
78 $this->show_form(_('Not a valid nickname.'));
79 } else if ($this->email_exists($email)) {
80 $this->show_form(_('Email address already exists.'));
81 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
82 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
83 $this->show_form(_('Homepage is not a valid URL.'));
85 } else if (!is_null($fullname) && strlen($fullname) > 255) {
86 $this->show_form(_('Full name is too long (max 255 chars).'));
88 } else if (!is_null($bio) && strlen($bio) > 140) {
89 $this->show_form(_('Bio is too long (max 140 chars).'));
91 } else if (!is_null($location) && strlen($location) > 255) {
92 $this->show_form(_('Location is too long (max 255 chars).'));
94 } else if (strlen($password) < 6) {
95 $this->show_form(_('Password must be 6 or more characters.'));
97 } else if ($password != $confirm) {
98 $this->show_form(_('Passwords don\'t match.'));
99 } else if ($user = User::register(array('nickname' => $nickname, 'password' => $password, 'email' => $email,
100 'fullname' => $fullname, 'homepage' => $homepage, 'bio' => $bio,
101 'location' => $location, 'code' => $code))) {
103 $this->show_form(_('Invalid username or password.'));
107 if (!common_set_user($user)) {
108 common_server_error(_('Error setting user.'));
111 # this is a real login
112 common_real_login(true);
113 if ($this->boolean('rememberme')) {
114 common_debug('Adding rememberme cookie for ' . $nickname);
115 common_rememberme($user);
117 # Re-init language env in case it changed (not yet, but soon)
118 common_init_language();
119 $this->show_success();
121 $this->show_form(_('Invalid username or password.'));
125 # checks if *CANONICAL* nickname exists
127 function nickname_exists($nickname) {
128 $user = User::staticGet('nickname', $nickname);
129 return ($user !== false);
132 # checks if *CANONICAL* email exists
134 function email_exists($email) {
135 $email = common_canonical_email($email);
136 $user = User::staticGet('email', $email);
137 return ($user !== false);
140 function show_top($error=NULL) {
142 common_element('p', 'error', $error);
144 common_element('div', 'instructions',
145 _('You can create a new account to start posting notices.'));
149 function show_form($error=NULL) {
152 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
153 common_element_start('form', array('method' => 'post',
155 'action' => common_local_url('register')));
157 common_hidden('token', common_session_token());
159 if ($this->trimmed('code')) {
160 $code = ($this->trimmed('code'));
161 $invite = Invitation::staticGet($code);
162 common_hidden('code', $code);
165 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
166 _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
167 common_password('password', _('Password'),
168 _('6 or more characters. Required.'));
169 common_password('confirm', _('Confirm'),
170 _('Same as password above. Required.'));
171 if ($invite && $invite->address_type == 'email') {
172 common_input('email', _('Email'), $invite->address,
173 _('Used only for updates, announcements, and password recovery'));
175 common_input('email', _('Email'), $this->trimmed('email'),
176 _('Used only for updates, announcements, and password recovery'));
178 common_input('fullname', _('Full name'),
179 $this->trimmed('fullname'),
180 _('Longer name, preferably your "real" name'));
181 common_input('homepage', _('Homepage'),
182 $this->trimmed('homepage'),
183 _('URL of your homepage, blog, or profile on another site'));
184 common_textarea('bio', _('Bio'),
185 $this->trimmed('bio'),
186 _('Describe yourself and your interests in 140 chars'));
187 common_input('location', _('Location'),
188 $this->trimmed('location'),
189 _('Where you are, like "City, State (or Region), Country"'));
190 common_checkbox('rememberme', _('Remember me'),
191 $this->boolean('rememberme'),
192 _('Automatically login in the future; not for shared computers!'));
193 common_element_start('p');
194 $attrs = array('type' => 'checkbox',
198 if ($this->boolean('license')) {
199 $attrs['checked'] = 'checked';
201 common_element('input', $attrs);
202 common_text(_('My text and files are available under '));
203 common_element('a', array('href' => $config['license']['url']),
204 $config['license']['title']);
205 common_text(_(' except this private data: password, email address, IM address, phone number.'));
206 common_element_end('p');
207 common_submit('submit', _('Register'));
208 common_element_end('form');
209 common_show_footer();
212 function show_success() {
213 $nickname = $this->arg('nickname');
214 common_show_header(_('Registration successful'));
215 common_element_start('div', 'success');
216 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. From here, you may want to...'. "\n\n" .
217 '* Go to [your profile](%s) and post your first message.' . "\n" .
218 '* Add a [Jabber/GTalk address](%%%%action.imsettings%%%%) so you can send notices through instant messages.' . "\n" .
219 '* [Search for people](%%%%action.peoplesearch%%%%) that you may know or that share your interests. ' . "\n" .
220 '* Update your [profile settings](%%%%action.profilesettings%%%%) to tell others more about you. ' . "\n" .
221 '* Read over the [online docs](%%%%doc.help%%%%) for features you may have missed. ' . "\n\n" .
222 'Thanks for signing up and we hope you enjoy using this service.'),
223 $nickname, common_local_url('showstream', array('nickname' => $nickname)));
224 common_raw(common_markup_to_html($instr));
225 $have_email = $this->trimmed('email');
227 $emailinstr = _('(You should receive a message by email momentarily, with ' .
228 'instructions on how to confirm your email address.)');
229 common_raw(common_markup_to_html($emailinstr));
231 common_element_end('div');
232 common_show_footer();