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.

153 lines
3.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(UserData::REGEXP_LOGIN, $login)) {
  22. return false;
  23. }
  24. self::setInfo(self::getByLogin($login));
  25. if (!self::getInfo() || self::$user->isBanned()) {
  26. return false;
  27. }
  28. if(self::$user->pass != $password){
  29. return false;
  30. }
  31. self::setSession();
  32. $model = new UserDataModel();
  33. $model->loginUpdate($login);
  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. self::setInfo(Env::Session('user'));
  50. } elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
  51. self::remember();
  52. }
  53. }
  54. static function setSession()
  55. {
  56. Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
  57. Env::setCookie('login_hash', self::getHash(), TIME_NOW + LOGIN_COOKIE_TTL);
  58. @session_start();
  59. $_SESSION['user'] = self::$user;
  60. }
  61. static function remember()
  62. {
  63. self::setInfo(self::getByLogin(Env::getCookie('login')));
  64. if (!self::getInfo()) {
  65. self::logout();
  66. }
  67. if (Env::getCookie('login_hash') == self::getHash()) {
  68. self::setSession();
  69. } else {
  70. self::logout();
  71. }
  72. }
  73. static function getHash()
  74. {
  75. return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->pass.'dai');
  76. }
  77. static function getInfo()
  78. {
  79. return Env::Session('user', self::$user);
  80. }
  81. static function setInfo($data)
  82. {
  83. self::$user = $data;
  84. }
  85. static function isGuest()
  86. {
  87. return ! (bool) Env::Session('user');
  88. }
  89. static function getByLogin($login)
  90. {
  91. $model = new UserDataModel();
  92. return $model->getByLogin($login);
  93. }
  94. static function getById($id)
  95. {
  96. $model = new UserDataModel();
  97. return $model->getById($id);
  98. }
  99. /**
  100. * отправляет письмо для активации
  101. *
  102. * @param string $mail
  103. * @param string $login
  104. *
  105. */
  106. static function sendActivateMail($mail, $login)
  107. {
  108. $settings = Env::getParam('site_settings');
  109. $model = new UserActivateModel();
  110. $link = 'http://' . $settings['host_name'] . '/activate/?key=' . $model->generateKey($login, $mail);
  111. $text = new SettingsTextModel();
  112. $message = $text->getText('mail_activate');
  113. $replaces = array(
  114. 'link' => $link,
  115. 'host' => $settings['site_url'],
  116. 'email' => $mail,
  117. );
  118. if (!$message) {
  119. return false;
  120. }
  121. foreach ($replaces as $key => $val) {
  122. $message->text = str_replace('%' . $key . '%', $val, $message->text);
  123. }
  124. $mailer = new Mailer();
  125. return $mailer->sendMessage($mail, 'Подтверждение E-mail ' . $settings['host_name'], $message->text);
  126. }
  127. }
  128. ?>