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() {
39 $nickname = $this->trimmed('nickname');
40 $email = $this->trimmed('email');
41 $fullname = $this->trimmed('fullname');
42 $homepage = $this->trimmed('homepage');
43 $bio = $this->trimmed('bio');
44 $location = $this->trimmed('location');
46 # We don't trim these... whitespace is OK in a password!
48 $password = $this->arg('password');
49 $confirm = $this->arg('confirm');
53 $nickname = common_canonical_nickname($nickname);
54 $email = common_canonical_email($email);
56 if (!$this->boolean('license')) {
57 $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
58 } else if ($email && !Validate::email($email, true)) {
59 $this->show_form(_('Not a valid email address.'));
60 } else if (!Validate::string($nickname, array('min_length' => 1,
62 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
63 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
64 } else if ($this->nickname_exists($nickname)) {
65 $this->show_form(_('Nickname already in use. Try another one.'));
66 } else if (!User::allowed_nickname($nickname)) {
67 $this->show_form(_('Not a valid nickname.'));
68 } else if ($this->email_exists($email)) {
69 $this->show_form(_('Email address already exists.'));
70 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
71 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
72 $this->show_form(_('Homepage is not a valid URL.'));
74 } else if (!is_null($fullname) && strlen($fullname) > 255) {
75 $this->show_form(_('Full name is too long (max 255 chars).'));
77 } else if (!is_null($bio) && strlen($bio) > 140) {
78 $this->show_form(_('Bio is too long (max 140 chars).'));
80 } else if (!is_null($location) && strlen($location) > 255) {
81 $this->show_form(_('Location is too long (max 255 chars).'));
83 } else if ($password != $confirm) {
84 $this->show_form(_('Passwords don\'t match.'));
85 } else if ($user = $this->register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location)) {
87 $this->show_form(_('Invalid username or password.'));
91 if (!common_set_user($user)) {
92 common_server_error(_('Error setting user.'));
95 # this is a real login
96 common_real_login(true);
97 if ($this->boolean('rememberme')) {
98 common_debug('Adding rememberme cookie for ' . $nickname);
99 common_rememberme($user);
101 # Re-init language env in case it changed (not yet, but soon)
102 common_init_language();
103 $this->show_success();
105 $this->show_form(_('Invalid username or password.'));
109 # checks if *CANONICAL* nickname exists
111 function nickname_exists($nickname) {
112 $user = User::staticGet('nickname', $nickname);
113 return ($user !== false);
116 # checks if *CANONICAL* email exists
118 function email_exists($email) {
119 $email = common_canonical_email($email);
120 $user = User::staticGet('email', $email);
121 return ($user !== false);
124 function register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location) {
126 $profile = new Profile();
128 $profile->query('BEGIN');
130 $profile->nickname = $nickname;
131 $profile->profileurl = common_profile_url($nickname);
133 $profile->fullname = $fullname;
136 $profile->homepage = $homepage;
139 $profile->bio = $bio;
142 $profile->location = $location;
144 $profile->created = DB_DataObject_Cast::dateTime(); # current time
146 $id = $profile->insert();
149 common_log_db_error($profile, 'INSERT', __FILE__);
154 $user->nickname = $nickname;
155 $user->password = common_munge_password($password, $id);
156 $user->created = DB_DataObject_Cast::dateTime(); # current time
157 $user->uri = common_user_uri($user);
159 $result = $user->insert();
162 common_log_db_error($user, 'INSERT', __FILE__);
166 # Everyone is subscribed to themself
168 $subscription = new Subscription();
169 $subscription->subscriber = $user->id;
170 $subscription->subscribed = $user->id;
171 $subscription->created = $user->created;
173 $result = $subscription->insert();
176 common_log_db_error($subscription, 'INSERT', __FILE__);
182 $confirm = new Confirm_address();
183 $confirm->code = common_confirmation_code(128);
184 $confirm->user_id = $user->id;
185 $confirm->address = $email;
186 $confirm->address_type = 'email';
188 $result = $confirm->insert();
190 common_log_db_error($confirm, 'INSERT', __FILE__);
195 $profile->query('COMMIT');
198 mail_confirm_address($confirm->code,
206 function show_top($error=NULL) {
208 common_element('p', 'error', $error);
210 common_element('div', 'instructions',
211 _('You can create a new account to start posting notices.'));
215 function show_form($error=NULL) {
218 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
219 common_element_start('form', array('method' => 'post',
221 'action' => common_local_url('register')));
222 common_input('nickname', _('Nickname'), $this->trimmed('nickname'),
223 _('1-64 lowercase letters or numbers, no punctuation or spaces. Required.'));
224 common_password('password', _('Password'),
225 _('6 or more characters. Required.'));
226 common_password('confirm', _('Confirm'),
227 _('Same as password above. Required.'));
228 common_input('email', _('Email'), $this->trimmed('email'),
229 _('Used only for updates, announcements, and password recovery'));
230 common_input('fullname', _('Full name'),
231 $this->trimmed('fullname'),
232 _('Longer name, preferably your "real" name'));
233 common_input('homepage', _('Homepage'),
234 $this->trimmed('homepage'),
235 _('URL of your homepage, blog, or profile on another site'));
236 common_textarea('bio', _('Bio'),
237 $this->trimmed('bio'),
238 _('Describe yourself and your interests in 140 chars'));
239 common_input('location', _('Location'),
240 $this->trimmed('location'),
241 _('Where you are, like "City, State (or Region), Country"'));
242 common_checkbox('rememberme', _('Remember me'),
243 $this->boolean('rememberme'),
244 _('Automatically login in the future; not for shared computers!'));
245 common_element_start('p');
246 $attrs = array('type' => 'checkbox',
250 if ($this->boolean('license')) {
251 $attrs['checked'] = 'checked';
253 common_element('input', $attrs);
254 common_text(_('My text and files are available under '));
255 common_element('a', array(href => $config['license']['url']),
256 $config['license']['title']);
257 common_text(_(' except this private data: password, email address, IM address, phone number.'));
258 common_element_end('p');
259 common_submit('submit', _('Register'));
260 common_element_end('form');
261 common_show_footer();
264 function show_success() {
265 $nickname = $this->arg('nickname');
266 common_show_header(_('Registration successful'));
267 common_element_start('div', 'success');
268 $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. From here, you may want to...'. "\n\n" .
269 '* Go to [your profile](%s) and post your first message.' . "\n" .
270 '* Add a [Jabber/GTalk address](%%%%action.imsettings%%%%) so you can send notices through instant messages.' . "\n" .
271 '* [Search for people](%%%%action.peoplesearch%%%%) that you may know or that share your interests. ' . "\n" .
272 '* Update your [profile settings](%%%%action.profilesettings%%%%) to tell others more about you. ' . "\n" .
273 '* Read over the [online docs](%%%%doc.help%%%%) for features you may have missed. ' . "\n\n" .
274 'Thanks for signing up and we hope you enjoy using this service.'),
275 $nickname, common_local_url('showstream', array('nickname' => $nickname)));
276 common_raw(common_markup_to_html($instr));
277 $have_email = $this->trimmed('email');
279 $emailinstr = _('(You should receive a message by email momentarily, with ' .
280 'instructions on how to confirm your email address.)');
281 common_raw(common_markup_to_html($emailinstr));
283 common_element_end('div');
284 common_show_footer();