summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-03-21 17:29:58 -0600
committercflip <36554078+cflip@users.noreply.github.com>2021-03-21 17:29:58 -0600
commit7ef208fc1ae5a24d7b3cd2e22e969285fbf7262a (patch)
tree7995fdce15426408ea1f893c6842dc63cd2b2fae
parentd0e23fd32cd2c968bdb905604c543b8c1bb8f6ee (diff)
Add additional classes and functions
-rw-r--r--model/Category.php48
-rw-r--r--model/Post.php82
-rw-r--r--model/Thread.php55
-rw-r--r--model/User.php54
4 files changed, 236 insertions, 3 deletions
diff --git a/model/Category.php b/model/Category.php
index d98b08b..ffd903c 100644
--- a/model/Category.php
+++ b/model/Category.php
@@ -1,5 +1,7 @@
<?php
+include_once 'Thread.php';
+
class Category {
public $id = 0;
public $name = 'Unknown';
@@ -30,7 +32,49 @@ class Category {
mysqli_free_result($result);
}
- function get_threads() {
-
+ function get_threads($dbc) {
+ $sql = "SELECT thread_id FROM threads WHERE thread_category = " . $this->id;
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Could not get threads from category: ' . mysqli_error($dbc);
+ }
+
+ $threads = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $thread = new Thread();
+ $thread->get_from_database($row['thread_id'], $dbc);
+ array_push($threads, $thread);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $threads;
+ }
+}
+
+function get_all_categories($dbc) {
+ $sql = "SELECT cat_id FROM categories";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get categories: ' . mysqli_error($dbc);
}
+
+ $categories = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $category = new Category();
+ $category->get_from_database($row['cat_id'], $dbc);
+ array_push($categories, $category);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $categories;
} \ No newline at end of file
diff --git a/model/Post.php b/model/Post.php
new file mode 100644
index 0000000..bcaff29
--- /dev/null
+++ b/model/Post.php
@@ -0,0 +1,82 @@
+<?php
+
+include_once 'Thread.php';
+
+class Post {
+ public $id;
+ public $content;
+ public $date;
+ public $thread;
+ public $author;
+
+ function get_from_database($id, $dbc) {
+ // TODO: Potential SQL injection risk?
+ $sql = "SELECT post_content, post_date, 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) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $this->id = $id;
+ $this->content = $row['post_content'];
+ $this->date = $row['post_date'];
+
+ $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);
+ }
+
+ function display_content() {
+ echo '<div>#' . $this->id . ' Posted by <a href="/forum/user/'. $this->author->name .'">' . $this->author->name . '</a> on ' . date('m/d/Y g:ia', strtotime($this->date)) . '<br></div>';
+
+ $post_content = $this->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 $post_content;
+ }
+}
+
+function get_all_posts($dbc) {
+ $sql = "SELECT post_id FROM posts";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get posts: ' . mysqli_error($dbc);
+ }
+
+ $posts = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $post = new Post();
+ $post->get_from_database($row['post_id'], $dbc);
+ array_push($posts, $post);
+ }
+ }
+
+ mysqli_free_result($result);
+ return $posts;
+} \ No newline at end of file
diff --git a/model/Thread.php b/model/Thread.php
index 78d2614..ade24b5 100644
--- a/model/Thread.php
+++ b/model/Thread.php
@@ -1,6 +1,8 @@
<?php
include_once 'Category.php';
+include_once 'User.php';
+include_once 'Post.php';
class Thread {
public $id = 0;
@@ -11,7 +13,7 @@ class Thread {
public $author;
function get_from_database($id, $dbc) {
- $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category FROM threads WHERE thread_id = " . mysqli_real_escape_string($dbc, $id);
+ $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author FROM threads WHERE thread_id = " . mysqli_real_escape_string($dbc, $id);
$result = mysqli_query($dbc, $sql);
if (!$result) {
@@ -29,7 +31,58 @@ class Thread {
$this->category = new Category();
$this->category->get_from_database($row['thread_category'], $dbc);
+
+ $this->author = new User();
+ $this->author->get_by_id($row['thread_author'], $dbc);
+ }
+ }
+
+ mysqli_free_result($result);
+ }
+
+ function get_posts($dbc) {
+ $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id;
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Could not get posts from thread: ' . mysqli_error($dbc);
+ }
+
+ $posts = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $post = new Post();
+ $post->get_from_database($row['post_id'], $dbc);
+ array_push($posts, $post);
}
}
+
+ mysqli_free_result($result);
+ return $posts;
+ }
+}
+
+function get_all_threads($dbc) {
+ $sql = "SELECT thread_id FROM threads";
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get threads: ' . mysqli_error($dbc);
+ }
+
+ $threads = array();
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $thread = new Thread();
+ $thread->get_from_database($row['thread_id'], $dbc);
+ array_push($threads, $thread);
+ }
}
+
+ mysqli_free_result($result);
+ return $threads;
} \ No newline at end of file
diff --git a/model/User.php b/model/User.php
new file mode 100644
index 0000000..89a14a5
--- /dev/null
+++ b/model/User.php
@@ -0,0 +1,54 @@
+<?php
+
+class User {
+ public $id;
+ public $name = 'Unknown';
+ public $date = 0;
+
+ function get_by_name($name, $dbc) {
+ $sql = "SELECT user_id, user_date FROM users WHERE user_name = ?";
+ $stmt = mysqli_stmt_init($dbc);
+
+ if (!mysqli_stmt_prepare($stmt, $sql)) {
+ echo 'Failed to get user: ' . mysqli_error($dbc);
+ }
+
+ mysqli_stmt_bind_param($stmt, "s", $name);
+ mysqli_stmt_execute($stmt);
+
+ $result = mysqli_stmt_get_result($stmt);
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $this->id = $row['user_id'];
+ $this->name = $name;
+ $this->date = $row['user_date'];
+ }
+ }
+
+ mysqli_free_result($result);
+ mysqli_stmt_close($stmt);
+ }
+
+ function get_by_id($id, $dbc) {
+ $sql = "SELECT user_name, user_date FROM users WHERE user_id = " . mysqli_real_escape_string($dbc, $id);
+ $result = mysqli_query($dbc, $sql);
+
+ if (!$result) {
+ echo 'Failed to get user: ' . mysqli_error($dbc);
+ }
+
+ if (mysqli_num_rows($result) == 0) {
+ } else {
+ while ($row = mysqli_fetch_assoc($result)) {
+ $this->id = $id;
+ $this->name = $row['user_name'];
+ $this->date = $row['user_date'];
+ }
+ }
+
+ mysqli_free_result($result);
+ }
+
+} \ No newline at end of file