diff options
author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-02-10 20:40:32 -0700 |
---|---|---|
committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-02-10 20:40:32 -0700 |
commit | f83530a122119d7f69812493f9c2f4987ccb2065 (patch) | |
tree | 691ed8597a8d3275998f7db951b7b055ef5baf3b | |
parent | 4c9d433ba1c52ad67e4cccabf04e709bb8b85070 (diff) |
Reorganize code and add info to front page
-rw-r--r-- | all.php | 2 | ||||
-rw-r--r-- | category.php | 57 | ||||
-rw-r--r-- | includes/functions_display.php | 118 | ||||
-rw-r--r-- | includes/functions_inc.php | 30 | ||||
-rw-r--r-- | index.php | 62 | ||||
-rw-r--r-- | thread.php | 139 | ||||
-rw-r--r-- | user.php | 4 |
7 files changed, 215 insertions, 197 deletions
@@ -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 '<section>'; - -$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 '<section>Unknown category.</section>'; +} else { + echo '<section>'; -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 '<h1>' . $row['cat_name'] . '</h1>'; - 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 '<h1>' . $row['cat_name'] . '</h1>'; + echo $row['cat_description']; + } } -} -mysqli_free_result($result); + mysqli_free_result($result); -echo '</section>'; + echo '</section>'; -$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 '<table><tr><th class="left">Thread</th><th class="right">Latest Post</th></tr>'; -display_threads($dbc, $result); -mysqli_free_result($result); -echo '</table>'; + echo '<table><tr><th class="left">Thread</th><th class="right">Latest Post</th></tr>'; + display_threads($dbc, $result); + mysqli_free_result($result); + echo '</table>'; +} 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 @@ +<?php + +function display_navbar($dbc) { + +} + +function display_categories($dbc, $sql_result) { + $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = ? ORDER BY thread_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['cat_id']); + mysqli_stmt_execute($stmt); + + $thread_res = mysqli_stmt_get_result($stmt); + $thread = mysqli_fetch_assoc($thread_res); + + echo '<tr><td class="left">'; + echo '<h4><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h4>'; + echo $row['cat_description']; + if ($thread) { + echo '</td><td class="right">' . $thread['thread_subject'] . '<br>'; + echo '<small>by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b></small></td></tr>'; + } else { + $no_threads_msg = 'There are no threads in this category yet.'; + echo '</td><td class="right"><small>'. $no_threads_msg .'</small></td>'; + } + } + + 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 '<tr><td class="left">'; + echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; + echo '<small>by <b><a href="user.php?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></b> '; + if ($show_category) { + echo 'in <b><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></b> '; + } + echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small>'; + echo '</td><td class="right">by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b><br>'; + echo '<small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>'; + } + + 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 '<blockquote></blockquote>'; + } + + $reply = mysqli_fetch_assoc($result); + + if (empty($reply)) { + return '<blockquote>Invalid quote!</blockquote>'; + } + + $id = $id + 1; + + return '<blockquote><a href="#' . $id .'">Quote from ' . $reply['user_name'] . '</a><br>' . $reply['post_content'] . '</blockquote>'; + } +} + +function display_posts($dbc, $sql_result) { + echo '<table>'; + $post_index = 1; + $thread_id = $_GET['id']; + + while ($row = mysqli_fetch_assoc($sql_result)) { + echo '<tr><th></th><th>' . $post_index . '</th></tr>'; + echo '<tr class="post" id=' . $post_index . '><td>Posted by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a><br><small>' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '</small></td>'; + + $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", + '<br><iframe class="youtube-embed" src="//www.youtube.com/embed/$2" allowfullscreen></iframe>', $post_content); + // Replace Image URLs with embedded images. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:</\w+>|/?>))@i', '<img class="image-embed" src="http$2://$3" alt="http$2://$3" />', $post_content); + // Replace other URLs with links. + $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:</\w+>|/?>))@i', '<a href="http$2://$3">$0</a>', $post_content); + + echo '<td class="post-content">' . $post_content . '</td></tr>'; + + $post_index++; + } + echo '</table>'; +}
\ 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 @@ -<?php - -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 '<tr><td class="left">'; - echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>'; - echo '<small>by <b><a href="user.php?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></b> '; - if ($show_category) { - echo 'in <b><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></b> '; - } - echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small>'; - echo '</td><td class="right">by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b><br>'; - echo '<small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>'; - } - - mysqli_stmt_close($stmt); -}
\ No newline at end of file @@ -1,21 +1,22 @@ -<?php -include_once 'header.php'; -?> +<?php include_once 'header.php';?> <section> <h2>Welcome to the cflip.net forum!</h2> - Latest Updates: - <ul> - <li>5 latest threads are displayed on the homepage</li> - </ul> + <p> + 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, + <i>for the most part</i> I don't care that much about what is posted here. Some links and buttons may not have any functionality either! + </p> + <p> + If you notice a problem or have an idea for a feature that is missing, <a href="http://51.195.90.7/forum/thread.php?id=40">reply to this thread!</a> + </p> </section> - - <table> - <tr> - <th class="left">Category</th> - <th class="right">Latest Thread</th> - </tr> +<table> + <tr> + <th class="left">Category</th> + <th class="right">Latest Thread</th> + </tr> <?php include_once 'includes/db_inc.php'; + include_once 'includes/functions_display.php'; $sql = "SELECT cat_id, cat_name, cat_description FROM categories"; $result = mysqli_query($dbc, $sql); @@ -24,46 +25,17 @@ include_once 'header.php'; die('Failure trying to display categories: ' . mysqli_error($dbc)); } - $sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = ? ORDER BY thread_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($result)) { - mysqli_stmt_bind_param($stmt, "i", $row['cat_id']); - mysqli_stmt_execute($stmt); - - $thread_res = mysqli_stmt_get_result($stmt); - $thread = mysqli_fetch_assoc($thread_res); - - echo '<tr><td class="left">'; - echo '<h4><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h4>'; - echo $row['cat_description']; - if ($thread) { - echo '</td><td class="right">' . $thread['thread_subject'] . '<br>'; - echo '<small>by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b></small></td></tr>'; - } else { - $no_threads_msg = 'There are no threads in this category yet.'; - echo '</td><td class="right"><small>'. $no_threads_msg .'</small></td>'; - } - } - - mysqli_stmt_close($stmt); - mysqli_free_result($result); + display_categories($dbc, $result); ?> - </table> <table> <tr> <th class="left">Latest Threads <a href="all.php">View All</a></th> <th class="right">Latest Post</th> </tr> - <?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 LIMIT 5"; $result = mysqli_query($dbc, $sql); @@ -75,7 +47,5 @@ include_once 'header.php'; display_threads($dbc, $result, true); mysqli_free_result($result); ?> - </table> - <?php include_once 'footer.php';?>
\ No newline at end of file @@ -2,109 +2,66 @@ <?php include_once 'includes/db_inc.php'; +include_once 'includes/functions_display.php'; -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 '<blockquote></blockquote>'; - } - - $reply = mysqli_fetch_assoc($result); - - if (empty($reply)) { - return '<blockquote>Invalid quote!</blockquote>'; - } - - $id = $id + 1; +if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { + echo '<section>Unknown category.</section>'; +} 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 '<blockquote><a href="#' . $id .'">Quote from ' . $reply['user_name'] . '</a><br>' . $reply['post_content'] . '</blockquote>'; + 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 '<section><h1>' . $row['thread_subject'] . '</h1>'; - echo 'Created by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</section>'; - $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 '<section><h1>' . $row['thread_subject'] . '</h1>'; + echo 'Created by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a> on ' . date('M d, Y', strtotime($row['thread_date'])) . '</section>'; + $thread_id = $row['thread_id']; + } } -} -echo '</section>'; + echo '</section>'; -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 '<section>This thread has no posts</section>'; -} else { - echo '<table>'; - $post_index = 1; - $thread_id = $_GET['id']; - - while ($row = mysqli_fetch_assoc($result)) { - echo '<tr><th></th><th>' . $post_index . '</th></tr>'; - echo '<tr class="post" id=' . $post_index . '><td>Posted by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a><br><small>' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '</small></td>'; - - $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", - '<br><iframe class="youtube-embed" src="//www.youtube.com/embed/$2" allowfullscreen></iframe>', $post_content); - - $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:</\w+>|/?>))@i', '<img class="image-embed" src="http$2://$3" alt="http$2://$3" />', $post_content); - //$post_content = preg_replace('/^>/', '<span class="greentext">garb</span>', $post_content); - $post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:</\w+>|/?>))@i', '<a href="http$2://$3">$0</a>', $post_content); - - echo '<td class="post-content">' . $post_content . '</td></tr>'; - - $post_index++; + if (!$result) { + die('Error trying to display posts: ' . mysqli_error($dbc)); } - echo '</table>'; -} -mysqli_free_result($result); + if (mysqli_num_rows($result) == 0) { + echo '<section>This thread has no posts</section>'; + } else { + display_posts($dbc, $result); + } -if (isset($_SESSION['signed_in'])) { - echo ' - <section> - <form action="includes/reply_inc.php?reply_to=' . $thread_id .'" method="post"> - <h2>Reply to this thread</h2> - <i>Quote a post with ># and the number above the post (example: >#7)</i> - <textarea name="reply_content"></textarea> - <br> - <input type="submit" name="submit"> - </form> - </section> - '; -} else { - echo ' - <section> - <a href="signin.php">Sign in</a> to reply to this thread</a> - </section> - '; + mysqli_free_result($result); + + if (isset($_SESSION['signed_in'])) { + echo ' + <section> + <form action="includes/reply_inc.php?reply_to=' . $thread_id .'" method="post"> + <h2>Reply to this thread</h2> + <i>Quote a post with ># and the number above the post (example: >#7)</i> + <textarea name="reply_content"></textarea> + <br> + <input type="submit" name="submit"> + </form> + </section> + '; + } else { + echo ' + <section> + <a href="signin.php">Sign in</a> to reply to this thread</a> + </section> + '; + } } include_once 'footer.php'; @@ -1,7 +1,7 @@ <?php include_once 'header.php'; include_once 'includes/db_inc.php'; -include_once 'includes/functions_inc.php'; +include_once 'includes/functions_display.php'; ?> <?php @@ -33,7 +33,7 @@ if (!isset($_GET['id'])) { echo 'Member since '. date('M d, Y', strtotime($user['user_date'])); } - if ($_SESSION['user_id'] == $_GET['id']) { + if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $_GET['id']) { echo '<br><p><a href=change_passw.php>Change Password</a></p>'; } |