PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/User.php

https://github.com/liquidhot/md-master-server
PHP | 227 lines | 156 code | 53 blank | 18 comment | 21 complexity | 7ccaa88de5f4fcdef13b7d8206d017ec MD5 | raw file
  1. <?php
  2. require_once "Utility.php";
  3. class User {
  4. //Static Functions
  5. public static function getUser($username) {
  6. $user = new User();
  7. //Grab connection object
  8. $mysqli = Utility::getSQLConnection();
  9. //Prepare query to find user
  10. $stmt = $mysqli->prepare("SELECT user_id, username, password_hash, email, joined_date, last_login_date FROM users WHERE UPPER(username) = UPPER(?)");
  11. if ($mysqli->errno) {
  12. trigger_error($mysqli->error,E_USER_ERROR);
  13. }
  14. $stmt->bind_param("s", $username);
  15. //Run query
  16. $stmt->execute();
  17. if ($mysqli->errno) {
  18. $stmt->close();
  19. unset($stmt);
  20. trigger_error($mysqli->error,E_USER_ERROR);
  21. }
  22. $stmt->bind_result($userId, $username, $passwordHash, $email, $joinedDate, $lastLoginDate);
  23. $result = $stmt->fetch();
  24. $stmt->close();
  25. unset($stmt);
  26. if(!$result) { //Not found or error.
  27. return null;
  28. }
  29. //Fill user object
  30. $user->setUserId($userId);
  31. $user->setUsername($username);
  32. $user->setPasswordHash($passwordHash);
  33. $user->setEmail($email);
  34. $user->setJoinedDate($joinedDate);
  35. $user->setLastLoginDate($lastLoginDate);
  36. return $user;
  37. }
  38. public static function createUser(User $user) {
  39. if(User::getUser($user->username) !== null) {
  40. throw new Exception("Error: User already exists!");
  41. }
  42. //Grab connection object
  43. $mysqli = Utility::getSQLConnection();
  44. $stmt = $mysqli->prepare("INSERT INTO users(username, password_hash, email, last_login_date) VALUES (?,?,?, NOW())");
  45. if ($mysqli->errno) {
  46. trigger_error($mysqli->error,E_USER_ERROR);
  47. }
  48. //Bind parameters
  49. $stmt->bind_param("sss", $user->getUsername(), $user->getPasswordHash(), $user->getEmail());
  50. //Execute statement
  51. $stmt->execute();
  52. if ($mysqli->errno) {
  53. trigger_error($mysqli->error,E_USER_ERROR);
  54. }
  55. $stmt->close();
  56. unset($stmt);
  57. return "User created successfully.";
  58. }
  59. //Changes password or email
  60. public static function updateUser(User $user) {
  61. if(User::getUser($user->username) == null) {
  62. throw new Exception("Error: User not found!");
  63. }
  64. //Grab connection object
  65. $mysqli = Utility::getSQLConnection();
  66. $stmt = $mysqli->prepare("UPDATE users SET password_hash = ?, email = ? WHERE user_id = ?");
  67. if ($mysqli->errno) {
  68. trigger_error($mysqli->error,E_USER_ERROR);
  69. }
  70. //Bind parameters
  71. $stmt->bind_param("ssi", $user->getPasswordHash(), $user->getEmail(), $user->getUserId());
  72. //Execute statement
  73. $stmt->execute();
  74. if ($mysqli->errno) {
  75. trigger_error($mysqli->error,E_USER_ERROR);
  76. }
  77. $stmt->close();
  78. unset($stmt);
  79. return "User data updated successfully.";
  80. }
  81. public static function updateLoginDate(User $user) {
  82. if(User::getUser($user->username) == null) {
  83. throw new Exception("Error: User not found!");
  84. }
  85. //Grab connection object
  86. $mysqli = Utility::getSQLConnection();
  87. $stmt = $mysqli->prepare("UPDATE users SET last_login_date = NOW() WHERE user_id = ?");
  88. if ($mysqli->errno) {
  89. trigger_error($mysqli->error,E_USER_ERROR);
  90. }
  91. //Bind parameters
  92. $stmt->bind_param("i", $user->getUserId());
  93. //Execute statement
  94. $stmt->execute();
  95. if ($mysqli->errno) {
  96. trigger_error($mysqli->error,E_USER_ERROR);
  97. }
  98. $stmt->close();
  99. unset($stmt);
  100. return "User login date updated.";
  101. }
  102. //Object Functions
  103. public function setPassword($password) {
  104. if(strlen($password) < 6) {
  105. throw new Exception("Error: Password is too short (6 chars min).");
  106. }
  107. if(strlen($password) > 72) {
  108. throw new Exception("Error: Password is too long (72 chars max).");
  109. }
  110. $hasher = Utility::getPasswordHasher();
  111. $hash = $hasher->HashPassword($password);
  112. $this->setPasswordHash($hash);
  113. }
  114. public function checkPassword($password) {
  115. $hasher = Utility::getPasswordHasher();
  116. return $hasher->CheckPassword($password, $this->passwordHash);
  117. }
  118. //Internal Data
  119. private $userId;
  120. private $username;
  121. private $passwordHash;
  122. private $email;
  123. private $joinedDate;
  124. private $lastLoginDate;
  125. //Getters/Setters
  126. public function getUserId(){
  127. return $this->userId;
  128. }
  129. public function setUserId($userId){
  130. $this->userId = (int)$userId;
  131. }
  132. public function getUsername(){
  133. return $this->username;
  134. }
  135. public function setUsername($username){
  136. if(strlen($username) === 0) {
  137. throw new Exception("Error: Username cannot be empty.");
  138. }
  139. if(strlen($username) > 16) {
  140. throw new Exception("Error: Username is too long (16 chars max).");
  141. }
  142. if(preg_match('/^(\w|-){1,16}$/', $username) === 0) {
  143. throw new Exception("Error: Invalid username (allowed characters: a-z,A-Z,0-9,-,_)");
  144. }
  145. $this->username = (string)$username;
  146. }
  147. public function getPasswordHash(){
  148. return $this->passwordHash;
  149. }
  150. public function setPasswordHash($passwordHash){
  151. $this->passwordHash = (string)$passwordHash;
  152. }
  153. public function getEmail(){
  154. return $this->email;
  155. }
  156. public function setEmail($email){
  157. if(strlen($email) > 0 && !preg_match("/[^\\s]*@[a-z0-9.-]*/i", $email)) {
  158. throw new Exception("Error: Invalid email address.");
  159. }
  160. $this->email = (string)$email;
  161. }
  162. public function getJoinedDate(){
  163. return $this->joinedDate;
  164. }
  165. public function setJoinedDate($joinedDate){
  166. $this->joinedDate = $joinedDate;
  167. }
  168. public function getLastLoginDate(){
  169. return $this->lastLoginDate;
  170. }
  171. public function setLastLoginDate($lastLoginDate){
  172. $this->lastLoginDate = $lastLoginDate;
  173. }
  174. }