/**
  * get message content (body, headers and attachments)
  *
  * @param Expressomail_Model_Message $_message
  * @param Expressomail_Model_Account $_account
  * @param string $_partId
  */
 protected function _getCompleteMessageContent(Expressomail_Model_Message $_message, Expressomail_Model_Account $_account, $_partId = NULL)
 {
     $mimeType = $_account->display_format == Expressomail_Model_Account::DISPLAY_HTML || $_account->display_format == Expressomail_Model_Account::DISPLAY_CONTENT_TYPE ? Zend_Mime::TYPE_HTML : Zend_Mime::TYPE_TEXT;
     $headers = $this->getMessageHeaders($_message, $_partId, true);
     $attachments = $this->getAttachments($_message, $_partId);
     $this->_attachments = $attachments;
     $body = $this->getMessageBody($_message, $_partId, $mimeType, $_account, true);
     $signature = $this->getDigitalSignature($_message, $_partId);
     if ($signature['ret_type'] == 'cipher') {
         $smimeEml = $signature['content'];
         unset($signature);
     } else {
         $smimeEml = '';
     }
     if ($_partId === null) {
         $message = $_message;
         $message->body = $body;
         $message->headers = $headers;
         $message->attachments = $attachments;
         // make sure the structure is present
         $message->structure = $message->structure;
         $message->signature_info = $signature;
         $message->smimeEml = $smimeEml;
     } else {
         // create new object for rfc822 message
         $structure = $_message->getPartStructure($_partId, FALSE);
         $message = new Expressomail_Model_Message(array('messageuid' => $_message->messageuid, 'folder_id' => $_message->folder_id, 'received' => $_message->received, 'size' => array_key_exists('size', $structure) ? $structure['size'] : 0, 'partid' => $_partId, 'body' => $body, 'headers' => $headers, 'attachments' => $attachments, 'signature_info' => $signature, 'smimeEml' => $smimeEml, 'structure' => $structure));
         $message->parseHeaders($headers);
         $message->parseSmime($message->structure);
         $structure = array_key_exists('messageStructure', $structure) ? $structure['messageStructure'] : $structure;
         $message->parseStructure($structure);
     }
     if (!$_partId) {
         $message->sendReadingConfirmation();
     }
     if (isset($message['signature_info']) && $message['signature_info']) {
         if ($message['signature_info']['ret_type'] == 'signature') {
             $signature['smime'] = Expressomail_Smime::TYPE_ENVELOPED_DATA_VALUE;
             $extract1_headers = 'application/pkcs7';
             $extract1a_headers = 'name="smime.p7m"';
             $extract1b_headers = 'MIME-Version: 1.0';
             $extract2_headers = $message['headers']['content-disposition'];
             $extract3_headers = $message['headers']['content-transfer-encoding'];
             $imapBackend = $this->_getBackendAndSelectFolder($_message->folder_id);
             $contentHeaders = preg_replace("/(?<!\\r)\\n/", "\r\n", $imapBackend->getRawContent($_message->messageuid, 'HEADER'));
             $header_lines = explode(chr(0xd) . chr(0xa), $contentHeaders);
             $newHeaders = '';
             foreach ($header_lines as $line) {
                 if (strpos($line, $extract1_headers) === false && strpos($line, $extract1a_headers) === false && strpos($line, $extract1b_headers) === false && strpos($line, $extract2_headers) === false && strpos($line, $extract3_headers) === false) {
                     $newHeaders = $newHeaders . $line . chr(0xd) . chr(0xa);
                 }
             }
             $newHeaders = substr($newHeaders, 0, -4) . 'MIME-Version: 1.0' . chr(0xd) . chr(0xa);
             $msg = $newHeaders . $message['signature_info']['content'];
             $msg = preg_replace("/(?<!\\r)\\n/", "\r\n", $msg);
             $account = Expressomail_Controller_Account::getInstance()->get($message->account_id);
             $folder = Expressomail_Controller_Folder::getInstance()->getByBackendAndGlobalName($message->account_id, $account['trash_folder']);
             $_messageUid = Expressomail_Controller_Message::getInstance()->appendMessage($folder, $msg);
             $_messageid = Expressomail_Backend_Message::createMessageId($account['id'], $folder['id'], $_messageUid);
             $messageB = Expressomail_Controller_Message::getInstance()->getCompleteMessage($_messageid, Null, false);
             $messageB['signature_info'] = $message['signature_info'];
             return $messageB;
         }
     }
     return $message;
 }
 /**
  * create new message for the cache
  *
  * @param array $_message
  * @param Expressomail_Model_Folder $_folder
  * @return Expressomail_Model_Message
  */
 protected function _createModelMessage(array $_message, Expressomail_Model_Folder $_folder = NULL)
 {
     // Optimization!!!
     if ($_folder == NULL) {
         if (isset($_message['folder_id'])) {
             $_folder = array_key_exists($_message['folder_id'], $this->_folderMap) ? $this->_folderMap[$_message['folder_id']] : ($this->_folderMap[$_message['folder_id']] = Expressomail_Controller_Folder::getInstance()->get($_message['folder_id']));
         } else {
             return NULL;
         }
     }
     $message = new Expressomail_Model_Message(array('id' => self::createMessageId($_folder->account_id, $_folder->getId(), $_message['uid']), 'account_id' => $_folder->account_id, 'messageuid' => $_message['uid'], 'folder_id' => $_folder->getId(), 'timestamp' => Tinebase_DateTime::now(), 'received' => Expressomail_Message::convertDate($_message['received']), 'size' => $_message['size'], 'flags' => $_message['flags']), true);
     $message->parseStructure($_message['structure']);
     $message->parseHeaders($_message['header']);
     $message->fixToListModel();
     $message->parseBodyParts();
     $message->parseSmime($_message['structure']);
     $attachments = Expressomail_Controller_Message::getInstance()->getAttachments($message);
     $message->has_attachment = count($attachments) > 0 ? true : false;
     if ($message->content_type === Expressomail_Model_Message::CONTENT_TYPE_MULTIPART_REPORT) {
         $bodyParts = $message->getBodyParts($message->structure);
         foreach ($bodyParts as $partId => $partStructure) {
             if ($partStructure['contentType'] === Expressomail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822) {
                 $translate = Tinebase_Translation::getTranslation('Expressomail');
                 $partHeaders = Expressomail_Controller_Message::getInstance()->getMessageHeaders($message, $partId, true);
                 $message->subject = $partHeaders['subject'] . ' - ' . $translate->_($message->subject);
                 $message->from_name = $translate->_($message->from_name);
             }
         }
     }
     return $message;
 }