19 declare(strict_types=1);
172 string $address = null,
173 string $default_domain = null,
174 bool $nest_groups = null,
175 bool $validate = null,
178 if (isset($address)) {
181 if (isset($default_domain)) {
184 if (isset($nest_groups)) {
185 $this->nestGroups = $nest_groups;
187 if (isset($validate)) {
207 string $address = null,
208 string $default_domain = null,
209 bool $nest_groups = null,
210 bool $validate = null,
213 if (!isset($this, $this->mailRFC822)) {
214 $obj =
new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
215 return $obj->parseAddressList();
218 if (isset($address)) {
221 if (isset($default_domain)) {
224 if (isset($nest_groups)) {
225 $this->nestGroups = $nest_groups;
227 if (isset($validate)) {
234 $this->structure = [];
235 $this->addresses = [];
240 $this->address = preg_replace(
'/\r?\n/',
"\r\n", $this->address);
241 $this->address = preg_replace(
'/\r\n(\t| )+/',
' ', $this->address);
243 $tmp_address =
false;
245 $this->address = $tmp_address;
248 if ($tmp_address ===
false || isset($this->error)) {
256 foreach ($this->addresses as $address) {
259 if (
$valid ===
false || isset($this->error)) {
265 if (!$this->nestGroups) {
266 $this->structure = array_merge($this->structure,
$valid);
268 $this->structure[] =
$valid;
283 if (!empty($this->limit) && count($this->addresses) === $this->limit) {
287 if (!isset($this->error) && $this->
_isGroup($address)) {
290 } elseif (!isset($this->error)) {
293 } elseif (isset($this->error)) {
298 $parts = explode($split_char, $address);
307 if (strpos($string,
':') ===
false) {
308 $this->error =
'Invalid address: ' . $string;
313 if (!$this->
_splitCheck(explode(
':', $string),
':')) {
323 $this->addresses[] = [
324 'address' => trim($string),
325 'group' => $is_group,
330 $address = trim((
string) substr($address, strlen($string) + 1));
335 if ($is_group && $address[0] ===
',') {
336 $address = trim(substr($address, 1));
352 $parts = explode(
',', $address);
358 if (count(
$parts = explode(
':', $string)) > 1) {
360 return ($string2 !== $string);
377 for (
$i = 0, $iMax = count($parts);
$i < $iMax;
$i++) {
382 || substr($string, -1) ===
'\\') {
383 if (isset($parts[
$i + 1])) {
384 $string .= $char . $parts[
$i + 1];
386 $this->error =
'Invalid address spec. Unclosed bracket or quotes';
406 $string = trim($string);
407 $iMax = strlen($string);
411 for (;
$i < $iMax; ++
$i) {
412 switch ($string[
$i]) {
418 if ($slashes % 2 === 0) {
419 $in_quote = !$in_quote;
443 $num_angle_start = substr_count($string, $chars[0]);
444 $num_angle_end = substr_count($string, $chars[1]);
449 if ($num_angle_start < $num_angle_end) {
450 $this->error =
'Invalid address spec. Unmatched quote or bracket (' . $chars .
')';
454 return ($num_angle_start > $num_angle_end);
467 $parts = explode($char, $string);
472 if (isset(
$parts[$i + 1])) {
491 if ($address[
'group']) {
495 $parts = explode(
':', $address[
'address']);
501 $this->error =
'Group name did not validate.';
505 if ($this->nestGroups) {
509 $structure->groupname = $groupname;
512 $address[
'address'] = ltrim(substr($address[
'address'], strlen($groupname .
':')));
518 while (strlen($address[
'address']) > 0) {
519 $parts = explode(
',', $address[
'address']);
521 $address[
'address'] = trim(substr($address[
'address'], strlen(end($addresses) .
',')));
524 $addresses[] = $address[
'address'];
528 array_map(
'trim', $addresses);
535 for (
$i = 0, $iMax = count($addresses);
$i < $iMax;
$i++) {
537 if (empty($this->error)) {
538 $this->error =
'Validation failed for: ' . $addresses[
$i];
545 if ($this->nestGroups) {
549 $structure = $addresses[0];
553 } elseif ($is_group) {
554 $structure = array_merge($structure, $addresses);
571 $parts = preg_split(
'/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
574 while (count(
$parts) > 0) {
576 for (
$i = 0;
$i < $this->index + 1;
$i++) {
581 foreach ($phrase_parts as $part) {
583 if (strpos($part,
'"') === 0) {
613 if (!$this->validate) {
621 if (preg_match(
'/[][()<>@,;\\:". ]/', $atom)) {
626 if (preg_match(
'/[\\x00-\\x1F]+/', $atom)) {
631 if (!(
bool) preg_match(
'//u', $atom)) {
649 $qstring = substr($qstring, 1, -1);
652 return !preg_match(
'/[\x0D\\\\"]/', preg_replace(
'/\\\\./',
'', $qstring));
671 $_mailbox = $mailbox;
672 while (trim($_mailbox) !==
'') {
673 $parts = explode(
'(', $_mailbox);
675 if ($before_comment !== $_mailbox) {
677 $comment = substr(str_replace($before_comment,
'', $_mailbox), 1);
683 $_mailbox = substr($_mailbox, strpos($_mailbox,
'(' .
$comment) + strlen(
$comment) + 2);
690 $mailbox = str_replace(
"($comment)",
'', $mailbox);
693 $mailbox = trim($mailbox);
696 if (substr($mailbox, -1) ===
'>' && $mailbox[0] !==
'<') {
697 $parts = explode(
'<', $mailbox);
700 $phrase = trim(
$name);
701 $route_addr = trim(substr($mailbox, strlen(
$name .
'<'), -1));
711 if ($mailbox[0] ===
'<' && substr($mailbox, -1) ===
'>') {
712 $addr_spec = substr($mailbox, 1, -1);
714 $addr_spec = $mailbox;
726 $mbox->personal = $phrase;
729 if (isset($route_addr)) {
730 $mbox->mailbox = $route_addr[
'local_part'];
731 $mbox->host = $route_addr[
'domain'];
732 if ($route_addr[
'adl'] !==
'') {
733 $mbox->adl = $route_addr[
'adl'];
736 $mbox->mailbox = $addr_spec[
'local_part'];
737 $mbox->host = $addr_spec[
'domain'];
757 if (strpos($route_addr,
':') !==
false) {
758 $parts = explode(
':', $route_addr);
761 $route = $route_addr;
766 if ($route === $route_addr) {
768 $addr_spec = $route_addr;
778 $addr_spec = substr($route_addr, strlen($route .
':'));
787 $return[
'adl'] = $route;
792 $return = array_merge($return, $addr_spec);
806 $domains = explode(
',', trim($route));
808 foreach ($domains as $domain) {
809 $domain = str_replace(
'@',
'', trim($domain));
830 $subdomains = explode(
'.', $domain);
833 while (count($subdomains) > 0) {
834 $sub_domains[] = $this->
_splitCheck($subdomains,
'.');
835 for (
$i = 0;
$i < $this->index + 1;
$i++) {
836 array_shift($subdomains);
840 foreach ($sub_domains as $sub_domain) {
859 if (preg_match(
'|^\[(.*)]$|', $subdomain, $arr)) {
880 return !preg_match(
'/(.)[][\x0D\\\\]/', $dliteral, $matches) &&
881 ((!isset($matches[1])) || $matches[1] !=
'\\');
894 $addr_spec = trim($addr_spec);
900 if (strpos($addr_spec,
'@') !==
false) {
901 $parts = explode(
'@', $addr_spec);
903 $domain = substr($addr_spec, strlen($local_part .
'@'));
905 if (substr_count($addr_spec,
'@') !== 1 && $local_part ===
'') {
906 $this->validate =
false;
907 $local_part = $addr_spec;
913 $local_part = $addr_spec;
921 if ($validateState !== $this->validate) {
922 $this->validate = $validateState;
930 return [
'local_part' => $local_part,
'domain' => $domain];
943 $parts = explode(
'.', $local_part);
947 while (count(
$parts) > 0) {
949 for (
$i = 0;
$i < $this->index + 1;
$i++) {
955 foreach ($words as $word) {
963 if (strpos($word,
' ') && $word[0] !==
'"') {
992 return count(preg_split(
'/(?<!\\\\),/', $data));
1012 '/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' :
1013 '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
1014 if (preg_match($regex, trim($data), $matches)) {
1015 return [$matches[1], $matches[2]];
_validateQuotedString(string $qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> ...
_validateAddrSpec(string $addr_spec)
Function to validate an addr-spec.
int $num_groups
The number of groups that have been found in the address list.
_isGroup(string $address)
Checks for a group at the start of the string.
bool $nestGroups
Should we return a nested array showing groups, or flatten everything?
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
_hasUnclosedBrackets(string $string, string $chars)
Checks if a string has an unclosed brackets or not.
_validateLocalPart(string $local_part)
Function to validate the local part of an address: local-part = word *("." word)
_validateAddress(array $address)
Function to begin checking the address.
string $default_domain
The default domain to use for unqualified addresses.
__construct(string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
Sets up the object.
_validateDomain(string $domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
_hasUnclosedQuotes(string $string)
Checks if a string has unclosed quotes or not.
_validateRoute(string $route)
Function to validate a route, which is: route = 1#("@" domain) ":".
bool $validate
Whether or not to validate atoms for non-ascii characters.
_validateAtom(string $atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
_validateRouteAddr(string $route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"...
array $structure
The final array of parsed address information that we build up.
isValidInetAddress(string $data, bool $strict=false)
This is a email validating function separate to the rest of the class.
_validateSubdomain(string $subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
_hasUnclosedBracketsSub(string $string, int &$num, string $char)
Sub function that is used only by hasUnclosedBrackets().
bool $mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
int $limit
A limit after which processing stops.
string $error
The current error message, if any.
approximateCount(string $data)
Returns an approximate count of how many addresses are in the given string.
parseAddressList(string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
Starts the whole process.
validateMailbox(string &$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
string $address
The address being parsed by the RFC822 object.
_validateDliteral(string $dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"...
_splitAddresses(string $address)
Splits an address into separate addresses.
array $addresses
The array of raw addresses built up as we parse.
_validatePhrase(string $phrase)
Function to validate a phrase.
int $index
An internal counter/pointer.