added login requirement to view articles to protect against potential data scrapping, with redirect functionality to the article the user attempts to view prior to login.

This commit is contained in:
2025-04-12 13:40:51 -06:00
parent f4dfaba1c6
commit 8d78a7d182
11 changed files with 102 additions and 195 deletions
+37 -91
View File
@@ -6,17 +6,15 @@ use Postmark\PostmarkClient;
function user_id_exists($user_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT user_id FROM user WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$result = exec_stmt('SELECT user_id FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();
if ($result == null)
return false;
return true;
}
function username_exists($username) {}
function cookies($user, $time)
{
setcookie('user_id', $user['user_id'], $time);
@@ -72,11 +70,7 @@ function login($username, $password, $stay_logged_in)
{
$password_hash = hash('sha256', $password);
$conn = get_connection();
$stmt = $conn->prepare('SELECT user_id, username, email, role_id, profile_picture FROM user WHERE username = ? OR email = ? AND password_hash = ?');
$stmt->bind_param('sss', $username, $username, $password_hash);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$user = exec_stmt('SELECT user_id, username, email, role_id, profile_picture FROM user WHERE username = ? OR email = ? AND password_hash = ?', 'sss', $username, $username, $password_hash)->fetch_assoc();
if ($user == null) {
$error_msg = '
@@ -97,9 +91,7 @@ function login($username, $password, $stay_logged_in)
// echo strlen($token);
setcookie('remember_user', $token, time() + 60 * 60 * 24 * 30, '/', '', true, true);
$stmt = $conn->prepare('INSERT INTO remember_user (token, user_id, remote_addr, http_forward) VALUES (?, ?, ?, ?)');
$stmt->bind_param('siss', $token, $user['user_id'], hash('sha256', $_SERVER['REMOTE_ADDR']), (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? hash('sha256', $_SERVER['HTTP_X_FORWARDED_FOR']) : null));
$stmt->execute();
exec_stmt('INSERT INTO remember_user (token, user_id, remote_addr, http_forward) VALUES (?, ?, ?, ?)', 'siss', $token, $user['user_id'], hash('sha256', $_SERVER['REMOTE_ADDR']), (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? hash('sha256', $_SERVER['HTTP_X_FORWARDED_FOR']) : null));
}
return true;
@@ -177,6 +169,14 @@ function create_user($username, $email, $password, $verify_password, $role_id, $
return $error_msg;
}
if (username_exists($username)) {
$error_msg = '
<div class="error">
Username is taken!
</div>
';
}
$password_hash = hash('sha256', $password);
$id = rand(1000, 999999999);
@@ -188,19 +188,15 @@ function create_user($username, $email, $password, $verify_password, $role_id, $
$orig_width = $orig_size[0];
$orig_height = $orig_size[1];
$crop_size = min($orig_width, $orig_height) * .56;
$crop_size = min($orig_width, $orig_height) * 0.56;
crop_image($upload['profile_picture']['tmp_name'], $x, $y, $crop_size, $crop_size);
crop_image($upload['profile_picture']['tmp_name'], $x, $y, $crop_size, $crop_size);
$target = upload($upload['profile_picture'], $_ENV['PROFILE_IMAGES_FQ_PATH'], $id);
} else {
$target = 'default-profile.png';
}
$conn = get_connection();
$stmt = $conn->prepare('INSERT INTO user (user_id, username, email, password_hash, role_id, profile_picture) VALUES (?, ?, ?, ?, ?, ?)');
$stmt->bind_param('isssis', $id, $username, $email, $password_hash, $role_id, $target);
$stmt->execute();
exec_stmt('INSERT INTO user (user_id, username, email, password_hash, role_id, profile_picture) VALUES (?, ?, ?, ?, ?, ?)', 'isssis', $id, $username, $email, $password_hash, $role_id, $target);
return true;
}
@@ -263,10 +259,7 @@ function reset_password($user_id, $new_password, $verify_new_password)
}
$password_hash = hash('sha256', $new_password);
$conn = get_connection();
$stmt = $conn->prepare('UPDATE user SET password_hash = ? WHERE user_id = ?');
$stmt->bind_param('si', $password_hash, $user_id);
$stmt->execute();
exec_stmt('UPDATE user SET password_hash = ? WHERE user_id = ?', 'si', $password_hash, $user_id);
}
function update_email_form()
@@ -305,10 +298,7 @@ function update_email($user_id, $new_email, $verify_new_email)
return $error_msg;
}
$conn = get_connection();
$stmt = $conn->prepare('UPDATE user SET email = ? WHERE user_id = ?');
$stmt->bind_param('si', $new_email, $user_id);
$stmt->execute();
exec_stmt('UPDATE user SET email = ? WHERE user_id = ?', 'si', $new_email, $user_id);
}
function update_username_form()
@@ -347,25 +337,10 @@ function update_username($user_id, $new_username, $verify_new_username)
return $error_msg;
}
$conn = get_connection();
$stmt = $conn->prepare('SELECT username FROM user WHERE username = ?');
$stmt->bind_param('s', $new_username);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
if ($result = username_exists($new_username))
return $result;
if ($result != null) {
$error_msg = '
<div class="error">
Usernames is taken!
</div>
';
return $error_msg;
}
$stmt = $conn->prepare('UPDATE user SET username = ? WHERE user_id = ?');
$stmt->bind_param('si', $new_username, $user_id);
$stmt->execute();
exec_stmt('UPDATE user SET username = ? WHERE user_id = ?', 'si', $new_username, $user_id);
}
function update_profile_picture_form()
@@ -438,16 +413,8 @@ function request_role_change_form()
function request_role_change($user_id, $new_role_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT user_id, username, email, roles.role, created_at FROM user INNER JOIN roles WHERE user.role_id = roles.role_id AND user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt = $conn->prepare('SELECT role FROM roles WHERE role_id = ?');
$stmt->bind_param('i', $new_role_id);
$stmt->execute();
$new_role = $stmt->get_result()->fetch_array();
$user = exec_stmt('SELECT user_id, username, email, roles.role, created_at FROM user INNER JOIN roles WHERE user.role_id = roles.role_id AND user_id = ?', 'i', $user_id)->fetch_assoc();
$new_role = exec_stmt('SELECT role FROM roles WHERE role_id = ?', 'i', $new_role_id)->fetch_array();
$email_html = '
<h1>User Role Change Request</h1>
@@ -461,7 +428,7 @@ function request_role_change($user_id, $new_role_id)
$client = new PostmarkClient($_ENV['POSTMARK_API_TOKEN']);
$send_result = $client->sendEmail(
$client->sendEmail(
'mailer@joshashton.dev',
'me@joshashton.dev',
'User Role Change Request - ' . $user['user_id'],
@@ -515,22 +482,16 @@ function delete_account($user_id, $password = null, $verify_password = null)
return $error_msg;
}
$conn = get_connection();
if ($password && $verify_password) {
$password_hash = hash('sha256', $password);
$stmt = $conn->prepare('SELECT user_id, profile_picture FROM user WHERE user_id = ? AND password_hash = ?');
$stmt->bind_param('is', $user_id, $password_hash);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$user = exec_stmt('SELECT user_id, profile_picture FROM user WHERE user_id = ? AND password_hash = ?', 'is', $user_id, $password_hash)->fetch_assoc();
if ($user == null) {
$error_msg = '
<div class="error">
Username and/or Password are invalid!
</div>
';
<div class="error">
Username and/or Password are invalid!
</div>
';
return $error_msg;
}
@@ -539,23 +500,17 @@ function delete_account($user_id, $password = null, $verify_password = null)
if (!empty($user['profile_picture']) && $user['profile_picture'] != '2025.jpg')
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
$stmt = $conn->prepare('DELETE FROM user WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
exec_stmt('DELETE FROM user WHERE user_id = ?', 'i', $user_id);
logout();
} else if ($_COOKIE['role_id'] <= 2) {
$stmt = $conn->prepare('SELECT user_id, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$user = exec_stmt('SELECT user_id, profile_picture FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();
if ($user['user_id'] == 2025) {
$error_msg = '
<div class="error">
Cannot delete the owner!
</div>
';
<div class="error">
Cannot delete the owner!
</div>
';
return $error_msg;
}
@@ -564,21 +519,13 @@ function delete_account($user_id, $password = null, $verify_password = null)
if (!empty($user['profile_picture']) && $user['profile_picture'] != '2025.jpg')
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
$stmt = $conn->prepare('DELETE FROM user WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
exec_stmt('DELETE FROM user WHERE user_id = ?', 'i', $user_id);
}
}
function user_view($user_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT user_id, username, email, role_id, created_at, last_active, is_active, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$user = exec_stmt('SELECT user_id, username, email, role_id, created_at, last_active, is_active, profile_picture FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();
if ($user == null)
header('Location: articles.php');
@@ -593,7 +540,6 @@ function user_view($user_id)
<div class="line"></div>
';
// TODO: Allow user to update profile picture.
// <button id="update_profile_picture_form_button" class="modal_button underline">Update Profile Picture</button>
// <div id="update_profile_picture_form" class="modal_form">' . update_profile_picture_form() . '</div>