3 class RegisterAction extends Action {
5 function handle($args) {
8 if (common_logged_in()) {
9 common_user_error(_t('Already logged in.'));
10 } else if ($this->arg('METHOD') == 'POST') {
11 $this->try_register();
17 function try_register() {
18 $nickname = $this->arg('nickname');
19 $password = $this->arg('password');
20 $confirm = $this->arg('confirm');
21 $email = $this->arg('email');
25 $nickname = common_canonical_nickname($nickname);
26 $email = common_canonical_email($email);
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'));
38 $this->show_form(_t('Invalid username or password.'));
42 # checks if *CANONICAL* nickname exists
44 function nickname_exists($nickname) {
45 $user = User::staticGet('nickname', $nickname);
46 return ($user !== false);
49 # checks if *CANONICAL* email exists
51 function email_exists($email) {
52 $email = common_canonicalize_email($email);
53 $user = User::staticGet('email', $email);
54 return ($user !== false);
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();
68 $user->nickname = $nickname;
69 $user->password = common_munge_password($password, $id);
70 $user->email = $email;
71 $user->created = time();
72 $result = $user->insert();
80 function show_form($error=NULL) {
82 common_show_header(_t('Login'));
83 common_start_element('form', array('method' => 'POST',
85 'action' => common_local_url('login')));
86 common_element('label', array('for' => 'username'),
88 common_element('input', array('name' => 'username',
91 common_element('label', array('for' => 'password'),
93 common_element('input', array('name' => 'password',
96 common_element('label', array('for' => 'confirm'),
98 common_element('input', array('name' => 'confirm',
101 common_element('label', array('for' => 'email'),
103 common_element('input', array('name' => 'email',
106 common_element('input', array('name' => 'submit',
110 common_element('input', array('name' => 'cancel',