Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to ban terms in forum content #3488

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/classes/Core/Validate.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading