Log In
';
return $form;
}
function login($username, $password, $stay_logged_in)
{
$password_hash = hash('sha256', $password);
$query = 'SELECT user_id, username, email, role_id, profile_picture FROM user WHERE username ="' . $username . '" AND password_hash = "' . $password_hash . '";';
$user = query_one_result($query);
if ($user == null) {
$error_msg = '
Username and/or Password are invalid!
';
return $error_msg;
}
if ($stay_logged_in)
$time = time() + 60 * 60 * 24 * 365;
else
$time = time() + 60 * 60 * 24 * 1;
cookies($user, $time);
return true;
}
function signup_form($error_msg = '')
{
$form = '
';
return $form;
}
function create_user($username, $email, $password, $verify_password, $role_id, $upload = null, $x = null, $y = null, $crop_width = null)
{
if ($password != $verify_password) {
$error_msg = '
Passwords do not match!
';
return $error_msg;
}
$password_hash = hash('sha256', $password);
$id = rand(1000, 999999999);
while (user_id_exists($id))
$id = rand(1000, 999999999);
if (!empty($upload)) {
$orig_size = getimagesize($upload['profile_picture']['tmp_name']);
$orig_width = $orig_size[0];
$orig_height = $orig_size[1];
$crop_size = min($orig_width, $orig_height) * .56;
crop_image($upload['profile_picture']['tmp_name'], $x, $y, $crop_size, $crop_size);
$target = upload($upload['profile_picture'], $_ENV['PROFILE_IMAGES_FQ_PATH'], $id);
}
$stmt = 'INSERT INTO user (user_id, username, email, password_hash, role_id' . (!empty($target) ? ', profile_picture' : '') . ') VALUES (' . $id . ', "' . $username . '", "' . $email . '", "' . $password_hash . '", ' . $role_id . (!empty($target) ? ', "' . $target . '"' : '') . ');';
exec_statement($stmt, 0);
}
function profile_cropper()
{
$display = '
';
return $display;
}
function reset_pw_form()
{
$form = '
Reset Password
×
';
return $form;
}
function reset_password($user_id, $new_password, $verify_new_password)
{
if ($new_password != $verify_new_password) {
$error_msg = '
Passwords do not match!
';
return $error_msg;
}
$password_hash = hash('sha256', $new_password);
$stmt = 'UPDATE user SET password_hash = "' . $password_hash . '" WHERE user_id = ' . $user_id . ';';
exec_statement($stmt, 0);
}
function update_email_form()
{
$form = '
Update Email
×
';
return $form;
}
function update_email($user_id, $new_email, $verify_new_email)
{
if ($new_email != $verify_new_email) {
$error_msg = '
Emails do not match!
';
return $error_msg;
}
$stmt = 'UPDATE user SET email = "' . $new_email . '" WHERE user_id = ' . $user_id . ';';
exec_statement($stmt, 0);
}
function update_username_form()
{
$form = '
Update Username
×
';
return $form;
}
function update_username($user_id, $new_username, $verify_new_username)
{
if ($new_username != $verify_new_username) {
$error_msg = '
Usernames do not match!
';
return $error_msg;
}
$query = 'SELECT username FROM user WHERE username = "' . $new_username . '";';
if (query_one_result($query) != null) {
$error_msg = '
Usernames is taken!
';
return $error_msg;
}
$stmt = 'UPDATE user SET username = "' . $new_username . '" WHERE user_id = ' . $user_id . ';';
exec_statement($stmt, 0);
}
function request_role_change_form()
{
$query = 'SELECT * FROM roles WHERE role_id <> 1;';
$results = query_one_or_more_results($query);
$roles = '';
while ($row = mysqli_fetch_array($results)) {
$roles .= '
';
}
$roles .= '
';
$form = '
Request Role
×
';
return $form;
}
function request_role_change($user_id, $new_role_id)
{
// TODO: Need to implement email handler first.
}
function delete_account_form()
{
$form = '
Delete Account
×
';
return $form;
}
function delete_account($user_id, $password = null, $verify_password = null)
{
if ($user_id == 2025) {
$error_msg = '
I am the owner, and I cannot delete myself...
';
return $error_msg;
}
if ($password != $verify_password) {
$error_msg = '
Passwords do not match!
';
return $error_msg;
}
if ($password && $verify_password) {
$password_hash = hash('sha256', $password);
$query = 'SELECT user_id, profile_picture FROM user WHERE user_id = ' . $user_id . ' AND password_hash = "' . $password_hash . '";';
$user = query_one_result($query);
if ($user == null) {
$error_msg = '
Username and/or Password are invalid!
';
return $error_msg;
}
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
exec_statement($stmt, 1);
logout();
} else if ($_COOKIE['role_id'] < 3) {
$query = 'SELECT user_id, profile_picture FROM user WHERE user_id = ' . $user_id . ';';
$user = query_one_result($query);
if ($user['user_id'] == 2025) {
$error_msg = '
Cannot delete the owner!
';
return $error_msg;
}
delete_file($_ENV['PROFILE_IMAGES_FQ_PATH'] . $user['profile_picture']);
$stmt = 'DELETE FROM user WHERE user_id = ' . $user_id . ';';
exec_statement($stmt, 1);
}
}
function user_view($user_id)
{
if (!isset($_COOKIE['role_id']) || ($_COOKIE['role_id'] > 2 && $_COOKIE['user_id'] != $user_id))
header('Location: articles.php');
$query = 'SELECT user_id, username, email, role_id, created_at, last_active, is_active, profile_picture FROM user WHERE user_id = ' . $user_id . ';';
$user = query_one_result($query);
if ($user == null)
header('Location: articles.php');
$view = '
' . $user['username'] . '
User Actions
';
// TODO: Need to setup an email handler.
//
Request Role
//
' . request_role_change_form() . '
$view .= '
' . reset_pw_form() . '
' . update_email_form() . '
' . update_username_form() . '
' . delete_account_form() . '
';
return $view;
}