Exemplo n.º 1
0
 /**
  * @param string $sInputString
  * @param string $sInputFromEncoding
  * @param string $sInputToEncoding
  *
  * @return string
  */
 public static function ConvertEncoding($sInputString, $sInputFromEncoding, $sInputToEncoding)
 {
     $sResult = $sInputString;
     $sFromEncoding = \MailSo\Base\Utils::normalizeCharset($sInputFromEncoding);
     $sToEncoding = \MailSo\Base\Utils::normalizeCharset($sInputToEncoding);
     if ('' === \trim($sResult) || $sFromEncoding === $sToEncoding && \MailSo\Base\Enumerations\Charset::UTF_8 !== $sFromEncoding) {
         return $sResult;
     }
     $bUnknown = false;
     switch (true) {
         default:
             $bUnknown = true;
             break;
         case $sFromEncoding === \MailSo\Base\Enumerations\Charset::ISO_8859_1 && $sToEncoding === \MailSo\Base\Enumerations\Charset::UTF_8 && \function_exists('utf8_encode'):
             $sResult = \utf8_encode($sResult);
             break;
         case $sFromEncoding === \MailSo\Base\Enumerations\Charset::UTF_8 && $sToEncoding === \MailSo\Base\Enumerations\Charset::ISO_8859_1 && \function_exists('utf8_decode'):
             $sResult = \utf8_decode($sResult);
             break;
         case $sFromEncoding === \MailSo\Base\Enumerations\Charset::UTF_7_IMAP && $sToEncoding === \MailSo\Base\Enumerations\Charset::UTF_8:
             $sResult = \MailSo\Base\Utils::Utf7ModifiedToUtf8($sResult);
             if (false === $sResult) {
                 $sResult = $sInputString;
             }
             break;
         case $sFromEncoding === \MailSo\Base\Enumerations\Charset::UTF_8 && $sToEncoding === \MailSo\Base\Enumerations\Charset::UTF_7_IMAP:
             $sResult = \MailSo\Base\Utils::Utf8ToUtf7Modified($sResult);
             if (false === $sResult) {
                 $sResult = $sInputString;
             }
             break;
         case $sFromEncoding === \MailSo\Base\Enumerations\Charset::UTF_7_IMAP:
             $sResult = \MailSo\Base\Utils::ConvertEncoding(\MailSo\Base\Utils::ModifiedToPlainUtf7($sResult), \MailSo\Base\Enumerations\Charset::UTF_7, $sToEncoding);
             break;
         case \in_array(\strtolower($sFromEncoding), \MailSo\Base\Utils::$SuppostedCharsets):
             if (\MailSo\Base\Utils::IsIconvSupported()) {
                 $sResult = @\iconv(\strtoupper($sFromEncoding), \strtoupper($sToEncoding) . '//IGNORE', $sResult);
             } else {
                 if (\MailSo\Base\Utils::IsMbStringSupported()) {
                     $sResult = @\mb_convert_encoding($sResult, \strtoupper($sToEncoding), \strtoupper($sFromEncoding));
                 } else {
                     if (\MailSo\Base\Utils::IsLibIconvSupported()) {
                         $sResult = @\libiconv(\strtoupper($sFromEncoding), \strtoupper($sToEncoding), $sResult);
                     }
                 }
             }
             $sResult = false !== $sResult ? $sResult : $sInputString;
             break;
     }
     if ($bUnknown && \MailSo\Base\Utils::IsMbStringSupported()) {
         $sResult = @\mb_convert_encoding($sResult, $sToEncoding);
     }
     return $sResult;
 }