2018-05-23 03:41:57 +02:00
|
|
|
<?php
|
|
|
|
function forum_post_create(
|
|
|
|
int $topicId,
|
|
|
|
int $forumId,
|
|
|
|
int $userId,
|
|
|
|
string $ipAddress,
|
2018-05-24 21:31:48 +02:00
|
|
|
string $text,
|
2018-09-21 10:56:52 +02:00
|
|
|
int $parser = MSZ_PARSER_PLAIN
|
2018-05-23 03:41:57 +02:00
|
|
|
): int {
|
2018-10-07 01:30:48 +02:00
|
|
|
$createPost = db_prepare('
|
2018-05-23 03:41:57 +02:00
|
|
|
INSERT INTO `msz_forum_posts`
|
2018-05-24 21:31:48 +02:00
|
|
|
(`topic_id`, `forum_id`, `user_id`, `post_ip`, `post_text`, `post_parse`)
|
2018-05-23 03:41:57 +02:00
|
|
|
VALUES
|
2018-05-24 21:31:48 +02:00
|
|
|
(:topic_id, :forum_id, :user_id, INET6_ATON(:post_ip), :post_text, :post_parse)
|
2018-05-23 03:41:57 +02:00
|
|
|
');
|
|
|
|
$createPost->bindValue('topic_id', $topicId);
|
|
|
|
$createPost->bindValue('forum_id', $forumId);
|
|
|
|
$createPost->bindValue('user_id', $userId);
|
|
|
|
$createPost->bindValue('post_ip', $ipAddress);
|
|
|
|
$createPost->bindValue('post_text', $text);
|
2018-05-24 21:31:48 +02:00
|
|
|
$createPost->bindValue('post_parse', $parser);
|
2018-05-23 03:41:57 +02:00
|
|
|
|
2018-10-07 01:30:48 +02:00
|
|
|
return $createPost->execute() ? db_last_insert_id() : 0;
|
2018-05-23 03:41:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 04:23:04 +01:00
|
|
|
function forum_post_update(
|
2018-12-30 04:02:35 +01:00
|
|
|
int $postId,
|
|
|
|
string $ipAddress,
|
|
|
|
string $text,
|
2018-12-30 18:23:55 +01:00
|
|
|
int $parser = MSZ_PARSER_PLAIN,
|
|
|
|
bool $bumpUpdate = true
|
2018-12-30 04:02:35 +01:00
|
|
|
): bool {
|
|
|
|
if ($postId < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updatePost = db_prepare('
|
|
|
|
UPDATE `msz_forum_posts`
|
|
|
|
SET `post_ip` = INET6_ATON(:post_ip),
|
|
|
|
`post_text` = :post_text,
|
2018-12-30 18:23:55 +01:00
|
|
|
`post_parse` = :post_parse,
|
2019-01-01 04:56:30 +01:00
|
|
|
`post_edited` = IF(:bump, NOW(), `post_edited`)
|
2018-12-30 04:02:35 +01:00
|
|
|
WHERE `post_id` = :post_id
|
|
|
|
');
|
|
|
|
$updatePost->bindValue('post_id', $postId);
|
|
|
|
$updatePost->bindValue('post_ip', $ipAddress);
|
|
|
|
$updatePost->bindValue('post_text', $text);
|
|
|
|
$updatePost->bindValue('post_parse', $parser);
|
2018-12-30 18:23:55 +01:00
|
|
|
$updatePost->bindValue('bump', $bumpUpdate ? 1 : 0);
|
2018-12-30 04:02:35 +01:00
|
|
|
|
|
|
|
return $updatePost->execute();
|
|
|
|
}
|
|
|
|
|
2019-01-04 01:11:31 +01:00
|
|
|
function forum_post_find(int $postId, int $userId): array
|
2018-05-23 03:41:57 +02:00
|
|
|
{
|
2019-01-04 01:11:31 +01:00
|
|
|
$getPostInfo = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
p.`post_id`, p.`topic_id`,
|
|
|
|
((%s) & %d) as `can_view_deleted`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = p.`topic_id`
|
|
|
|
AND `post_id` < p.`post_id`
|
|
|
|
AND (`can_view_deleted` OR `post_deleted` IS NULL)
|
|
|
|
ORDER BY `post_id`
|
|
|
|
) as `preceeding_post_count`
|
|
|
|
FROM `msz_forum_posts` AS p
|
|
|
|
WHERE p.`post_id` = :post_id
|
|
|
|
',
|
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 'p.`forum_id`'),
|
|
|
|
MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST
|
|
|
|
));
|
2018-05-23 03:41:57 +02:00
|
|
|
$getPostInfo->bindValue('post_id', $postId);
|
2019-01-04 01:11:31 +01:00
|
|
|
$getPostInfo->bindValue('perm_user_id_user', $userId);
|
|
|
|
$getPostInfo->bindValue('perm_user_id_role', $userId);
|
2018-05-23 03:41:57 +02:00
|
|
|
|
2018-12-30 04:02:35 +01:00
|
|
|
return $getPostInfo->execute() ? $getPostInfo->fetch(PDO::FETCH_ASSOC) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function forum_post_get(int $postId, bool $allowDeleted = false): array
|
|
|
|
{
|
|
|
|
$getPost = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`,
|
|
|
|
p.`topic_id`, p.`post_deleted`, p.`post_edited`,
|
2018-12-30 04:23:04 +01:00
|
|
|
INET6_NTOA(p.`post_ip`) AS `post_ip`,
|
|
|
|
u.`user_id` AS `poster_id`,
|
|
|
|
u.`username` AS `poster_name`,
|
|
|
|
u.`user_created` AS `poster_joined`,
|
|
|
|
u.`user_country` AS `poster_country`,
|
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) AS `poster_colour`,
|
2018-12-30 04:02:35 +01:00
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `user_id` = p.`user_id`
|
|
|
|
AND `post_deleted` IS NULL
|
2018-12-30 04:23:04 +01:00
|
|
|
) AS `poster_post_count`,
|
|
|
|
(
|
|
|
|
SELECT MIN(`post_id`) = p.`post_id`
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = p.`topic_id`
|
|
|
|
) AS `is_opening_post`
|
|
|
|
FROM `msz_forum_posts` AS p
|
|
|
|
LEFT JOIN `msz_users` AS u
|
2018-12-30 04:02:35 +01:00
|
|
|
ON u.`user_id` = p.`user_id`
|
2018-12-30 04:23:04 +01:00
|
|
|
LEFT JOIN `msz_roles` AS r
|
2018-12-30 04:02:35 +01:00
|
|
|
ON r.`role_id` = u.`display_role`
|
|
|
|
WHERE `post_id` = :post_id
|
|
|
|
%1$s
|
|
|
|
ORDER BY `post_id`
|
|
|
|
',
|
|
|
|
$allowDeleted ? '' : 'AND `post_deleted` IS NULL'
|
|
|
|
));
|
|
|
|
$getPost->bindValue('post_id', $postId);
|
|
|
|
$post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false;
|
|
|
|
return $post ? $post : [];
|
2018-05-23 03:41:57 +02:00
|
|
|
}
|
2018-05-24 02:38:42 +02:00
|
|
|
|
2018-12-22 12:02:26 +01:00
|
|
|
function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
|
|
|
$hasPagination = $offset >= 0 && $take > 0;
|
2018-12-22 12:02:26 +01:00
|
|
|
$getPosts = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`,
|
|
|
|
p.`topic_id`, p.`post_deleted`, p.`post_edited`,
|
|
|
|
INET6_NTOA(p.`post_ip`) as `post_ip`,
|
|
|
|
u.`user_id` as `poster_id`,
|
|
|
|
u.`username` as `poster_name`,
|
|
|
|
u.`user_created` as `poster_joined`,
|
|
|
|
u.`user_country` as `poster_country`,
|
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) as `poster_colour`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `user_id` = p.`user_id`
|
|
|
|
AND `post_deleted` IS NULL
|
2018-12-30 04:23:04 +01:00
|
|
|
) as `poster_post_count`,
|
|
|
|
(
|
|
|
|
SELECT MIN(`post_id`) = p.`post_id`
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = p.`topic_id`
|
|
|
|
) AS `is_opening_post`
|
2018-12-22 12:02:26 +01:00
|
|
|
FROM `msz_forum_posts` as p
|
|
|
|
LEFT JOIN `msz_users` as u
|
|
|
|
ON u.`user_id` = p.`user_id`
|
|
|
|
LEFT JOIN `msz_roles` as r
|
|
|
|
ON r.`role_id` = u.`display_role`
|
|
|
|
WHERE `topic_id` = :topic_id
|
|
|
|
%1$s
|
|
|
|
ORDER BY `post_id`
|
|
|
|
%2$s
|
|
|
|
',
|
|
|
|
$showDeleted ? '' : 'AND `post_deleted` IS NULL',
|
|
|
|
$hasPagination ? 'LIMIT :offset, :take' : ''
|
|
|
|
));
|
2018-05-24 02:38:42 +02:00
|
|
|
$getPosts->bindValue('topic_id', $topicId);
|
|
|
|
|
|
|
|
if ($hasPagination) {
|
|
|
|
$getPosts->bindValue('offset', $offset);
|
|
|
|
$getPosts->bindValue('take', $take);
|
|
|
|
}
|
|
|
|
|
2018-12-30 04:02:35 +01:00
|
|
|
return $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : [];
|
2018-05-24 02:38:42 +02:00
|
|
|
}
|