]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
42b46bcc35a4a0c11083d3bfbe7e023233a311b1
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class RegisterAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26
27                 if (common_logged_in()) {
28                         common_user_error(_t('Already logged in.'));
29                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30                         $this->try_register();
31                 } else {
32                         $this->show_form();
33                 }
34         }
35
36         function try_register() {
37                 print_r($this->args);
38                 $nickname = $this->trimmed('nickname');
39                 $email = $this->trimmed('email');
40                 
41                 # We don't trim these... whitespace is OK in a password!
42                 
43                 $password = $this->arg('password');
44                 $confirm = $this->arg('confirm');
45
46                 # Input scrubbing
47
48                 $nickname = common_canonical_nickname($nickname);
49                 $email = common_canonical_email($email);
50
51                 if (!$this->boolean('license')) {
52                         $this->show_form(_t('You can\'t register if you don\'t agree to the license.'));
53                 } else if (!Validate::email($email, true)) {
54                         $this->show_form(_t('Not a valid email address.'));
55                 } else if (!Validate::string($nickname, array('min_length' => 1,
56                                                                                                           'max_length' => 64,
57                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
58                         $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
59                 } else if ($this->nickname_exists($nickname)) {
60                         $this->show_form(_t('Nickname already exists.'));
61                 } else if ($this->email_exists($email)) {
62                         $this->show_form(_t('Email address already exists.'));
63                 } else if ($password != $confirm) {
64                         $this->show_form(_t('Passwords don\'t match.'));
65                 } else if ($this->register_user($nickname, $password, $email)) {
66                         # success!
67                         if (!common_set_user($nickname)) {
68                                 common_server_error(_t('Error setting user.'));
69                                 return;
70                         }
71                         common_redirect(common_local_url('profilesettings'));
72                 } else {
73                         $this->show_form(_t('Invalid username or password.'));
74                 }
75         }
76
77         # checks if *CANONICAL* nickname exists
78
79         function nickname_exists($nickname) {
80                 $user = User::staticGet('nickname', $nickname);
81                 return ($user !== false);
82         }
83
84         # checks if *CANONICAL* email exists
85
86         function email_exists($email) {
87                 $email = common_canonical_email($email);
88                 $user = User::staticGet('email', $email);
89                 return ($user !== false);
90         }
91
92         function register_user($nickname, $password, $email) {
93                 # TODO: wrap this in a transaction!
94                 $profile = new Profile();
95                 $profile->nickname = $nickname;
96                 $profile->profileurl = common_profile_url($nickname);
97                 $profile->created = DB_DataObject_Cast::dateTime(); # current time
98
99                 $id = $profile->insert();
100                 if (!$id) {
101                         return FALSE;
102                 }
103                 $user = new User();
104                 $user->id = $id;
105                 $user->nickname = $nickname;
106                 $user->password = common_munge_password($password, $id);
107                 $user->email = $email;
108                 $user->created =  DB_DataObject_Cast::dateTime(); # current time
109                 $user->uri = common_mint_tag('user:'.$id);
110                 
111                 $result = $user->insert();
112                 if (!$result) {
113                         # Try to clean up...
114                         $profile->delete();
115                 }
116                 return $result;
117         }
118
119         function show_form($error=NULL) {
120                 global $config;
121
122                 common_show_header(_t('Register'));
123                 if ($error) {
124                         common_element('div', 'error', $error);
125                 }
126                 common_element_start('form', array('method' => 'POST',
127                                                                                    'id' => 'login',
128                                                                                    'action' => common_local_url('register')));
129                 common_input('nickname', _t('Nickname'));
130                 common_password('password', _t('Password'));
131                 common_password('confirm', _t('Confirm'));
132                 common_input('email', _t('Email'));
133                 common_element_start('p');
134                 common_element('input', array('type' => 'checkbox',
135                                                                           'id' => 'license',
136                                                                           'name' => 'license',
137                                                                           'value' => 'true'));
138                 common_text(_t('My text and files are available under '));
139                 common_element('a', array(href => $config['license']['url']),
140                                            $config['license']['title']);
141                 common_text(_t(' except this private data: password, email address, IM address, phone number.'));
142                 common_element_end('p');
143                 common_submit('submit', _t('Register'));
144                 common_element_end('form');
145                 common_show_footer();
146         }
147 }