diff --git a/cleantalk.antispam/include.php b/cleantalk.antispam/include.php index 4e66840..5b7b134 100644 --- a/cleantalk.antispam/include.php +++ b/cleantalk.antispam/include.php @@ -1536,6 +1536,11 @@ static function CheckAllBefore(&$arEntity, $bSendEmail = FALSE, $form_errors = n 'event_token' => isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : null, ); + // Fix encoding recursively for message + if (is_array($arEntity['message'])) { + $arEntity['message'] = self::fix_encoding_recursive($arEntity['message']); + } + switch ($type) { case 'topic_add': case 'comment': @@ -1735,6 +1740,30 @@ static function CheckAllBefore(&$arEntity, $bSendEmail = FALSE, $form_errors = n return false; } + /** + * Fix encoding recursively in array + * @param array $array + * @param string $from + * @param string $to + */ + private static function fix_encoding_recursive($array, $from = 'CP1251', $to = 'UTF-8') { + foreach ($array as $key => $value) { + if (is_array($value)) { + $array[$key] = self::fix_encoding_recursive($value, $from, $to); + } else { + if (is_string($value) && !mb_check_encoding($value, $to)) { + $converted = iconv($from, $to . '//IGNORE', $value); + if (mb_check_encoding($converted, $to)) { + $array[$key] = $converted; + } else { + unset($array[$key]); + } + } + } + } + return $array; + } + /** * Addon to CheckAllBefore method after comments/messages checking * It fills special CleanTalk tables according to CleanTalk result