From f83530a122119d7f69812493f9c2f4987ccb2065 Mon Sep 17 00:00:00 2001 From: Cflip <36554078+cflip@users.noreply.github.com> Date: Wed, 10 Feb 2021 20:40:32 -0700 Subject: Reorganize code and add info to front page --- all.php | 2 +- category.php | 57 +++++++++-------- includes/functions_display.php | 118 ++++++++++++++++++++++++++++++++++ includes/functions_inc.php | 30 --------- index.php | 62 +++++------------- thread.php | 139 ++++++++++++++--------------------------- user.php | 4 +- 7 files changed, 215 insertions(+), 197 deletions(-) create mode 100644 includes/functions_display.php delete mode 100644 includes/functions_inc.php diff --git a/all.php b/all.php index c8bbc75..dc97914 100644 --- a/all.php +++ b/all.php @@ -3,7 +3,7 @@ include_once 'header.php'; include_once 'includes/db_inc.php'; -include_once 'includes/functions_inc.php'; +include_once 'includes/functions_display.php'; $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name, cat_id, cat_name FROM threads JOIN users ON thread_author = user_id JOIN categories ON thread_cat = cat_id ORDER BY thread_id DESC"; $result = mysqli_query($dbc, $sql); diff --git a/category.php b/category.php index 9fffd10..4280767 100644 --- a/category.php +++ b/category.php @@ -2,42 +2,45 @@ include_once 'includes/db_inc.php'; include_once 'header.php'; -include_once 'includes/functions_inc.php'; +include_once 'includes/functions_display.php'; -echo '
'; - -$sql = "SELECT cat_name, cat_description FROM categories WHERE cat_id = " . mysqli_real_escape_string($dbc, $_GET['id']); -$result = mysqli_query($dbc, $sql); +if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { + echo '
Unknown category.
'; +} else { + echo '
'; -if (!$result) { - die('Error trying to display category: ' . mysqli_error($dbc)); -} + $sql = "SELECT cat_name, cat_description FROM categories WHERE cat_id = " . mysqli_real_escape_string($dbc, $_GET['id']); + $result = mysqli_query($dbc, $sql); -// Display category name and description + if (!$result) { + die('Error trying to display category: ' . mysqli_error($dbc)); + } -if (mysqli_num_rows($result) == 0) { - echo 'This category does not exist'; -} else { - while ($row = mysqli_fetch_assoc($result)) { - echo '

' . $row['cat_name'] . '

'; - echo $row['cat_description']; + // Display category name and description + if (mysqli_num_rows($result) == 0) { + echo 'This category does not exist'; + } else { + while ($row = mysqli_fetch_assoc($result)) { + echo '

' . $row['cat_name'] . '

'; + echo $row['cat_description']; + } } -} -mysqli_free_result($result); + mysqli_free_result($result); -echo '
'; + echo '
'; -$sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY thread_id DESC"; -$result = mysqli_query($dbc, $sql); + $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = " . mysqli_real_escape_string($dbc, $_GET['id']) . " ORDER BY thread_id DESC"; + $result = mysqli_query($dbc, $sql); -if (!$result) { - die('Error trying to display threads: ' . mysqli_error($dbc)); -} + if (!$result) { + die('Error trying to display threads: ' . mysqli_error($dbc)); + } -echo ''; -display_threads($dbc, $result); -mysqli_free_result($result); -echo '
ThreadLatest Post
'; + echo ''; + display_threads($dbc, $result); + mysqli_free_result($result); + echo '
ThreadLatest Post
'; +} include 'footer.php'; \ No newline at end of file diff --git a/includes/functions_display.php b/includes/functions_display.php new file mode 100644 index 0000000..479648f --- /dev/null +++ b/includes/functions_display.php @@ -0,0 +1,118 @@ +'; + echo '

' . $row['cat_name'] . '

'; + echo $row['cat_description']; + if ($thread) { + echo '' . $thread['thread_subject'] . '
'; + echo 'by ' . $thread['user_name'] . ''; + } else { + $no_threads_msg = 'There are no threads in this category yet.'; + echo ''. $no_threads_msg .''; + } + } + + mysqli_stmt_close($stmt); + mysqli_free_result($thread_res); +} + +function display_threads($dbc, $sql_result, $show_category = false) { + $sql = "SELECT post_id, post_date, user_id, user_name FROM posts JOIN users ON post_author = user_id WHERE post_thread = ? ORDER BY post_id DESC LIMIT 1"; + $stmt = mysqli_stmt_init($dbc); + + if (!mysqli_stmt_prepare($stmt, $sql)) { + die('Could not create thread due to internal error: ' . mysqli_error($dbc)); + } + + while ($row = mysqli_fetch_assoc($sql_result)) { + mysqli_stmt_bind_param($stmt, "i", $row['thread_id']); + mysqli_stmt_execute($stmt); + + $thread_res = mysqli_stmt_get_result($stmt); + $thread = mysqli_fetch_assoc($thread_res); + + echo ''; + echo '

' . $row['thread_subject'] . '

'; + echo 'by ' . $row['user_name'] . ' '; + if ($show_category) { + echo 'in ' . $row['cat_name'] . ' '; + } + echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . ''; + echo 'by ' . $thread['user_name'] . '
'; + echo '' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . ''; + } + + mysqli_stmt_close($stmt); +} + +function add_quote($dbc, $thread_id, $matches) { + foreach ($matches as $match) { + $id = (int) filter_var($match, FILTER_SANITIZE_NUMBER_INT) - 1; + $sql = "SELECT post_content, post_author, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . $thread_id . " LIMIT 1 OFFSET " . $id; + $result = mysqli_query($dbc, $sql); + + if (!$result) { + return '
'; + } + + $reply = mysqli_fetch_assoc($result); + + if (empty($reply)) { + return '
Invalid quote!
'; + } + + $id = $id + 1; + + return '
Quote from ' . $reply['user_name'] . '
' . $reply['post_content'] . '
'; + } +} + +function display_posts($dbc, $sql_result) { + echo ''; + $post_index = 1; + $thread_id = $_GET['id']; + + while ($row = mysqli_fetch_assoc($sql_result)) { + echo ''; + echo ''; + + $post_content = $row['post_content']; + + $post_content = preg_replace_callback('/>#\d+/', function($matches) use($thread_id, $dbc) { + return add_quote($dbc, $thread_id, $matches); + }, $post_content); + + // Replace YouTube URLs with embedded YouTube videos. + $post_content = preg_replace( + "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", + '
', $post_content); + // Replace Image URLs with embedded images. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:|/?>))@i', 'http$2://$3', $post_content); + // Replace other URLs with links. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:|/?>))@i', '$0', $post_content); + + echo ''; + + $post_index++; + } + echo '
' . $post_index . '
Posted by ' . $row['user_name'] . '
' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '
' . $post_content . '
'; +} \ No newline at end of file diff --git a/includes/functions_inc.php b/includes/functions_inc.php deleted file mode 100644 index c10b65b..0000000 --- a/includes/functions_inc.php +++ /dev/null @@ -1,30 +0,0 @@ -'; - echo '

' . $row['thread_subject'] . '

'; - echo 'by ' . $row['user_name'] . ' '; - if ($show_category) { - echo 'in ' . $row['cat_name'] . ' '; - } - echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . ''; - echo 'by ' . $thread['user_name'] . '
'; - echo '' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . ''; - } - - mysqli_stmt_close($stmt); -} \ No newline at end of file diff --git a/index.php b/index.php index a3d6fce..ec81d0a 100644 --- a/index.php +++ b/index.php @@ -1,21 +1,22 @@ - +

Welcome to the cflip.net forum!

- Latest Updates: - +

+ 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! +

- - - - - - +
CategoryLatest Thread
+ + + + '; - } else { - $no_threads_msg = 'There are no threads in this category yet.'; - echo ''; - } - } - - mysqli_stmt_close($stmt); - mysqli_free_result($result); + display_categories($dbc, $result); ?> -
CategoryLatest Thread
'; - echo '

' . $row['cat_name'] . '

