/** * * @param string $text * * @return array */ protected function buildAddresses($text) { if (!$this->identityParser) { /** * @see Conjoon_Text_Parser_Mail_EmailAddressIdentityParser */ require_once 'Conjoon/Text/Parser/Mail/EmailAddressIdentityParser.php'; $this->identityParser = new \Conjoon_Text_Parser_Mail_EmailAddressIdentityParser(); } $res = $this->identityParser->parse($text); $addresses = array(); foreach ($res as $values) { $addresses[] = array('address' => isset($values[0]) ? $values[0] : '', 'name' => isset($values[1]) ? $values[1] : ''); } return $addresses; }
/** * * @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; }
/** * Tries to guess the mail account which represents the first found email * address found in the supplied string. * * @param string $address A string containing possible recipient addresses * * @return int The account id of the account that matches any address found * in the address string. If not fund, -1 will be returned. */ protected function guessMailAccountForAddress($address) { /** * @see Conjoon_Text_Parser_Mail_EmailAddressIdentityParser */ require_once 'Conjoon/Text/Parser/Mail/EmailAddressIdentityParser.php'; $addressParser = new Conjoon_Text_Parser_Mail_EmailAddressIdentityParser(array('addSlashes' => false)); $orgAddresses = $addressParser->parse($address); $accountServiceHelper = $this->getAccountServiceHelper(); $matchingAccount = null; $matchingAccountId = -1; foreach ($orgAddresses as $singleAddress) { try { $matchingAccount = $accountServiceHelper->getMailAccountForMailAddress($singleAddress[0]); } catch (\Conjoon\Mail\Client\Account\AccountServiceException $ase) { // ignore } if ($matchingAccount) { $matchingAccountId = $matchingAccount->getId(); break; } } return $matchingAccountId; }
/** * Defined by Zend_Filter_Interface * * Expects an array with recipients for an email address. Returns an array * with address/name pairs. * * Input: * [ * "\"Thorsten Suckow-Homberg\" <*****@*****.**>, yo@mtv.com", * "\"Pit Bull\" <*****@*****.**>", * ] * * Returns: * [ * ["*****@*****.**", "Thorsten Suckow-Homberg"], * ["*****@*****.**"], * ["*****@*****.**", "Pit Bull"], * ] * * * @param mixed $value * @return integer */ public function filter($value) { /** * @see Conjoon_Text_Parser_Mail_EmailAddressIdentityParser */ require_once 'Conjoon/Text/Parser/Mail/EmailAddressIdentityParser.php'; $parser = new Conjoon_Text_Parser_Mail_EmailAddressIdentityParser(); $value = (array) $value; $data = array(); for ($i = 0, $len = count($value); $i < $len; $i++) { $data = array_merge($data, $parser->parse($value[$i])); } return $data; }