Guest User

Untitled

a guest
Jul 15th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2.  
  3. class Auth {
  4.  
  5. protected static $config;
  6. protected static $current_user;
  7. protected static $instance;
  8.  
  9. public static function instance()
  10. {
  11. if ( ! isset(Auth::$instance))
  12. Auth::$instance = new Auth;
  13.  
  14. return Auth::$instance;
  15. }
  16.  
  17. public function init()
  18. {
  19. Auth::$config = Kohana::config('auth');
  20. Auth::$current_user = ORM::factory('user', Session::instance()->get(Auth::$config['session_key']));
  21. }
  22.  
  23. public function load_user()
  24. {
  25. if (Auth::$current_user->loaded())
  26. return Auth::$current_user;
  27.  
  28. return FALSE;
  29. }
  30.  
  31. public function login($account, $unique_key, $password, $remember = FALSE)
  32. {
  33. $user = ORM::factory('user')->where(array(
  34. Auth::$config['unique_key'] => $unique_key,
  35. 'password' => Auth::hash_password($password)
  36. ))->find();
  37.  
  38. if ($user->loaded())
  39. {
  40. $session->regenerate();
  41. Session::instance()->set(Auth::$config['session_key'], $user->id);
  42.  
  43. return $user;
  44. }
  45.  
  46. return FALSE;
  47. }
  48.  
  49. public static function logout($destroy = TRUE)
  50. {
  51. if ($destroy === TRUE)
  52. {
  53. Session::instance()->destroy();
  54. }
  55. else
  56. {
  57. Session::instance()->delete($this->config['session_key']);
  58. Session::instance()->regenerate();
  59. }
  60. }
  61.  
  62. public static function hash_password($password)
  63. {
  64. return hash(Auth::$config['hash_method'], $password.$this->salt($password));
  65. }
  66.  
  67. private function salt($password)
  68. {
  69. $salt = '';
  70. $salt_pattern = preg_split('/,\s*/', $this->config['salt_pattern']);
  71.  
  72. foreach ($salt_pattern as $i => $offset)
  73. $salt .= substr($password, $offset + $i, 1);
  74.  
  75. return $salt;
  76. }
  77.  
  78. public function auto_login()
  79. {
  80. if ($token = cookie::get('authautologin'))
  81. {
  82. // Load the token and user
  83. $token = ORM::factory('user_token', array('token' => $token));
  84.  
  85. if ($token->loaded() AND $token->user->loaded())
  86. {
  87. if ($token->user_agent === sha1(Request::$user_agent))
  88. {
  89. // Save the token to create a new unique token
  90. $token->save();
  91.  
  92. // Set the new token
  93. cookie::set('authautologin', $token->token, $token->expires - time());
  94.  
  95. // Complete the login with the found data
  96. $this->complete_login($token->user);
  97.  
  98. // Automatic login was successful
  99. return TRUE;
  100. }
  101.  
  102. // Token is invalid
  103. $token->delete();
  104. }
  105. }
  106.  
  107. return FALSE;
  108. }
  109.  
  110. public static function generate_password($length = 8)
  111. {
  112. $password = '';
  113.  
  114. $chars = "023456789bcdfghjkmnpqrstvwxyz";
  115. $i = 0;
  116. while ($i < $length)
  117. {
  118. $char = substr($chars, mt_rand(0, strlen($chars)-1), 1);
  119.  
  120. if (!strstr($password, $char))
  121. {
  122. $password .= $char;
  123. $i++;
  124. }
  125. }
  126. return $password;
  127. }
  128. }
Add Comment
Please, Sign In to add comment