Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.756.2.88 diff -u -p -r1.756.2.88 common.inc --- includes/common.inc 2 Jun 2010 18:52:32 -0000 1.756.2.88 +++ includes/common.inc 10 Aug 2010 21:33:15 -0000 @@ -955,20 +955,280 @@ function t($string, $args = array(), $la /** * Verify the syntax of the given e-mail address. * - * Empty e-mail addresses are allowed. See RFC 2822 for details. - * - * @param $mail + * Email Check By Dominic Sayers http://www.dominicsayers.com/isemail/. + * Keeping with standard string functions as not all the functions used + * here have a drupal equivalent + * @param $email * A string containing an e-mail address. * @return * TRUE if the address is in a valid format. */ -function valid_email_address($mail) { - $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+'; - $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+'; - $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}'; - $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}'; +function valid_email_address($email) { + + $email_length = strlen($email); + if ($email_length > 256) { + return FALSE; + } + $at_index = strrpos($email, '@'); + + if ($at_index === FALSE) { + return FALSE; + } + if ($at_index === 0) { + return FALSE; + } + if ($at_index === $email_length) { + return FALSE; + } + $brace_depth = 0; + $in_quote = FALSE; + $escape_this_char = FALSE; + + for ($i = 0; $i < $email_length; ++$i) { + $char = $email[$i]; + $replace_char = FALSE; + + if ($char === '\\') { + $escape_this_char = !$escape_this_char; + } + else { + switch ($char) { + case '(': + if ($escape_this_char) { + $replace_char = TRUE; + } + else { + if ($in_quote) { + $replace_char = TRUE; + } + else { + if ($brace_depth++ > 0) { + $replace_char = TRUE; + } + } + } + break; + + case ')': + if ($escape_this_char) { + $replace_char = TRUE; + } + else { + if ($in_quote) { + $replace_char = TRUE; + } + else { + if (--$brace_depth > 0) { + $replace_char = TRUE; + } + if ($brace_depth < 0) { + $brace_depth = 0; + } + } + } + break; + + case '"': + if ($escape_this_char) { + $replace_char = TRUE; + } + else { + if ($brace_depth === 0) { + $in_quote = !$in_quote; + } + else { + $replace_char = TRUE; + } + } + break; + + case '.': + if ($escape_this_char) { + $replace_char = TRUE; + } + else { + if ($brace_depth > 0) { + $replace_char = TRUE; + } + } + break; + + default: + } + + $escape_this_char = FALSE; + if ($replace_char) { + $email[$i] = 'x'; + } + } + } + + $local_part = substr($email, 0, $at_index); + $domain = substr($email, $at_index + 1); + $FWS = "(?:(?:(?:[ \\t]*(?:\\r\\n))?[ \\t]+)|(?:[ \\t]+(?:(?:\\r\\n)[ \\t]+)*))"; + $dot_array = preg_split('/\\.(?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/m', $local_part); + $part_length = 0; + + foreach ($dot_array as $element) { + $element = preg_replace("/^$FWS|$FWS\$/", '', $element); + $element_length = strlen($element); + + if ($element_length > 0 and $element[0] === '(') { + $index_brace = strpos($element, ')'); + if ($index_brace !== FALSE) { + if (preg_match('/(? 0) { + return FALSE; + } + $element = substr($element, $index_brace + 1, $element_length - $index_brace - 1); + $element_length = strlen($element); + } + } + + if ($element_length > 1 and $element[$element_length - 1] === ')') { + $index_brace = strrpos($element, '('); + if ($index_brace !== FALSE) { + if (preg_match('/(? 0) { + return FALSE; + } + $element = substr($element, 0, $index_brace); + $element_length = strlen($element); + } + } + $element = preg_replace("/^$FWS|$FWS\$/", '', $element); + if ($part_length > 0) { + $part_length++; + } + $part_length += strlen($element); + if (preg_match('/^"(?:.)*"$/s', $element) > 0) { + $element = preg_replace("/(? 0) { + return FALSE; + } + } + else { + if ($element === '') { + return FALSE; + } + if (preg_match('/[\\x00-\\x20\\(\\)<>\\[\\]:;@\\\\,\\."]/', $element) > 0) { + return FALSE; + } + } + } + + if ($part_length > 64) { + return FALSE; + } + if (preg_match('/^\\[(.)+]$/', $domain) === 1) { + $address_literal = substr($domain, 1, strlen($domain) - 2); + $matches_IP = array(); + 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]?)$/', $address_literal, $matches_IP) > 0) { + $index = strrpos($address_literal, $matches_IP[0]); + + if ($index === 0) { + return TRUE; + } + else { + if ($address_literal[$index - 1] !== ':') { + return FALSE; + } + if (substr($address_literal, 0, 5) !== 'IPv6:') { + return FALSE; + } - return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail); + $IP_v6 = substr($address_literal, 5, ($index ===7) ? 2 : $index - 6); + $group_max = 6; + } + } + else { + if (substr($address_literal, 0, 5) !== 'IPv6:') { + return FALSE; + } + $IP_v6 = substr($address_literal, 5); + $group_max = 8; + } + + $group_count = preg_match_all('/^[0-9a-fA-F]{0,4}|\\:[0-9a-fA-F]{0,4}|(.)/', $IP_v6, $matches_IP); + $index = strpos($IP_v6, '::'); + + if ($index === FALSE) { + if ($group_count !== $group_max) { + return FALSE; + } + } + else { + if ($index !== strrpos($IP_v6, '::')) { + return FALSE; + } + $group_max = ($index === 0 || $index === (strlen($IP_v6) - 2)) ? $group_max : $group_max - 1; + if ($group_count > $group_max) { + return FALSE; + } + } + array_multisort($matches_IP[1], SORT_DESC); + if ($matches_IP[1][0] !== '') { + return FALSE; + } + return TRUE; + } + else { + $dot_array = preg_split('/\\.(?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/m', $domain); + $part_length = 0; + + if (count($dot_array) === 1) { + return FALSE; + } + + foreach ($dot_array as $element) { + $element = preg_replace("/^$FWS|$FWS\$/", '', $element); + $element_length = strlen($element); + + if ($element_length > 0 and $element[0] === '(') { + $index_brace = strpos($element, ')'); + if ($index_brace !== FALSE) { + if (preg_match('/(? 0) { + return FALSE; + } + $element = substr($element, $index_brace + 1, $element_length - $index_brace - 1); + $element_length = strlen($element); + } + } + + if ($element_length > 0 and $element[$element_length - 1] === ')') { + $index_brace = strrpos($element, '('); + if ($index_brace !== FALSE) { + if (preg_match('/(? 0) { + return FALSE; + } + $element = substr($element, 0, $index_brace); + $element_length = strlen($element); + } + } + $element = preg_replace("/^$FWS|$FWS\$/", '', $element); + if ($part_length > 0) { + $part_length++; + } + $part_length += strlen($element); + if ($element_length > 63) { + return FALSE; + } + if ($element_length === 0) { + return FALSE; + } + if (preg_match('/[\\x00-\\x20\\(\\)<>\\[\\]:;@\\\\,\\."]|^-|-$/', $element) > 0) { + return FALSE; + } + } + + if ($part_length > 255) { + return FALSE; + } + + if (preg_match('/^[0-9]+$/', $element) > 0) { + return FALSE; + } + } + return TRUE; } /**