File size: 7.21Kb
<?PHP
class Users {
/*
Регистрация
*/
public function Register($_ARRAY = [], $CallBack = null) {
try {
/* Проверка логина */
$_ARRAY['login'] = TextClear($_ARRAY['login']);
if(empty($_ARRAY['login'])) {
throw new Exception(getLang('users_error_login'));
}
$_ARRAY['login'] = trim($_ARRAY['login']);
/* Проверка наличия логина */
if($this->isLogin($_ARRAY['login'])) {
throw new Exception(getLang('users_error_valid'));
}
/* Проверка имени */
$_ARRAY['first_name'] = TextClear($_ARRAY['first_name']);
if(empty($_ARRAY['first_name'])) {
throw new Exception(getLang('users_error_firstname'));
}
$_ARRAY['first_name'] = trim($_ARRAY['first_name']);
/* Проверка фамилии */
$_ARRAY['last_name'] = TextClear($_ARRAY['last_name']);
if(empty($_ARRAY['last_name'])) {
throw new Exception(getLang('users_error_lastname'));
}
$_ARRAY['last_name'] = trim($_ARRAY['last_name']);
$sth = pdo()->prepare('INSERT INTO `users`(`login`, `password`, `first_name`, `last_name`, `rights`, `approved`, `language`, `date_register`) VALUES (:login, :password, :first_name, :last_name, :rights, :approved, :language, :date_register)');
$sth->execute([
':login' => $_ARRAY['login'],
':password' => password_hash($_ARRAY['password'], PASSWORD_DEFAULT),
':first_name' => $_ARRAY['first_name'],
':last_name' => $_ARRAY['last_name'],
':rights' => (($this->rowCount() >= 1) ? 'z' : 'abcdefghijklmnopqrstuvwxyz'),
':approved' => (conf()->approved == '1') ? (($this->rowCount() >= 1) ? 0 : 1) : 1,
':language' => isset($_SESSION['lang']) ? $_SESSION['lang'] : 'ru',
':date_register' => time()
]);
if(isset($CallBack)) {
call_user_func($CallBack, pdo()->lastInsertId());
}
return true;
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Авториация
*/
public function Auth($_ARRAY = [], $CallBack = null) {
try {
$sth = pdo()->prepare('SELECT * FROM `users` WHERE `login`=:login LIMIT 1');
$sth->execute([
':login' => trim($_ARRAY['login'])
]);
if(!$sth->rowCount()) {
throw new Exception(getLang('users_message_login'));
}
$UserData = $sth->fetch(PDO::FETCH_OBJ);
if(password_verify($_ARRAY['password'], $UserData->password)) {
if($UserData->approved != '1') {
throw new Exception(getLang('users_message_approved'));
}
if(empty($_SESSION['id'])) {
$_SESSION['id'] = $UserData->id;
}
if(empty($UserData->date_password_change)) {
$sth = pdo()->prepare('UPDATE `users` SET `date_password_change`=:date_password_change WHERE `id`=:userid LIMIT 1');
$sth->execute([
':date_password_change' => time(),
':userid' => $_SESSION['id']
]);
}
if(isset($CallBack)) {
call_user_func($CallBack, $UserData);
}
$this->updateOnline();
return true;
}
return false;
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Обновление Токена пользователя
*/
public function updateToken($userid) {
try {
$sth = pdo()->prepare('UPDATE `users` SET `token`=:token WHERE `id`=:id LIMIT 1');
$sth->execute([
':token' => bin2hex(random_bytes(32)),
':id' => $userid
]);
return true;
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Обновление онлайна
*/
public function updateOnline($time = null) {
if(empty($_SESSION['id'])) {
return false;
}
try {
$sth = pdo()->prepare('UPDATE `users` SET `date_online`=:date_online WHERE `id`=:id LIMIT 1');
$sth->execute([
':date_online' => isset($time) ? $time : strtotime('+5 minutes'),
':id' => $_SESSION['id']
]);
return true;
}
catch(Exception $e) {
return false;
}
}
/*
Проверка валидности юзера
*/
public function IsValid($userid) {
$sth = pdo()->prepare('SELECT * FROM `users` WHERE `id`=:id LIMIT 1');
$sth->execute([':id' => $userid]);
return $sth->rowCount();
}
/*
Получение сведений пользователя
*/
public function Get($userid) {
try {
if(!$this->IsValid($userid)) {
return null;
}
$sth = pdo()->prepare('SELECT * FROM `users` WHERE `id`=:id LIMIT 1');
$sth->execute([
':id' => $userid
]);
return $sth->fetch(PDO::FETCH_OBJ);
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Получение сведений пользователя по логину
*/
public function getInLogin($login) {
try {
if(!$this->isLogin($login)) {
return null;
}
$sth = pdo()->prepare('SELECT * FROM `users` WHERE `login`=:login LIMIT 1');
$sth->execute([
':login' => $login
]);
return $sth->fetch(PDO::FETCH_OBJ);
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Проверка валидности логина
*/
public function isLogin($login) {
try {
$sth = pdo()->prepare('SELECT * FROM `users` WHERE `login`=:login LIMIT 1');
$sth->execute([
':login' => $login
]);
return $sth->rowCount();
}
catch(Exception $e) {
throw new Exception($e->getMessage());
}
}
/*
Выход с учетной записи
*/
public function logOut() {
if(isset($_SESSION['id'])) {
$this->updateOnline(time());
unset($_SESSION['id']);
unset($_SESSION['NextSetOnline']);
}
return true;
}
/*
Проверка на онлайн
*/
public function isOnline($userid) {
try {
if(!$this->IsValid($userid)) {
return false;
}
$UserData = $this->Get($userid);
if($UserData->date_online > time()) {
return true;
}
return false;
}
catch(Exception $e) {
return false;
}
}
/*
Количество юзеров
*/
public function rowCount() {
try {
$sth = pdo()->query('SELECT * FROM `users`');
return $sth->rowCount();
}
catch(Exception $e) {
return 0;
}
}
/*
Получаем IP адрес пользователя
*/
public function getRemote() {
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/*
Проверка прав
*/
public function isRights($userid, $searchRights) {
$userData = $this->Get($userid);
if(strpos($userData->rights, $searchRights) !== false) {
return true;
}
return false;
}
}