Cleanup MailSo Validator

Cleanup removed features
This commit is contained in:
djmaze 2020-03-20 14:53:12 +01:00
parent 8fe8369b2a
commit 1009398d0c
15 changed files with 65 additions and 192 deletions

View file

@ -87,7 +87,7 @@ class Crypt
private static function XxteaDecrypt(string $sEncryptedString, string $sKey) : string
{
if (\is_callable('xxtea_decrypt')) {
return xxtea_decrypt($sString, $sKey);
return xxtea_decrypt($sEncryptedString, $sKey);
}
$aV = self::str2long($sEncryptedString, false);
@ -166,6 +166,6 @@ class Crypt
private static function int32(int $iN) : int
{
return ($n & 0xffffffff);
return $iN & 0xffffffff;
}
}

View file

@ -17,51 +17,13 @@ namespace MailSo\Base;
*/
class Validator
{
public static function EmailString(string $sEmail, array $aAllowedInternalDomains = array('localhost')) : bool
{
$bResult = false;
if (\MailSo\Base\Validator::NotEmptyString($sEmail, true))
{
$bResult = false !== \filter_var($sEmail, FILTER_VALIDATE_EMAIL);
if (!$bResult)
{
$aSplit = \explode("@", $sEmail);
$bResult = 2 === \count($aSplit) &&
\in_array($aSplit[1], $aAllowedInternalDomains) &&
false !== \filter_var(($aSplit[0].'@example.com'), FILTER_VALIDATE_EMAIL);
}
}
return $bResult;
}
public static function SimpleEmailString(string $sEmail) : bool
{
return \MailSo\Base\Validator::NotEmptyString($sEmail, true) &&
!!\preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\.\+\-_]*@[a-zA-Z0-9][a-zA-Z0-9\.\+\-_]*$/', $sEmail);
}
public static function NotEmptyString(string $sString, bool $bTrim = false) : bool
{
return \is_string($sString) &&
(0 < \strlen($bTrim ? \trim($sString) : $sString));
}
public static function NotEmptyArray(array $aList) : bool
{
return \is_array($aList) && 0 < \count($aList);
}
public static function RangeInt(int $iNumber, int $iMin = null, int $iMax = null) : bool
{
return \is_int($iNumber) &&
(null !== $iMin && $iNumber >= $iMin || null === $iMin) &&
(null !== $iMax && $iNumber <= $iMax || null === $iMax);
return (null === $iMin || $iNumber >= $iMin) && (null === $iMax || $iNumber <= $iMax);
}
public static function PortInt(int $iPort) : bool
{
return \MailSo\Base\Validator::RangeInt($iPort, 0, 65535);
return static::RangeInt($iPort, 0, 65535);
}
}

View file

