mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-09-06 05:04:24 +08:00
Factorizing Validator.php
Improved email validation. Add SimpleEmailString validation.
This commit is contained in:
parent
ecd1cc0c5b
commit
790a867674
1 changed files with 28 additions and 32 deletions
|
@ -10,20 +10,39 @@ class Validator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param string $sEmail
|
* @param string $sEmail
|
||||||
|
* @param array $aAllowedInternalDomains = array('localhost')
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function EmailString($sEmail)
|
public static function EmailString($sEmail, $aAllowedInternalDomains = array('localhost'))
|
||||||
{
|
{
|
||||||
$bResult = false;
|
$bResult = false;
|
||||||
if (self::NotEmptyString($sEmail))
|
if (\MailSo\Base\Validator::NotEmptyString($sEmail, true))
|
||||||
{
|
{
|
||||||
$bResult = (bool) \preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\.\+\-_]*@[a-zA-Z0-9][a-zA-Z0-9\.\+\-_]*$/', $sEmail);
|
$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;
|
return $bResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sEmail
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function SimpleEmailString($sEmail)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sString
|
* @param string $sString
|
||||||
* @param bool $bTrim = false
|
* @param bool $bTrim = false
|
||||||
|
@ -32,18 +51,8 @@ class Validator
|
||||||
*/
|
*/
|
||||||
public static function NotEmptyString($sString, $bTrim = false)
|
public static function NotEmptyString($sString, $bTrim = false)
|
||||||
{
|
{
|
||||||
$bResult = false;
|
return \is_string($sString) &&
|
||||||
if (\is_string($sString))
|
(0 < \strlen($bTrim ? \trim($sString) : $sString));
|
||||||
{
|
|
||||||
if ($bTrim)
|
|
||||||
{
|
|
||||||
$sString = \trim($sString);
|
|
||||||
}
|
|
||||||
|
|
||||||
$bResult = 0 < \strlen($sString);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $bResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,22 +74,9 @@ class Validator
|
||||||
*/
|
*/
|
||||||
public static function RangeInt($iNumber, $iMin = null, $iMax = null)
|
public static function RangeInt($iNumber, $iMin = null, $iMax = null)
|
||||||
{
|
{
|
||||||
$bResult = false;
|
return \is_int($iNumber) &&
|
||||||
if (\is_int($iNumber))
|
(null !== $iMin && $iNumber >= $iMin || null === $iMin) &&
|
||||||
{
|
(null !== $iMax && $iNumber <= $iMax || null === $iMax);
|
||||||
$bResult = true;
|
|
||||||
if ($bResult && null !== $iMin)
|
|
||||||
{
|
|
||||||
$bResult = $iNumber >= $iMin;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($bResult && null !== $iMax)
|
|
||||||
{
|
|
||||||
$bResult = $iNumber <= $iMax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $bResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,6 +86,6 @@ class Validator
|
||||||
*/
|
*/
|
||||||
public static function PortInt($iPort)
|
public static function PortInt($iPort)
|
||||||
{
|
{
|
||||||
return self::RangeInt($iPort, 0, 65535);
|
return \MailSo\Base\Validator::RangeInt($iPort, 0, 65535);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue