Improve MailSo\NetClient (and children) code

This commit is contained in:
djmaze 2021-10-21 14:46:08 +02:00
parent c8a5ae5c3a
commit 5fcf31f512
10 changed files with 110 additions and 318 deletions

View file

@ -30,17 +30,17 @@ class ImapClient extends \MailSo\Net\NetClient
/** /**
* @var int * @var int
*/ */
private $iTagCount; private $iTagCount = 0;
/** /**
* @var array * @var array
*/ */
private $aCapabilityItems; private $aCapabilityItems = null;
/** /**
* @var FolderInformation * @var FolderInformation
*/ */
private $oCurrentFolderInfo; private $oCurrentFolderInfo = null;
/** /**
* @var ResponseCollection * @var ResponseCollection
@ -55,52 +55,36 @@ class ImapClient extends \MailSo\Net\NetClient
/** /**
* @var bool * @var bool
*/ */
private $bNeedNext; private $bNeedNext = true;
/** /**
* @var array * @var array
*/ */
private $aTagTimeouts; private $aTagTimeouts = array();
/** /**
* @var bool * @var bool
*/ */
private $bIsLoggined; private $bIsLoggined = false;
/** /**
* @var bool * @var bool
*/ */
private $bIsSelected; private $bIsSelected = false;
/** /**
* @var string * @var string
*/ */
private $sLogginedUser; private $sLogginedUser = '';
/** /**
* @var bool * @var bool
*/ */
public $__FORCE_SELECT_ON_EXAMINE__; public $__FORCE_SELECT_ON_EXAMINE__ = false;
function __construct() function __construct()
{ {
parent::__construct();
$this->iTagCount = 0;
$this->aCapabilityItems = null;
$this->oCurrentFolderInfo = null;
$this->oLastResponse = new ResponseCollection; $this->oLastResponse = new ResponseCollection;
$this->bNeedNext = true;
$this->aTagTimeouts = array();
$this->bIsLoggined = false;
$this->bIsSelected = false;
$this->sLogginedUser = '';
$this->__FORCE_SELECT_ON_EXAMINE__ = false;
\ini_set('xdebug.max_nesting_level', 500); \ini_set('xdebug.max_nesting_level', 500);
} }
@ -149,7 +133,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function Login(string $sLogin, string $sPassword, string $sProxyAuthUser = '', public function Login(string $sLogin, string $sPassword, string $sProxyAuthUser = '',
bool $bUseAuthPlainIfSupported = true, bool $bUseAuthCramMd5IfSupported = true) : self bool $bUseAuthPlainIfSupported = true, bool $bUseAuthCramMd5IfSupported = true) : self
{ {
if (!strlen(\trim($sLogin)) || !strlen(\trim($sPassword))) if (!\strlen(\trim($sLogin)) || !\strlen(\trim($sPassword)))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -160,7 +144,7 @@ class ImapClient extends \MailSo\Net\NetClient
$this->sLogginedUser = $sLogin; $this->sLogginedUser = $sLogin;
// $encrypted = !empty(\stream_get_meta_data($this->rConnect)['crypto']); // $encrypted = !empty(\stream_get_meta_data($this->ConnectionResource())['crypto']);
$type = $this->IsSupported('LOGINDISABLED') ? '' : 'LOGIN'; // RFC3501 6.2.3 $type = $this->IsSupported('LOGINDISABLED') ? '' : 'LOGIN'; // RFC3501 6.2.3
$types = [ $types = [
// 'SCRAM-SHA-256' => 1, // !$encrypted // 'SCRAM-SHA-256' => 1, // !$encrypted
@ -265,15 +249,13 @@ class ImapClient extends \MailSo\Net\NetClient
/** /**
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
*/ */
public function Logout() : self public function Logout() : void
{ {
if ($this->bIsLoggined) if ($this->bIsLoggined)
{ {
$this->bIsLoggined = false; $this->bIsLoggined = false;
$this->SendRequestGetResponse('LOGOUT'); $this->SendRequestGetResponse('LOGOUT');
} }
return $this;
} }
public function IsLoggined() : bool public function IsLoggined() : bool
@ -523,7 +505,7 @@ class ImapClient extends \MailSo\Net\NetClient
} }
} }
if (!strlen(\trim($sFolderName))) if (!\strlen(\trim($sFolderName)))
{ {
throw new \MailSo\Base\Exceptions\InvalidArgumentException; throw new \MailSo\Base\Exceptions\InvalidArgumentException;
} }
@ -548,6 +530,11 @@ class ImapClient extends \MailSo\Net\NetClient
} }
/** /**
* The EXAMINE command is identical to SELECT and returns the same output;
* however, the selected mailbox is identified as read-only.
* No changes to the permanent state of the mailbox, including per-user state,
* are permitted; in particular, EXAMINE MUST NOT cause messages to lose the \Recent flag.
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException * @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception
@ -579,7 +566,7 @@ class ImapClient extends \MailSo\Net\NetClient
*/ */
public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array
{ {
if (!strlen(\trim($sIndexRange))) if (!\strlen(\trim($sIndexRange)))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -648,7 +635,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function MessageSimpleSort(array $aSortTypes, string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array public function MessageSimpleSort(array $aSortTypes, string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array
{ {
$sCommandPrefix = ($bReturnUid) ? 'UID ' : ''; $sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = !strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias $sSearchCriterias = !\strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias; ? 'ALL' : $sSearchCriterias;
if (!$aSortTypes) if (!$aSortTypes)
@ -766,8 +753,6 @@ class ImapClient extends \MailSo\Net\NetClient
public function MessageSimpleSearch(string $sSearchCriterias = 'ALL', bool $bReturnUid = true, string $sCharset = '') : array public function MessageSimpleSearch(string $sSearchCriterias = 'ALL', bool $bReturnUid = true, string $sCharset = '') : array
{ {
$sCommandPrefix = ($bReturnUid) ? 'UID ' : ''; $sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = 0 === \strlen($sSearchCriterias) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias;
$aRequest = array(); $aRequest = array();
if (0 < \strlen($sCharset)) if (0 < \strlen($sCharset))
@ -776,14 +761,14 @@ class ImapClient extends \MailSo\Net\NetClient
$aRequest[] = \strtoupper($sCharset); $aRequest[] = \strtoupper($sCharset);
} }
$aRequest[] = $sSearchCriterias; $aRequest[] = !\strlen($sSearchCriterias) || '*' === $sSearchCriterias ? 'ALL' : $sSearchCriterias;
$sCmd = 'SEARCH'; $sCmd = 'SEARCH';
$sCont = $this->SendRequest($sCommandPrefix.$sCmd, $aRequest, true); $sCont = $this->SendRequest($sCommandPrefix.$sCmd, $aRequest, true);
$oResult = $this->getResponse();
if ('' !== $sCont) if ('' !== $sCont)
{ {
$oResult = $this->getResponse();
$oItem = $oResult->getLast(); $oItem = $oResult->getLast();
if ($oItem && Enumerations\ResponseType::CONTINUATION === $oItem->ResponseType) if ($oItem && Enumerations\ResponseType::CONTINUATION === $oItem->ResponseType)
@ -802,10 +787,6 @@ class ImapClient extends \MailSo\Net\NetClient
} }
} }
} }
else
{
$oResult = $this->getResponse();
}
return $oResult->getMessageSimpleSearchResult($sCmd, $bReturnUid); return $oResult->getMessageSimpleSearchResult($sCmd, $bReturnUid);
} }
@ -818,7 +799,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function MessageSimpleThread(string $sSearchCriterias = 'ALL', bool $bReturnUid = true, string $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8) : array public function MessageSimpleThread(string $sSearchCriterias = 'ALL', bool $bReturnUid = true, string $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8) : array
{ {
$sCommandPrefix = ($bReturnUid) ? 'UID ' : ''; $sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = !strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias $sSearchCriterias = !\strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias; ? 'ALL' : $sSearchCriterias;
$sThreadType = ''; $sThreadType = '';
@ -926,9 +907,9 @@ class ImapClient extends \MailSo\Net\NetClient
*/ */
public function MessageStoreFlag(string $sIndexRange, bool $bIndexIsUid, array $aInputStoreItems, string $sStoreAction) : self public function MessageStoreFlag(string $sIndexRange, bool $bIndexIsUid, array $aInputStoreItems, string $sStoreAction) : self
{ {
if (!strlen(\trim($sIndexRange)) || if (!\strlen(\trim($sIndexRange)) ||
!strlen(\trim($sStoreAction)) || !\strlen(\trim($sStoreAction)) ||
0 === \count($aInputStoreItems)) !\count($aInputStoreItems))
{ {
return false; return false;
} }
@ -959,7 +940,7 @@ class ImapClient extends \MailSo\Net\NetClient
$this->writeLog('Write to connection stream', \MailSo\Log\Enumerations\Type::NOTE); $this->writeLog('Write to connection stream', \MailSo\Log\Enumerations\Type::NOTE);
\MailSo\Base\Utils::MultipleStreamWriter($rMessageAppendStream, array($this->rConnect)); \MailSo\Base\Utils::MultipleStreamWriter($rMessageAppendStream, array($this->ConnectionResource()));
$this->sendRaw(''); $this->sendRaw('');
$this->getResponse(); $this->getResponse();
@ -1085,7 +1066,7 @@ class ImapClient extends \MailSo\Net\NetClient
try { try {
$oResult = new ResponseCollection; $oResult = new ResponseCollection;
if (\is_resource($this->rConnect)) { if (\is_resource($this->ConnectionResource())) {
$sEndTag = (null === $sEndTag) ? $this->getCurrentTag() : $sEndTag; $sEndTag = (null === $sEndTag) ? $this->getCurrentTag() : $sEndTag;
while (true) { while (true) {
@ -1213,7 +1194,7 @@ class ImapClient extends \MailSo\Net\NetClient
while (0 < $iRead) while (0 < $iRead)
{ {
$sAddRead = \fread($this->rConnect, $iRead); $sAddRead = \fread($this->ConnectionResource(), $iRead);
if (false === $sAddRead) if (false === $sAddRead)
{ {
$sLiteral = false; $sLiteral = false;
@ -1636,7 +1617,7 @@ class ImapClient extends \MailSo\Net\NetClient
} }
$rImapLiteralStream = $rImapLiteralStream =
\MailSo\Base\StreamWrappers\Literal::CreateStream($this->rConnect, $iLiteralLen); \MailSo\Base\StreamWrappers\Literal::CreateStream($this->ConnectionResource(), $iLiteralLen);
$this->writeLog('Start Callback for '.$sParent.' / '.$sLiteralAtomUpperCase. $this->writeLog('Start Callback for '.$sParent.' / '.$sLiteralAtomUpperCase.
' - try to read '.$iLiteralLen.' bytes.', \MailSo\Log\Enumerations\Type::NOTE); ' - try to read '.$iLiteralLen.' bytes.', \MailSo\Log\Enumerations\Type::NOTE);
@ -1707,7 +1688,7 @@ class ImapClient extends \MailSo\Net\NetClient
$sReturn = ''; $sReturn = '';
foreach ($aParams as $mParamItem) foreach ($aParams as $mParamItem)
{ {
if (\is_array($mParamItem) && 0 < \count($mParamItem)) if (\is_array($mParamItem) && \count($mParamItem))
{ {
$sReturn .= ' ('.\trim($this->prepareParamLine($mParamItem)).')'; $sReturn .= ' ('.\trim($this->prepareParamLine($mParamItem)).')';
} }

View file

@ -65,15 +65,6 @@ class MailClient
return $this; return $this;
} }
/**
* @throws \MailSo\Net\Exceptions\Exception
*/
public function Logout() : self
{
$this->oImapClient->Logout();
return $this;
}
/** /**
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
*/ */
@ -83,14 +74,6 @@ class MailClient
return $this; return $this;
} }
/**
* @throws \MailSo\Net\Exceptions\Exception
*/
public function LogoutAndDisconnect() : self
{
return $this->Logout()->Disconnect();
}
public function IsConnected() : bool public function IsConnected() : bool
{ {
return $this->oImapClient->IsConnected(); return $this->oImapClient->IsConnected();
@ -347,7 +330,7 @@ class MailClient
*/ */
public function MessageMimeStream($mCallback, string $sFolderName, int $iIndex, bool $bIndexIsUid = true, string $sMimeIndex = '') : bool public function MessageMimeStream($mCallback, string $sFolderName, int $iIndex, bool $bIndexIsUid = true, string $sMimeIndex = '') : bool
{ {
if (!is_callable($mCallback)) if (!\is_callable($mCallback))
{ {
throw new \MailSo\Base\Exceptions\InvalidArgumentException; throw new \MailSo\Base\Exceptions\InvalidArgumentException;
} }

View file

@ -20,118 +20,72 @@ abstract class NetClient
/** /**
* @var resource * @var resource
*/ */
protected $rConnect; private $rConnect = null;
/** /**
* @var bool * @var bool
*/ */
protected $bUnreadBuffer; private $bUnreadBuffer = false;
/** /**
* @var bool * @var bool
*/ */
protected $bRunningCallback; protected $bRunningCallback = false;
/** /**
* @var string * @var string
*/ */
protected $sResponseBuffer; protected $sResponseBuffer = '';
/** /**
* @var int * @var int
*/ */
protected $iSecurityType; protected $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::NONE;
/** /**
* @var string * @var string
*/ */
protected $sConnectedHost; private $sConnectedHost = '';
/** /**
* @var int * @var int
*/ */
protected $iConnectedPort; private $iConnectedPort = 0;
/** /**
* @var bool * @var bool
*/ */
protected $bSecure; private $bSecure = false;
/** /**
* @var int * @var int
*/ */
protected $iConnectTimeOut; private $iConnectTimeOut = 10;
/** /**
* @var int * @var int
*/ */
protected $iSocketTimeOut; private $iSocketTimeOut = 10;
/** /**
* @var int * @var int
*/ */
protected $iStartConnectTime; private $iStartConnectTime = 0;
/** /**
* @var \MailSo\Log\Logger * @var \MailSo\Log\Logger
*/ */
protected $oLogger; protected $oLogger = null;
/**
* @var bool
*/
public $__AUTOLOGOUT__;
function __construct()
{
$this->rConnect = null;
$this->bUnreadBuffer = false;
$this->bRunningCallback = false;
$this->oLogger = null;
$this->__AUTOLOGOUT__ = true;
$this->sResponseBuffer = '';
$this->iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::NONE;
$this->sConnectedHost = '';
$this->iConnectedPort = 0;
$this->bSecure = false;
$this->iConnectTimeOut = 10;
$this->iSocketTimeOut = 10;
$this->Clear();
}
public function __destruct() public function __destruct()
{ {
try try
{ {
if ($this->__AUTOLOGOUT__) $this->Disconnect();
{
$this->LogoutAndDisconnect();
}
else
{
$this->Disconnect();
}
} }
catch (\Throwable $oException) {} catch (\Throwable $oException) {}
} }
public function Clear() : void
{
$this->sResponseBuffer = '';
$this->sConnectedHost = '';
$this->iConnectedPort = 0;
$this->iStartConnectTime = 0;
$this->bSecure = false;
}
public function GetConnectedHost() : string public function GetConnectedHost() : string
{ {
return $this->sConnectedHost; return $this->sConnectedHost;
@ -171,7 +125,7 @@ abstract class NetClient
bool $bVerifySsl = false, bool $bAllowSelfSigned = true, bool $bVerifySsl = false, bool $bAllowSelfSigned = true,
string $sClientCert = '') : void string $sClientCert = '') : void
{ {
if (!strlen(\trim($sServerName)) || !\MailSo\Base\Validator::PortInt($iPort)) if (!\strlen(\trim($sServerName)) || !\MailSo\Base\Validator::PortInt($iPort))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -213,9 +167,6 @@ abstract class NetClient
$this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"', $this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"',
\MailSo\Log\Enumerations\Type::NOTE); \MailSo\Log\Enumerations\Type::NOTE);
// $this->rConnect = \fsockopen($this->sConnectedHost, $this->iConnectedPort,
// $iErrorNo, $sErrorStr, $this->iConnectTimeOut);
$bVerifySsl = !!$bVerifySsl; $bVerifySsl = !!$bVerifySsl;
$bAllowSelfSigned = $bVerifySsl ? !!$bAllowSelfSigned : true; $bAllowSelfSigned = $bVerifySsl ? !!$bAllowSelfSigned : true;
$aStreamContextSettings = array( $aStreamContextSettings = array(
@ -251,10 +202,10 @@ abstract class NetClient
\restore_error_handler(); \restore_error_handler();
$this->writeLog('Connected ('.(\is_resource($this->rConnect) ? 'success' : 'unsuccess').')', $this->writeLog('Connect ('.($this->rConnect ? 'success' : 'failed').')',
\MailSo\Log\Enumerations\Type::NOTE); \MailSo\Log\Enumerations\Type::NOTE);
if (!\is_resource($this->rConnect)) if (!$this->rConnect)
{ {
$this->writeLogException( $this->writeLogException(
new Exceptions\SocketCanNotConnectToHostException( new Exceptions\SocketCanNotConnectToHostException(
@ -278,7 +229,7 @@ abstract class NetClient
public function EnableCrypto() public function EnableCrypto()
{ {
$bError = true; $bError = true;
if (\is_resource($this->rConnect) && if ($this->rConnect &&
\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_socket_enable_crypto')) \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_socket_enable_crypto'))
{ {
switch (true) switch (true)
@ -304,16 +255,24 @@ abstract class NetClient
} }
} }
/**
* @throws \MailSo\Net\Exceptions\Exception
*/
public function Disconnect() : void public function Disconnect() : void
{ {
if (\is_resource($this->rConnect)) if ($this->rConnect)
{ {
if (!$this->bUnreadBuffer && !$this->bRunningCallback)
{
$this->Logout();
}
$bResult = \fclose($this->rConnect); $bResult = \fclose($this->rConnect);
$this->writeLog('Disconnected from "'.$this->sConnectedHost.':'.$this->iConnectedPort.'" ('. $this->writeLog('Disconnected from "'.$this->sConnectedHost.':'.$this->iConnectedPort.'" ('.
(($bResult) ? 'success' : 'unsuccess').')', \MailSo\Log\Enumerations\Type::NOTE); (($bResult) ? 'success' : 'unsuccess').')', \MailSo\Log\Enumerations\Type::NOTE);
if (0 !== $this->iStartConnectTime) if ($this->iStartConnectTime)
{ {
$this->writeLog((\microtime(true) - $this->iStartConnectTime).' (net session)', $this->writeLog((\microtime(true) - $this->iStartConnectTime).' (net session)',
\MailSo\Log\Enumerations\Type::TIME); \MailSo\Log\Enumerations\Type::TIME);
@ -325,30 +284,19 @@ abstract class NetClient
} }
} }
/** abstract public function Logout() : void;
* @throws \MailSo\Net\Exceptions\Exception
*/
public function LogoutAndDisconnect() : void
{
if (\method_exists($this, 'Logout') && !$this->bUnreadBuffer && !$this->bRunningCallback)
{
$this->Logout();
}
$this->Disconnect();
}
public function IsConnected(bool $bThrowExceptionOnFalse = false) : bool public function IsConnected(bool $bThrowExceptionOnFalse = false) : bool
{ {
$bResult = \is_resource($this->rConnect); if ($this->rConnect) {
if (!$bResult && $bThrowExceptionOnFalse) return true;
{ }
if ($bThrowExceptionOnFalse) {
$this->writeLogException( $this->writeLogException(
new Exceptions\SocketConnectionDoesNotAvailableException, new Exceptions\SocketConnectionDoesNotAvailableException,
\MailSo\Log\Enumerations\Type::ERROR, true); \MailSo\Log\Enumerations\Type::ERROR, true);
} }
return false;
return $bResult;
} }
/** /**
@ -361,7 +309,7 @@ abstract class NetClient
public function StreamContextParams() : array public function StreamContextParams() : array
{ {
return \is_resource($this->rConnect) && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_get_options') return $this->rConnect && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_get_options')
? \stream_context_get_params($this->rConnect) : false; ? \stream_context_get_params($this->rConnect) : false;
} }

View file

@ -20,34 +20,27 @@ class ManageSieveClient extends \MailSo\Net\NetClient
/** /**
* @var bool * @var bool
*/ */
private $bIsLoggined; private $bIsLoggined = false;
/** /**
* @var array * @var array
*/ */
private $aCapa; private $aCapa = array();
/** /**
* @var int * @var array
*/ */
private $iRequestTime; private $aAuth = array();
/**
* @var array
*/
private $aModules = array();
/** /**
* @var bool * @var bool
*/ */
public $__USE_INITIAL_AUTH_PLAIN_COMMAND; public $__USE_INITIAL_AUTH_PLAIN_COMMAND = true;
function __construct()
{
parent::__construct();
$this->bIsLoggined = false;
$this->iRequestTime = 0;
$this->aCapa = array();
$this->aModules = array();
$this->__USE_INITIAL_AUTH_PLAIN_COMMAND = true;
}
public function IsSupported(string $sCapa) : bool public function IsSupported(string $sCapa) : bool
{ {
@ -78,8 +71,6 @@ class ManageSieveClient extends \MailSo\Net\NetClient
int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT,
bool $bVerifySsl = false, bool $bAllowSelfSigned = true, string $sClientCert = '') : void bool $bVerifySsl = false, bool $bAllowSelfSigned = true, string $sClientCert = '') : void
{ {
$this->iRequestTime = \microtime(true);
parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl, $bAllowSelfSigned); parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl, $bAllowSelfSigned);
$aResponse = $this->parseResponse(); $aResponse = $this->parseResponse();
@ -111,7 +102,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
*/ */
public function Login(string $sLogin, string $sPassword, string $sLoginAuthKey = '') : self public function Login(string $sLogin, string $sPassword, string $sLoginAuthKey = '') : self
{ {
if (!strlen(\trim($sLogin)) || !strlen(\trim($sPassword))) if (!\strlen(\trim($sLogin)) || !\strlen(\trim($sPassword)))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -120,7 +111,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
if ($this->IsSupported('SASL')) if ($this->IsSupported('SASL'))
{ {
// $encrypted = !empty(\stream_get_meta_data($this->rConnect)['crypto']); // $encrypted = !empty(\stream_get_meta_data($this->ConnectionResource())['crypto']);
$type = ''; $type = '';
$types = [ $types = [
// 'SCRAM-SHA-256' => 1, // !$encrypted // 'SCRAM-SHA-256' => 1, // !$encrypted
@ -209,15 +200,13 @@ class ManageSieveClient extends \MailSo\Net\NetClient
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Sieve\Exceptions\NegativeResponseException * @throws \MailSo\Sieve\Exceptions\NegativeResponseException
*/ */
public function Logout() : self public function Logout() : void
{ {
if ($this->bIsLoggined) if ($this->bIsLoggined)
{ {
$this->sendRequestWithCheck('LOGOUT'); $this->sendRequestWithCheck('LOGOUT');
$this->bIsLoggined = false; $this->bIsLoggined = false;
} }
return $this;
} }
/** /**
@ -281,7 +270,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
$this->validateResponse($aResponse); $this->validateResponse($aResponse);
$sScript = ''; $sScript = '';
if (0 < \count($aResponse)) if (\count($aResponse))
{ {
if ('{' === $aResponse[0][0]) if ('{' === $aResponse[0][0])
{ {
@ -452,7 +441,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
*/ */
private function sendRequest(string $sRequest) : void private function sendRequest(string $sRequest) : void
{ {
if (!strlen(\trim($sRequest))) if (!\strlen(\trim($sRequest)))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -502,8 +491,6 @@ class ManageSieveClient extends \MailSo\Net\NetClient
private function parseResponse() : array private function parseResponse() : array
{ {
$this->iRequestTime = \microtime(true);
$aResult = array(); $aResult = array();
do do
{ {
@ -526,9 +513,6 @@ class ManageSieveClient extends \MailSo\Net\NetClient
} }
while (true); while (true);
$this->writeLog((\microtime(true) - $this->iRequestTime),
\MailSo\Log\Enumerations\Type::TIME);
return $aResult; return $aResult;
} }

View file

@ -20,73 +20,51 @@ class SmtpClient extends \MailSo\Net\NetClient
/** /**
* @var bool * @var bool
*/ */
private $bHelo; private $bHelo = false;
/** /**
* @var bool * @var bool
*/ */
private $bRcpt; private $bRcpt = false;
/** /**
* @var bool * @var bool
*/ */
private $bMail; private $bMail = false;
/** /**
* @var bool * @var bool
*/ */
private $bData; private $bData = false;
/** /**
* @var array * @var array
*/ */
private $aAuthTypes; private $aAuthTypes = array();
/** /**
* @var array * @var array
*/ */
private $aCapa; private $aCapa = array();
/** /**
* @var int * @var int
*/ */
private $iSizeCapaValue; private $iSizeCapaValue = 0;
/**
* @var int
*/
private $iRequestTime;
/** /**
* @var array * @var array
*/ */
private $aResults; private $aResults = array();
function __construct()
{
parent::__construct();
$this->aAuthTypes = array();
$this->iRequestTime = 0;
$this->iSizeCapaValue = 0;
$this->aResults = array();
$this->aCapa = array();
$this->bHelo = false;
$this->bRcpt = false;
$this->bMail = false;
$this->bData = false;
}
public function IsSupported(string $sCapa) : bool public function IsSupported(string $sCapa) : bool
{ {
return in_array(strtoupper($sCapa), $this->aCapa); return \in_array(\strtoupper($sCapa), $this->aCapa);
} }
public function IsAuthSupported(string $sAuth) : bool public function IsAuthSupported(string $sAuth) : bool
{ {
return in_array(strtoupper($sAuth), $this->aAuthTypes); return \in_array(\strtoupper($sAuth), $this->aAuthTypes);
} }
public function HasSupportedAuth() : bool public function HasSupportedAuth() : bool
@ -127,8 +105,6 @@ class SmtpClient extends \MailSo\Net\NetClient
bool $bVerifySsl = false, bool $bAllowSelfSigned = true, bool $bVerifySsl = false, bool $bAllowSelfSigned = true,
string $sClientCert = '', string $sEhloHost = '[127.0.0.1]') : void string $sClientCert = '', string $sEhloHost = '[127.0.0.1]') : void
{ {
$this->iRequestTime = microtime(true);
parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl, $bAllowSelfSigned); parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl, $bAllowSelfSigned);
$this->validateResponse(220); $this->validateResponse(220);
@ -145,7 +121,7 @@ class SmtpClient extends \MailSo\Net\NetClient
{ {
$sLogin = \MailSo\Base\Utils::IdnToAscii(\MailSo\Base\Utils::Trim($sLogin)); $sLogin = \MailSo\Base\Utils::IdnToAscii(\MailSo\Base\Utils::Trim($sLogin));
// $encrypted = !empty(\stream_get_meta_data($this->rConnect)['crypto']); // $encrypted = !empty(\stream_get_meta_data($this->ConnectionResource())['crypto']);
$type = ''; $type = '';
$types = [ $types = [
'SCRAM-SHA-256' => 1, // !$encrypted 'SCRAM-SHA-256' => 1, // !$encrypted
@ -162,7 +138,7 @@ class SmtpClient extends \MailSo\Net\NetClient
} }
if (!$type) { if (!$type) {
\trigger_error("SMTP {$this->sConnectedHost} no supported AUTH options. Disable login" . ($this->IsSupported('STARTTLS') ? ' or try with STARTTLS' : '')); \trigger_error("SMTP {$this->GetConnectedHost()} no supported AUTH options. Disable login" . ($this->IsSupported('STARTTLS') ? ' or try with STARTTLS' : ''));
$this->writeLogException( $this->writeLogException(
new \MailSo\Smtp\Exceptions\LoginBadMethodException, new \MailSo\Smtp\Exceptions\LoginBadMethodException,
\MailSo\Log\Enumerations\Type::NOTICE, true); \MailSo\Log\Enumerations\Type::NOTICE, true);
@ -315,7 +291,7 @@ class SmtpClient extends \MailSo\Net\NetClient
*/ */
public function Data(string $sData) : self public function Data(string $sData) : self
{ {
if (!strlen(\trim($sData))) if (!\strlen(\trim($sData)))
{ {
throw new \MailSo\Base\Exceptions\InvalidArgumentException; throw new \MailSo\Base\Exceptions\InvalidArgumentException;
} }
@ -433,7 +409,7 @@ class SmtpClient extends \MailSo\Net\NetClient
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Smtp\Exceptions\Exception * @throws \MailSo\Smtp\Exceptions\Exception
*/ */
public function Logout() : self public function Logout() : void
{ {
if ($this->IsConnected()) if ($this->IsConnected())
{ {
@ -444,8 +420,6 @@ class SmtpClient extends \MailSo\Net\NetClient
$this->bMail = false; $this->bMail = false;
$this->bRcpt = false; $this->bRcpt = false;
$this->bData = false; $this->bData = false;
return $this;
} }
private function preLoginStartTLSAndEhloProcess(string $sEhloHost) : void private function preLoginStartTLSAndEhloProcess(string $sEhloHost) : void
@ -483,7 +457,7 @@ class SmtpClient extends \MailSo\Net\NetClient
*/ */
private function sendRequest(string $sCommand, string $sAddToCommand = '', bool $bSecureLog = false) : void private function sendRequest(string $sCommand, string $sAddToCommand = '', bool $bSecureLog = false) : void
{ {
if (!strlen(\trim($sCommand))) if (!\strlen(\trim($sCommand)))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException, new \MailSo\Base\Exceptions\InvalidArgumentException,
@ -497,7 +471,6 @@ class SmtpClient extends \MailSo\Net\NetClient
$sFakeCommand = ($bSecureLog) ? '********' : ''; $sFakeCommand = ($bSecureLog) ? '********' : '';
$this->iRequestTime = \microtime(true);
$this->sendRaw($sRealCommand, true, $sFakeCommand); $this->sendRaw($sRealCommand, true, $sFakeCommand);
} }
@ -612,7 +585,7 @@ class SmtpClient extends \MailSo\Net\NetClient
$this->writeLogException( $this->writeLogException(
new Exceptions\NegativeResponseException($this->aResults, new Exceptions\NegativeResponseException($this->aResults,
('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim( ('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim(
(0 < \count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : ''). (\count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : '').
$this->sResponseBuffer)), \MailSo\Log\Enumerations\Type::ERROR, true); $this->sResponseBuffer)), \MailSo\Log\Enumerations\Type::ERROR, true);
} }
} }
@ -621,16 +594,13 @@ class SmtpClient extends \MailSo\Net\NetClient
$this->writeLogException( $this->writeLogException(
new Exceptions\ResponseException($this->aResults, new Exceptions\ResponseException($this->aResults,
('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim( ('' === $sErrorPrefix ? '' : $sErrorPrefix.': ').\trim(
(0 < \count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : ''). (\count($this->aResults) ? \implode("\r\n", $this->aResults)."\r\n" : '').
$this->sResponseBuffer)), \MailSo\Log\Enumerations\Type::ERROR, true); $this->sResponseBuffer)), \MailSo\Log\Enumerations\Type::ERROR, true);
} }
$this->aResults[] = $this->sResponseBuffer; $this->aResults[] = $this->sResponseBuffer;
} }
while ('-' === \substr($aParts[1], 0, 1)); while ('-' === \substr($aParts[1], 0, 1));
$this->writeLog((microtime(true) - $this->iRequestTime),
\MailSo\Log\Enumerations\Type::TIME);
} }
protected function getLogName() : string protected function getLogName() : string

View file

@ -289,7 +289,7 @@ class Actions
{ {
try { try {
if ($this->MailClient()->IsLoggined()) { if ($this->MailClient()->IsLoggined()) {
$this->MailClient()->LogoutAndDisconnect(); $this->MailClient()->Disconnect();
} }
} catch (\Throwable $oException) { } catch (\Throwable $oException) {
unset($oException); unset($oException);
@ -1560,7 +1560,7 @@ class Actions
$iResult = $oMailClient->InboxUnreadCount(); $iResult = $oMailClient->InboxUnreadCount();
$oMailClient->LogoutAndDisconnect(); $oMailClient->Disconnect();
} catch (\Throwable $oException) { } catch (\Throwable $oException) {
$this->Logger()->WriteException($oException); $this->Logger()->WriteException($oException);
} }

View file

@ -345,13 +345,9 @@ trait Admin
$bSieveResult = false; $bSieveResult = false;
$sSieveErrorDesc = ''; $sSieveErrorDesc = '';
$iImapTime = 0;
$iSmtpTime = 0;
$iSieveTime = 0;
$iConnectionTimeout = 5; $iConnectionTimeout = 5;
$oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de'); $oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'test.example.com');
if ($oDomain) if ($oDomain)
{ {
try try
@ -367,7 +363,6 @@ trait Admin
$this->Config()->Get('ssl', 'client_cert', '') $this->Config()->Get('ssl', 'client_cert', '')
); );
$iImapTime = \microtime(true) - $iTime;
$oImapClient->Disconnect(); $oImapClient->Disconnect();
$bImapResult = true; $bImapResult = true;
} }
@ -409,7 +404,6 @@ trait Admin
'', \MailSo\Smtp\SmtpClient::EhloHelper() '', \MailSo\Smtp\SmtpClient::EhloHelper()
); );
$iSmtpTime = \microtime(true) - $iTime;
$oSmtpClient->Disconnect(); $oSmtpClient->Disconnect();
$bSmtpResult = true; $bSmtpResult = true;
} }
@ -444,7 +438,6 @@ trait Admin
!!$this->Config()->Get('ssl', 'allow_self_signed', true) !!$this->Config()->Get('ssl', 'allow_self_signed', true)
); );
$iSieveTime = \microtime(true) - $iTime;
$oSieveClient->Disconnect(); $oSieveClient->Disconnect();
$bSieveResult = true; $bSieveResult = true;
} }

View file

@ -715,7 +715,7 @@ trait Messages
$oSmtpClient->SetLogger($this->Logger()); $oSmtpClient->SetLogger($this->Logger());
$oSmtpClient->SetTimeOuts(10, (int) \RainLoop\Api::Config()->Get('labs', 'smtp_timeout', 60)); $oSmtpClient->SetTimeOuts(10, (int) \RainLoop\Api::Config()->Get('labs', 'smtp_timeout', 60));
$bLoggined = $oAccount->OutConnectAndLoginHelper( $oAccount->OutConnectAndLoginHelper(
$this->Plugins(), $oSmtpClient, $this->Config(), null, $bUsePhpMail $this->Plugins(), $oSmtpClient, $this->Config(), null, $bUsePhpMail
); );
@ -794,11 +794,6 @@ trait Messages
$oSmtpClient->DataWithStream($rMessageStream); $oSmtpClient->DataWithStream($rMessageStream);
if ($bLoggined)
{
$oSmtpClient->Logout();
}
$oSmtpClient->Disconnect(); $oSmtpClient->Disconnect();
} }
} }

View file

@ -90,7 +90,7 @@ class Domain extends AbstractProvider
return $this->oDriver->Count($sSearch); return $this->oDriver->Count($sSearch);
} }
public function LoadOrCreateNewFromAction(\RainLoop\Actions $oActions, string $sNameForTest = '') : ?\RainLoop\Model\Domain public function LoadOrCreateNewFromAction(\RainLoop\Actions $oActions, string $sNameForTest = null) : ?\RainLoop\Model\Domain
{ {
$oDomain = null; $oDomain = null;
@ -98,12 +98,12 @@ class Domain extends AbstractProvider
{ {
$sName = (string) $oActions->GetActionParam('Name', ''); $sName = (string) $oActions->GetActionParam('Name', '');
if (0 < \strlen($sName) && 0 < \strlen($sNameForTest) && !\str_contains($sName, '*')) if (\strlen($sName) && $sNameForTest && !\str_contains($sName, '*'))
{ {
$sNameForTest = ''; $sNameForTest = null;
} }
if (0 < strlen($sName) || 0 < strlen($sNameForTest)) if (\strlen($sName) || $sNameForTest)
{ {
$bCreate = '1' === (string) $oActions->GetActionParam('Create', '0'); $bCreate = '1' === (string) $oActions->GetActionParam('Create', '0');
$sIncHost = (string) $oActions->GetActionParam('IncHost', ''); $sIncHost = (string) $oActions->GetActionParam('IncHost', '');
@ -123,7 +123,7 @@ class Domain extends AbstractProvider
$bOutUsePhpMail = '1' === (string) $oActions->GetActionParam('OutUsePhpMail', '0'); $bOutUsePhpMail = '1' === (string) $oActions->GetActionParam('OutUsePhpMail', '0');
$sWhiteList = (string) $oActions->GetActionParam('WhiteList', ''); $sWhiteList = (string) $oActions->GetActionParam('WhiteList', '');
$oDomain = 0 < strlen($sNameForTest) ? null : $this->Load($sName); $oDomain = $sNameForTest ? null : $this->Load($sName);
if ($oDomain instanceof \RainLoop\Model\Domain) if ($oDomain instanceof \RainLoop\Model\Domain)
{ {
if ($bCreate) if ($bCreate)
@ -141,69 +141,7 @@ class Domain extends AbstractProvider
} }
else else
{ {
$oDomain = new \RainLoop\Model\Domain(0 < strlen($sNameForTest) ? $sNameForTest : $sName, $oDomain = new \RainLoop\Model\Domain($sNameForTest ?: $sName,
$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutSetSender, $bOutUsePhpMail,
$sWhiteList);
}
}
}
return $oDomain;
}
public function CreateNewAliasFromAction(\RainLoop\Actions $oActions, string $sNameForTest = '') : ?\RainLoop\Model\Domain
{
$oDomain = null;
if ($this->bAdmin)
{
$bCreate = '1' === (string) $oActions->GetActionParam('Create', '0');
$sName = (string) $oActions->GetActionParam('Name', '');
$sIncHost = (string) $oActions->GetActionParam('IncHost', '');
$iIncPort = (int) $oActions->GetActionParam('IncPort', 143);
$iIncSecure = (int) $oActions->GetActionParam('IncSecure', \MailSo\Net\Enumerations\ConnectionSecurityType::NONE);
$bIncShortLogin = '1' === (string) $oActions->GetActionParam('IncShortLogin', '0');
$bUseSieve = '1' === (string) $oActions->GetActionParam('UseSieve', '0');
$sSieveHost = (string) $oActions->GetActionParam('SieveHost', '');
$iSievePort = (int) $oActions->GetActionParam('SievePort', 4190);
$iSieveSecure = (int) $oActions->GetActionParam('SieveSecure', \MailSo\Net\Enumerations\ConnectionSecurityType::NONE);
$sOutHost = (string) $oActions->GetActionParam('OutHost', '');
$iOutPort = (int) $oActions->GetActionParam('OutPort', 25);
$iOutSecure = (int) $oActions->GetActionParam('OutSecure', \MailSo\Net\Enumerations\ConnectionSecurityType::NONE);
$bOutShortLogin = '1' === (string) $oActions->GetActionParam('OutShortLogin', '0');
$bOutAuth = '1' === (string) $oActions->GetActionParam('OutAuth', '1');
$bOutSetSender = '1' === (string) $oActions->GetActionParam('OutSetSender', '0');
$bOutUsePhpMail = '1' === (string) $oActions->GetActionParam('OutUsePhpMail', '0');
$sWhiteList = (string) $oActions->GetActionParam('WhiteList', '');
if (0 < \strlen($sName) && 0 < strlen($sNameForTest) && false === \strpos($sName, '*'))
{
$sNameForTest = '';
}
if (0 < strlen($sName) || 0 < strlen($sNameForTest))
{
$oDomain = 0 < strlen($sNameForTest) ? null : $this->Load($sName);
if ($oDomain instanceof \RainLoop\Model\Domain)
{
if ($bCreate)
{
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DomainAlreadyExists);
}
else
{
$oDomain->UpdateInstance(
$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutSetSender, $bOutUsePhpMail,
$sWhiteList);
}
}
else
{
$oDomain = new \RainLoop\Model\Domain(0 < strlen($sNameForTest) ? $sNameForTest : $sName,
$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin, $sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure, $bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutSetSender, $bOutUsePhpMail, $sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutSetSender, $bOutUsePhpMail,

View file

@ -60,7 +60,7 @@ class SieveStorage implements FiltersInterface
); );
} }
$oSieveClient->LogoutAndDisconnect(); $oSieveClient->Disconnect();
if (!isset($aList[self::SIEVE_FILE_NAME])) { if (!isset($aList[self::SIEVE_FILE_NAME])) {
$aScripts[self::SIEVE_FILE_NAME] = array( $aScripts[self::SIEVE_FILE_NAME] = array(
@ -97,7 +97,7 @@ class SieveStorage implements FiltersInterface
} else { } else {
$oSieveClient->PutScript($sScriptName, $sRaw); $oSieveClient->PutScript($sScriptName, $sRaw);
} }
$oSieveClient->LogoutAndDisconnect(); $oSieveClient->Disconnect();
return true; return true;
} }
return false; return false;