Advertisement
Guest User

User.php

a guest
Apr 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. <?php
  2. require_once("Db.php");
  3. require_once("Security.php");
  4. class User{
  5. private $username;
  6. private $email;
  7. private $password;
  8. private $passwordConfirmation;
  9. private $firstname;
  10. private $lastname;
  11. private $image;
  12. private $description;
  13. /**
  14. * @return username
  15. */
  16. public function getUsername()
  17. {
  18. return $this->username;
  19. }
  20. /**
  21. * @param $username
  22. */
  23. public function setUsername($username)
  24. {
  25. $this->username = $username;
  26. }
  27. /**
  28. * @return email
  29. */
  30. public function getEmail()
  31. {
  32. return $this->email;
  33. }
  34. /**
  35. * @param $email
  36. */
  37. public function setEmail($email)
  38. {
  39. $this->email = $email;
  40. }
  41. /**
  42. * @return password
  43. */
  44. public function getPassword()
  45. {
  46. return $this->password;
  47. }
  48. /**
  49. * @param $password
  50. */
  51. public function setPassword($password)
  52. {
  53. $this->password = $password;
  54. }
  55. /**
  56. * @return passwordConfirmation
  57. */
  58. public function getPasswordConfirmation()
  59. {
  60. return $this->passwordConfirmation;
  61. }
  62. /**
  63. * @param $passwordConfirmation
  64. */
  65. public function setPasswordConfirmation($passwordConfirmation)
  66. {
  67. $this->passwordConfirmation = $passwordConfirmation;
  68. }
  69. /**
  70. * @return firstname
  71. */
  72. public function getFirstname()
  73. {
  74. return $this->firstname;
  75. }
  76. /**
  77. * @param $firstname
  78. */
  79. public function setFirstname($firstname)
  80. {
  81. $this->firstname = $firstname;
  82. }
  83. /**
  84. * @return lastname
  85. */
  86. public function getLastname()
  87. {
  88. return $this->lastname;
  89. }
  90. /**
  91. * @param $lastname
  92. */
  93. public function setLastname($lastname)
  94. {
  95. $this->lastname = $lastname;
  96. }
  97. /**
  98. * @return image
  99. */
  100. public function getImage()
  101. {
  102. return $this->image;
  103. }
  104. /**
  105. * @param $image
  106. */
  107. public function setImage($image)
  108. {
  109. $this->image = $image;
  110. }
  111. /**
  112. * @return description (bio)
  113. */
  114. public function getDescription()
  115. {
  116. return $this->description;
  117. }
  118. /**
  119. * @param $description
  120. */
  121. public function setDescription($description)
  122. {
  123. $this->description = $description;
  124. }
  125. /**
  126. * @return boolean - true if successful, false if unsuccessful
  127. */
  128. public function register(){
  129. $hash = Security::hash($this->password);
  130. try{
  131. $pdo = Db::getConnection();
  132. $statement = $pdo->prepare("insert into user (firstname, lastname, username, email, password) values (:firstname,:lastname,:username,:email,:password)");
  133. $statement->bindParam(":firstname", $this->firstname);
  134. $statement->bindParam(":lastname", $this->lastname);
  135. $statement->bindParam(':username', $this->username);
  136. $statement->bindParam(":email", $this->email);
  137. $statement->bindParam(":password", $hash);
  138. $result = $statement->execute();
  139. return $result;
  140. }
  141. catch( Throwable $t){
  142. $err = $t->getMessage();
  143. //Write this error to errorLog.txt file
  144. $file = fopen("errorLog.txt", "a");
  145. fwrite($file, $err."\n");
  146. fclose($file);
  147. }
  148. }
  149. /*
  150. * Returns true if length of a string is longer than given allowedLength
  151. */
  152. public static function maxLength($string, $maxLength){
  153. if( strlen($string) > $maxLength){
  154. //String is too long, return true for error handling
  155. return true;
  156. }
  157. else{
  158. return false;
  159. }
  160. }
  161. /*
  162. * Returns true if length of a string is shorter than given allowedLength
  163. */
  164. public static function minLength($string, $minLength){
  165. if( strlen($string) < $minLength){
  166. //String is too short, return true for error handling
  167. return true;
  168. }
  169. else{
  170. return false;
  171. }
  172. }
  173. /*
  174. * Find a user based on email addres
  175. */
  176. public static function findByEmail($email){
  177. $conn = Db::getConnection();
  178. $statement = $conn->prepare("select * from user where email = :email limit 1");
  179. $statement->bindParam(":email", $email);
  180. $statement->execute();
  181. return $statement->fetch(PDO::FETCH_ASSOC);
  182. }
  183. //Check if a user exists by email address
  184. public static function isEmailAvailable($email){
  185. $result = self::findByEmail($email);
  186. // PDO returns false if no records are found so let's check for that
  187. if($result == false){
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. /*
  194. * Find a user based on username
  195. */
  196. public static function findByUsername($username){
  197. $conn = Db::getConnection();
  198. $statement = $conn->prepare("select * from user where username = :username limit 1");
  199. $statement->bindParam(":username", $username);
  200. $statement->execute();
  201. return $statement->fetch(PDO::FETCH_ASSOC);
  202. }
  203. //Check if a user exists by username
  204. public static function isUsernameAvailable($username){
  205. $result = self::findByUsername($username);
  206. // PDO returns false if no records are found so let's check for that
  207. if($result == false){
  208. return true;
  209. } else {
  210. return false;
  211. }
  212. }
  213. public static function getUserId(){
  214. //Get email of loggedin user via session
  215. $sessionEmail = $_SESSION['email'];
  216. //Get the ID of current user
  217. $conn = Db::getConnection();
  218. $statement = $conn->prepare("select id from user where email = :sessionEmail");
  219. $statement->bindParam(":sessionEmail", $sessionEmail);
  220. $statement->execute();
  221. $user_id = $statement->fetch(PDO::FETCH_ASSOC);
  222. $user_id = $user_id['id'];
  223. return $user_id;
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement