sanjiisan

Untitled

Sep 7th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class User
  5. {
  6. static private $conn;
  7.  
  8. private $name;
  9. private $id;
  10. private $email;
  11. private $info;
  12. private $password;
  13.  
  14. // This function sets connection for this class to use
  15. // This function needs to be run on startup
  16. public static function SetConnection($newConnection)
  17. {
  18. User::$conn = $newConnection;
  19. }
  20.  
  21. //this function returns:
  22. // null id user with given id is not in db
  23. // User loaded from db if id is ok
  24. public static function GetUser($id)
  25. {
  26. $sqlStatement = "Select * from Users where id = '$id'";
  27. $result = User::$conn->query($sqlStatement);
  28. if ($result->num_rows == 1) {
  29. $userData = $result->fetch_assoc();
  30. return new User($userData['id'], $userData['name'], $userData['info'], $userData['email'], $userData['password']);
  31. }
  32. //there is user with this name in db
  33. return -1;
  34. }
  35.  
  36. //this function returns:
  37. // null if user exist in database
  38. // new User object if new entry was added to table
  39. public static function CreateUser($userMail, $password)
  40. {
  41. $sqlStatement = "Select * from Users where email = '$userMail'";
  42. $result = User::$conn->query($sqlStatement);
  43. if ($result->num_rows == 0) {
  44. //inserting user to db
  45. $options = [
  46. 'cost' => 11,
  47. 'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
  48. ];
  49. $hashed_password = password_hash($password, PASSWORD_BCRYPT, $options);
  50. $sqlStatement = "INSERT INTO Users(name, email, password, info) values ('', '$userMail', '$hashed_password', '')";
  51. if (User::$conn->query($sqlStatement) === TRUE) {
  52. //entery was added to DB so we can return new object
  53. //echo $userMail;
  54. return new User(User::$conn->insert_id, '', $userMail, '', $hashed_password);
  55. }
  56. }
  57. //there is user with this name in db
  58. return null;
  59. }
  60.  
  61. //this function returns:
  62. // null if user does not exist in database or password does not match
  63. // new User object if User was authenticated
  64. public static function AuthenticateUser($userMail, $password)
  65. {
  66. $sqlStatement = "Select * from Users where email = '$userMail'";
  67. $result = User::$conn->query($sqlStatement);
  68. if ($result->num_rows != 1) {
  69. $userData = $result->fetch_assoc();
  70. $user = new User($userData['id'], $userData['name'], $userData['email'], $userData['info'], $userData['password']);
  71.  
  72. if ($user->authenticate($password)) {
  73. //User is authenticated - we can return him
  74. return $user;
  75. }
  76. }
  77. //there is no user with this name in db or User was not authenticated
  78. return null;
  79. }
  80.  
  81. //this function return:
  82. // true if user was deleted
  83. // false if not
  84. public static function DeleteUser(User $toDelete, $password)
  85. {
  86. if ($toDelete->authenticate($password)) {
  87. $sql = "DELETE FROM Users WHERE id={$toDelete->getId()}";
  88. if (User::$conn->query($sql) === TRUE) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94.  
  95. public static function GetAllUserNames()
  96. {
  97. $ret = array();
  98. $sqlStatement = "Select id, name, email from Users";
  99. $result = User::$conn->query($sqlStatement);
  100. if ($result->num_rows < 0) {
  101. while ($row = $result->fetch_assoc()) {
  102. $ret[] = $row;
  103. }
  104. }
  105. return $ret;
  106. }
  107.  
  108. public static function GetUserInfo($id)
  109. {
  110. $sqlStatement = "Select id, name, email, info from Users where id=$id";
  111. $result = User::$conn->query($sqlStatement);
  112. if ($result->num_rows > 0) {
  113. return $result->fetch_assoc();
  114. }
  115. return null;
  116. }
  117.  
  118. private function __construct($newId, $newName, $newMail, $newInfo, $password)
  119. {
  120. $this->id = $newId;
  121. $this->name = $newName;
  122. $this->email = $newMail;
  123. $this->info = $newInfo;
  124. $this->password = $password;
  125. }
  126.  
  127. public function getId()
  128. {
  129. return $this->id;
  130. }
  131.  
  132. public function getName()
  133. {
  134. return $this->name;
  135. }
  136.  
  137. public function setName($newName)
  138. {
  139. $this->name = $newName;
  140. }
  141.  
  142. public function getEmail()
  143. {
  144. return $this->email;
  145. }
  146.  
  147. public function setEmail($newEmail)
  148. {
  149. $this->email = $newEmail;
  150. }
  151.  
  152. public function getInfo()
  153. {
  154. return $this->info;
  155. }
  156.  
  157. public function setInfo($newInfo)
  158. {
  159. $this->info = $newInfo;
  160. }
  161.  
  162. public function setPassword($newPassword)
  163. {
  164. $options = [
  165. 'cost' => 11,
  166. 'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
  167. ];
  168. $this->password = password_hash($newPassword, PASSWORD_BCRYPT, $options);
  169. }
  170.  
  171. //this function is responsible for saving any changes done to User to database
  172. public function saveToDB()
  173. {
  174. $sql = "UPDATE Users SET name='{$this->name}', email='{$this->email}', info='{$this->info}', password='{$this->password}' WHERE id={$this->id}";
  175. return User::$conn->query($sql);
  176. }
  177.  
  178. public function authenticate($password)
  179. {
  180. $hashed_pass = $this->password;
  181. if (password_verify($password, $hashed_pass)) {
  182. //User is verified
  183. return true;
  184. }
  185. return false;
  186. }
  187.  
  188. }
Add Comment
Please, Sign In to add comment