Cleanup Exception logging

This commit is contained in:
the-djmaze 2022-12-18 23:56:38 +01:00
parent d126380282
commit a21c859aba
11 changed files with 134 additions and 308 deletions

View file

@ -34,7 +34,7 @@ trait Messages
{
if (!\strlen(\trim($sIndexRange)))
{
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$aReturn = array();
@ -199,7 +199,7 @@ trait Messages
public function MessageCopy(string $sToFolder, SequenceSet $oRange) : ResponseCollection
{
if (!\count($oRange)) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
return $this->SendRequestGetResponse(
@ -217,13 +217,11 @@ trait Messages
public function MessageMove(string $sToFolder, SequenceSet $oRange) : ResponseCollection
{
if (!\count($oRange)) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
if (!$this->hasCapability('MOVE')) {
$this->writeLogException(
new \MailSo\RuntimeException('Move is not supported'),
\LOG_ERR, true);
$this->writeLogException(new \MailSo\RuntimeException('Move is not supported'), \LOG_ERR);
}
return $this->SendRequestGetResponse(

View file

@ -88,7 +88,7 @@ class ImapClient extends \MailSo\Net\NetClient
} else {
$this->writeLogException(
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'),
\LOG_ERR, true);
\LOG_ERR);
}
}
@ -112,9 +112,7 @@ class ImapClient extends \MailSo\Net\NetClient
if (!\strlen($sLogin) || !\strlen($sPassword))
{
$this->writeLogException(
new \InvalidArgumentException,
\LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$this->sLogginedUser = $sLogin;
@ -233,10 +231,7 @@ class ImapClient extends \MailSo\Net\NetClient
}
catch (Exceptions\NegativeResponseException $oException)
{
$this->writeLogException(
new Exceptions\LoginBadCredentialsException(
$oException->GetResponses(), '', 0, $oException),
\LOG_NOTICE, true);
$this->writeLogException(new Exceptions\LoginBadCredentialsException($oException->GetResponses(), '', 0, $oException));
}
$this->bIsLoggined = true;
@ -381,7 +376,6 @@ class ImapClient extends \MailSo\Net\NetClient
throw new Exceptions\ResponseException;
} catch (\Throwable $e) {
$this->writeLogException($e, \LOG_ERR);
throw $e;
}
}
@ -419,7 +413,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
$sCommand = \trim($sCommand);
if (!\strlen($sCommand)) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$this->IsConnected(true);
@ -462,13 +456,9 @@ class ImapClient extends \MailSo\Net\NetClient
if ($sResult) {
return $sResult;
}
$this->writeLogException(
new Exceptions\LoginException,
\LOG_NOTICE, true);
$this->writeLogException(new Exceptions\LoginException);
}
$this->writeLogException(
new Exceptions\LoginException,
\LOG_NOTICE, true);
$this->writeLogException(new Exceptions\LoginException);
}
/**
@ -499,7 +489,6 @@ class ImapClient extends \MailSo\Net\NetClient
}
} catch (\Throwable $e) {
$this->writeLogException($e, \LOG_WARNING);
throw $e;
}
}
@ -537,7 +526,6 @@ class ImapClient extends \MailSo\Net\NetClient
} catch (\Throwable $e) {
$this->writeLogException($e, \LOG_WARNING);
throw $e;
}
return $oResult;
@ -582,7 +570,6 @@ class ImapClient extends \MailSo\Net\NetClient
} catch (\Throwable $e) {
$this->writeLogException($e, \LOG_WARNING);
throw $e;
}
}

View file

@ -53,9 +53,7 @@ class ESEARCH extends Request
function __construct(\MailSo\Imap\ImapClient $oImapClient)
{
if (!$oImapClient->hasCapability('ESEARCH')) {
$oImapClient->writeLogException(
new \MailSo\RuntimeException('ESEARCH is not supported'),
\LOG_ERR, true);
$oImapClient->writeLogException(new \MailSo\RuntimeException('ESEARCH is not supported'), \LOG_ERR);
}
parent::__construct($oImapClient);
}
@ -67,9 +65,7 @@ class ESEARCH extends Request
/* // RFC 6203
if (false !== \stripos($this->sCriterias, 'FUZZY') && !$this->oImapClient->hasCapability('SEARCH=FUZZY')) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('SEARCH=FUZZY is not supported'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('SEARCH=FUZZY is not supported'), \LOG_ERR);
}
*/
@ -88,9 +84,7 @@ class ESEARCH extends Request
}
if ($aFolders) {
if (!$this->oImapClient->hasCapability('MULTISEARCH')) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('MULTISEARCH is not supported'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('MULTISEARCH is not supported'), \LOG_ERR);
}
$sCmd = 'ESEARCH';
$aReques[] = 'IN';
@ -108,9 +102,7 @@ class ESEARCH extends Request
if (!$this->oImapClient->hasCapability('CONTEXT=SEARCH')) {
foreach ($this->aReturn as $sReturn) {
if (\preg_match('/PARTIAL|UPDATE|CONTEXT/i', $sReturn)) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('CONTEXT=SEARCH is not supported'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('CONTEXT=SEARCH is not supported'), \LOG_ERR);
}
}
}

