getContents() public method

Return the body of the part.
public getContents ( array $options = [] ) : mixed
$options array Additional options: - canonical: (boolean) Returns the contents in strict RFC 822 & 2045 output - namely, all newlines end with the canonical sequence. DEFAULT: No - stream: (boolean) Return the body as a stream resource. DEFAULT: No
return mixed The body text (string) of the part, null if there is no contents, and a stream resource if 'stream' is true.
Example #1
0
    public function testBug10431()
    {
        $text = 'Das könnte zum Beispiel so aussehen, dass wir bei entsprechenden Anfragen diese an eine Kontaktperson bei Euch weiterleiten. Oder Ihr schnürt ein entsprechendes Paket, dass wir in unseren Angeboten mit anführen. Bei erfolgreicher Vermittlung bekämen wir eine Vermittlungsgebühr.
Wir ständen dann weiterhin für 3rd-Level-Support zur Verfügung, d.h. für alle Anfragen des Kunden bzgl. Horde, die nicht zum Tagesgeschäft gehören.';
        $text = Horde_String::convertCharset($text, 'UTF-8', 'ISO-8859-1');
        $textBody = new Horde_Mime_Part();
        $textBody->setType('text/plain');
        $textBody->setCharset('ISO-8859-1');
        $flowed = new Horde_Text_Flowed($text, 'ISO-8859-1');
        $flowed->setDelSp(true);
        $textBody->setContents($flowed->toFlowed());
        $flowed_txt = $textBody->toString(array('headers' => false));
        $textBody2 = new Horde_Mime_Part();
        $textBody2->setType('text/plain');
        $textBody2->setCharset('ISO-8859-1');
        $textBody2->setContents($flowed_txt, array('encoding' => 'quoted-printable'));
        $flowed2 = new Horde_Text_Flowed($textBody2->getContents(), 'ISO-8859-1');
        $flowed2->setMaxLength(0);
        $flowed2->setDelSp(true);
        $this->assertEquals($text, trim($flowed2->toFixed()));
    }
