Skip to content

Commit

Permalink
feat: ability to ban terms in forum content (#3488)
Browse files Browse the repository at this point in the history
  • Loading branch information
samerton committed Mar 7, 2024
1 parent c4cc85d commit 11ab0ef
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
23 changes: 23 additions & 0 deletions core/classes/Core/Validate.php
Expand Up @@ -96,6 +96,11 @@ class Validate {
*/
public const NOT_START_WITH = 'not_start_with';

/**
* @var string Check that the value does not contain a pattern
*/
public const NOT_CONTAIN = 'not_contain';

/**
* @var string Set a rate limit
*/
Expand Down Expand Up @@ -365,6 +370,24 @@ public static function check(array $source, array $items = []): Validate {
}
break;

case self::NOT_CONTAIN:
if (!is_array($rule_value)) {
$rule_value = [$rule_value];
}

foreach ($rule_value as $term) {
if (strpos(strtolower($value), strtolower($term)) !== false) {
$validator->addError([
'field' => $item,
'rule' => self::NOT_CONTAIN,
'fallback' => "$item must not contain $term",
]);
break;
}
}

break;

case self::IN:
$values = is_string($rule_value) ? [$rule_value] : $rule_value;
if (!in_array($value, $values)) {
Expand Down
12 changes: 12 additions & 0 deletions custom/panel_templates/Default/forum/forums_settings.tpl
Expand Up @@ -67,6 +67,18 @@
</label>
</div>

<div class="form-group">
<label for="InputBannedTerms">
{$BANNED_TERMS}
</label>
<span class="badge badge-info">
<i class="fas fa-question-circle" data-container="body" data-toggle="popover"
data-placement="top" title="{$INFO}"
data-content="{$BANNED_TERMS_INFO}"></i>
</span>
<textarea id="InputBannedTerms" class="form-control" name="banned_terms">{$BANNED_TERMS_VALUE}</textarea>
</div>

<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="submit" class="btn btn-primary" value="{$SUBMIT}" />
Expand Down
10 changes: 10 additions & 0 deletions modules/Forum/classes/Forum.php
Expand Up @@ -798,6 +798,16 @@ public static function getAccessibleLabels(array $labels, array $user_groups): a
}, []);
}

/**
* Get banned terms from the Forum module
*
* @return array Array of banned terms
*/
public static function getBannedTerms(): array {
$terms = Settings::get('banned_terms', '', 'forum');
return explode("\n", $terms);
}

