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.

100 lines
2.1 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,20}$/i", $login)) {
  22. return false;
  23. }
  24. if (!self::$user = self::getByLogin(strtolower($login)) ) {
  25. return false;
  26. }
  27. if (self::$user->pass != $password) {
  28. return false;
  29. }
  30. self::setSession();
  31. return true;
  32. }
  33. static function logout()
  34. {
  35. Env::setCookie(session_name(), '', 0);
  36. Env::setCookie('login', '', 0);
  37. Env::setCookie('login_hash', '', 0);
  38. if (session_id()) {
  39. session_destroy();
  40. }
  41. }
  42. static function process()
  43. {
  44. if (Env::getCookie(session_name())) { //есть сессия
  45. session_start();
  46. } elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
  47. self::remember();
  48. }
  49. }
  50. static function setSession()
  51. {
  52. Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
  53. Env::setCookie('login_hash', self::getHash(), TIME_NOW + LOGIN_COOKIE_TTL);
  54. session_start();
  55. $_SESSION['user'] = self::$user;
  56. }
  57. static function remember()
  58. {
  59. if (! self::$user = self::getByLogin(Env::getCookie('login'))) {
  60. self::logout();
  61. }
  62. if (Env::getCookie('login_hash') == self::getHash()) {
  63. self::setSession();
  64. } else {
  65. self::logout();
  66. }
  67. }
  68. static function getHash()
  69. {
  70. return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->pass.'dai');
  71. }
  72. static function getInfo()
  73. {
  74. return Env::Session('user', self::$user);
  75. }
  76. static function isGuest()
  77. {
  78. return ! (bool) Env::Session('user');
  79. }
  80. static function getByLogin($login)
  81. {
  82. return Load::model('UserData')->getByLogin($login);
  83. }
  84. }
  85. ?>