From 790a8676747f3d9515db591f8aad0917f782a92f Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Wed, 27 Nov 2013 21:19:28 +0400 Subject: [PATCH] Factorizing Validator.php Improved email validation. Add SimpleEmailString validation. --- .../app/libraries/MailSo/Base/Validator.php | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/rainloop/v/0.0.0/app/libraries/MailSo/Base/Validator.php b/rainloop/v/0.0.0/app/libraries/MailSo/Base/Validator.php index 22a7e7938..c260565f4 100644 --- a/rainloop/v/0.0.0/app/libraries/MailSo/Base/Validator.php +++ b/rainloop/v/0.0.0/app/libraries/MailSo/Base/Validator.php @@ -10,20 +10,39 @@ class Validator { /** * @param string $sEmail + * @param array $aAllowedInternalDomains = array('localhost') * * @return bool */ - public static function EmailString($sEmail) + public static function EmailString($sEmail, $aAllowedInternalDomains = array('localhost')) { $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; } + /** + * @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 bool $bTrim = false @@ -32,18 +51,8 @@ class Validator */ public static function NotEmptyString($sString, $bTrim = false) { - $bResult = false; - if (\is_string($sString)) - { - if ($bTrim) - { - $sString = \trim($sString); - } - - $bResult = 0 < \strlen($sString); - } - - return $bResult; + return \is_string($sString) && + (0 < \strlen($bTrim ? \trim($sString) : $sString)); } /** @@ -65,22 +74,9 @@ class Validator */ public static function RangeInt($iNumber, $iMin = null, $iMax = null) { - $bResult = false; - if (\is_int($iNumber)) - { - $bResult = true; - if ($bResult && null !== $iMin) - { - $bResult = $iNumber >= $iMin; - } - - if ($bResult && null !== $iMax) - { - $bResult = $iNumber <= $iMax; - } - } - - return $bResult; + return \is_int($iNumber) && + (null !== $iMin && $iNumber >= $iMin || null === $iMin) && + (null !== $iMax && $iNumber <= $iMax || null === $iMax); } /** @@ -90,6 +86,6 @@ class Validator */ public static function PortInt($iPort) { - return self::RangeInt($iPort, 0, 65535); + return \MailSo\Base\Validator::RangeInt($iPort, 0, 65535); } }