implemented secure remember me functionality
This commit is contained in:
+2
-1
@@ -4,7 +4,8 @@
|
|||||||
"type": "project",
|
"type": "project",
|
||||||
"require": {
|
"require": {
|
||||||
"erusev/parsedown": "^1.7",
|
"erusev/parsedown": "^1.7",
|
||||||
"vlucas/phpdotenv": "^5.6"
|
"vlucas/phpdotenv": "^5.6",
|
||||||
|
"phpmailer/phpmailer": "^6.9"
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ function cookies($user, $time)
|
|||||||
function logout()
|
function logout()
|
||||||
{
|
{
|
||||||
foreach (array_keys($_COOKIE) as $key) {
|
foreach (array_keys($_COOKIE) as $key) {
|
||||||
echo 'unsetting ' . $key . ' now.';
|
|
||||||
setcookie($key, '', time() - 3600, '/');
|
setcookie($key, '', time() - 3600, '/');
|
||||||
unset($_COOKIE[$key]);
|
unset($_COOKIE[$key]);
|
||||||
}
|
}
|
||||||
@@ -93,13 +92,19 @@ function login($username, $password, $stay_logged_in)
|
|||||||
return $error_msg;
|
return $error_msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($stay_logged_in)
|
$time = time() + 60 * 60 * 1;
|
||||||
$time = time() + 60 * 60 * 24 * 365;
|
|
||||||
else
|
|
||||||
$time = time() + 60 * 60 * 24 * 1;
|
|
||||||
|
|
||||||
cookies($user, $time);
|
cookies($user, $time);
|
||||||
|
|
||||||
|
if ($stay_logged_in) {
|
||||||
|
$token = bin2hex(random_bytes(64));
|
||||||
|
// echo $token . '<br>';
|
||||||
|
// echo strlen($token);
|
||||||
|
setcookie('remember_user', $token, time() + 60 * 60 * 24 * 30, '/', '', true, true);
|
||||||
|
|
||||||
|
$stmt = 'INSERT INTO remember_user (token, user_id, remote_addr' . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? ', http_forward' : '') . ') VALUES ("' . $token . '", ' . $user['user_id'] . ', "' . hash('sha256', $_SERVER['REMOTE_ADDR']) . '"' . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? ', "' . hash('sha256', $_SERVER['HTTP_X_FORWARDED_FOR']) . '"' : '') . ');';
|
||||||
|
exec_statement($stmt, 1);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -439,7 +444,10 @@ function delete_account($user_id, $password = null, $verify_password = null)
|
|||||||
return $error_msg;
|
return $error_msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hardcoded prevention of deleting the owner's profile picture
|
||||||
|
if ($user['profile_picture'] != '2025.jpg')
|
||||||
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
|
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
|
||||||
|
|
||||||
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
|
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
|
||||||
exec_statement($stmt, 1);
|
exec_statement($stmt, 1);
|
||||||
logout();
|
logout();
|
||||||
@@ -457,7 +465,10 @@ function delete_account($user_id, $password = null, $verify_password = null)
|
|||||||
return $error_msg;
|
return $error_msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hardcoded prevention of deleting the owner's profile picture
|
||||||
|
if ($user['profile_picture'] != '2025.jpg')
|
||||||
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
|
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
|
||||||
|
|
||||||
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
|
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
|
||||||
exec_statement($stmt, 1);
|
exec_statement($stmt, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ session_start();
|
|||||||
// Initialize Composer.
|
// Initialize Composer.
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
require_once 'db_functions.php';
|
require_once 'db_functions.php';
|
||||||
|
require_once 'account_functions.php';
|
||||||
|
|
||||||
// Load environment variables.
|
// Load environment variables.
|
||||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||||
@@ -20,7 +21,24 @@ if ($_SESSION['initialized'] && isset($_POST['theme'])) {
|
|||||||
unset($_POST['theme']);
|
unset($_POST['theme']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the database and cookies to keep the user logged in.
|
||||||
if (isset($_COOKIE['user_id'])) {
|
if (isset($_COOKIE['user_id'])) {
|
||||||
exec_statement('UPDATE user SET is_active = true WHERE user_id = ' . $_COOKIE['user_id'] . ';', 1);
|
exec_statement('UPDATE user SET is_active = true WHERE user_id = ' . $_COOKIE['user_id'] . ';', 1);
|
||||||
exec_statement('UPDATE user SET last_active = CURRENT_TIMESTAMP WHERE user_id = ' . $_COOKIE['user_id'] . ';', 1);
|
exec_statement('UPDATE user SET last_active = CURRENT_TIMESTAMP WHERE user_id = ' . $_COOKIE['user_id'] . ';', 1);
|
||||||
|
|
||||||
|
$query = 'SELECT user_id, username, email, role_id, profile_picture FROM user WHERE user_id = ' . $_COOKIE['user_id'] . ';';
|
||||||
|
$user = query_one_result($query);
|
||||||
|
$time = time() + 60 * 60 * 1;
|
||||||
|
cookies($user, $time);
|
||||||
|
} else if (isset($_COOKIE['remember_user'])) {
|
||||||
|
$query = 'SELECT * FROM remember_user WHERE token = "' . $_COOKIE['remember_user'] . '";';
|
||||||
|
$remember = query_one_result($query);
|
||||||
|
$query = 'SELECT user_id, username, email, role_id, profile_picture FROM user WHERE user_id = ' . $remember['user_id'] . ';';
|
||||||
|
$user = query_one_result($query);
|
||||||
|
|
||||||
|
exec_statement('UPDATE user SET is_active = true WHERE user_id = ' . $user['user_id'] . ';', 1);
|
||||||
|
exec_statement('UPDATE user SET last_active = CURRENT_TIMESTAMP WHERE user_id = ' . $user['user_id'] . ';', 1);
|
||||||
|
|
||||||
|
$time = time() + 60 * 60 * 1;
|
||||||
|
cookies($user, $time);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,15 @@ CREATE TABLE user (
|
|||||||
FOREIGN KEY (role_id) REFERENCES roles (role_id)
|
FOREIGN KEY (role_id) REFERENCES roles (role_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE remember_user (
|
||||||
|
token VARCHAR(128) PRIMARY KEY,
|
||||||
|
user_id INT NOT NULL,
|
||||||
|
remote_addr VARCHAR(64) NOT NULL,
|
||||||
|
http_forward VARCHAR(64),
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES user (user_id)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE article (
|
CREATE TABLE article (
|
||||||
article_id INT PRIMARY KEY AUTO_INCREMENT,
|
article_id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
author_id INT NOT NULL,
|
author_id INT NOT NULL,
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ if (isset($_POST['form_id'])) {
|
|||||||
$msg = login(
|
$msg = login(
|
||||||
$_POST['username'],
|
$_POST['username'],
|
||||||
$_POST['password'],
|
$_POST['password'],
|
||||||
(isset($POST['stay_logged_in']) ? true : false)
|
(isset($_POST['stay_logged_in']) ? true : false)
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($msg === true)
|
if ($msg === true)
|
||||||
|
|||||||
Reference in New Issue
Block a user