View file

@ -54,9 +54,7 @@ class SORT extends Request
function __construct(\MailSo\Imap\ImapClient $oImapClient)
{
if (!$oImapClient->hasCapability('SORT')) {
$oImapClient->writeLogException(
new \MailSo\RuntimeException('SORT is not supported'),
\LOG_ERR, true);
$oImapClient->writeLogException(new \MailSo\RuntimeException('SORT is not supported'), \LOG_ERR);
}
parent::__construct($oImapClient);
}
@ -64,9 +62,7 @@ class SORT extends Request
public function SendRequest() : string
{
if (!$this->aSortTypes) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('SortTypes are missing'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('SortTypes are missing'), \LOG_ERR);
}
$aRequest = array();
@ -74,16 +70,12 @@ class SORT extends Request
if ($this->aReturn) {
// RFC 5267 checks
if (!$this->oImapClient->hasCapability('ESORT')) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('ESORT is not supported'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('ESORT is not supported'), \LOG_ERR);
}
if (!$this->oImapClient->hasCapability('CONTEXT=SORT')) {
foreach ($this->aReturn as $sReturn) {
if (\preg_match('/PARTIAL|UPDATE|CONTEXT/i', $sReturn)) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException('CONTEXT=SORT is not supported'),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException('CONTEXT=SORT is not supported'), \LOG_ERR);
}
}
}

View file

@ -34,9 +34,7 @@ class THREAD extends Request
} else if ($oImapClient->hasCapability('THREAD=ORDEREDSUBJECT')) {
$this->sAlgorithm = 'ORDEREDSUBJECT';
} else {
$oImapClient->writeLogException(
new \MailSo\RuntimeException('THREAD is not supported'),
\LOG_ERR, true);
$oImapClient->writeLogException(new \MailSo\RuntimeException('THREAD is not supported'), \LOG_ERR);
}
parent::__construct($oImapClient);
}
@ -44,9 +42,7 @@ class THREAD extends Request
public function SendRequestIterateResponse() : iterable
{
if (!$this->oImapClient->hasCapability(\strtoupper("THREAD={$this->sAlgorithm}"))) {
$this->oImapClient->writeLogException(
new \MailSo\RuntimeException("THREAD={$this->sAlgorithm} is not supported"),
\LOG_ERR, true);
$this->oImapClient->writeLogException(new \MailSo\RuntimeException("THREAD={$this->sAlgorithm} is not supported"), \LOG_ERR);
}
$this->oImapClient->SendRequest(

View file

@ -412,7 +412,7 @@ trait ResponseParser
}
catch (\Throwable $oException)
{
$this->writeLogException($oException);
$this->writeLogException($oException, \LOG_NOTICE, false);
}
if ($rImapLiteralStream) {

View file

@ -189,13 +189,21 @@ class Logger extends \SplFixedArray
return $this->Write(\print_r($mValue, true), $iType, $sName);
}
private $aExceptions = [];
public function WriteException(\Throwable $oException, int $iType = \LOG_NOTICE, string $sName = '') : void
{
$this->Write((string) $oException, $iType, $sName);
if (!\in_array($oException, $this->aExceptions)) {
$this->Write((string) $oException, $iType, $sName);
$this->aExceptions[] = $oException;
}
}
public function WriteExceptionShort(\Throwable $oException, int $iType = \LOG_NOTICE, string $sName = '') : void
{
$this->Write($oException->getMessage(), $iType, $sName);
if (!\in_array($oException, $this->aExceptions)) {
$this->Write($oException->getMessage(), $iType, $sName);
$this->aExceptions[] = $oException;
}
}
}

