misuzu/public/forum/posting.php

213 lines
6.3 KiB
PHP
Raw Normal View History

2018-05-22 01:05:25 +02:00
<?php
require_once '../../misuzu.php';
2018-05-22 01:05:25 +02:00
if (!user_session_active()) {
echo render_error(401);
return;
}
if (user_warning_check_restriction(user_session_current('user_id', 0))) {
echo render_error(403);
2018-05-22 01:05:25 +02:00
return;
}
2018-12-30 04:02:35 +01:00
$forumPostingModes = [
'create', 'edit', 'quote',
'delete', 'restore', 'nuke',
];
2018-12-27 05:25:51 +01:00
if (!empty($_POST)) {
2018-12-30 04:02:35 +01:00
$mode = $_POST['post']['mode'] ?? 'create';
$postId = max(0, (int)($_POST['post']['id'] ?? 0));
2018-05-22 01:05:25 +02:00
$topicId = max(0, (int)($_POST['post']['topic'] ?? 0));
$forumId = max(0, (int)($_POST['post']['forum'] ?? 0));
} else {
2018-12-30 04:02:35 +01:00
$mode = $_GET['m'] ?? 'create';
2018-05-22 01:05:25 +02:00
$postId = max(0, (int)($_GET['p'] ?? 0));
$topicId = max(0, (int)($_GET['t'] ?? 0));
$forumId = max(0, (int)($_GET['f'] ?? 0));
}
2018-12-30 04:02:35 +01:00
if (!in_array($mode, $forumPostingModes, true)) {
echo render_error(400);
return;
}
if (empty($postId) && empty($topicId) && empty($forumId)) {
echo render_error(404);
return;
}
2018-05-22 01:05:25 +02:00
if (!empty($postId)) {
2018-12-30 04:02:35 +01:00
$post = forum_post_get($postId);
2018-05-22 01:05:25 +02:00
if (isset($post['topic_id'])) { // should automatic cross-quoting be a thing? if so, check if $topicId is < 1 first
$topicId = (int)$post['topic_id'];
}
}
if (!empty($topicId)) {
$getTopic = db_prepare('
2018-05-23 03:41:57 +02:00
SELECT `topic_id`, `forum_id`, `topic_title`, `topic_locked`
2018-05-22 01:05:25 +02:00
FROM `msz_forum_topics`
WHERE `topic_id` = :topic_id
');
$getTopic->bindValue('topic_id', $topicId);
2018-12-27 05:25:51 +01:00
$topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false;
2018-05-22 01:05:25 +02:00
if (isset($topic['forum_id'])) {
$forumId = (int)$topic['forum_id'];
}
}
if (!empty($forumId)) {
$getForum = db_prepare('
2018-05-23 03:41:57 +02:00
SELECT `forum_id`, `forum_name`, `forum_type`, `forum_archived`
2018-05-22 01:05:25 +02:00
FROM `msz_forum_categories`
WHERE `forum_id` = :forum_id
');
$getForum->bindValue('forum_id', $forumId);
2018-12-27 05:25:51 +01:00
$forum = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false;
2018-05-22 01:05:25 +02:00
}
if (empty($forum)) {
echo render_error(404);
return;
}
2018-12-30 04:02:35 +01:00
$perms = forum_perms_get_user(MSZ_FORUM_PERMS_GENERAL, $forum['forum_id'], user_session_current('user_id'));
if ($forum['forum_archived']
|| !empty($topic['topic_locked'])
|| !perms_check($perms, MSZ_FORUM_PERM_VIEW_FORUM | MSZ_FORUM_PERM_CREATE_POST)
|| (empty($topic) && !perms_check($perms, MSZ_FORUM_PERM_CREATE_TOPIC))) {
echo render_error(403);
return;
}
if (!forum_may_have_topics($forum['forum_type'])) {
echo render_error(400);
2018-05-23 03:41:57 +02:00
return;
}
2018-12-30 04:02:35 +01:00
// edit mode stuff
if ($mode === 'edit') {
if (empty($post)) {
echo render_error(404);
return;
}
if (!perms_check($perms, $post['poster_id'] === user_session_current('user_id') ? MSZ_FORUM_PERM_EDIT_POST : MSZ_FORUM_PERM_EDIT_ANY_POST)) {
echo render_error(403);
return;
}
}
2018-12-27 05:25:51 +01:00
$notices = [];
2018-05-23 03:41:57 +02:00
2018-12-27 05:25:51 +01:00
if (!empty($_POST)) {
if (!csrf_verify('forum_post', $_POST['csrf'] ?? '')) {
$notices[] = 'Could not verify request.';
2018-05-22 01:05:25 +02:00
} else {
2018-12-27 05:25:51 +01:00
$topicTitle = $_POST['post']['title'] ?? '';
$setTopicTitle = empty($topic) || ($mode === 'edit' && $post['is_opening_post'] && $topicTitle !== $topic['topic_title']);
2018-12-27 05:25:51 +01:00
$postText = $_POST['post']['text'] ?? '';
$postParser = (int)($_POST['post']['parser'] ?? MSZ_PARSER_BBCODE);
if (!parser_is_valid($postParser)) {
$notices[] = 'Invalid parser selected.';
}
switch (forum_validate_post($postText)) {
2018-05-23 03:41:57 +02:00
case 'too-short':
2018-12-27 05:25:51 +01:00
$notices[] = 'Post content was too short.';
break;
2018-05-23 03:41:57 +02:00
case 'too-long':
2018-12-27 05:25:51 +01:00
$notices[] = 'Post content was too long.';
break;
2018-05-23 03:41:57 +02:00
}
if ($setTopicTitle) {
switch (forum_validate_title($topicTitle)) {
2018-12-27 05:25:51 +01:00
case 'too-short':
$notices[] = 'Topic title was too short.';
break;
2018-05-22 01:05:25 +02:00
2018-12-27 05:25:51 +01:00
case 'too-long':
$notices[] = 'Topic title was too long.';
break;
}
}
if (empty($notices)) {
2018-12-30 04:02:35 +01:00
switch ($mode) {
case 'create':
if (!empty($topic)) {
forum_topic_bump($topic['topic_id']);
} else {
$topicId = forum_topic_create($forum['forum_id'], user_session_current('user_id', 0), $topicTitle);
}
$postId = forum_post_create(
$topicId,
$forum['forum_id'],
user_session_current('user_id', 0),
ip_remote_address(),
$postText,
$postParser
);
forum_topic_mark_read(user_session_current('user_id', 0), $topicId, $forum['forum_id']);
break;
case 'edit':
if (!forum_post_update($postId, ip_remote_address(), $postText, $postParser)) {
2018-12-30 04:02:35 +01:00
$notices[] = 'Post edit failed.';
}
if ($setTopicTitle) {
if (!forum_topic_update($topicId, $topicTitle)) {
$notices[] = 'Topic update failed.';
}
}
2018-12-30 04:02:35 +01:00
break;
2018-12-27 05:25:51 +01:00
}
2018-12-30 04:02:35 +01:00
if (empty($notices)) {
header("Location: /forum/topic.php?p={$postId}#p{$postId}");
return;
}
2018-12-27 05:25:51 +01:00
}
}
2018-05-22 01:05:25 +02:00
}
2018-05-22 02:54:20 +02:00
if (!empty($topic)) {
2018-08-15 03:12:58 +02:00
tpl_var('posting_topic', $topic);
2018-05-22 02:54:20 +02:00
}
2018-05-22 01:05:25 +02:00
2018-12-30 04:02:35 +01:00
if ($mode === 'edit') { // $post is pretty much sure to be populated at this point
tpl_var('posting_post', $post);
}
2018-12-27 05:25:51 +01:00
// fetches additional data for simulating a forum post
$getDisplayInfo = db_prepare('
SELECT u.`user_country`, u.`user_created`, (
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `user_id` = u.`user_id`
) AS `user_forum_posts`
FROM `msz_users` as u
WHERE `user_id` = :user_id
');
$getDisplayInfo->bindValue('user_id', user_session_current('user_id'));
$displayInfo = $getDisplayInfo->execute() ? $getDisplayInfo->fetch(PDO::FETCH_ASSOC) : [];
2018-08-15 03:12:58 +02:00
echo tpl_render('forum.posting', [
2018-05-23 03:41:57 +02:00
'posting_breadcrumbs' => forum_get_breadcrumbs($forumId),
2018-10-22 00:11:14 +02:00
'global_accent_colour' => forum_get_colour($forumId),
2018-05-22 02:54:20 +02:00
'posting_forum' => $forum,
2018-12-27 05:25:51 +01:00
'posting_info' => $displayInfo,
'posting_notices' => $notices,
2018-12-30 04:02:35 +01:00
'posting_mode' => $mode,
2018-05-22 02:54:20 +02:00
]);