/**
  * the singleton pattern
  *
  * @return Expressomail_Controller_ActiveSync
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Expressomail_Controller_ActiveSync();
     }
     return self::$_instance;
 }
 /**
  * assert if processed mimes equals expected results
  */
 public function testMimesAndResults()
 {
     $controller = Expressomail_Controller_ActiveSync::getInstance();
     foreach ($this->mimes as $filename => $content) {
         $expected = $this->results[$filename];
         $onlyInline = strpos($filename, 'REPLY') !== FALSE ? TRUE : FALSE;
         $generated = $controller->getHtmlBodyAndAttachmentData($content, $onlyInline);
         $message = 'Expected:' . PHP_EOL . PHP_EOL . print_r($expected, TRUE) . PHP_EOL . PHP_EOL . 'Generated:' . PHP_EOL . PHP_EOL . print_r($generated, TRUE) . PHP_EOL . PHP_EOL . 'on file: ' . $filename;
         $this->assertEquals($expected, $generated, $message);
     }
 }
 /**
  * send email from smart reply or smart forward resquest
  *
  * @param array $source
  * @param string $inputStream
  * @param bool $saveInSent
  * @param bool $replaceMime
  * @param string $flag
  */
 public function sendReplyForward($source, $inputStream, $saveInSent, $replaceMime, $flag)
 {
     $controller = Expressomail_Controller_ActiveSync::getInstance();
     if (is_resource($inputStream)) {
         $inputStream = stream_get_contents($inputStream);
     }
     $cleanIncoming = preg_replace('/(\\r\\n|\\r|\\n)/s', PHP_EOL, $inputStream);
     $incomingDecodedMime = $controller->getHtmlBodyAndAttachmentData($cleanIncoming);
     $decodedBody = '<div>' . $incomingDecodedMime['body'] . '</div>';
     $messageId = $this->getMessageIdFromSource($source);
     if (!is_resource($inputStream)) {
         $stream = fopen("php://temp", 'r+');
         fwrite($stream, $inputStream);
         $inputStream = $stream;
         rewind($inputStream);
     }
     if ($this->_debugEmail == true) {
         $debugStream = fopen("php://temp", 'r+');
         stream_copy_to_stream($inputStream, $debugStream);
         rewind($debugStream);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " email to send:" . stream_get_contents($debugStream));
         }
         //replace original stream with debug stream, as php://input can't be rewinded
         $inputStream = $debugStream;
         rewind($inputStream);
     }
     $incomingMessage = new Zend_Mail_Message(array('file' => $inputStream));
     $defaultAccountId = Tinebase_Core::getPreference('Expressomail')->{Expressomail_Preference::DEFAULTACCOUNT};
     try {
         $account = Expressomail_Controller_Account::getInstance()->get($defaultAccountId);
     } catch (Tinebase_Exception_NotFound $ten) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " no email account configured");
         throw new Syncroton_Exception('no email account configured');
     }
     if (empty(Tinebase_Core::getUser()->accountEmailAddress)) {
         throw new Syncroton_Exception('no email address set for current user');
     }
     $fmailMessage = Expressomail_Controller_Message::getInstance()->get($messageId);
     $fmailMessage->flags = $flag;
     $mail = Tinebase_Mail::createFromZMM($incomingMessage);
     $rfc822 = Expressomail_Controller_Message::getInstance()->getMessagePart($fmailMessage);
     $originalDecodedMime = $controller->getHtmlBodyAndAttachmentData($rfc822->getContent(), $flag != Zend_Mail_Storage::FLAG_PASSED);
     $decodedBody = $decodedBody . '<div><br></div><div>' . $originalDecodedMime['body'] . '</div>';
     $mail->setBodyText('');
     $mail->setBodyHtml($decodedBody);
     if (isset($incomingDecodedMime['attachmentData']) && count($incomingDecodedMime['attachmentData']) > 0) {
         $controller->addAttachments($mail, $incomingDecodedMime['attachmentData']);
     }
     if (isset($originalDecodedMime['attachmentData']) && count($originalDecodedMime['attachmentData']) > 0) {
         $controller->addAttachments($mail, $originalDecodedMime['attachmentData']);
     }
     Expressomail_Controller_Message_Send::getInstance()->sendZendMail($account, $mail, (bool) $saveInSent, $fmailMessage);
 }