Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. <?php
  2. /**
  3.  * @package     isemail
  4.  * @author      Dominic Sayers <dominic_sayers@hotmail.com>
  5.  * @copyright   2010 Dominic Sayers
  6.  * @license     http://www.opensource.org/licenses/bsd-license.php BSD License
  7.  * @link        http://www.dominicsayers.com/isemail
  8.  * @version     1.17 - Upper length limit corrected to 254 characters
  9.  */
  10.  
  11. /*
  12. Copyright (c) 2008-2010, Dominic Sayers
  13. All rights reserved.
  14.  
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17.  
  18.  * Redistributions of source code must retain the above copyright notice, this
  19.    list of conditions and the following disclaimer.
  20.  * Redistributions in binary form must reproduce the above copyright notice,
  21.    this list of conditions and the following disclaimer in the documentation
  22.    and/or other materials provided with the distribution.
  23.  * Neither the name of Dominic Sayers nor the names of its contributors may be
  24.    used to endorse or promote products derived from this software without
  25.    specific prior written permission.
  26.  
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  28. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  31. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  34. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38.  
  39. /*.
  40.         require_module 'standard';
  41.         require_module 'pcre';
  42. .*/
  43. /*.mixed.*/ function is_email (/*.string.*/ $email, $checkDNS = false, $diagnose = false) {
  44.         // Check that $email is a valid address. Read the following RFCs to understand the constraints:
  45.         //      (http://tools.ietf.org/html/rfc5322)
  46.         //      (http://tools.ietf.org/html/rfc3696)
  47.         //      (http://tools.ietf.org/html/rfc5321)
  48.         //      (http://tools.ietf.org/html/rfc4291#section-2.2)
  49.         //      (http://tools.ietf.org/html/rfc1123#section-2.1)
  50.  
  51.         if (!defined('ISEMAIL_VALID')) {
  52.                 define('ISEMAIL_VALID'                  , 0);
  53.                 define('ISEMAIL_TOOLONG'                , 1);
  54.                 define('ISEMAIL_NOAT'                   , 2);
  55.                 define('ISEMAIL_NOLOCALPART'            , 3);
  56.                 define('ISEMAIL_NODOMAIN'               , 4);
  57.                 define('ISEMAIL_ZEROLENGTHELEMENT'      , 5);
  58.                 define('ISEMAIL_BADCOMMENT_START'       , 6);
  59.                 define('ISEMAIL_BADCOMMENT_END'         , 7);
  60.                 define('ISEMAIL_UNESCAPEDDELIM'         , 8);
  61.                 define('ISEMAIL_EMPTYELEMENT'           , 9);
  62.                 define('ISEMAIL_UNESCAPEDSPECIAL'       , 10);
  63.                 define('ISEMAIL_LOCALTOOLONG'           , 11);
  64.                 define('ISEMAIL_IPV4BADPREFIX'          , 12);
  65.                 define('ISEMAIL_IPV6BADPREFIXMIXED'     , 13);
  66.                 define('ISEMAIL_IPV6BADPREFIX'          , 14);
  67.                 define('ISEMAIL_IPV6GROUPCOUNT'         , 15);
  68.                 define('ISEMAIL_IPV6DOUBLEDOUBLECOLON'  , 16);
  69.                 define('ISEMAIL_IPV6BADCHAR'            , 17);
  70.                 define('ISEMAIL_IPV6TOOMANYGROUPS'      , 18);
  71.                 define('ISEMAIL_TLD'                    , 19);
  72.                 define('ISEMAIL_DOMAINEMPTYELEMENT'     , 20);
  73.                 define('ISEMAIL_DOMAINELEMENTTOOLONG'   , 21);
  74.                 define('ISEMAIL_DOMAINBADCHAR'          , 22);
  75.                 define('ISEMAIL_DOMAINTOOLONG'          , 23);
  76.                 define('ISEMAIL_TLDNUMERIC'             , 24);
  77.                 define('ISEMAIL_DOMAINNOTFOUND'         , 25);
  78.                 define('ISEMAIL_NOTDEFINED'             , 99);
  79.         }
  80.  
  81.         // the upper limit on address lengths should normally be considered to be 254
  82.         //      (http://www.rfc-editor.org/errata_search.php?rfc=3696)
  83.         //      NB My erratum has now been verified by the IETF so the correct answer is 254
  84.         //
  85.         // The maximum total length of a reverse-path or forward-path is 256
  86.         // characters (including the punctuation and element separators)
  87.         //      (http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3)
  88.         //      NB There is a mandatory 2-character wrapper round the actual address
  89.         $emailLength = strlen($email);
  90.         // revision 1.17: Max length reduced to 254 (see above)
  91.         if ($emailLength > 254)                 return $diagnose ? ISEMAIL_TOOLONG      : false;        // Too long
  92.  
  93.         // Contemporary email addresses consist of a "local part" separated from
  94.         // a "domain part" (a fully-qualified domain name) by an at-sign ("@").
  95.         //      (http://tools.ietf.org/html/rfc3696#section-3)
  96.         $atIndex = strrpos($email,'@');
  97.  
  98.         if ($atIndex === false)                 return $diagnose ? ISEMAIL_NOAT         : false;        // No at-sign
  99.         if ($atIndex === 0)                     return $diagnose ? ISEMAIL_NOLOCALPART  : false;        // No local part
  100.         if ($atIndex === $emailLength - 1)      return $diagnose ? ISEMAIL_NODOMAIN     : false;        // No domain part
  101. // revision 1.14: Length test bug suggested by Andrew Campbell of Gloucester, MA
  102.        
  103.         // Sanitize comments
  104.         // - remove nested comments, quotes and dots in comments
  105.         // - remove parentheses and dots from quoted strings
  106.         $braceDepth     = 0;
  107.         $inQuote        = false;
  108.         $escapeThisChar = false;
  109.  
  110.         for ($i = 0; $i < $emailLength; ++$i) {
  111.                 $char = $email[$i];
  112.                 $replaceChar = false;
  113.  
  114.                 if ($char === '\\') {
  115.                         $escapeThisChar = !$escapeThisChar;     // Escape the next character?
  116.                 } else {
  117.                         switch ($char) {
  118.                         case '(':
  119.                                 if ($escapeThisChar) {
  120.                                         $replaceChar = true;
  121.                                 } else {
  122.                                         if ($inQuote) {
  123.                                                 $replaceChar = true;
  124.                                         } else {
  125.                                                 if ($braceDepth++ > 0) $replaceChar = true;     // Increment brace depth
  126.                                         }
  127.                                 }
  128.  
  129.                                 break;
  130.                         case ')':
  131.                                 if ($escapeThisChar) {
  132.                                         $replaceChar = true;
  133.                                 } else {
  134.                                         if ($inQuote) {
  135.                                                 $replaceChar = true;
  136.                                         } else {
  137.                                                 if (--$braceDepth > 0) $replaceChar = true;     // Decrement brace depth
  138.                                                 if ($braceDepth < 0) $braceDepth = 0;
  139.                                         }
  140.                                 }
  141.  
  142.                                 break;
  143.                         case '"':
  144.                                 if ($escapeThisChar) {
  145.                                         $replaceChar = true;
  146.                                 } else {
  147.                                         if ($braceDepth === 0) {
  148.                                                 $inQuote = !$inQuote;   // Are we inside a quoted string?
  149.                                         } else {
  150.                                                 $replaceChar = true;
  151.                                         }
  152.                                 }
  153.  
  154.                                 break;
  155.                         case '.':       // Dots don't help us either
  156.                                 if ($escapeThisChar) {
  157.                                         $replaceChar = true;
  158.                                 } else {
  159.                                         if ($braceDepth > 0) $replaceChar = true;
  160.                                 }
  161.  
  162.                                 break;
  163.                         default:
  164.                         }
  165.  
  166.                         $escapeThisChar = false;
  167. //                      if ($replaceChar) $email[$i] = 'x';     // Replace the offending character with something harmless
  168. // revision 1.12: Line above replaced because PHPLint doesn't like that syntax
  169.                         if ($replaceChar) $email = (string) substr_replace($email, 'x', $i, 1); // Replace the offending character with something harmless
  170.                 }
  171.         }
  172.  
  173.         $localPart      = substr($email, 0, $atIndex);
  174.         $domain         = substr($email, $atIndex + 1);
  175.         $FWS            = "(?:(?:(?:[ \\t]*(?:\\r\\n))?[ \\t]+)|(?:[ \\t]+(?:(?:\\r\\n)[ \\t]+)*))";    // Folding white space
  176.         // Let's check the local part for RFC compliance...
  177.         //
  178.         // local-part      =       dot-atom / quoted-string / obs-local-part
  179.         // obs-local-part  =       word *("." word)
  180.         //      (http://tools.ietf.org/html/rfc5322#section-3.4.1)
  181.         //
  182.         // Problem: need to distinguish between "first.last" and "first"."last"
  183.         // (i.e. one element or two). And I suck at regexes.
  184.         $dotArray       = /*. (array[int]string) .*/ preg_split('/\\.(?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/m', $localPart);
  185.         $partLength     = 0;
  186.  
  187.         foreach ($dotArray as $element) {
  188.                 // Remove any leading or trailing FWS
  189.                 $element        = preg_replace("/^$FWS|$FWS\$/", '', $element);
  190.                 $elementLength  = strlen($element);
  191.  
  192.                 if ($elementLength === 0)                                                               return $diagnose ? ISEMAIL_ZEROLENGTHELEMENT    : false;        // Can't have empty element (consecutive dots or dots at the start or end)
  193. // revision 1.15: Speed up the test and get rid of "unitialized string offset" notices from PHP
  194.  
  195.                 // We need to remove any valid comments (i.e. those at the start or end of the element)
  196.                 if ($element[0] === '(') {
  197.                         $indexBrace = strpos($element, ')');
  198.                         if ($indexBrace !== false) {
  199.                                 if (preg_match('/(?<!\\\\)[\\(\\)]/', substr($element, 1, $indexBrace - 1)) > 0) {
  200.                                                                                                         return $diagnose ? ISEMAIL_BADCOMMENT_START     : false;        // Illegal characters in comment
  201.                                 }
  202.                                 $element        = substr($element, $indexBrace + 1, $elementLength - $indexBrace - 1);
  203.                                 $elementLength  = strlen($element);
  204.                         }
  205.                 }
  206.                
  207.                 if ($element[$elementLength - 1] === ')') {
  208.                         $indexBrace = strrpos($element, '(');
  209.                         if ($indexBrace !== false) {
  210.                                 if (preg_match('/(?<!\\\\)(?:[\\(\\)])/', substr($element, $indexBrace + 1, $elementLength - $indexBrace - 2)) > 0) {
  211.                                                                                                         return $diagnose ? ISEMAIL_BADCOMMENT_END       : false;        // Illegal characters in comment
  212.                                 }
  213.                                 $element        = substr($element, 0, $indexBrace);
  214.                                 $elementLength  = strlen($element);
  215.                         }
  216.                 }
  217.  
  218.                 // Remove any leading or trailing FWS around the element (inside any comments)
  219.                 $element = preg_replace("/^$FWS|$FWS\$/", '', $element);
  220.  
  221.                 // What's left counts towards the maximum length for this part
  222.                 if ($partLength > 0) $partLength++;     // for the dot
  223.                 $partLength += strlen($element);
  224.  
  225.                 // Each dot-delimited component can be an atom or a quoted string
  226.                 // (because of the obs-local-part provision)
  227.                 if (preg_match('/^"(?:.)*"$/s', $element) > 0) {
  228.                         // Quoted-string tests:
  229.                         //
  230.                         // Remove any FWS
  231.                         $element = preg_replace("/(?<!\\\\)$FWS/", '', $element);
  232.                         // My regex skillz aren't up to distinguishing between \" \\" \\\" \\\\" etc.
  233.                         // So remove all \\ from the string first...
  234.                         $element = preg_replace('/\\\\\\\\/', ' ', $element);
  235.                         if (preg_match('/(?<!\\\\|^)["\\r\\n\\x00](?!$)|\\\\"$|""/', $element) > 0)     return $diagnose ? ISEMAIL_UNESCAPEDDELIM       : false;        // ", CR, LF and NUL must be escaped, "" is too short
  236.                 } else {
  237.                         // Unquoted string tests:
  238.                         //
  239.                         // Period (".") may...appear, but may not be used to start or end the
  240.                         // local part, nor may two or more consecutive periods appear.
  241.                         //      (http://tools.ietf.org/html/rfc3696#section-3)
  242.                         //
  243.                         // A zero-length element implies a period at the beginning or end of the
  244.                         // local part, or two periods together. Either way it's not allowed.
  245.                         if ($element === '')                                                            return $diagnose ? ISEMAIL_EMPTYELEMENT : false;        // Dots in wrong place
  246.  
  247.                         // Any ASCII graphic (printing) character other than the
  248.                         // at-sign ("@"), backslash, double quote, comma, or square brackets may
  249.                         // appear without quoting.  If any of that list of excluded characters
  250.                         // are to appear, they must be quoted
  251.                         //      (http://tools.ietf.org/html/rfc3696#section-3)
  252.                         //
  253.                         // Any excluded characters? i.e. 0x00-0x20, (, ), <, >, [, ], :, ;, @, \, comma, period, "
  254.                         if (preg_match('/[\\x00-\\x20\\(\\)<>\\[\\]:;@\\\\,\\."]/', $element) > 0)      return $diagnose ? ISEMAIL_UNESCAPEDSPECIAL     : false;        // These characters must be in a quoted string
  255.                 }
  256.         }
  257.  
  258.         if ($partLength > 64) return $diagnose ? ISEMAIL_LOCALTOOLONG   : false;        // Local part must be 64 characters or less
  259.  
  260.         // Now let's check the domain part...
  261.  
  262.         // The domain name can also be replaced by an IP address in square brackets
  263.         //      (http://tools.ietf.org/html/rfc3696#section-3)
  264.         //      (http://tools.ietf.org/html/rfc5321#section-4.1.3)
  265.         //      (http://tools.ietf.org/html/rfc4291#section-2.2)
  266.         if (preg_match('/^\\[(.)+]$/', $domain) === 1) {
  267.                 // It's an address-literal
  268.                 $addressLiteral = substr($domain, 1, strlen($domain) - 2);
  269.                 $matchesIP      = array();
  270.                
  271.                 // Extract IPv4 part from the end of the address-literal (if there is one)
  272.                 if (preg_match('/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $addressLiteral, $matchesIP) > 0) {
  273.                         $index = strrpos($addressLiteral, $matchesIP[0]);
  274.                        
  275.                         if ($index === 0) {
  276.                                 // Nothing there except a valid IPv4 address, so...
  277.                                 return $diagnose ? ISEMAIL_VALID : true;
  278.                         } else {
  279.                                 // Assume it's an attempt at a mixed address (IPv6 + IPv4)
  280.                                 if ($addressLiteral[$index - 1] !== ':')        return $diagnose ? ISEMAIL_IPV4BADPREFIX        : false;        // Character preceding IPv4 address must be ':'
  281.                                 if (substr($addressLiteral, 0, 5) !== 'IPv6:')  return $diagnose ? ISEMAIL_IPV6BADPREFIXMIXED   : false;        // RFC5321 section 4.1.3
  282.  
  283.                                 $IPv6           = substr($addressLiteral, 5, ($index ===7) ? 2 : $index - 6);
  284.                                 $groupMax       = 6;
  285.                         }
  286.                 } else {
  287.                         // It must be an attempt at pure IPv6
  288.                         if (substr($addressLiteral, 0, 5) !== 'IPv6:')          return $diagnose ? ISEMAIL_IPV6BADPREFIX        : false;        // RFC5321 section 4.1.3
  289.                         $IPv6 = substr($addressLiteral, 5);
  290.                         $groupMax = 8;
  291.                 }
  292.  
  293.                 $groupCount     = preg_match_all('/^[0-9a-fA-F]{0,4}|\\:[0-9a-fA-F]{0,4}|(.)/', $IPv6, $matchesIP);
  294.                 $index          = strpos($IPv6,'::');
  295.  
  296.                 if ($index === false) {
  297.                         // We need exactly the right number of groups
  298.                         if ($groupCount !== $groupMax)                          return $diagnose ? ISEMAIL_IPV6GROUPCOUNT       : false;        // RFC5321 section 4.1.3
  299.                 } else {
  300.                         if ($index !== strrpos($IPv6,'::'))                     return $diagnose ? ISEMAIL_IPV6DOUBLEDOUBLECOLON : false;       // More than one '::'
  301.                         $groupMax = ($index === 0 || $index === (strlen($IPv6) - 2)) ? $groupMax : $groupMax - 1;
  302.                         if ($groupCount > $groupMax)                            return $diagnose ? ISEMAIL_IPV6TOOMANYGROUPS    : false;        // Too many IPv6 groups in address
  303.                 }
  304.  
  305.                 // Check for unmatched characters
  306.                 array_multisort($matchesIP[1], SORT_DESC);
  307.                 if ($matchesIP[1][0] !== '')                                    return $diagnose ? ISEMAIL_IPV6BADCHAR          : false;        // Illegal characters in address
  308.  
  309.                 // It's a valid IPv6 address, so...
  310.                 return $diagnose ? ISEMAIL_VALID : true;
  311.         } else {
  312.                 // It's a domain name...
  313.  
  314.                 // The syntax of a legal Internet host name was specified in RFC-952
  315.                 // One aspect of host name syntax is hereby changed: the
  316.                 // restriction on the first character is relaxed to allow either a
  317.                 // letter or a digit.
  318.                 //      (http://tools.ietf.org/html/rfc1123#section-2.1)
  319.                 //
  320.                 // NB RFC 1123 updates RFC 1035, but this is not currently apparent from reading RFC 1035.
  321.                 //
  322.                 // Most common applications, including email and the Web, will generally not
  323.                 // permit...escaped strings
  324.                 //      (http://tools.ietf.org/html/rfc3696#section-2)
  325.                 //
  326.                 // the better strategy has now become to make the "at least one period" test,
  327.                 // to verify LDH conformance (including verification that the apparent TLD name
  328.                 // is not all-numeric)
  329.                 //      (http://tools.ietf.org/html/rfc3696#section-2)
  330.                 //
  331.                 // Characters outside the set of alphabetic characters, digits, and hyphen MUST NOT appear in domain name
  332.                 // labels for SMTP clients or servers
  333.                 //      (http://tools.ietf.org/html/rfc5321#section-4.1.2)
  334.                 //
  335.                 // RFC5321 precludes the use of a trailing dot in a domain name for SMTP purposes
  336.                 //      (http://tools.ietf.org/html/rfc5321#section-4.1.2)
  337.                 $dotArray       = /*. (array[int]string) .*/ preg_split('/\\.(?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/m', $domain);
  338.                 $partLength     = 0;
  339.                 $element        = ''; // Since we use $element after the foreach loop let's make sure it has a value
  340. // revision 1.13: Line above added because PHPLint now checks for Definitely Assigned Variables
  341.  
  342.                 if (count($dotArray) === 1)                                     return $diagnose ? ISEMAIL_TLD  : false;        // Mail host can't be a TLD (cite? What about localhost?)
  343.  
  344.                 foreach ($dotArray as $element) {
  345.                         // Remove any leading or trailing FWS
  346.                         $element        = preg_replace("/^$FWS|$FWS\$/", '', $element);
  347.                         $elementLength  = strlen($element);
  348.        
  349.                         // Each dot-delimited component must be of type atext
  350.                         // A zero-length element implies a period at the beginning or end of the
  351.                         // local part, or two periods together. Either way it's not allowed.
  352.                         if ($elementLength === 0)                               return $diagnose ? ISEMAIL_DOMAINEMPTYELEMENT   : false;        // Dots in wrong place
  353. // revision 1.15: Speed up the test and get rid of "unitialized string offset" notices from PHP
  354.        
  355.                         // Then we need to remove all valid comments (i.e. those at the start or end of the element
  356.                         if ($element[0] === '(') {
  357.                                 $indexBrace = strpos($element, ')');
  358.                                 if ($indexBrace !== false) {
  359.                                         if (preg_match('/(?<!\\\\)[\\(\\)]/', substr($element, 1, $indexBrace - 1)) > 0) {
  360. // revision 1.17: Fixed name of constant (also spotted by turboflash - thanks!)
  361.                                                                                 return $diagnose ? ISEMAIL_BADCOMMENT_START     : false;        // Illegal characters in comment
  362.                                         }
  363.                                         $element        = substr($element, $indexBrace + 1, $elementLength - $indexBrace - 1);
  364.                                         $elementLength  = strlen($element);
  365.                                 }
  366.                         }
  367.                        
  368.                         if ($element[$elementLength - 1] === ')') {
  369.                                 $indexBrace = strrpos($element, '(');
  370.                                 if ($indexBrace !== false) {
  371.                                         if (preg_match('/(?<!\\\\)(?:[\\(\\)])/', substr($element, $indexBrace + 1, $elementLength - $indexBrace - 2)) > 0)
  372. // revision 1.17: Fixed name of constant (also spotted by turboflash - thanks!)
  373.                                                                                 return $diagnose ? ISEMAIL_BADCOMMENT_END       : false;        // Illegal characters in comment
  374.  
  375.                                         $element        = substr($element, 0, $indexBrace);
  376.                                         $elementLength  = strlen($element);
  377.                                 }
  378.                         }                      
  379.        
  380.                         // Remove any leading or trailing FWS around the element (inside any comments)
  381.                         $element = preg_replace("/^$FWS|$FWS\$/", '', $element);
  382.        
  383.                         // What's left counts towards the maximum length for this part
  384.                         if ($partLength > 0) $partLength++;     // for the dot
  385.                         $partLength += strlen($element);
  386.        
  387.                         // The DNS defines domain name syntax very generally -- a
  388.                         // string of labels each containing up to 63 8-bit octets,
  389.                         // separated by dots, and with a maximum total of 255
  390.                         // octets.
  391.                         //      (http://tools.ietf.org/html/rfc1123#section-6.1.3.5)
  392.                         if ($elementLength > 63)                                return $diagnose ? ISEMAIL_DOMAINELEMENTTOOLONG : false;        // Label must be 63 characters or less
  393.        
  394.                         // Any ASCII graphic (printing) character other than the
  395.                         // at-sign ("@"), backslash, double quote, comma, or square brackets may
  396.                         // appear without quoting.  If any of that list of excluded characters
  397.                         // are to appear, they must be quoted
  398.                         //      (http://tools.ietf.org/html/rfc3696#section-3)
  399.                         //
  400.                         // If the hyphen is used, it is not permitted to appear at
  401.                         // either the beginning or end of a label.
  402.                         //      (http://tools.ietf.org/html/rfc3696#section-2)
  403.                         //
  404.                         // Any excluded characters? i.e. 0x00-0x20, (, ), <, >, [, ], :, ;, @, \, comma, period, "
  405.                         if (preg_match('/[\\x00-\\x20\\(\\)<>\\[\\]:;@\\\\,\\."]|^-|-$/', $element) > 0) {
  406.                                                                                 return $diagnose ? ISEMAIL_DOMAINBADCHAR        : false;
  407.                         }
  408.                 }
  409.  
  410.                 if ($partLength > 255)                                          return $diagnose ? ISEMAIL_DOMAINTOOLONG        : false;        // Domain part must be 255 characters or less (http://tools.ietf.org/html/rfc1123#section-6.1.3.5)
  411.  
  412.                 if (preg_match('/^[0-9]+$/', $element) > 0)                     return $diagnose ? ISEMAIL_TLDNUMERIC           : false;        // TLD can't be all-numeric (http://www.apps.ietf.org/rfc/rfc3696.html#sec-2)
  413.  
  414.                 // Check DNS?
  415.                 if ($checkDNS && function_exists('checkdnsrr')) {
  416.                         if (!(checkdnsrr($domain, 'A') || checkdnsrr($domain, 'MX'))) {
  417.                                                                                 return $diagnose ? ISEMAIL_DOMAINNOTFOUND       : false;        // Domain doesn't actually exist
  418.                         }
  419.                 }
  420.         }
  421.  
  422.         // Eliminate all other factors, and the one which remains must be the truth.
  423.         //      (Sherlock Holmes, The Sign of Four)
  424.         return $diagnose ? ISEMAIL_VALID : true;
  425. }
  426. ?>
  427.