123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- class EmailAddressValidator
- {
-
- public static function checkEmailAddress($emailAddress, $allowLocal = false)
- {
-
-
-
-
-
-
-
- if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $emailAddress)) {
- return false;
- }
-
- if (!self::checkTextLength($emailAddress, 3, 256)) {
- return false;
- }
-
- $atSymbol = strrpos($emailAddress, '@');
- if ($atSymbol === false) {
-
- return false;
- }
- $emailAddressParts[0] = substr($emailAddress, 0, $atSymbol);
- $emailAddressParts[1] = substr($emailAddress, $atSymbol + 1);
-
-
-
-
-
- $tempAddressParts[0] = preg_replace('/\./', '', $emailAddressParts[0]);
- $tempAddressParts[0] = preg_replace('/"[^"]+"/', '', $tempAddressParts[0]);
- $tempAddressParts[1] = $emailAddressParts[1];
- $tempAddress = $tempAddressParts[0] . $tempAddressParts[1];
-
- if (strrpos($tempAddress, '@') !== false) {
-
- return false;
- }
-
- if (!self::checkLocalPortion($emailAddressParts[0])) {
- return false;
- }
-
- if (!self::checkDomainPortion($emailAddressParts[1], $allowLocal)) {
- return false;
- }
-
- return true;
- }
-
- public static function checkLocalPortion($localPortion)
- {
-
-
-
- if (!self::checkTextLength($localPortion, 1, 64)) {
- return false;
- }
-
-
-
-
- $localPortionParts = explode('.', $localPortion);
- for ($i = 0, $max = sizeof($localPortionParts); $i < $max; $i++) {
- if (!preg_match('.^('
- . '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
- . '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
- .'|'
- . '("[^\\\"]{0,62}")'
- .')$.'
- ,$localPortionParts[$i])) {
- return false;
- }
- }
- return true;
- }
-
- public static function checkDomainPortion($domainPortion, $allowLocal = false)
- {
-
- if (!self::checkTextLength($domainPortion, 1, 255)) {
- return false;
- }
-
-
- $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])';
- $hex_digit = '[A-Fa-f0-9]';
- $h16 = "{$hex_digit}{1,4}";
- $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet";
- $ls32 = "(?:$h16:$h16|$IPv4Address)";
- $IPv6Address =
- "(?:(?:{$IPv4Address})|(?:" .
- "(?:$h16:){6}$ls32" .
- "|::(?:$h16:){5}$ls32" .
- "|(?:$h16)?::(?:$h16:){4}$ls32" .
- "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" .
- "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" .
- "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" .
- "|(?:(?:$h16:){0,4}$h16)?::$ls32" .
- "|(?:(?:$h16:){0,5}$h16)?::$h16" .
- "|(?:(?:$h16:){0,6}$h16)?::" .
- ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)";
- if (preg_match("/^($IPv4Address|\\[$IPv4Address\\]|\\[$IPv6Address\\])$/",
- $domainPortion)){
- return true;
- } else {
- $domainPortionParts = explode('.', $domainPortion);
- if (!$allowLocal && sizeof($domainPortionParts) < 2) {
- return false;
- }
- for ($i = 0, $max = sizeof($domainPortionParts); $i < $max; $i++) {
-
- if (!self::checkTextLength($domainPortionParts[$i], 1, 63)) {
- return false;
- }
- if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
- .'([A-Za-z0-9]+))$/', $domainPortionParts[$i])) {
- return false;
- }
- if ($i == $max - 1) {
- if (strlen(preg_replace('/[0-9]/', '', $domainPortionParts[$i])) <= 0) {
- return false;
- }
- }
- }
- }
- return true;
- }
-
- protected static function checkTextLength($text, $minimum, $maximum)
- {
-
- $textLength = strlen($text);
- return ($textLength >= $minimum && $textLength <= $maximum);
- }
- }
|