summaryrefslogtreecommitdiff
path: root/extensions/tmwa/backend/models/account.php
blob: aaabd4185ec000ef5009a3d35d4b683130fbf797 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
class TMWAccount
{
    const GENDER_MALE   = 'M';
    const GENDER_FEMALE = 'F';
    const GENDER_NEUTRAL = 'N';
    const BAD_STRING_DESC = 'Only alphanumeric characters are allowed.';

    private $errors;
    private $ladmin;
    private $ladmin_result;

    private static function existsUsername($str)
    {
        global $wgTMWAccountLib;
        $ladmin = New $wgTMWAccountLib;
        $ladmin_result = $ladmin->account_exists($str);
        $ladmin->close();
        return $ladmin_result;
    }

    public function setUsername($name){ $this->username = $name; }
    public function setPassword1($pwd){ $this->password1 = $pwd; }
    public function setPassword2($pwd){ $this->password2 = $pwd; }
    public function setEMail($email){ $this->email = $email; }
    public function setGender($gender){ $this->gender = $gender; }

    public function validate()
    {
        $errors = array();

        // check here for correct values..
        if (strlen($this->username) < 4) {
            $errors[] = "Username is too short";
        }

        if (strlen($this->username) >= 24) {
            $errors[] = "Username is too long";
        }

        if (strlen($this->password1) < 4) {
            $errors[] = "Password is too short";
        }

        if (strlen($this->password1) >= 24) {
            $errors[] = "Password is too long";
        }

        if (strlen($this->email) < 4) {
            $errors[] = "EMail is too short";
        }

        if (strlen($this->email) >= 40) {
            $errors[] = "EMail is too long";
        }

        if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = "EMail is not valid";
        }

        if (!self::check_chars($this->username)) {
            $errors[] = 'Username contains invalid characters.<br/> '.self::BAD_STRING_DESC;
        }

        if (!self::check_chars($this->password1)) {
            $errors[] = 'Password contains invalid characters.<br/> '.self::BAD_STRING_DESC;  
        }

        if ($this->password1 != $this->password2)
        {
            $errors[] = "The given passwords don't match!";
        }

        if ($this->gender != self::GENDER_MALE &&
            $this->gender != self::GENDER_FEMALE &&
            $this->gender != self::GENDER_NEUTRAL)
        {
            $errors[] = 'Gender has to be picked!'; 
        }

        if (!filter_var($this->email, FILTER_VALIDATE_EMAIL))
        {
            $errors[] = 'EMail has wrong format.';  
        }

        if (self::existsUsername($this->username))
        {
            $errors[] = "The username is in use!";
        }

        if (count($errors) > 0)
        {
            return $errors;
        }
        return null;
    }

    public function check_chars($string) {
	    return ctype_alnum($string) && (strpos($string, '"') === FALSE);
    }

    private function sendEMail() {
        $email = New TMWMail();
        $email->mail_to = $this->email;
        $email->subject = "The Mana World Registration";
        $data = "== Account Created ==\n";
        $data .= "  Welcome ".$this->username." to The Mana World! Your account should now be enabled. If you have any problems with your login contact The Mana World via the Support (live) or Forums (maybe a delay) for help. Game tips & walkthroughs are available on the Wiki. Our Forums are a great place to ask for advice and discuss possible changes. News is available in the client or on the Home Site.
        Godspeed Adventurer,
            The Mana World\n";
        $email->getEmailTemplate($data);

        return $email->sendEMail();
    }

    public function createAccount()
    {
        global $wgTMWAccountLib;
        $ladmin = New $wgTMWAccountLib;
        $ladmin_result = $ladmin->create_account($this->username, $this->password1, $this->gender, $this->email);
        $ladmin->close();
        if($ladmin_result) {
            $this->sendEMail();
            return true;
        } else {
            return false;
        }
    }
}
?>