From b1442ebfd1a215d2fc44934c599dca38509476c9 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 10 Dec 2022 14:43:30 -0500 Subject: [PATCH] [nsfw] Suppress warnings about failed regexp compilation - Add failed regexp compilation system messages on addon settings submit - Address https://github.com/friendica/friendica/issues/11992#issuecomment-1336418781 --- nsfw/lang/C/messages.po | 11 ++++++++--- nsfw/nsfw.php | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/nsfw/lang/C/messages.po b/nsfw/lang/C/messages.po index 150b77d3..241143e8 100644 --- a/nsfw/lang/C/messages.po +++ b/nsfw/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2022-11-18 11:57-0500\n" +"POT-Creation-Date: 2022-12-10 14:42-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -43,12 +43,17 @@ msgstr "" msgid "Content Filter (NSFW and more)" msgstr "" -#: nsfw.php:140 +#: nsfw.php:96 +#, php-format +msgid "Regular expression \"%s\" fails to compile" +msgstr "" + +#: nsfw.php:154 #, php-format msgid "Filtered tag: %s" msgstr "" -#: nsfw.php:142 +#: nsfw.php:156 #, php-format msgid "Filtered word: %s" msgstr "" diff --git a/nsfw/nsfw.php b/nsfw/nsfw.php index 31d17f74..340cae27 100644 --- a/nsfw/nsfw.php +++ b/nsfw/nsfw.php @@ -81,9 +81,23 @@ function nsfw_addon_settings_post(App $a, array &$b) } if (!empty($_POST['nsfw-submit'])) { - DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'nsfw', 'words', trim($_POST['nsfw-words'])); - $enable = (!empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0); + $enable = !empty($_POST['nsfw-enable']) ? intval($_POST['nsfw-enable']) : 0; $disable = 1 - $enable; + + $words = trim($_POST['nsfw-words']); + $word_list = explode(',', $words); + foreach ($word_list as $word) { + $word = trim($word); + if (!$words || $word[0] != '/') { + continue; + } + + if (@preg_match($word, '') === false) { + DI::sysmsg()->addNotice(DI::l10n()->t('Regular expression "%s" fails to compile', $word)); + }; + } + + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'nsfw', 'words', $words); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'nsfw', 'disable', $disable); } } @@ -118,7 +132,7 @@ function nsfw_prepare_body_content_filter(App $a, &$hook_data) $tag_search = false; switch ($word[0]) { case '/'; // Regular expression - $found = preg_match($word, $body); + $found = @preg_match($word, $body); break; case '#': // Hashtag-only search $tag_search = true;