summaryrefslogtreecommitdiff
path: root/includes/models/account.php
blob: ef253c67fbb8b894aadf4e4654f7fcf84a2d16c3 (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
<?php

require_once "includes/libs/libstrutils.php";

class TMWAccount
{
	const ACCOUNT_TBL = "tmw_accounts";
	
	const GENDER_MALE   = 1;
	const GENDER_FEMALE = 2;
	
	const STATE_PENDING = 0;
	const STATE_CREATED = 0;
	const STATE_FAILED  = 0;

	private $id;
	private $username;
	private $password;
	private $email;
	private $gender;
	private $state;
	private $registration;
	
	public static function getAccountCount()
	{
		$db = Database::getInstance();
		$sql = "SELECT COUNT(*) FROM " . TMWAccount::ACCOUNT_TBL;
		return $db->getValue( $sql );
	}
	
	public static function existsUsername($str)
	{
		$db = Database::getInstance();
		$sql = sprintf("SELECT COUNT(*) FROM " . TMWAccount::ACCOUNT_TBL .
			   " WHERE USERNAME = '%s'", $db->escape($str));
		return ($db->getValue($sql) == 1);
	}
	
	public function setUsername($name){ $this->username = $name; }
	public function setPassword($pwd){ $this->password = $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->password) < 4)
			$errors[] = "Password is too short";
			
		if (strlen($this->password) >= 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 (!check_chars($this->username))
			$errors[] = 'Username contains invalid characters. ' . BAD_STRING_DESC;
			
		if (!check_chars($this->password))
			$errors[] = 'Password contains invalid characters. ' . BAD_STRING_DESC;	
			
		if ($this->gender != TMWAccount::GENDER_MALE &&
			$this->gender != TMWAccount::GENDER_FEMALE )
		{
			$errors[] = 'Gender has to be Male or Female!';	
		}
		
		if (!filter_var($this->email, FILTER_VALIDATE_EMAIL))
		{
			$errors[] = 'EMail has wrong format.';	
		}
		
			
		// returns true if everything is fine ( test with === true)
		if (count($errors) == 0)
		{
			return true;
		}
		else
		{
			return $errors;
		}
	}
	
	
	
	public function storeAccount()
	{
		$db = Database::getInstance();
		$sql = sprintf( "INSERT INTO " . TMWAccount::ACCOUNT_TBL . 
		                "   (USERNAME, PASSWORD, EMAIL, GENDER) " . 
						"VALUES ('%s', '%s', '%s', %d) ", 
						$db->escape($this->username),
						$db->escape($this->password),
						$db->escape($this->email),
						$this->gender);
						
		$rows = $db->exec( $sql );
		return ( $rows == 1 );
	}
}

?>