1<?php 2 3/** 4 * Class EmailAddressValidator 5 * 6 * @link https://github.com/aziraphale/email-address-validator 7 * @link http://code.google.com/p/php-email-address-validation/ 8 * @license New BSD license http://www.opensource.org/licenses/bsd-license.php 9 * @example if (EmailAddressValidator::checkEmailAddress('test@example.org')) { 10 * @example // Email address is technically valid 11 * @example } 12 */ 13class EmailAddressValidator 14{ 15 /** 16 * Check email address validity 17 * @param string $emailAddress Email address to be checked 18 * @param bool $allowLocal allow local domains 19 * @return bool Whether email is valid 20 */ 21 public static function checkEmailAddress($emailAddress, $allowLocal = false) 22 { 23 // If magic quotes is "on", email addresses with quote marks will 24 // fail validation because of added escape characters. Uncommenting 25 // the next three lines will allow for this issue. 26 //if (get_magic_quotes_gpc()) { 27 // $emailAddress = stripslashes($emailAddress); 28 //} 29 30 // Control characters are not allowed 31 if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $emailAddress)) { 32 return false; 33 } 34 35 // Check email length - min 3 (a@a), max 256 36 if (!self::checkTextLength($emailAddress, 3, 256)) { 37 return false; 38 } 39 40 // Split it into sections using last instance of "@" 41 $atSymbol = strrpos($emailAddress, '@'); 42 if ($atSymbol === false) { 43 // No "@" symbol in email. 44 return false; 45 } 46 $emailAddressParts[0] = substr($emailAddress, 0, $atSymbol); 47 $emailAddressParts[1] = substr($emailAddress, $atSymbol + 1); 48 49 // Count the "@" symbols. Only one is allowed, except where 50 // contained in quote marks in the local part. Quickest way to 51 // check this is to remove anything in quotes. We also remove 52 // characters escaped with backslash, and the backslash 53 // character. 54 $tempAddressParts[0] = preg_replace('/\./', '', $emailAddressParts[0]); 55 $tempAddressParts[0] = preg_replace('/"[^"]+"/', '', $tempAddressParts[0]); 56 $tempAddressParts[1] = $emailAddressParts[1]; 57 $tempAddress = $tempAddressParts[0] . $tempAddressParts[1]; 58 // Then check - should be no "@" symbols. 59 if (strrpos($tempAddress, '@') !== false) { 60 // "@" symbol found 61 return false; 62 } 63 64 // Check local portion 65 if (!self::checkLocalPortion($emailAddressParts[0])) { 66 return false; 67 } 68 69 // Check domain portion 70 if (!self::checkDomainPortion($emailAddressParts[1], $allowLocal)) { 71 return false; 72 } 73 74 // If we're still here, all checks above passed. Email is valid. 75 return true; 76 } 77 78 /** 79 * Checks email section before "@" symbol for validity 80 * @param string $localPortion Text to be checked 81 * @return bool Whether local portion is valid 82 */ 83 public static function checkLocalPortion($localPortion) 84 { 85 // Local portion can only be from 1 to 64 characters, inclusive. 86 // Please note that servers are encouraged to accept longer local 87 // parts than 64 characters. 88 if (!self::checkTextLength($localPortion, 1, 64)) { 89 return false; 90 } 91 // Local portion must be: 92 // 1) a dot-atom (strings separated by periods) 93 // 2) a quoted string 94 // 3) an obsolete format string (combination of the above) 95 $localPortionParts = explode('.', $localPortion); 96 for ($i = 0, $max = sizeof($localPortionParts); $i < $max; $i++) { 97 if (!preg_match('.^(' 98 . '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]' 99 . '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})' 100 .'|' 101 . '("[^\\\"]{0,62}")' 102 .')$.' 103 ,$localPortionParts[$i])) { 104 return false; 105 } 106 } 107 return true; 108 } 109 110 /** 111 * Checks email section after "@" symbol for validity 112 * @param string $domainPortion Text to be checked 113 * @param bool $allowLocal allow local domains? 114 * @return bool Whether domain portion is valid 115 */ 116 public static function checkDomainPortion($domainPortion, $allowLocal = false) 117 { 118 // Total domain can only be from 1 to 255 characters, inclusive 119 if (!self::checkTextLength($domainPortion, 1, 255)) { 120 return false; 121 } 122 123 // some IPv4/v6 regexps borrowed from Feyd 124 // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 125 $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 126 $hex_digit = '[A-Fa-f0-9]'; 127 $h16 = "{$hex_digit}{1,4}"; 128 $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 129 $ls32 = "(?:$h16:$h16|$IPv4Address)"; 130 $IPv6Address = 131 "(?:(?:{$IPv4Address})|(?:" . 132 "(?:$h16:){6}$ls32" . 133 "|::(?:$h16:){5}$ls32" . 134 "|(?:$h16)?::(?:$h16:){4}$ls32" . 135 "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" . 136 "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" . 137 "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" . 138 "|(?:(?:$h16:){0,4}$h16)?::$ls32" . 139 "|(?:(?:$h16:){0,5}$h16)?::$h16" . 140 "|(?:(?:$h16:){0,6}$h16)?::" . 141 ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 142 143 if (preg_match("/^($IPv4Address|\\[$IPv4Address\\]|\\[$IPv6Address\\])$/", 144 $domainPortion)){ 145 return true; 146 } else { 147 $domainPortionParts = explode('.', $domainPortion); 148 if (!$allowLocal && sizeof($domainPortionParts) < 2) { 149 return false; // Not enough parts to domain 150 } 151 for ($i = 0, $max = sizeof($domainPortionParts); $i < $max; $i++) { 152 // Each portion must be between 1 and 63 characters, inclusive 153 if (!self::checkTextLength($domainPortionParts[$i], 1, 63)) { 154 return false; 155 } 156 if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|' 157 .'([A-Za-z0-9]+))$/', $domainPortionParts[$i])) { 158 return false; 159 } 160 if ($i == $max - 1) { // TLD cannot be only numbers 161 if (strlen(preg_replace('/[0-9]/', '', $domainPortionParts[$i])) <= 0) { 162 return false; 163 } 164 } 165 } 166 } 167 return true; 168 } 169 170 /** 171 * Check given text length is between defined bounds 172 * @param string $text Text to be checked 173 * @param int $minimum Minimum acceptable length 174 * @param int $maximum Maximum acceptable length 175 * @return bool Whether string is within bounds (inclusive) 176 */ 177 protected static function checkTextLength($text, $minimum, $maximum) 178 { 179 // Minimum and maximum are both inclusive 180 $textLength = strlen($text); 181 return ($textLength >= $minimum && $textLength <= $maximum); 182 } 183} 184