2018-05-23 03:41:57 +02:00
|
|
|
<?php
|
2018-08-18 04:31:46 +02:00
|
|
|
define('MSZ_PERM_FORUM_MANAGE_FORUMS', 1);
|
2018-07-10 23:24:00 +02:00
|
|
|
|
2018-08-18 04:10:52 +02:00
|
|
|
define('MSZ_FORUM_PERM_LIST_FORUM', 1); // can see stats, but will get error when trying to view
|
|
|
|
define('MSZ_FORUM_PERM_VIEW_FORUM', 1 << 1);
|
|
|
|
|
|
|
|
define('MSZ_FORUM_PERM_CREATE_TOPIC', 1 << 10);
|
2019-01-12 00:00:53 +01:00
|
|
|
//define('MSZ_FORUM_PERM_DELETE_TOPIC', 1 << 11); // use MSZ_FORUM_PERM_DELETE_ANY_POST instead
|
2018-08-18 04:10:52 +02:00
|
|
|
define('MSZ_FORUM_PERM_MOVE_TOPIC', 1 << 12);
|
|
|
|
define('MSZ_FORUM_PERM_LOCK_TOPIC', 1 << 13);
|
|
|
|
define('MSZ_FORUM_PERM_STICKY_TOPIC', 1 << 14);
|
|
|
|
define('MSZ_FORUM_PERM_ANNOUNCE_TOPIC', 1 << 15);
|
|
|
|
define('MSZ_FORUM_PERM_GLOBAL_ANNOUNCE_TOPIC', 1 << 16);
|
2019-01-12 00:00:53 +01:00
|
|
|
define('MSZ_FORUM_PERM_BUMP_TOPIC', 1 << 17);
|
2018-08-18 04:10:52 +02:00
|
|
|
|
|
|
|
define('MSZ_FORUM_PERM_CREATE_POST', 1 << 20);
|
|
|
|
define('MSZ_FORUM_PERM_EDIT_POST', 1 << 21);
|
|
|
|
define('MSZ_FORUM_PERM_EDIT_ANY_POST', 1 << 22);
|
|
|
|
define('MSZ_FORUM_PERM_DELETE_POST', 1 << 23);
|
|
|
|
define('MSZ_FORUM_PERM_DELETE_ANY_POST', 1 << 24);
|
|
|
|
|
2018-12-28 06:03:42 +01:00
|
|
|
// shorthands, never use these to SET!!!!!!!
|
2018-12-29 02:56:45 +01:00
|
|
|
define('MSZ_FORUM_PERM_SET_READ', MSZ_FORUM_PERM_LIST_FORUM | MSZ_FORUM_PERM_VIEW_FORUM);
|
2018-12-28 06:03:42 +01:00
|
|
|
define(
|
|
|
|
'MSZ_FORUM_PERM_SET_WRITE',
|
|
|
|
MSZ_FORUM_PERM_CREATE_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_MOVE_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_LOCK_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_STICKY_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_ANNOUNCE_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_GLOBAL_ANNOUNCE_TOPIC
|
|
|
|
| MSZ_FORUM_PERM_CREATE_POST
|
|
|
|
| MSZ_FORUM_PERM_EDIT_POST
|
|
|
|
| MSZ_FORUM_PERM_EDIT_ANY_POST
|
|
|
|
| MSZ_FORUM_PERM_DELETE_POST
|
|
|
|
| MSZ_FORUM_PERM_DELETE_ANY_POST
|
2019-01-12 00:00:53 +01:00
|
|
|
| MSZ_FORUM_PERM_BUMP_TOPIC
|
2018-12-28 06:03:42 +01:00
|
|
|
);
|
|
|
|
|
2018-05-23 03:41:57 +02:00
|
|
|
define('MSZ_FORUM_TYPE_DISCUSSION', 0);
|
|
|
|
define('MSZ_FORUM_TYPE_CATEGORY', 1);
|
|
|
|
define('MSZ_FORUM_TYPE_LINK', 2);
|
|
|
|
define('MSZ_FORUM_TYPES', [
|
|
|
|
MSZ_FORUM_TYPE_DISCUSSION,
|
|
|
|
MSZ_FORUM_TYPE_CATEGORY,
|
|
|
|
MSZ_FORUM_TYPE_LINK,
|
|
|
|
]);
|
|
|
|
|
2018-05-24 02:38:42 +02:00
|
|
|
define('MSZ_FORUM_MAY_HAVE_CHILDREN', [
|
|
|
|
MSZ_FORUM_TYPE_DISCUSSION,
|
|
|
|
MSZ_FORUM_TYPE_CATEGORY,
|
|
|
|
]);
|
|
|
|
|
|
|
|
define('MSZ_FORUM_MAY_HAVE_TOPICS', [
|
|
|
|
MSZ_FORUM_TYPE_DISCUSSION,
|
|
|
|
]);
|
|
|
|
|
|
|
|
define('MSZ_FORUM_ROOT', 0);
|
|
|
|
define('MSZ_FORUM_ROOT_DATA', [ // should be compatible with the data fetched in forum_get_root_categories
|
2018-12-31 03:22:13 +01:00
|
|
|
'forum_id' => MSZ_FORUM_ROOT,
|
2018-05-24 02:38:42 +02:00
|
|
|
'forum_name' => 'Forums',
|
|
|
|
'forum_children' => 0,
|
|
|
|
'forum_type' => MSZ_FORUM_TYPE_CATEGORY,
|
2018-10-16 23:38:17 +02:00
|
|
|
'forum_colour' => null,
|
2019-01-03 16:57:17 +01:00
|
|
|
'forum_permissions' => MSZ_FORUM_PERM_SET_READ,
|
2018-05-24 02:38:42 +02:00
|
|
|
]);
|
|
|
|
|
2018-12-30 19:58:30 +01:00
|
|
|
function forum_is_valid_type(int $type): bool
|
|
|
|
{
|
|
|
|
return in_array($type, MSZ_FORUM_TYPES, true);
|
|
|
|
}
|
|
|
|
|
2018-05-24 02:38:42 +02:00
|
|
|
function forum_may_have_children(int $forumType): bool
|
|
|
|
{
|
|
|
|
return in_array($forumType, MSZ_FORUM_MAY_HAVE_CHILDREN);
|
|
|
|
}
|
|
|
|
|
|
|
|
function forum_may_have_topics(int $forumType): bool
|
|
|
|
{
|
|
|
|
return in_array($forumType, MSZ_FORUM_MAY_HAVE_TOPICS);
|
|
|
|
}
|
|
|
|
|
2019-01-03 16:57:17 +01:00
|
|
|
function forum_fetch(int $forumId, bool $showDeleted = false): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
2019-01-03 16:57:17 +01:00
|
|
|
$getForum = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
SELECT
|
|
|
|
`forum_id`, `forum_name`, `forum_type`, `forum_link`, `forum_archived`,
|
|
|
|
`forum_link_clicks`, `forum_parent`, `forum_colour`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`topic_id`)
|
|
|
|
FROM `msz_forum_topics`
|
|
|
|
WHERE `forum_id` = f.`forum_id`
|
|
|
|
%1$s
|
|
|
|
) as `forum_topic_count`
|
|
|
|
FROM `msz_forum_categories` as f
|
|
|
|
WHERE `forum_id` = :forum_id
|
|
|
|
',
|
|
|
|
$showDeleted ? '' : 'AND `topic_deleted` IS NULL'
|
|
|
|
));
|
2018-05-24 02:38:42 +02:00
|
|
|
$getForum->bindValue('forum_id', $forumId);
|
2019-01-09 20:06:02 +01:00
|
|
|
return db_fetch($getForum);
|
2018-05-24 02:38:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:20:10 +02:00
|
|
|
function forum_get_root_categories(int $userId): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
2018-11-03 17:44:19 +01:00
|
|
|
$getCategories = db_prepare(sprintf(
|
2019-01-03 16:57:17 +01:00
|
|
|
'
|
2018-11-03 17:44:19 +01:00
|
|
|
SELECT
|
|
|
|
f.`forum_id`, f.`forum_name`, f.`forum_type`, f.`forum_colour`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`forum_id`)
|
2019-01-03 16:57:17 +01:00
|
|
|
FROM `msz_forum_categories` AS sf
|
2018-11-03 17:44:19 +01:00
|
|
|
WHERE sf.`forum_parent` = f.`forum_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
) AS `forum_children`,
|
|
|
|
(%2$s) AS `forum_permissions`
|
|
|
|
FROM `msz_forum_categories` AS f
|
2018-11-03 17:44:19 +01:00
|
|
|
WHERE f.`forum_parent` = 0
|
2019-01-03 16:57:17 +01:00
|
|
|
AND f.`forum_type` = %1$d
|
2018-11-03 17:44:19 +01:00
|
|
|
AND f.`forum_hidden` = 0
|
2019-01-03 16:57:17 +01:00
|
|
|
GROUP BY f.`forum_id`
|
|
|
|
HAVING (`forum_permissions` & %3$d) > 0
|
2018-11-03 17:44:19 +01:00
|
|
|
ORDER BY f.`forum_order`
|
2019-01-03 16:57:17 +01:00
|
|
|
',
|
2018-11-03 17:44:19 +01:00
|
|
|
MSZ_FORUM_TYPE_CATEGORY,
|
2019-01-04 01:12:27 +01:00
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 'f.`forum_id`'),
|
2018-12-29 02:56:45 +01:00
|
|
|
MSZ_FORUM_PERM_SET_READ
|
2018-11-03 17:44:19 +01:00
|
|
|
));
|
2018-08-23 20:13:37 +02:00
|
|
|
$getCategories->bindValue('perm_user_id_user', $userId);
|
|
|
|
$getCategories->bindValue('perm_user_id_role', $userId);
|
2019-01-09 20:06:02 +01:00
|
|
|
$categories = array_merge([MSZ_FORUM_ROOT_DATA], db_fetch_all($getCategories));
|
2018-05-24 02:38:42 +02:00
|
|
|
|
2018-11-03 17:44:19 +01:00
|
|
|
$getRootForumCount = db_prepare(sprintf(
|
|
|
|
"
|
|
|
|
SELECT COUNT(`forum_id`)
|
|
|
|
FROM `msz_forum_categories`
|
|
|
|
WHERE `forum_parent` = %d
|
|
|
|
AND `forum_type` != %d
|
|
|
|
AND (%s & %d) > 0
|
|
|
|
",
|
|
|
|
MSZ_FORUM_ROOT,
|
|
|
|
MSZ_FORUM_TYPE_CATEGORY,
|
2019-01-04 01:12:27 +01:00
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, '`forum_id`'),
|
2018-12-29 02:56:45 +01:00
|
|
|
MSZ_FORUM_PERM_SET_READ
|
2018-11-03 17:44:19 +01:00
|
|
|
));
|
2018-08-23 20:13:37 +02:00
|
|
|
$getRootForumCount->bindValue('perm_user_id_user', $userId);
|
|
|
|
$getRootForumCount->bindValue('perm_user_id_role', $userId);
|
2019-01-02 23:03:39 +01:00
|
|
|
$categories[0]['forum_children'] = (int)($getRootForumCount->execute() ? $getRootForumCount->fetchColumn() : 0);
|
2018-05-24 02:38:42 +02:00
|
|
|
|
|
|
|
return $categories;
|
|
|
|
}
|
|
|
|
|
2018-05-23 03:41:57 +02:00
|
|
|
function forum_get_breadcrumbs(
|
|
|
|
int $forumId,
|
|
|
|
string $linkFormat = '/forum/forum.php?f=%d',
|
2018-12-31 03:22:13 +01:00
|
|
|
string $rootFormat = '/forum/#f%d',
|
2018-05-23 03:41:57 +02:00
|
|
|
array $indexLink = ['Forums' => '/forum/']
|
|
|
|
): array {
|
|
|
|
$breadcrumbs = [];
|
2018-10-07 01:30:48 +02:00
|
|
|
$getBreadcrumbs = db_prepare('
|
2019-01-01 04:54:01 +01:00
|
|
|
WITH RECURSIVE breadcrumbs(forum_id, forum_name, forum_parent, forum_type) as (
|
|
|
|
SELECT c.`forum_id`, c.`forum_name`, c.`forum_parent`, c.`forum_type`
|
2018-08-23 20:13:37 +02:00
|
|
|
FROM `msz_forum_categories` as c
|
|
|
|
WHERE `forum_id` = :forum_id
|
|
|
|
UNION ALL
|
2019-01-01 04:54:01 +01:00
|
|
|
SELECT p.`forum_id`, p.`forum_name`, p.`forum_parent`, p.`forum_type`
|
2018-08-23 20:13:37 +02:00
|
|
|
FROM `msz_forum_categories` as p
|
|
|
|
INNER JOIN breadcrumbs
|
|
|
|
ON p.`forum_id` = breadcrumbs.forum_parent
|
|
|
|
)
|
|
|
|
SELECT * FROM breadcrumbs
|
2018-05-23 03:41:57 +02:00
|
|
|
');
|
2018-08-23 20:13:37 +02:00
|
|
|
$getBreadcrumbs->bindValue('forum_id', $forumId);
|
2019-01-09 20:06:02 +01:00
|
|
|
$breadcrumbsDb = db_fetch_all($getBreadcrumbs);
|
2018-05-23 03:41:57 +02:00
|
|
|
|
2018-08-23 20:13:37 +02:00
|
|
|
if (!$breadcrumbsDb) {
|
|
|
|
return [$indexLink];
|
|
|
|
}
|
2018-05-23 03:41:57 +02:00
|
|
|
|
2018-08-23 20:13:37 +02:00
|
|
|
foreach ($breadcrumbsDb as $breadcrumb) {
|
2018-12-31 03:22:13 +01:00
|
|
|
$breadcrumbs[$breadcrumb['forum_name']] = sprintf(
|
2019-01-01 04:54:01 +01:00
|
|
|
$breadcrumb['forum_parent'] === MSZ_FORUM_ROOT
|
|
|
|
&& $breadcrumb['forum_type'] === MSZ_FORUM_TYPE_CATEGORY
|
|
|
|
? $rootFormat
|
|
|
|
: $linkFormat,
|
2018-12-31 03:22:13 +01:00
|
|
|
$breadcrumb['forum_id']
|
|
|
|
);
|
2018-05-23 03:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return array_reverse($breadcrumbs + $indexLink);
|
|
|
|
}
|
|
|
|
|
2018-10-21 23:53:32 +02:00
|
|
|
function forum_get_colour(int $forumId): int
|
|
|
|
{
|
|
|
|
$getColours = db_prepare('
|
|
|
|
WITH RECURSIVE breadcrumbs(forum_id, forum_parent, forum_colour) as (
|
|
|
|
SELECT c.`forum_id`, c.`forum_parent`, c.`forum_colour`
|
|
|
|
FROM `msz_forum_categories` as c
|
|
|
|
WHERE `forum_id` = :forum_id
|
|
|
|
UNION ALL
|
|
|
|
SELECT p.`forum_id`, p.`forum_parent`, p.`forum_colour`
|
|
|
|
FROM `msz_forum_categories` as p
|
|
|
|
INNER JOIN breadcrumbs
|
|
|
|
ON p.`forum_id` = breadcrumbs.forum_parent
|
|
|
|
)
|
|
|
|
SELECT * FROM breadcrumbs
|
|
|
|
');
|
|
|
|
$getColours->bindValue('forum_id', $forumId);
|
2019-01-09 20:06:02 +01:00
|
|
|
$colours = db_fetch_all($getColours);
|
2018-10-21 23:53:32 +02:00
|
|
|
|
|
|
|
if ($colours) {
|
|
|
|
foreach ($colours as $colour) {
|
|
|
|
if ($colour['forum_colour'] !== null) {
|
|
|
|
return $colour['forum_colour'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return colour_none();
|
|
|
|
}
|
|
|
|
|
2018-05-23 03:41:57 +02:00
|
|
|
function forum_increment_clicks(int $forumId): void
|
|
|
|
{
|
2018-10-07 01:30:48 +02:00
|
|
|
$incrementLinkClicks = db_prepare(sprintf('
|
2018-05-23 03:41:57 +02:00
|
|
|
UPDATE `msz_forum_categories`
|
|
|
|
SET `forum_link_clicks` = `forum_link_clicks` + 1
|
|
|
|
WHERE `forum_id` = :forum_id
|
2018-08-23 20:13:37 +02:00
|
|
|
AND `forum_type` = %d
|
2018-05-23 03:41:57 +02:00
|
|
|
AND `forum_link_clicks` IS NOT NULL
|
2018-08-23 20:13:37 +02:00
|
|
|
', MSZ_FORUM_TYPE_LINK));
|
2018-05-23 03:41:57 +02:00
|
|
|
$incrementLinkClicks->bindValue('forum_id', $forumId);
|
|
|
|
$incrementLinkClicks->execute();
|
|
|
|
}
|
2018-05-24 02:38:42 +02:00
|
|
|
|
2018-08-23 20:13:37 +02:00
|
|
|
function forum_read_status_sql(
|
|
|
|
string $topic_id_param,
|
2018-10-19 10:44:38 +02:00
|
|
|
string $user_param_sub,
|
2018-08-23 20:13:37 +02:00
|
|
|
string $forum_id_param = 'f.`forum_id`',
|
|
|
|
string $user_param = '`target_user_id`'
|
|
|
|
): string {
|
|
|
|
return sprintf(
|
|
|
|
'
|
2018-05-24 02:38:42 +02:00
|
|
|
SELECT
|
2018-08-23 20:13:37 +02:00
|
|
|
%1$s > 0
|
2018-05-24 02:38:42 +02:00
|
|
|
AND
|
2018-08-23 20:13:37 +02:00
|
|
|
%2$s IS NOT NULL
|
2018-05-24 02:38:42 +02:00
|
|
|
AND (
|
2018-10-19 10:44:38 +02:00
|
|
|
SELECT COUNT(ti.`topic_id`)
|
|
|
|
FROM `msz_forum_topics` AS ti
|
|
|
|
LEFT JOIN `msz_forum_topics_track` AS tt
|
2018-11-03 00:01:05 +01:00
|
|
|
ON tt.`topic_id` = ti.`topic_id` AND tt.`user_id` = %4$s
|
|
|
|
WHERE ti.`forum_id` = %3$s
|
2018-10-19 10:44:38 +02:00
|
|
|
AND ti.`topic_deleted` IS NULL
|
2018-11-03 00:01:05 +01:00
|
|
|
AND ti.`topic_bumped` >= NOW() - INTERVAL 1 MONTH
|
2018-10-19 10:14:06 +02:00
|
|
|
AND (
|
|
|
|
tt.`track_last_read` IS NULL
|
|
|
|
OR tt.`track_last_read` < ti.`topic_bumped`
|
|
|
|
)
|
2018-05-24 02:38:42 +02:00
|
|
|
)
|
2018-08-23 20:13:37 +02:00
|
|
|
',
|
|
|
|
$user_param,
|
|
|
|
$topic_id_param,
|
2018-10-19 10:44:38 +02:00
|
|
|
$forum_id_param,
|
|
|
|
$user_param_sub
|
2018-08-23 20:13:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
define(
|
|
|
|
'MSZ_FORUM_GET_CHILDREN_QUERY_SMALL',
|
2018-09-09 17:33:00 +02:00
|
|
|
'
|
|
|
|
SELECT
|
2019-01-03 16:57:17 +01:00
|
|
|
:user_id AS `target_user_id`,
|
2018-09-09 17:33:00 +02:00
|
|
|
f.`forum_id`, f.`forum_name`,
|
2019-01-03 16:57:17 +01:00
|
|
|
(%1$s) AS `forum_unread`,
|
|
|
|
(%4$s) AS `forum_permissions`
|
|
|
|
FROM `msz_forum_categories` AS f
|
|
|
|
LEFT JOIN `msz_forum_topics` AS t
|
2018-09-09 17:33:00 +02:00
|
|
|
ON t.`topic_id` = (
|
|
|
|
SELECT `topic_id`
|
|
|
|
FROM `msz_forum_topics`
|
|
|
|
WHERE `forum_id` = f.`forum_id`
|
|
|
|
AND `topic_deleted` IS NULL
|
|
|
|
ORDER BY `topic_bumped` DESC
|
|
|
|
LIMIT 1
|
|
|
|
)
|
|
|
|
WHERE `forum_parent` = :parent_id
|
|
|
|
AND `forum_hidden` = false
|
2019-01-03 16:57:17 +01:00
|
|
|
GROUP BY f.`forum_id`
|
|
|
|
HAVING (`forum_permissions` & %5$d) > 0
|
2018-09-09 17:33:00 +02:00
|
|
|
ORDER BY f.`forum_order`
|
|
|
|
'
|
2018-08-23 20:13:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
define(
|
|
|
|
'MSZ_FORUM_GET_CHILDREN_QUERY_STANDARD',
|
2018-09-09 17:33:00 +02:00
|
|
|
'
|
|
|
|
SELECT
|
2019-01-03 16:57:17 +01:00
|
|
|
:user_id AS `target_user_id`,
|
2018-09-09 17:33:00 +02:00
|
|
|
f.`forum_id`, f.`forum_name`, f.`forum_description`, f.`forum_type`,
|
2018-10-16 23:38:17 +02:00
|
|
|
f.`forum_link`, f.`forum_link_clicks`, f.`forum_archived`, f.`forum_colour`,
|
2019-01-03 16:57:17 +01:00
|
|
|
t.`topic_id` AS `recent_topic_id`, p.`post_id` AS `recent_post_id`,
|
|
|
|
t.`topic_title` AS `recent_topic_title`, t.`topic_bumped` AS `recent_topic_bumped`,
|
|
|
|
p.`post_created` AS `recent_post_created`,
|
|
|
|
u.`user_id` AS `recent_post_user_id`,
|
|
|
|
u.`username` AS `recent_post_username`,
|
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) AS `recent_post_user_colour`,
|
2018-09-09 17:33:00 +02:00
|
|
|
(
|
|
|
|
SELECT COUNT(`topic_id`)
|
2018-08-23 20:13:37 +02:00
|
|
|
FROM `msz_forum_topics`
|
|
|
|
WHERE `forum_id` = f.`forum_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%6$s
|
|
|
|
) AS `forum_topic_count`,
|
2018-09-09 17:33:00 +02:00
|
|
|
(
|
|
|
|
SELECT COUNT(`post_id`)
|
2018-08-23 20:13:37 +02:00
|
|
|
FROM `msz_forum_posts`
|
2018-09-09 17:33:00 +02:00
|
|
|
WHERE `forum_id` = f.`forum_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%7$s
|
|
|
|
) AS `forum_post_count`,
|
|
|
|
(%1$s) AS `forum_unread`,
|
|
|
|
(%4$s) AS `forum_permissions`
|
|
|
|
FROM `msz_forum_categories` AS f
|
|
|
|
LEFT JOIN `msz_forum_topics` AS t
|
2018-09-09 17:33:00 +02:00
|
|
|
ON t.`topic_id` = (
|
|
|
|
SELECT `topic_id`
|
|
|
|
FROM `msz_forum_topics`
|
|
|
|
WHERE `forum_id` = f.`forum_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%6$s
|
2018-09-09 17:33:00 +02:00
|
|
|
ORDER BY `topic_bumped` DESC
|
|
|
|
LIMIT 1
|
|
|
|
)
|
2019-01-03 16:57:17 +01:00
|
|
|
LEFT JOIN `msz_forum_posts` AS p
|
2018-09-09 17:33:00 +02:00
|
|
|
ON p.`post_id` = (
|
|
|
|
SELECT `post_id`
|
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `topic_id` = t.`topic_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
%7$s
|
2018-09-09 17:33:00 +02:00
|
|
|
ORDER BY `post_id` DESC
|
|
|
|
LIMIT 1
|
|
|
|
)
|
2019-01-03 16:57:17 +01:00
|
|
|
LEFT JOIN `msz_users` AS u
|
2018-09-09 17:33:00 +02:00
|
|
|
ON u.`user_id` = p.`user_id`
|
2019-01-03 16:57:17 +01:00
|
|
|
LEFT JOIN `msz_roles` AS r
|
2018-09-09 17:33:00 +02:00
|
|
|
ON r.`role_id` = u.`display_role`
|
|
|
|
WHERE f.`forum_parent` = :parent_id
|
2018-11-03 17:44:19 +01:00
|
|
|
AND f.`forum_hidden` = 0
|
2018-09-09 17:33:00 +02:00
|
|
|
AND (
|
|
|
|
(f.`forum_parent` = %2$d AND f.`forum_type` != %3$d)
|
|
|
|
OR f.`forum_parent` != %2$d
|
|
|
|
)
|
2019-01-03 16:57:17 +01:00
|
|
|
GROUP BY f.`forum_id`
|
|
|
|
HAVING (`forum_permissions` & %5$d) > 0
|
2018-09-09 17:33:00 +02:00
|
|
|
ORDER BY f.`forum_order`
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
2019-01-03 16:57:17 +01:00
|
|
|
function forum_get_children_query(bool $showDeleted = false, bool $small = false): string
|
2018-09-09 17:33:00 +02:00
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
$small
|
|
|
|
? MSZ_FORUM_GET_CHILDREN_QUERY_SMALL
|
|
|
|
: MSZ_FORUM_GET_CHILDREN_QUERY_STANDARD,
|
2018-11-03 00:01:05 +01:00
|
|
|
forum_read_status_sql('t.`topic_id`', ':user_for_check'),
|
2018-08-23 20:13:37 +02:00
|
|
|
MSZ_FORUM_ROOT,
|
|
|
|
MSZ_FORUM_TYPE_CATEGORY,
|
2019-01-04 01:12:27 +01:00
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 'f.`forum_id`'),
|
2019-01-03 16:57:17 +01:00
|
|
|
MSZ_FORUM_PERM_SET_READ,
|
|
|
|
$showDeleted ? '' : 'AND `topic_deleted` IS NULL',
|
|
|
|
$showDeleted ? '' : 'AND `post_deleted` IS NULL'
|
2018-09-09 17:33:00 +02:00
|
|
|
);
|
|
|
|
}
|
2018-05-24 02:38:42 +02:00
|
|
|
|
2019-01-03 16:57:17 +01:00
|
|
|
function forum_get_children(int $parentId, int $userId, bool $showDeleted = false, bool $small = false): array
|
2018-05-24 02:38:42 +02:00
|
|
|
{
|
2019-01-03 16:57:17 +01:00
|
|
|
$getListing = db_prepare(forum_get_children_query($showDeleted, $small));
|
2018-05-24 02:38:42 +02:00
|
|
|
$getListing->bindValue('user_id', $userId);
|
2018-08-23 20:13:37 +02:00
|
|
|
$getListing->bindValue('perm_user_id_user', $userId);
|
|
|
|
$getListing->bindValue('perm_user_id_role', $userId);
|
2018-10-19 10:44:38 +02:00
|
|
|
$getListing->bindValue('user_for_check', $userId);
|
2018-05-24 02:38:42 +02:00
|
|
|
$getListing->bindValue('parent_id', $parentId);
|
|
|
|
|
2019-01-09 20:06:02 +01:00
|
|
|
return db_fetch_all($getListing);
|
2018-05-24 02:38:42 +02:00
|
|
|
}
|
2019-01-05 19:08:18 +01:00
|
|
|
|
|
|
|
function forum_timeout(int $forumId, int $userId): int
|
|
|
|
{
|
|
|
|
$checkTimeout = db_prepare('
|
2019-01-05 21:09:07 +01:00
|
|
|
SELECT TIMESTAMPDIFF(SECOND, COALESCE(MAX(`post_created`), NOW() - INTERVAL 1 YEAR), NOW())
|
2019-01-05 19:08:18 +01:00
|
|
|
FROM `msz_forum_posts`
|
|
|
|
WHERE `forum_id` = :forum_id
|
|
|
|
AND `user_id` = :user_id
|
|
|
|
');
|
|
|
|
$checkTimeout->bindValue('forum_id', $forumId);
|
|
|
|
$checkTimeout->bindValue('user_id', $userId);
|
|
|
|
|
|
|
|
return (int)($checkTimeout->execute() ? $checkTimeout->fetchColumn() : 0);
|
|
|
|
}
|
2019-01-09 21:22:54 +01:00
|
|
|
|
|
|
|
// $forumId == null marks all forums as read
|
|
|
|
function forum_mark_read(?int $forumId, int $userId): bool
|
|
|
|
{
|
|
|
|
if (($forumId !== null && $forumId < 1) || $userId < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entireForum = $forumId === null;
|
|
|
|
$doMark = db_prepare(sprintf(
|
|
|
|
'
|
|
|
|
REPLACE INTO `msz_forum_topics_track`
|
|
|
|
(`user_id`, `topic_id`, `forum_id`, `track_last_read`)
|
|
|
|
SELECT u.`user_id`, t.`topic_id`, t.`forum_id`, NOW()
|
|
|
|
FROM `msz_forum_topics` AS t
|
|
|
|
LEFT JOIN `msz_users` AS u
|
|
|
|
ON u.`user_id` = :user
|
|
|
|
WHERE t.`topic_deleted` IS NULL
|
|
|
|
AND t.`topic_bumped` >= NOW() - INTERVAL 1 MONTH
|
|
|
|
%1$s
|
|
|
|
GROUP BY t.`topic_id`
|
|
|
|
HAVING ((%2$s) & %3$d) > 0
|
|
|
|
',
|
|
|
|
$entireForum ? '' : 'AND t.`forum_id` = :forum',
|
|
|
|
forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 't.`forum_id`', 'u.`user_id`', 'u.`user_id`'),
|
|
|
|
MSZ_FORUM_PERM_SET_READ
|
|
|
|
));
|
|
|
|
$doMark->bindValue('user', $userId);
|
|
|
|
|
|
|
|
if (!$entireForum) {
|
|
|
|
$doMark->bindValue('forum', $forumId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $doMark->execute();
|
|
|
|
}
|