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');
40 # We don't trim these... whitespace is OK in a password!
42 $password = $this->arg('password');
43 $confirm = $this->arg('confirm');
47 $nickname = common_canonical_nickname($nickname);
48 $email = common_canonical_email($email);
50 if (!$this->boolean('license')) {
51 $this->show_form(_('You can\'t register if you don\'t agree to the license.'));
52 } else if ($email && !Validate::email($email, true)) {
53 $this->show_form(_('Not a valid email address.'));
54 } else if (!Validate::string($nickname, array('min_length' => 1,
56 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
57 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
58 } else if ($this->nickname_exists($nickname)) {
59 $this->show_form(_('Nickname already in use. Try another one.'));
60 } else if (!User::allowed_nickname($nickname)) {
61 $this->show_form(_('Not a valid nickname.'));
62 } else if ($this->email_exists($email)) {
63 $this->show_form(_('Email address already exists.'));
64 } else if ($password != $confirm) {
65 $this->show_form(_('Passwords don\'t match.'));
66 } else if ($user = $this->register_user($nickname, $password, $email)) {
68 $this->show_form(_('Invalid username or password.'));
72 if (!common_set_user($user)) {
73 common_server_error(_('Error setting user.'));
76 # this is a real login
77 common_real_login(true);
78 if ($this->boolean('rememberme')) {
79 common_debug('Adding rememberme cookie for ' . $nickname);
80 common_rememberme($user);
82 common_redirect(common_local_url('profilesettings'));
84 $this->show_form(_('Invalid username or password.'));
88 # checks if *CANONICAL* nickname exists
90 function nickname_exists($nickname) {
91 $user = User::staticGet('nickname', $nickname);
92 return ($user !== false);
95 # checks if *CANONICAL* email exists
97 function email_exists($email) {
98 $email = common_canonical_email($email);
99 $user = User::staticGet('email', $email);
100 return ($user !== false);
103 function register_user($nickname, $password, $email) {
105 $profile = new Profile();
107 $profile->query('BEGIN');
109 $profile->nickname = $nickname;
110 $profile->profileurl = common_profile_url($nickname);
111 $profile->created = DB_DataObject_Cast::dateTime(); # current time
113 $id = $profile->insert();
116 common_log_db_error($profile, 'INSERT', __FILE__);
121 $user->nickname = $nickname;
122 $user->password = common_munge_password($password, $id);
123 $user->created = DB_DataObject_Cast::dateTime(); # current time
124 $user->uri = common_user_uri($user);
126 $result = $user->insert();
129 common_log_db_error($user, 'INSERT', __FILE__);
135 $confirm = new Confirm_address();
136 $confirm->code = common_confirmation_code(128);
137 $confirm->user_id = $user->id;
138 $confirm->address = $email;
139 $confirm->address_type = 'email';
141 $result = $confirm->insert();
143 common_log_db_error($confirm, 'INSERT', __FILE__);
148 $profile->query('COMMIT');
151 mail_confirm_address($confirm->code,
159 function show_top($error=NULL) {
161 common_element('p', 'error', $error);
163 common_element('div', 'instructions',
164 _('You can create a new account to start posting notices.'));
168 function show_form($error=NULL) {
171 common_show_header(_('Register'), NULL, $error, array($this, 'show_top'));
172 common_element_start('form', array('method' => 'post',
174 'action' => common_local_url('register')));
175 common_input('nickname', _('Nickname'), NULL,
176 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
177 common_password('password', _('Password'),
178 _('6 or more characters'));
179 common_password('confirm', _('Confirm'),
180 _('Same as password above'));
181 common_input('email', _('Email'), NULL,
182 _('Used only for updates, announcements, and password recovery'));
183 common_checkbox('rememberme', _('Remember me'), false,
184 _('Automatically login in the future; not for shared computers!'));
185 common_element_start('p');
186 common_element('input', array('type' => 'checkbox',
190 common_text(_('My text and files are available under '));
191 common_element('a', array(href => $config['license']['url']),
192 $config['license']['title']);
193 common_text(_(' except this private data: password, email address, IM address, phone number.'));
194 common_element_end('p');
195 common_submit('submit', _('Register'));
196 common_element_end('form');
197 common_show_footer();