EmailAddressValidator.php 7.1 KB

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