View file

@ -31,6 +31,7 @@ class ConnectSettings implements \JsonSerializable
// public int $type = Enumerations\ConnectionSecurityType::NONE;
public SSLContext $ssl;
// public bool $tls_weak = false;
// Authentication settings use by all child classes
public bool $useAuth = true;
@ -74,6 +75,7 @@ class ConnectSettings implements \JsonSerializable
if (isset($aSettings['sasl'])) {
$object->SASLMechanisms = $aSettings['sasl'];
}
// $object->tls_weak = !empty($aSettings['tls_weak']);
return $object;
}
@ -89,6 +91,7 @@ class ConnectSettings implements \JsonSerializable
'shortLogin' => $this->shortLogin,
'sasl' => $this->SASLMechanisms,
'ssl' => $this->ssl
// 'tls_weak' => $this->tls_weak
);
}

View file

@ -90,15 +90,11 @@ abstract class NetClient
{
$oSettings->host = \trim($oSettings->host);
if (!\strlen($oSettings->host) || !\MailSo\Base\Validator::RangeInt($oSettings->port, 0, 65535)) {
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException,
\LOG_ERR, true);
$this->writeLogException(new \MailSo\Base\Exceptions\InvalidArgumentException, \LOG_ERR);
}
if ($this->IsConnected()) {
$this->writeLogException(
new Exceptions\SocketAlreadyConnectedException,
\LOG_ERR, true);
$this->writeLogException(new Exceptions\SocketAlreadyConnectedException, \LOG_ERR);
}
$this->Settings = $oSettings;
@ -118,7 +114,7 @@ abstract class NetClient
if (!$this->ssl && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->Settings->type) {
$this->writeLogException(
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported: ('.\implode(', ', \stream_get_transports()).')'),
\LOG_ERR, true);
\LOG_ERR);
}
$this->iStartConnectTime = \microtime(true);
@ -150,15 +146,14 @@ abstract class NetClient
new Exceptions\SocketCanNotConnectToHostException(
\MailSo\Base\Locale::ConvertSystemString($sErrorStr), (int) $iErrorNo,
'Can\'t connect to host "'.$this->sConnectedHost.':'.$this->Settings->port.'"'
), \LOG_NOTICE, true);
)
);
}
$this->writeLog((\microtime(true) - $this->iStartConnectTime).' (raw connection)', \LOG_DEBUG);
if ($this->rConnect) {
if (\MailSo\Base\Utils::FunctionCallable('stream_set_timeout')) {
\stream_set_timeout($this->rConnect, \max(5, $oSettings->timeout));
}
if ($this->rConnect && \MailSo\Base\Utils::FunctionCallable('stream_set_timeout')) {
\stream_set_timeout($this->rConnect, \max(5, $oSettings->timeout));
}
}
@ -167,27 +162,21 @@ abstract class NetClient
return $this->rConnect && !empty(\stream_get_meta_data($this->rConnect)['crypto']);
}
public function EnableCrypto(bool $insecure = false)
public function EnableCrypto() : void
{
$bError = true;
$bSuccess = false;
if ($this->rConnect && \MailSo\Base\Utils::FunctionCallable('stream_socket_enable_crypto')) {
$crypto_method = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && OPENSSL_VERSION_NUMBER >= 0x10101000) {
$crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT;
}
if ($insecure) {
$crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}
if (\stream_socket_enable_crypto($this->rConnect, true, $crypto_method)) {
$bError = false;
}
// if ($this->Settings->tls_weak) {
// $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
// }
$bSuccess = \stream_socket_enable_crypto($this->rConnect, true, $crypto_method);
}
if ($bError) {
$this->writeLogException(
new \MailSo\RuntimeException('Cannot enable STARTTLS.'),
\LOG_ERR, true);
}
$bSuccess || $this->writeLogException(new \MailSo\RuntimeException('Cannot enable STARTTLS.'), \LOG_ERR);
}
/**
@ -196,10 +185,8 @@ abstract class NetClient
*/
public function Disconnect() : void
{
if ($this->rConnect)
{
if (!$this->bUnreadBuffer && !$this->bRunningCallback)
{
if ($this->rConnect) {
if (!$this->bUnreadBuffer && !$this->bRunningCallback) {
$this->Logout();
}
@ -208,13 +195,10 @@ abstract class NetClient
$this->writeLog('Disconnected from "'.$this->sConnectedHost.':'.$this->Settings->port.'" ('.
(($bResult) ? 'success' : 'unsuccess').')');
if ($this->iStartConnectTime)
{
if ($this->iStartConnectTime) {
$this->writeLog((\microtime(true) - $this->iStartConnectTime).' (net session)', \LOG_DEBUG);
$this->iStartConnectTime = 0;
}
$this->rConnect = null;
}
}
@ -227,9 +211,7 @@ abstract class NetClient
return true;
}
if ($bThrowExceptionOnFalse) {
$this->writeLogException(
new Exceptions\SocketConnectionDoesNotAvailableException,
\LOG_ERR, true);
$this->writeLogException(new Exceptions\SocketConnectionDoesNotAvailableException, \LOG_ERR);
}
return false;
}
@ -255,7 +237,7 @@ abstract class NetClient
protected function sendRaw(string $sRaw, bool $bWriteToLog = true, string $sFakeRaw = '') : void
{
if ($this->bUnreadBuffer) {
$this->writeLogException(new Exceptions\SocketUnreadBufferException, \LOG_ERR, true);
$this->writeLogException(new Exceptions\SocketUnreadBufferException, \LOG_ERR);
}
$sRaw .= "\r\n";
@ -266,13 +248,10 @@ abstract class NetClient
}
$mResult = \fwrite($this->rConnect, $sRaw);
if (false === $mResult)
{
if (false === $mResult) {
$this->IsConnected(true);
$this->writeLogException(new Exceptions\SocketWriteException, \LOG_ERR, true);
}
else if ($bWriteToLog)
{
$this->writeLogException(new Exceptions\SocketWriteException, \LOG_ERR);
} else if ($bWriteToLog) {
$this->writeLogWithCrlf('> '.($bFake ? $sFakeRaw : $sRaw));
}
}
@ -281,70 +260,42 @@ abstract class NetClient
* @throws \MailSo\Net\Exceptions\SocketConnectionDoesNotAvailableException
* @throws \MailSo\Net\Exceptions\SocketReadException
*/
protected function getNextBuffer(?int $iReadLen = null, bool $bForceLogin = false) : void
protected function getNextBuffer(?int $iReadLen = null) : void
{
if (null === $iReadLen)
{
if (null === $iReadLen) {
$this->sResponseBuffer = \fgets($this->rConnect);
}
else
{
} else {
$this->sResponseBuffer = '';
$iRead = $iReadLen;
while (0 < $iRead)
{
while (0 < $iRead) {
$sAddRead = \fread($this->rConnect, $iRead);
if (false === $sAddRead)
{
if (false === $sAddRead) {
$this->sResponseBuffer = false;
break;
}
$this->sResponseBuffer .= $sAddRead;
$iRead -= \strlen($sAddRead);
}
}
if (false === $this->sResponseBuffer)
{
if (false === $this->sResponseBuffer) {
$this->IsConnected(true);
$this->bUnreadBuffer = true;
$aSocketStatus = \stream_get_meta_data($this->rConnect);
if (isset($aSocketStatus['timed_out']) && $aSocketStatus['timed_out'])
{
$this->writeLogException(
new Exceptions\SocketReadTimeoutException,
\LOG_ERR, true);
}
else
{
if (isset($aSocketStatus['timed_out']) && $aSocketStatus['timed_out']) {
$this->writeLogException(new Exceptions\SocketReadTimeoutException, \LOG_ERR);
} else {
$this->writeLog('Stream Meta: '.\print_r($aSocketStatus, true), \LOG_ERR);
$this->writeLogException(
new Exceptions\SocketReadException,
\LOG_ERR, true);
$this->writeLogException(new Exceptions\SocketReadException, \LOG_ERR);
}
}
else
{
$iReadedLen = \strlen($this->sResponseBuffer);
if (null === $iReadLen || $bForceLogin)
{
$iLimit = 5000; // 5KB
if ($iLimit < $iReadedLen)
{
$this->writeLogWithCrlf('[cutted:'.$iReadedLen.'] < '.\substr($this->sResponseBuffer, 0, $iLimit).'...');
}
else
{
// $this->writeLogWithCrlf('< '.$this->sResponseBuffer, //.' ['.$iReadedLen.']',
$this->writeLogWithCrlf('< '.$this->sResponseBuffer);
}
}
else
{
$this->writeLog('Received '.$iReadedLen.'/'.$iReadLen.' bytes.');
} else {
$iReadBytes = \strlen($this->sResponseBuffer);
// $iReadLen && $this->writeLog('Received '.$iReadBytes.'/'.$iReadLen.' bytes.');
$iLimit = 5000; // 5KB
if ($iLimit < $iReadBytes) {
$this->writeLogWithCrlf('[cutted:'.$iReadBytes.'] < '.\substr($this->sResponseBuffer, 0, $iLimit).'...');
} else {
$this->writeLogWithCrlf('< '.$this->sResponseBuffer);
}
}
}
@ -361,21 +312,15 @@ abstract class NetClient
$this->oLogger && $this->oLogger->Write($sDesc, \LOG_INFO, $this->getLogName(), true, true);
}
protected function writeLogException(\Throwable $oException,
int $iDescType = \LOG_NOTICE, bool $bThrowException = false) : void
protected function writeLogException(\Throwable $oException, int $iDescType = \LOG_NOTICE, bool $bThrowException = true) : void
{
if ($this->oLogger)
{
if ($oException instanceof Exceptions\SocketCanNotConnectToHostException)
{
if ($this->oLogger) {
if ($oException instanceof Exceptions\SocketCanNotConnectToHostException) {
$this->oLogger->Write('Socket: ['.$oException->getSocketCode().'] '.$oException->getSocketMessage(), $iDescType, $this->getLogName());
}
$this->oLogger->WriteException($oException, $iDescType, $this->getLogName());
}
if ($bThrowException)
{
if ($bThrowException) {
throw $oException;
}
}

View file

@ -78,7 +78,7 @@ class SieveClient extends \MailSo\Net\NetClient
} else {
$this->writeLogException(
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'),
\LOG_ERR, true);
\LOG_ERR);
}
}
@ -93,11 +93,8 @@ class SieveClient extends \MailSo\Net\NetClient
$sLogin = $oSettings->Login;
$sPassword = $oSettings->Password;
$sLoginAuthKey = '';
if (!\strlen($sLogin) || !\strlen($sPassword))
{
$this->writeLogException(
new \InvalidArgumentException,
\LOG_ERR, true);
if (!\strlen($sLogin) || !\strlen($sPassword)) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$type = '';
@ -112,9 +109,7 @@ class SieveClient extends \MailSo\Net\NetClient
$this->StartTLS();
return $this->Login($oSettings);
}
$this->writeLogException(
new \MailSo\Sieve\Exceptions\LoginException,
\LOG_ERR, true);
$this->writeLogException(new \MailSo\Sieve\Exceptions\LoginException, \LOG_ERR);
}
$SASL = \SnappyMail\SASL::factory($type);
@ -173,17 +168,11 @@ class SieveClient extends \MailSo\Net\NetClient
catch (\MailSo\Sieve\Exceptions\NegativeResponseException $oException)
{
$this->writeLogException(
new \MailSo\Sieve\Exceptions\LoginBadCredentialsException(
$oException->GetResponses(), '', 0, $oException),
\LOG_ERR, true);
new \MailSo\Sieve\Exceptions\LoginBadCredentialsException($oException->GetResponses(), '', 0, $oException),
\LOG_ERR);
}
if (!$bAuth)
{
$this->writeLogException(
new \MailSo\Sieve\Exceptions\LoginBadMethodException,
\LOG_ERR, true);
}
$bAuth || $this->writeLogException(new \MailSo\Sieve\Exceptions\LoginBadMethodException, \LOG_ERR);
$this->bIsLoggined = true;
@ -197,8 +186,7 @@ class SieveClient extends \MailSo\Net\NetClient
*/
public function Logout() : void
{
if ($this->bIsLoggined)
{
if ($this->bIsLoggined) {
$this->sendRequestWithCheck('LOGOUT');
$this->bIsLoggined = false;
}
@ -214,17 +202,12 @@ class SieveClient extends \MailSo\Net\NetClient
$this->sendRequest('LISTSCRIPTS');
$aResponse = $this->parseResponse();
$this->validateResponse($aResponse);
$aResult = array();
foreach ($aResponse as $sLine)
{
foreach ($aResponse as $sLine) {
$aTokens = $this->parseLine($sLine);
if (!$aTokens)
{
continue;
if ($aTokens) {
$aResult[$aTokens[0]] = 'ACTIVE' === substr($sLine, -6);
}
$aResult[$aTokens[0]] = 'ACTIVE' === substr($sLine, -6);
}
return $aResult;
@ -269,15 +252,12 @@ class SieveClient extends \MailSo\Net\NetClient
$this->validateResponse($aResponse);
$sScript = '';
if (\count($aResponse))
{
if ('{' === $aResponse[0][0])
{
if (\count($aResponse)) {
if ('{' === $aResponse[0][0]) {
\array_shift($aResponse);
}
if (\in_array(\substr($aResponse[\count($aResponse) - 1], 0, 2), array('OK', 'NO')))
{
if (\in_array(\substr($aResponse[\count($aResponse) - 1], 0, 2), array('OK', 'NO'))) {
\array_pop($aResponse);
}
@ -351,39 +331,9 @@ class SieveClient extends \MailSo\Net\NetClient
return $this;
}
/**
* @throws \MailSo\RuntimeException
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Sieve\Exceptions\*
*/
public function GetActiveScriptName() : string
{
$aList = $this->ListScripts();
foreach ($aList as $sName => $bIsActive)
{
if ($bIsActive)
{
return $sName;
}
}
return '';
}
/**
* @throws \MailSo\RuntimeException
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Sieve\Exceptions\*
*/
public function IsActiveScript(string $sScriptName) : bool
{
return $sScriptName === $this->GetActiveScriptName();
}
private function parseLine(string $sLine) : ?array
{
if (false === $sLine || null === $sLine || \in_array(\substr($sLine, 0, 2), array('OK', 'NO')))
{
if (false === $sLine || null === $sLine || \in_array(\substr($sLine, 0, 2), array('OK', 'NO'))) {
return null;
}
@ -391,16 +341,11 @@ class SieveClient extends \MailSo\Net\NetClient
$iIndex = 0;
$aResult = array();
for ($iPos = 0; $iPos < \strlen($sLine); $iPos++)
{
if ('"' === $sLine[$iPos] && '\\' !== $sLine[$iPos])
{
if (-1 === $iStart)
{
for ($iPos = 0; $iPos < \strlen($sLine); ++$iPos) {
if ('"' === $sLine[$iPos] && '\\' !== $sLine[$iPos]) {
if (-1 === $iStart) {
$iStart = $iPos;
}
else
{
} else {
$aResult[$iIndex++] = \substr($sLine, $iStart + 1, $iPos - $iStart - 1);
$iStart = -1;
}
@ -417,8 +362,7 @@ class SieveClient extends \MailSo\Net\NetClient
*/
private function parseStartupResponse(array $aResponse) : void
{
foreach ($aResponse as $sLine)
{
foreach ($aResponse as $sLine) {
$aTokens = $this->parseLine($sLine);
if (false === $aTokens || !isset($aTokens[0]) ||
@ -430,8 +374,7 @@ class SieveClient extends \MailSo\Net\NetClient
$sToken = \strtoupper($aTokens[0]);
$this->aCapa[$sToken] = isset($aTokens[1]) ? $aTokens[1] : '';
if (isset($aTokens[1]))
{
if (isset($aTokens[1])) {
switch ($sToken) {
case 'SASL':
$this->aAuth = \explode(' ', \strtoupper($aTokens[1]));
@ -451,11 +394,8 @@ class SieveClient extends \MailSo\Net\NetClient
*/
private function sendRequest(string $sRequest) : void
{
if (!\strlen(\trim($sRequest)))
{
$this->writeLogException(
new \InvalidArgumentException,
\LOG_ERR, true);
if (!\strlen(\trim($sRequest))) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$this->IsConnected(true);
@ -474,52 +414,34 @@ class SieveClient extends \MailSo\Net\NetClient
$this->validateResponse($this->parseResponse());
}
private function convertEndOfLine(string $sLine) : string
{
$sLine = \trim($sLine);
if ('}' === \substr($sLine, -1))
{
$iPos = \strrpos($sLine, '{');
if (false !== $iPos)
{
$sSunLine = \substr($sLine, $iPos + 1, -1);
if (\is_numeric($sSunLine) && 0 < (int) $sSunLine)
{
$iLen = (int) $sSunLine;
$this->getNextBuffer($iLen, true);
if (\strlen($this->sResponseBuffer) === $iLen)
{
$sLine = \trim(\substr_replace($sLine, $this->sResponseBuffer, $iPos));
}
}
}
}
return $sLine;
}
private function parseResponse() : array
{
$aResult = array();
do
{
$this->getNextBuffer();
$sLine = $this->sResponseBuffer;
if (false === $sLine)
{
if (false === $sLine) {
break;
}
else if (\in_array(\substr($sLine, 0, 2), array('OK', 'NO')))
{
$aResult[] = $this->convertEndOfLine($sLine);
break;
$bEnd = \in_array(\substr($sLine, 0, 2), array('OK', 'NO'));
// convertEndOfLine
$sLine = \trim($sLine);
if ('}' === \substr($sLine, -1)) {
$iPos = \strrpos($sLine, '{');
if (false !== $iPos) {
$iLen = \intval(\substr($sLine, $iPos + 1, -1));
if (0 < $iLen) {
$this->getNextBuffer($iLen);
if (\strlen($this->sResponseBuffer) === $iLen) {
$sLine = \trim(\substr_replace($sLine, $this->sResponseBuffer, $iPos));
}
}
}
}
else
{
$aResult[] = $this->convertEndOfLine($sLine);
$aResult[] = $sLine;
if ($bEnd) {
break;
}
}
while (true);
@ -532,12 +454,8 @@ class SieveClient extends \MailSo\Net\NetClient
*/
private function validateResponse(array $aResponse)
{
if (!$aResponse ||
'OK' !== \substr($aResponse[\count($aResponse) - 1], 0, 2))
{
$this->writeLogException(
new \MailSo\Sieve\Exceptions\NegativeResponseException($aResponse),
\LOG_WARNING, true);
if (!$aResponse || 'OK' !== \substr($aResponse[\count($aResponse) - 1], 0, 2)) {
$this->writeLogException(new \MailSo\Sieve\Exceptions\NegativeResponseException($aResponse), \LOG_WARNING);
}
}

View file

@ -101,7 +101,7 @@ class SmtpClient extends \MailSo\Net\NetClient
} else {
$this->writeLogException(
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('STARTTLS is not supported'),
\LOG_ERR, true);
\LOG_ERR);
}
}
@ -129,9 +129,7 @@ class SmtpClient extends \MailSo\Net\NetClient
return $this->Login($oSettings);
}
\trigger_error("SMTP {$this->GetConnectedHost()} no supported AUTH options. Disable login");
$this->writeLogException(
new \MailSo\Smtp\Exceptions\LoginBadMethodException,
\LOG_NOTICE, true);
$this->writeLogException(new \MailSo\Smtp\Exceptions\LoginBadMethodException);
}
$SASL = \SnappyMail\SASL::factory($type);
@ -145,9 +143,8 @@ class SmtpClient extends \MailSo\Net\NetClient
catch (\MailSo\Smtp\Exceptions\NegativeResponseException $oException)
{
$this->writeLogException(
new \MailSo\Smtp\Exceptions\LoginBadMethodException(
$oException->GetResponses(), $oException->getMessage(), 0, $oException),
\LOG_NOTICE, true);
new \MailSo\Smtp\Exceptions\LoginBadMethodException($oException->GetResponses(), $oException->getMessage(), 0, $oException)
);
}
try
@ -178,10 +175,7 @@ class SmtpClient extends \MailSo\Net\NetClient
// RFC 2195
case 'CRAM-MD5':
if (empty($sResult)) {
$this->writeLogException(
new \MailSo\Smtp\Exceptions\NegativeResponseException,
\LOG_NOTICE, true
);
$this->writeLogException(new \MailSo\Smtp\Exceptions\NegativeResponseException);
}
$this->sendRequestWithCheck($SASL->authenticate($sLogin, $sPassword, $sResult), 235, '', true);
break;
@ -190,9 +184,8 @@ class SmtpClient extends \MailSo\Net\NetClient
catch (\MailSo\Smtp\Exceptions\NegativeResponseException $oException)
{
$this->writeLogException(
new \MailSo\Smtp\Exceptions\LoginBadCredentialsException(
$oException->GetResponses(), $oException->getMessage(), 0, $oException),
\LOG_NOTICE, true);
new \MailSo\Smtp\Exceptions\LoginBadCredentialsException($oException->GetResponses(), $oException->getMessage(), 0, $oException)
);
}
return $this;
@ -233,9 +226,7 @@ class SmtpClient extends \MailSo\Net\NetClient
public function Rcpt(string $sTo, bool $bDsn = false) : self
{
if (!$this->bMail) {
$this->writeLogException(
new \MailSo\RuntimeException('No sender reverse path has been supplied'),
\LOG_ERR, true);
$this->writeLogException(new \MailSo\RuntimeException('No sender reverse path has been supplied'), \LOG_ERR);
}
$sTo = \MailSo\Base\Utils::IdnToAscii(\MailSo\Base\Utils::Trim($sTo), true);
@ -300,9 +291,7 @@ class SmtpClient extends \MailSo\Net\NetClient
}
if (!$this->bRcpt) {
$this->writeLogException(
new \MailSo\RuntimeException('No recipient forward path has been supplied'),
\LOG_ERR, true);
$this->writeLogException(new \MailSo\RuntimeException('No recipient forward path has been supplied'), \LOG_ERR);
}
$this->sendRequestWithCheck('DATA', 354);
@ -315,9 +304,7 @@ class SmtpClient extends \MailSo\Net\NetClient
$sBuffer = \fgets($rDataStream);
if (false === $sBuffer) {
if (!\feof($rDataStream)) {
$this->writeLogException(
new \MailSo\RuntimeException('Cannot read input resource'),
\LOG_ERR, true);
$this->writeLogException(new \MailSo\RuntimeException('Cannot read input resource'), \LOG_ERR);
}
break;
}
@ -397,7 +384,7 @@ class SmtpClient extends \MailSo\Net\NetClient
{
$sCommand = \trim($sCommand);
if (!\strlen($sCommand)) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR, true);
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
}
$this->IsConnected(true);
@ -518,14 +505,14 @@ class SmtpClient extends \MailSo\Net\NetClient
new Exceptions\NegativeResponseException($this->aResults,
('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim(
(\count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : '').
$this->sResponseBuffer)), \LOG_ERR, true);
$this->sResponseBuffer)), \LOG_ERR);
}
} else {
$this->writeLogException(
new Exceptions\ResponseException($this->aResults,
('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim(
(\count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : '').
$this->sResponseBuffer)), \LOG_ERR, true);
$this->sResponseBuffer)), \LOG_ERR);
}
$this->aResults[] = $this->sResponseBuffer;