/**
  * Defined by Zend_Filter_Interface
  *
  * Returns a ivonv_mime_decoded_header value.
  *
  * @param  mixed $value
  * @return integer
  *
  * @deprecated use Conjoon_Text_Transformer_MimeDecoder
  */
 public function filter($value)
 {
     /**
      * @see Conjoon_Text_Transformer_MimeDecoder
      */
     require_once 'Conjoon/Text/Transformer/MimeDecoder.php';
     $transformer = new Conjoon_Text_Transformer_MimeDecoder();
     return $transformer->transform($value);
 }
 private function _parseAttachments($part, &$emailItem)
 {
     $encodingInformation = $this->_getEncodingInformation($part);
     $fileName = $encodingInformation['name'];
     $contentType = $encodingInformation['contentType'];
     $contentTransferEncoding = $encodingInformation['contentTransferEncoding'];
     if ($contentType == 'message/rfc822' || $contentType == 'rfc822') {
         try {
             $nm = new \Conjoon_Mail_Message(array('raw' => $part->getContent()));
             $n = $nm->subject;
             /**
              * @see \Conjoon_Text_Transformer_MimeDecoder
              */
             require_once 'Conjoon/Text/Transformer/MimeDecoder.php';
             $transformer = new \Conjoon_Text_Transformer_MimeDecoder();
             $fileName = $transformer->transform($n) . '.eml';
         } catch (\Zend_Mail_Exception $e) {
             // ignore
         }
     }
     if ($fileName === "") {
         $fileName = 'attachment[' . $this->_attachmentCounter++ . ']';
     }
     try {
         $contentId = $part->contentId;
     } catch (\Zend_Mail_Exception $e) {
         $contentId = "";
     }
     $emailItem['attachments'][] = array('mimeType' => $contentType, 'encoding' => $contentTransferEncoding, 'content' => $part->getContent(), 'fileName' => $fileName, 'contentId' => $contentId);
 }
 /**
  * Parses the header into an array.
  *
  * @param string $header The raw header
  *
  * @return array
  *
  * @throws MailEntityCreatorException
  */
 protected function parseHeader($header)
 {
     try {
         /**
          * @see \Conjoon_Text_Parser_Mail_MessageHeaderParser
          */
         require_once 'Conjoon/Text/Parser/Mail/MessageHeaderParser.php';
         $parser = new \Conjoon_Text_Parser_Mail_MessageHeaderParser();
         $header = $parser->parse($header);
         /**
          * @see \Conjoon_Text_Transformer_DateStringSanitizer
          */
         require_once 'Conjoon/Text/Transformer/DateStringSanitizer.php';
         $sanitizeDateTransformer = new \Conjoon_Text_Transformer_DateStringSanitizer();
         /**
          * @see \Conjoon_Date_Format
          */
         require_once 'Conjoon/Date/Format.php';
         $header['date'] = new \DateTime(\Conjoon_Date_Format::toUtc($sanitizeDateTransformer->transform($header['date'])), new \DateTimeZone('UTC'));
         /**
          * @see Conjoon_Text_Transformer_MimeDecoder
          */
         require_once 'Conjoon/Text/Transformer/MimeDecoder.php';
         $mimeDecoder = new \Conjoon_Text_Transformer_MimeDecoder();
         $header['subject'] = $mimeDecoder->transform($header['subject']);
         $header['to'] = isset($header['to']) ? $header['to'] : "";
         $header['cc'] = isset($header['cc']) ? $header['cc'] : "";
         $header['bcc'] = isset($header['bcc']) ? $header['bcc'] : "";
         $header['from'] = isset($header['from']) ? $header['from'] : "";
         $header['replyTo'] = isset($header['replyTo']) ? $header['replyTo'] : "";
         $header['inReplyTo'] = isset($header['inReplyTo']) ? $header['inReplyTo'] : "";
         $header['references'] = isset($header['references']) ? $header['references'] : "";
         return $header;
     } catch (\Exception $e) {
         throw new MailEntityCreatorException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
     }
 }
 /**
  *
  * @param Conjoon_Modules_Groupware_Email_Account_Dto $accountDto
  * @param array $pathInfo the pathInfo parts for the remote folder,
  * which have to be assembled again using the remote storage's delimiter
  * @param array $sortInfo
  *
  * @return array
  *
  * @throws Conjoon_Argument_Exception
  */
 protected function _getEmailItemListForAccountAndRemoteFolder(Conjoon_Modules_Groupware_Email_Account_Dto $accountDto, $pathInfo, array $sortInfo = array(), $additionalInfo, $from, $to, $userId)
 {
     if (is_array($pathInfo)) {
         $path = $pathInfo['path'];
         $rootId = $pathInfo['rootId'];
         $globalName = $this->_getFolderFacade()->getAssembledGlobalNameForAccountAndPath($accountDto, $path);
     } else {
         $globalName = $pathInfo;
         $fld = $this->_getFolderFacade()->getRootFolderForAccountId($accountDto, $userId);
         $rootId = $fld[0]->id;
         /**
          * @see Conjoon_Modules_Groupware_Email_ImapHelper
          */
         require_once 'Conjoon/Modules/Groupware/Email/ImapHelper.php';
         $path = Conjoon_Modules_Groupware_Email_ImapHelper::splitFolderForImapAccount($globalName, $accountDto);
     }
     /**
      * @see Conjoon_Date_Format
      */
     require_once 'Conjoon/Date/Format.php';
     /**
      * @see Conjoon_Modules_Groupware_Email_ImapHelper
      */
     require_once 'Conjoon/Modules/Groupware/Email/ImapHelper.php';
     $protocol = Conjoon_Modules_Groupware_Email_ImapHelper::reuseImapProtocolForAccount($accountDto);
     /**
      * @see Zend_Mail_Storage_Imap
      */
     require_once 'Conjoon/Mail/Storage/Imap.php';
     $storage = new Conjoon_Mail_Storage_Imap($protocol);
     $messageStruct = $storage->getHeaderListAndMetaInformationForGlobalName($globalName, $from, $to);
     /**
      * @see Conjoon_Text_Parser_Mail_MessageHeaderParser
      */
     require_once 'Conjoon/Text/Parser/Mail/MessageHeaderParser.php';
     $parser = new Conjoon_Text_Parser_Mail_MessageHeaderParser();
     /**
      * @see Conjoon_Text_Transformer_DateStringSanitizer
      */
     require_once 'Conjoon/Text/Transformer/DateStringSanitizer.php';
     $sanitizeDateTransformer = new Conjoon_Text_Transformer_DateStringSanitizer();
     /**
      * @see Conjoon_Text_Transformer_MimeDecoder
      */
     require_once 'Conjoon/Text/Transformer/MimeDecoder.php';
     $mimeDecoder = new Conjoon_Text_Transformer_MimeDecoder();
     /**
      * @see Conjoon_Text_Parser_Mail_EmailAddressIdentityParser
      */
     require_once 'Conjoon/Text/Parser/Mail/EmailAddressIdentityParser.php';
     $identityParser = new Conjoon_Text_Parser_Mail_EmailAddressIdentityParser();
     $identityParserSender = new Conjoon_Text_Parser_Mail_EmailAddressIdentityParser(array('addSlashes' => false, 'useQuoting' => false));
     /**
      * @see Conjoon_Text_Transformer_Mail_EmailAddressNameQuoteTransformer
      */
     require_once 'Conjoon/Text/Transformer/Mail/EmailAddressNameQuoteTransformer.php';
     $quoteTransformer = new Conjoon_Text_Transformer_Mail_EmailAddressNameQuoteTransformer();
     $parsedHeaders = array();
     for ($i = 0, $len = count($messageStruct); $i < $len; $i++) {
         try {
             $header = $parser->parse($messageStruct[$i]['header']);
         } catch (Exception $e) {
             continue;
         }
         $header['date'] = Conjoon_Date_Format::toUtc($sanitizeDateTransformer->transform($header['date']));
         // no mime decode needed, already done in EmailHeaderParser
         $header['subject'] = $mimeDecoder->transform($header['subject']);
         // RECIPIENTS IS NOT PART OF THE HEADER.
         $recipients = array_merge(isset($header['to']) ? $identityParser->parse($header['to']) : array(), isset($header['cc']) ? $identityParser->parse($header['cc']) : array(), isset($header['bcc']) ? $identityParser->parse($header['bcc']) : array());
         $recipientNames = array();
         foreach ($recipients as $address => $addressValue) {
             if (isset($addressValue[1])) {
                 $recipientNames[] = $quoteTransformer->transform($addressValue[1]);
             } else {
                 $recipientNames[] = $addressValue[0];
             }
         }
         $header['recipients'] = $recipientNames;
         // SENDER IS NOT PART OF THE HEADER.
         $sender = $identityParserSender->parse($header['from']);
         $header['sender'] = isset($sender[0][1]) ? $sender[0][1] : $sender[0][0];
         // the uid
         $header['uid'] = $messageStruct[$i]['uid'];
         $header['isAttachment'] = false;
         $header['isRead'] = false;
         $header['isSpam'] = false;
         $header['referencedAsType'] = array();
         // flag processing
         $flags =& $messageStruct[$i]['flags'];
         for ($u = 0, $lenu = count($flags); $u < $lenu; $u++) {
             if ($flags[$u] == '\\Seen') {
                 $header['isRead'] = true;
             } else {
                 if ($flags[$u] == '$Junk') {
                     $header['isSpam'] = true;
                 } else {
                     if (stripos($flags[$u], 'forwarded') !== false) {
                         array_push($header['referencedAsType'], 'forwarded');
                     } else {
                         if ($flags[$u] == '\\Answered') {
                             array_push($header['referencedAsType'], 'reply');
                         }
                     }
                 }
             }
         }
         // possible attachment?
         $header['isAttachment'] = $this->lookUpImapAttachmentInBodyStructure($messageStruct[$i]['bodystructure']);
         $parsedHeaders[] = $header;
     }
     // PARSED HEADERS IS THE INPUT AS WE NEED IT FOR STORING INTO THE DB
     // NOW GENERATE THE OUTPUT AS CONJOON EXPECTS IT!
     /**
      * @see Conjoon_Date_Format
      */
     require_once 'Conjoon/Date/Format.php';
     /**
      * @see Zend_Filter_HtmlEntities
      */
     require_once 'Zend/Filter/HtmlEntities.php';
     $htmlEntitiesFilter = new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_COMPAT));
     $responseItems = array();
     for ($i = 0, $len = count($parsedHeaders); $i < $len; $i++) {
         $header =& $parsedHeaders[$i];
         $responseItems[] = array('id' => $header['uid'], 'recipients' => $header['recipients'], 'sender' => $header['sender'], 'subject' => $htmlEntitiesFilter->filter($header['subject']), 'date' => Conjoon_Date_Format::utcToLocal($header['date']), 'isRead' => (int) $header['isRead'], 'isAttachment' => (int) $header['isAttachment'], 'isSpam' => (int) $header['isSpam'], 'isDraft' => 0, 'isOutboxPending' => 0, 'referencedAsTypes' => $header['referencedAsType'], 'groupwareEmailFoldersId' => -1, 'path' => array_merge(array($rootId), $path));
     }
     // sort
     if (isset($sortInfo['sort'])) {
         switch ($sortInfo['sort']) {
             case 'date':
                 usort($responseItems, array($this, 'memorySortDate'));
                 break;
             case 'subject':
                 usort($responseItems, array($this, 'memorySortSubject'));
                 break;
             case 'recipients':
                 usort($responseItems, array($this, 'memorySortRecipients'));
                 break;
             case 'sender':
                 usort($responseItems, array($this, 'memorySortSender'));
                 break;
             case 'is_spam':
                 usort($responseItems, array($this, 'memorySortIsSpam'));
                 break;
             case 'is_read':
                 usort($responseItems, array($this, 'memorySortIsRead'));
                 break;
             case 'is_attachment':
                 usort($responseItems, array($this, 'memorySortisAttachment'));
                 break;
         }
         if ($sortInfo['dir'] == 'DESC') {
             $responseItems = array_reverse($responseItems);
         }
     }
     $totalCount = count($responseItems);
     if (isset($sortInfo['limit']) && isset($sortInfo['start'])) {
         $responseItems = array_splice($responseItems, $sortInfo['start'], $sortInfo['limit']);
     }
     if ($additionalInfo) {
         return array('items' => $responseItems, 'totalCount' => $totalCount, 'pendingItems' => $this->getPendingCountForGlobalName($accountDto, $globalName));
     }
     return $responseItems;
 }