From 7f66600ef4f2a9070d2123c6c53fef70d2383cf6 Mon Sep 17 00:00:00 2001 From: Jebb Date: Thu, 29 Aug 2024 15:04:15 +0800 Subject: [PATCH] #665: Replace usage of utf8_decode and add backward compatibility --- .../joomlatools/library/filter/ascii.php | 2 +- code/libraries/joomlatools/library/legacy.php | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/code/libraries/joomlatools/library/filter/ascii.php b/code/libraries/joomlatools/library/filter/ascii.php index c0e16cb11..72f9f9649 100644 --- a/code/libraries/joomlatools/library/filter/ascii.php +++ b/code/libraries/joomlatools/library/filter/ascii.php @@ -55,7 +55,7 @@ public function sanitize($value) if ($result === null) { - $result = htmlentities(utf8_decode($value), ENT_SUBSTITUTE); + $result = htmlentities(mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8')); $result = preg_replace( array('/ß/','/&(..)lig;/', '/&([aouAOU])uml;/','/&(.)[^;]*;/'), array('ss',"$1","$1".'e',"$1"), diff --git a/code/libraries/joomlatools/library/legacy.php b/code/libraries/joomlatools/library/legacy.php index a081ccfc8..9379b5731 100644 --- a/code/libraries/joomlatools/library/legacy.php +++ b/code/libraries/joomlatools/library/legacy.php @@ -27,7 +27,7 @@ { function mb_strlen($str) { - return strlen(utf8_decode($str)); + return strlen(mb_convert_encoding($str, 'ISO-8859-1', 'UTF-8')); } } @@ -61,7 +61,7 @@ function mb_substr($str, $offset, $length = NULL) if ($offset < 0) { // see notes - $strlen = strlen(utf8_decode($str)); + $strlen = strlen(mb_convert_encoding($str, 'ISO-8859-1', 'UTF-8')); $offset = $strlen + $offset; if ($offset < 0) $offset = 0; @@ -100,7 +100,7 @@ function mb_substr($str, $offset, $length = NULL) if (!isset($strlen)) { // see notes - $strlen = strlen(utf8_decode($str)); + $strlen = strlen(mb_convert_encoding($str, 'ISO-8859-1', 'UTF-8')); } // another trivial case @@ -401,3 +401,25 @@ function str_contains($haystack, $needle) { } } +/** + * utf8 encoding/decoding compatibility + * + * @link https://www.php.net/manual/en/function.utf8-decode.php + */ + + if (!function_exists('utf8_decode')) + { + function utf8_decode($string) + { + return mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8'); + } + } + + if (!function_exists('utf8_encode')) + { + function utf8_encode($string) + { + return mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string)); + } + } +