remove imap_message_list_hide_deleted_messages setting (it's a bad idea to use it)

fixed uid cacher
certificate validatetion on test connection (first look)
This commit is contained in:
RainLoop Team 2014-10-01 01:26:23 +04:00
parent 1fee4d35f9
commit 60b449b9f8
8 changed files with 168 additions and 170 deletions

View file

@ -1,60 +1,55 @@
<?php
namespace MailSo;
/**
* @category MailSo
* @package Base
*/
class Config
{
/**
* @var bool
*/
public static $ICONV = true;
/**
* @var bool
*/
public static $MBSTRING = true;
/**
* @var bool
*/
public static $FixIconvByMbstring = true;
/**
* @var int
*/
public static $MessageListFastSimpleSearch = true;
/**
* @var int
*/
public static $MessageListCountLimitTrigger = 0;
/**
* @var bool
*/
public static $MessageListUndeletedFilter = false;
/**
* @var int
*/
public static $MessageListDateFilter = 0;
/**
* @var bool
*/
public static $LogSimpleLiterals = false;
/**
* @var bool
*/
public static $PreferStartTlsIfAutoDetect = true;
/**
* @var \MailSo\Log\Logger|null
*/
public static $SystemLogger = null;
}
<?php
namespace MailSo;
/**
* @category MailSo
* @package Base
*/
class Config
{
/**
* @var bool
*/
public static $ICONV = true;
/**
* @var bool
*/
public static $MBSTRING = true;
/**
* @var bool
*/
public static $FixIconvByMbstring = true;
/**
* @var int
*/
public static $MessageListFastSimpleSearch = true;
/**
* @var int
*/
public static $MessageListCountLimitTrigger = 0;
/**
* @var int
*/
public static $MessageListDateFilter = 0;
/**
* @var bool
*/
public static $LogSimpleLiterals = false;
/**
* @var bool
*/
public static $PreferStartTlsIfAutoDetect = true;
/**
* @var \MailSo\Log\Logger|null
*/
public static $SystemLogger = null;
}

View file

@ -126,6 +126,7 @@ class ImapClient extends \MailSo\Net\NetClient
* @param string $sServerName
* @param int $iPort = 143
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
* @param bool $bCapturePeerCertIfSsl = false
*
* @return \MailSo\Imap\ImapClient
*
@ -134,11 +135,11 @@ class ImapClient extends \MailSo\Net\NetClient
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function Connect($sServerName, $iPort = 143,
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT)
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false)
{
$this->aTagTimeouts['*'] = \microtime(true);
parent::Connect($sServerName, $iPort, $iSecurityType);
parent::Connect($sServerName, $iPort, $iSecurityType, $bCapturePeerCertIfSsl);
$this->parseResponseWithValidation('*', true);
@ -628,19 +629,19 @@ class ImapClient extends \MailSo\Net\NetClient
}
}
}
if ($bUseListStatus)
{
foreach ($aResult as /* @var $oImapResponse \MailSo\Imap\Response */ $oImapResponse)
{
if (\MailSo\Imap\Enumerations\ResponseType::UNTAGGED === $oImapResponse->ResponseType &&
'STATUS' === $oImapResponse->StatusOrIndex &&
'STATUS' === $oImapResponse->StatusOrIndex &&
isset($oImapResponse->ResponseList[2]) &&
isset($oImapResponse->ResponseList[3]) &&
\is_array($oImapResponse->ResponseList[3]))
{
$sFolderNameRaw = $oImapResponse->ResponseList[2];
$oCurrentFolder = null;
foreach ($aReturn as &$oFolder)
{
@ -650,7 +651,7 @@ class ImapClient extends \MailSo\Net\NetClient
break;
}
}
if (null !== $oCurrentFolder)
{
$sName = null;
@ -667,13 +668,13 @@ class ImapClient extends \MailSo\Net\NetClient
$sName = null;
}
}
if (0 < count($aStatus))
{
$oCurrentFolder->SetExtended('STATUS', $aStatus);
}
}
unset($oCurrentFolder);
}
}
@ -700,9 +701,9 @@ class ImapClient extends \MailSo\Net\NetClient
{
$sCmd = 'LIST';
}
$sListPattern = 0 === strlen(trim($sListPattern)) ? '*' : $sListPattern;
$aParameters = array(
$this->EscapeString($sParentFolderName),
$this->EscapeString($sListPattern)
@ -724,7 +725,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$bUseListStatus = false;
}
$this->SendRequest($sCmd, $aParameters);
return $this->getFoldersFromResult(
@ -1085,7 +1086,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$iStart = 3;
}
for ($iIndex = $iStart, $iLen = \count($oImapResponse->ResponseList); $iIndex < $iLen; $iIndex++)
{
$aReturn[] = (int) $oImapResponse->ResponseList[$iIndex];
@ -1142,7 +1143,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$aRequest[] = 'RETURN';
$aRequest[] = $aSearchOrSortReturn;
$aRequest[] = $aSortTypes;
$aRequest[] = \MailSo\Base\Utils::IsAscii($sSearchCriterias) ? 'US-ASCII' : 'UTF-8';
}
@ -1415,7 +1416,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$iStart = 3;
}
for ($iIndex = $iStart, $iLen = \count($oImapResponse->ResponseList); $iIndex < $iLen; $iIndex++)
{
$aNewValue = $this->validateThreadItem($oImapResponse->ResponseList[$iIndex]);
@ -1454,7 +1455,7 @@ class ImapClient extends \MailSo\Net\NetClient
return $this->SendRequestWithCheck($sCommandPrefix.'COPY',
array($sIndexRange, $this->EscapeString($sToFolder)));
}
/**
* @param string $sToFolder
* @param string $sIndexRange
@ -1488,21 +1489,23 @@ class ImapClient extends \MailSo\Net\NetClient
}
/**
* @param string $sUidRangeIfSupported
* @param string $sUidRangeIfSupported = ''
* @param bool $bForceUidExpunge = false
* @param bool $bExpungeAll = false
*
* @return \MailSo\Imap\ImapClient
*
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageExpunge($sUidRangeIfSupported = '', $bForceUidExpunge = false)
public function MessageExpunge($sUidRangeIfSupported = '', $bForceUidExpunge = false, $bExpungeAll = false)
{
$sUidRangeIfSupported = \trim($sUidRangeIfSupported);
$sCmd = 'EXPUNGE';
$aArguments = array();
if ($bForceUidExpunge && 0 < \strlen($sUidRangeIfSupported) && $this->IsSupported('UIDPLUS'))
if (!$bExpungeAll && $bForceUidExpunge && 0 < \strlen($sUidRangeIfSupported) && $this->IsSupported('UIDPLUS'))
{
$sCmd = 'UID '.$sCmd;
$aArguments = array($sUidRangeIfSupported);
@ -1737,7 +1740,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$oImapResponse = null;
$sEndTag = (null === $sEndTag) ? $this->getCurrentTag() : $sEndTag;
while (true)
{
$oImapResponse = Response::NewInstance();
@ -1929,11 +1932,11 @@ class ImapClient extends \MailSo\Net\NetClient
$this->writeLog('Literal stream read warning "read '.$iLiteralSize.' of '.
$iLiteralLen.'" bytes', \MailSo\Log\Enumerations\Type::WARNING);
}
if (!$bTreatAsAtom)
{
$aList[] = $sLiteral;
if (\MailSo\Config::$LogSimpleLiterals)
{
$this->writeLog('{'.\strlen($sLiteral).'} '.$sLiteral, \MailSo\Log\Enumerations\Type::INFO);
@ -1965,7 +1968,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$sAtomBlock = $this->partialParseResponseBranch($mNull, $iStackIndex, true,
null === $sPreviousAtomUpperCase ? '' : \strtoupper($sPreviousAtomUpperCase));
$sAtomBuilder .= $sAtomBlock;
$iPos = $this->iResponseBufParsedPos;
$sAtomBuilder .= ($bIsClosingBracketSquare) ? ']' : ')';
@ -1987,7 +1990,7 @@ class ImapClient extends \MailSo\Net\NetClient
if (null !== $oImapResponse && $oImapResponse->IsStatusResponse)
{
$oImapResponse->OptionalResponse = $aSubItems;
$bIsGotoDefault = true;
$bIsGotoNotAtomBracket = false;
continue;
@ -2350,7 +2353,7 @@ class ImapClient extends \MailSo\Net\NetClient
if (\is_resource($rImapLiteralStream))
{
$iNotReadLiteralLen = 0;
$bFeof = \feof($rImapLiteralStream);
$this->writeLog('End Callback for '.$sParent.' / '.$sLiteralAtomUpperCase.
' - feof = '.($bFeof ? 'good' : 'BAD'), $bFeof ?

View file

@ -495,6 +495,7 @@ class MailClient
* @param array $aIndexRange
* @param bool $bIndexIsUid
* @param bool $bUseExpunge = true
* @param bool $bExpungeAll = false
*
* @return \MailSo\Mail\MailClient
*
@ -502,7 +503,7 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageDelete($sFolder, $aIndexRange, $bIndexIsUid, $bUseExpunge = true)
public function MessageDelete($sFolder, $aIndexRange, $bIndexIsUid, $bUseExpunge = true, $bExpungeAll = false)
{
if (0 === \strlen($sFolder) || !\is_array($aIndexRange) || 0 === \count($aIndexRange))
{
@ -520,7 +521,7 @@ class MailClient
if ($bUseExpunge)
{
$this->oImapClient->MessageExpunge($bIndexIsUid ? $sIndexRange : '', $bIndexIsUid);
$this->oImapClient->MessageExpunge($bIndexIsUid ? $sIndexRange : '', $bIndexIsUid, $bExpungeAll);
}
return $this;
@ -532,6 +533,7 @@ class MailClient
* @param array $aIndexRange
* @param bool $bIndexIsUid
* @param bool $bUseMoveSupported = false
* @param bool $bExpungeAll = false
*
* @return \MailSo\Mail\MailClient
*
@ -539,7 +541,7 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageMove($sFromFolder, $sToFolder, $aIndexRange, $bIndexIsUid, $bUseMoveSupported = false)
public function MessageMove($sFromFolder, $sToFolder, $aIndexRange, $bIndexIsUid, $bUseMoveSupported = false, $bExpungeAll = false)
{
if (0 === \strlen($sFromFolder) || 0 === \strlen($sToFolder) ||
!\is_array($aIndexRange) || 0 === \count($aIndexRange))
@ -559,7 +561,7 @@ class MailClient
$this->oImapClient->MessageCopy($sToFolder,
\MailSo\Base\Utils::PrepearFetchSequence($aIndexRange), $bIndexIsUid);
$this->MessageDelete($sFromFolder, $aIndexRange, $bIndexIsUid, true);
$this->MessageDelete($sFromFolder, $aIndexRange, $bIndexIsUid, true, $bExpungeAll);
}
return $this;
@ -647,11 +649,10 @@ class MailClient
* @param int $iCount
* @param int $iUnseenCount
* @param string $sUidNext
* @param \MailSo\Cache\CacheClient|null $oCacher = null
*
* @return void
*/
protected function initFolderValues($sFolderName, &$iCount, &$iUnseenCount, &$sUidNext, $oCacher = null)
protected function initFolderValues($sFolderName, &$iCount, &$iUnseenCount, &$sUidNext)
{
$aFolderStatus = $this->oImapClient->FolderStatus($sFolderName, array(
\MailSo\Imap\Enumerations\FolderResponseStatus::MESSAGES,
@ -668,36 +669,7 @@ class MailClient
$sUidNext = isset($aFolderStatus[\MailSo\Imap\Enumerations\FolderResponseStatus::UIDNEXT])
? (string) $aFolderStatus[\MailSo\Imap\Enumerations\FolderResponseStatus::UIDNEXT] : '0';
if (\MailSo\Config::$MessageListUndeletedFilter)
{
$oFolder = $this->oImapClient->FolderCurrentInformation();
if (!$oFolder || $oFolder->FolderName !== $sFolderName)
{
$this->oImapClient->FolderExamine($sFolderName);
}
$aUids = $this->getSearchUidsResult('',
$sFolderName, false, false, false, $oCacher);
$iNewCount = \count($aUids);
if (0 < $iNewCount && $iNewCount !== $iCount)
{
$iCount = $iNewCount;
$aUids = $this->getSearchUidsResult('is:unread',
$sFolderName, false, false, false, $oCacher);
$iUnseenCount = \count($aUids);
}
else if (0 === $iNewCount)
{
$iCount = 0;
$iUnseenCount = 0;
}
unset($aUids);
}
else if ($this->IsGmail())
if ($this->IsGmail())
{
$oFolder = $this->oImapClient->FolderCurrentInformation();
if ($oFolder && null !== $oFolder->Exists && $oFolder->FolderName === $sFolderName)
@ -800,7 +772,6 @@ class MailClient
* @param string $sFolderName
* @param string $sPrevUidNext = ''
* @param array $aUids = ''
* @param \MailSo\Cache\CacheClient|null $oCacher = null
*
* @return string
*
@ -808,7 +779,7 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function FolderInformation($sFolderName, $sPrevUidNext = '', $aUids = array(), $oCacher = null)
public function FolderInformation($sFolderName, $sPrevUidNext = '', $aUids = array())
{
$aFlags = array();
@ -847,7 +818,7 @@ class MailClient
$iUnseenCount = 0;
$sUidNext = '0';
$this->initFolderValues($sFolderName, $iCount, $iUnseenCount, $sUidNext, $oCacher);
$this->initFolderValues($sFolderName, $iCount, $iUnseenCount, $sUidNext);
$aResult = array(
'Folder' => $sFolderName,
@ -865,7 +836,6 @@ class MailClient
/**
* @param string $sFolderName
* @param \MailSo\Cache\CacheClient|null $oCacher = null
*
* @return string
*
@ -873,13 +843,13 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function FolderHash($sFolderName, $oCacher = null)
public function FolderHash($sFolderName)
{
$iCount = 0;
$iUnseenCount = 0;
$sUidNext = '0';
$this->initFolderValues($sFolderName, $iCount, $iUnseenCount, $sUidNext, $oCacher);
$this->initFolderValues($sFolderName, $iCount, $iUnseenCount, $sUidNext);
return self::GenerateHash($sFolderName, $iCount, $iUnseenCount, $sUidNext);
}
@ -1327,10 +1297,6 @@ class MailClient
}
$sCriteriasResult = \trim(\implode(' ', $aCriteriasResult));
if (\MailSo\Config::$MessageListUndeletedFilter)
{
$sCriteriasResult .= ' UNDELETED';
}
if (0 < $iTimeFilter)
{
@ -1420,11 +1386,6 @@ class MailClient
public function MessageListThreadsMap($sFolderName, $sFolderHash, $oCacher)
{
$sSearchHash = '';
if (\MailSo\Config::$MessageListUndeletedFilter)
{
$sSearchHash .= ' UNDELETED';
}
if (0 < \MailSo\Config::$MessageListDateFilter)
{
$iD = \time() - 3600 * 24 * 30 * \MailSo\Config::$MessageListDateFilter;
@ -1608,7 +1569,7 @@ class MailClient
$aSerialized = @\unserialize($sSerialized);
if (\is_array($aSerialized) && isset($aSerialized['FolderHash'], $aSerialized['Uids']) &&
\is_array($aSerialized['Uids']) &&
($sFolderHash === $aSerialized['FolderHash'] || false === $sFolderHash))
($sFolderHash === $aSerialized['FolderHash']))
{
if ($this->oLogger)
{
@ -1762,7 +1723,7 @@ class MailClient
$oCacher = null;
}
$this->initFolderValues($sFolderName, $iMessageRealCount, $iMessageUnseenCount, $sUidNext, $oCacher);
$this->initFolderValues($sFolderName, $iMessageRealCount, $iMessageUnseenCount, $sUidNext);
$iMessageCount = $iMessageRealCount;
$oMessageCollection->FolderHash = self::GenerateHash($sFolderName, $iMessageRealCount, $iMessageUnseenCount, $sUidNext);
@ -1860,8 +1821,7 @@ class MailClient
if (1 < $iMessageCount)
{
if ($bMessageListOptimization ||
(!\MailSo\Config::$MessageListUndeletedFilter && 0 === \MailSo\Config::$MessageListDateFilter))
if ($bMessageListOptimization || 0 === \MailSo\Config::$MessageListDateFilter)
{
$aIndexOrUids = \array_reverse(\range(1, $iMessageCount));
}

View file

@ -172,6 +172,7 @@ abstract class NetClient
* @param string $sServerName
* @param int $iPort
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
* @param bool $bCapturePeerCertIfSsl = false
*
* @return void
*
@ -180,7 +181,7 @@ abstract class NetClient
* @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException
*/
public function Connect($sServerName, $iPort,
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT)
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false)
{
if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort))
{
@ -206,6 +207,8 @@ abstract class NetClient
$this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL($iPort, $iSecurityType);
$this->sConnectedHost = $this->bSecure ? 'ssl://'.$sServerName : $sServerName;
$bCapturePeerCertIfSsl = !!($bCapturePeerCertIfSsl && $this->bSecure);
if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType)
{
$this->writeLogException(
@ -217,8 +220,24 @@ abstract class NetClient
$this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"',
\MailSo\Log\Enumerations\Type::NOTE);
$this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort,
$iErrorNo, $sErrorStr, $this->iConnectTimeOut);
if ($bCapturePeerCertIfSsl && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_create') &&
\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_socket_client') && defined('STREAM_CLIENT_CONNECT'))
{
$rStreamContext = \stream_context_create(array('ssl' => array('capture_peer_cert' => true)));
$sRemoteSocket = (0 === \strpos($this->sConnectedHost, 'ssl://')
? $this->sConnectedHost : 'tcp://'.$this->sConnectedHost).':'.$this->iConnectedPort;
if ($rStreamContext)
{
$this->rConnect = @\stream_socket_client($sRemoteSocket, $iErrorNo, $sErrorStr,
$this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext);
}
}
else
{
$this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort,
$iErrorNo, $sErrorStr, $this->iConnectTimeOut);
}
if (!\is_resource($this->rConnect))
{
@ -308,6 +327,15 @@ abstract class NetClient
$this->IsConnected(true);
}
/**
* @return array|bool
*/
public function StreamContextParams()
{
return \is_resource($this->rConnect) && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_get_options')
? \stream_context_get_params($this->rConnect) : false;
}
/**
* @param string $sRaw
* @param bool $bWriteToLog = true