PHP_SMART_HOME_V3/www/app/models/managers/UserManager.php

211 lines
5.9 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class UserManager
{
2020-04-21 17:44:30 +00:00
public static function getUsers () {
2019-08-23 11:39:42 +00:00
try {
2019-10-25 16:48:48 +00:00
$allUsers = Db::loadAll ("SELECT user_id, username, at_home, ota FROM users");
2019-10-13 12:00:24 +00:00
return $allUsers;
2019-08-23 11:39:42 +00:00
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function getUser ($userName) {
2019-08-23 11:39:42 +00:00
try {
$user = Db::loadOne ("SELECT * FROM users WHERE username = ?", [$userName]);
return $user;
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function getUserId ($userId) {
2020-02-21 13:25:25 +00:00
try {
$user = Db::loadOne ("SELECT * FROM users WHERE user_id = ?", [$userId]);
return $user;
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function getAvatarUrl($userId = null){
2020-01-21 10:24:50 +00:00
$email = self::getUserData('email');
2020-03-25 14:58:47 +00:00
if ($userId != null){
$email = self::getUserData('email',$userId);
}
2020-01-21 10:24:50 +00:00
return 'https://secure.gravatar.com/avatar/' . md5( strtolower( trim( $email ) ) );
}
2020-04-21 17:44:30 +00:00
public static function login ($username, $password, $rememberMe) {
2019-08-23 11:39:42 +00:00
try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) {
if ($user['password'] == UserManager::getHashPassword($password)) {
if (isset($rememberMe) && $rememberMe == 'true') {
2020-04-21 17:44:30 +00:00
setcookie ("rememberMe", self::setEncryptedCookie($user['username']), time () + (30 * 24 * 60 * 60 * 1000), BASEDIR, $_SERVER['HTTP_HOST'], 1);
2019-08-23 11:39:42 +00:00
}
$_SESSION['user']['id'] = $user['user_id'];
2019-10-22 16:31:14 +00:00
$page = "";
2019-08-23 11:39:42 +00:00
if ($user["startPage"] == 1) {
2019-10-22 14:52:45 +00:00
$page = "dashboard";
2019-08-23 11:39:42 +00:00
}
unset($_POST['login']);
return $page;
} else {
throw new PDOException("Heslo není správné!");
}
} else {
throw new PDOException("Uživatel s tím to jménem neexistuje!");
}
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function loginNew ($username, $password) {
2020-03-31 09:13:21 +00:00
try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) {
if ($user['password'] == UserManager::getHashPassword($password)) {
echo "user loged in";
return $user['user_id'];
} else {
return false;
}
} else {
return false;
}
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function isLogin () {
2019-08-23 11:39:42 +00:00
if (isset ($_SESSION['user']) && isset($_SESSION['user']['id'])) {
return true;
} else {
2019-09-26 18:48:53 +00:00
if (isset ($_COOKIE['rememberMe'])){
2020-04-21 17:44:30 +00:00
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array (self::getDecryptedCookie($_COOKIE['rememberMe'])))) {
2019-08-23 11:39:42 +00:00
$_SESSION['user']['id'] = $user['user_id'];
return true;
}
}
}
return false;
}
2020-04-21 17:44:30 +00:00
public static function logout () {
2019-08-23 11:39:42 +00:00
unset($_SESSION['user']);
session_destroy();
2019-10-13 10:29:07 +00:00
if (isset($_COOKIE['rememberMe'])){
2019-10-13 10:55:22 +00:00
unset($_COOKIE['rememberMe']);
setcookie("rememberMe", 'false', time(), BASEDIR, $_SERVER['HTTP_HOST']);
2019-10-13 10:29:07 +00:00
}
2019-08-23 11:39:42 +00:00
}
2020-04-21 17:44:30 +00:00
public static function setEncryptedCookie($value){
2019-08-23 11:39:42 +00:00
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
$method = "aes-256-cbc";
$ivlen = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($ivlen);
$newvalue_raw = openssl_encrypt($value, $method, $first_key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $newvalue_raw, $second_key, TRUE);
$newvalue = base64_encode ($iv.$hmac.$newvalue_raw);
return $newvalue;
}
2020-04-21 17:44:30 +00:00
public static function getDecryptedCookie($value){
2019-08-23 11:39:42 +00:00
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
$c = base64_decode($value);
$method = "aes-256-cbc";
$ivlen = openssl_cipher_iv_length($method);
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, 32);
$newValue_raw = substr($c, $ivlen+32);
$newValue = openssl_decrypt($newValue_raw, $method, $first_key, OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $newValue_raw, $second_key, TRUE);
if (hash_equals ($hmac, $calcmac)) {
return $newValue;
}
return false;
}
2019-10-22 14:52:45 +00:00
public static function getUserData ($type, $userId = '') {
2020-01-17 07:12:50 +00:00
if ($userId == '') {
2019-10-22 14:52:45 +00:00
$userId = $_SESSION['user']['id'];
2019-08-23 11:39:42 +00:00
}
2019-10-22 14:52:45 +00:00
$user = Db::loadOne ('SELECT ' . $type . ' FROM users WHERE user_id=?', array ($userId));
return $user[$type];
2019-08-23 11:39:42 +00:00
}
2020-04-21 17:44:30 +00:00
public static function setUserData ($type, $value) {
2019-08-23 11:39:42 +00:00
if (isset ($_SESSION['user']['id'])) {
Db::command ('UPDATE users SET ' . $type . '=? WHERE user_id=?', array ($value, $_SESSION['user']['id']));
}
}
2020-04-21 17:44:30 +00:00
public static function getHashPassword ($password) {
2019-08-23 11:39:42 +00:00
$salt = "s0mRIdlKvI";
$hashPassword = hash('sha512', ($password . $salt));
return $hashPassword;
}
2020-04-21 17:44:30 +00:00
public static function atHome($userId, $atHome){
2019-10-22 14:52:45 +00:00
try {
Db::edit ('users', ['at_home' => $atHome], 'WHERE user_id = ?', array($userId));
} catch(PDOException $error) {
echo $error->getMessage();
die();
2019-08-23 11:39:42 +00:00
}
2019-10-22 14:52:45 +00:00
}
2019-08-23 11:39:42 +00:00
2020-04-21 17:44:30 +00:00
public static function changePassword($oldPassword, $newPassword, $newPassword2){
2019-10-22 14:52:45 +00:00
if ($newPassword == $newPassword2) {
//Password Criteria
$oldPasswordSaved = self::getUserData('password');
if (self::getHashPassword($oldPassword) == $oldPasswordSaved) {
self::setUserData('password', self::getHashPassword($newPassword));
} else {
throw new Exception ("old password did not match");
2019-08-23 11:39:42 +00:00
}
2019-10-22 14:52:45 +00:00
} else {
throw new Exception ("new password arent same");
2019-08-23 11:39:42 +00:00
}
2019-10-22 14:52:45 +00:00
}
2019-10-13 10:29:07 +00:00
2020-04-21 17:44:30 +00:00
public static function createUser($userName, $password){
2019-10-22 14:52:45 +00:00
$userId = Db::loadOne('SELECT * FROM users WHERE username = ?;', array($userName))['user_id'];
if ($userId != null) {
return false;
};
try {
$user = [
'username' => $userName,
'password' => self::getHashPassword($password),
];
return Db::add ('users', $user);
} catch(PDOException $error) {
echo $error->getMessage();
die();
2019-10-13 12:00:24 +00:00
}
2019-10-22 14:52:45 +00:00
}
2019-10-13 12:00:24 +00:00
2020-04-21 17:44:30 +00:00
public static function haveOtaEnabled($userName){
$ota = self::getUser($userName)['ota'];
2019-10-22 14:52:45 +00:00
if ($ota != ''){
return ($ota != '' ? $ota : false);
} else {
return false;
2019-10-13 10:29:07 +00:00
}
2019-08-23 11:39:42 +00:00
}
2019-10-22 14:52:45 +00:00
}
?>