@ -21,11 +21,11 @@ class Hooks
*/
static $aCallbacks = array();
static public function Run(string $sName, array $aArg = array())
static public function Run(string $sName, array $aArg = array()) : void
{
if (isset(\MailSo\Hooks::$aCallbacks[$sName]))
if (isset(static::$aCallbacks[$sName]))
{
foreach (\MailSo\Hooks::$aCallbacks[$sName] as $mCallback)
foreach (static::$aCallbacks[$sName] as $mCallback)
{
\call_user_func_array($mCallback, $aArg);
}
@ -35,16 +35,16 @@ class Hooks
/**
* @param mixed $mCallback
*/
static public function Add(string $sName, $mCallback)
static public function Add(string $sName, $mCallback) : void
{
if (\is_callable($mCallback))
{
if (!isset(\MailSo\Hooks::$aCallbacks[$sName]))
if (!isset(static::$aCallbacks[$sName]))
{
\MailSo\Hooks::$aCallbacks[$sName] = array();
static::$aCallbacks[$sName] = array();
}
\MailSo\Hooks::$aCallbacks[$sName][] = $mCallback;
static::$aCallbacks[$sName][] = $mCallback;
}
}
}

View file

@ -172,8 +172,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function Login(string $sLogin, string $sPassword, string $sProxyAuthUser = '',
bool $bUseAuthPlainIfSupported = true, bool $bUseAuthCramMd5IfSupported = true) : self
{
if (!\MailSo\Base\Validator::NotEmptyString($sLogin, true) ||
!\MailSo\Base\Validator::NotEmptyString($sPassword, true))
if (!strlen(\trim($sLogin)) || !strlen(\trim($sPassword)))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),
@ -341,7 +340,7 @@ class ImapClient extends \MailSo\Net\NetClient
*/
public function IsSupported(string $sExtentionName) : bool
{
$bResult = \MailSo\Base\Validator::NotEmptyString($sExtentionName, true);
$bResult = strlen(\trim($sExtentionName));
if ($bResult && null === $this->aCapabilityItems)
{
$this->aCapabilityItems = $this->Capability();
@ -762,7 +761,7 @@ class ImapClient extends \MailSo\Net\NetClient
}
}
if (!\MailSo\Base\Validator::NotEmptyString($sFolderName, true))
if (!strlen(\trim($sFolderName)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
@ -821,7 +820,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array
{
$sIndexRange = (string) $sIndexRange;
if (!\MailSo\Base\Validator::NotEmptyString($sIndexRange, true))
if (!strlen(\trim($sIndexRange)))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),
@ -928,7 +927,7 @@ class ImapClient extends \MailSo\Net\NetClient
public function MessageSimpleSort(array $aSortTypes, string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array
{
$sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = !\MailSo\Base\Validator::NotEmptyString($sSearchCriterias, true) || '*' === $sSearchCriterias
$sSearchCriterias = !strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias;
if (!\is_array($aSortTypes) || 0 === \count($aSortTypes))
@ -1252,7 +1251,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
{
$sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = !\MailSo\Base\Validator::NotEmptyString($sSearchCriterias, true) || '*' === $sSearchCriterias
$sSearchCriterias = !strlen(\trim($sSearchCriterias)) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias;
$sThreadType = '';
@ -1390,8 +1389,8 @@ class ImapClient extends \MailSo\Net\NetClient
*/
public function MessageStoreFlag(string $sIndexRange, bool $bIndexIsUid, array $aInputStoreItems, string $sStoreAction) : self
{
if (!\MailSo\Base\Validator::NotEmptyString($sIndexRange, true) ||
!\MailSo\Base\Validator::NotEmptyString($sStoreAction, true) ||
if (!strlen(\trim($sIndexRange)) ||
!strlen(\trim($sStoreAction)) ||
0 === \count($aInputStoreItems))
{
return false;
@ -1462,7 +1461,8 @@ class ImapClient extends \MailSo\Net\NetClient
*/
public function SendRequest(string $sCommand, array $aParams = array(), bool $bBreakOnLiteral = false) : string
{
if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true) || !\is_array($aParams))
$sCommand = \trim($sCommand);
if (!\strlen($sCommand))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),
@ -1473,14 +1473,11 @@ class ImapClient extends \MailSo\Net\NetClient
$sTag = $this->getNewTag();
$sCommand = \trim($sCommand);
$sRealCommand = $sTag.' '.$sCommand.$this->prepearParamLine($aParams);
$sRealCommand = $sTag.' '.$sCommand.$this->prepareParamLine($aParams);
$sFakeCommand = '';
$aFakeParams = $this->secureRequestParams($sCommand, $aParams);
if (null !== $aFakeParams)
{
$sFakeCommand = $sTag.' '.$sCommand.$this->prepearParamLine($aFakeParams);
if ($aFakeParams = $this->secureRequestParams($sCommand, $aParams)) {
$sFakeCommand = $sTag.' '.$sCommand.$this->prepareParamLine($aFakeParams);
}
$this->aTagTimeouts[$sTag] = \microtime(true);
@ -1505,19 +1502,11 @@ class ImapClient extends \MailSo\Net\NetClient
private function secureRequestParams(string $sCommand, array $aParams) : ?array
{
$aResult = null;
switch ($sCommand)
{
case 'LOGIN':
$aResult = $aParams;
if (\is_array($aResult) && 2 === count($aResult))
{
$aResult[1] = '"********"';
}
break;
if ('LOGIN' === $sCommand && isset($aParams[1])) {
$aParams[1] = '"********"';
return $aParams;
}
return $aResult;
return null;
}
/**
@ -2241,7 +2230,7 @@ class ImapClient extends \MailSo\Net\NetClient
return $bResult;
}
private function prepearParamLine(array $aParams = array()) : string
private function prepareParamLine(array $aParams = array()) : string
{
$sReturn = '';
if (\is_array($aParams) && 0 < \count($aParams))
@ -2250,7 +2239,7 @@ class ImapClient extends \MailSo\Net\NetClient
{
if (\is_array($mParamItem) && 0 < \count($mParamItem))
{
$sReturn .= ' ('.\trim($this->prepearParamLine($mParamItem)).')';
$sReturn .= ' ('.\trim($this->prepareParamLine($mParamItem)).')';
}
else if (\is_string($mParamItem))
{

View file

@ -175,7 +175,7 @@ abstract class Driver
{
return \ltrim(
($this->bTimePrefix ? '['.$sTimePrefix.']' : '').
($this->bGuidPrefix ? '['.\MailSo\Log\Logger::Guid().']' : '').
($this->bGuidPrefix ? '['.Logger::Guid().']' : '').
($this->bTypedPrefix ? ' '.$this->getTypedPrefix($iType, $sName) : '')
).$sDesc;
}
@ -188,7 +188,7 @@ abstract class Driver
protected function getTimeWithMicroSec() : string
{
$aMicroTimeItems = \explode(' ', \microtime());
return \MailSo\Log\Logger::DateHelper($this->sDatePattern, $this->sTimeOffset, $aMicroTimeItems[1]).'.'.
return Logger::DateHelper($this->sDatePattern, $this->sTimeOffset, $aMicroTimeItems[1]).'.'.
\str_pad((int) ($aMicroTimeItems[0] * 1000), 3, '0', STR_PAD_LEFT);
}

View file

@ -270,7 +270,7 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function Message(string $sFolderName, int $iIndex, bool $bIndexIsUid = true, ?\MailSo\Cache\CacheClient $oCacher = null, int $iBodyTextLimit = 0) : ?\MailSo\Mail\Message
public function Message(string $sFolderName, int $iIndex, bool $bIndexIsUid = true, ?\MailSo\Cache\CacheClient $oCacher = null, int $iBodyTextLimit = 0) : ?Message
{
if (!\MailSo\Base\Validator::RangeInt($iIndex, 1))
{
@ -346,7 +346,7 @@ class MailClient
$aFetchResponse = $this->oImapClient->Fetch($aFetchItems, $iIndex, $bIndexIsUid);
if (0 < \count($aFetchResponse))
{
$oMessage = \MailSo\Mail\Message::NewFetchResponseInstance(
$oMessage = Message::NewFetchResponseInstance(
$sFolderName, $aFetchResponse[0], $oBodyStructure);
}
@ -1482,7 +1482,7 @@ class MailClient
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageListByRequestIndexOrUids(\MailSo\Mail\MessageCollection $oMessageCollection, array $aRequestIndexOrUids, bool $bIndexAsUid, bool $bSimple = false)
public function MessageListByRequestIndexOrUids(MessageCollection $oMessageCollection, array $aRequestIndexOrUids, bool $bIndexAsUid, bool $bSimple = false)
{
if (\is_array($aRequestIndexOrUids) && 0 < \count($aRequestIndexOrUids))
{
@ -1617,7 +1617,7 @@ class MailClient
public function MessageList(string $sFolderName, int $iOffset = 0, int $iLimit = 10,
string $sSearch = '', string $sPrevUidNext = '', ?\MailSo\Cache\CacheClient $oCacher = null,
bool $bUseSortIfSupported = false, bool $bUseThreadSortIfSupported = false,
string $sThreadUid = '', string $sFilter = '')
string $sThreadUid = '', string $sFilter = '') : MessageCollection
{
$sFilter = \trim($sFilter);
$sSearch = \trim($sSearch);
@ -1905,7 +1905,7 @@ class MailClient
$iCountLimit = $iForeachLimit;
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder \MailSo\Mail\Folder */ $oFolder)
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder Folder */ $oFolder)
{
// mandatory folders
if ($oFolder && \in_array(\str_replace(' ', '', \strtolower($oFolder->NameRaw())), $aFilteredNames))
@ -1915,7 +1915,7 @@ class MailClient
}
}
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder \MailSo\Mail\Folder */ $oFolder)
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder Folder */ $oFolder)
{
// subscribed folders
if ($oFolder && $oFolder->IsSubscribed())
@ -1943,7 +1943,7 @@ class MailClient
if ($iOptimizationLimit >= \count($aNewMailFoldersHelper))
{
// name filter
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder \MailSo\Mail\Folder */ $oFolder)
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder Folder */ $oFolder)
{
if ($oFolder && !\preg_match('/[{}\[\]]/', $oFolder->NameRaw()))
{
@ -1971,7 +1971,7 @@ class MailClient
if ($iOptimizationLimit >= \count($aNewMailFoldersHelper))
{
// other
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder \MailSo\Mail\Folder */ $oFolder)
foreach ($aMailFoldersHelper as $iIndex => /* @var $oImapFolder Folder */ $oFolder)
{
if ($oFolder)
{
@ -2006,7 +2006,7 @@ class MailClient
return $aMailFoldersHelper;
}
public function Folders(string $sParent = '', string $sListPattern = '*', bool $bUseListSubscribeStatus = true, int $iOptimizationLimit = 0) : \MailSo\Mail\FolderCollection
public function Folders(string $sParent = '', string $sListPattern = '*', bool $bUseListSubscribeStatus = true, int $iOptimizationLimit = 0) : FolderCollection
{
$oFolderCollection = false;
@ -2101,8 +2101,7 @@ class MailClient
*/
public function FolderCreate(string $sFolderNameInUtf8, string $sFolderParentFullNameRaw = '', bool $bSubscribeOnCreation = true, string $sDelimiter = '') : self
{
if (!\MailSo\Base\Validator::NotEmptyString($sFolderNameInUtf8, true) ||
!\is_string($sFolderParentFullNameRaw))
if (!strlen(\trim($sFolderNameInUtf8)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}

View file

@ -42,7 +42,7 @@ class Email
*/
private function __construct(string $sEmail, string $sDisplayName = '')
{
if (!\MailSo\Base\Validator::NotEmptyString($sEmail, true))
if (!strlen(\trim($sEmail)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
@ -70,7 +70,7 @@ class Email
public static function Parse(string $sEmailAddress) : self
{
$sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress);
if (!\MailSo\Base\Validator::NotEmptyString($sEmailAddress, true))
if (!strlen(\trim($sEmailAddress)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}

View file

@ -48,7 +48,7 @@ class EmailDep
*/
private function __construct(string $sEmail, string $sDisplayName = '', string $sRemark = '')
{
if (!\MailSo\Base\Validator::NotEmptyString($sEmail, true))
if (!strlen(\trim($sEmail)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
@ -77,7 +77,7 @@ class EmailDep
public static function Parse(string $sEmailAddress) : \MailSo\Mime\Email
{
$sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress);
if (!\MailSo\Base\Validator::NotEmptyString($sEmailAddress, true))
if (!strlen(\trim($sEmailAddress)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}

View file

@ -171,7 +171,7 @@ abstract class NetClient
bool $bVerifySsl = false, bool $bAllowSelfSigned = true,
string $sClientCert = '') : void
{
if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort))
if (!strlen(\trim($sServerName)) || !\MailSo\Base\Validator::PortInt($iPort))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),

View file

@ -116,8 +116,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
*/
public function Login(string $sLogin, string $sPassword, string $sLoginAuthKey = '') : self
{
if (!\MailSo\Base\Validator::NotEmptyString($sLogin, true) ||
!\MailSo\Base\Validator::NotEmptyString($sPassword, true))
if (!strlen(\trim($sLogin)) || !strlen(\trim($sPassword)))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),
@ -434,7 +433,7 @@ class ManageSieveClient extends \MailSo\Net\NetClient
*/
private function sendRequest(string $sRequest) : void
{
if (!\MailSo\Base\Validator::NotEmptyString($sRequest, true))
if (!strlen(\trim($sRequest)))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),

View file

@ -357,7 +357,7 @@ class SmtpClient extends \MailSo\Net\NetClient
*/
public function Data(string $sData) : self
{
if (!\MailSo\Base\Validator::NotEmptyString($sData, true))
if (!strlen(\trim($sData)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
@ -525,7 +525,7 @@ class SmtpClient extends \MailSo\Net\NetClient
*/
private function sendRequest(string $sCommand, string $sAddToCommand = '', bool $bSecureLog = false) : void
{
if (!\MailSo\Base\Validator::NotEmptyString($sCommand, true))
if (!strlen(\trim($sCommand)))
{
$this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),