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