/** * @param bool $bSort = false * @param string $sSearchCriterias = 'ALL' * @param array $aSearchOrSortReturn = null * @param bool $bReturnUid = true * @param string $sLimit = '' * @param string $sCharset = '' * @param array $aSortTypes = null * * @return array * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ private function simpleESearchOrESortHelper($bSort = false, $sSearchCriterias = 'ALL', $aSearchOrSortReturn = null, $bReturnUid = true, $sLimit = '', $sCharset = '', $aSortTypes = null) { $sCommandPrefix = $bReturnUid ? 'UID ' : ''; $sSearchCriterias = 0 === \strlen($sSearchCriterias) || '*' === $sSearchCriterias ? 'ALL' : $sSearchCriterias; $sCmd = $bSort ? 'SORT' : 'SEARCH'; if ($bSort && (!\is_array($aSortTypes) || 0 === \count($aSortTypes) || !$this->IsSupported('SORT'))) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } if (!$this->IsSupported($bSort ? 'ESORT' : 'ESEARCH')) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } if (!\is_array($aSearchOrSortReturn) || 0 === \count($aSearchOrSortReturn)) { $aSearchOrSortReturn = array('ALL'); } $aRequest = array(); if ($bSort) { $aRequest[] = 'RETURN'; $aRequest[] = $aSearchOrSortReturn; $aRequest[] = $aSortTypes; $aRequest[] = \MailSo\Base\Utils::IsAscii($sSearchCriterias) ? 'US-ASCII' : 'UTF-8'; } else { if (0 < \strlen($sCharset)) { $aRequest[] = 'CHARSET'; $aRequest[] = \strtoupper($sCharset); } $aRequest[] = 'RETURN'; $aRequest[] = $aSearchOrSortReturn; } $aRequest[] = $sSearchCriterias; if (0 < \strlen($sLimit)) { $aRequest[] = $sLimit; } $this->SendRequest($sCommandPrefix . $sCmd, $aRequest); $sRequestTag = $this->getCurrentTag(); $aResult = array(); $aResponse = $this->parseResponseWithValidation(); if (\is_array($aResponse)) { $oImapResponse = null; foreach ($aResponse as $oImapResponse) { if (\MailSo\Imap\Enumerations\ResponseType::UNTAGGED === $oImapResponse->ResponseType && ('ESEARCH' === $oImapResponse->StatusOrIndex || 'ESORT' === $oImapResponse->StatusOrIndex) && \is_array($oImapResponse->ResponseList) && isset($oImapResponse->ResponseList[2], $oImapResponse->ResponseList[2][0], $oImapResponse->ResponseList[2][1]) && 'TAG' === $oImapResponse->ResponseList[2][0] && $sRequestTag === $oImapResponse->ResponseList[2][1] && (!$bReturnUid || $bReturnUid && !empty($oImapResponse->ResponseList[3]) && 'UID' === $oImapResponse->ResponseList[3])) { $iStart = 3; foreach ($oImapResponse->ResponseList as $iIndex => $mItem) { if ($iIndex >= $iStart) { switch ($mItem) { case 'ALL': case 'MAX': case 'MIN': case 'COUNT': if (isset($oImapResponse->ResponseList[$iIndex + 1])) { $aResult[$mItem] = $oImapResponse->ResponseList[$iIndex + 1]; } break; } } } } } } return $aResult; }
/** * @param string $sFolderName * @param int $iOffset = 0 * @param int $iLimit = 10 * @param string $sSearch = '' * @param string $sPrevUidNext = '' * @param mixed $oCacher = null * @param string $sCachePrefix = '' * @param bool $bUseSortIfSupported = false * @param bool $bUseThreadSortIfSupported = false * @param array $aExpandedThreadsUids = array() * * @return \MailSo\Mail\MessageCollection * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ public function MessageList($sFolderName, $iOffset = 0, $iLimit = 10, $sSearch = '', $sPrevUidNext = '', $oCacher = null, $bUseSortIfSupported = false, $bUseThreadSortIfSupported = false, $aExpandedThreadsUids = array()) { $sSearch = \trim($sSearch); if (!\MailSo\Base\Validator::RangeInt($iOffset, 0) || !\MailSo\Base\Validator::RangeInt($iLimit, 0, 999) || !is_string($sSearch)) { throw new \MailSo\Base\Exceptions\InvalidArgumentException(); } $this->oImapClient->FolderExamine($sFolderName); $oMessageCollection = MessageCollection::NewInstance(); $oMessageCollection->FolderName = $sFolderName; $oMessageCollection->Offset = $iOffset; $oMessageCollection->Limit = $iLimit; $oMessageCollection->Search = $sSearch; $aLastCollapsedThreadUids = array(); $aThreads = array(); $iMessageCount = 0; $iMessageRealCount = 0; $iMessageUnseenCount = 0; $sUidNext = '0'; $sSerializedHash = ''; $bUseSortIfSupported = $bUseSortIfSupported ? $this->oImapClient->IsSupported('SORT') : false; $bUseThreadSortIfSupported = $bUseThreadSortIfSupported ? $this->oImapClient->IsSupported('THREAD=REFS') || $this->oImapClient->IsSupported('THREAD=REFERENCES') || $this->oImapClient->IsSupported('THREAD=ORDEREDSUBJECT') : false; if (!$oCacher || !$oCacher instanceof \MailSo\Cache\CacheClient) { $oCacher = null; } $this->initFolderValues($sFolderName, $iMessageRealCount, $iMessageUnseenCount, $sUidNext); $iMessageCount = $iMessageRealCount; $oMessageCollection->FolderHash = self::GenerateHash($sFolderName, $iMessageRealCount, $iMessageUnseenCount, $sUidNext); $oMessageCollection->UidNext = $sUidNext; $oMessageCollection->NewMessages = $this->getFolderNextMessageInformation($sFolderName, $sPrevUidNext, $sUidNext); $bCacher = false; $bSearch = false; if (0 < $iMessageRealCount) { $bIndexAsUid = false; $aIndexOrUids = array(); $bSearch = 0 < \strlen($sSearch); if ($bSearch || $bUseSortIfSupported && !$bUseThreadSortIfSupported) { $bIndexAsUid = true; $aIndexOrUids = null; $sSearchCriterias = $this->getSearchBuilder($sSearch)->Complete(); if ($oCacher && $oCacher->IsInited()) { $sSerializedHash = ($bUseSortIfSupported ? 'S' : 'N') . '/' . ($bUseThreadSortIfSupported ? 'T' : 'N') . '/' . $this->oImapClient->GetLogginedUser() . '@' . $this->oImapClient->GetConnectedHost() . ':' . $this->oImapClient->GetConnectedPort() . '/' . $oMessageCollection->FolderName . '/' . $oMessageCollection->FolderHash . '/' . $sSearchCriterias; $sSerializedUids = $oCacher->Get($sSerializedHash); if (!empty($sSerializedUids)) { $aSerializedUids = @\unserialize($sSerializedUids); if (\is_array($aSerializedUids)) { $aIndexOrUids = $aSerializedUids; $bCacher = true; } } } if (!\is_array($aIndexOrUids)) { if ($bUseSortIfSupported && !$bUseThreadSortIfSupported) { $aIndexOrUids = $this->oImapClient->MessageSimpleSort(array('ARRIVAL'), $sSearchCriterias, $bIndexAsUid); } else { if (!\MailSo\Base\Utils::IsAscii($sSearch)) { try { $aIndexOrUids = $this->oImapClient->MessageSimpleSearch($sSearchCriterias, $bIndexAsUid, 'UTF-8'); } catch (\MailSo\Imap\Exceptions\NegativeResponseException $oException) { $oException = null; $aIndexOrUids = null; } } if (null === $aIndexOrUids) { $aIndexOrUids = $this->oImapClient->MessageSimpleSearch($sSearchCriterias, $bIndexAsUid); } } } } else { if ($bUseThreadSortIfSupported && 1 < $iMessageCount) { $bIndexAsUid = true; $aThreads = $this->MessageListThreadsMap($oMessageCollection->FolderName, $oMessageCollection->FolderHash, $oCacher); $aIndexOrUids = $this->compileLineThreadUids($aThreads, $aLastCollapsedThreadUids, $aExpandedThreadsUids, 0); $iMessageCount = count($aIndexOrUids); } else { $bIndexAsUid = false; $aIndexOrUids = array(1); if (1 < $iMessageCount) { $aIndexOrUids = \array_reverse(\range(1, $iMessageCount)); } } } if ($bIndexAsUid && !$bCacher && \is_array($aIndexOrUids) && $oCacher && $oCacher->IsInited() && 0 < \strlen($sSerializedHash)) { $oCacher->Set($sSerializedHash, \serialize($aIndexOrUids)); } if (\is_array($aIndexOrUids)) { $oMessageCollection->MessageCount = $iMessageRealCount; $oMessageCollection->MessageUnseenCount = $iMessageUnseenCount; $oMessageCollection->MessageResultCount = 0 === \strlen($sSearch) ? $iMessageCount : \count($aIndexOrUids); if (0 < count($aIndexOrUids)) { $iOffset = 0 > $iOffset ? 0 : $iOffset; $aRequestIndexOrUids = \array_slice($aIndexOrUids, $iOffset, $iLimit); $this->MessageListByRequestIndexOrUids($oMessageCollection, $aRequestIndexOrUids, $bIndexAsUid); } } } $aLastCollapsedThreadUidsForPage = array(); if (!$bSearch && $bUseThreadSortIfSupported && 0 < \count($aThreads)) { $oMessageCollection->ForeachList(function ($oMessage) use($aThreads, $aLastCollapsedThreadUids, &$aLastCollapsedThreadUidsForPage) { $iUid = $oMessage->Uid(); if (in_array($iUid, $aLastCollapsedThreadUids)) { $aLastCollapsedThreadUidsForPage[] = $iUid; } if (isset($aThreads[$iUid]) && \is_array($aThreads[$iUid]) && 0 < \count($aThreads[$iUid])) { $oMessage->SetThreads($aThreads[$iUid]); $oMessage->SetThreadsLen(\count($aThreads[$iUid])); } else { if (!isset($aThreads[$iUid])) { foreach ($aThreads as $iKeyUid => $mSubItem) { if (is_array($mSubItem) && in_array($iUid, $mSubItem)) { $oMessage->SetParentThread($iKeyUid); $oMessage->SetThreadsLen(\count($mSubItem)); } } } } }); $oMessageCollection->LastCollapsedThreadUids = $aLastCollapsedThreadUidsForPage; } return $oMessageCollection; }
/** * @return string */ public function ValueWithCharsetAutoDetect() { $sValue = $this->Value(); if (!\MailSo\Base\Utils::IsAscii($sValue) && 0 < \strlen($this->sEncodedValueForReparse) && !\MailSo\Base\Utils::IsAscii($this->sEncodedValueForReparse)) { $sValueCharset = \MailSo\Base\Utils::CharsetDetect($this->sEncodedValueForReparse); if (0 < \strlen($sValueCharset)) { $this->SetParentCharset($sValueCharset); $sValue = $this->Value(); } } return $sValue; }
/** * @param string $sStr * * @return string */ public static function CharsetDetect($sStr) { $mResult = ''; if (!\MailSo\Base\Utils::IsAscii($sStr)) { $mResult = \MailSo\Base\Utils::IsMbStringSupported() && \MailSo\Base\Utils::FunctionExistsAndEnabled('mb_detect_encoding') ? @\mb_detect_encoding($sStr, 'auto', true) : false; if (false === $mResult && \MailSo\Base\Utils::IsIconvSupported()) { $mResult = \md5(@\iconv('utf-8', 'utf-8//IGNORE', $sStr)) === \md5($sStr) ? 'utf-8' : ''; } } return \is_string($mResult) && 0 < \strlen($mResult) ? $mResult : ''; }
/** * Obtains message list with messages data. * * @param CAccount $oAccount Account object. * @param string $sFolderFullNameRaw Raw full name of the folder. * @param int $iOffset = 0. Offset value for obtaining a partial list. * @param int $iLimit = 20. Limit value for obtaining a partial list. * @param string $sSearch = ''. Search text. * @param bool $bUseThreads = false. If **true**, message list will be returned in threaded mode. * @param array $aFilters = array(). Contains filters for searching of messages. * @param string $sInboxUidnext = ''. Uidnext value of Inbox folder. * * @return CApiMailMessageCollection * * @throws CApiInvalidArgumentException */ public function getMessageList($oAccount, $sFolderFullNameRaw, $iOffset = 0, $iLimit = 20, $sSearch = '', $bUseThreads = false, $aFilters = array(), $sInboxUidnext = '') { if (0 === strlen($sFolderFullNameRaw) || 0 > $iOffset || 0 >= $iLimit || 999 < $iLimit) { throw new CApiInvalidArgumentException(); } $oMessageCollection = false; $oSettings =& CApi::GetSettings(); $oImapClient =& $this->_getImapClient($oAccount, 20, 60 * 2); $oImapClient->FolderExamine($sFolderFullNameRaw); $aList = $this->_getFolderInformation($oImapClient, $sFolderFullNameRaw); $iMessageCount = $aList[0]; $iRealMessageCount = $aList[0]; $iMessageUnseenCount = $aList[1]; $sUidNext = $aList[2]; $oMessageCollection = CApiMailMessageCollection::createInstance(); $oMessageCollection->FolderName = $sFolderFullNameRaw; $oMessageCollection->Offset = $iOffset; $oMessageCollection->Limit = $iLimit; $oMessageCollection->Search = $sSearch; $oMessageCollection->UidNext = $sUidNext; $oMessageCollection->Filters = implode(',', $aFilters); $aThreads = array(); $bUseThreadsIfSupported = !!$oSettings->GetConf('WebMail/UseThreadsIfSupported'); if ($bUseThreadsIfSupported) { $bUseThreadsIfSupported = $bUseThreads; } $oMessageCollection->FolderHash = $aList[3]; $bSearch = false; if (0 < $iRealMessageCount) { $bIndexAsUid = false; $aIndexOrUids = array(); $bUseSortIfSupported = !!$oSettings->GetConf('WebMail/UseSortImapForDateMode'); if ($bUseSortIfSupported) { $bUseSortIfSupported = $oImapClient->IsSupported('SORT'); } if ($bUseThreadsIfSupported) { $bUseThreadsIfSupported = $oImapClient->IsSupported('THREAD=REFS') || $oImapClient->IsSupported('THREAD=REFERENCES') || $oImapClient->IsSupported('THREAD=ORDEREDSUBJECT'); } if (0 < strlen($sSearch) || 0 < count($aFilters)) { $sCutedSearch = $sSearch; $sCutedSearch = \preg_replace('/[\\s]+/', ' ', $sCutedSearch); $sCutedSearch = \preg_replace('/attach[ ]?:[ ]?/i', 'attach:', $sCutedSearch); $bSearchAttachments = false; $fAttachmentSearchCallback = null; $aMatch = array(); if (CApi::GetConf('labs.use-body-structures-for-has-attachments-search', false) && \preg_match('/has[ ]?:[ ]?attachments/i', $sSearch) || \preg_match('/attach:([^\\s]+)/i', $sSearch, $aMatch)) { $bSearchAttachments = true; $sAttachmentName = isset($aMatch[1]) ? trim($aMatch[1]) : ''; $sAttachmentRegs = !empty($sAttachmentName) && '*' !== $sAttachmentName ? '/[^>]*' . str_replace('\\*', '[^>]*', preg_quote(trim($sAttachmentName, '*'), '/')) . '[^>]*/ui' : ''; if (CApi::GetConf('labs.use-body-structures-for-has-attachments-search', false)) { $sCutedSearch = trim(preg_replace('/has[ ]?:[ ]?attachments/i', '', $sCutedSearch)); } $sCutedSearch = trim(preg_replace('/attach:([^\\s]+)/', '', $sCutedSearch)); $fAttachmentSearchCallback = function ($oBodyStructure, $sSize, $sInternalDate, $aFlagsLower, $sUid) use($sFolderFullNameRaw, $sAttachmentRegs) { $bResult = false; if ($oBodyStructure) { $aAttachmentsParts = $oBodyStructure->SearchAttachmentsParts(); if ($aAttachmentsParts && 0 < count($aAttachmentsParts)) { $oAttachments = CApiMailAttachmentCollection::createInstance(); foreach ($aAttachmentsParts as $oAttachmentItem) { $oAttachments->Add(CApiMailAttachment::createInstance($sFolderFullNameRaw, $sUid, $oAttachmentItem)); } $bResult = $oAttachments->hasNotInlineAttachments(); if ($bResult && !empty($sAttachmentRegs)) { $aList = $oAttachments->FilterList(function ($oAttachment) use($sAttachmentRegs) { if ($oAttachment && !$oAttachment->isInline() && !$oAttachment->getCid()) { return !!preg_match($sAttachmentRegs, $oAttachment->getFileName()); } return false; }); return is_array($aList) ? 0 < count($aList) : false; } } } unset($oBodyStructure); return $bResult; }; } if (0 < strlen($sCutedSearch) || 0 < count($aFilters)) { $bSearch = true; $sSearchCriterias = $this->_prepareImapSearchString($oImapClient, $sCutedSearch, $oAccount->getDefaultTimeOffset() * 60, $aFilters); $bIndexAsUid = true; $aIndexOrUids = null; if ($bUseSortIfSupported) { $aIndexOrUids = $oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), $sSearchCriterias, $bIndexAsUid); } else { if (!\MailSo\Base\Utils::IsAscii($sCutedSearch)) { try { $aIndexOrUids = $oImapClient->MessageSimpleSearch($sSearchCriterias, $bIndexAsUid, 'UTF-8'); } catch (\MailSo\Imap\Exceptions\NegativeResponseException $oException) { // Charset is not supported. Skip and try request without charset. $aIndexOrUids = null; } } if (null === $aIndexOrUids) { $aIndexOrUids = $oImapClient->MessageSimpleSearch($sSearchCriterias, $bIndexAsUid); } } if ($bSearchAttachments && is_array($aIndexOrUids) && 0 < count($aIndexOrUids)) { $aIndexOrUids = $this->_doSpecialUidsSearch($oImapClient, $fAttachmentSearchCallback, $sFolderFullNameRaw, $aIndexOrUids, $iOffset, $iLimit); } } else { if ($bSearchAttachments) { $bIndexAsUid = true; $aIndexOrUids = $this->_doSpecialIndexSearch($oImapClient, $fAttachmentSearchCallback, $sFolderFullNameRaw, $iOffset, $iLimit); } } } else { if ($bUseThreadsIfSupported && 1 < $iMessageCount) { $bIndexAsUid = true; $aThreadUids = array(); try { $aThreadUids = $oImapClient->MessageSimpleThread(); } catch (\MailSo\Imap\Exceptions\RuntimeException $oException) { $aThreadUids = array(); } $aThreads = $this->_compileThreadList($aThreadUids); if ($bUseSortIfSupported) { $aThreads = $this->_resortThreadList($aThreads, $oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), 'ALL', true)); } else { // $this->chunkThreadArray($aThreads); } $aIndexOrUids = array_keys($aThreads); $iMessageCount = count($aIndexOrUids); } else { if ($bUseSortIfSupported && 1 < $iMessageCount) { $bIndexAsUid = true; $aIndexOrUids = $oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), 'ALL', $bIndexAsUid); } else { $bIndexAsUid = false; $aIndexOrUids = array(1); if (1 < $iMessageCount) { $aIndexOrUids = array_reverse(range(1, $iMessageCount)); } } } } if (is_array($aIndexOrUids)) { $oMessageCollection->MessageCount = $iRealMessageCount; $oMessageCollection->MessageUnseenCount = $iMessageUnseenCount; $oMessageCollection->MessageResultCount = 0 < strlen($sSearch) || 0 < count($aFilters) ? count($aIndexOrUids) : $iMessageCount; if (0 < count($aIndexOrUids)) { $iOffset = 0 > $iOffset ? 0 : $iOffset; $aRequestIndexOrUids = array_slice($aIndexOrUids, $iOffset, $iLimit); if ($bIndexAsUid) { $oMessageCollection->Uids = $aRequestIndexOrUids; } if (is_array($aRequestIndexOrUids) && 0 < count($aRequestIndexOrUids)) { $aFetchResponse = $oImapClient->Fetch(array(\MailSo\Imap\Enumerations\FetchType::INDEX, \MailSo\Imap\Enumerations\FetchType::UID, \MailSo\Imap\Enumerations\FetchType::RFC822_SIZE, \MailSo\Imap\Enumerations\FetchType::INTERNALDATE, \MailSo\Imap\Enumerations\FetchType::FLAGS, \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE, \MailSo\Imap\Enumerations\FetchType::BODY_HEADER_PEEK), implode(',', $aRequestIndexOrUids), $bIndexAsUid); if (is_array($aFetchResponse) && 0 < count($aFetchResponse)) { $aFetchIndexArray = array(); $oFetchResponseItem = null; foreach ($aFetchResponse as &$oFetchResponseItem) { $aFetchIndexArray[$bIndexAsUid ? $oFetchResponseItem->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::UID) : $oFetchResponseItem->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::INDEX)] =& $oFetchResponseItem; unset($oFetchResponseItem); } foreach ($aRequestIndexOrUids as $iFUid) { if (isset($aFetchIndexArray[$iFUid])) { $oMailMessage = CApiMailMessage::createInstance($oMessageCollection->FolderName, $aFetchIndexArray[$iFUid]); if (!$bIndexAsUid) { $oMessageCollection->Uids[] = $oMailMessage->getUid(); } $oMessageCollection->Add($oMailMessage); unset($oMailMessage); } } } } } } } if (!$bSearch && $bUseThreadsIfSupported && 0 < count($aThreads)) { $oMessageCollection->ForeachList(function ($oMessage) use($aThreads) { $iUid = $oMessage->getUid(); if (isset($aThreads[$iUid]) && is_array($aThreads[$iUid])) { $oMessage->setThreads($aThreads[$iUid]); } }); } if (0 < strlen($sInboxUidnext) && 'INBOX' === $oMessageCollection->FolderName && $sInboxUidnext !== $oMessageCollection->UidNext) { $oMessageCollection->New = $this->getNewMessagesInformation($oAccount, 'INBOX', $sInboxUidnext, $oMessageCollection->UidNext); } return $oMessageCollection; }
/** * @param \MailSo\Cache\CacheClient|null $oCacher * @param string $sSearch * @param string $sFilter * @param string $sFolderName * @param string $sFolderHash * @param bool $bUseSortIfSupported = false * * @return array * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ public function GetUids($oCacher, $sSearch, $sFilter, $sFolderName, $sFolderHash, $bUseSortIfSupported = false) { $aResultUids = false; $bUidsFromCacher = false; $bUseCacheAfterSearch = true; $sSerializedHash = ''; $sSerializedLog = ''; $bUseSortIfSupported = $bUseSortIfSupported ? !!$this->oImapClient->IsSupported('SORT') : false; if (0 < \strlen($sSearch)) { $bUseSortIfSupported = false; } $sSearchCriterias = $this->getImapSearchCriterias($sSearch, $sFilter, 0, $bUseCacheAfterSearch); if ($bUseCacheAfterSearch && $oCacher && $oCacher->IsInited()) { $sSerializedHash = 'GetUids/' . ($bUseSortIfSupported ? 'S' : 'N') . '/' . $this->GenerateImapClientHash() . '/' . $sFolderName . '/' . $sSearchCriterias; $sSerializedLog = '"' . $sFolderName . '" / ' . $sSearchCriterias . ''; $sSerialized = $oCacher->Get($sSerializedHash); if (!empty($sSerialized)) { $aSerialized = @\json_decode($sSerialized, true); if (\is_array($aSerialized) && isset($aSerialized['FolderHash'], $aSerialized['Uids']) && $sFolderHash === $aSerialized['FolderHash'] && \is_array($aSerialized['Uids'])) { if ($this->oLogger) { $this->oLogger->Write('Get Serialized UIDS from cache (' . $sSerializedLog . ') [count:' . \count($aSerialized['Uids']) . ']'); } $aResultUids = $aSerialized['Uids']; $bUidsFromCacher = true; } } } if (!\is_array($aResultUids)) { $aResultUids = $bUseSortIfSupported ? $this->oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), $sSearchCriterias, true) : $this->oImapClient->MessageSimpleSearch($sSearchCriterias, true, \MailSo\Base\Utils::IsAscii($sSearchCriterias) ? '' : 'UTF-8'); if (!$bUidsFromCacher && $bUseCacheAfterSearch && \is_array($aResultUids) && $oCacher && $oCacher->IsInited() && 0 < \strlen($sSerializedHash)) { $oCacher->Set($sSerializedHash, @\json_encode(array('FolderHash' => $sFolderHash, 'Uids' => $aResultUids))); if ($this->oLogger) { $this->oLogger->Write('Save Serialized UIDS to cache (' . $sSerializedLog . ') [count:' . \count($aResultUids) . ']'); } } } return \is_array($aResultUids) ? $aResultUids : array(); }
/** * @param string $sSearch * @param string $sFolderName * @param string|bool $sFolderHash * @param bool $bUseSortIfSupported = true * @param bool $bUseESearchOrESortRequest = false * @param \MailSo\Cache\CacheClient|null $oCacher = null * * @return Array|bool */ private function getSearchUidsResult($sSearch, $sFolderName, $sFolderHash, $bUseSortIfSupported = true, $bUseESearchOrESortRequest = false, $oCacher = null) { $bUidsFromCacher = false; $aResultUids = false; $bUseCacheAfterSearch = true; $sSerializedHash = ''; $bESortSupported = $bUseSortIfSupported && $bUseESearchOrESortRequest ? $this->oImapClient->IsSupported('ESORT') : false; $bESearchSupported = $bUseESearchOrESortRequest ? $this->oImapClient->IsSupported('ESEARCH') : false; $bUseSortIfSupported = $bUseSortIfSupported ? $this->oImapClient->IsSupported('SORT') : false; $sSearchCriterias = $this->getImapSearchCriterias($sSearch, 0, $bUseCacheAfterSearch); if ($bUseCacheAfterSearch && $oCacher && $oCacher->IsInited()) { $sSerializedHash = ($bUseSortIfSupported ? 'S' : 'N') . '/' . $this->oImapClient->GetLogginedUser() . '@' . $this->oImapClient->GetConnectedHost() . ':' . $this->oImapClient->GetConnectedPort() . '/' . $sFolderName . '/' . $sSearchCriterias; $sSerializedLog = '"' . $sFolderName . '" / ' . $sSearchCriterias . ''; $sSerialized = $oCacher->Get($sSerializedHash); if (!empty($sSerialized)) { $aSerialized = @\unserialize($sSerialized); if (\is_array($aSerialized) && isset($aSerialized['FolderHash'], $aSerialized['Uids']) && \is_array($aSerialized['Uids']) && $sFolderHash === $aSerialized['FolderHash']) { if ($this->oLogger) { $this->oLogger->Write('Get Serialized UIDS from cache (' . $sSerializedLog . ') [count:' . \count($aSerialized['Uids']) . ']'); } $aResultUids = $aSerialized['Uids']; $bUidsFromCacher = true; } } } if (!\is_array($aResultUids)) { if ($bUseSortIfSupported) { if ($bESortSupported) { $aESorthData = $this->oImapClient->MessageSimpleESort(array('ARRIVAL'), $sSearchCriterias, array('ALL'), true, ''); if (isset($aESorthData['ALL'])) { $aResultUids = \MailSo\Base\Utils::ParseFetchSequence($aESorthData['ALL']); $aResultUids = \array_reverse($aResultUids); } unset($aESorthData); } else { $aResultUids = $this->oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), $sSearchCriterias, true); } } else { if (!\MailSo\Base\Utils::IsAscii($sSearch)) { try { if ($bESearchSupported) { $aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), true, '', 'UTF-8'); if (isset($aESearchData['ALL'])) { $aResultUids = \MailSo\Base\Utils::ParseFetchSequence($aESearchData['ALL']); $aResultUids = \array_reverse($aResultUids); } unset($aESearchData); } else { $aResultUids = $this->oImapClient->MessageSimpleSearch($sSearchCriterias, true, 'UTF-8'); } } catch (\MailSo\Imap\Exceptions\NegativeResponseException $oException) { $oException = null; $aResultUids = false; } } if (false === $aResultUids) { if ($bESearchSupported) { $aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), true); if (isset($aESearchData['ALL'])) { $aResultUids = \MailSo\Base\Utils::ParseFetchSequence($aESearchData['ALL']); $aResultUids = \array_reverse($aResultUids); } unset($aESearchData); } else { $aResultUids = $this->oImapClient->MessageSimpleSearch($sSearchCriterias, true); } } } if (!$bUidsFromCacher && $bUseCacheAfterSearch && \is_array($aResultUids) && $oCacher && $oCacher->IsInited() && 0 < \strlen($sSerializedHash)) { $oCacher->Set($sSerializedHash, \serialize(array('FolderHash' => $sFolderHash, 'Uids' => $aResultUids))); if ($this->oLogger) { $this->oLogger->Write('Save Serialized UIDS to cache (' . $sSerializedLog . ') [count:' . \count($aResultUids) . ']'); } } } return $aResultUids; }
/** * @return string */ public function Test() { $sResult = ''; try { $this->SyncDatabase(); if (0 >= $this->getVersion($this->sDsnType . '-ab-version')) { $sResult = 'Unknown database error'; } } catch (\Exception $oException) { $sResult = $oException->getMessage(); if (!empty($sResult) && !\MailSo\Base\Utils::IsAscii($sResult) && !\MailSo\Base\Utils::IsUtf8($sResult)) { $sResult = @\utf8_encode($sResult); } if (!\is_string($sResult) || empty($sResult)) { $sResult = 'Unknown database error'; } } return $sResult; }
/** * @param string $sCsvName * * @return int */ private function csvNameToTypeConvertor($sCsvName) { static $aMap = null; if (null === $aMap) { $aMap = array('Title' => PropertyType::FULLNAME, 'Name' => PropertyType::FULLNAME, 'FullName' => PropertyType::FULLNAME, 'DisplayName' => PropertyType::FULLNAME, 'GivenName' => PropertyType::FULLNAME, 'First' => PropertyType::FIRST_NAME, 'FirstName' => PropertyType::FIRST_NAME, 'Middle' => PropertyType::MIDDLE_NAME, 'MiddleName' => PropertyType::MIDDLE_NAME, 'Last' => PropertyType::LAST_NAME, 'LastName' => PropertyType::LAST_NAME, 'Suffix' => PropertyType::NAME_SUFFIX, 'NameSuffix' => PropertyType::NAME_SUFFIX, 'Prefix' => PropertyType::NAME_PREFIX, 'NamePrefix' => PropertyType::NAME_PREFIX, 'ShortName' => PropertyType::NICK_NAME, 'NickName' => PropertyType::NICK_NAME, 'BusinessFax' => array(PropertyType::PHONE, 'Work,Fax'), 'BusinessFax2' => array(PropertyType::PHONE, 'Work,Fax'), 'BusinessFax3' => array(PropertyType::PHONE, 'Work,Fax'), 'BusinessPhone' => array(PropertyType::PHONE, 'Work'), 'BusinessPhone2' => array(PropertyType::PHONE, 'Work'), 'BusinessPhone3' => array(PropertyType::PHONE, 'Work'), 'CompanyPhone' => array(PropertyType::PHONE, 'Work'), 'CompanyMainPhone' => array(PropertyType::PHONE, 'Work'), 'HomeFax' => array(PropertyType::PHONE, 'Home,Fax'), 'HomeFax2' => array(PropertyType::PHONE, 'Home,Fax'), 'HomeFax3' => array(PropertyType::PHONE, 'Home,Fax'), 'HomePhone' => array(PropertyType::PHONE, 'Home'), 'HomePhone2' => array(PropertyType::PHONE, 'Home'), 'HomePhone3' => array(PropertyType::PHONE, 'Home'), 'Mobile' => array(PropertyType::PHONE, 'Mobile'), 'MobilePhone' => array(PropertyType::PHONE, 'Mobile'), 'BusinessMobile' => array(PropertyType::PHONE, 'Work,Mobile'), 'BusinessMobilePhone' => array(PropertyType::PHONE, 'Work,Mobile'), 'OtherFax' => array(PropertyType::PHONE, 'Other,Fax'), 'OtherPhone' => array(PropertyType::PHONE, 'Other'), 'PrimaryPhone' => array(PropertyType::PHONE, 'Pref,Home'), 'Email' => array(PropertyType::EMAIl, 'Home'), 'Email2' => array(PropertyType::EMAIl, 'Home'), 'Email3' => array(PropertyType::EMAIl, 'Home'), 'HomeEmail' => array(PropertyType::EMAIl, 'Home'), 'HomeEmail2' => array(PropertyType::EMAIl, 'Home'), 'HomeEmail3' => array(PropertyType::EMAIl, 'Home'), 'EmailAddress' => array(PropertyType::EMAIl, 'Home'), 'Email2Address' => array(PropertyType::EMAIl, 'Home'), 'Email3Address' => array(PropertyType::EMAIl, 'Home'), 'OtherEmail' => array(PropertyType::EMAIl, 'Other'), 'BusinessEmail' => array(PropertyType::EMAIl, 'Work'), 'BusinessEmail2' => array(PropertyType::EMAIl, 'Work'), 'BusinessEmail3' => array(PropertyType::EMAIl, 'Work'), 'PersonalEmail' => array(PropertyType::EMAIl, 'Home'), 'PersonalEmail2' => array(PropertyType::EMAIl, 'Home'), 'PersonalEmail3' => array(PropertyType::EMAIl, 'Home'), 'Notes' => PropertyType::NOTE, 'Web' => PropertyType::WEB_PAGE, 'BusinessWeb' => array(PropertyType::WEB_PAGE, 'Work'), 'WebPage' => PropertyType::WEB_PAGE, 'BusinessWebPage' => array(PropertyType::WEB_PAGE, 'Work'), 'WebSite' => PropertyType::WEB_PAGE, 'BusinessWebSite' => array(PropertyType::WEB_PAGE, 'Work'), 'PersonalWebSite' => PropertyType::WEB_PAGE); $aMap = \array_change_key_case($aMap, CASE_LOWER); } $sCsvNameLower = \MailSo\Base\Utils::IsAscii($sCsvName) ? \preg_replace('/[\\s\\-]+/', '', \strtolower($sCsvName)) : ''; return !empty($sCsvNameLower) && isset($aMap[$sCsvNameLower]) ? $aMap[$sCsvNameLower] : PropertyType::UNKNOWN; }
/** * @param array $aSortTypes * @param string $sSearchCriterias * @param bool $bReturnUid * * @return array * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception */ public function MessageSimpleSort($aSortTypes, $sSearchCriterias = 'ALL', $bReturnUid = true) { $sCommandPrefix = $bReturnUid ? 'UID ' : ''; $sSearchCriterias = !\MailSo\Base\Validator::NotEmptyString($sSearchCriterias, true) || '*' === $sSearchCriterias ? 'ALL' : $sSearchCriterias; if (!is_array($aSortTypes) || 0 === count($aSortTypes)) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } else { if (!$this->IsSupported('SORT')) { $this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); } } $aRequest = array(); $aRequest[] = $aSortTypes; $aRequest[] = \MailSo\Base\Utils::IsAscii($sSearchCriterias) ? 'US-ASCII' : 'UTF-8'; $aRequest[] = $sSearchCriterias; $sCmd = 'SORT'; $this->SendRequest($sCommandPrefix . $sCmd, $aRequest); $aResult = $this->parseResponseWithValidation(); $aReturn = array(); $oImapResponse = null; foreach ($aResult as $oImapResponse) { if (\MailSo\Imap\Enumerations\ResponseType::UNTAGGED === $oImapResponse->ResponseType && ($sCmd === $oImapResponse->StatusOrIndex || $bReturnUid && 'UID' === $oImapResponse->StatusOrIndex && !empty($oImapResponse->ResponseList[2]) && $sCmd === $oImapResponse->ResponseList[2]) && is_array($oImapResponse->ResponseList) && 2 < count($oImapResponse->ResponseList)) { $iStart = 2; if ($bReturnUid && 'UID' === $oImapResponse->StatusOrIndex && !empty($oImapResponse->ResponseList[2]) && $sCmd === $oImapResponse->ResponseList[2]) { $iStart = 3; } for ($iIndex = $iStart, $iLen = count($oImapResponse->ResponseList); $iIndex < $iLen; $iIndex++) { $aReturn[] = (int) $oImapResponse->ResponseList[$iIndex]; } } } $aReturn = array_reverse($aReturn); return $aReturn; }
/** * @param string $sSearch * @param bool $bDetectGmail = true * * @return string */ private function escapeSearchString($sSearch, $bDetectGmail = true) { return $bDetectGmail && !\MailSo\Base\Utils::IsAscii($sSearch) && $this->IsGmail() ? '{' . \strlen($sSearch) . '+}' . "\r\n" . $sSearch : $this->oImapClient->EscapeString($sSearch); }