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.

138 lines
3.0 KiB

<?php
/**
* Класс для работы с пользователями
*
* @copyright
* @link
* @package Majestic
* @subpackage Decorator
* @since
* @version SVN: $Id$
* @filesource $URL$
*/
class User
{
static private $user = false;
static function login($login, $password)
{
if (empty($login) || empty($password)) {
return false;
}
if(!preg_match(UserData::REGEXP_LOGIN, $login)) {
return false;
}
self::setInfo(self::getByLogin($login));
if (!self::getInfo() || self::$user->isBanned()) {
return false;
}
if(self::$user->pass != $password){
return false;
}
self::setSession();
$model = new UserDataModel();
$model->loginUpdate($login);
return true;
}
static function logout()
{
Env::setCookie(session_name(), '', 0);
Env::setCookie('login', '', 0);
Env::setCookie('login_hash', '', 0);
if (session_id()) {
session_destroy();
}
}
static function process()
{
if (Env::getCookie(session_name())) { //есть сессия
@session_start();
self::setInfo(Env::Session('user'));
} elseif (Env::getCookie('login') && Env::getCookie('login_hash')) {
self::remember();
}
}
static function setSession()
{
Env::setCookie('login', self::$user->login, TIME_NOW + LOGIN_COOKIE_TTL);
Env::setCookie('login_hash', self::getHash(), TIME_NOW + LOGIN_COOKIE_TTL);
@session_start();
$_SESSION['user'] = self::$user;
}
static function remember()
{
self::setInfo(self::getByLogin(Env::getCookie('login')));
if (!self::getInfo()) {
self::logout();
}
if (Env::getCookie('login_hash') == self::getHash()) {
self::setSession();
} else {
self::logout();
}
}
static function getHash()
{
return md5(self::$user->id.'hckrz'.self::$user->login.'mst'.self::$user->pass.'dai');
}
static function getInfo()
{
return Env::Session('user', self::$user);
}
static function setInfo($data)
{
self::$user = $data;
}
static function isGuest()
{
return ! (bool) Env::Session('user');
}
static function getByLogin($login)
{
$model = new UserDataModel();
return $model->getByLogin($login);
}
static function getById($id)
{
$model = new UserDataModel();
return $model->getById($id);
}
/**
* отправляет письмо для активации
*
* @param string $mail
* @param string $login
*
*/
static function sendActivateMail($mail, $login)
{
$model = new UserActivateModel();
$link = 'http://'.Env::Server('SERVER_NAME').'/activate/?key=' . $model->generateKey($login, $mail);
$mailer = new Mailer('UserRegister');
return $mailer->send($mail, "Подтверждени E-mail ".Env::Server('SERVER_NAME'), array('link' => $link, 'site_name' => Env::Server('SERVER_NAME')));
}
}
?>