From 50a0b22d0042f6b41d6a0727a20bcba4c3c5d6f8 Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sat, 2 Jul 2022 18:19:32 -0700 Subject: [PATCH 1/2] Add 'falsiness' check to MessageFilter. --- Code/Lib/MessageFilter.php | 10 ++++++++++ include/items.php | 2 -- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Code/Lib/MessageFilter.php b/Code/Lib/MessageFilter.php index 6d64f93fc..36a242664 100644 --- a/Code/Lib/MessageFilter.php +++ b/Code/Lib/MessageFilter.php @@ -127,6 +127,7 @@ class MessageFilter * - ?foo {} baz which will check if 'baz' is an array element in item.foo * - ?foo {*} baz which will check if 'baz' is an array key in item.foo * - ?foo which will check for a return of a true condition for item.foo; + * - ?!foo which will check for a return of a false condition for item.foo; * * The values 0, '', an empty array, and an unset value will all evaluate to false. * @@ -208,6 +209,15 @@ class MessageFilter } return false; } + + // Ordering of this check (for falsiness) with relation to the following one (check for truthiness) is important. + if (preg_match('/\!(.*?)$/', $s, $matches)) { + $x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR); + if (!$x) { + return true; + } + return false; + } if (preg_match('/(.*?)$/', $s, $matches)) { $x = ((array_key_exists(trim($matches[1]),$item)) ? $item[trim($matches[1])] : EMPTY_STR); diff --git a/include/items.php b/include/items.php index 12c0bba9c..8ccfe7027 100644 --- a/include/items.php +++ b/include/items.php @@ -3438,7 +3438,6 @@ function post_is_importable($channel_id, $item, $abook) { return true; } - $incl = PConfig::get($channel_id, 'system', 'message_filter_incl', EMPTY_STR); $excl = PConfig::get($channel_id, 'system', 'message_filter_excl', EMPTY_STR); if ($incl || $excl) { @@ -3449,7 +3448,6 @@ function post_is_importable($channel_id, $item, $abook) { } } - if (! $abook) { return true; } From df4eabd23bb735011e23c6ad774cc618052671ea Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sat, 2 Jul 2022 22:36:49 -0700 Subject: [PATCH 2/2] refactor language checking in MessageFilter to account for ambiguous language results. --- Code/Lib/MessageFilter.php | 42 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/Code/Lib/MessageFilter.php b/Code/Lib/MessageFilter.php index 36a242664..45b941323 100644 --- a/Code/Lib/MessageFilter.php +++ b/Code/Lib/MessageFilter.php @@ -13,14 +13,18 @@ class MessageFilter $text = prepare_text($item['body'],((isset($item['mimetype'])) ? $item['mimetype'] : 'text/x-multicode')); - $text = html2plain(($item['title']) ? $item['title'] . ' ' . $text : $text); + $text = html2plain((isset($item['title']) && $item['title']) ? $item['title'] . ' ' . $text : $text); $lang = null; + // Language matching is a bit tricky, because the language can be ambiguous (detect_language() returns ''). + // If the language is ambiguous, the message will be accepted regardless of language rules. + if ((strpos($incl, 'lang=') !== false) || (strpos($excl, 'lang=') !== false) || (strpos($incl, 'lang!=') !== false) || (strpos($excl, 'lang!=') !== false)) { $lang = detect_language($text); } + $tags = ((isset($item['term']) && is_array($item['term']) && count($item['term'])) ? $item['term'] : false); // exclude always has priority @@ -33,7 +37,19 @@ class MessageFilter if (! $word) { continue; } - if (substr($word, 0, 1) === '#' && $tags) { + if (isset($lang) && ((strpos($word, 'lang=') === 0) || (strpos($word, 'lang!=') === 0))) { + if (! strlen($lang)) { + // Result is ambiguous. As we are matching deny rules only at this time, continue tests. + // Any matching deny rule concludes testing. + continue; + } + if (strpos($word, 'lang=') === 0 && strcasecmp($lang, trim(substr($word, 5))) == 0) { + return false; + } elseif (strpos($word, 'lang!=') === 0 && strcasecmp($lang, trim(substr($word, 6))) != 0) { + return false; + } + } + elseif (substr($word, 0, 1) === '#' && $tags) { foreach ($tags as $t) { if ((($t['ttype'] == TERM_HASHTAG) || ($t['ttype'] == TERM_COMMUNITYTAG)) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) { return false; @@ -55,10 +71,6 @@ class MessageFilter } } elseif ((strpos($word, '/') === 0) && preg_match($word, $text)) { return false; - } elseif ((strpos($word, 'lang=') === 0) && ($lang) && (strcasecmp($lang, trim(substr($word, 5))) == 0)) { - return false; - } elseif ((strpos($word, 'lang!=') === 0) && ($lang) && (strcasecmp($lang, trim(substr($word, 6))) != 0)) { - return false; } elseif (stristr($text, $word) !== false) { return false; } @@ -73,7 +85,19 @@ class MessageFilter if (! $word) { continue; } - if (substr($word, 0, 1) === '#' && $tags) { + if (isset($lang) && ((strpos($word, 'lang=') === 0) || (strpos($word, 'lang!=') === 0))) { + if (! strlen($lang)) { + // Result is ambiguous. However we are checking allow rules + // and an ambiguous language is always permitted. + return true; + } + if (strpos($word, 'lang=') === 0 && strcasecmp($lang, trim(substr($word, 5))) == 0) { + return true; + } elseif (strpos($word, 'lang!=') === 0 && strcasecmp($lang, trim(substr($word, 6))) != 0) { + return true; + } + } + elseif (substr($word, 0, 1) === '#' && $tags) { foreach ($tags as $t) { if ((($t['ttype'] == TERM_HASHTAG) || ($t['ttype'] == TERM_COMMUNITYTAG)) && (($t['term'] === substr($word, 1)) || (substr($word, 1) === '*'))) { return true; @@ -95,10 +119,6 @@ class MessageFilter } } elseif ((strpos($word, '/') === 0) && preg_match($word, $text)) { return true; - } elseif ((strpos($word, 'lang=') === 0) && ($lang) && (strcasecmp($lang, trim(substr($word, 5))) == 0)) { - return true; - } elseif ((strpos($word, 'lang!=') === 0) && ($lang) && (strcasecmp($lang, trim(substr($word, 6))) != 0)) { - return true; } elseif (stristr($text, $word) !== false) { return true; }