2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2012-09-25 17:33:56 +00:00
|
|
|
/*
|
|
|
|
Todo:
|
|
|
|
*Quote buttons on posts
|
|
|
|
*Move delete and quote buttons away from each other
|
|
|
|
*Bring us on par with comment extension(post linking, image linking, thumb links, URL autolink)
|
|
|
|
*Smiley filter, word filter, etc should work with our extension
|
|
|
|
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class Forum extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var ForumTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2024-06-19 13:16:01 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_int("forumTitleSubString", 25);
|
|
|
|
$config->set_default_int("forumThreadsPerPage", 15);
|
|
|
|
$config->set_default_int("forumPostsPerPage", 15);
|
|
|
|
$config->set_default_int("forumMaxCharsPerPost", 512);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database;
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// shortcut to latest
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("forum_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("forum_threads", "
|
2012-03-11 02:03:28 +00:00
|
|
|
id SCORE_AIPK,
|
2020-10-26 22:37:25 +00:00
|
|
|
sticky BOOLEAN NOT NULL DEFAULT FALSE,
|
2012-03-11 02:03:28 +00:00
|
|
|
title VARCHAR(255) NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
2019-11-03 19:25:51 +00:00
|
|
|
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
uptodate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2012-03-11 02:03:28 +00:00
|
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX forum_threads_date_idx ON forum_threads(date)", []);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("forum_posts", "
|
2012-03-11 02:03:28 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
thread_id INTEGER NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
2019-11-03 19:25:51 +00:00
|
|
|
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2012-03-11 02:03:28 +00:00
|
|
|
message TEXT,
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT,
|
|
|
|
FOREIGN KEY (thread_id) REFERENCES forum_threads (id) ON UPDATE CASCADE ON DELETE CASCADE
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX forum_posts_date_idx ON forum_posts(date)", []);
|
|
|
|
|
2020-10-29 00:57:58 +00:00
|
|
|
$this->set_version("forum_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("forum_version") < 2) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE forum_threads ADD FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT");
|
|
|
|
$database->execute("ALTER TABLE forum_posts ADD FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT");
|
2020-10-29 00:57:58 +00:00
|
|
|
$this->set_version("forum_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("forum_version") < 3) {
|
2020-10-26 20:15:34 +00:00
|
|
|
$database->standardise_boolean("forum_threads", "sticky");
|
2020-10-29 00:57:58 +00:00
|
|
|
$this->set_version("forum_version", 3);
|
2020-10-26 20:15:34 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Forum");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_int_option("forumTitleSubString", "Title max long: ");
|
|
|
|
$sb->add_int_option("forumThreadsPerPage", "<br>Threads per page: ");
|
|
|
|
$sb->add_int_option("forumPostsPerPage", "<br>Posts per page: ");
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_int_option("forumMaxCharsPerPost", "<br>Max chars per post: ");
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserPageBuilding(UserPageBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$threads_count = $database->get_one("SELECT COUNT(*) FROM forum_threads WHERE user_id=:user_id", ['user_id' => $event->display_user->id]);
|
|
|
|
$posts_count = $database->get_one("SELECT COUNT(*) FROM forum_posts WHERE user_id=:user_id", ['user_id' => $event->display_user->id]);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-02-20 00:22:25 +00:00
|
|
|
$days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2009-11-24 14:07:18 +00:00
|
|
|
$threads_rate = sprintf("%.1f", ($threads_count / $days_old));
|
2019-05-28 16:59:38 +00:00
|
|
|
$posts_rate = sprintf("%.1f", ($posts_count / $days_old));
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-03-28 12:42:39 +00:00
|
|
|
$event->add_part("Forum threads: $threads_count, $threads_rate per day");
|
|
|
|
$event->add_part("Forum posts: $posts_count, $posts_rate per day");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageNavBuilding(PageNavBuildingEvent $event): void
|
2020-09-18 23:18:45 +00:00
|
|
|
{
|
2020-09-18 23:20:09 +00:00
|
|
|
$event->add_nav_link("forum", new Link('forum/index'), "Forum");
|
2020-09-18 23:18:45 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $user;
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("forum/index", paged: true)) {
|
|
|
|
$pageNumber = $event->get_iarg('page_num', 1) - 1;
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->show_last_threads($page, $pageNumber, $user->can(Permissions::FORUM_ADMIN));
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($user->can(Permissions::FORUM_CREATE)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->theme->display_new_thread_composer($page);
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("forum/view/{threadID}", paged: true)) {
|
|
|
|
$threadID = $event->get_iarg('threadID');
|
|
|
|
$pageNumber = $event->get_iarg('page_num', 1) - 1;
|
2024-02-10 23:03:14 +00:00
|
|
|
$errors = $this->sanity_check_viewed_thread($threadID);
|
|
|
|
|
|
|
|
if (count($errors) > 0) {
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new InvalidInput(implode("<br>", $errors));
|
2024-02-10 23:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->show_posts($threadID, $pageNumber, $user->can(Permissions::FORUM_ADMIN));
|
|
|
|
if ($user->can(Permissions::FORUM_ADMIN)) {
|
|
|
|
$this->theme->add_actions_block($page, $threadID);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($user->can(Permissions::FORUM_CREATE)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->theme->display_new_post_composer($page, $threadID);
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($event->page_matches("forum/new", permission: Permissions::FORUM_CREATE)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->theme->display_new_thread_composer($page);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($event->page_matches("forum/create", permission: Permissions::FORUM_CREATE)) {
|
|
|
|
$errors = $this->sanity_check_new_thread();
|
|
|
|
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
throw new InvalidInput(implode("<br>", $errors));
|
2024-02-10 23:03:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 11:13:09 +00:00
|
|
|
$newThreadID = $this->save_new_thread($user);
|
|
|
|
$this->save_new_post($newThreadID, $user);
|
|
|
|
$redirectTo = "forum/view/" . $newThreadID . "/1";
|
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link($redirectTo));
|
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($event->page_matches("forum/delete/{threadID}/{postID}", permission: Permissions::FORUM_ADMIN)) {
|
2024-02-11 11:34:09 +00:00
|
|
|
$threadID = $event->get_iarg('threadID');
|
|
|
|
$postID = $event->get_iarg('postID');
|
2024-02-21 11:13:09 +00:00
|
|
|
$this->delete_post($postID);
|
2024-02-10 23:03:14 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("forum/view/" . $threadID));
|
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($event->page_matches("forum/nuke/{threadID}", permission: Permissions::FORUM_ADMIN)) {
|
2024-02-11 11:34:09 +00:00
|
|
|
$threadID = $event->get_iarg('threadID');
|
2024-02-21 11:13:09 +00:00
|
|
|
$this->delete_thread($threadID);
|
2024-02-10 23:03:14 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("forum/index"));
|
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
if ($event->page_matches("forum/answer", permission: Permissions::FORUM_CREATE)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
$threadID = int_escape($event->req_POST("threadID"));
|
|
|
|
$total_pages = $this->get_total_pages_for_thread($threadID);
|
|
|
|
|
2024-02-21 11:13:09 +00:00
|
|
|
$errors = $this->sanity_check_new_post();
|
|
|
|
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
throw new InvalidInput(implode("<br>", $errors));
|
2024-02-10 23:03:14 +00:00
|
|
|
}
|
2024-02-21 11:13:09 +00:00
|
|
|
$this->save_new_post($threadID, $user);
|
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("forum/view/" . $threadID . "/" . $total_pages));
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function get_total_pages_for_thread(int $threadID): int
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $config;
|
2024-01-04 16:08:53 +00:00
|
|
|
$result = $database->get_row("
|
|
|
|
SELECT COUNT(1) AS count
|
|
|
|
FROM forum_posts
|
|
|
|
WHERE thread_id = :thread_id
|
|
|
|
", ['thread_id' => $threadID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
return (int) ceil($result["count"] / $config->get_int("forumPostsPerPage"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
private function sanity_check_new_thread(): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!array_key_exists("title", $_POST)) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "No title supplied.";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (strlen($_POST["title"]) == 0) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "You cannot have an empty title.";
|
|
|
|
} elseif (strlen($_POST["title"]) > 255) {
|
|
|
|
$errors[] = "Your title is too long.";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists("message", $_POST)) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "No message supplied.";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (strlen($_POST["message"]) == 0) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "You cannot have an empty message.";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:08:53 +00:00
|
|
|
return $errors;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
private function sanity_check_new_post(): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!array_key_exists("threadID", $_POST)) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "No thread ID supplied.";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (strlen($_POST["threadID"]) == 0) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "No thread ID supplied.";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (is_numeric($_POST["threadID"])) {
|
|
|
|
if (!array_key_exists("message", $_POST)) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "No message supplied.";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (strlen($_POST["message"]) == 0) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "You cannot have an empty message.";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 16:08:53 +00:00
|
|
|
return $errors;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:08:53 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
private function sanity_check_viewed_thread(int $threadID): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!$this->threadExists($threadID)) {
|
2024-01-04 16:08:53 +00:00
|
|
|
$errors[] = "Inexistent thread.";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-01-04 16:08:53 +00:00
|
|
|
return $errors;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function get_thread_title(int $threadID): string
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2024-01-04 16:08:53 +00:00
|
|
|
return $database->get_one("SELECT t.title FROM forum_threads AS t WHERE t.id = :id ", ['id' => $threadID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
private function show_last_threads(Page $page, int $pageNumber, bool $showAdminOptions = false): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database;
|
|
|
|
$threadsPerPage = $config->get_int('forumThreadsPerPage', 15);
|
2024-02-10 23:03:14 +00:00
|
|
|
$totalPages = (int) ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$threads = $database->get_all(
|
2024-02-10 23:03:14 +00:00
|
|
|
"SELECT f.id, f.sticky, f.title, f.date, f.uptodate, u.name AS user_name, u.email AS user_email, u.class AS user_class, sum(1) - 1 AS response_count " .
|
|
|
|
"FROM forum_threads AS f " .
|
|
|
|
"INNER JOIN users AS u " .
|
|
|
|
"ON f.user_id = u.id " .
|
|
|
|
"INNER JOIN forum_posts AS p " .
|
|
|
|
"ON p.thread_id = f.id " .
|
|
|
|
"GROUP BY f.id, f.sticky, f.title, f.date, u.name, u.email, u.class " .
|
|
|
|
"ORDER BY f.sticky ASC, f.uptodate DESC LIMIT :limit OFFSET :offset",
|
2023-11-11 21:49:12 +00:00
|
|
|
["limit" => $threadsPerPage, "offset" => $pageNumber * $threadsPerPage]
|
2019-11-02 19:57:34 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$this->theme->display_thread_list($page, $threads, $showAdminOptions, $pageNumber + 1, $totalPages);
|
|
|
|
}
|
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
private function show_posts(int $threadID, int $pageNumber, bool $showAdminOptions = false): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database;
|
|
|
|
$postsPerPage = $config->get_int('forumPostsPerPage', 15);
|
2024-02-10 23:03:14 +00:00
|
|
|
$totalPages = (int) ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = :id", ['id' => $threadID]) / $postsPerPage);
|
2019-05-28 16:59:38 +00:00
|
|
|
$threadTitle = $this->get_thread_title($threadID);
|
|
|
|
|
|
|
|
$posts = $database->get_all(
|
2024-02-10 23:03:14 +00:00
|
|
|
"SELECT p.id, p.date, p.message, u.name as user_name, u.email AS user_email, u.class AS user_class " .
|
|
|
|
"FROM forum_posts AS p " .
|
|
|
|
"INNER JOIN users AS u " .
|
|
|
|
"ON p.user_id = u.id " .
|
|
|
|
"WHERE thread_id = :thread_id " .
|
|
|
|
"ORDER BY p.date ASC " .
|
|
|
|
"LIMIT :limit OFFSET :offset",
|
2023-11-11 21:49:12 +00:00
|
|
|
["thread_id" => $threadID, "offset" => $pageNumber * $postsPerPage, "limit" => $postsPerPage]
|
2019-11-02 19:57:34 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_thread($posts, $showAdminOptions, $threadTitle, $threadID, $pageNumber + 1, $totalPages);
|
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function save_new_thread(User $user): int
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-04 16:08:53 +00:00
|
|
|
$title = $_POST["title"];
|
2020-10-26 20:15:34 +00:00
|
|
|
$sticky = !empty($_POST["sticky"]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
global $database;
|
2023-06-25 20:31:11 +00:00
|
|
|
$database->execute(
|
2019-05-28 16:59:38 +00:00
|
|
|
"
|
2016-06-19 16:41:40 +00:00
|
|
|
INSERT INTO forum_threads
|
|
|
|
(title, sticky, user_id, date, uptodate)
|
|
|
|
VALUES
|
2023-06-25 20:31:11 +00:00
|
|
|
(:title, :sticky, :user_id, now(), now())",
|
2023-11-11 21:49:12 +00:00
|
|
|
['title' => $title, 'sticky' => $sticky, 'user_id' => $user->id]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
2016-06-19 16:41:40 +00:00
|
|
|
|
2023-06-25 20:31:11 +00:00
|
|
|
$threadID = $database->get_last_insert_id("forum_threads_id_seq");
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("forum", "Thread {$threadID} created by {$user->name}");
|
2016-06-19 16:41:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
return $threadID;
|
|
|
|
}
|
2016-06-19 16:41:40 +00:00
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function save_new_post(int $threadID, User $user): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$userID = $user->id;
|
2024-01-04 16:08:53 +00:00
|
|
|
$message = $_POST["message"];
|
2016-06-19 16:41:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$max_characters = $config->get_int('forumMaxCharsPerPost');
|
|
|
|
$message = substr($message, 0, $max_characters);
|
2016-06-19 16:41:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
global $database;
|
2023-06-25 20:31:11 +00:00
|
|
|
$database->execute("
|
2019-11-27 11:22:46 +00:00
|
|
|
INSERT INTO forum_posts (thread_id, user_id, date, message)
|
|
|
|
VALUES (:thread_id, :user_id, now(), :message)
|
2023-11-11 21:49:12 +00:00
|
|
|
", ['thread_id' => $threadID, 'user_id' => $userID, 'message' => $message]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-06-25 20:31:11 +00:00
|
|
|
$postID = $database->get_last_insert_id("forum_posts_id_seq");
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("forum", "Post {$postID} created by {$user->name}");
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE forum_threads SET uptodate=now() WHERE id=:id", ['id' => $threadID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function delete_thread(int $threadID): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("DELETE FROM forum_threads WHERE id = :id", ['id' => $threadID]);
|
|
|
|
$database->execute("DELETE FROM forum_posts WHERE thread_id = :thread_id", ['thread_id' => $threadID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function delete_post(int $postID): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("DELETE FROM forum_posts WHERE id = :id", ['id' => $postID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 23:43:50 +00:00
|
|
|
private function threadExists(int $threadID): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$result = $database->get_one("SELECT EXISTS (SELECT * FROM forum_threads WHERE id=:id)", ['id' => $threadID]);
|
2019-11-27 11:22:46 +00:00
|
|
|
return $result == 1;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
}
|