private function _getParts($structure, $part_number_prefix = '') { if (isset($structure->parts)) { $structure->ctype_primary = strtolower($structure->ctype_primary); $structure->ctype_secondary = strtolower($structure->ctype_secondary); //$part_number=0; foreach ($structure->parts as $part_number => $part) { $part->ctype_primary = strtolower($part->ctype_primary); $part->ctype_secondary = strtolower($part->ctype_secondary); //text part and no attachment so it must be the body if ($structure->ctype_primary == 'multipart' && $structure->ctype_secondary == 'alternative' && $part->ctype_primary == 'text' && $part->ctype_secondary == 'plain') { //check if html part is there if ($this->_hasHtmlPart($structure)) { continue; } } if ($part->ctype_primary == 'text' && ($part->ctype_secondary == 'plain' || $part->ctype_secondary == 'html') && (!isset($part->disposition) || $part->disposition != 'attachment') && empty($part->d_parameters['filename'])) { $charset = isset($part->ctype_parameters['charset']) ? $part->ctype_parameters['charset'] : 'UTF-8'; $body = \GO\Base\Util\String::clean_utf8($part->body, $charset); if (stripos($part->ctype_secondary, 'plain') !== false) { $body = nl2br($body); } else { $body = \GO\Base\Util\String::convertLinks($body); $body = \GO\Base\Util\String::sanitizeHtml($body); $body = $body; } $this->_loadedBody .= $body; } elseif ($part->ctype_primary == 'multipart') { } else { //attachment if (!empty($part->ctype_parameters['name'])) { $filename = $part->ctype_parameters['name']; } elseif (!empty($part->d_parameters['filename'])) { $filename = $part->d_parameters['filename']; } elseif (!empty($part->d_parameters['filename*'])) { $filename = $part->d_parameters['filename*']; } else { $filename = uniqid(time()); } $mime_type = $part->ctype_primary . '/' . $part->ctype_secondary; if (isset($part->headers['content-id'])) { $content_id = trim($part->headers['content-id']); if (strpos($content_id, '>')) { $content_id = substr($part->headers['content-id'], 1, strlen($part->headers['content-id']) - 2); } } else { $content_id = ''; } $f = new \GO\Base\Fs\File($filename); $a = new MessageAttachment(); $a->name = $filename; $a->number = $part_number_prefix . $part_number; $a->content_id = $content_id; $a->mime = $mime_type; $tmp_file = new \GO\Base\Fs\File($this->_getTempDir() . $filename); if (!empty($part->body)) { $tmp_file = new \GO\Base\Fs\File($this->_getTempDir() . $filename); if (!$tmp_file->exists()) { $tmp_file->putContents($part->body); } $a->setTempFile($tmp_file); } $a->index = count($this->attachments); $a->size = isset($part->body) ? strlen($part->body) : 0; $a->encoding = isset($part->headers['content-transfer-encoding']) ? $part->headers['content-transfer-encoding'] : ''; $a->disposition = isset($part->disposition) ? $part->disposition : ''; $this->addAttachment($a); } //$part_number++; if (isset($part->parts)) { $this->_getParts($part, $part_number_prefix . $part_number . '.'); } } } elseif (isset($structure->body)) { $charset = isset($structure->ctype_parameters['charset']) ? $structure->ctype_parameters['charset'] : 'UTF-8'; $text_part = \GO\Base\Util\String::clean_utf8($structure->body, $charset); //convert text to html if (stripos($structure->ctype_secondary, 'plain') !== false) { $this->extractUuencodedAttachments($text_part); $text_part = nl2br($text_part); } else { $text_part = \GO\Base\Util\String::convertLinks($text_part); $text_part = \GO\Base\Util\String::sanitizeHtml($text_part); } $this->_loadedBody .= $text_part; } }
public function getHtmlBody($asText = false, $noMaxBodySize = false) { if (!isset($this->_htmlBody)) { $imap = $this->getImapConnection(); $this->_loadBodyParts(); $this->_htmlBody = ''; if ($this->_htmlParts['text_found']) { //check if we found a html body //\GO::debug($this->_htmlParts); foreach ($this->_htmlParts['parts'] as $htmlPart) { if ($htmlPart['type'] == 'text') { if (!empty($this->_htmlBody)) { $this->_htmlBody .= '<br />'; } $maxBodySize = $noMaxBodySize ? false : $this->maxBodySize; $htmlPartStr = $imap->get_message_part_decoded($this->uid, $htmlPart['number'], $htmlPart['encoding'], $htmlPart['charset'], $this->peek, false); $htmlPartStr = \GO\Base\Util\String::convertLinks($htmlPartStr); $htmlPartStr = \GO\Base\Util\String::sanitizeHtml($htmlPartStr); $this->_bodyTruncated = $imap->max_read; $this->_htmlBody .= $htmlPartStr; } else { $attachment = $this->getAttachment($htmlPart['number']); if ($attachment) { $attachment->content_id = 'go-autogen-' . $htmlPart['number']; $this->_htmlBody .= '<img alt="' . $htmlPart['name'] . '" src="cid:' . $attachment->content_id . '" style="display:block;margin:10px 0;" />'; } } // else // { // \GO::debug("Missing from attachments: ".$htmlPart['number']); // } } //$this->_htmlBody = \GO\Base\Util\String::sanitizeHtml($this->_htmlBody); } if (empty($this->_htmlBody) && !$asText) { $this->_htmlBody = $this->getPlainBody(true, $noMaxBodySize); } } else { // $this->_setSeen(); } if ($asText) { $htmlToText = new \GO\Base\Util\Html2Text($this->_htmlBody); return $htmlToText->get_text(); } return $this->_htmlBody; }
/** * Returns MIME fields contained in this class's instance as an associative * array. * * @param boolean $html Whether or not to return the HTML body. The alternative is * plain text. Defaults to true. * * @return Array */ public function toOutputArray($html = true, $recipientsAsString = false, $noMaxBodySize = false, $useHtmlSpecialChars = true) { $from = $this->from->getAddresses(); $response['notification'] = $this->disposition_notification_to; //seen is expensive because it can't be recovered from cache. // We'll use the grid to check if a message was seen or not. //$response['seen']=$this->seen; $from = $this->from->getAddress(); $response['seen'] = $this->seen; $response['forwarded'] = $this->forwarded; $response['flagged'] = $this->flagged; $response['answered'] = $this->answered; $response['from'] = $from['personal']; $response['sender'] = $from['email']; $response['to'] = $recipientsAsString ? (string) $this->to : $this->_convertRecipientArray($this->to->getAddresses()); $response['cc'] = $recipientsAsString ? (string) $this->cc : $this->_convertRecipientArray($this->cc->getAddresses()); $response['bcc'] = $recipientsAsString ? (string) $this->bcc : $this->_convertRecipientArray($this->bcc->getAddresses()); $response['reply_to'] = (string) $this->reply_to; $response['message_id'] = $this->message_id; $response['date'] = $this->date; $response['to_string'] = (string) $this->to; if (!$recipientsAsString && empty($response['to'])) { $response['to'][] = array('email' => '', 'personal' => \GO::t('no_recipients', 'email')); } $response['full_from'] = (string) $this->from; $response['priority'] = intval($this->x_priority); $response['udate'] = $this->udate; $response['date'] = \GO\Base\Util\Date::get_timestamp($this->udate); $response['size'] = $this->size; $labels = array(); if (property_exists($this, 'account')) { $labels = \GO\Email\Model\Label::model()->getAccountLabels($this->account->id); } $response['labels'] = array(); if (!empty($this->labels)) { foreach ($this->labels as $label) { if (isset($labels[$label])) { $response['labels'][] = array('name' => $labels[$label]->name, 'color' => $labels[$label]->color); } } } $response['attachments'] = array(); $response['zip_of_attachments_url'] = $this->getZipOfAttachmentsUrl(); $response['inlineAttachments'] = array(); if ($html) { $response['htmlbody'] = $this->getHtmlBody(false, $noMaxBodySize); } else { $response['plainbody'] = $this->getPlainBody(false, $noMaxBodySize); } if ($useHtmlSpecialChars) { $response['subject'] = htmlspecialchars($this->subject, ENT_COMPAT, 'UTF-8'); } else { $response['subject'] = $this->subject; } $response['body_truncated'] = $this->bodyIsTruncated(); $response['smime_signed'] = isset($this->content_type_attributes['smime-type']) && $this->content_type_attributes['smime-type'] == 'signed-data'; $attachments = $this->getAttachments(); foreach ($attachments as $att) { if ($html && $att->disposition != 'attachment') { if ($att->mime == 'text/html') { $htmlPartStr = $att->getData(); $htmlPartStr = \GO\Base\Util\String::convertLinks($htmlPartStr); $htmlPartStr = \GO\Base\Util\String::sanitizeHtml($htmlPartStr); $response['htmlbody'] .= '<hr />' . $htmlPartStr; continue; } else { if ($att->mime == 'text/plain') { $htmlPartStr = $att->getData(); $htmlPartStr = \GO\Base\Util\String::text_to_html($htmlPartStr); $response['htmlbody'] .= '<hr />' . $htmlPartStr; continue; } } } $replaceCount = 0; $a = $att->getAttributes(); //add unique token for detecting precense of inline attachment when we submit the message in handleFormInput $a['token'] = md5($a['tmp_file']); $a['url'] .= '&token=' . $a['token']; if ($html && !empty($a['content_id'])) { $response['htmlbody'] = str_replace('cid:' . $a['content_id'], $a['url'], $response['htmlbody'], $replaceCount); } if ($a['name'] == 'smime.p7s') { $response['smime_signed'] = true; continue; } if (!$replaceCount) { $response['attachments'][] = $a; } else { $response['inlineAttachments'][] = $a; } } $response['contact_name'] = ""; $response['contact_thumb_url'] = GO::config()->host . 'modules/addressbook/themes/Default/images/unknown-person.png'; $response['blocked_images'] = 0; $response['xssDetected'] = false; $this->fireEvent('tooutputarray', array(&$response, $this)); return $response; }