summaryrefslogtreecommitdiff
path: root/src/application/actions/newtopic/post.php
blob: 79110ebe7c8cc8d8ac298bd81598d77e0a8a099c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

use mystic\forum\orm\Attachment;
use mystic\forum\orm\Post;
use mystic\forum\orm\Subscription;
use mystic\forum\orm\Topic;
use mystic\forum\utils\RequestUtils;

$formId = "newtopic";
$title = trim(RequestUtils::getRequiredField("title", $formId));
$message = trim(RequestUtils::getRequiredField("message", $formId));

$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);
    }
}

if (strlen($title) < 1 || strlen($title) > 255) {
    RequestUtils::triggerFormError(__("Title too short or too long!"), $formId);
}

if (strlen($message) < 1 || strlen($message) > 0x8000) {
    RequestUtils::triggerFormError(__("Message too short or too long!"), $formId);
}

$topic = new Topic();
$topic->createdBy = $currentUser->id;
$topic->id = $db->generateId();
$topic->title = $title;
$topic->creationDate = new DateTimeImmutable();
$topic->isLocked = false;

$db->insert($topic);

$item = new Post();
$item->id = $db->generateId();
$item->authorId = $currentUser->id;
$item->topicId = $topic->id;
$item->content = $message;
$item->postDate = $topic->creationDate;
$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);
}

$subscription = new Subscription();
$subscription->id = $db->generateId();
$subscription->userId = $currentUser->id;
$subscription->topicId = $topic->id;
$db->insert($subscription);

header("Location: ?_action=viewtopic&topic=" . urlencode($topic->id));