/** * Encodes a string pursuant to RFC 2231. * * @param string $name The parameter name. * @param string $string The string to encode. * @param string $charset The charset the text should be encoded with. * @param string $lang The language to use when encoding. * * @return array The encoded parameter string. */ public function encodeRFC2231($name, $string, $charset, $lang = null) { $encode = $wrap = false; $output = array(); if (Horde_MIME::is8bit($string, $charset)) { $string = Horde_String::lower($charset) . '\'' . ($lang === null ? '' : Horde_String::lower($lang)) . '\'' . rawurlencode($string); $encode = true; } // 4 = '*', 2x '"', ';' $pre_len = strlen($name) + 4 + ($encode ? 1 : 0); if ($pre_len + strlen($string) > 76) { while ($string) { $chunk = 76 - $pre_len; $pos = min($chunk, strlen($string) - 1); if ($chunk == $pos && $pos > 2) { for ($i = 0; $i <= 2; $i++) { if ($string[$pos - $i] == '%') { $pos -= $i + 1; break; } } } $lines[] = substr($string, 0, $pos + 1); $string = substr($string, $pos + 1); } $wrap = true; } else { $lines = array($string); } $i = 0; foreach ($lines as $val) { $output[] = $name . ($wrap ? '*' . $i++ : '') . ($encode ? '*' : '') . '="' . $val . '"'; } return implode('; ', $output); }
/** * Decodes the contents of the part to either a 7bit or 8bit encoding. * * @return string The decoded text. * Returns the empty string if there is no text to decode. */ public function transferDecode() { $encoding = $this->getCurrentEncoding(); /* If the contents are empty, return now. */ if (!strlen($this->_contents)) { $this->_flags['lastTransferDecode'] = $encoding; return $this->_contents; } switch ($encoding) { case 'base64': $message = base64_decode($this->_contents); $this->_flags['lastTransferDecode'] = '8bit'; break; case 'quoted-printable': $message = preg_replace("/=\r?\n/", '', $this->_contents); $message = $this->replaceEOL($message); $message = quoted_printable_decode($message); $this->_flags['lastTransferDecode'] = Horde_MIME::is8bit($message) ? '8bit' : '7bit'; break; /* Support for uuencoded encoding - although not required by RFCs, some mailers may still encode this way. */ /* Support for uuencoded encoding - although not required by RFCs, some mailers may still encode this way. */ case 'uuencode': case 'x-uuencode': case 'x-uue': if (function_exists('convert_uudecode')) { $message = convert_uuencode($this->_contents); } else { $files =& Mail_mimeDecode::uudecode($this->_contents); $message = $files[0]['filedata']; } $this->_flags['lastTransferDecode'] = '8bit'; break; default: if (isset($this->_flags['lastTransferDecode']) && $this->_flags['lastTransferDecode'] != $encoding) { $message = $this->replaceEOL($this->_contents); } else { $message = $this->_contents; } $this->_flags['lastTransferDecode'] = $encoding; break; } return $message; }