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_logged_in()) {
28 common_user_error(_('Already logged in.'));
29 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30 $this->try_register();
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');
44 # We don't trim these... whitespace is OK in a password!
46 $password = $this->arg('password');
47 $confirm = $this->arg('confirm');
51 $nickname = common_canonical_nickname($nickname);
52 $email = common_canonical_email($email);
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,
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.'));
72 } else if (!is_null($fullname) && strlen($fullname) > 255) {
73 $this->show_form(_('Full name is too long (max 255 chars).'));
75 } else if (!is_null($bio) && strlen($bio) > 140) {
76 $this->show_form(_('Bio is too long (max 140 chars).'));
78 } else if (!is_null($location) && strlen($location) > 255) {
79 $this->show_form(_('Location is too long (max 255 chars).'));
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)) {
85 $this->show_form(_('Invalid username or password.'));
89 if (!common_set_user($user)) {
90 common_server_error(_('Error setting user.'));
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);
99 $this->show_success();
101 $this->show_form(_('Invalid username or password.'));
105 # checks if *CANONICAL* nickname exists
107 function nickname_exists($nickname) {
108 $user = User::staticGet('nickname', $nickname);
109 return ($user !== false);
112 # checks if *CANONICAL* email exists
114 function email_exists($email) {
115 $email = common_canonical_email($email);
116 $user = User::staticGet('email', $email);
117 return ($user !== false);
120 function register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location) {
122 $profile = new Profile();
124 $profile->query('BEGIN');
126 $profile->nickname = $nickname;
127 $profile->profileurl = common_profile_url($nickname);
129 $profile->fullname = $fullname;
132 $profile->homepage = $homepage;
135 $profile->bio = $bio;
138 $profile->location = $location;
140 $profile->created = DB_DataObject_Cast::dateTime(); # current time
142 $id = $profile->insert();
145 common_log_db_error($profile, 'INSERT', __FILE__);
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);
155 $result = $user->insert();
158 common_log_db_error($user, 'INSERT', __FILE__);
162 # Everyone is subscribed to themself
164 $subscription = new Subscription();
165 $subscription->subscriber = $user->id;
166 $subscription->subscribed = $user->id;
167 $subscription->created = $user->created;
169 $result = $subscription->insert();
172 common_log_db_error($subscription, 'INSERT', __FILE__);
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';
184 $result = $confirm->insert();
186 common_log_db_error($confirm, 'INSERT', __FILE__);
191 $profile->query('COMMIT');
194 mail_confirm_address($confirm->code,
202 function show_top($error=NULL) {
204 common_element('p', 'error', $error);
206 common_element('div', 'instructions',
207 _('You can create a new account to start posting notices.'));
211 function show_form($error=NULL) {
214 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
215 common_element_start('form', array('method' => 'post',
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',
246 if ($this->boolean('license')) {
247 $attrs['checked'] = 'checked';
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();
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');
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));
279 common_element_end('div');
280 common_show_footer();