/**
  * 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;
 }
Exemple #2
0
 /**
  * Add the appropriate MIME headers for this part to an existing array.
  *
  * @param array $headers An array of any other headers for the part.
  *
  * @return array  The headers, with the MIME headers added.
  */
 public function header($headers = array())
 {
     $eol = $this->getEOL();
     $ptype = $this->getPrimaryType();
     /* Get the character set for this part. */
     $charset = $this->getCharset();
     /* Get the Content-Type - this is ALWAYS required. */
     $ctype = $this->getType(true);
     foreach ($this->getAllContentTypeParameters() as $key => $value) {
         /* Skip the charset key since that would have already been
          * added to $ctype by getType(). */
         if ($key == 'charset') {
             continue;
         }
         $encode_2231 = Horde_MIME::encodeRFC2231($key, $value, $charset);
         /* Try to work around non RFC 2231-compliant MUAs by sending both
          * a RFC 2047-like parameter name and then the correct RFC 2231
          * parameter.  See:
          *   http://lists.horde.org/archives/dev/Week-of-Mon-20040426/014240.html */
         /*if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) &&
               ((strpos($encode_2231, '*=') !== false) ||
                (strpos($encode_2231, '*0=') !== false))) {
               $ctype .= '; ' . $key . '="' . MIME::encode($value, $charset) . '"';
           }*/
         $ctype .= '; ' . $encode_2231;
     }
     $headers['Content-Type'] = Horde_MIME::wrapHeaders('Content-Type', $ctype, $eol);
     /* Get the description, if any. */
     if ($descrip = $this->getDescription()) {
         $headers['Content-Description'] = Horde_MIME::wrapHeaders('Content-Description', Horde_MIME::encode($descrip, $charset), $eol);
     }
     /* message/* parts require no additional header information. */
     if ($ptype == 'message') {
         return $headers;
     }
     /* Don't show Content-Disposition for multipart messages unless
        there is a name parameter. */
     $name = $this->getName();
     if ($ptype != 'multipart' || !empty($name)) {
         $disp = $this->getDisposition();
         /* Add any disposition parameter information, if available. */
         if (!empty($name)) {
             $encode_2231 = Horde_MIME::encodeRFC2231('filename', $name, $charset);
             /* Same broken RFC 2231 workaround as above. */
             /*if (!empty($GLOBALS['conf']['mailformat']['brokenrfc2231']) &&
                   ((strpos($encode_2231, '*=') !== false) ||
                    (strpos($encode_2231, '*0=') !== false))) {
                   $disp .= '; filename="' . MIME::encode($name, $charset) . '"';
               }*/
             $disp .= '; ' . $encode_2231;
         }
         $headers['Content-Disposition'] = Horde_MIME::wrapHeaders('Content-Disposition', $disp, $eol);
     }
     /* Add transfer encoding information. */
     $headers['Content-Transfer-Encoding'] = $this->getTransferEncoding();
     /* Add content ID information. */
     if (!is_null($this->_contentid)) {
         $headers['Content-ID'] = $this->_contentid;
     }
     return $headers;
 }