/**
* Get the latest post in a "View own topic" forum
* This could be a topic created by the user, or a sticky topic
Expand Down
3 changes: 3 additions & 0 deletions modules/Forum/language/en_UK.json
Expand Up @@ -3,6 +3,8 @@
"forum/available_hooks": "Available Hooks",
"forum/average_posts": "Average user post count",
"forum/topic_reply": "Topic reply",
"forum/banned_terms": "Banned Terms",
"forum/banned_terms_info": "One term per line, case insensitive",
"forum/by": "by",
"forum/can_create_topic": "Can create topics?",
"forum/can_edit_topic": "Can edit their topics?",
Expand All @@ -13,6 +15,7 @@
"forum/confirm_delete_post": "Are you sure you want to delete this post?",
"forum/confirm_delete_topic": "Are you sure you want to delete this topic?",
"forum/confirm_unfollow_all_topics": "Are you sure you want to unfollow all topics?",
"forum/content_contains_banned_term": "Your post content contains a banned term",
"forum/content_max_50000": "Your post content must be no longer than 50.000 characters",
"forum/content_min_2": "Your post content must be a minimum of 2 characters",
"forum/content_required": "Please input post content",
Expand Down
6 changes: 4 additions & 2 deletions modules/Forum/pages/forum/edit.php
Expand Up @@ -85,7 +85,8 @@
'content' => [
Validate::REQUIRED => true,
Validate::MIN => 2,
Validate::MAX => 50000
Validate::MAX => 50000,
Validate::NOT_CONTAIN => Forum::getBannedTerms(),
]
];
// Add title to validation if we need to
Expand All @@ -101,7 +102,8 @@
'content' => [
Validate::REQUIRED => $forum_language->get('forum', 'content_required'),
Validate::MIN => $forum_language->get('forum', 'content_min_2'),
Validate::MAX => $forum_language->get('forum', 'content_max_50000')
Validate::MAX => $forum_language->get('forum', 'content_max_50000'),
Validate::NOT_CONTAIN => $forum_language->get('forum', 'content_contains_banned_term'),
],
'title' => [
Validate::REQUIRED => $forum_language->get('forum', 'title_required'),
Expand Down
6 changes: 4 additions & 2 deletions modules/Forum/pages/forum/new_topic.php
Expand Up @@ -112,7 +112,8 @@
'content' => [
Validate::REQUIRED => true,
Validate::MIN => 2,
Validate::MAX => 50000
Validate::MAX => 50000,
Validate::NOT_CONTAIN => Forum::getBannedTerms(),
]
])->messages([
'title' => [
Expand All @@ -123,7 +124,8 @@
'content' => [
Validate::REQUIRED => $forum_language->get('forum', 'content_required'),
Validate::MIN => $forum_language->get('forum', 'content_min_2'),
Validate::MAX => $forum_language->get('forum', 'content_max_50000')
Validate::MAX => $forum_language->get('forum', 'content_max_50000'),
Validate::NOT_CONTAIN => $forum_language->get('forum', 'content_contains_banned_term'),
]
]);

Expand Down
6 changes: 4 additions & 2 deletions modules/Forum/pages/forum/view_topic.php
Expand Up @@ -248,13 +248,15 @@
'content' => [
Validate::REQUIRED => true,
Validate::MIN => 2,
Validate::MAX => 50000
Validate::MAX => 50000,
Validate::NOT_CONTAIN => Forum::getBannedTerms(),
]
])->messages([
'content' => [
Validate::REQUIRED => $forum_language->get('forum', 'content_required'),
Validate::MIN => $forum_language->get('forum', 'content_min_2'),
Validate::MAX => $forum_language->get('forum', 'content_max_50000')
Validate::MAX => $forum_language->get('forum', 'content_max_50000'),
Validate::NOT_CONTAIN => $forum_language->get('forum', 'content_contains_banned_term'),
]
]);

Expand Down
5 changes: 5 additions & 0 deletions modules/Forum/pages/panel/settings.php
Expand Up @@ -58,6 +58,7 @@

Settings::set('forum_reactions', (isset($_POST['use_reactions']) && $_POST['use_reactions'] == 'on') ? '1' : 0);
Settings::set('news_items_front_page', $_POST['news_items'], 'forum');
Settings::set('banned_terms', $_POST['banned_terms'], 'forum');

Session::flash('admin_forums_settings', $forum_language->get('forum', 'settings_updated_successfully'));
} else {
Expand Down Expand Up @@ -114,6 +115,10 @@
'USE_REACTIONS_VALUE' => Settings::get('forum_reactions') === '1',
'NEWS_ITEMS_ON_FRONT_PAGE' => $forum_language->get('forum', 'news_items_front_page_limit'),
'NEWS_ITEMS_ON_FRONT_PAGE_VALUE' => Settings::get('news_items_front_page', 5, 'forum'),
'BANNED_TERMS' => $forum_language->get('forum', 'banned_terms'),
'BANNED_TERMS_INFO' => $forum_language->get('forum', 'banned_terms_info'),
'BANNED_TERMS_VALUE' => Output::getClean(Settings::get('banned_terms', '', 'forum')),
'INFO' => $language->get('general', 'info'),
'PAGE' => PANEL_PAGE,
'TOKEN' => Token::get(),
'SUBMIT' => $language->get('general', 'submit')
Expand Down

0 comments on commit 11ab0ef

Please sign in to comment.