ILIAS  release_8 Revision v8.24
Mail_RFC822 Class Reference
+ Collaboration diagram for Mail_RFC822:

Public Member Functions

 __construct (string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
 Sets up the object. More...
 
 parseAddressList (string $address=null, string $default_domain=null, bool $nest_groups=null, bool $validate=null, int $limit=null)
 Starts the whole process. More...
 
 validateMailbox (string &$mailbox)
 Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; name and route-addr. More...
 
 approximateCount (string $data)
 Returns an approximate count of how many addresses are in the given string. More...
 
 isValidInetAddress (string $data, bool $strict=false)
 This is a email validating function separate to the rest of the class. More...
 

Data Fields

string $address = ''
 The address being parsed by the RFC822 object. More...
 
string $default_domain = 'localhost'
 The default domain to use for unqualified addresses. More...
 
bool $nestGroups = true
 Should we return a nested array showing groups, or flatten everything? More...
 
bool $validate = true
 Whether or not to validate atoms for non-ascii characters. More...
 
array $addresses = []
 The array of raw addresses built up as we parse. More...
 
array $structure = []
 The final array of parsed address information that we build up. More...
 
string $error = null
 The current error message, if any. More...
 
int $index = null
 An internal counter/pointer. More...
 
int $num_groups = 0
 The number of groups that have been found in the address list. More...
 
bool $mailRFC822 = true
 A variable so that we can tell whether or not we're inside a Mail_RFC822 object. More...
 
int $limit = null
 A limit after which processing stops. More...
 

Protected Member Functions

 _splitAddresses (string $address)
 Splits an address into separate addresses. More...
 
 _isGroup (string $address)
 Checks for a group at the start of the string. More...
 
 _splitCheck (array $parts, string $char)
 A common function that will check an exploded string. More...
 
 _hasUnclosedQuotes (string $string)
 Checks if a string has unclosed quotes or not. More...
 
 _hasUnclosedBrackets (string $string, string $chars)
 Checks if a string has an unclosed brackets or not. More...
 
 _hasUnclosedBracketsSub (string $string, int &$num, string $char)
 Sub function that is used only by hasUnclosedBrackets(). More...
 
 _validateAddress (array $address)
 Function to begin checking the address. More...
 
 _validatePhrase (string $phrase)
 Function to validate a phrase. More...
 
 _validateAtom (string $atom)
 Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs> More...
 
 _validateQuotedString (string $qstring)
 Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> More...
 
 _validateRouteAddr (string $route_addr)
 This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">". More...
 
 _validateRoute (string $route)
 Function to validate a route, which is: route = 1#("@" domain) ":". More...
 
 _validateDomain (string $domain)
 Function to validate a domain, though this is not quite what you expect of a strict internet domain. More...
 
 _validateSubdomain (string $subdomain)
 Function to validate a subdomain: subdomain = domain-ref / domain-literal. More...
 
 _validateDliteral (string $dliteral)
 Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]". More...
 
 _validateAddrSpec (string $addr_spec)
 Function to validate an addr-spec. More...
 
 _validateLocalPart (string $local_part)
 Function to validate the local part of an address: local-part = word *("." word) More...
 

Detailed Description

Definition at line 91 of file RFC822.php.

Constructor & Destructor Documentation

◆ __construct()

Mail_RFC822::__construct ( string  $address = null,
string  $default_domain = null,
bool  $nest_groups = null,
bool  $validate = null,
int  $limit = null 
)

Sets up the object.

The address must either be set here or when calling parseAddressList(). One or the other.

Parameters
string$addressThe address(es) to validate.
string$default_domainDefault domain/host etc. If not supplied, will be set to localhost.
bool$nest_groupsWhether to return the structure with groups nested for easier viewing.
bool$validateWhether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.

Definition at line 171 of file RFC822.php.

