Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. class Users {
  2. private static $key, $table, $logtable;
  3.  
  4. public static function init($key) {
  5. self::$key = $key;
  6. self::$table = DBPREFIX.$key.'s';
  7. self::$logtable = self::$table.'_access_log';
  8. }
  9.  
  10. public static function isLogged() {
  11. if(isset($_SESSION[self::$key])) {
  12. if(isset($_SESSION[self::$key]['uid'])) {
  13. $uid = intval($_SESSION[self::$key]['uid']);
  14. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  15. if(count($user) > 0) {
  16. return true;
  17. }
  18. else {
  19. return false;
  20. }
  21.  
  22. }
  23. else {
  24. return false;
  25. }
  26. }
  27. else {
  28. return false;
  29. }
  30. }
  31.  
  32. public static function userExist($login) {
  33. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  34. if(count($user) > 0) {
  35. return true;
  36. }
  37. else {
  38. return false;
  39. }
  40. }
  41.  
  42. public static function getUser($login) {
  43. $user = DB::select()->from(self::$table)->where('login', '=', $login)->execute()->object();
  44. if(count($user) > 0) {
  45. return $user[0];
  46. }
  47. else {
  48. return false;
  49. }
  50. }
  51.  
  52. public static function generateHash($password) {
  53. $salt = self::generateSalt();
  54. $hashedPassword = crypt($password, $salt);
  55. return array('hashed_password' => $hashedPassword, 'salt' => $salt);
  56. }
  57.  
  58. private static function generateSalt() {
  59. $blowfishPre = '$2y$10$';
  60. $blowfishEnd = '$';
  61. $allowedChars ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
  62. $charsLength = strlen($allowedChars);
  63. $saltLength = 20;
  64. $salt = "";
  65. for($i=0; $i < $saltLength; $i++)
  66. {
  67. $salt .= $allowedChars[mt_rand(0,$charsLength)];
  68. }
  69. $bcrypt_salt = $blowfishPre . $salt . $blowfishEnd;
  70. return $bcrypt_salt;
  71. }
  72.  
  73. public static function comparePasswords($input, $uid) {
  74. $user = DB::select()->from(self::$table)->where('id', '=', $uid)->execute()->object();
  75. if(count($user) > 0) {
  76. $user = $user[0];
  77. $password = $user->password;
  78. return crypt($input, $password) === $password;
  79. }
  80. else {
  81. return false;
  82. }
  83. }
  84.  
  85. public static function userExit() {
  86. if(isset($_SESSION[self::$key])) {
  87. unset($_SESSION[self::$key]);
  88. }
  89. }
  90.  
  91. public static function changePassword($password, $uid) {
  92. $newPassword = self::generateHash($password);
  93. if(isset($newPassword['hashed_password']) && isset($newPassword['salt'])) {
  94. DB::update(self::$table)->set(array('password' => $newPassword['hashed_password'], 'salt' => $newPassword['salt']))->where('id', '=', $uid)->execute();
  95. }
  96. }
  97.  
  98. public static function updateTime($uid) {
  99. DB::update(self::$table)->set(array('last_login' => time()))->where('id', '=', $uid)->execute();
  100. }
  101.  
  102. public static function writeAccessLog($uid, $success) {
  103. if($success) {
  104. DB::insert(self::$logtable)->set(array('admin_id' => $uid, 'date' => time(), 'success' => '1'))->execute();
  105. }
  106. else if(!$success) {
  107. DB::insert(self::$logtable)->set(array('admin_id' => $uid, 'date' => time(), 'success' => '0'))->execute();
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement