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.

109 lines
2.3 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. if (!self::$user = self::getByLogin(strtolower($login)) ) {
  25. return false;
  26. }
  27. if (stripslashes(self::$user->pass) != $password) {
  28. return false;
  29. }
  30. if (!self::isActive(self::$user->id)) {
  31. return false;
  32. }
  33. self::setSession();
  34. return true;
  35. }
  36. static function logout()
  37. {
  38. Env::setCookie(session_name(), '', 0);
  39. Env::setCookie('login', '', 0);
  40. Env::setCookie('login_hash', '', 0);
  41. if (session_id()) {
  42. session_destroy();
  43. }
  44. }
  45. static function process()
  46. {
  47. if (Env::getCookie(session_name())) { //есть сессия
  48. session_start();
  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. if (! self::$user = self::getByLogin(Env::getCookie('login'))) {
  63. self::logout();
  64. }
  65. if (Env::getCookie('login_hash') == self::getHash()) {
  66. self::setSession();
  67. } else {
  68. self::logout();
  69. }
  70. }
  71. static function getHash()
  72. {
  73. return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->pass.'dai');
  74. }
  75. static function getInfo()
  76. {
  77. return Env::Session('user', self::$user);
  78. }
  79. static function isGuest()
  80. {
  81. return ! (bool) Env::Session('user');
  82. }
  83. static function getByLogin($login)
  84. {
  85. return Load::model('UserData')->getByLogin($login);
  86. }
  87. static function isActive($user_id)
  88. {
  89. return Load::model('UserActivate')->isActive($user_id);
  90. }
  91. }
  92. ?>