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.

117 lines
2.5 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) || empty($password)) {
  19. return false;
  20. }
  21. if(!preg_match("/^[a-z0-9_-]{3,50}$/i", $login)) {
  22. return false;
  23. }
  24. self::setInfo(self::getByLogin(strtolower($login)));
  25. if (!self::getInfo()) {
  26. return false;
  27. }
  28. if (stripslashes(self::$user->pass) != $password) {
  29. return false;
  30. }
  31. self::setSession();
  32. Load::model('UserData')->loginUpdate($login);
  33. return true;
  34. }
  35. static function logout()
  36. {
  37. Env::setCookie(session_name(), '', 0);
  38. Env::setCookie('login', '', 0);
  39. Env::setCookie('login_hash', '', 0);
  40. if (session_id()) {
  41. session_destroy();
  42. }
  43. }
  44. static function process()
  45. {
  46. if (Env::getCookie(session_name())) { //есть сессия
  47. session_start();
  48. self::setInfo(Env::Session('user'));
  49. } elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
  50. self::remember();
  51. }
  52. }
  53. static function setSession()
  54. {
  55. Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
  56. Env::setCookie('login_hash', self::getHash(), 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. return Load::model('UserData')->getByLogin($login);
  91. }
  92. static function getById($id)
  93. {
  94. return Load::model('UserData')->getById($id);
  95. }
  96. }
  97. ?>