177 {
178 if (isset($address)) {
179 $this->address = $address;
180 }
181 if (isset($default_domain)) {
182 $this->default_domain = $default_domain;
183 }
184 if (isset($nest_groups)) {
185 $this->nestGroups = $nest_groups;
186 }
187 if (isset($validate)) {
188 $this->validate = $validate;
189 }
190 if (isset($limit)) {
191 $this->limit = $limit;
192 }
193 }
bool $validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:115
string $default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:103
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:97
int $limit
A limit after which processing stops.
Definition: RFC822.php:159

References $address, $default_domain, $limit, and $validate.

Member Function Documentation

◆ _hasUnclosedBrackets()

Mail_RFC822::_hasUnclosedBrackets ( string  $string,
string  $chars 
)
protected

Checks if a string has an unclosed brackets or not.

IMPORTANT: This function handles both angle brackets and square brackets;

Parameters
string$stringThe string to check.
string$charsThe characters to check for.
Returns
bool True if there are unclosed brackets inside the string, false otherwise.

Definition at line 441 of file RFC822.php.

441 : bool
442 {
443 $num_angle_start = substr_count($string, $chars[0]);
444 $num_angle_end = substr_count($string, $chars[1]);
445
446 $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
447 $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
448
449 if ($num_angle_start < $num_angle_end) {
450 $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
451 return false;
452 }
453
454 return ($num_angle_start > $num_angle_end);
455 }
_hasUnclosedBracketsSub(string $string, int &$num, string $char)
Sub function that is used only by hasUnclosedBrackets().
Definition: RFC822.php:465
error(string $a_errmsg)

References error().

+ Here is the call graph for this function:

◆ _hasUnclosedBracketsSub()

Mail_RFC822::_hasUnclosedBracketsSub ( string  $string,
int &  $num,
string  $char 
)
protected

Sub function that is used only by hasUnclosedBrackets().

Parameters
string$stringThe string to check.
int&$numThe number of occurences.
string$charThe character to count.
Returns
int The number of occurences of $char in $string, adjusted for backslashes.

Definition at line 465 of file RFC822.php.

