Replace PHP 8.2.0 deprecated utf8_decode/utf8_encode

This commit is contained in:
the-djmaze 2022-10-06 09:43:34 +02:00
parent 897f22ee6e
commit ba19d4e6b1
6 changed files with 24 additions and 50 deletions

View file

@ -75,40 +75,24 @@ abstract class Locale
public static function DetectSystemCharset() : string public static function DetectSystemCharset() : string
{ {
$sResult = ''; $sLocale = \strtolower(\trim(\setlocale(LC_ALL, '')));
$sLocale = \setlocale(LC_ALL, ''); foreach (static::$aLocaleMapping as $sKey => $sValue) {
$sLocaleLower = \strtolower(\trim($sLocale)); if (\str_contains($sLocale, $sKey) || \str_contains($sLocale, '.'.$sValue)) {
return $sValue;
foreach (static::$aLocaleMapping as $sKey => $sValue)
{
if (false !== \strpos($sLocaleLower, $sKey) ||
false !== \strpos($sLocaleLower, '.'.$sValue))
{
$sResult = $sValue;
break;
} }
} }
return '';
return $sResult;
} }
public static function ConvertSystemString(string $sSrt) : string public static function ConvertSystemString(string $sSrt) : string
{ {
$sSrt = \trim($sSrt); $sSrt = \trim($sSrt);
if (!empty($sSrt) && !Utils::IsUtf8($sSrt)) if (!empty($sSrt) && !Utils::IsUtf8($sSrt)) {
{
$sCharset = static::DetectSystemCharset(); $sCharset = static::DetectSystemCharset();
if (!empty($sCharset)) $sSrt = $sCharset
{ ? Utils::ConvertEncoding($sSrt, $sCharset, Enumerations\Charset::UTF_8)
$sSrt = Utils::ConvertEncoding( : \mb_convert_encoding($sSrt, 'UTF-8', 'ISO-8859-1');
$sSrt, $sCharset, Enumerations\Charset::UTF_8);
}
else
{
$sSrt = \utf8_encode($sSrt);
}
} }
return $sSrt; return $sSrt;
} }

View file

@ -183,22 +183,12 @@ abstract class Utils
return $sInputString; return $sInputString;
} }
if ($sToEncoding === Enumerations\Charset::UTF_8) { if ($sToEncoding === Enumerations\Charset::UTF_8 && $sFromEncoding === 'utf7-imap') {
if ($sFromEncoding === Enumerations\Charset::ISO_8859_1) { return static::Utf7ModifiedToUtf8($sInputString);
return \utf8_encode($sInputString);
}
if ($sFromEncoding === 'utf7-imap') {
return static::Utf7ModifiedToUtf8($sInputString);
}
} }
if ($sFromEncoding === Enumerations\Charset::UTF_8) { if ($sFromEncoding === Enumerations\Charset::UTF_8 && $sToEncoding === 'utf7-imap') {
if ($sToEncoding === Enumerations\Charset::ISO_8859_1) { return static::Utf8ToUtf7Modified($sInputString);
return \utf8_decode($sInputString);
}
if ($sToEncoding === 'utf7-imap') {
return static::Utf8ToUtf7Modified($sInputString);
}
} }
return static::MbConvertEncoding($sInputString, $sFromEncoding, $sToEncoding); return static::MbConvertEncoding($sInputString, $sFromEncoding, $sToEncoding);
@ -1129,9 +1119,10 @@ abstract class Utils
{ {
$sResult = \is_callable('imap_mutf7_to_utf8') $sResult = \is_callable('imap_mutf7_to_utf8')
? \imap_mutf7_to_utf8($sStr) ? \imap_mutf7_to_utf8($sStr)
: \mb_convert_encoding($sStr, 'UTF-8', 'UTF7-IMAP'); // : \mb_convert_encoding($sStr, 'UTF-8', 'UTF7-IMAP');
// static::MbConvertEncoding($sStr, 'UTF7-IMAP', 'UTF-8'); : static::MbConvertEncoding($sStr, 'UTF7-IMAP', 'UTF-8');
// $sResult = \UConverter::transcode($sStr, \UConverter::UTF8, \UConverter::IMAP_MAILBOX); // ucnv U_FILE_ACCESS_ERROR
// $sResult = \UConverter::transcode($sStr, \UConverter::UTF8, \UConverter::IMAP_MAILBOX, ['to_subst' => '<27>']);
return (false === $sResult) ? $sStr : $sResult; return (false === $sResult) ? $sStr : $sResult;
} }
@ -1139,8 +1130,9 @@ abstract class Utils
{ {
$sResult = \is_callable('imap_utf8_to_mutf7') $sResult = \is_callable('imap_utf8_to_mutf7')
? \imap_utf8_to_mutf7($sStr) ? \imap_utf8_to_mutf7($sStr)
: \mb_convert_encoding($sStr, 'UTF7-IMAP', 'UTF-8'); // : \mb_convert_encoding($sStr, 'UTF7-IMAP', 'UTF-8');
// static::MbConvertEncoding($sStr, 'UTF-8', 'UTF7-IMAP'); : static::MbConvertEncoding($sStr, 'UTF-8', 'UTF7-IMAP');
// ucnv U_FILE_ACCESS_ERROR
// $sResult = \UConverter::transcode($sStr, \UConverter::IMAP_MAILBOX, \UConverter::UTF8); // $sResult = \UConverter::transcode($sStr, \UConverter::IMAP_MAILBOX, \UConverter::UTF8);
return (false === $sResult) ? $sStr : $sResult; return (false === $sResult) ? $sStr : $sResult;
} }

