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(_t('Already logged in.'));
29 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30 $this->try_register();
36 function try_register() {
37 $nickname = $this->arg('nickname');
38 $password = $this->arg('password');
39 $confirm = $this->arg('confirm');
40 $email = $this->arg('email');
44 $nickname = common_canonical_nickname($nickname);
45 $email = common_canonical_email($email);
47 if ($this->nickname_exists($nickname)) {
48 $this->show_form(_t('Username already exists.'));
49 } else if ($this->email_exists($email)) {
50 $this->show_form(_t('Email address already exists.'));
51 } else if ($password != $confirm) {
52 $this->show_form(_t('Passwords don\'t match.'));
53 } else if ($this->register_user($nickname, $password, $email)) {
55 if (!common_set_user($nickname)) {
56 common_server_error(_t('Error setting user.'));
59 common_redirect(common_local_url('profilesettings'));
61 $this->show_form(_t('Invalid username or password.'));
65 # checks if *CANONICAL* nickname exists
67 function nickname_exists($nickname) {
68 $user = User::staticGet('nickname', $nickname);
69 return ($user !== false);
72 # checks if *CANONICAL* email exists
74 function email_exists($email) {
75 $email = common_canonical_email($email);
76 $user = User::staticGet('email', $email);
77 return ($user !== false);
80 function register_user($nickname, $password, $email) {
81 # TODO: wrap this in a transaction!
82 $profile = new Profile();
83 $profile->nickname = $nickname;
84 $profile->profileurl = common_profile_url($nickname);
85 $profile->created = DB_DataObject_Cast::dateTime(); # current time
86 $id = $profile->insert();
92 $user->nickname = $nickname;
93 $user->password = common_munge_password($password, $id);
94 $user->email = $email;
95 $user->created = DB_DataObject_Cast::dateTime(); # current time
96 $result = $user->insert();
104 function show_form($error=NULL) {
106 common_show_header(_t('Register'));
107 common_element_start('form', array('method' => 'POST',
109 'action' => common_local_url('register')));
110 common_element('label', array('for' => 'nickname'),
112 common_element('input', array('name' => 'nickname',
114 'id' => 'nickname'));
115 common_element('label', array('for' => 'password'),
117 common_element('input', array('name' => 'password',
118 'type' => 'password',
119 'id' => 'password'));
120 common_element('label', array('for' => 'confirm'),
122 common_element('input', array('name' => 'confirm',
123 'type' => 'password',
125 common_element('label', array('for' => 'email'),
127 common_element('input', array('name' => 'email',
130 common_element('input', array('name' => 'submit',
133 'value' => _t('Register')));
134 common_element('input', array('name' => 'cancel',
137 'value' => _t('Cancel')));
138 common_element_end('form');
139 common_show_footer();