'; - echo $row['cat_description']; - if ($thread) { - echo '
' . $thread['thread_subject'] . '
'; - echo 'by ' . $thread['user_name'] . '
'. $no_threads_msg .'
- -
Latest Threads View All Latest Post
- \ No newline at end of file diff --git a/thread.php b/thread.php index 01ffeaa..97ab6b9 100644 --- a/thread.php +++ b/thread.php @@ -2,109 +2,66 @@ '; - } - - $reply = mysqli_fetch_assoc($result); - - if (empty($reply)) { - return '
Invalid quote!
'; - } - - $id = $id + 1; +if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { + echo '
Unknown category.
'; +} else { + $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads LEFT JOIN users ON thread_author = user_id WHERE thread_id = " . mysqli_real_escape_string($dbc, $_GET['id']); + $result = mysqli_query($dbc, $sql); - return '
Quote from ' . $reply['user_name'] . '
' . $reply['post_content'] . '
'; + if (!$result) { + die('Error trying to display thread page: ' . mysqli_error($dbc)); } -} - -$sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads LEFT JOIN users ON thread_author = user_id WHERE thread_id = " . mysqli_real_escape_string($dbc, $_GET['id']); -$result = mysqli_query($dbc, $sql); - -if (!$result) { - die('Error trying to display thread page: ' . mysqli_error($dbc)); -} -if (mysqli_num_rows($result) == 0) { - echo 'This thread does not exist'; -} else { - while ($row = mysqli_fetch_assoc($result)) { - echo '

' . $row['thread_subject'] . '

'; - echo 'Created by ' . $row['user_name'] . ' on ' . date('M d, Y', strtotime($row['thread_date'])) . '
'; - $thread_id = $row['thread_id']; + if (mysqli_num_rows($result) == 0) { + echo 'This thread does not exist'; + } else { + while ($row = mysqli_fetch_assoc($result)) { + echo '

' . $row['thread_subject'] . '

'; + echo 'Created by ' . $row['user_name'] . ' on ' . date('M d, Y', strtotime($row['thread_date'])) . '
'; + $thread_id = $row['thread_id']; + } } -} -echo ''; + echo ''; -mysqli_free_result($result); + mysqli_free_result($result); -$sql = "SELECT post_id, post_content, post_date, post_author, user_id, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . mysqli_real_escape_string($dbc, $_GET['id']); -$result = mysqli_query($dbc, $sql); + $sql = "SELECT post_id, post_content, post_date, post_author, user_id, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . mysqli_real_escape_string($dbc, $_GET['id']); + $result = mysqli_query($dbc, $sql); -if (!$result) { - die('Error trying to display posts: ' . mysqli_error($dbc)); -} - -if (mysqli_num_rows($result) == 0) { - echo '
This thread has no posts
'; -} else { - echo ''; - $post_index = 1; - $thread_id = $_GET['id']; - - while ($row = mysqli_fetch_assoc($result)) { - echo ''; - echo ''; - - $post_content = $row['post_content']; - - $post_content = preg_replace_callback('/>#\d+/', function($matches) use($thread_id, $dbc) { - return add_quote($dbc, $thread_id, $matches); - }, $post_content); - - $post_content = preg_replace( - "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", - '
', $post_content); - - $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:|/?>))@i', 'http$2://$3', $post_content); - //$post_content = preg_replace('/^>/', 'garb', $post_content); - $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:|/?>))@i', '$0', $post_content); - - echo ''; - - $post_index++; + if (!$result) { + die('Error trying to display posts: ' . mysqli_error($dbc)); } - echo '
' . $post_index . '
Posted by ' . $row['user_name'] . '
' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '
' . $post_content . '
'; -} -mysqli_free_result($result); + if (mysqli_num_rows($result) == 0) { + echo '
This thread has no posts
'; + } else { + display_posts($dbc, $result); + } -if (isset($_SESSION['signed_in'])) { - echo ' -
-
-

Reply to this thread

- Quote a post with ># and the number above the post (example: >#7) - -
- -
-
- '; -} else { - echo ' -
- Sign in to reply to this thread -
- '; + mysqli_free_result($result); + + if (isset($_SESSION['signed_in'])) { + echo ' +
+
+

Reply to this thread

+ Quote a post with ># and the number above the post (example: >#7) + +
+ +
+
+ '; + } else { + echo ' +
+ Sign in to reply to this thread +
+ '; + } } include_once 'footer.php'; diff --git a/user.php b/user.php index 5a84156..62b9144 100644 --- a/user.php +++ b/user.php @@ -1,7 +1,7 @@

Change Password

'; } -- cgit v1.2.3