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.

116 lines
2.5 KiB

  1. <?php
  2. /**
  3. * Класс для работы с пользователями
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link
  7. * @package Majestic
  8. * @subpackage Decorator
  9. * @since
  10. */
  11. class User
  12. {
  13. static protected $user = false;
  14. static function login($login, $password)
  15. {
  16. if (empty($login) || empty($password)) {
  17. return false;
  18. }
  19. if(!preg_match(UserData::REGEXP_LOGIN, $login)) {
  20. return false;
  21. }
  22. self::setInfo(self::getByLogin($login));
  23. if (!self::getInfo()) {
  24. return false;
  25. }
  26. if(self::$user->pass != $password){
  27. return false;
  28. }
  29. self::setSession();
  30. return true;
  31. }
  32. static function logout()
  33. {
  34. Env::setCookie(session_name(), '', 0);
  35. Env::setCookie('login', '', 0);
  36. Env::setCookie('login_hash', '', 0);
  37. if (session_id()) {
  38. session_destroy();
  39. }
  40. }
  41. static function process()
  42. {
  43. if (Env::getCookie(session_name())) { //есть сессия
  44. @session_start();
  45. self::setInfo(Env::Session('user'));
  46. } elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
  47. self::remember();
  48. }
  49. }
  50. static function setSession()
  51. {
  52. $hash = self::getHash();
  53. Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
  54. Env::setCookie('login_hash', $hash, TIME_NOW + LOGIN_COOKIE_TTL);
  55. @session_start();
  56. $_SESSION['user'] = self::$user;
  57. }
  58. static function remember()
  59. {
  60. self::setInfo(self::getByLogin(Env::getCookie('login')));
  61. if (!self::getInfo()) {
  62. self::logout();
  63. }
  64. if (Env::getCookie('login_hash') == self::getHash()) {
  65. self::setSession();
  66. } else {
  67. self::logout();
  68. }
  69. }
  70. static function getHash()
  71. {
  72. return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->pass.'dai');
  73. }
  74. static function getInfo()
  75. {
  76. return Env::Session('user', self::$user);
  77. }
  78. static function setInfo($data)
  79. {
  80. self::$user = $data;
  81. }
  82. static function isGuest()
  83. {
  84. return ! (bool) Env::Session('user');
  85. }
  86. static function getByLogin($login)
  87. {
  88. $model = new UserDataModel();
  89. return $model->getByLogin($login);
  90. }
  91. static function getById($id)
  92. {
  93. $model = new UserDataModel();
  94. return $model->getById($id);
  95. }
  96. }