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.

118 lines
2.6 KiB

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