Example #2
0
 /**
  * Build the data needed for the BodyPart part.
  *
  * @param  Horde_Imap_Client_Data_Fetch $data  The FETCH results.
  * @param  Horde_Mime_Part $mime  The plaintext MIME part.
  * @param boolean $to_html        If true, $id is assumed to be a text/plain
  *                                part and is converted to html.
  *
  * @return array  The BodyPart data.
  *     - charset:  (string)   The charset of the text.
  *     - body: (string)       The body text.
  *     - truncated: (boolean) True if text was truncated.
  *     - size: (integer)      The original part size, in bytes.
  */
 protected function _getBodyPart(Horde_Imap_Client_Data_Fetch $data, Horde_Mime_Part $mime, $to_html)
 {
     $id = $mime->getMimeId();
     $text = $data->getBodyPart($id);
     if (!$data->getBodyPartDecode($id)) {
         $mime->setContents($text);
         $text = $mime->getContents();
     }
     if ($to_html) {
         $text = Horde_Text_Filter::filter($text, 'Text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO, 'charset' => $mime->getCharset()));
         $size = strlen($text);
     } else {
         $size = !is_null($data->getBodyPartSize($id)) ? $data->getBodyPartSize($id) : strlen($text);
     }
     if (!empty($this->_options['bodypartprefs']['truncationsize'])) {
         $text = Horde_String::substr($text, 0, $this->_options['bodypartprefs']['truncationsize'], $mime->getCharset());
     }
     return array('charset' => $mime->getCharset(), 'body' => $text, 'truncated' => $size > strlen($text), 'size' => $size);
 }
Example #3
0
 /**
  * Process a message again to add body and attachment data.
  *
  * @param \Horde_Imap_Client_Data_Fetch $messagedata The structure and part of the message body
  * @param \Horde_Mime_Part $partdata The part data
  * @param string $part The part ID.
  * @param string $filename The filename of the attachment
  * @return \stdClass
  * @throws \core\message\inbound\processing_failed_exception If the attachment can't be saved to disk.
  */
 private function process_message_part_attachment($messagedata, $partdata, $part, $filename)
 {
     global $CFG;
     // If a filename is present, assume that this part is an attachment.
     $attachment = new \stdClass();
     $attachment->filename = $filename;
     $attachment->type = $partdata->getType();
     $attachment->content = $partdata->getContents();
     $attachment->charset = $partdata->getCharset();
     $attachment->description = $partdata->getDescription();
     $attachment->contentid = $partdata->getContentId();
     $attachment->filesize = $messagedata->getBodyPartSize($part);
     if (!empty($CFG->antiviruses)) {
         mtrace("--> Attempting virus scan of '{$attachment->filename}'");
         // Store the file on disk - it will need to be virus scanned first.
         $itemid = rand(1, 999999999);
         $directory = make_temp_directory("/messageinbound/{$itemid}", false);
         $filepath = $directory . "/" . $attachment->filename;
         if (!($fp = fopen($filepath, "w"))) {
             // Unable to open the temporary file to write this to disk.
             mtrace("--> Unable to save the file to disk for virus scanning. Check file permissions.");
             throw new \core\message\inbound\processing_failed_exception('attachmentfilepermissionsfailed', 'tool_messageinbound');
         }
         fwrite($fp, $attachment->content);
         fclose($fp);
         // Perform a virus scan now.
         try {
             \core\antivirus\manager::scan_file($filepath, $attachment->filename, true);
         } catch (\core\antivirus\scanner_exception $e) {
             mtrace("--> A virus was found in the attachment '{$attachment->filename}'.");
             $this->inform_attachment_virus();
             return;
         }
     }
     return $attachment;
 }
Example #4
0
 /**
  * Sends this message.
  *
  * @param Mail $mailer     A Mail object.
  * @param boolean $resend  If true, the message id and date are re-used;
  *                         If false, they will be updated.
  * @param boolean $flowed  Send message in flowed text format.
  *
  * @throws Horde_Mime_Exception
  */
 public function send($mailer, $resend = false, $flowed = true)
 {
     /* Add mandatory headers if missing. */
     $has_header = $this->_headers->getValue('Message-ID');
     if (!$resend || !$has_header) {
         if ($has_header) {
             $this->_headers->removeHeader('Message-ID');
         }
         $this->_headers->addMessageIdHeader();
     }
     if (!$this->_headers->getValue('User-Agent')) {
         $this->_headers->addUserAgentHeader();
     }
     $has_header = $this->_headers->getValue('Date');
     if (!$resend || !$has_header) {
         if ($has_header) {
             $this->_headers->removeHeader('Date');
         }
         $this->_headers->addHeader('Date', date('r'));
     }
     if (isset($this->_base)) {
         $basepart = $this->_base;
     } else {
         /* Send in flowed format. */
         if ($flowed && !empty($this->_body)) {
             $flowed = new Horde_Text_Flowed($this->_body->getContents(), $this->_body->getCharset());
             $flowed->setDelSp(true);
             $this->_body->setContentTypeParameter('format', 'flowed');
             $this->_body->setContentTypeParameter('DelSp', 'Yes');
             $this->_body->setContents($flowed->toFlowed());
         }
         /* Build mime message. */
         $body = new Horde_Mime_Part();
         if (!empty($this->_body) && !empty($this->_htmlBody)) {
             $body->setType('multipart/alternative');
             $this->_body->setDescription(Horde_Mime_Translation::t("Plaintext Version of Message"));
             $body->addPart($this->_body);
             $this->_htmlBody->setDescription(Horde_Mime_Translation::t("HTML Version of Message"));
             $body->addPart($this->_htmlBody);
         } elseif (!empty($this->_htmlBody)) {
             $body = $this->_htmlBody;
         } elseif (!empty($this->_body)) {
             $body = $this->_body;
         }
         if (count($this->_parts)) {
             $basepart = new Horde_Mime_Part();
             $basepart->setType('multipart/mixed');
             $basepart->isBasePart(true);
             if ($body) {
                 $basepart->addPart($body);
             }
             foreach ($this->_parts as $mime_part) {
                 $basepart->addPart($mime_part);
             }
         } else {
             $basepart = $body;
             $basepart->isBasePart(true);
         }
     }
     $basepart->setHeaderCharset($this->_charset);
     /* Build recipients. */
     $recipients = clone $this->_recipients;
     foreach (array('to', 'cc') as $header) {
         $recipients->add($this->_headers->getOb($header));
     }
     if ($this->_bcc) {
         $recipients->add($this->_bcc);
     }
     /* Trick Horde_Mime_Part into re-generating the message headers. */
     $this->_headers->removeHeader('MIME-Version');
     /* Send message. */
     $recipients->unique();
     $basepart->send($recipients->writeAddress(), $this->_headers, $mailer);
     /* Remember the basepart */
     $this->_base = $basepart;
 }
Example #5
0
 /**
  * @return string
  */
 public function getContents()
 {
     return $this->mimePart->getContents();
 }
Example #6
0
 public function testNoOverwriteOfPartContentsWithItsOwnStreamData()
 {
     $text = 'foo';
     $part = new Horde_Mime_Part();
     $part->setType('text/plain');
     $part->setContents($text);
     $stream = $part->getContents(array('stream' => true));
     $part->setContents($stream);
     $this->assertEquals($text, $part->getContents());
 }
Example #7
0
 /**
  * Rebuilds $part and forces it to be a base64 encoded
  * application/octet-stream part.
  *
  * @param  Horde_Mime_Part $part   The MIME part.
  */
 protected function _reEncodeMessageAttachment(Horde_Mime_Part $part)
 {
     $new_part = Horde_Mime_Part::parseMessage($part->getContents());
     $part->setContents($new_part->getContents(array('stream' => true)), array('encoding' => self::ENCODE_BINARY));
     $part->setTransferEncoding('base64', array('send' => true));
 }
Example #8
0
 /**
  *
  * @param unknown $struct
  * @param unknown $partno
  * @param unknown $outStruct
  */
 public function subMimeStructToFlatStruct(Horde_Mime_Part $struct, &$outStruct)
 {
     $partno = $struct->getMimeId();
     $outStruct[$partno] = array();
     $outStruct[$partno]['type'] = $struct->getType(false);
     $outStruct[$partno]['subtype'] = $struct->getSubType();
     $outStruct[$partno]['content'] = $struct->getContents(array('stream' => false));
     if ($v = $struct->getCharset()) {
         $outStruct[$partno]['charset'] = $v;
     }
     if ($v = $struct->getName(false)) {
         $outStruct[$partno]['name'] = $v;
     }
     if ($v = $struct->getSize(false)) {
         $outStruct[$partno]['bytes'] = $v;
     }
     if ($v = $struct->getContentId()) {
         $outStruct[$partno]['id'] = $v;
     }
     foreach ($struct->getParts() as $sStruct) {
         $this->subMimeStructToFlatStruct($sStruct, $outStruct);
     }
 }
Example #9
0
File: Base.php Project: horde/horde
 /**
  * Internal helper function to create render data array for a MIME Part
  * object that only has a single part.
  *
  * @param string $data  The rendered data.
  * @param string $type  The rendered type.
  *
  * @return array  See render().
  */
 protected function _renderReturn($data = null, $type = null)
 {
     return array($this->_mimepart->getMimeId() => array('data' => is_null($data) ? $this->_mimepart->getContents() : $data, 'status' => array(), 'type' => is_null($type) ? $this->_mimepart->getType() : $type));
 }
Example #10
0
 /**
  * @param \Horde_Mime_Part $p
  * @param int $partNo
  * @return string
  * @throws DoesNotExistException
  * @throws \Exception
  */
 private function loadBodyData($p, $partNo)
 {
     // DECODE DATA
     $fetch_query = new \Horde_Imap_Client_Fetch_Query();
     $ids = new \Horde_Imap_Client_Ids($this->messageId);
     $fetch_query->bodyPart($partNo, ['peek' => true]);
     $fetch_query->bodyPartSize($partNo);
     $fetch_query->mimeHeader($partNo, ['peek' => true]);
     $headers = $this->conn->fetch($this->mailBox, $fetch_query, ['ids' => $ids]);
     /** @var $fetch \Horde_Imap_Client_Data_Fetch */
     $fetch = $headers[$this->messageId];
     if (is_null($fetch)) {
         throw new DoesNotExistException("Mail body for this mail({$this->messageId}) could not be loaded");
     }
     $mimeHeaders = $fetch->getMimeHeader($partNo, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
     if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
         $p->setTransferEncoding($enc);
     }
     $data = $fetch->getBodyPart($partNo);
     $p->setContents($data);
     $data = $p->getContents();
     $data = iconv($p->getCharset(), 'utf-8//IGNORE', $data);
     return $data;
 }
Example #11
0
 /**
  * Determines the S/MIME type of a part. Uses the smime-type content
  * parameter (if it exists), and falls back to ASN.1 parsing of data if
  * it doesn't exist.
  *
  * @param Horde_Mime_Part $part  MIME part with S/MIME data.
  *
  * @return string  'signed-data', 'enveloped-data', or null.
  */
 protected function _getSmimeType(Horde_Mime_Part $part)
 {
     if ($type = $part->getContentTypeParameter('smime-type')) {
         return strtolower($type);
     }
     if (!class_exists('File_ASN1')) {
         return null;
     }
     $asn1 = new File_ASN1();
     $decoded = $asn1->decodeBER($part->getContents());
     foreach ($decoded as $val) {
         if ($val['type'] == FILE_ASN1_TYPE_SEQUENCE) {
             foreach ($val['content'] as $val2) {
                 if ($val2['type'] == FILE_ASN1_TYPE_OBJECT_IDENTIFIER) {
                     /* ASN.1 values from STD 70/RFC 5652 - CMS syntax */
                     switch ($val2['content']) {
                         case '1.2.840.113549.1.7.2':
                             return 'signed-data';
                         case '1.2.840.113549.1.7.3':
                             return 'enveloped-data';
                         default:
                             // Other types not supported as of now.
                             return null;
                     }
                 }
             }
         }
     }
     return null;
 }
Example #12
0
 /**
  * Adds an attachment from Horde_Mime_Part data.
  *
  * @param Horde_Mime_Part $part  The object that contains the attachment
  *                               data.
  *
  * @return IMP_Compose_Attachment  Attachment object.
  * @throws IMP_Compose_Exception
  */
 public function addAttachmentFromPart($part)
 {
     /* Extract the data from the Horde_Mime_Part. */
     $atc_file = Horde::getTempFile('impatt');
     $stream = $part->getContents(array('stream' => true));
     rewind($stream);
     if (file_put_contents($atc_file, $stream) === false) {
         throw new IMP_Compose_Exception(sprintf(_("Could not attach %s to the message."), $part->getName()));
     }
     return $this->_addAttachment($atc_file, ftell($stream), $part->getName(true), $part->getType());
 }