Exemple #1
0
 /**
  * @param string $sEncodedValue
  * @param string $sIncomingCharset = ''
  * @param string $sForcedIncomingCharset = ''
  *
  * @return string
  */
 public static function DecodeHeaderValue($sEncodedValue, $sIncomingCharset = '', $sForcedIncomingCharset = '')
 {
     $sValue = $sEncodedValue;
     if (0 < \strlen($sIncomingCharset)) {
         $sIncomingCharset = \MailSo\Base\Utils::NormalizeCharsetByValue($sIncomingCharset, $sValue);
         $sValue = \MailSo\Base\Utils::ConvertEncoding($sValue, $sIncomingCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
     }
     $sValue = \preg_replace('/\\?=[\\n\\r\\t\\s]{1,5}=\\?/m', '?==?', $sValue);
     $sValue = \preg_replace('/[\\r\\n\\t]+/m', ' ', $sValue);
     $aEncodeArray = array('');
     $aMatch = array();
     \preg_match_all('/=\\?[^\\?]+\\?[q|b|Q|B]\\?[^\\?]*(\\?=)/', $sValue, $aMatch);
     if (isset($aMatch[0]) && \is_array($aMatch[0])) {
         for ($iIndex = 0, $iLen = \count($aMatch[0]); $iIndex < $iLen; $iIndex++) {
             if (isset($aMatch[0][$iIndex])) {
                 $iPos = @\strpos($aMatch[0][$iIndex], '*');
                 if (false !== $iPos) {
                     $aMatch[0][$iIndex][0] = \substr($aMatch[0][$iIndex][0], 0, $iPos);
                 }
             }
         }
         $aEncodeArray = $aMatch[0];
     }
     $aParts = array();
     $sMainCharset = '';
     $bOneCharset = true;
     for ($iIndex = 0, $iLen = \count($aEncodeArray); $iIndex < $iLen; $iIndex++) {
         $aTempArr = array('', $aEncodeArray[$iIndex]);
         if ('=?' === \substr(\trim($aTempArr[1]), 0, 2)) {
             $iPos = \strpos($aTempArr[1], '?', 2);
             $aTempArr[0] = \substr($aTempArr[1], 2, $iPos - 2);
             $sEncType = \strtoupper($aTempArr[1][$iPos + 1]);
             switch ($sEncType) {
                 case 'Q':
                     $sHeaderValuePart = \str_replace('_', ' ', $aTempArr[1]);
                     $aTempArr[1] = \quoted_printable_decode(\substr($sHeaderValuePart, $iPos + 3, \strlen($sHeaderValuePart) - $iPos - 5));
                     break;
                 case 'B':
                     $sHeaderValuePart = $aTempArr[1];
                     $aTempArr[1] = \MailSo\Base\Utils::Base64Decode(\substr($sHeaderValuePart, $iPos + 3, \strlen($sHeaderValuePart) - $iPos - 5));
                     break;
             }
         }
         if (0 < \strlen($aTempArr[0])) {
             $sCharset = 0 === \strlen($sForcedIncomingCharset) ? $aTempArr[0] : $sForcedIncomingCharset;
             $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset, true);
             if ('' === $sMainCharset) {
                 $sMainCharset = $sCharset;
             } else {
                 if ($sMainCharset !== $sCharset) {
                     $bOneCharset = false;
                 }
             }
         }
         $aParts[] = array($aEncodeArray[$iIndex], $aTempArr[1], $sCharset);
         unset($aTempArr);
     }
     for ($iIndex = 0, $iLen = \count($aParts); $iIndex < $iLen; $iIndex++) {
         if ($bOneCharset) {
             $sValue = \str_replace($aParts[$iIndex][0], $aParts[$iIndex][1], $sValue);
         } else {
             $aParts[$iIndex][2] = \MailSo\Base\Utils::NormalizeCharsetByValue($aParts[$iIndex][2], $aParts[$iIndex][1]);
             $sValue = \str_replace($aParts[$iIndex][0], \MailSo\Base\Utils::ConvertEncoding($aParts[$iIndex][1], $aParts[$iIndex][2], \MailSo\Base\Enumerations\Charset::UTF_8), $sValue);
         }
     }
     if ($bOneCharset && 0 < \strlen($sMainCharset)) {
         $sMainCharset = \MailSo\Base\Utils::NormalizeCharsetByValue($sMainCharset, $sValue);
         $sValue = \MailSo\Base\Utils::ConvertEncoding($sValue, $sMainCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
     }
     return $sValue;
 }