View file

@ -219,7 +219,7 @@ class ImapClient extends \MailSo\Net\NetClient
} }
else else
{ {
$sPassword = $this->EscapeString(\utf8_decode($sPassword)); $sPassword = $this->EscapeString(\mb_convert_encoding($sPassword, 'ISO-8859-1', 'UTF-8'));
if ($this->oLogger) if ($this->oLogger)
{ {
$this->oLogger->AddSecret($sPassword); $this->oLogger->AddSecret($sPassword);

View file

@ -41,7 +41,7 @@ class Legacy
$sValue = \trim($oProp); $sValue = \trim($oProp);
if ($bOldVersion && !isset($oProp->parameters['CHARSET'])) { if ($bOldVersion && !isset($oProp->parameters['CHARSET'])) {
if (\strlen($sValue)) { if (\strlen($sValue)) {
$sEncValue = \utf8_encode($sValue); $sEncValue = \mb_convert_encoding($sValue, 'UTF-8', 'ISO-8859-1');
if (\strlen($sEncValue)) { if (\strlen($sEncValue)) {
$sValue = $sEncValue; $sValue = $sEncValue;
} }
@ -67,7 +67,7 @@ class Legacy
$sValue = \trim($sValue); $sValue = \trim($sValue);
if ($bOldVersion && !isset($oVCard->N->parameters['CHARSET'])) { if ($bOldVersion && !isset($oVCard->N->parameters['CHARSET'])) {
if (\strlen($sValue)) { if (\strlen($sValue)) {
$sEncValue = \utf8_encode($sValue); $sEncValue = \mb_convert_encoding($sValue, 'UTF-8', 'ISO-8859-1');
if (\strlen($sEncValue)) { if (\strlen($sEncValue)) {
$sValue = $sEncValue; $sValue = $sEncValue;
} }

View file

@ -1030,7 +1030,7 @@ class PdoAddressBook
catch (\Throwable $oException) { catch (\Throwable $oException) {
$sResult = $oException->getMessage(); $sResult = $oException->getMessage();
if (!empty($sResult) && !\MailSo\Base\Utils::IsAscii($sResult) && !\MailSo\Base\Utils::IsUtf8($sResult)) { if (!empty($sResult) && !\MailSo\Base\Utils::IsAscii($sResult) && !\MailSo\Base\Utils::IsUtf8($sResult)) {
$sResult = \utf8_encode($sResult); $sResult = \mb_convert_encoding($sResult, 'UTF-8', 'ISO-8859-1');
} }
if (!\is_string($sResult) || empty($sResult)) { if (!\is_string($sResult) || empty($sResult)) {

View file

@ -465,8 +465,6 @@ class MimeDir extends Parser
case 'utf-8': case 'utf-8':
break; break;
case 'iso-8859-1': case 'iso-8859-1':
$property['value'] = utf8_encode($property['value']);
break;
case 'windows-1252': case 'windows-1252':
$property['value'] = mb_convert_encoding($property['value'], 'UTF-8', $charset); $property['value'] = mb_convert_encoding($property['value'], 'UTF-8', $charset);
break; break;