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.

96 lines
2.0 KiB

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