Advertisement
Guest User

Untitled

a guest
May 16th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: erhanlammar
  5. * Date: 23/04/16
  6. * Time: 10:13
  7. */
  8.  
  9. include_once("Db.class.php");
  10.  
  11. class User{
  12.  
  13. // todo: 1 private variabelen aanmaken voor firstname, lastname, ...
  14. private $_db;
  15. private $m_sUsername;
  16. private $m_sFirstname;
  17. private $m_sLastname;
  18. private $m_sEmail;
  19. private $m_sPassword;
  20. private $m_sPasswordconfirmation;
  21.  
  22. //private $m_sProfileimage;
  23. //private $m_susersid;
  24.  
  25. // todo: 2 getters & setters!
  26.  
  27. public function __set($p_sProperty, $p_vValue){
  28. switch($p_sProperty){
  29. case "Username":
  30. if(!empty($p_vValue)){
  31. $this->m_sUsername = $p_vValue;
  32. break;
  33. }else{
  34. //opvangen van leeg veld username.
  35. throw new exception("vergeet geen username in te vullen");
  36. }
  37. case "Firstname":
  38. if(!empty($p_vValue)){
  39. $this->m_sFirstname = $p_vValue;
  40. break;
  41. }else{
  42. //opvangen van leeg veld firstname.
  43. throw new exception("Uw voornaam hebben we echt wel nodig");
  44. }
  45. case "Lastname":
  46. if(!empty($p_vValue)){
  47. $this->m_sLastname = $p_vValue;
  48. break;
  49. }else{
  50. //opvangen van leeg veld lastname.
  51. throw new exception("Heeft u geen achternaam?");
  52. }
  53. case "Email":
  54. if(!empty($p_vValue)){
  55. $this->m_sEmail = $p_vValue;
  56. break;
  57. }else{
  58. //opvangen van leeg veld email.
  59. throw new exception("Wij hebben uw email nodig om u op de hoogte te houden");
  60. }
  61. case "Password":
  62. if(!empty($p_vValue)){
  63. $this->m_sPassword = $p_vValue;
  64. break;
  65. }else{
  66. //opvangen van leeg veld firstname.
  67. throw new exception("Zonder wachtwoord geen login");
  68. }
  69. case "Passwordconfirmation":
  70. if(!empty($p_vValue)){
  71. $this->m_sPasswordconfirmation = $p_vValue;
  72. break;
  73. }else{
  74. //opvangen van leeg veld firstname.
  75. throw new exception("Zonder wachtwoord geen login");
  76. }
  77. }
  78. }
  79. public function __get($p_sProperty){
  80. switch($p_sProperty){
  81. case "Username":
  82. return $this->m_sUsername;
  83. break;
  84. case "Firstname":
  85. return $this->m_sFirstname;
  86. break;
  87. case "Lastname":
  88. return $this->m_sLastname;
  89. break;
  90. case "Email":
  91. return $this->m_sEmail;
  92. break;
  93. case "Password":
  94. return $this->m_sPassword;
  95. break;
  96. case "Passwordconfirmation":
  97. return $this->m_sPasswordconfirmation;
  98. break;
  99. }
  100. }
  101.  
  102. private function checkPasswordConfirmation(){
  103. if($this->m_sPassword == $this->m_sPasswordconfirmation){
  104. return true;
  105. }else{
  106. throw new exception("wachtwoorden komen niet overeen");
  107. }
  108. }
  109.  
  110.  
  111. public function signup(){
  112. if(!$this->checkEmail()){
  113. throw new exception("Dit emailadres bestaat al neem een ander of ga naar login");
  114. }
  115. if(!$this->checkUsername()){
  116. throw new exception("De username die u gekozen heeft bestaat al!!");
  117. }
  118. if(!$this->checkPasswordConfirmation()){
  119. throw new exception("De registratie is niet correct verlopen. Check alles nog eens");
  120. }
  121. $conn = new PDO("mysql:host=localhost;dbname=IMDstagram", "root","");
  122. $options= ['cost' => 12];
  123. $this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options);
  124. $statement = $conn->prepare("INSERT INTO users(
  125. username,
  126. firstname,
  127. lastname,
  128. email,
  129. password
  130. )
  131. VALUES(
  132. :username,
  133. :firstname,
  134. :lastname,
  135. :email,
  136. :password
  137. )
  138. ");
  139. $statement->bindValue(":username", $this->m_sUsername);
  140. $statement->bindValue(":firstname", $this->m_sFirstname);
  141. $statement->bindValue(":lastname", $this->m_sLastname);
  142. $statement->bindValue(":email", $this->m_sEmail);
  143. $statement->bindValue(":password", $this->m_sPassword);
  144. return $statement->execute();
  145.  
  146. }
  147.  
  148. public function checkEmail(){
  149.  
  150. $PDO = Db::getInstance();
  151. $stmt = $PDO->prepare("SELECT * FROM users WHERE email= :email");
  152. $stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR);
  153. $stmt->execute();
  154.  
  155. if( $stmt->rowCount() > 0 ){
  156. return false;
  157. throw new exception( "" ) ;
  158. }
  159. else{
  160.  
  161. return true;
  162.  
  163. }
  164. }
  165. public function checkUsername(){
  166.  
  167. $PDO = Db::getInstance();
  168. $stmt = $PDO->prepare("SELECT * FROM users WHERE username= :username");
  169. $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
  170. $stmt->execute();
  171.  
  172. if( $stmt->rowCount() > 0 ){
  173. return false;
  174. throw new exception( "" ) ;
  175. }
  176. else{
  177. return true;
  178.  
  179. }
  180. }
  181.  
  182. public function loggingIn(){
  183. if(!empty($this->m_sUsername) && !empty($this->m_sPassword)){
  184. $PDO = Db::getInstance();
  185. $stmt = $PDO->prepare("SELECT * FROM users WHERE username = :username");
  186. $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
  187. $stmt->execute();
  188.  
  189. if($stmt->rowCount() > 0){
  190. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  191. $password = $this->m_sPassword;
  192. $hash = $result['password'];
  193.  
  194. if(password_verify($password, $hash)){
  195. session_start();
  196. $_SESSION["loggedIn"] = $result['usersid'];
  197. session_write_close();
  198. return true;
  199. }else{
  200. return false;
  201. }
  202. }
  203. }
  204. }
  205. public function Update($userid){
  206. echo $userid;
  207. echo " UPDATE users SET username= '" . $this->m_sUsername . "', email = '" . $this->m_sEmail . "', password = '" . $this->m_sPassword . "' WHERE usersid = '" . $userid . "'";
  208. $PDO = Db::getInstance();
  209. if(!empty($this->m_sUsername) && !empty($this->m_sEmail) && !empty($this->m_sPassword) && !empty($this->m_sPasswordconfirmation)){
  210. if($this->m_sPassword == $this->m_sPasswordconfirmation){
  211. $options= ['cost' => 12];
  212. $this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options);
  213.  
  214. $stmt = $PDO->prepare("UPDATE users SET username= :username, email = :email, password = :password WHERE usersid = :usersid");
  215. $stmt->bindValue(":usersid", $userid, PDO::PARAM_INT);
  216. $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
  217. $stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR);
  218. $stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR);
  219. if($stmt->execute()){
  220. // success
  221. throw new exception("Je aanpassingen zijn doorgevoerd");
  222. }else{
  223. // error
  224. throw new exception("Je hebt ergens een fout ingevuld");
  225. }
  226. }else{
  227. // Mismatch password
  228. throw new exception("Wachtwoorden komen niet overeen");
  229. }
  230. }else{
  231. // some values are not set
  232. throw new exception("Je hebt enkele velden niet ingevuld");
  233. }
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement