171 lines
4.9 KiB
PHP
171 lines
4.9 KiB
PHP
<?php
|
|
require 'db_functions.php';
|
|
|
|
function user_id_exists($user_id)
|
|
{
|
|
$query = 'SELECT user_id FROM user WHERE user_id = ' . $user_id . ';';
|
|
$result = query_one_result($query);
|
|
|
|
if ($result == null)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
function auth_level($user_id)
|
|
{
|
|
$query = 'SELECT role_id FROM user WHERE user_id = ' . $user_id . ';';
|
|
$result = query_one_result($query);
|
|
$role = $result['role_id'];
|
|
return $role;
|
|
}
|
|
|
|
function modify_self()
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
function delete_self()
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
function create_account($username, $email, $password, $verify_password, $role_id, $profile_picture)
|
|
{
|
|
if ($password != $verify_password) {
|
|
$error_msg = '
|
|
<div class="error">
|
|
Passwords do not match!
|
|
</div>
|
|
';
|
|
|
|
return $error_msg;
|
|
}
|
|
|
|
$password_hash = hash('sha256', $password);
|
|
|
|
$id = rand(1000, 999999999);
|
|
while (user_id_exists($id))
|
|
$id = rand(1000, 999999999);
|
|
|
|
if (!empty($profile_picture)) {
|
|
$filetype = explode('.', $profile_picture);
|
|
$pic_path = $id . $filetype[count($filetype) - 1];
|
|
}
|
|
|
|
$stmt = 'INSERT INTO user (user_id, username, email, password_hash, role_id' . (!empty($profile_picture) ? ', profile_picture' : '') . ') VALUES (' . $id . ', "' . $username . '", "' . $email . '", "' . $password_hash . '", ' . $role_id . (!empty($profile_picture) ? '", "' . $pic_path . '"' : '') . ');';
|
|
exec_statement($stmt, 0);
|
|
|
|
login($username, $password, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
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 = '
|
|
<div class="error">
|
|
Username and/or Password are invalid!
|
|
</div>
|
|
';
|
|
|
|
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 cookies($user, $time)
|
|
{
|
|
setcookie('user_id', $user['user_id'], $time);
|
|
setcookie('username', $user['username'], $time);
|
|
setcookie('email', $user['email'], $time);
|
|
setcookie('role_id', $user['role_id'], $time);
|
|
setcookie('profile_picture', $user['profile_picture'], $time);
|
|
}
|
|
|
|
function logout()
|
|
{
|
|
foreach ($_COOKIE as $key => $value) {
|
|
echo 'unsetting ' . $key . ' now.';
|
|
setcookie($key, '', time() - 3600, '/');
|
|
unset($_COOKIE[$key]);
|
|
}
|
|
|
|
header('Location: articles.php');
|
|
}
|
|
|
|
function login_form($error_msg = '')
|
|
{
|
|
$form = '
|
|
<div class="form_card">
|
|
<h3>Log In</h3>
|
|
<form method="post">
|
|
<label for="username">Username / Email</label>
|
|
<input id="username" name="username" required>
|
|
|
|
<label for="password">Password</label>
|
|
<input id="password" name="password" type="password" required>
|
|
|
|
<label class="form_checkbox_container">Stay Logged In?
|
|
<input name="stay_logged_in" type="checkbox" value="true">
|
|
<span class="checkmark"></span>
|
|
</label>
|
|
|
|
<a href="user.php?action=signup">Sign Up</a>
|
|
<input class="button" type="submit" value="Log In">
|
|
' . $error_msg . '
|
|
</form>
|
|
</div>
|
|
';
|
|
|
|
return $form;
|
|
}
|
|
|
|
function signup_form($error_msg = '')
|
|
{
|
|
$form = '
|
|
<div class="form_card">
|
|
<h3>Sign Up</h3>
|
|
<form method="post">
|
|
<label for="username">Username</label>
|
|
<input id="username" name="username" required>
|
|
|
|
<label for="email">Email Address</label>
|
|
<input id="email" name="email" required>
|
|
|
|
<label for="password">Password</label>
|
|
<input id="password" name="password" type="password" required>
|
|
|
|
<label for="verify_password">Verify Password</label>
|
|
<input id="verify_password" name="verify_password" type="password" required>
|
|
|
|
<label for="profile_picture_upload" class="upload_button">Upload a Profile Picture</label>
|
|
<input id="profile_picture_upload" name="profile_picture_upload" type="file" accept="image/png, image/jpeg">
|
|
|
|
<label class="form_checkbox_container">Stay Logged In?
|
|
<input name="stay_logged_in" type="checkbox" value="true">
|
|
<span class="checkmark"></span>
|
|
</label>
|
|
|
|
<a href="user.php?action=login">Log In</a>
|
|
<input class="button" type="submit" value="Sign Up">
|
|
' . $error_msg . '
|
|
</form>
|
|
</div>
|
|
';
|
|
|
|
return $form;
|
|
}
|