]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
add stub email canonicalizer
[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->arg('nickname');
38                 $password = $this->arg('password');
39                 $confirm = $this->arg('confirm');
40                 $email = $this->arg('email');
41                 
42                 # Input scrubbing
43                 
44                 $nickname = common_canonical_nickname($nickname);
45                 $email = common_canonical_email($email);
46                 
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)) {
54                         common_set_user($nickname);
55                         common_redirect(common_local_url('settings'));
56                 } else {
57                         $this->show_form(_t('Invalid username or password.'));
58                 }
59         }
60
61         # checks if *CANONICAL* nickname exists
62         
63         function nickname_exists($nickname) {
64                 $user = User::staticGet('nickname', $nickname);
65                 return ($user !== false);
66         }
67
68         # checks if *CANONICAL* email exists
69         
70         function email_exists($email) {
71                 $email = common_canonicalize_email($email);
72                 $user = User::staticGet('email', $email);
73                 return ($user !== false);
74         }
75
76         function register_user($nickname, $password, $email) {
77                 # TODO: wrap this in a transaction!
78                 $profile = new Profile();
79                 $profile->nickname = $nickname;
80                 $profile->created = time();
81                 $id = $profile->insert();
82                 if (!$id) {
83                         return FALSE;
84                 }
85                 $user = new User();
86                 $user->id = $id;
87                 $user->nickname = $nickname;
88                 $user->password = common_munge_password($password, $id);
89                 $user->email = $email;
90                 $user->created = time();
91                 $result = $user->insert();
92                 if (!$result) {
93                         # Try to clean up...
94                         $profile->delete();
95                 }
96                 return $result;
97         }
98         
99         function show_form($error=NULL) {
100                 
101                 common_show_header(_t('Register'));
102                 common_element_start('form', array('method' => 'POST',
103                                                                                    'id' => 'login',
104                                                                                    'action' => common_local_url('register')));
105                 common_element('label', array('for' => 'username'),
106                                            _t('Name'));
107                 common_element('input', array('name' => 'username',
108                                                                           'type' => 'text',
109                                                                           'id' => 'username'));
110                 common_element('label', array('for' => 'password'),
111                                            _t('Password'));
112                 common_element('input', array('name' => 'password',
113                                                                           'type' => 'password',                                                                   
114                                                                           'id' => 'password'));
115                 common_element('label', array('for' => 'confirm'),
116                                            _t('Confirm'));
117                 common_element('input', array('name' => 'confirm',
118                                                                           'type' => 'password',                                                                   
119                                                                           'id' => 'confirm'));
120                 common_element('label', array('for' => 'email'),
121                                            _t('Email'));
122                 common_element('input', array('name' => 'email',
123                                                                           'type' => 'text',                                                                       
124                                                                           'id' => 'email'));
125                 common_element('input', array('name' => 'submit',
126                                                                           'type' => 'submit',
127                                                                           'id' => 'submit',
128                                                                           'value' => _t('Login')));
129                 common_element('input', array('name' => 'cancel',
130                                                                           'type' => 'button',
131                                                                           'id' => 'cancel',
132                                                                           'value' => _t('Cancel')));
133                 common_element_end('form');
134         }
135 }