public function testReplaceMime() { $fixture = file_get_contents(__DIR__ . '/fixtures/signed_attachment.eml'); $mime = new Horde_ActiveSync_Mime(Horde_Mime_Part::parseMessage($fixture)); foreach ($mime->contentTypeMap() as $id => $type) { if ($mime->isAttachment($id, $type)) { $part = new Horde_Mime_Part(); $part->setType('text/plain'); $part->setContents(sprintf('An attachment named %s was removed by Horde_ActiveSync_Test', $mime->getPart($id)->getName(true))); $mime->removePart($id); $mime->addPart($part); } } $this->assertEquals(true, $mime->hasAttachments()); $this->assertEquals('An attachment named foxtrotjobs.png was removed by Horde_ActiveSync_Test', $mime->getPart('3')->getContents()); }
/** * Fetch a part of a MIME message. * * @param integer $id The MIME index of the part requested. * @param array $options Additional options: * - length: (integer) If set, only download this many bytes of the * bodypart from the server. * DEFAULT: All data is retrieved. * - nocontents: (boolean) If true, don't add the contents to the part * DEFAULT: Contents are added to the part * * @return Horde_Mime_Part The raw MIME part asked for (reference). */ public function getMimePart($id, array $options = array()) { $part = $this->_message->getPart($id); if ($part && strcasecmp($part->getCharset(), 'ISO-8859-1') === 0) { $part->setCharset('windows-1252'); } if (!empty($id) && !is_null($part) && substr($id, -2) != '.0' && empty($options['nocontents']) && !$part->getContents(array('stream' => true))) { $body = $this->getBodyPart($id, array('decode' => true, 'length' => empty($options['length']) ? null : $options['length'], 'stream' => true)); $part->setContents($body, array('encoding' => $this->_lastBodyPartDecode, 'usestream' => true)); } return $part; }
/** * Determine which parts we need, and fetches them from the IMAP client. * Takes into account the available parts and the BODYPREF/BODYPARTPREF * options. */ protected function _getParts() { // Look for the parts we need. We try to detect and fetch only the parts // we need, while ensuring we have something to return. So, e.g., if we // don't have BODYPREF_TYPE_HTML, we only request plain text, but if we // can't find plain text but we have a html body, fetch that anyway. $text_id = $this->_basePart->findBody('plain'); $html_id = $this->_basePart->findBody('html'); // Deduce which part(s) we need to request. $want_html_text = $this->_wantHtml(); $want_plain_text = $this->_wantPlainText($html_id, $want_html_text); $want_html_as_plain = false; if (!empty($text_id) && $want_plain_text) { $text_body_part = $this->_basePart->getPart($text_id); } elseif ($want_plain_text && !empty($html_id) && empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_MIME])) { $want_html_text = true; $want_html_as_plain = true; } if (!empty($html_id) && $want_html_text) { $html_body_part = $this->_basePart->getPart($html_id); } // Make sure we have truncation if needed. if (empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_PLAIN]) && !empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_HTML]) && $want_plain_text && $want_html_text) { // We only have HTML truncation data, requested HTML body but only // have plaintext. $this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_PLAIN] = $this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_HTML]; } // Fetch the data from the IMAP client. $data = $this->_fetchData(array('html_id' => $html_id, 'text_id' => $text_id)); if (!empty($text_id) && $want_plain_text) { $this->_plain = $this->_getPlainPart($data, $text_body_part); } if (!empty($html_id) && $want_html_text) { $results = $this->_getHtmlPart($data, $html_body_part, $want_html_as_plain); $this->_html = !empty($results['html']) ? $results['html'] : null; $this->_plain = !empty($results['plain']) ? $results['plain'] : null; } if (!empty($this->_options['bodypartprefs'])) { $this->_bodyPart = $this->_getBodyPart($data, !empty($html_id) ? $html_body_part : $text_body_part, empty($html_id)); } $text_body_part = null; $html_body_part = null; }
/** * Determine which parts we need, and fetches them from the IMAP client. * Takes into account the available parts and the BODYPREF/BODYPARTPREF * options. */ protected function _getParts() { // Look for the parts we need. We try to detect and fetch only the parts // we need, while ensuring we have something to return. So, e.g., if we // don't have BODYPREF_TYPE_HTML, we only request plain text, but if we // can't find plain text but we have a html body, fetch that anyway. // // If this is any type of Report (like a NDR) we can't use findBody // since some MTAs generate MDRs with no explicit mime type in the // human readable portion (the first part). We assume the MDR contains // three parts as specified in the RFC: (1) A human readable part, (2) // A machine parsable body Machine parsable body part // [message/disposition-notification] and (3) The (optional) original // message [message/rfc822] switch ($this->_basePart->getType()) { case 'message/disposition-notification': // OL may send this without an appropriate multipart/report wrapper. // Not sure what to do about this yet. Probably parse the machine // part and write out some basic text? break; case 'multipart/report': $iterator = $this->_basePart->partIterator(false); $iterator->rewind(); if (!($curr = $iterator->current())) { break; } $text_id = $curr->getMimeId(); $html_id = null; break; default: $text_id = $this->_basePart->findBody('plain'); $html_id = $this->_basePart->findBody('html'); } // Deduce which part(s) we need to request. $want_html_text = $this->_wantHtml(); $want_plain_text = $this->_wantPlainText($html_id, $want_html_text); $want_html_as_plain = false; if (!empty($text_id) && $want_plain_text) { $text_body_part = $this->_basePart->getPart($text_id); } elseif ($want_plain_text && !empty($html_id) && empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_MIME])) { $want_html_text = true; $want_html_as_plain = true; } if (!empty($html_id) && $want_html_text) { $html_body_part = $this->_basePart->getPart($html_id); } // Make sure we have truncation if needed. if (empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_PLAIN]) && !empty($this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_HTML]) && $want_plain_text && $want_html_text) { // We only have HTML truncation data, requested HTML body but only // have plaintext. $this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_PLAIN] = $this->_options['bodyprefs'][Horde_ActiveSync::BODYPREF_TYPE_HTML]; } // Fetch the data from the IMAP client. $data = $this->_fetchData(array('html_id' => $html_id, 'text_id' => $text_id)); if (!empty($text_id) && $want_plain_text) { $this->_plain = $this->_getPlainPart($data, $text_body_part); } if (!empty($html_id) && $want_html_text) { $results = $this->_getHtmlPart($data, $html_body_part, $want_html_as_plain); $this->_html = !empty($results['html']) ? $results['html'] : null; $this->_plain = !empty($results['plain']) ? $results['plain'] : null; } if (!empty($this->_options['bodypartprefs'])) { $this->_bodyPart = $this->_getBodyPart($data, !empty($html_id) ? $html_body_part : $text_body_part, empty($html_id)); } $text_body_part = null; $html_body_part = null; }