From faae3f3906575b9c72d628a5e48d03f943c794fc Mon Sep 17 00:00:00 2001 From: cflip <36554078+cflip@users.noreply.github.com> Date: Sat, 17 Apr 2021 17:36:18 -0600 Subject: Automatically sort threads without PHP comparisons --- index.php | 9 --------- model/Category.php | 4 ++-- viewcategory.php | 10 ---------- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/index.php b/index.php index 7dd0fe4..bdbf770 100644 --- a/index.php +++ b/index.php @@ -27,16 +27,7 @@ include_once 'includes/db_inc.php'; include_once 'model/Category.php'; - // TODO: The get_all_categories function should return them in the right order - function cmp($a, $b) { - if ($a->id == $b->id) { - return 0; - } - return ($a->id < $b->id) ? -1 : 1; - } - $categories = get_all_categories($dbc); - usort($categories, "cmp"); foreach ($categories as $category) { $latest_thread = $category->get_latest_thread($dbc); diff --git a/model/Category.php b/model/Category.php index 1b699fb..b7c46d9 100644 --- a/model/Category.php +++ b/model/Category.php @@ -34,7 +34,7 @@ class Category { } function get_threads($dbc) { - $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost"; + $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id . " ORDER BY thread_date_lastpost DESC"; $result = mysqli_query($dbc, $sql); if (!$result) { @@ -80,7 +80,7 @@ class Category { } function get_all_categories($dbc) { - $sql = "SELECT cat_id FROM categories"; + $sql = "SELECT cat_id FROM categories ORDER BY cat_id ASC;"; $result = mysqli_query($dbc, $sql); if (!$result) { diff --git a/viewcategory.php b/viewcategory.php index a10afce..e10797a 100644 --- a/viewcategory.php +++ b/viewcategory.php @@ -39,17 +39,7 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
This post has been deleted'; + } + + return '
Quote from ' . $reply['user_name'] . ''; + } +} + +class Post { + public $id; + public $content; + public $date_created; + public $date_edited; + public $thread; + public $author; + + function get_from_database($id, $dbc) { + // TODO: Potential SQL injection risk? + $sql = "SELECT post_content, post_date_created, post_date_edited, post_thread, post_author FROM posts WHERE post_id = " . mysqli_real_escape_string($dbc, $id); + $result = mysqli_query($dbc, $sql); + + if (!$result) { + echo 'Failed to get post: ' . mysqli_error($dbc); + } + + if (mysqli_num_rows($result) == 0) { + return 0; + } else { + while ($row = mysqli_fetch_assoc($result)) { + $this->id = $id; + $this->content = $row['post_content']; + $this->date_created = $row['post_date_created']; + $this->date_edited = $row['post_date_edited']; + + $this->thread = new Thread(); + $this->thread->get_from_database($row['post_thread'], $dbc); + + $this->author = new User(); + $this->author->get_by_id($row['post_author'], $dbc); + } + } + + mysqli_free_result($result); + return 1; + } + + function display_content($dbc) { + echo '
' . $reply['post_content'] . '
The page you requested does not exist.
+ + diff --git a/includes/templates/header.php b/includes/templates/header.php new file mode 100644 index 0000000..4eb17e3 --- /dev/null +++ b/includes/templates/header.php @@ -0,0 +1,14 @@ +- This is the beta test of the forum website, so there are lots of features missing. Since there are no moderation features built into the website, - for the most part I don't care that much about what is posted here. Some links and buttons may not have any functionality either! -
-- If you notice a problem or have an idea for a feature that is missing, reply to this thread! -
-Category | -Threads | -Posts | -Latest Thread | -
---|
Category | +Threads | +Posts | +Latest Thread | +|
---|---|---|---|---|
';
- echo '' . $category->name . '';
- echo ' ' . $category->description . ''; - echo ' | ';
- echo '' . $category->thread_count . ' | '; - echo '' . $category->post_count . ' | '; - if (!is_null($latest_thread)) { - echo '' . $latest_thread->subject . ' '; - echo 'by ' . $latest_thread->author->name . ', ' . $latest_thread->date_created . ' | ';
- } else {
- echo 'No threads yet! | '; - } - echo '
';
+ echo '' . $category->name . '';
+ echo ' ' . $category->description . ''; + echo ' | ';
+ echo '' . $category->thread_count . ' | '; + echo '' . $category->post_count . ' | '; + if (!is_null($latest_thread)) { + echo '' . $latest_thread->subject . ' '; + echo 'by ' . $latest_thread->author->name . ', ' . $latest_thread->date_created . ' | ';
+ } else {
+ echo 'No threads yet! | '; } + echo '
This post has been deleted'; - } - - return '
Quote from ' . $reply['user_name'] . ''; - } -} - -class Post { - public $id; - public $content; - public $date_created; - public $date_edited; - public $thread; - public $author; - - function get_from_database($id, $dbc) { - // TODO: Potential SQL injection risk? - $sql = "SELECT post_content, post_date_created, post_date_edited, post_thread, post_author FROM posts WHERE post_id = " . mysqli_real_escape_string($dbc, $id); - $result = mysqli_query($dbc, $sql); - - if (!$result) { - echo 'Failed to get post: ' . mysqli_error($dbc); - } - - if (mysqli_num_rows($result) == 0) { - return 0; - } else { - while ($row = mysqli_fetch_assoc($result)) { - $this->id = $id; - $this->content = $row['post_content']; - $this->date_created = $row['post_date_created']; - $this->date_edited = $row['post_date_edited']; - - $this->thread = new Thread(); - $this->thread->get_from_database($row['post_thread'], $dbc); - - $this->author = new User(); - $this->author->get_by_id($row['post_author'], $dbc); - } - } - - mysqli_free_result($result); - return 1; - } - - function display_content($dbc) { - echo '
' . $reply['post_content'] . '
You must be signed in to manage a post.
'; + return; +} - // Admin users should be able to delete posts, but they should not be able to edit them - // Or should they?? - if ($current_user->id != $current->author->id/* && $current_user->level < 1*/) { - echo "You can't manage another user's post!"; - return; - } +// Admin users should be able to delete posts, but they should not be able to edit them +// Or should they?? +if (Session::get()->get_current_user()->id != $current->author->id) { + echo 'You can\'t manage another user\'s post!
'; + return; +} - // TODO: Disallow editing/deleting posts if they have been around for a while - ?> - +// TODO: Disallow editing/deleting posts if they have been around for a while +?> + diff --git a/moderate.php b/moderate.php index afeefa1..3b8d05d 100644 --- a/moderate.php +++ b/moderate.php @@ -1,25 +1,22 @@ get_by_id($_SESSION['user_id'], $dbc); - if ($user->level > 0) { + $user = Session::get()->get_current_user(); + + if ($user->level == USER_LEVEL_MODERATOR) { if (strcasecmp($delete, "on") == 0) { - delete_thread($dbc, $thread_id); + $thread = new Thread(); + $thread->get_from_database($thread_id, $dbc); + delete_thread($thread); header("Location: /"); exit(); diff --git a/register.php b/register.php index 03eac6b..050878e 100644 --- a/register.php +++ b/register.php @@ -1,27 +1,29 @@ + - + -'; + echo '