You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.8 KiB

  1. <?php
  2. /**
  3. * Класс пользователя
  4. *
  5. */
  6. class User
  7. {
  8. static private $user = false;
  9. static function login($login, $password)
  10. {
  11. if (empty($login)) {
  12. return false;
  13. }
  14. if (! self::$user = self::getByLogin($login) ) {
  15. return false;
  16. }
  17. if (self::$user->password != md5($password)) {
  18. return false;
  19. }
  20. self::setSession();
  21. return true;
  22. }
  23. static function logout()
  24. {
  25. Env::setCookie(session_name(), '', 0);
  26. Env::setCookie('login', '', 0);
  27. Env::setCookie('login_hash', '', 0);
  28. if (session_id()) {
  29. session_destroy();
  30. }
  31. }
  32. static function process()
  33. {
  34. if (Env::getCookie(session_name())) { //есть сессия
  35. session_start();
  36. } elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
  37. self::remember();
  38. }
  39. }
  40. static function setSession()
  41. {
  42. Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
  43. Env::setCookie('login_hash', self::getHash(), TIME_NOW + LOGIN_COOKIE_TTL);
  44. session_start();
  45. $_SESSION['user'] = self::$user;
  46. }
  47. static function remember()
  48. {
  49. if (! self::$user = self::getByLogin(Env::getCookie('login'))) {
  50. self::logout();
  51. }
  52. if (Env::getCookie('login_hash') == self::getHash()) {
  53. self::setSession();
  54. } else {
  55. self::logout();
  56. }
  57. }
  58. static function getHash()
  59. {
  60. return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->password.'dai');
  61. }
  62. static function getInfo()
  63. {
  64. return Env::Session('user', self::$user);
  65. }
  66. static function isGuest()
  67. {
  68. return ! (bool) Env::Session('user');
  69. }
  70. static function getByLogin($login)
  71. {
  72. return Load::model('UserData')->getByLogin($login);
  73. }
  74. }
  75. ?>