From 2a7d8ccab863a147faee9664a9b3a7ead5a348d4 Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Wed, 12 Aug 2015 23:39:49 +0400 Subject: [PATCH] Fixes a search for non-ascii strings (#793) --- .../app/libraries/MailSo/Imap/ImapClient.php | 68 +++++++++++++++++-- .../app/libraries/MailSo/Mail/MailClient.php | 10 +-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php b/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php index 85ed08edd..be7811139 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Imap/ImapClient.php @@ -1300,6 +1300,25 @@ class ImapClient extends \MailSo\Net\NetClient return $this->simpleESearchOrESortHelper(true, $sSearchCriterias, $aSearchReturn, $bReturnUid, $sLimit, '', $aSortTypes); } + /** + * @param array $aResult + * @return \MailSo\Imap\Response + */ + private function findLastResponse($aResult) + { + $oResult = null; + if (\is_array($aResult) && 0 < \count($aResult)) + { + $oResult = $aResult[\count($aResult) - 1]; + if (!($oResult instanceof \MailSo\Imap\Response)) + { + $oResult = null; + } + } + + return $oResult; + } + /** * @param string $sSearchCriterias * @param bool $bReturnUid = true @@ -1328,8 +1347,32 @@ class ImapClient extends \MailSo\Net\NetClient $sCmd = 'SEARCH'; - $this->SendRequest($sCommandPrefix.$sCmd, $aRequest); - $aResult = $this->parseResponseWithValidation(); + $sCont = $this->SendRequest($sCommandPrefix.$sCmd, $aRequest, true); + if ('' !== $sCont) + { + $aResult = $this->parseResponseWithValidation(); + $oItem = $this->findLastResponse($aResult); + + if ($oItem && \MailSo\Imap\Enumerations\ResponseType::CONTINUATION === $oItem->ResponseType) + { + $aParts = explode("\r\n", $sCont); + foreach ($aParts as $sLine) + { + $this->sendRaw($sLine); + + $aResult = $this->parseResponseWithValidation(); + $oItem = $this->findLastResponse($aResult); + if ($oItem && \MailSo\Imap\Enumerations\ResponseType::CONTINUATION === $oItem->ResponseType) + { + continue; + } + } + } + } + else + { + $aResult = $this->parseResponseWithValidation(); + } $aReturn = array(); $oImapResponse = null; @@ -1660,13 +1703,14 @@ class ImapClient extends \MailSo\Net\NetClient /** * @param string $sCommand * @param array $aParams = array() + * @param bool $bBreakOnLiteral = false * - * @return void + * @return string * * @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Net\Exceptions\Exception */ - public function SendRequest($sCommand, $aParams = array()) + public function SendRequest($sCommand, $aParams = array(), $bBreakOnLiteral = false) { if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true) || !\is_array($aParams)) { @@ -1690,7 +1734,23 @@ class ImapClient extends \MailSo\Net\NetClient } $this->aTagTimeouts[$sTag] = \microtime(true); + + if ($bBreakOnLiteral && !\preg_match('/\d\+\}\r\n/', $sRealCommand)) + { + $iPos = \strpos($sRealCommand, "}\r\n"); + if (false !== $iPos) + { + $iFakePos = \strpos($sFakeCommand, "}\r\n"); + + $this->sendRaw(\substr($sRealCommand, 0, $iPos + 1), true, + false !== $iFakePos ? \substr($sFakeCommand, 0, $iFakePos + 3) : ''); + + return \substr($sRealCommand, $iPos + 3); + } + } + $this->sendRaw($sRealCommand, true, $sFakeCommand); + return ''; } /** diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php b/rainloop/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php index ea8665381..378f4b032 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php @@ -967,8 +967,10 @@ class MailClient */ private function escapeSearchString($sSearch, $bDetectGmail = true) { - return ($bDetectGmail && !\MailSo\Base\Utils::IsAscii($sSearch) && $this->IsGmail()) - ? '{'.\strlen($sSearch).'+}'."\r\n".$sSearch : $this->oImapClient->EscapeString($sSearch); + return !\MailSo\Base\Utils::IsAscii($sSearch) + ? '{'.\strlen($sSearch).'}'."\r\n".$sSearch : $this->oImapClient->EscapeString($sSearch); +// return ($bDetectGmail && !\MailSo\Base\Utils::IsAscii($sSearch) && $this->IsGmail()) +// ? '{'.\strlen($sSearch).'+}'."\r\n".$sSearch : $this->oImapClient->EscapeString($sSearch); } /** @@ -1396,7 +1398,7 @@ class MailClient } $sCriteriasResult = \trim($sCriteriasResult); - $sCriteriasResult .= ' NOT DELETED'; + $sCriteriasResult .= ' UNDELETED'; $sFilter = \trim($sFilter); if ('' !== $sFilter) @@ -1832,7 +1834,7 @@ class MailClient { $aResultUids = $bUseSortIfSupported ? $this->oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), $sSearchCriterias, true) : - $this->oImapClient->MessageSimpleSearch($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))