465 : int
466 {
467 $parts = explode($char, $string);
468 for ($i = 0, $iMax = count($parts); $i < $iMax; $i++) {
469 if (substr($parts[$i], -1) === '\\' || $this->_hasUnclosedQuotes($parts[$i])) {
470 $num--;
471 }
472 if (isset($parts[$i + 1])) {
473 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
474 }
475 }
476
477 return $num;
478 }
_hasUnclosedQuotes(string $string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:404
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
$i
Definition: metadata.php:41

References $i, and $parts.

◆ _hasUnclosedQuotes()

Mail_RFC822::_hasUnclosedQuotes ( string  $string)
protected

Checks if a string has unclosed quotes or not.

Parameters
string$stringThe string to check.
Returns
bool True if there are unclosed quotes inside the string, false otherwise.

Definition at line 404 of file RFC822.php.

404 : bool
405 {
406 $string = trim($string);
407 $iMax = strlen($string);
408 $in_quote = false;
409 $i = $slashes = 0;
410
411 for (; $i < $iMax; ++$i) {
412 switch ($string[$i]) {
413 case '\\':
414 ++$slashes;
415 break;
416
417 case '"':
418 if ($slashes % 2 === 0) {
419 $in_quote = !$in_quote;
420 }
421 // Fall through to default action below.
422
423 // no break
424 default:
425 $slashes = 0;
426 break;
427 }
428 }
429
430 return $in_quote;
431 }

References $i.

◆ _isGroup()

Mail_RFC822::_isGroup ( string  $address)
protected

Checks for a group at the start of the string.

Parameters
string$addressThe address to check.
Returns
bool Whether or not there is a group at the start of the string.

Definition at line 349 of file RFC822.php.

349 : bool
350 {
351 // First comma not in quotes, angles or escaped:
352 $parts = explode(',', $address);
353 $string = $this->_splitCheck($parts, ',');
354
355 // Now we have the first address, we can reliably check for a
356 // group by searching for a colon that's not escaped or in
357 // quotes or angle brackets.
358 if (count($parts = explode(':', $string)) > 1) {
359 $string2 = $this->_splitCheck($parts, ':');
360 return ($string2 !== $string);
361 }
362
363 return false;
364 }
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:373

References $parts.

◆ _splitAddresses()

Mail_RFC822::_splitAddresses ( string  $address)
protected

Splits an address into separate addresses.

Parameters
string$addressThe addresses to split.
Returns
bool|string

Definition at line 281 of file RFC822.php.

282 {
283 if (!empty($this->limit) && count($this->addresses) === $this->limit) {
284 return false;
285 }
286
287 if (!isset($this->error) && $this->_isGroup($address)) {
288 $split_char = ';';
289 $is_group = true;
290 } elseif (!isset($this->error)) {
291 $split_char = ',';
292 $is_group = false;
293 } elseif (isset($this->error)) {
294 return false;
295 }
296
297 // Split the string based on the above ten or so lines.
298 $parts = explode($split_char, $address);
299 $string = $this->_splitCheck($parts, $split_char);
300
301 // If a group...
302 if ($is_group) {
303 // If $string does not contain a colon outside of
304 // brackets/quotes etc then something's fubar.
305
306 // First check there's a colon at all:
307 if (strpos($string, ':') === false) {
308 $this->error = 'Invalid address: ' . $string;
309 return false;
310 }
311
312 // Now check it's outside of brackets/quotes:
313 if (!$this->_splitCheck(explode(':', $string), ':')) {
314 return false;
315 }
316
317 // We must have a group at this point, so increase the counter:
318 $this->num_groups++;
319 }
320
321 // $string now contains the first full address/group.
322 // Add to the addresses array.
323 $this->addresses[] = [
324 'address' => trim($string),
325 'group' => $is_group,
326 ];
327
328 // Remove the now stored address from the initial line, the +1
329 // is to account for the explode character.
330 $address = trim((string) substr($address, strlen($string) + 1));
331
332 // If the next char is a comma and this was a group, then
333 // there are more addresses, otherwise, if there are any more
334 // chars, then there is another address.
335 if ($is_group && $address[0] === ',') {
336 $address = trim(substr($address, 1));
337 return $address;
338 }
339
340 return $address;
341 }
_isGroup(string $address)
Checks for a group at the start of the string.
Definition: RFC822.php:349

References $parts, and error().

+ Here is the call graph for this function:

◆ _splitCheck()

Mail_RFC822::_splitCheck ( array  $parts,
string  $char 
)
protected

A common function that will check an exploded string.

Parameters
array$partsThe exloded string.
string$charThe char that was exploded on.
Returns
string An empty string if the string contains unclosed quotes/brackets, or the string on success.

Definition at line 373 of file RFC822.php.

373 : string
374 {
375 $string = $parts[0];
376
377 for ($i = 0, $iMax = count($parts); $i < $iMax; $i++) {
378 if ($this->_hasUnclosedQuotes($string)
379 || $this->_hasUnclosedBrackets($string, '<>')
380 || $this->_hasUnclosedBrackets($string, '[]')
381 || $this->_hasUnclosedBrackets($string, '()')
382 || substr($string, -1) === '\\') {
383 if (isset($parts[$i + 1])) {
384 $string .= $char . $parts[$i + 1];
385 } else {
386 $this->error = 'Invalid address spec. Unclosed bracket or quotes';
387 return '';
388 }
389 } else {
390 $this->index = $i;
391 break;
392 }
393 }
394
395 return $string;
396 }
_hasUnclosedBrackets(string $string, string $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:441

References $i, $parts, and error().

+ Here is the call graph for this function:

◆ _validateAddress()

Mail_RFC822::_validateAddress ( array  $address)
protected

Function to begin checking the address.

Parameters
array$addressThe address to validate.
Returns
mixed False on failure, or a structured array of address information on success.

Definition at line 486 of file RFC822.php.

487 {
488 $is_group = false;
489 $addresses = [];
490
491 if ($address['group']) {
492 $is_group = true;
493
494 // Get the group part of the name
495 $parts = explode(':', $address['address']);
496 $groupname = $this->_splitCheck($parts, ':');
497 $structure = [];
498
499 // And validate the group part of the name.
500 if (!$this->_validatePhrase($groupname)) {
501 $this->error = 'Group name did not validate.';
502 return false;
503 }
504
505 if ($this->nestGroups) {
506 // Don't include groups if we are not nesting
507 // them. This avoids returning invalid addresses.
508 $structure = new stdClass();
509 $structure->groupname = $groupname;
510 }
511
512 $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
513 }
514
515 // If a group then split on comma and put into an array.
516 // Otherwise, Just put the whole address in an array.
517 if ($is_group) {
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) . ',')));
522 }
523 } else {
524 $addresses[] = $address['address'];
525 }
526
527 // Trim the whitespace from all of the address strings.
528 array_map('trim', $addresses);
529
530 // Validate each mailbox.
531 // Format could be one of: name <geezer@domain.com>
532 // geezer@domain.com
533 // geezer
534 // ... or any other format valid by RFC 822.
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];
539 }
540 return false;
541 }
542 }
543
544 // Nested format
545 if ($this->nestGroups) {
546 if ($is_group) {
547 $structure->addresses = $addresses;
548 } else {
550 }
551
552 // Flat format
553 } elseif ($is_group) {
554 $structure = array_merge($structure, $addresses);
555 } else {
557 }
558
559 return $structure;
560 }
array $structure
The final array of parsed address information that we build up.
Definition: RFC822.php:127
_validatePhrase(string $phrase)
Function to validate a phrase.
Definition: RFC822.php:568
validateMailbox(string &$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:663
array $addresses
The array of raw addresses built up as we parse.
Definition: RFC822.php:121

References $i, $parts, and error().

+ Here is the call graph for this function:

◆ _validateAddrSpec()

Mail_RFC822::_validateAddrSpec ( string  $addr_spec)
protected

Function to validate an addr-spec.

addr-spec = local-part "@" domain

Parameters
string$addr_specThe string to check.
Returns
mixed False on failure, or the validated addr-spec on success.

Definition at line 892 of file RFC822.php.

893 {
894 $addr_spec = trim($addr_spec);
895
896 // mjansen patch 16 Sep 2016 start
897 $validateState = $this->validate;
898 // mjansen patch 16 Sep 2016 end
899 // Split on @ sign if there is one.
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 . '@'));
904 // mjansen patch 16 Sep 2016 start
905 if (substr_count($addr_spec, '@') !== 1 && $local_part === '') {
906 $this->validate = false;
907 $local_part = $addr_spec;
908 $domain = $this->default_domain;
909 }
910 // mjansen patch 16 Sep 2016 end
911 // No @ sign so assume the default domain.
912 } else {
913 $local_part = $addr_spec;
914 $domain = $this->default_domain;
915 }
916
917 if (($local_part = $this->_validateLocalPart($local_part)) === false) {
918 return false;
919 }
920 // mjansen patch 16 Sep 2016 start
921 if ($validateState !== $this->validate) {
922 $this->validate = $validateState;
923 }
924 // mjansen patch 16 Sep 2016 end
925 if (($domain = $this->_validateDomain($domain)) === false) {
926 return false;
927 }
928
929 // Got here so return successful.
930 return ['local_part' => $local_part, 'domain' => $domain];
931 }
_validateDomain(string $domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
Definition: RFC822.php:827
_validateLocalPart(string $local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:941

References $parts.

◆ _validateAtom()

Mail_RFC822::_validateAtom ( string  $atom)
protected

Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>

If validation ($this->validate) has been turned off, then validateAtom() doesn't actually check anything. This is so that you can split a list of addresses up before encoding personal names (umlauts, etc.), for example.

Parameters
string$atomThe string to check.
Returns
bool Success or failure.

Definition at line 611 of file RFC822.php.

611 : bool
612 {
613 if (!$this->validate) {
614 // Validation has been turned off; assume the atom is okay.
615 return true;
616 }
617
618 // Check for any char from ASCII 0 - ASCII 127
619 // mjansen patch 16 Sep 2015 start
620 // Check for specials:
621 if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
622 return false;
623 }
624
625 // Check for control characters (ASCII 0-31):
626 if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
627 return false;
628 }
629 #16291
630 #17618
631 if (!(bool) preg_match('//u', $atom)) {
632 return false;
633 }
634 // mjansen patch 16 Sep 2015 end
635
636 return true;
637 }

◆ _validateDliteral()

Mail_RFC822::_validateDliteral ( string  $dliteral)
protected

Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".

Parameters
string$dliteralThe string to check.
Returns
bool Success or failure.

Definition at line 878 of file RFC822.php.

878 : bool
879 {
880 return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) &&
881 ((!isset($matches[1])) || $matches[1] != '\\');
882 }

◆ _validateDomain()

Mail_RFC822::_validateDomain ( string  $domain)
protected

Function to validate a domain, though this is not quite what you expect of a strict internet domain.

domain = sub-domain *("." sub-domain)

Parameters
string$domainThe string to check.
Returns
mixed False on failure, or the validated domain on success.

Definition at line 827 of file RFC822.php.

828 {
829 // Note the different use of $subdomains and $sub_domains
830 $subdomains = explode('.', $domain);
831
832 $sub_domains = [];
833 while (count($subdomains) > 0) {
834 $sub_domains[] = $this->_splitCheck($subdomains, '.');
835 for ($i = 0; $i < $this->index + 1; $i++) {
836 array_shift($subdomains);
837 }
838 }
839
840 foreach ($sub_domains as $sub_domain) {
841 if (!$this->_validateSubdomain(trim($sub_domain))) {
842 return false;
843 }
844 }
845
846 // Managed to get here, so return input.
847 return $domain;
848 }
_validateSubdomain(string $subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
Definition: RFC822.php:857

References $i.

◆ _validateLocalPart()

Mail_RFC822::_validateLocalPart ( string  $local_part)
protected

Function to validate the local part of an address: local-part = word *("." word)

@access private

Parameters
string$local_part
Returns
mixed False on failure, or the validated local part on success.

Definition at line 941 of file RFC822.php.

942 {
943 $parts = explode('.', $local_part);
944 $words = [];
945
946 // Split the local_part into words.
947 while (count($parts) > 0) {
948 $words[] = $this->_splitCheck($parts, '.');
949 for ($i = 0; $i < $this->index + 1; $i++) {
950 array_shift($parts);
951 }
952 }
953
954 // Validate each word.
955 foreach ($words as $word) {
956 // iszmais patch 19 May 2020 start
957 // word cannot be empty (#17317)
958 //if ($word === '') {
959 // return false;
960 //}
961 // iszmais patch 19 May 2020 end
962 // If this word contains an unquoted space, it is invalid. (6.2.4)
963 if (strpos($word, ' ') && $word[0] !== '"') {
964 // mjansen patch 24 Feb 2016 start
965 // Mantis issue #18018
966 // # http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
967 //return false;
968 // mjansen patch 24 Feb 2016 end
969 }
970
971 if ($this->_validatePhrase(trim($word)) === false) {
972 return false;
973 }
974 }
975
976 // Managed to get here, so return the input.
977 return $local_part;
978 }

References $i, and $parts.

◆ _validatePhrase()

Mail_RFC822::_validatePhrase ( string  $phrase)
protected

Function to validate a phrase.

Parameters
string$phraseThe phrase to check.
Returns
bool Success or failure.

Definition at line 568 of file RFC822.php.

568 : bool
569 {
570 // Splits on one or more Tab or space.
571 $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
572
573 $phrase_parts = [];
574 while (count($parts) > 0) {
575 $phrase_parts[] = $this->_splitCheck($parts, ' ');
576 for ($i = 0; $i < $this->index + 1; $i++) {
577 array_shift($parts);
578 }
579 }
580
581 foreach ($phrase_parts as $part) {
582 // If quoted string:
583 if (strpos($part, '"') === 0) {
584 if (!$this->_validateQuotedString($part)) {
585 return false;
586 }
587 continue;
588 }
589
590 // Otherwise it's an atom:
591 if (!$this->_validateAtom($part)) {
592 return false;
593 }
594 }
595
596 return true;
597 }
_validateQuotedString(string $qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
Definition: RFC822.php:646
_validateAtom(string $atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials,...
Definition: RFC822.php:611

References $i, and $parts.

◆ _validateQuotedString()

Mail_RFC822::_validateQuotedString ( string  $qstring)
protected

Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">

Parameters
string$qstringThe string to check
Returns
bool Success or failure.

Definition at line 646 of file RFC822.php.

646 : bool
647 {
648 // Leading and trailing "
649 $qstring = substr($qstring, 1, -1);
650
651 // Perform check, removing quoted characters first.
652 return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
653 }

◆ _validateRoute()

Mail_RFC822::_validateRoute ( string  $route)
protected

Function to validate a route, which is: route = 1#("@" domain) ":".

Parameters
string$routeThe string to check.
Returns
mixed False on failure, or the validated $route on success.

Definition at line 803 of file RFC822.php.

804 {
805 // Split on comma.
806 $domains = explode(',', trim($route));
807
808 foreach ($domains as $domain) {
809 $domain = str_replace('@', '', trim($domain));
810 if (!$this->_validateDomain($domain)) {
811 return false;
812 }
813 }
814
815 return $route;
816 }

◆ _validateRouteAddr()

Mail_RFC822::_validateRouteAddr ( string  $route_addr)
protected

This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">".

Angle brackets have already been removed at the point of getting to this function.

Parameters
string$route_addrThe string to check.
Returns
mixed False on failure, or an array containing validated address/route information on success.

Definition at line 754 of file RFC822.php.

755 {
756 // Check for colon.
757 if (strpos($route_addr, ':') !== false) {
758 $parts = explode(':', $route_addr);
759 $route = $this->_splitCheck($parts, ':');
760 } else {
761 $route = $route_addr;
762 }
763
764 // If $route is same as $route_addr then the colon was in
765 // quotes or brackets or, of course, non existent.
766 if ($route === $route_addr) {
767 unset($route);
768 $addr_spec = $route_addr;
769 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
770 return false;
771 }
772 } else {
773 // Validate route part.
774 if (($route = $this->_validateRoute($route)) === false) {
775 return false;
776 }
777
778 $addr_spec = substr($route_addr, strlen($route . ':'));
779
780 // Validate addr-spec part.
781 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
782 return false;
783 }
784 }
785
786 if (isset($route)) {
787 $return['adl'] = $route;
788 } else {
789 $return['adl'] = '';
790 }
791
792 $return = array_merge($return, $addr_spec);
793 return $return;
794 }
_validateRoute(string $route)
Function to validate a route, which is: route = 1#("@" domain) ":".
Definition: RFC822.php:803
_validateAddrSpec(string $addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:892

References $parts.

◆ _validateSubdomain()

Mail_RFC822::_validateSubdomain ( string  $subdomain)
protected

Function to validate a subdomain: subdomain = domain-ref / domain-literal.

Parameters
string$subdomainThe string to check.
Returns
bool Success or failure.

Definition at line 857 of file RFC822.php.

857 : bool
858 {
859 if (preg_match('|^\[(.*)]$|', $subdomain, $arr)) {
860 if (!$this->_validateDliteral($arr[1])) {
861 return false;
862 }
863 } elseif (!$this->_validateAtom($subdomain)) {
864 return false;
865 }
866
867 // Got here, so return successful.
868 return true;
869 }
_validateDliteral(string $dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".
Definition: RFC822.php:878

◆ approximateCount()

Mail_RFC822::approximateCount ( string  $data)

Returns an approximate count of how many addresses are in the given string.

This is APPROXIMATE as it only splits based on a comma which has no preceding backslash. Could be useful as large amounts of addresses will end up producing large structures when used with parseAddressList().

Parameters
string$dataAddresses to count
Returns
int Approximate count

Definition at line 990 of file RFC822.php.

990 : int
991 {
992 return count(preg_split('/(?<!\\\\),/', $data));
993 }

References $data.

◆ isValidInetAddress()

Mail_RFC822::isValidInetAddress ( string  $data,
bool  $strict = false 
)

This is a email validating function separate to the rest of the class.

It simply validates whether an email is of the common internet form: <user><domain>. This can be sufficient for most people. Optional stricter mode can be utilised which restricts mailbox characters allowed to alphanumeric, full stop, hyphen and underscore.

Parameters
string$dataAddress to check
bool$strictOptional stricter mode
Returns
mixed False if it fails, an indexed array username/domain if it matches

Definition at line 1008 of file RFC822.php.

1009 {
1010 $regex =
1011 $strict ?
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]];
1016 }
1017
1018 return false;
1019 }

References $data.

◆ parseAddressList()

Mail_RFC822::parseAddressList ( string  $address = null,
string  $default_domain = null,
bool  $nest_groups = null,
bool  $validate = null,
int  $limit = null 
)

Starts the whole process.

The address must either be set here or when creating the object. One or the other.

Parameters
string$addressThe address(es) to validate.
string$default_domainDefault domain/host etc.
bool$nest_groupsWhether to return the structure with groups nested for easier viewing.
bool$validateWhether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
Returns
array A structured array of addresses.

Definition at line 206 of file RFC822.php.

212 : array {
213 if (!isset($this, $this->mailRFC822)) {
214 $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
215 return $obj->parseAddressList();
216 }
217
218 if (isset($address)) {
219 $this->address = $address;
220 }
221 if (isset($default_domain)) {
222 $this->default_domain = $default_domain;
223 }
224 if (isset($nest_groups)) {
225 $this->nestGroups = $nest_groups;
226 }
227 if (isset($validate)) {
228 $this->validate = $validate;
229 }
230 if (isset($limit)) {
231 $this->limit = $limit;
232 }
233
234 $this->structure = [];
235 $this->addresses = [];
236 $this->error = null;
237 $this->index = null;
238
239 // Unfold any long lines in $this->address.
240 $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
241 $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
242
243 $tmp_address = false;
244 while ($tmp_address = $this->_splitAddresses($this->address)) {
245 $this->address = $tmp_address;
246 }
247
248 if ($tmp_address === false || isset($this->error)) {
249 // mjansen patch 14 Ap 2016 start
250 throw new ilMailException($this->error);
251 // mjansen patch 14 Ap 2016 end
252 }
253
254 // Validate each address individually. If we encounter an invalid
255 // address, stop iterating and return an error immediately.
256 foreach ($this->addresses as $address) {
257 $valid = $this->_validateAddress($address);
258
259 if ($valid === false || isset($this->error)) {
260 // mjansen patch 14 Ap 2016 start
261 throw new ilMailException($this->error);
262 // mjansen patch 14 Ap 2016 end
263 }
264
265 if (!$this->nestGroups) {
266 $this->structure = array_merge($this->structure, $valid);
267 } else {
268 $this->structure[] = $valid;
269 }
270 }
271
272 return $this->structure;
273 }
_splitAddresses(string $address)
Splits an address into separate addresses.
Definition: RFC822.php:281
_validateAddress(array $address)
Function to begin checking the address.
Definition: RFC822.php:486
Class ilMailException.
$valid

