md-master-server /library/User.php

Language PHP Lines 227
MD5 Hash 7ccaa88de5f4fcdef13b7d8206d017ec
Repository https://github.com/liquidhot/md-master-server.git View Raw File
  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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
require_once "Utility.php";

class User {

	//Static Functions
	public static function getUser($username) {
		$user = new User();
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		//Prepare query to find user
		$stmt = $mysqli->prepare("SELECT user_id, username, password_hash, email, joined_date, last_login_date FROM users WHERE UPPER(username) = UPPER(?)");
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_param("s", $username);
		
		//Run query
		$stmt->execute();
		if ($mysqli->errno) {
			$stmt->close();
		    unset($stmt);
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->bind_result($userId, $username, $passwordHash, $email, $joinedDate, $lastLoginDate);
		
		$result = $stmt->fetch();
		
		$stmt->close();
		unset($stmt);
		
		if(!$result) { //Not found or error.
			return null; 
		}		
		
		//Fill user object
		$user->setUserId($userId);
		$user->setUsername($username);
		$user->setPasswordHash($passwordHash);
		$user->setEmail($email);
		$user->setJoinedDate($joinedDate);
		$user->setLastLoginDate($lastLoginDate);
				
		return $user;
	}
	
	public static function createUser(User $user) {
		if(User::getUser($user->username) !== null) {
			throw new Exception("Error: User already exists!");
		}
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$stmt = $mysqli->prepare("INSERT INTO users(username, password_hash, email, last_login_date) VALUES (?,?,?, NOW())");
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Bind parameters
		$stmt->bind_param("sss", $user->getUsername(), $user->getPasswordHash(), $user->getEmail());
		
		//Execute statement
		$stmt->execute();
		
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->close();
		unset($stmt);
		
		return "User created successfully.";
	}
	
	//Changes password or email
	public static function updateUser(User $user) {
		if(User::getUser($user->username) == null) {
			throw new Exception("Error: User not found!");
		}
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$stmt = $mysqli->prepare("UPDATE users SET password_hash = ?, email = ? WHERE user_id = ?");
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Bind parameters
		$stmt->bind_param("ssi", $user->getPasswordHash(), $user->getEmail(), $user->getUserId());
		
		//Execute statement
		$stmt->execute();
		
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->close();
		unset($stmt);
		
		return "User data updated successfully.";
	}
	
	public static function updateLoginDate(User $user) {
		if(User::getUser($user->username) == null) {
			throw new Exception("Error: User not found!");
		}
		
		//Grab connection object
		$mysqli = Utility::getSQLConnection();
		
		$stmt = $mysqli->prepare("UPDATE users SET last_login_date = NOW() WHERE user_id = ?");
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		//Bind parameters
		$stmt->bind_param("i", $user->getUserId());
		
		//Execute statement
		$stmt->execute();
		
		if ($mysqli->errno) {
			trigger_error($mysqli->error,E_USER_ERROR);
		}
		
		$stmt->close();
		unset($stmt);
		
		return "User login date updated.";
	}
	
	//Object Functions
	public function setPassword($password) {
		if(strlen($password) < 6) {
			throw new Exception("Error: Password is too short (6 chars min).");
		}
		if(strlen($password) > 72) {
			throw new Exception("Error: Password is too long (72 chars max).");
		}
		
		$hasher = Utility::getPasswordHasher();
		$hash = $hasher->HashPassword($password);
		$this->setPasswordHash($hash);
	}
	
	public function checkPassword($password) {
		$hasher = Utility::getPasswordHasher();
		return $hasher->CheckPassword($password, $this->passwordHash);
	}

	//Internal Data
	private $userId;
	private $username;
	private $passwordHash;
	private $email;
	private $joinedDate;
	private $lastLoginDate;
	
	
	//Getters/Setters
	public function getUserId(){
		return $this->userId;
	}
	public function setUserId($userId){
		$this->userId = (int)$userId;
	}

	public function getUsername(){
		return $this->username;
	}

	public function setUsername($username){
		if(strlen($username) === 0) {
			throw new Exception("Error: Username cannot be empty.");
		}
		if(strlen($username) > 16) {
			throw new Exception("Error: Username is too long (16 chars max).");
		}
		if(preg_match('/^(\w|-){1,16}$/', $username) === 0) {
			throw new Exception("Error: Invalid username (allowed characters: a-z,A-Z,0-9,-,_)");
		}
	
		$this->username = (string)$username;
	}

	public function getPasswordHash(){
		return $this->passwordHash;
	}
	
	public function setPasswordHash($passwordHash){
		$this->passwordHash = (string)$passwordHash;
	}

	public function getEmail(){
		return $this->email;
	}

	public function setEmail($email){
		if(strlen($email) > 0 && !preg_match("/[^\\s]*@[a-z0-9.-]*/i", $email)) {
			throw new Exception("Error: Invalid email address.");
		}
		$this->email = (string)$email;
	}

	public function getJoinedDate(){
		return $this->joinedDate;
	}

	public function setJoinedDate($joinedDate){
		$this->joinedDate = $joinedDate;
	}

	public function getLastLoginDate(){
		return $this->lastLoginDate;
	}

	public function setLastLoginDate($lastLoginDate){
		$this->lastLoginDate = $lastLoginDate;
	}
}
Back to Top