19declare(strict_types=1);
174 bool $nest_groups =
null,
184 if (isset($nest_groups)) {
185 $this->nestGroups = $nest_groups;
209 bool $nest_groups =
null,
213 if (!isset($this, $this->mailRFC822)) {
215 return $obj->parseAddressList();
224 if (isset($nest_groups)) {
225 $this->nestGroups = $nest_groups;
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) {
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);
299 $string = $this->_splitCheck(
$parts, $split_char);
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);
353 $string = $this->_splitCheck(
$parts,
',');
358 if (count(
$parts = explode(
':', $string)) > 1) {
359 $string2 = $this->_splitCheck(
$parts,
':');
360 return ($string2 !== $string);
378 if ($this->_hasUnclosedQuotes($string)
379 || $this->_hasUnclosedBrackets($string,
'<>')
380 || $this->_hasUnclosedBrackets($string,
'[]')
381 || $this->_hasUnclosedBrackets($string,
'()')
382 || substr($string, -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]);
446 $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
447 $this->_hasUnclosedBracketsSub($string, $num_angle_end, $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);
469 if (substr(
$parts[
$i], -1) ===
'\\' || $this->_hasUnclosedQuotes(
$parts[
$i])) {
491 if ($address[
'group']) {
495 $parts = explode(
':', $address[
'address']);
496 $groupname = $this->_splitCheck(
$parts,
':');
500 if (!$this->_validatePhrase($groupname)) {
501 $this->
error =
'Group name did not validate.';
505 if ($this->nestGroups) {
508 $structure =
new stdClass();
509 $structure->groupname = $groupname;
512 $address[
'address'] = ltrim(substr($address[
'address'], strlen($groupname .
':')));
518 while (strlen($address[
'address']) > 0) {
519 $parts = explode(
',', $address[
'address']);
520 $addresses[] = $this->_splitCheck(
$parts,
',');
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++) {
536 if (!$this->validateMailbox($addresses[
$i])) {
537 if (empty($this->
error)) {
538 $this->
error =
'Validation failed for: ' . $addresses[
$i];
545 if ($this->nestGroups) {
547 $structure->addresses = $addresses;
549 $structure = $addresses[0];
553 } elseif ($is_group) {
554 $structure = array_merge($structure, $addresses);
556 $structure = $addresses;
571 $parts = preg_split(
'/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
574 while (count(
$parts) > 0) {
575 $phrase_parts[] = $this->_splitCheck(
$parts,
' ');
576 for (
$i = 0;
$i < $this->index + 1;
$i++) {
581 foreach ($phrase_parts as $part) {
583 if (strpos($part,
'"') === 0) {
584 if (!$this->_validateQuotedString($part)) {
591 if (!$this->_validateAtom($part)) {
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);
674 $before_comment = $this->_splitCheck(
$parts,
'(');
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));
703 if ($this->_validatePhrase($phrase) ===
false ||
704 ($route_addr = $this->_validateRouteAddr($route_addr)) ===
false) {
711 if ($mailbox[0] ===
'<' && substr($mailbox, -1) ===
'>') {
712 $addr_spec = substr($mailbox, 1, -1);
714 $addr_spec = $mailbox;
717 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) ===
false) {
723 $mbox =
new stdClass();
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);
759 $route = $this->_splitCheck(
$parts,
':');
761 $route = $route_addr;
766 if ($route === $route_addr) {
768 $addr_spec = $route_addr;
769 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) ===
false) {
774 if (($route = $this->_validateRoute($route)) ===
false) {
778 $addr_spec = substr($route_addr, strlen($route .
':'));
781 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) ===
false) {
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));
810 if (!$this->_validateDomain($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) {
841 if (!$this->_validateSubdomain(trim($sub_domain))) {
859 if (preg_match(
'|^\[(.*)]$|', $subdomain, $arr)) {
860 if (!$this->_validateDliteral($arr[1])) {
863 } elseif (!$this->_validateAtom($subdomain)) {
880 return !preg_match(
'/(.)[][\x0D\\\\]/', $dliteral, $matches) &&
881 ((!isset($matches[1])) || $matches[1] !=
'\\');
894 $addr_spec = trim($addr_spec);
897 $validateState = $this->validate;
900 if (strpos($addr_spec,
'@') !==
false) {
901 $parts = explode(
'@', $addr_spec);
902 $local_part = $this->_splitCheck(
$parts,
'@');
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;
908 $domain = $this->default_domain;
913 $local_part = $addr_spec;
914 $domain = $this->default_domain;
917 if (($local_part = $this->_validateLocalPart($local_part)) ===
false) {
921 if ($validateState !== $this->validate) {
922 $this->validate = $validateState;
925 if (($domain = $this->_validateDomain($domain)) ===
false) {
930 return [
'local_part' => $local_part,
'domain' => $domain];
943 $parts = explode(
'.', $local_part);
947 while (count(
$parts) > 0) {
948 $words[] = $this->_splitCheck(
$parts,
'.');
949 for (
$i = 0;
$i < $this->index + 1;
$i++) {
955 foreach ($words as $word) {
963 if (strpos($word,
' ') && $word[0] !==
'"') {
971 if ($this->_validatePhrase(trim($word)) ===
false) {
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]];
_hasUnclosedBracketsSub(string $string, int &$num, string $char)
Sub function that is used only by hasUnclosedBrackets().
bool $validate
Whether or not to validate atoms for non-ascii characters.
string $error
The current error message, if any.
_validateRouteAddr(string $route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">".
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
_splitAddresses(string $address)
Splits an address into separate addresses.
_validateAddress(array $address)
Function to begin checking the address.
approximateCount(string $data)
Returns an approximate count of how many addresses are in the given string.
_validateDliteral(string $dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".
int $index
An internal counter/pointer.
_validateQuotedString(string $qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
parseAddressList(string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
Starts the whole process.
array $structure
The final array of parsed address information that we build up.
__construct(string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
Sets up the object.
string $default_domain
The default domain to use for unqualified addresses.
_validatePhrase(string $phrase)
Function to validate a phrase.
isValidInetAddress(string $data, bool $strict=false)
This is a email validating function separate to the rest of the class.
_validateRoute(string $route)
Function to validate a route, which is: route = 1#("@" domain) ":".
_validateDomain(string $domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
_isGroup(string $address)
Checks for a group at the start of the string.
int $num_groups
The number of groups that have been found in the address list.
validateMailbox(string &$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
_hasUnclosedBrackets(string $string, string $chars)
Checks if a string has an unclosed brackets or not.
bool $mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
array $addresses
The array of raw addresses built up as we parse.
_validateAtom(string $atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials,...
_hasUnclosedQuotes(string $string)
Checks if a string has unclosed quotes or not.
bool $nestGroups
Should we return a nested array showing groups, or flatten everything?
string $address
The address being parsed by the RFC822 object.
int $limit
A limit after which processing stops.
_validateLocalPart(string $local_part)
Function to validate the local part of an address: local-part = word *("." word)
_validateAddrSpec(string $addr_spec)
Function to validate an addr-spec.
_validateSubdomain(string $subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts