Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. <?php
  2. class Accounts {
  3. private static $userdata = false;
  4.  
  5. public static function userExists($id) {
  6.  
  7. }
  8.  
  9. public static function getUserData($id, $override = true) {
  10. if(self::$userdata === false || $override) {
  11. $q = dibi::query("SELECT id, status, username, email, password_hash FROM accounts WHERE id = %i", $id);
  12. self::$userdata = $q->fetch();
  13. }
  14. return self::$userdata;
  15. }
  16.  
  17. public static function getCurrentUserId() {
  18. $q = dibi::query("SELECT user_id FROM sessions WHERE hash = %s", Session::getSession());
  19. return $q->fetchSingle();
  20. }
  21.  
  22. public static function getIdByUsername($username) {
  23. $q = dibi::query("SELECT id FROM accounts WHERE username = %s", $username);
  24. return $q->fetchSingle();
  25. }
  26.  
  27. public static function getIdByEmail($email) {
  28. $q = dibi::query("SELECT id FROM accounts WHERE email = %s", $email);
  29. return $q->fetchSingle();
  30. }
  31.  
  32. public static function getIdByRefAndHash($ref, $hash) {
  33. $q = dibi::query("SELECT id FROM accounts WHERE activate_hash = %s", $hash);
  34. return $q->fetchSingle();
  35. }
  36.  
  37. public static function verifyUserCredentials($email, $password) {
  38. $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE email = %s", $email, " AND password_hash = %s", password($password));
  39. return (intval($q->fetchSingle()) > 0);
  40. }
  41.  
  42. public static function verifyAccountIsActive($email) {
  43. $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE status = 1 AND email = %s", $email);
  44. return (intval($q->fetchSingle()) > 0);
  45. }
  46.  
  47. public static function emailTaken($email) {
  48. $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE email = %s", $email);
  49. return (intval($q->fetchSingle()) !== 0);
  50. }
  51.  
  52. public static function createAccount($email, $password) {
  53. $hash = generate_hash();
  54. dibi::query("INSERT INTO accounts (email, password_hash, activate_hash, activate_ref) VALUES (%s", $email,", %s", password($password),", %s", $hash, ",%s", sha1($hash),")");
  55. }
  56.  
  57. public static function activateAccount($hash, $ref) {
  58. $db_hash = dibi::query("SELECT activate_hash FROM accounts where activate_ref = %s", $ref);
  59. $db_hash = $db_hash->fetchSingle();
  60. if($db_hash === false) return false;
  61. if($db_hash === $hash) {
  62. dibi::query('UPDATE accounts SET status = 1 WHERE activate_ref = %s', $ref, ' AND [accounts.activate_hash] = %s', $hash);
  63. $id = dibi::query("SELECT id FROM accounts where activate_ref = %s", $ref);
  64.  
  65. $event = new EventFactory();
  66. $event->setEvent('ActivateAccount');
  67. $event->addParameter('id', $id->fetchSingle());
  68. $event->createEvent();
  69.  
  70. return true;
  71. }
  72. }
  73.  
  74. public static function deactivateAccount($id) {
  75. dibi::query('UPDATE accounts SET status = 0 WHERE id = %i', $id);
  76.  
  77. $event = new EventFactory();
  78. $event->setEvent('DeactivateAccount');
  79. $event->addParameter('id', $id);
  80. $event->createEvent();
  81.  
  82. return true;
  83. }
  84.  
  85. public static function updateUsername($id, $username) {
  86. if(dibi::query("UPDATE accounts SET username = %s", $username, " WHERE id = %i", $id)) {
  87. return true;
  88. }
  89. return false;
  90. }
  91.  
  92. public static function updatePassword($id, $password, $revovery = false) {
  93. $password = password($password);
  94. if(dibi::query("UPDATE accounts SET password_hash = %s", $password, " WHERE id = %i", $id)) {
  95. $event = new EventFactory();
  96.  
  97. $event->setEvent('ChangePassword');
  98. $event->addParameter('id', $id);
  99. $event->addParameter('password', $password);
  100. $event->createEvent();
  101.  
  102. return true;
  103. }
  104. return false;
  105. }
  106.  
  107. public static function updateEmail($id, $email) {
  108. if(self::getIdByEmail($email) !== false) return false;
  109. if(dibi::query("UPDATE accounts SET email = %s", $email, " WHERE id = %i", $id)) {
  110. $event = new EventFactory();
  111.  
  112. $event->setEvent('ChangeEmail');
  113. $event->addParameter('id', $id);
  114. $event->addParameter('email', $email);
  115. $event->createEvent();
  116.  
  117. return true;
  118. }
  119. return false;
  120. }
  121.  
  122. public static function regenerateHash($id) {
  123. $hash = generate_hash();
  124. $ref = sha1($hash);
  125.  
  126. dibi::query("UPDATE accounts SET activate_ref = %s", $ref, ", activate_hash = %s", $hash, " WHERE id = %i", $id);
  127.  
  128. return [$hash, $ref];
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement