From 8f2c241c3dea0a32639efb1a6697dfc5f39c7cd7 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Sun, 13 Apr 2025 15:57:16 -0600 Subject: [PATCH] refactoring --- admin.php | 60 ++-- articles.php | 113 ++++--- components/admin/article_view.php | 68 +++++ components/admin/user_view.php | 71 +++++ components/article/article.php | 41 +++ components/article/card.php | 49 +++ components/article/compose_preview.php | 12 + components/article/compose_view.php | 36 +++ components/article/composer.php | 46 +++ components/article/list.php | 55 ++++ components/foot.php | 37 ++- components/nav.php | 10 +- components/user/delete_account.php | 24 ++ components/user/login.php | 28 ++ components/user/profile_cropper.php | 22 ++ components/user/request_role_change.php | 33 +++ components/user/reset_password.php | 24 ++ components/user/signup.php | 50 ++++ components/user/update_email.php | 24 ++ components/user/update_profile_picture.php | 33 +++ components/user/update_username.php | 24 ++ components/user/user_view.php | 59 ++++ compose.php | 119 -------- functions/account_functions.php | 330 +-------------------- functions/admin_functions.php | 175 ++--------- functions/article_functions.php | 105 ------- functions/init.php | 2 +- js/md_preview.js | 31 +- user.php | 27 +- 29 files changed, 867 insertions(+), 841 deletions(-) create mode 100644 components/admin/article_view.php create mode 100644 components/admin/user_view.php create mode 100644 components/article/article.php create mode 100644 components/article/card.php create mode 100644 components/article/compose_preview.php create mode 100644 components/article/compose_view.php create mode 100644 components/article/composer.php create mode 100644 components/article/list.php create mode 100644 components/user/delete_account.php create mode 100644 components/user/login.php create mode 100644 components/user/profile_cropper.php create mode 100644 components/user/request_role_change.php create mode 100644 components/user/reset_password.php create mode 100644 components/user/signup.php create mode 100644 components/user/update_email.php create mode 100644 components/user/update_profile_picture.php create mode 100644 components/user/update_username.php create mode 100644 components/user/user_view.php delete mode 100644 compose.php diff --git a/admin.php b/admin.php index 9db80fa..b6c32eb 100644 --- a/admin.php +++ b/admin.php @@ -2,49 +2,53 @@ require_once 'functions/init.php'; require_once 'functions/functions.php'; +// UI Components +foreach (glob('components/admin/*.php') as $file) + require_once $file; + include_once 'components/head.php'; if (!isset($_COOKIE['role_id'])) - header('Location: user.php'); + header('Location: user.php'); else if ($_COOKIE['role_id'] >= 3) - header('Location: user.php?action=view&user_id=' . $_COOKIE['user_id']); + header('Location: user.php?action=view&user_id=' . $_COOKIE['user_id']); include_once 'functions/admin_functions.php'; if (isset($_POST['action']) && isset($_POST['form_id']) || isset($_FILES['csv'])) { - if (!empty($_FILES['csv']['name'])) { - upload($_FILES['csv'], $_ENV['UPLOAD_TMP_FQ_PATH'], 'new_users'); - create_users_from_csv($_ENV['UPLOAD_TMP_FQ_PATH'] . 'new_users.csv'); - delete_file($_ENV['UPLOAD_TMP_FQ_PATH'] . 'new_users.csv'); - } else { - switch ($_POST['action']) { - case 'delete': - if ($_POST['form_id'] == 'users') - foreach ($_POST['selected_users'] as $id) - delete_account($id); - else if ($_POST['form_id'] == 'articles') - foreach ($_POST['selected_articles'] as $id) - delete_article($id); - break; + if (!empty($_FILES['csv']['name'])) { + upload($_FILES['csv'], $_ENV['UPLOAD_TMP_FQ_PATH'], 'new_users'); + create_users_from_csv($_ENV['UPLOAD_TMP_FQ_PATH'] . 'new_users.csv'); + delete_file($_ENV['UPLOAD_TMP_FQ_PATH'] . 'new_users.csv'); + } else { + switch ($_POST['action']) { + case 'delete': + if ($_POST['form_id'] == 'users') + foreach ($_POST['selected_users'] as $id) + delete_account($id); + else if ($_POST['form_id'] == 'articles') + foreach ($_POST['selected_articles'] as $id) + delete_article($id); + break; - case 'view': - header('Location: user.php?action=view&user_id=' . $_POST['selected_users'][0]); + case 'view': + header('Location: user.php?action=view&user_id=' . $_POST['selected_users'][0]); + } } - } } if (isset($_GET['view'])) { - switch ($_GET['view']) { - case 'users': - echo admin_user_view(); - break; + switch ($_GET['view']) { + case 'users': + echo admin_user_view(); + break; - case 'articles': - echo admin_article_view(); - break; - } + case 'articles': + echo admin_article_view(); + break; + } } else { - echo admin_article_view(); + echo admin_article_view(); } include_once 'components/foot.php'; diff --git a/articles.php b/articles.php index 6277c82..063b92c 100644 --- a/articles.php +++ b/articles.php @@ -6,68 +6,65 @@ include_once 'components/head.php'; include_once 'functions/article_functions.php'; -// Default view, it'll show articles by recency -if (!isset($_GET['article_id']) && !isset($_GET['tag']) && !isset($_GET['author_id'])) { - $filters = ' -
-
- '; +// UI Components +foreach (glob('components/article/*.php') as $file) + require_once $file; - if (!isset($_GET['sort']) || $_GET['sort'] == 'newest') { - $ids = article_ids_by_newest(); - $filters .= ' -

Newest - Oldest

- - '; - } else if ($_GET['sort'] == 'oldest') { - $ids = article_ids_by_oldest(); - $filters .= ' -

Oldest - Newest

- - '; +if (isset($_POST['form_id'])) { + switch ($_POST['form_id']) { + case 'compose': + if (isset($_POST['article_id'])) { + $tags = []; + for ($i = 0; $i < count($_POST['tags']); $i++) + $tags[] = $_POST['tags'][$i]; + + update_article($_POST['article_id'], $_POST['title'], $_POST['excerpt'], $tags, $_POST['markdown']); + header('Location: articles.php?article_id=' . $_POST['article_id']); + } else { + $id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], (isset($_POST['tags']) ? $_POST['tags'] : []), $_POST['markdown']); + header('Location: articles.php?article_id=' . $id); + } + + break; } - - $filters .= ' -
-
- '; - - echo ' -
-
- ' . $filters . ' -
-
- '; - foreach ($ids as $id) - echo article_card($id); - echo ' -
-
- '; -} else if (isset($_COOKIE['user_id']) && isset($_GET['article_id'])) { - increment_read_counter($_GET['article_id']); - echo article_page_from_markdown($_GET['article_id']); -} else if (isset($_GET['author_id'])) { - $ids = articles_ids_by_author($_GET['author_id']); - - foreach ($ids as $id) - echo article_card($id); -} else if (isset($_GET['tag'])) { - $ids = article_ids_by_tag($_GET['tag']); - - foreach ($ids as $id) - echo article_card($id); } else { - $redirect = (isset($_GET['article_id']) ? 'articles.php?article_id=' . $_GET['article_id'] : ''); - if ($redirect) - header('Location: user.php?action=login&redirect=' . urlencode($redirect)); + if (isset($_GET['view'])) + $view = $_GET['view']; else - header('Location: articles.php'); + $view = 'list'; + + switch ($view) { + case 'list': + echo article_list(); + break; + case 'filter': + if (isset($_GET['tag'])) + echo article_list(article_ids_by_tag($_GET['tag'])); + else if (isset($_GET['author_id'])) + echo article_list(articles_ids_by_author($_GET['author_id'])); + break; + case 'sort': + if (isset($_GET['sort']) && $_GET['sort'] == 'oldest') + echo article_list(article_ids_by_oldest()); + else if (isset($_GET['sort']) && $_GET['sort'] == 'newest') + echo article_list(article_ids_by_newest()); + break; + case 'read': + if (isset($_COOKIE['user_id']) && isset($_GET['article_id'])) { + increment_read_counter($_GET['article_id']); + echo article_page_from_markdown($_GET['article_id']); + } else { + $redirect = (isset($_GET['article_id']) ? 'articles.php?view=read&article_id=' . $_GET['article_id'] : ''); + if ($redirect) + header('Location: user.php?action=login&redirect=' . urlencode($redirect)); + else + header('Location: articles.php'); + } + break; + case 'compose': + echo compose_view((isset($_GET['article_id']) ? $_GET['article_id'] : '')); + break; + } } include_once 'components/foot.php'; diff --git a/components/admin/article_view.php b/components/admin/article_view.php new file mode 100644 index 0000000..88c1c9a --- /dev/null +++ b/components/admin/article_view.php @@ -0,0 +1,68 @@ + +
+

Articles

+ View Users +
+
+
+ +
+ + +
+ + + + + + + + + + + + + + + + '; + $conn = get_connection(); + $results = $conn->query('SELECT article_id, username, title, read_count, article.created_at, updated_at, published_at, excerpt FROM article INNER JOIN user ON article.author_id = user.user_id;'); + + foreach ($results as $key => $result) { + $display .= ' + + + + + + + + + + + '; + } + + $display .= ' + +
SelectArticle IDAuthorTitleCreatedUpdatedPublishedRead Count
+ + ' . $result['article_id'] . '' . $result['username'] . '' . $result['title'] . '' . $result['created_at'] . '' . $result['updated_at'] . '' . $result['published_at'] . '' . $result['read_count'] . '
+ +
+ + '; + + return $display; +} diff --git a/components/admin/user_view.php b/components/admin/user_view.php new file mode 100644 index 0000000..665294a --- /dev/null +++ b/components/admin/user_view.php @@ -0,0 +1,71 @@ + +
+

Users

+ View Articles +
+
+
+ +
+ + + + + No file selected. + + +
+ + + + + + + + + + + + + + '; + $conn = get_connection(); + $results = $conn->query('SELECT user_id, username, email, last_active, role, created_at, is_active, profile_picture FROM user INNER JOIN roles ON user.role_id = roles.role_id;'); + + foreach ($results as $key => $result) { + $display .= ' + + + + + + + + + + + + '; + } + + $display .= ' +
SelectUser IDUsernameEmailRoleCreatedLast ActiveActiveProfile Picture
+ + ' . $result['user_id'] . '' . htmlspecialchars($result['username']) . '' . htmlspecialchars($result['email']) . '' . ucwords($result['role']) . '' . $result['created_at'] . '' . $result['last_active'] . '' . $result['is_active'] . '
+ +
+ + '; + + return $display; +} diff --git a/components/article/article.php b/components/article/article.php new file mode 100644 index 0000000..6eaa64f --- /dev/null +++ b/components/article/article.php @@ -0,0 +1,41 @@ +fetch_assoc(); + $author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc(); + $markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md'); + + $parsedown = new Parsedown(); + $parsedown->setSafeMode(true); + $article_content = $parsedown->text($markdown); + + $html = ' +
+ +
+

' . $article['title'] . '

+ ' . (isset($_COOKIE['user_id']) && $_COOKIE['user_id'] == $article['author_id'] ? '' : '') . ' +
+
+
+
Written by ' . $author['username'] . '
+

Published on ' . $article['published_at'] . '
+ Lasted edited on ' . $article['updated_at'] . '

+
+
+ +
+
+
+
+ ' . $article_content . ' +
+
+ '; + + return $html; +} diff --git a/components/article/card.php b/components/article/card.php new file mode 100644 index 0000000..d58f1c5 --- /dev/null +++ b/components/article/card.php @@ -0,0 +1,49 @@ +fetch_assoc(); + + $tags_results = exec_stmt('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?', 'i', $article_id); + + $tags_html = ''; + if ($tags_results != null) { + $tags_html .= '
'; + + while ($row = $tags_results->fetch_assoc()) + $tags_html .= '#' . $row['tag'] . ''; + + $tags_html .= '
'; + } + + $author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc(); + + $card = ' +
+
+

' . $article['title'] . '

+
+
+ Written by: ' . $author['username'] . ' + Published: ' . $article['published_at'] . ' + Updated: ' . $article['updated_at'] . ' +
+
+ +
+
+
+
+

' . $article['excerpt'] . '

+
+ Read More +
' . $tags_html . '
+
+
+ '; + + return $card; +} diff --git a/components/article/compose_preview.php b/components/article/compose_preview.php new file mode 100644 index 0000000..66f2962 --- /dev/null +++ b/components/article/compose_preview.php @@ -0,0 +1,12 @@ + +
+
+ + '; + + return $preview; +} diff --git a/components/article/compose_view.php b/components/article/compose_view.php new file mode 100644 index 0000000..7221e62 --- /dev/null +++ b/components/article/compose_view.php @@ -0,0 +1,36 @@ +prepare('SELECT * FROM article WHERE article_id = ?'); + $stmt->bind_param('i', $article_id); + $stmt->execute(); + $article = $stmt->get_result()->fetch_assoc(); + + $stmt = $conn->prepare('SELECT tag_id FROM article_tags WHERE article_id = ?'); + $stmt->bind_param('i', $article_id); + $stmt->execute(); + $result = $stmt->get_result(); + $tags = []; + while ($row = $result->fetch_assoc()) + $tags[] = $row['tag_id']; + + $title = $article['title']; + $excerpt = $article['excerpt']; + $existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md'); + } + + $view = ' +
+ ' . (!empty($existing_markdown) ? form($article_id, $title, $excerpt, $existing_markdown, $tags) : form()) . ' + ' . preview() . ' + '; + + $view .= preview() . ' +
+ '; + + return $view; +} diff --git a/components/article/composer.php b/components/article/composer.php new file mode 100644 index 0000000..2c89871 --- /dev/null +++ b/components/article/composer.php @@ -0,0 +1,46 @@ + + + '; + + foreach ($raw_tags as $row) { + $tags_boxes .= ' + + '; + } + + $tags_boxes .= ' + + '; + + $form = ' +
+
+
+ + ' . (isset($article_id) ? '' : '') . ' + + + + + + + + + +' . $tags_boxes . ' + +
+
+
+'; + + return $form; +} diff --git a/components/article/list.php b/components/article/list.php new file mode 100644 index 0000000..962fd8a --- /dev/null +++ b/components/article/list.php @@ -0,0 +1,55 @@ + + +
+ '; + + if (!isset($_GET['sort']) || $_GET['sort'] == 'newest') { + $ids = article_ids_by_newest(); + $filters .= ' +

Newest - Oldest

+ + '; + } else if ($_GET['sort'] == 'oldest') { + $ids = article_ids_by_oldest(); + $filters .= ' +

Oldest - Newest

+ + '; + } + + $filters .= ' +
+ + '; + + $content .= ' +
+
+ ' . $filters . ' +
+
+ '; + + if (!isset($list)) + foreach ($ids as $id) + $content .= article_card($id); + else + foreach ($list as $id) + $content .= article_card($id); + + $content .= ' +
+
+ '; + + return $content; +} diff --git a/components/foot.php b/components/foot.php index c7dc6ca..bdddf1d 100644 --- a/components/foot.php +++ b/components/foot.php @@ -21,37 +21,42 @@ function get_args_from_url() if ($_SERVER['SCRIPT_NAME'] == '/user.php') { $args = get_args_from_url(); - if (isset($args['action'])) { - if ($args['action'] == 'signup') { + if (isset($args['view'])) { + if ($args['view'] == 'signup') { echo ' - - - '; - } else if ($args['action'] == 'view') { + + + '; + } else if ($args['view'] == 'display') { echo ' - - - + + '; } } } else if ($_SERVER['SCRIPT_NAME'] == '/admin.php') { echo ' - + '; $args = get_args_from_url(); if (isset($args['view'])) { if ($args['view'] == 'users') { echo ' - - '; + + '; + } + } +} else if ($_SERVER['SCRIPT_NAME'] == '/articles.php') { + $args = get_args_from_url(); + + if (isset($args['view'])) { + if ($args['view'] == 'compose') { + echo ' + + '; } } -} else if ($_SERVER['SCRIPT_NAME'] == '/compose.php') { - echo ' - - '; } ?> diff --git a/components/nav.php b/components/nav.php index f61067f..05c82b1 100644 --- a/components/nav.php +++ b/components/nav.php @@ -6,7 +6,7 @@ $nav = ' + '; + + return $form; +} diff --git a/components/user/login.php b/components/user/login.php new file mode 100644 index 0000000..20af59c --- /dev/null +++ b/components/user/login.php @@ -0,0 +1,28 @@ + +

Log In

+
+ + + + + + + + + + + + ' . $error_msg . ' +
+ + '; + + return $form; +} diff --git a/components/user/profile_cropper.php b/components/user/profile_cropper.php new file mode 100644 index 0000000..f648bff --- /dev/null +++ b/components/user/profile_cropper.php @@ -0,0 +1,22 @@ + + +
+
+
+
+
+
+ +
+ + +
+ + '; + + return $display; +} diff --git a/components/user/request_role_change.php b/components/user/request_role_change.php new file mode 100644 index 0000000..769c694 --- /dev/null +++ b/components/user/request_role_change.php @@ -0,0 +1,33 @@ +query('SELECT * FROM roles WHERE role_id <> 1'); + + $roles = '
'; + while ($row = $result->fetch_assoc()) { + $roles .= ' + + '; + } + + $roles .= '
'; + + $form = ' +
+
Request Role
+ × +
+ + ' . $roles . ' + + +
+
+ '; + + return $form; +} diff --git a/components/user/reset_password.php b/components/user/reset_password.php new file mode 100644 index 0000000..87eb895 --- /dev/null +++ b/components/user/reset_password.php @@ -0,0 +1,24 @@ + +
Reset Password
+ × +
+ + + + + + + + + + +
+ + '; + + return $form; +} diff --git a/components/user/signup.php b/components/user/signup.php new file mode 100644 index 0000000..420466b --- /dev/null +++ b/components/user/signup.php @@ -0,0 +1,50 @@ + +

Sign Up

+
+ + + + + + + + + + + + + + + + + + + + + No file selected. + + + + + ' . $error_msg . ' +
+ + + +
+ +
+ + '; + + return $form; +} diff --git a/components/user/update_email.php b/components/user/update_email.php new file mode 100644 index 0000000..ca04d6d --- /dev/null +++ b/components/user/update_email.php @@ -0,0 +1,24 @@ + +
Update Email
+ × +
+ + + + + + + + + + +
+ + '; + + return $form; +} diff --git a/components/user/update_profile_picture.php b/components/user/update_profile_picture.php new file mode 100644 index 0000000..2b3782d --- /dev/null +++ b/components/user/update_profile_picture.php @@ -0,0 +1,33 @@ + +
Update Profile Picture
+ × +
+ + + + + + + + + No file selected. + + +
+ + + +
+ +
+ + '; + + return $form; +} diff --git a/components/user/update_username.php b/components/user/update_username.php new file mode 100644 index 0000000..1b3d2ce --- /dev/null +++ b/components/user/update_username.php @@ -0,0 +1,24 @@ + +
Update Username
+ × +
+ + + + + + + + + + +
+ + '; + + return $form; +} diff --git a/components/user/user_view.php b/components/user/user_view.php new file mode 100644 index 0000000..89c4efb --- /dev/null +++ b/components/user/user_view.php @@ -0,0 +1,59 @@ +fetch_assoc(); + if ($user == null) + header('Location: articles.php'); + + $view = ' +
+
+

' . $user['username'] . '

+
+ +
+
+
+ '; + + // TODO: Allow user to update profile picture. + // + // + if ((isset($_COOKIE['user_id']) && $_COOKIE['user_id'] == $user_id) || (isset($_COOKIE['role_id']) && $_COOKIE['role_id'] <= 2)) { + $view .= ' +

User Actions

+ + + + +
+ + + + + +
+
+ '; + } else { + $ids = articles_ids_by_author($_GET['user_id']); + $view .= ' +
+ '; + + foreach ($ids as $id) + $view .= article_card($id); + + $view .= '
'; + } + + return $view; +} diff --git a/compose.php b/compose.php deleted file mode 100644 index 780bf0b..0000000 --- a/compose.php +++ /dev/null @@ -1,119 +0,0 @@ -prepare('SELECT * FROM article WHERE article_id = ?'); - $stmt->bind_param('i', $article_id); - $stmt->execute(); - $article = $stmt->get_result()->fetch_assoc(); - - $stmt = $conn->prepare('SELECT tag_id FROM article_tags WHERE article_id = ?'); - $stmt->bind_param('i', $article_id); - $stmt->execute(); - $result = $stmt->get_result(); - $tags = []; - while ($row = $result->fetch_assoc()) - $tags[] = $row['tag_id']; - - $title = $article['title']; - $excerpt = $article['excerpt']; - $existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md'); -} - -echo ' -
-'; -if (!empty($existing_markdown)) { - echo form($article_id, $title, $excerpt, $existing_markdown, $tags); -} else { - echo form(); -} - -echo preview(); - -echo ' -
-'; - -include_once 'components/foot.php'; - -function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null) -{ - $raw_tags = article_tags(); - $tags_boxes = ' -
- - '; - - foreach ($raw_tags as $row) { - $tags_boxes .= ' - - '; - } - - $tags_boxes .= ' -
- '; - - $form = ' -
-
-
- ' . (isset($article_id) ? '' : '') . ' - - - - - - - - - -' . $tags_boxes . ' - -
-
-
-'; - - return $form; -} - -function preview() -{ - $preview = ' -
-
-
-
- '; - - return $preview; -} diff --git a/functions/account_functions.php b/functions/account_functions.php index f6c20f6..5c1be10 100644 --- a/functions/account_functions.php +++ b/functions/account_functions.php @@ -13,7 +13,14 @@ function user_id_exists($user_id) return true; } -function username_exists($username) {} +function username_exists($username) +{ + $result = exec_stmt('SELECT username FROM user WHERE username = "?"', 'i', $username)->fetch_assoc(); + + if ($result == null) + return false; + return true; +} function cookies($user, $time) { @@ -38,34 +45,6 @@ function logout() header('Location: articles.php'); } -function login_form($error_msg = '') -{ - $form = ' -
-

Log In

-
- - - - - - - - - - - - ' . $error_msg . ' -
-
- '; - - return $form; -} - function login($username, $password, $stay_logged_in) { $password_hash = hash('sha256', $password); @@ -97,56 +76,6 @@ function login($username, $password, $stay_logged_in) return true; } -function signup_form($error_msg = '') -{ - $form = ' -
-

Sign Up

-
- - - - - - - - - - - - - - - - - - - - - No file selected. - - - - - ' . $error_msg . ' -
- - - -
- -
-
- '; - - return $form; -} - function create_user($username, $email, $password, $verify_password, $role_id, $upload = null, $x = null, $y = null, $crop_width = null, $honeypot = null) { if (!empty($honeypot)) { @@ -200,52 +129,6 @@ function create_user($username, $email, $password, $verify_password, $role_id, $ return true; } -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) { @@ -262,30 +145,6 @@ function reset_password($user_id, $new_password, $verify_new_password) exec_stmt('UPDATE user SET password_hash = ? WHERE user_id = ?', 'si', $password_hash, $user_id); } -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) { @@ -301,30 +160,6 @@ function update_email($user_id, $new_email, $verify_new_email) exec_stmt('UPDATE user SET email = ? WHERE user_id = ?', 'si', $new_email, $user_id); } -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) { @@ -343,74 +178,8 @@ function update_username($user_id, $new_username, $verify_new_username) exec_stmt('UPDATE user SET username = ? WHERE user_id = ?', 'si', $new_username, $user_id); } -function update_profile_picture_form() -{ - $form = ' -
-
Update Profile Picture
- × -
- - - - - - - - - No file selected. - - -
- - - -
- -
-
- '; - - return $form; -} - function update_profile_picture($user_id) {} -function request_role_change_form() -{ - $conn = get_connection(); - $result = $conn->query('SELECT * FROM roles WHERE role_id <> 1'); - - $roles = '
'; - while ($row = $result->fetch_assoc()) { - $roles .= ' - - '; - } - - $roles .= '
'; - - $form = ' -
-
Request Role
- × -
- - ' . $roles . ' - - -
-
- '; - - return $form; -} - function request_role_change($user_id, $new_role_id) { $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(); @@ -436,30 +205,6 @@ function request_role_change($user_id, $new_role_id) ); } -function delete_account_form() -{ - $form = ' -
-
Delete Account
- × -
- - - - - - - - - - -
-
- '; - - return $form; -} - function delete_account($user_id, $password = null, $verify_password = null) { if ($user_id == 2025) { @@ -522,62 +267,3 @@ function delete_account($user_id, $password = null, $verify_password = null) exec_stmt('DELETE FROM user WHERE user_id = ?', 'i', $user_id); } } - -function user_view($user_id) -{ - $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'); - - $view = ' -
-
-

' . $user['username'] . '

-
- -
-
-
- '; - - // TODO: Allow user to update profile picture. - // - // - if ((isset($_COOKIE['user_id']) && $_COOKIE['user_id'] == $user_id) || (isset($_COOKIE['role_id']) && $_COOKIE['role_id'] <= 2)) { - $view .= ' -

User Actions

- - - - -
- - - - - -
-
- '; - } else { - $ids = articles_ids_by_author($_GET['user_id']); - $view .= ' -
- '; - - foreach ($ids as $id) - $view .= article_card($id); - - $view .= '
'; - } - - return $view; -} diff --git a/functions/admin_functions.php b/functions/admin_functions.php index d3790e2..7f9d91f 100644 --- a/functions/admin_functions.php +++ b/functions/admin_functions.php @@ -6,167 +6,28 @@ require_once 'article_functions.php'; function create_users_from_csv($csv) { - $csv = read_file($csv); - while ($record = fgets($csv)) { - $fields = explode(',', $record); + $csv = read_file($csv); + while ($record = fgets($csv)) { + $fields = explode(',', $record); - $username = $fields[0]; - $email = $fields[1]; - $password = $fields[2]; - $role_id = $fields[3]; + $username = $fields[0]; + $email = $fields[1]; + $password = $fields[2]; + $role_id = $fields[3]; - create_user( - $username, - $email, - $password, - $password, - $role_id, - ); - } -} - -function admin_article_view() -{ - $display = ' -
-
-

Articles

- View Users -
-
-
- -
- - -
- - - - - - - - - - - - - - - - '; - $conn = get_connection(); - $results = $conn->query('SELECT article_id, username, title, read_count, article.created_at, updated_at, published_at, excerpt FROM article INNER JOIN user ON article.author_id = user.user_id;'); - - foreach ($results as $key => $result) { - $display .= ' - - - - - - - - - - - '; - } - - $display .= ' - -
SelectArticle IDAuthorTitleCreatedUpdatedPublishedRead Count
- - ' . $result['article_id'] . '' . $result['username'] . '' . $result['title'] . '' . $result['created_at'] . '' . $result['updated_at'] . '' . $result['published_at'] . '' . $result['read_count'] . '
- -
-
- '; - - return $display; -} - -function admin_user_view() -{ - $display = ' -
-
-

Users

- View Articles -
-
-
- -
- - - - - No file selected. - - -
- - - - - - - - - - - - - - '; - $conn = get_connection(); - $results = $conn->query('SELECT user_id, username, email, last_active, role, created_at, is_active, profile_picture FROM user INNER JOIN roles ON user.role_id = roles.role_id;'); - - foreach ($results as $key => $result) { - $display .= ' - - - - - - - - - - - - '; - } - - $display .= ' -
SelectUser IDUsernameEmailRoleCreatedLast ActiveActiveProfile Picture
- - ' . $result['user_id'] . '' . htmlspecialchars($result['username']) . '' . htmlspecialchars($result['email']) . '' . ucwords($result['role']) . '' . $result['created_at'] . '' . $result['last_active'] . '' . $result['is_active'] . '
- -
-
- '; - - return $display; + create_user( + $username, + $email, + $password, + $password, + $role_id, + ); + } } function delete_users($ids) { - foreach ($ids as $id) { - delete_account($id); - } + foreach ($ids as $id) { + delete_account($id); + } } diff --git a/functions/article_functions.php b/functions/article_functions.php index 05b5010..6f9c7d0 100644 --- a/functions/article_functions.php +++ b/functions/article_functions.php @@ -59,111 +59,6 @@ function article_ids_by_tag($tag) return $ids; } -/* - * Reads the markdown file for a given article and generates the HTML for it. - */ -function article_page_from_markdown($article_id) -{ - $article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc(); - $author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc(); - $markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md'); - - $parsedown = new Parsedown(); - $parsedown->setSafeMode(true); - $article_content = $parsedown->text($markdown); - - $html = ' -
- -
-

' . $article['title'] . '

- ' . (isset($_COOKIE['user_id']) && $_COOKIE['user_id'] == $article['author_id'] ? '' : '') . ' -
-
-
-
Written by ' . $author['username'] . '
-

Published on ' . $article['published_at'] . '
- Lasted edited on ' . $article['updated_at'] . '

-
-
- -
-
-
-
- ' . $article_content . ' -
-
- '; - - return $html; -} - -function article_filters() -{ - $filters = ' -
- - - -
- '; - - return $filters; -} - -/* - * Generates cards for a given article. - */ -function article_card($article_id) -{ - $article = exec_stmt('SELECT * FROM article WHERE article_id = ?', 'i', $article_id)->fetch_assoc(); - - $tags_results = exec_stmt('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?', 'i', $article_id); - - $tags_html = ''; - if ($tags_results != null) { - $tags_html .= '
'; - - while ($row = $tags_results->fetch_assoc()) - $tags_html .= '#' . $row['tag'] . ''; - - $tags_html .= '
'; - } - - $author = exec_stmt('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?', 'i', $article['author_id'])->fetch_assoc(); - - $card = ' -
-
-

' . $article['title'] . '

-
-
- Written by: ' . $author['username'] . ' - Published: ' . $article['published_at'] . ' - Updated: ' . $article['updated_at'] . ' -
-
- -
-
-
-
-

' . $article['excerpt'] . '

-
- Read More -
' . $tags_html . '
-
-
- '; - - return $card; -} - function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents) { $id = exec_stmt('INSERT INTO article (author_id, title, excerpt, published_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)', 'iss', $author_id, $title, $excerpt); diff --git a/functions/init.php b/functions/init.php index 97cddb8..d67324b 100644 --- a/functions/init.php +++ b/functions/init.php @@ -1,6 +1,6 @@ response.text()) - .then(data => { - preview.innerHTML = decodeURIComponent(data); - }); - }); -} else { - preview.style.display = 'none'; -} +textarea.addEventListener('input', function() { + fetch('functions/preview.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: 'markdown=' + encodeURIComponent(this.value), + }) + .then(response => response.text()) + .then(data => { + preview.innerHTML = decodeURIComponent(data); + }); +}); diff --git a/user.php b/user.php index 47fe111..1f8f3d9 100644 --- a/user.php +++ b/user.php @@ -1,9 +1,12 @@