References $address, $default_domain, $limit, and $validate.

◆ validateMailbox()

Mail_RFC822::validateMailbox ( string &  $mailbox)

Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; name and route-addr.

Parameters
string&$mailboxThe string to check.
Returns
bool Success or failure.

Definition at line 663 of file RFC822.php.

663 : bool
664 {
665 // A couple of defaults.
666 $phrase = '';
667 $comment = '';
668 $comments = [];
669
670 // Catch any RFC822 comments and store them separately.
671 $_mailbox = $mailbox;
672 while (trim($_mailbox) !== '') {
673 $parts = explode('(', $_mailbox);
674 $before_comment = $this->_splitCheck($parts, '(');
675 if ($before_comment !== $_mailbox) {
676 // First char should be a (.
677 $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
678 $parts = explode(')', $comment);
679 $comment = $this->_splitCheck($parts, ')');
681
682 // +2 is for the brackets
683 $_mailbox = substr($_mailbox, strpos($_mailbox, '(' . $comment) + strlen($comment) + 2);
684 } else {
685 break;
686 }
687 }
688
689 foreach ($comments as $comment) {
690 $mailbox = str_replace("($comment)", '', $mailbox);
691 }
692
693 $mailbox = trim($mailbox);
694
695 // Check for name + route-addr
696 if (substr($mailbox, -1) === '>' && $mailbox[0] !== '<') {
697 $parts = explode('<', $mailbox);
698 $name = $this->_splitCheck($parts, '<');
699
700 $phrase = trim($name);
701 $route_addr = trim(substr($mailbox, strlen($name . '<'), -1));
702
703 if ($this->_validatePhrase($phrase) === false ||
704 ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
705 return false;
706 }
707
708 // Only got addr-spec
709 } else {
710 // First snip angle brackets if present.
711 if ($mailbox[0] === '<' && substr($mailbox, -1) === '>') {
712 $addr_spec = substr($mailbox, 1, -1);
713 } else {
714 $addr_spec = $mailbox;
715 }
716
717 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
718 return false;
719 }
720 }
721
722 // Construct the object that will be returned.
723 $mbox = new stdClass();
724
725 // Add the phrase (even if empty) and comments
726 $mbox->personal = $phrase;
727 $mbox->comment = $comments ?? [];
728
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'];
734 }
735 } else {
736 $mbox->mailbox = $addr_spec['local_part'];
737 $mbox->host = $addr_spec['domain'];
738 }
739
740 $mailbox = $mbox;
741 return true;
742 }
$comment
Definition: buildRTE.php:72
_validateRouteAddr(string $route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">".
Definition: RFC822.php:754
if($format !==null) $name
Definition: metadata.php:247
$comments

References $comment, $comments, $name, and $parts.

Field Documentation

◆ $address

string Mail_RFC822::$address = ''

The address being parsed by the RFC822 object.

Definition at line 97 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

◆ $addresses

array Mail_RFC822::$addresses = []

The array of raw addresses built up as we parse.

Definition at line 121 of file RFC822.php.

◆ $default_domain

string Mail_RFC822::$default_domain = 'localhost'

The default domain to use for unqualified addresses.

Definition at line 103 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

◆ $error

string Mail_RFC822::$error = null

The current error message, if any.

Definition at line 133 of file RFC822.php.

◆ $index

int Mail_RFC822::$index = null

An internal counter/pointer.

Definition at line 139 of file RFC822.php.

◆ $limit

int Mail_RFC822::$limit = null

A limit after which processing stops.

Definition at line 159 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

◆ $mailRFC822

bool Mail_RFC822::$mailRFC822 = true

A variable so that we can tell whether or not we're inside a Mail_RFC822 object.

Definition at line 153 of file RFC822.php.

◆ $nestGroups

bool Mail_RFC822::$nestGroups = true

Should we return a nested array showing groups, or flatten everything?

Definition at line 109 of file RFC822.php.

◆ $num_groups

int Mail_RFC822::$num_groups = 0

The number of groups that have been found in the address list.

@access public

Definition at line 146 of file RFC822.php.

◆ $structure

array Mail_RFC822::$structure = []

The final array of parsed address information that we build up.

Definition at line 127 of file RFC822.php.

◆ $validate

bool Mail_RFC822::$validate = true

Whether or not to validate atoms for non-ascii characters.

Definition at line 115 of file RFC822.php.

Referenced by __construct(), and parseAddressList().


The documentation for this class was generated from the following file: