]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
considerable coding
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2
3 class RegisterAction extends Action {
4         
5         function handle($args) {
6                 parent::handle($args);
7                 
8                 if (common_logged_in()) {
9                         common_user_error(_t('Already logged in.'));
10                 } else if ($this->arg('METHOD') == 'POST') {
11                         $this->try_register();
12                 } else {
13                         $this->show_form();
14                 }
15         }
16
17         function try_register() {
18                 $nickname = $this->arg('nickname');
19                 $password = $this->arg('password');
20                 $confirm = $this->arg('confirm');
21                 $email = $this->arg('email');
22                 
23                 # Input scrubbing
24                 
25                 $nickname = common_canonical_nickname($nickname);
26                 $email = common_canonical_email($email);
27                 
28                 if ($this->nickname_exists($nickname)) {
29                         $this->show_form(_t('Username already exists.'));
30                 } else if ($this->email_exists($email)) {
31                         $this->show_form(_t('Email address already exists.'));
32                 } else if ($password != $confirm) {
33                         $this->show_form(_t('Passwords don\'t match.'));
34                 } else if ($this->register_user($nickname, $password, $email)) {
35                         common_set_user($nickname);
36                         common_redirect(common_local_url('settings'));
37                 } else {
38                         $this->show_form(_t('Invalid username or password.'));
39                 }
40         }
41
42         # checks if *CANONICAL* nickname exists
43         
44         function nickname_exists($nickname) {
45                 $user = User::staticGet('nickname', $nickname);
46                 return ($user !== false);
47         }
48
49         # checks if *CANONICAL* email exists
50         
51         function email_exists($email) {
52                 $email = common_canonicalize_email($email);
53                 $user = User::staticGet('email', $email);
54                 return ($user !== false);
55         }
56
57         function register_user($nickname, $password, $email) {
58                 # TODO: wrap this in a transaction!
59                 $profile = new Profile();
60                 $profile->nickname = $nickname;
61                 $profile->created = time();
62                 $id = $profile->insert();
63                 if (!$id) {
64                         return FALSE;
65                 }
66                 $user = new User();
67                 $user->id = $id;
68                 $user->nickname = $nickname;
69                 $user->password = common_munge_password($password, $id);
70                 $user->email = $email;
71                 $user->created = time();
72                 $result = $user->insert();
73                 if (!$result) {
74                         # Try to clean up...
75                         $profile->delete();
76                 }
77                 return $result;
78         }
79         
80         function show_form($error=NULL) {
81                 
82                 common_show_header(_t('Login'));
83                 common_start_element('form', array('method' => 'POST',
84                                                                                    'id' => 'login',
85                                                                                    'action' => common_local_url('login')));
86                 common_element('label', array('for' => 'username'),
87                                            _t('Name'));
88                 common_element('input', array('name' => 'username',
89                                                                           'type' => 'text',
90                                                                           'id' => 'username'));
91                 common_element('label', array('for' => 'password'),
92                                            _t('Password'));
93                 common_element('input', array('name' => 'password',
94                                                                           'type' => 'password',                                                                   
95                                                                           'id' => 'password'));
96                 common_element('label', array('for' => 'confirm'),
97                                            _t('Confirm'));
98                 common_element('input', array('name' => 'confirm',
99                                                                           'type' => 'password',                                                                   
100                                                                           'id' => 'confirm'));
101                 common_element('label', array('for' => 'email'),
102                                            _t('Email'));
103                 common_element('input', array('name' => 'email',
104                                                                           'type' => 'text',                                                                       
105                                                                           'id' => 'email'));
106                 common_element('input', array('name' => 'submit',
107                                                                           'type' => 'submit',
108                                                                           'id' => 'submit'),
109                                            _t('Login'));
110                 common_element('input', array('name' => 'cancel',
111                                                                           'type' => 'button',
112                                                                           'id' => 'cancel'),
113                                            _t('Cancel'));
114         }
115 }