Skip to content
Permalink
Browse files

optimize is_valid_email_address()

  • Loading branch information...
teiling88 committed May 14, 2017
1 parent 28e54f3 commit 0bdeab6b046631847afaab5703cd1a8ef62fb27e
Showing with 15 additions and 202 deletions.
  1. +15 −201 htdocs/lib2/util.inc.php
  2. +0 −1 tests/Modules/Lib2/UtilIncTest.php
@@ -3,197 +3,17 @@
* for license information see doc/license.txt
***************************************************************************/
/*
* RFC(2)822 Email Parser
*
* By Cal Henderson <cal@iamcal.com>
* This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
* http://creativecommons.org/licenses/by-sa/2.5/
*
* Revision 4
*/
/**
* @param $eMail
*
* @return int
*/
function is_valid_email_address($eMail)
{
/*
* NO-WS-CTL = %d1-8 / ; US-ASCII control characters
* %d11 / ; that do not include the
* %d12 / ; carriage return, line feed,
* %d14-31 / ; and white space characters
* %d127
* ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
* DIGIT = %x30-39
*/
$no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
$alpha = "[\\x41-\\x5a\\x61-\\x7a]";
$digit = "[\\x30-\\x39]";
$cr = "\\x0d";
$lf = "\\x0a";
$crlf = "($cr$lf)";
/*
*
*obs-char = %d0-9 / %d11 / ; %d0-127 except CR and
* %d12 / %d14-127 ; LF
* obs-text = *LF *CR *(obs-char *LF *CR)
* text = %d1-9 / ; Characters excluding CR and LF
* %d11 /
* %d12 /
* %d14-127 /
* obs-text
* obs-qp = "\" (%d0-127)
* quoted-pair = ("\" text) / obs-qp
*/
$obs_char = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
$obs_text = "($lf*$cr*($obs_char$lf*$cr*)*)";
$text = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|$obs_text)";
$obs_qp = "(\\x5c[\\x00-\\x7f])";
$quoted_pair = "(\\x5c$text|$obs_qp)";
/*
*
* obs-FWS = 1*WSP *(CRLF 1*WSP)
* FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
* obs-FWS
* ctext = NO-WS-CTL / ; Non white space controls
* %d33-39 / ; The rest of the US-ASCII
* %d42-91 / ; characters not including "(",
* %d93-126 ; ")", or "\"
* ccontent = ctext / quoted-pair / comment
* comment = "(" *([FWS] ccontent) [FWS] ")"
* CFWS = *([FWS] comment) (([FWS] comment) / FWS)
*/
/*
* note: we translate ccontent only partially to avoid an infinite loop
* instead, we'll recursively strip comments before processing the input
*/
$wsp = "[\\x20\\x09]";
$obs_fws = "($wsp+($crlf$wsp+)*)";
$fws = "((($wsp*$crlf)?$wsp+)|$obs_fws)";
$ctext = "($no_ws_ctl|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
$ccontent = "($ctext|$quoted_pair)";
$comment = "(\\x28($fws?$ccontent)*$fws?\\x29)";
$cfws = "(($fws?$comment)*($fws?$comment|$fws))";
$cfws = "$fws*";
/*
*
* atext = ALPHA / DIGIT / ; Any character except controls,
* "!" / "#" / ; SP, and specials.
* "$" / "%" / ; Used for atoms
* "&" / "'" /
* "*" / "+" /
* "-" / "/" /
* "=" / "?" /
* "^" / "_" /
* "" / "{" /
* "|" / "}" /
* "~"
* atom = [CFWS] 1*atext [CFWS]
*/
$atext = "($alpha|$digit|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
$atom = "($cfws?$atext+$cfws?)";
/*
*
* qtext = NO-WS-CTL / ; Non white space controls
* %d33 / ; The rest of the US-ASCII
* %d35-91 / ; characters not including "\"
* %d93-126 ; or the quote character
* qcontent = qtext / quoted-pair
* quoted-string = [CFWS]
* DQUOTE *([FWS] qcontent) [FWS] DQUOTE
* [CFWS]
* word = atom / quoted-string
*/
$qtext = "($no_ws_ctl|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
$qcontent = "($qtext|$quoted_pair)";
$quoted_string = "($cfws?\\x22($fws?$qcontent)*$fws?\\x22$cfws?)";
$word = "($atom|$quoted_string)";
/*
*
* obs-local-part = word *("." word)
* obs-domain = atom *("." atom)
*/
$obs_local_part = "($word(\\x2e$word)*)";
$obs_domain = "($atom(\\x2e$atom)*)";
/*
*
* dot-atom-text = 1*atext *("." 1*atext)
* dot-atom = [CFWS] dot-atom-text [CFWS]
*/
$dot_atom_text = "($atext+(\\x2e$atext+)*)";
$dot_atom = "($cfws?$dot_atom_text$cfws?)";
/*
*
* domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]
* dcontent = dtext / quoted-pair
* dtext = NO-WS-CTL / ; Non white space controls
*
* %d33-90 / ; The rest of the US-ASCII
* %d94-126 ; characters not including "[",
* ; "]", or "\"
*/
$dtext = "($no_ws_ctl|[\\x21-\\x5a\\x5e-\\x7e])";
$dcontent = "($dtext|$quoted_pair)";
$domain_literal = "($cfws?\\x5b($fws?$dcontent)*$fws?\\x5d$cfws?)";
/*
*
* local-part = dot-atom / quoted-string / obs-local-part
* domain = dot-atom / domain-literal / obs-domain
* addr-spec = local-part "@" domain
*/
$local_part = "($dot_atom|$quoted_string|$obs_local_part)";
$domain = "($dot_atom|$domain_literal|$obs_domain)";
$addr_spec = "($local_part\\x40$domain)";
/*
* we need to strip comments first (repeat until we can't find any more)
*/
$done = 0;
while (!$done) {
$new = preg_replace("!$comment!", '', $eMail);
if (strlen($new) == strlen($eMail)) {
$done = 1;
}
$eMail = $new;
}
/*
* now match what's left
*/
return preg_match("!^$addr_spec$!", $eMail) ? 1 : 0;
return (int) ($eMail === filter_var($eMail, FILTER_VALIDATE_EMAIL));
}
/**
* @param $str
*
* @return string
*/
function mb_trim($str)
@@ -211,7 +31,7 @@ function mb_trim($str)
$bLoop = true;
while ($bLoop === true) {
$sPos = mb_substr($str, - 1, 1);
$sPos = mb_substr($str, -1, 1);
if ($sPos === ' ' || $sPos === "\r" || $sPos === "\n" || $sPos === "\t" || $sPos === "\x0B" || $sPos === "\0") {
$str = mb_substr($str, 0, mb_strlen($str) - 1);
@@ -228,18 +48,17 @@ function mb_trim($str)
*
* @param $str
* @param $sep
*
* @return array
*/
function explode_multi($str, $sep)
{
$ret = array();
$ret = [];
$nCurPos = 0;
while ($nCurPos < mb_strlen($str)) {
$nNextSep = mb_strlen($str);
$sepLength = mb_strlen($sep);
for ($nSepPos = 0; $nSepPos < $sepLength; $nSepPos ++) {
for ($nSepPos = 0; $nSepPos < $sepLength; $nSepPos++) {
$nThisPos = mb_strpos($str, mb_substr($sep, $nSepPos, 1), $nCurPos);
if ($nThisPos !== false && $nNextSep > $nThisPos) {
$nNextSep = $nThisPos;
@@ -257,7 +76,6 @@ function explode_multi($str, $sep)
/**
* @param string $name
* @param string $default
*
* @return string
*/
function getSysConfig($name, $default)
@@ -281,7 +99,6 @@ function setSysConfig($name, $value)
/**
* @param $filename
* @param $maxLength
*
* @return bool|string
*/
function read_file($filename, $maxLength = 4096)
@@ -305,7 +122,6 @@ function read_file($filename, $maxLength = 4096)
* encodes &<>"'
*
* @param $str
*
* @return string
*/
function xmlentities($str)
@@ -319,7 +135,6 @@ function xmlentities($str)
* This is ok for XML content text between tags, but not for XML attribute contents.
*
* @param $str
*
* @return string
*/
function text_xmlentities($str)
@@ -329,7 +144,6 @@ function text_xmlentities($str)
/**
* @param $str
*
* @return string
*/
function xmlfilterevilchars($str)
@@ -343,14 +157,13 @@ function xmlfilterevilchars($str)
* decimal longitude to string E/W hhh°mm.mmm
*
* @param $lon
*
* @return string
*/
function help_lonToDegreeStr($lon)
{
if ($lon < 0) {
$retVal = 'W ';
$lon = - $lon;
$lon = -$lon;
} else {
$retVal = 'E ';
}
@@ -366,14 +179,13 @@ function help_lonToDegreeStr($lon)
* decimal latitude to string N/S hh°mm.mmm
*
* @param $lat
*
* @return string
*/
function help_latToDegreeStr($lat)
{
if ($lat < 0) {
$retVal = 'S ';
$lat = - $lat;
$lat = -$lat;
} else {
$retVal = 'N ';
}
@@ -388,24 +200,27 @@ function help_latToDegreeStr($lat)
/**
* @param $text
*
* @return string
*/
function escape_javascript($text)
{
return str_replace('\'', '\\\'', str_replace('"', '&quot;', $text));
return str_replace(
['\'', '"',],
['\\\'', '&quot;'],
$text
);
}
/**
* perform str_rot13 without renaming parts in []
*
* @param $str
*
* @return string
*/
function str_rot13_gc($str)
{
/** @var array $delimiter */
$delimiter[0][0] = '[';
$delimiter[0][1] = ']';
@@ -426,7 +241,7 @@ function str_rot13_gc($str)
}
if ($nNextStart === false) {
$retVal .= str_rot13(mb_substr($str, mb_strlen($retVal), mb_strlen($str) - mb_strlen($retVal)));
$retVal .= str_rot13(mb_substr($str, mb_strlen($retVal)));
} else {
// crypted part
$retVal .= str_rot13(mb_substr($str, mb_strlen($retVal), $nNextStart - mb_strlen($retVal)));
@@ -450,7 +265,6 @@ function str_rot13_gc($str)
* format number with 1000s dots
*
* @param $n
*
* @return mixed|string
*/
function number1000($n)
@@ -461,7 +275,7 @@ function number1000($n)
$opt['locale'][$opt['template']['locale']]['format']['dot1000'] === ','
) {
return number_format($n);
} else {
return str_replace(',', '.', number_format($n));
}
return str_replace(',', '.', number_format($n));
}
@@ -54,7 +54,6 @@ public function testIsValidEmailAddress()
{
self::assertTrue((bool) \is_valid_email_address('example@test.com'));
self::assertFalse((bool) \is_valid_email_address('example'));
self::assertFalse((bool) \is_valid_email_address('ääää1230@test.de'));
self::assertFalse((bool) \is_valid_email_address('!"/()!)"@test.de'));

0 comments on commit 0bdeab6

Please sign in to comment.
You can’t perform that action at this time.