Beispiel #1
1
 /**
  * Check if the email is valid.
  *
  * @param string $email
  * @param bool   $useExampleDomainCheck
  * @param bool   $useTypoInDomainCheck
  * @param bool   $useTemporaryDomainCheck
  * @param bool   $useDnsCheck (do not use, if you don't need it)
  *
  * @return bool
  */
 public static function isValid($email, $useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false)
 {
     // must be a string
     if (!is_string($email)) {
         return false;
     }
     // make sure string length is limited to avoid DOS attacks
     $emailStringLength = strlen($email);
     if ($emailStringLength >= 320 || $emailStringLength <= 2) {
         return false;
     }
     unset($emailStringLength);
     $email = str_replace(array('.', '@'), array('.', '@'), $email);
     if (strpos($email, '@') === false || strpos($email, '.') === false && strpos($email, ':') === false) {
         return false;
     }
     if (!preg_match('/^(?<local>.*<?)(?:.*)@(?<domain>.*)(?:>?)$/', $email, $parts)) {
         return false;
     } else {
         $local = $parts['local'];
         $domain = $parts['domain'];
         // Escaped spaces are allowed in the "local"-part.
         $local = str_replace('\\ ', '', $local);
         // Spaces in quotes e.g. "firstname lastname"@foo.bar are also allowed in the "local"-part.
         $quoteHelperForIdn = false;
         if (preg_match('/^"(?<inner>[^"]*)"$/mU', $local, $parts)) {
             $quoteHelperForIdn = true;
             $local = trim(str_replace($parts['inner'], str_replace(' ', '', $parts['inner']), $local), '"');
         }
         if (strpos($local, ' ') !== false || strpos($local, '".') !== false) {
             return false;
         }
         if (function_exists('idn_to_ascii') && UTF8::max_chr_width($email) <= 3) {
             $localTmp = idn_to_ascii($local);
             if ($localTmp) {
                 $local = $localTmp;
             }
             unset($localTmp);
             $domainTmp = idn_to_ascii($domain);
             if ($domainTmp) {
                 $domain = $domainTmp;
             }
             unset($domainTmp);
         } else {
             static $punycode = null;
             if ($punycode === null) {
                 $punycode = new Punycode();
             }
             $local = $punycode->encode($local);
             $domain = $punycode->encode($domain);
         }
         if ($quoteHelperForIdn === true) {
             $local = '"' . $local . '"';
         }
         $email = $local . '@' . $domain;
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             return false;
         } else {
             $valid = true;
         }
         if ($useExampleDomainCheck === true && self::isExampleDomain($domain) === true) {
             return false;
         }
         if ($useTypoInDomainCheck === true && self::isTypoInDomain($domain) === true) {
             return false;
         }
         if ($useTemporaryDomainCheck === true && self::isTemporaryDomain($domain) === true) {
             return false;
         }
         if ($useDnsCheck === true && self::isDnsError($domain) === true) {
             return false;
         }
     }
     return $valid;
 }