From d5397ce9ceadd4650cb38f0cced1778d7917c032 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 6 Mar 2024 18:07:22 +0000 Subject: [PATCH] feat: ability to ban terms in forum content --- core/classes/Core/Validate.php | 23 +++++++++++++++++++ .../Default/forum/forums_settings.tpl | 12 ++++++++++ modules/Forum/classes/Forum.php | 10 ++++++++ modules/Forum/language/en_UK.json | 3 +++ modules/Forum/pages/forum/edit.php | 6 +++-- modules/Forum/pages/forum/new_topic.php | 6 +++-- modules/Forum/pages/forum/view_topic.php | 6 +++-- modules/Forum/pages/panel/settings.php | 5 ++++ 8 files changed, 65 insertions(+), 6 deletions(-) diff --git a/core/classes/Core/Validate.php b/core/classes/Core/Validate.php index 29dbf0572c..b8439ac303 100644 --- a/core/classes/Core/Validate.php +++ b/core/classes/Core/Validate.php @@ -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 */ @@ -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)) { diff --git a/custom/panel_templates/Default/forum/forums_settings.tpl b/custom/panel_templates/Default/forum/forums_settings.tpl index a7262cd3b4..cbb5b18269 100644 --- a/custom/panel_templates/Default/forum/forums_settings.tpl +++ b/custom/panel_templates/Default/forum/forums_settings.tpl @@ -67,6 +67,18 @@ +
+ + + + + +
+
diff --git a/modules/Forum/classes/Forum.php b/modules/Forum/classes/Forum.php index 7cdf4a57ef..6fd4c24b03 100644 --- a/modules/Forum/classes/Forum.php +++ b/modules/Forum/classes/Forum.php @@ -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 diff --git a/modules/Forum/language/en_UK.json b/modules/Forum/language/en_UK.json index 4eca0ffc7f..6cfb603190 100644 --- a/modules/Forum/language/en_UK.json +++ b/modules/Forum/language/en_UK.json @@ -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?", @@ -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", diff --git a/modules/Forum/pages/forum/edit.php b/modules/Forum/pages/forum/edit.php index 617247423d..55e5f4ffa9 100644 --- a/modules/Forum/pages/forum/edit.php +++ b/modules/Forum/pages/forum/edit.php @@ -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 @@ -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'), diff --git a/modules/Forum/pages/forum/new_topic.php b/modules/Forum/pages/forum/new_topic.php index 85d6733bf3..64253e809b 100644 --- a/modules/Forum/pages/forum/new_topic.php +++ b/modules/Forum/pages/forum/new_topic.php @@ -112,7 +112,8 @@ 'content' => [ Validate::REQUIRED => true, Validate::MIN => 2, - Validate::MAX => 50000 + Validate::MAX => 50000, + Validate::NOT_CONTAIN => Forum::getBannedTerms(), ] ])->messages([ 'title' => [ @@ -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'), ] ]); diff --git a/modules/Forum/pages/forum/view_topic.php b/modules/Forum/pages/forum/view_topic.php index 9a4c38a0d7..345904bbb1 100644 --- a/modules/Forum/pages/forum/view_topic.php +++ b/modules/Forum/pages/forum/view_topic.php @@ -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'), ] ]); diff --git a/modules/Forum/pages/panel/settings.php b/modules/Forum/pages/panel/settings.php index da82c018c5..679fb8578d 100644 --- a/modules/Forum/pages/panel/settings.php +++ b/modules/Forum/pages/panel/settings.php @@ -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 { @@ -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')