Файл: system/classes/User.php
Строк: 79
<?php
class User
{
/**
* Объект с данными юзера.
*
* @var object
*/
private $data;
/**
* Идентификатор авторизованности.
*
* @var boolean
*/
private $session;
/**
* Аутентификация.
*/
public function __construct()
{
global $db;
$session = $this->getSession();
if (!empty($session)) {
$stmt = $db->prepare("SELECT user_id, token FROM users_sessions WHERE user_id = :user_id AND token = :token");
$stmt->bindParam(':user_id', $session->user_id, PDO::PARAM_INT);
$stmt->bindParam(':token', $session->token, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
if (@$result->user_id == $session->user_id && @$result->token == $session->token) {
$this->session = true;
$stmt = $db->prepare("SELECT * FROM users WHERE user_id = :user_id");
$stmt->bindParam(':user_id', $session->user_id, PDO::PARAM_INT);
$stmt->execute();
$this->data = $stmt->fetch(PDO::FETCH_OBJ);
} else {
$this->session = false;
setcookie('__session', null, -1, '/');
unset($_SESSION['__session']);
}
} else {
$this->session = false;
setcookie('__session', null, -1, '/');
unset($_SESSION['__session']);
}
}
/**
* Получение данных из сессии.
*
* @return object|boolean
*/
public function getSession()
{
if (isset($_COOKIE['__session'])) {
$session = $_COOKIE['__session'];
} elseif (isset($_SESSION['__session'])) {
$session = $_SESSION['__session'];
}
if (isset($session)) {
$session = json_decode(base64_decode($session));
$session->user_id = base64_decode($session->user_id);
$session->token = base64_decode($session->token);
return $session;
} else {
return false;
}
}
/**
* Идентификатор авторизованности.
*
* @return boolean
*/
public function isAuth()
{
return $this->session;
}
/**
* Получение данных пользователя.
*
* @param integer $id
* @return mixed
*/
public function get($id = 0)
{
global $db;
if ($id && $id != @$this->data->id) {
$stmt = $db->prepare("SELECT * FROM users WHERE user_id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
} else {
return $this->data;
}
}
public function admin()
{
global $db;
$id = @$this->data->id;
$stmt = $db->prepare('SELECT * FROM users WHERE user_id = ? and role = ?');
$stmt -> execute([$id,1]);
$stmt = $stmt -> rowCount();
return 1;
}
}