diff options
Diffstat (limited to 'src/application/actions/viewtopic')
| -rw-r--r-- | src/application/actions/viewtopic/_common.php | 13 | ||||
| -rw-r--r-- | src/application/actions/viewtopic/get.php | 76 | ||||
| -rw-r--r-- | src/application/actions/viewtopic/post.php | 64 | 
3 files changed, 153 insertions, 0 deletions
| diff --git a/src/application/actions/viewtopic/_common.php b/src/application/actions/viewtopic/_common.php new file mode 100644 index 0000000..7f249bb --- /dev/null +++ b/src/application/actions/viewtopic/_common.php @@ -0,0 +1,13 @@ +<?php + +use mystic\forum\orm\Topic; + +$formId = "addpost"; +$topicId = $_GET["topic"] ?? throw new Exception("Missing topic id"); +$topic = new Topic(); +$topic->id = $topicId; +if (!$db->fetch($topic)) { +    http_response_code(404); +    msg_error("No topic exists with this id"); +    exit; +} diff --git a/src/application/actions/viewtopic/get.php b/src/application/actions/viewtopic/get.php new file mode 100644 index 0000000..45dc824 --- /dev/null +++ b/src/application/actions/viewtopic/get.php @@ -0,0 +1,76 @@ +<?php + +/** @var Post[] $posts */ + +use mystic\forum\orm\Attachment; +use mystic\forum\orm\Post; +use mystic\forum\orm\TopicLogMessage; +use mystic\forum\orm\User; +use mystic\forum\utils\RequestUtils; + +$posts = $db->fetchCustom(Post::class, 'WHERE topic_id = $1 ORDER BY post_date', [ $topicId ]); +/** @var TopicLogMessage[] $logMessages */ +$logMessages = $db->fetchCustom(TopicLogMessage::class, 'WHERE topic_id = $1 ORDER BY post_date', [ $topicId ]); +$userCache = []; + +$topicAuthor = null; +if ($topic->createdBy !== null) { +    $topicAuthor = new User(); +    $topicAuthor->id = $topic->createdBy; +    if (!$db->fetch($topicAuthor)) { +        $topicAuthor = null; +    } +} + +$allItems = [...$posts, ...$logMessages]; +usort($allItems, fn(Post|TopicLogMessage $a, Post|TopicLogMessage $b): int => $a->postDate <=> $b->postDate); + +_view("template_start", ["_title" => $topic->title]); +_view("template_navigation_start"); +_view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]); +_view("template_navigation_end"); +_view("view_topic_start", ["topic" => $topic, "topicAuthor" => $topicAuthor]); + +foreach ($allItems as $item) { +    /** @var ?User $postAuthor */ +    $postAuthor = null; +    if ($item->authorId !== null && !isset($userCache[$item->authorId])) { +        $usr = new User(); +        $usr->id = $item->authorId; +        if ($db->fetch($usr)) +            $userCache[$item->authorId] = &$usr; +    } +    if (isset($userCache[$item->authorId])) +        $postAuthor = &$userCache[$item->authorId]; + +    if ($item instanceof Post) { +        $attachments = $db->fetchCustom(Attachment::class, 'WHERE post_id = $1', [ $item->id ]); + +        _view("view_post", [ +            "post" => $item, +            "postAuthor" => $postAuthor, +            "topicAuthor" => $topicAuthor, +            "attachments" => $attachments, +            "topic" => $topic, +        ]); +    } else { +        _view("view_topiclog", [ +            "logMessage" => $item, +            "postAuthor" => $postAuthor, +            "topicAuthor" => $topicAuthor, +            "topic" => $topic, +        ]); +    } +} + +_view("view_topic_end"); + +if ($topic->isLocked) { +    _view("view_topic_locked"); +} elseif ($currentUser) { +    _view("form_addpost"); +} else { +    _view("view_logintoreply"); +} + +_view("template_end", [...getThemeAndLangInfo()]); diff --git a/src/application/actions/viewtopic/post.php b/src/application/actions/viewtopic/post.php new file mode 100644 index 0000000..1038222 --- /dev/null +++ b/src/application/actions/viewtopic/post.php @@ -0,0 +1,64 @@ +<?php + +use mystic\forum\orm\Attachment; +use mystic\forum\orm\Post; +use mystic\forum\utils\RequestUtils; + +if (!$currentUser) { +    http_response_code(403); +    msg_error("You need to be logged in to add new posts!"); +    exit; +} + +if ($topic->isLocked) { +    http_response_code(403); +    msg_error("This topic is locked!"); +    exit; +} + +$attachments = reArrayFiles($_FILES["files"]); + +if (count($attachments) > MAX_ATTACHMENT_COUNT) +    RequestUtils::triggerFormError(__("Too many attachments"), $formId); + +// check all attachments before saving one +foreach ($attachments as $att) { +    if ($att["size"] > MAX_ATTACHMENT_SIZE) { +        RequestUtils::triggerFormError(__("Individual file size exceeded"), $formId); +    } +} + +$message = trim(RequestUtils::getRequiredField("message", $formId)); + +if (strlen($message) < 1 || strlen($message) > 0x8000) { +    RequestUtils::triggerFormError(__("Message too short or too long!"), $formId); +} + +$item = new Post(); +$item->id = $db->generateId(); +$item->authorId = $currentUser->id; +$item->topicId = $topicId; +$item->content = $message; +$item->postDate = new DateTimeImmutable(); +$item->deleted = false; +$item->edited = false; + +$db->insert($item); + +foreach ($attachments as $att) { +    [ +        "name" => $name, +        "type" => $type, +        "tmp_name" => $tmpName, +    ] = $att; +    $attachment = new Attachment(); +    $attachment->id = $db->generateId(); +    $attachment->name = $name; +    $attachment->mimeType = $type; +    $attachment->postId = $item->id; +    $attachment->contents = file_get_contents($tmpName); +     +    $db->insert($attachment); +} + +header("Location: ?_action=viewtopic&topic=" . urlencode($topicId) . "#form"); |