/**
  * appends old body to mime part
  * 
  * @param Zend_Mime_Part $mp
  * @param string $replyBody plain/text reply body
  * @return Zend_Mime_Part
  */
 protected static function _appendReplyBody(Zend_Mime_Part $mp, $replyBody)
 {
     $decodedContent = Tinebase_Mail::getDecodedContent($mp, NULL, FALSE);
     $type = $mp->type;
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " mp content: " . $decodedContent);
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " reply body: " . $replyBody);
     }
     if ($type === Zend_Mime::TYPE_HTML && $replyBody === strip_tags($replyBody)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Converting plain/text reply body to HTML");
         }
         $replyBody = self::convertFromTextToHTML($replyBody);
     }
     if ($type === Zend_Mime::TYPE_HTML && preg_match('/(<\\/body>[\\s\\r\\n]*<\\/html>)/i', $decodedContent, $matches)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Appending reply body to html body.');
         }
         $decodedContent = str_replace($matches[1], $replyBody . $matches[1], $decodedContent);
     } else {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Appending reply body to mime text part.");
         }
         $decodedContent .= $replyBody;
     }
     $mp = new Zend_Mime_Part($decodedContent);
     $mp->charset = 'utf-8';
     $mp->type = $type;
     return $mp;
 }
 /**
  * get and decode message body
  * 
  * @param Felamimail_Model_Message $_message
  * @param string $_partId
  * @param string $_contentType
  * @param Felamimail_Model_Account $_account
  * @return string
  * 
  * @todo multipart_related messages should deliver inline images
  */
 protected function _getAndDecodeMessageBody(Felamimail_Model_Message $_message, $_partId, $_contentType, $_account = NULL)
 {
     $structure = $_message->getPartStructure($_partId);
     if (empty($structure)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
             Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Empty structure, could not find body parts of message ' . $_message->subject);
         }
         return '';
     }
     $bodyParts = $_message->getBodyParts($structure, $_contentType);
     if (empty($bodyParts)) {
         if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
             Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Could not find body parts of message ' . $_message->subject);
         }
         return '';
     }
     $messageBody = '';
     foreach ($bodyParts as $partId => $partStructure) {
         $bodyPart = $this->getMessagePart($_message, $partId, TRUE, $partStructure);
         $body = Tinebase_Mail::getDecodedContent($bodyPart, $partStructure);
         if ($partStructure['contentType'] != Zend_Mime::TYPE_TEXT) {
             $bodyCharCountBefore = strlen($body);
             $body = $this->_purifyBodyContent($body, $_message->getId());
             $bodyCharCountAfter = strlen($body);
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Purifying removed ' . ($bodyCharCountBefore - $bodyCharCountAfter) . ' / ' . $bodyCharCountBefore . ' characters.');
             }
             if ($_message->text_partid && $bodyCharCountAfter < $bodyCharCountBefore / 10) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                     Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Purify may have removed (more than 9/10) too many chars, using alternative text message part.');
                 }
                 $result = $this->_getAndDecodeMessageBody($_message, $_message->text_partid, Zend_Mime::TYPE_TEXT, $_account);
                 return Felamimail_Message::convertContentType(Zend_Mime::TYPE_TEXT, Zend_Mime::TYPE_HTML, $result);
             }
         }
         if (!($_account !== NULL && $_account->display_format === Felamimail_Model_Account::DISPLAY_CONTENT_TYPE && $bodyPart->type == Zend_Mime::TYPE_TEXT)) {
             $body = Felamimail_Message::convertContentType($partStructure['contentType'], $_contentType, $body);
             if ($bodyPart->type == Zend_Mime::TYPE_TEXT && $_contentType == Zend_Mime::TYPE_HTML) {
                 $body = Felamimail_Message::replaceUris($body);
                 $body = Felamimail_Message::replaceEmails($body);
             }
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Do not convert ' . $bodyPart->type . ' part to ' . $_contentType);
             }
         }
         $body = Felamimail_Message::replaceTargets($body);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Adding part ' . $partId . ' to message body.');
         }
         $messageBody .= Tinebase_Core::filterInputForDatabase($body);
     }
     return $messageBody;
 }