implemented article composer functionality + interface, login & signup functionality + interface, auth permission functionality reflected in the nav, and determined a unified accent color accessible in dark and light mode
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
<?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;
|
||||
}
|
||||
@@ -4,7 +4,7 @@ require 'db_functions.php';
|
||||
function article_ids_by_recency()
|
||||
{
|
||||
$query = 'SELECT article_id FROM article ORDER BY published_at DESC;';
|
||||
$results = query_db_one_or_more_results($query);
|
||||
$results = query_one_or_more_results($query);
|
||||
|
||||
$ids = [];
|
||||
|
||||
@@ -14,10 +14,22 @@ function article_ids_by_recency()
|
||||
return $ids;
|
||||
}
|
||||
|
||||
function article_tags()
|
||||
{
|
||||
$query = 'SELECT * FROM tags;';
|
||||
$results = query_one_or_more_results($query);
|
||||
$tags = [];
|
||||
|
||||
while ($row = mysqli_fetch_array($results))
|
||||
$tags[] = $row;
|
||||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
function article_ids_by_tag($tag_id)
|
||||
{
|
||||
$query = 'SELECT article_id FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE tag = "' . $tag_id . '";';
|
||||
$results = query_db_one_or_more_results($query);
|
||||
$results = query_one_or_more_results($query);
|
||||
|
||||
$ids = [];
|
||||
|
||||
@@ -33,12 +45,13 @@ function article_ids_by_tag($tag_id)
|
||||
function article_page_from_markdown($article_id)
|
||||
{
|
||||
$query = 'SELECT * FROM article WHERE article_id = ' . $article_id . ';';
|
||||
$article = query_db_one_result($query);
|
||||
$article = query_one_result($query);
|
||||
$markdown = read_file_one_string($_ENV['ARTICLES_PATH'] . $article_id . '/article.md');
|
||||
|
||||
$parsedown = new Parsedown();
|
||||
|
||||
$html = '<div class="article">';
|
||||
$html .= '<h3>' . $article['title'] . '</h3>';
|
||||
$html .= $parsedown->text($markdown);
|
||||
$html .= '</div>';
|
||||
|
||||
@@ -51,10 +64,10 @@ function article_page_from_markdown($article_id)
|
||||
function article_card($article_id)
|
||||
{
|
||||
$article_query = 'SELECT * FROM article WHERE article_id = ' . $article_id . ';';
|
||||
$article = query_db_one_result($article_query);
|
||||
$article = query_one_result($article_query);
|
||||
|
||||
$article_tags_query = 'SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ' . $article_id . ';';
|
||||
$tags_results = query_db_one_or_more_results($article_tags_query);
|
||||
$tags_results = query_one_or_more_results($article_tags_query);
|
||||
$tags_html = '';
|
||||
if ($tags_results != null) {
|
||||
$tags_html .= '<div class="tag_row">';
|
||||
@@ -66,7 +79,7 @@ function article_card($article_id)
|
||||
}
|
||||
|
||||
$author_query = 'SELECT username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
|
||||
$author = query_db_one_result($author_query);
|
||||
$author = query_one_result($author_query);
|
||||
|
||||
$card = '
|
||||
<div class="article_card">
|
||||
@@ -94,7 +107,44 @@ function article_card($article_id)
|
||||
return $card;
|
||||
}
|
||||
|
||||
/*
|
||||
* Displays the article creation wizard and opens a new markdown file.
|
||||
*/
|
||||
function create_article() {}
|
||||
function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
|
||||
{
|
||||
$stmt = 'INSERT INTO article (author_id, title, excerpt, published_at) VALUES (' . $author_id . ', "' . $title . '", "' . $excerpt . '", CURRENT_TIMESTAMP);';
|
||||
$id = exec_statement($stmt, 0);
|
||||
|
||||
$path = $_ENV['ARTICLES_PATH'] . $id . '/';
|
||||
mkdir($path);
|
||||
$fs = fopen($path . 'article.md', 'a');
|
||||
fwrite($fs, $markdown_file_contents);
|
||||
fclose($fs);
|
||||
|
||||
$stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
|
||||
for ($i = 0; $i < count($tags); $i++) {
|
||||
$stmt .= '(' . $id . ', ' . $tags[$i] . ')';
|
||||
|
||||
if ($i < count($tags) - 1)
|
||||
$stmt .= ', ';
|
||||
|
||||
if ($i == count($tags) - 1)
|
||||
$stmt .= ';';
|
||||
}
|
||||
|
||||
echo $stmt;
|
||||
pretty_dump($tags);
|
||||
if (count($tags) > 0)
|
||||
exec_statement($stmt, 0);
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
function delete_article($article_id)
|
||||
{
|
||||
$stmt = 'DELETE FROM article WHERE article_id = ' . $article_id . ';';
|
||||
return exec_statement($stmt, 1);
|
||||
}
|
||||
|
||||
function delete_articles_by_author($author_id)
|
||||
{
|
||||
$stmt = 'DELETE FROM article WHERE author_id = "' . $author_id . '";';
|
||||
return exec_statement($stmt, 1);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ if ($_SERVER['HTTP_HOST'] == 'localhost') {
|
||||
/*
|
||||
* Handles any queries that should have only one row results.
|
||||
*/
|
||||
function query_db_one_result($query_stmt)
|
||||
function query_one_result($query_stmt)
|
||||
{
|
||||
$conn = get_connection();
|
||||
$results = mysqli_query($conn, $query_stmt);
|
||||
@@ -32,7 +32,7 @@ function query_db_one_result($query_stmt)
|
||||
return mysqli_fetch_assoc($results);
|
||||
}
|
||||
|
||||
function query_db_one_or_more_results($query_stmt)
|
||||
function query_one_or_more_results($query_stmt)
|
||||
{
|
||||
$conn = get_connection();
|
||||
$results = mysqli_query($conn, $query_stmt);
|
||||
@@ -44,6 +44,23 @@ function query_db_one_or_more_results($query_stmt)
|
||||
return $results;
|
||||
}
|
||||
|
||||
/*
|
||||
* Executes a statement. Type is typically either 0 or 1, where 0 is an INSERT or UPDATE,
|
||||
* and 1 is DELETE. This is to determine whether a boolean value should be returned or
|
||||
* the ID of the row(s).
|
||||
*/
|
||||
function exec_statement($stmt, $type)
|
||||
{
|
||||
$conn = get_connection();
|
||||
$results = mysqli_query($conn, $stmt);
|
||||
|
||||
if ($type == 0)
|
||||
$results = mysqli_insert_id($conn);
|
||||
|
||||
mysqli_close($conn);
|
||||
return $results;
|
||||
}
|
||||
|
||||
function get_connection()
|
||||
{
|
||||
// Connect to the DB
|
||||
|
||||
Reference in New Issue
Block a user