/** * Builds an array consisting of MIME header/value pairs. * * @param string $headers A text string containing the headers (e.g. * output from imap_fetchheader()). * @param boolean $decode Should the headers be decoded? * @param boolean $lowercase Should the keys be in lowercase? * * @return array An array consisting of the header name as the key and * the header value as the value. * A header with multiple entries will be stored in * 'value' as an array. */ public static function parseMIMEHeaders($headers, $decode = true, $lowercase = false) { $header = $headval = ''; $ob = $toprocess = array(); foreach (explode("\n", $headers) as $val) { $val = rtrim($val); if (preg_match("/^([^\\s]+)\\:\\s*(.*)/", $val, $matches)) { if (!empty($header)) { $toprocess[] = array($header, $headval); } $header = $matches[1]; $headval = $matches[2]; } else { $val = ltrim($val); if ($val) { $headval .= ' ' . ltrim($val); } else { break; } } } if (!empty($header)) { $toprocess[] = array($header, $headval); } foreach ($toprocess as $val) { if ($decode) { // Fields defined in RFC 2822 that contain address information if (in_array(Horde_String::lower($val[0]), array('from', 'to', 'cc', 'bcc', 'reply-to', 'resent-to', 'resent-cc', 'resent-bcc', 'resent-from', 'sender'))) { $val[1] = Horde_MIME::decodeAddrString($val[1]); } else { $val[1] = Horde_MIME::decode($val[1]); } } if (isset($ob[$val[0]])) { if (!is_array($ob[$val[0]])) { $temp = $ob[$val[0]]; $ob[$val[0]] = array(); $ob[$val[0]][] = $temp; } $ob[$val[0]][] = $val[1]; } else { $ob[$val[0]] = $val[1]; } } return $lowercase ? array_change_key_case($ob, CASE_LOWER) : $ob; }
/** * Returns the MIME encoding for the given input. * * @param mixed $input Either the MIME code or encoding string. * @param integer $format If MIME_CODE, return code. * If MIME_STRING, returns lowercase string. * If not set, returns the opposite value. * * @return mixed See above. */ public function encoding($input, $format = null) { return Horde_MIME::_getCode($input, $format, 'mime_encodings'); }
/** * Take a set of headers and make sure they are encoded properly. * * @param array $headers The headers to encode. * @param string $charset The character set to use. * * @return array The array of encoded headers. */ public function encode($headers, $charset) { $addressKeys = array('To', 'Cc', 'Bcc', 'From'); $asciikeys = array('MIME-Version', 'Received', 'Message-ID', 'Date', 'Content-Disposition', 'Content-Transfer-Encoding', 'Content-ID', 'Content-Type', 'Content-Description'); foreach ($headers as $key => $val) { if (is_array($val)) { foreach ($val as $key2 => $val2) { $headers[$key][$key2] = Horde_MIME::wrapHeaders($key, $val2, $this->getEOL()); } } else { if (in_array($key, $addressKeys)) { $text = Horde_MIME::encodeAddress($val, $charset, $this->_defaultServer); } else { $text = Horde_MIME::encode($val, in_array($key, $asciikeys) ? 'US-ASCII' : $charset); } $headers[$key] = Horde_MIME::wrapHeaders($key, $text, $this->getEOL()); } } return $headers; }
/** * 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; }