Skip to content

Commit

Permalink
SQL condition to exclude private topics from the topics list
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex300 committed Jun 24, 2023
1 parent 2060406 commit 528b176
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 79 deletions.
41 changes: 41 additions & 0 deletions modules/forums/inc/forums.functions.php
Expand Up @@ -166,6 +166,47 @@ function cot_forums_prunetopics($mode, $section, $param)
return $topicsDeleted;
}

/**
* SQL condition to exclude private topics from the topics list
* @return string
*/
function cot_forums_sqlExcludePrivateTopics($tableAlias = null)
{
$authCategories = cot_authCategories('forums');

if ($authCategories['adminAll']) {
return '';
}

if ($tableAlias === null || $tableAlias === 'cot_forum_topics') {
$tableAlias = Cot::$db->forum_topics . '.';
} elseif ($tableAlias != '') {
$tableAlias .= '.';
}

$sqlAdminCats = '';
$sqlFirstPosterId = '';
if (Cot::$usr['id'] > 0) {
$sqlFirstPosterId = ' OR ' . Cot::$db->quoteC($tableAlias . 'ft_firstposterid') .' = ' . Cot::$usr['id'];
if (!empty($authCategories['admin'])) {
$categories = [];
foreach ($authCategories['admin'] as $category) {
$category = (string) $category;
if ($category !== '') {
$categories[] = Cot::$db->quote($category);
}
}
if (!empty($categories)) {
$sqlAdminCats = ' OR ' . Cot::$db->quoteC($tableAlias . 'ft_cat')
. ' IN (' . implode(', ', $categories) . ')';
}
}
}

return '(' . Cot::$db->quoteC($tableAlias . 'ft_mode') . ' = ' . COT_FORUMS_TOPIC_MODE_NORMAL
. $sqlFirstPosterId . $sqlAdminCats . ')';
}

/**
* Recounts posts in a given topic
*
Expand Down
20 changes: 3 additions & 17 deletions plugins/recentitems/inc/recentitems.functions.php
Expand Up @@ -31,23 +31,9 @@ function ($value) {return Cot::$db->quote($value);},
}

// Exclude private topics
if (!$authCategories['adminAll']) {
$sqlAdminCats = '';
$sqlFirstPosterId = '';
if (Cot::$usr > 0) {
$sqlFirstPosterId = ' OR ft_firstposterid = ' . Cot::$usr['id'];
if (!empty($authCategories['admin'])) {
$sqlAdminCats = array_map(
function ($value) {
return Cot::$db->quote($value);
},
$authCategories['admin']
);
$sqlAdminCats = ' OR ft_cat IN (' . implode(', ', $sqlAdminCats) . ')';
}
}
$where['privateTopic'] = '(ft_mode = ' . COT_FORUMS_TOPIC_MODE_NORMAL . $sqlFirstPosterId .
$sqlAdminCats . ')';
$where['privateTopic'] = cot_forums_sqlExcludePrivateTopics();
if ($where['privateTopic'] === '') {
unset($where['privateTopic']);
}

$recentitems = new XTemplate(cot_tplfile($template, 'plug'));
Expand Down
74 changes: 52 additions & 22 deletions plugins/recentitems/recentitems.index.php
@@ -1,5 +1,4 @@
<?php

/* ====================
[BEGIN_COT_EXT]
Hooks=index.tags,header.tags,footer.tags
Expand All @@ -14,6 +13,8 @@
* @package RecentItems
* @copyright (c) Cotonti Team
* @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
*
* @var XTemplate $t
*/
defined('COT_CODE') or die('Wrong URL');

Expand All @@ -23,17 +24,29 @@
if ($enpages || $enforums) {
require_once cot_incfile('recentitems', 'plug');

if ($enpages && Cot::$cfg['plugin']['recentitems']['recentpages'] && cot_module_active('page')) {
if (
$enpages
&& Cot::$cfg['plugin']['recentitems']['recentpages']
&& cot_module_active('page')
&& cot_auth('page', 'any')
) {
require_once cot_incfile('page', 'module');

$riPageCacheKey = null;
$riPageUseCache = false;
// Try to load from cache for guests
if (Cot::$usr['id'] == 0 && Cot::$cache && (int) Cot::$cfg['plugin']['recentitems']['cache_ttl'] > 0) {
$ri_cache_id = "$theme.$lang.pages";
$ri_html = Cot::$cache->disk->get($ri_cache_id, 'recentitems', (int)$cfg['plugin']['recentitems']['cache_ttl']);
$riPageUseCache = true;
$riPageCacheKey = "$theme.$lang.pages";
$riHtml = Cot::$cache->disk->get(
$riPageCacheKey,
'recentitems',
(int) Cot::$cfg['plugin']['recentitems']['cache_ttl']
);
}

if (empty($ri_html)) {
$ri_html = cot_build_recentpages(
if (empty($riHtml)) {
$riHtml = cot_build_recentpages(
'recentitems.pages.index',
'recent',
Cot::$cfg['plugin']['recentitems']['maxpages'],
Expand All @@ -42,34 +55,51 @@
Cot::$cfg['plugin']['recentitems']['recentpagestext'], Cot::$cfg['plugin']['recentitems']['rightscan']
);
if (Cot::$usr['id'] == 0 && Cot::$cache && (int) Cot::$cfg['plugin']['recentitems']['cache_ttl'] > 0) {
Cot::$cache->disk->store($ri_cache_id, $ri_html, 'recentitems');
Cot::$cache->disk->store($riPageCacheKey, $riHtml, 'recentitems');
}
}

$t->assign('RECENT_PAGES', $ri_html);
unset($ri_html);
$t->assign('RECENT_PAGES', $riHtml);
unset($riHtml);
}

if ($enforums && Cot::$cfg['plugin']['recentitems']['recentforums'] && cot_module_active('forums')) {
if (
$enforums
&& Cot::$cfg['plugin']['recentitems']['recentforums']
&& cot_module_active('forums')
&& cot_auth('forums', 'any')
) {
require_once cot_incfile('forums', 'module');

$riForumsCacheKey = null;
$riForumsUseCache = false;
// Try to load from cache for guests
if ($usr['id'] == 0 && $cache && (int)$cfg['plugin']['recentitems']['cache_ttl'] > 0)
{
$ri_cache_id = "$theme.$lang.forums";
$ri_html = $cache->disk->get($ri_cache_id, 'recentitems', (int) $cfg['plugin']['recentitems']['cache_ttl']);
if (Cot::$usr['id'] == 0 && Cot::$cache && (int) Cot::$cfg['plugin']['recentitems']['cache_ttl'] > 0) {
$riForumsUseCache = true;
$riForumsCacheKey = "$theme.$lang.forums";
$riHtml = Cot::$cache->disk->get(
$riForumsCacheKey,
'recentitems',
(int) Cot::$cfg['plugin']['recentitems']['cache_ttl']
);
}

if (empty($ri_html))
{
$ri_html = cot_build_recentforums('recentitems.forums.index', 'recent', $cfg['plugin']['recentitems']['maxtopics'], 0, $cfg['plugin']['recentitems']['recentforumstitle'], $cfg['plugin']['recentitems']['rightscan']);
if ($usr['id'] == 0 && $cache && (int)$cfg['plugin']['recentitems']['cache_ttl'] > 0)
{
$cache->disk->store($ri_cache_id, $ri_html, 'recentitems');
if (empty($riHtml)) {
$riHtml = cot_build_recentforums(
'recentitems.forums.index',
'recent',
Cot::$cfg['plugin']['recentitems']['maxtopics'],
0,
Cot::$cfg['plugin']['recentitems']['recentforumstitle'],
Cot::$cfg['plugin']['recentitems']['rightscan']
);

if ($riForumsUseCache) {
Cot::$cache->disk->store($riForumsCacheKey, $riHtml, 'recentitems');
}
}

$t->assign('RECENT_FORUMS', $ri_html);
unset($ri_html);
$t->assign('RECENT_FORUMS', $riHtml);
unset($riHtml);
}
}
59 changes: 21 additions & 38 deletions plugins/search/search.php
Expand Up @@ -102,19 +102,22 @@
}

/* === Hook === */
foreach (cot_getextplugins('search.first') as $pl)
{
foreach (cot_getextplugins('search.first') as $pl) {
include $pl;
}
/* ===== */

if (
($tab == 'pag' || empty($tab))
$searchInPages = ($tab == 'pag' || empty($tab))
&& cot_module_active('page')
&& Cot::$cfg['plugin']['search']['pagesearch']
) {
// Making the category list
&& cot_auth('page', 'any');
if ($searchInPages) {
$pageAuthCats = cot_authCategories('page');
$searchInPages = $searchInPages && !empty($pageAuthCats['read']);
}

if ($searchInPages) {
// Making the category list
$pages_cat_list['all'] = Cot::$L['plu_allcategories'];
if (!empty(Cot::$structure['page'])) {
foreach (Cot::$structure['page'] as $code => $cat) {
Expand Down Expand Up @@ -161,12 +164,16 @@
}
}

if (
($tab == 'frm' || empty($tab))
$searchInForums = ($tab == 'frm' || empty($tab))
&& cot_module_active('forums')
&& Cot::$cfg['plugin']['search']['forumsearch']
) {
&& cot_auth('forums', 'any');
if ($searchInForums) {
$forumAuthCats = cot_authCategories('forums');
$searchInForums = $searchInForums && !empty($forumAuthCats['read']);
}

if ($searchInForums) {
$forum_cat_list['all'] = Cot::$L['plu_allsections'];
if (!empty(Cot::$structure['forums'])) {
foreach (Cot::$structure['forums'] as $code => $cat) {
Expand Down Expand Up @@ -242,13 +249,7 @@

$items = 0;

if (
($tab == 'pag' || empty($tab))
&& cot_module_active('page')
&& Cot::$cfg['plugin']['search']['pagesearch']
&& !empty($pageAuthCats['read'])
&& !cot_error_found()
) {
if ($searchInPages && !cot_error_found()) {
$searchInCategories = [];

if ($rs['pagsub'][0] != 'all' && count($rs['pagsub']) > 0) {
Expand Down Expand Up @@ -437,13 +438,7 @@
unset($where_and, $where_or, $where);
}

if (
($tab == 'frm' || empty($tab))
&& cot_module_active('forums')
&& Cot::$cfg['plugin']['search']['forumsearch']
&& !empty($forumAuthCats['read'])
&& !cot_error_found()
) {
if ($searchInForums && !cot_error_found() ) {
$searchInCategories = [];

if ($rs['frmsub'][0] != 'all' && count($rs['frmsub']) > 0) {
Expand Down Expand Up @@ -472,21 +467,9 @@
}

// Exclude private topics
if (!$forumAuthCats['adminAll']) {
$sqlAdminCats = '';
$sqlFirstPosterId = '';
if (Cot::$usr > 0) {
$sqlFirstPosterId = ' OR ft_firstposterid = ' . Cot::$usr['id'];
if (!empty($forumAuthCats['admin'])) {
$sqlAdminCats = array_map(
function ($value) {return Cot::$db->quote($value);},
$forumAuthCats['admin']
);
$sqlAdminCats = ' OR t.ft_cat IN (' . implode(', ', $sqlAdminCats) . ')';
}
}
$where_and['privateTopic'] = '(t.ft_mode = ' . COT_FORUMS_TOPIC_MODE_NORMAL . $sqlFirstPosterId .
$sqlAdminCats . ')';
$where_and['privateTopic'] = cot_forums_sqlExcludePrivateTopics('t');
if ($where_and['privateTopic'] === '') {
unset($where_and['privateTopic']);
}

$where_and['reply'] = ($rs['frmreply'] == '1') ? 't.ft_postcount > 1' : '';
Expand Down
14 changes: 12 additions & 2 deletions system/functions.php
Expand Up @@ -1647,6 +1647,13 @@ function cot_auth($area, $option = null, $mask = 'RWA')
*/
function cot_authCategories($area, $accessMask = 'RA')
{
static $cache = [];

$cacheKey = $area . '-' . $accessMask;
if (isset($cache[$cacheKey])) {
return $cache[$cacheKey];
}

$masks = str_split($accessMask);
if (empty($masks)) {
$masks = ['R', 'A'];
Expand All @@ -1671,21 +1678,24 @@ function cot_authCategories($area, $accessMask = 'RA')
}

foreach (Cot::$structure[$area] as $code => $cat) {
if (in_array($code, ['all', 'system',])) {
$code = (string) $code;
if (in_array($code, ['all', '']) || empty($cat)) {
continue;
}

foreach ($masks as $mask) {
$key = isset($maskMap[$mask]) ? $maskMap[$mask] : $mask;

if (cot_auth($area, $code, $mask)) {
$result[$key][] = (string) $code;
$result[$key][] = $code;
} else {
$result[$key . 'All'] = false;
}
}
}

$cache[$cacheKey] = $result;

return $result;
}

Expand Down

0 comments on commit 528b176

Please sign in to comment.