ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 74 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 154 of file RFC822.php.

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

160  {
161  if (isset($address)) {
162  $this->address = $address;
163  }
164  if (isset($default_domain)) {
165  $this->default_domain = $default_domain;
166  }
167  if (isset($nest_groups)) {
168  $this->nestGroups = $nest_groups;
169  }
170  if (isset($validate)) {
171  $this->validate = $validate;
172  }
173  if (isset($limit)) {
174  $this->limit = $limit;
175  }
176  }
string $default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:86
bool $validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:98
int $limit
A limit after which processing stops.
Definition: RFC822.php:142
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:80

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 422 of file RFC822.php.

References _hasUnclosedBracketsSub().

Referenced by _splitCheck().

422  : bool
423  {
424  $num_angle_start = substr_count($string, $chars[0]);
425  $num_angle_end = substr_count($string, $chars[1]);
426 
427  $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
428  $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
429 
430  if ($num_angle_start < $num_angle_end) {
431  $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
432  return false;
433  }
434 
435  return ($num_angle_start > $num_angle_end);
436  }
_hasUnclosedBracketsSub(string $string, int &$num, string $char)
Sub function that is used only by hasUnclosedBrackets().
Definition: RFC822.php:446
+ Here is the call graph for this function:
+ Here is the caller 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 446 of file RFC822.php.

References $parts, and _hasUnclosedQuotes().

Referenced by _hasUnclosedBrackets().

446  : int
447  {
448  $parts = explode($char, $string);
449  for ($i = 0, $iMax = count($parts); $i < $iMax; $i++) {
450  if (substr($parts[$i], -1) === '\\' || $this->_hasUnclosedQuotes($parts[$i])) {
451  $num--;
452  }
453  if (isset($parts[$i + 1])) {
454  $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
455  }
456  }
457 
458  return $num;
459  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_hasUnclosedQuotes(string $string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:385
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 385 of file RFC822.php.

Referenced by _hasUnclosedBracketsSub(), and _splitCheck().

385  : bool
386  {
387  $string = trim($string);
388  $iMax = strlen($string);
389  $in_quote = false;
390  $i = $slashes = 0;
391 
392  for (; $i < $iMax; ++$i) {
393  switch ($string[$i]) {
394  case '\\':
395  ++$slashes;
396  break;
397 
398  case '"':
399  if ($slashes % 2 === 0) {
400  $in_quote = !$in_quote;
401  }
402  // Fall through to default action below.
403 
404  // no break
405  default:
406  $slashes = 0;
407  break;
408  }
409  }
410 
411  return $in_quote;
412  }
+ Here is the caller graph for this function:

◆ _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 330 of file RFC822.php.

References $parts, and _splitCheck().

Referenced by _splitAddresses().

330  : bool
331  {
332  // First comma not in quotes, angles or escaped:
333  $parts = explode(',', $address);
334  $string = $this->_splitCheck($parts, ',');
335 
336  // Now we have the first address, we can reliably check for a
337  // group by searching for a colon that's not escaped or in
338  // quotes or angle brackets.
339  if (count($parts = explode(':', $string)) > 1) {
340  $string2 = $this->_splitCheck($parts, ':');
341  return ($string2 !== $string);
342  }
343 
344  return false;
345  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:80
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 263 of file RFC822.php.

References $address, $parts, _isGroup(), and _splitCheck().

Referenced by parseAddressList().

264  {
265  if (!empty($this->limit) && count($this->addresses) === $this->limit) {
266  return false;
267  }
268 
269  if (!isset($this->error) && $this->_isGroup($address)) {
270  $split_char = ';';
271  $is_group = true;
272  } elseif (!isset($this->error)) {
273  $split_char = ',';
274  $is_group = false;
275  } elseif (isset($this->error)) {
276  return false;
277  }
278 
279  // Split the string based on the above ten or so lines.
280  $parts = explode($split_char, $address);
281  $string = $this->_splitCheck($parts, $split_char);
282 
283  // If a group...
284  if ($is_group) {
285  // If $string does not contain a colon outside of
286  // brackets/quotes etc then something's fubar.
287 
288  // First check there's a colon at all:
289  if (strpos($string, ':') === false) {
290  $this->error = 'Invalid address: ' . $string;
291  return false;
292  }
293 
294  // Now check it's outside of brackets/quotes:
295  if (!$this->_splitCheck(explode(':', $string), ':')) {
296  return false;
297  }
298 
299  // We must have a group at this point, so increase the counter:
300  $this->num_groups++;
301  }
302 
303  // $string now contains the first full address/group.
304  // Add to the addresses array.
305  $this->addresses[] = [
306  'address' => trim($string),
307  'group' => $is_group,
308  ];
309 
310  // Remove the now stored address from the initial line, the +1
311  // is to account for the explode character.
312  $address = trim((string) substr($address, strlen($string) + 1));
313 
314  // If the next char is a comma and this was a group, then
315  // there are more addresses, otherwise, if there are any more
316  // chars, then there is another address.
317  if ($is_group && $address !== '' && str_starts_with($address, ',')) {
318  $address = trim(substr($address, 1));
319  }
320 
321  return $address;
322  }
_isGroup(string $address)
Checks for a group at the start of the string.
Definition: RFC822.php:330
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:80
+ Here is the call graph for this function:
+ Here is the caller 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 354 of file RFC822.php.

References _hasUnclosedBrackets(), and _hasUnclosedQuotes().

Referenced by _isGroup(), _splitAddresses(), _validateAddress(), _validateAddrSpec(), _validateDomain(), _validateLocalPart(), _validatePhrase(), _validateRouteAddr(), and validateMailbox().

354  : string
355  {
356  $string = $parts[0];
357 
358  for ($i = 0, $iMax = count($parts); $i < $iMax; $i++) {
359  if ($this->_hasUnclosedQuotes($string)
360  || $this->_hasUnclosedBrackets($string, '<>')
361  || $this->_hasUnclosedBrackets($string, '[]')
362  || $this->_hasUnclosedBrackets($string, '()')
363  || substr($string, -1) === '\\') {
364  if (isset($parts[$i + 1])) {
365  $string .= $char . $parts[$i + 1];
366  } else {
367  $this->error = 'Invalid address spec. Unclosed bracket or quotes';
368  return '';
369  }
370  } else {
371  $this->index = $i;
372  break;
373  }
374  }
375 
376  return $string;
377  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_hasUnclosedBrackets(string $string, string $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:422
_hasUnclosedQuotes(string $string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:385
+ Here is the call graph for this function:
+ Here is the caller 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 467 of file RFC822.php.

References $addresses, $parts, $structure, _splitCheck(), _validatePhrase(), and validateMailbox().

Referenced by parseAddressList().

468  {
469  $is_group = false;
470  $addresses = [];
471 
472  if ($address['group']) {
473  $is_group = true;
474 
475  // Get the group part of the name
476  $parts = explode(':', $address['address']);
477  $groupname = $this->_splitCheck($parts, ':');
478  $structure = [];
479 
480  // And validate the group part of the name.
481  if (!$this->_validatePhrase($groupname)) {
482  $this->error = 'Group name did not validate.';
483  return false;
484  }
485 
486  if ($this->nestGroups) {
487  // Don't include groups if we are not nesting
488  // them. This avoids returning invalid addresses.
489  $structure = new stdClass();
490  $structure->groupname = $groupname;
491  }
492 
493  $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
494  }
495 
496  // If a group then split on comma and put into an array.
497  // Otherwise, Just put the whole address in an array.
498  if ($is_group) {
499  while (strlen($address['address']) > 0) {
500  $parts = explode(',', $address['address']);
501  $addresses[] = $this->_splitCheck($parts, ',');
502  $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
503  }
504  } else {
505  $addresses[] = $address['address'];
506  }
507 
508  // Trim the whitespace from all of the address strings.
509  array_map('trim', $addresses);
510 
511  // Validate each mailbox.
512  // Format could be one of: name <geezer@domain.com>
513  // geezer@domain.com
514  // geezer
515  // ... or any other format valid by RFC 822.
516  for ($i = 0, $iMax = count($addresses); $i < $iMax; $i++) {
517  if (!$this->validateMailbox($addresses[$i])) {
518  if (empty($this->error)) {
519  $this->error = 'Validation failed for: ' . $addresses[$i];
520  }
521  return false;
522  }
523  }
524 
525  // Nested format
526  if ($this->nestGroups) {
527  if ($is_group) {
528  $structure->addresses = $addresses;
529  } else {
530  $structure = $addresses[0];
531  }
532 
533  // Flat format
534  } elseif ($is_group) {
535  $structure = array_merge($structure, $addresses);
536  } else {
538  }
539 
540  return $structure;
541  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
array $structure
The final array of parsed address information that we build up.
Definition: RFC822.php:110
validateMailbox(string &$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:644
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:80
array $addresses
The array of raw addresses built up as we parse.
Definition: RFC822.php:104
_validatePhrase(string $phrase)
Function to validate a phrase.
Definition: RFC822.php:549
+ Here is the call graph for this function:
+ Here is the caller 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 873 of file RFC822.php.

References $default_domain, $parts, $validate, _splitCheck(), _validateDomain(), and _validateLocalPart().

Referenced by _validateRouteAddr(), and validateMailbox().

874  {
875  $addr_spec = trim($addr_spec);
876 
877  // mjansen patch 16 Sep 2016 start
878  $validateState = $this->validate;
879  // mjansen patch 16 Sep 2016 end
880  // Split on @ sign if there is one.
881  if (strpos($addr_spec, '@') !== false) {
882  $parts = explode('@', $addr_spec);
883  $local_part = $this->_splitCheck($parts, '@');
884  $domain = substr($addr_spec, strlen($local_part . '@'));
885  // mjansen patch 16 Sep 2016 start
886  if (substr_count($addr_spec, '@') !== 1 && $local_part === '') {
887  $this->validate = false;
888  $local_part = $addr_spec;
889  $domain = $this->default_domain;
890  }
891  // mjansen patch 16 Sep 2016 end
892  // No @ sign so assume the default domain.
893  } else {
894  $local_part = $addr_spec;
895  $domain = $this->default_domain;
896  }
897 
898  if (($local_part = $this->_validateLocalPart($local_part)) === false) {
899  return false;
900  }
901  // mjansen patch 16 Sep 2016 start
902  if ($validateState !== $this->validate) {
903  $this->validate = $validateState;
904  }
905  // mjansen patch 16 Sep 2016 end
906  if (($domain = $this->_validateDomain($domain)) === false) {
907  return false;
908  }
909 
910  // Got here so return successful.
911  return ['local_part' => $local_part, 'domain' => $domain];
912  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_validateLocalPart(string $local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:922
string $default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:86
_validateDomain(string $domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
Definition: RFC822.php:808
bool $validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:98
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 592 of file RFC822.php.

Referenced by _validatePhrase(), and _validateSubdomain().

592  : bool
593  {
594  if (!$this->validate) {
595  // Validation has been turned off; assume the atom is okay.
596  return true;
597  }
598 
599  // Check for any char from ASCII 0 - ASCII 127
600  // mjansen patch 16 Sep 2015 start
601  // Check for specials:
602  if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
603  return false;
604  }
605 
606  // Check for control characters (ASCII 0-31):
607  if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
608  return false;
609  }
610  #16291
611  #17618
612  if (!(bool) preg_match('//u', $atom)) {
613  return false;
614  }
615  // mjansen patch 16 Sep 2015 end
616 
617  return true;
618  }
+ Here is the caller graph for this function:

◆ _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 859 of file RFC822.php.

Referenced by _validateSubdomain().

859  : bool
860  {
861  return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) &&
862  ((!isset($matches[1])) || $matches[1] != '\\');
863  }
+ Here is the caller graph for this function:

◆ _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 808 of file RFC822.php.

References _splitCheck(), and _validateSubdomain().

Referenced by _validateAddrSpec(), and _validateRoute().

809  {
810  // Note the different use of $subdomains and $sub_domains
811  $subdomains = explode('.', $domain);
812 
813  $sub_domains = [];
814  while (count($subdomains) > 0) {
815  $sub_domains[] = $this->_splitCheck($subdomains, '.');
816  for ($i = 0; $i < $this->index + 1; $i++) {
817  array_shift($subdomains);
818  }
819  }
820 
821  foreach ($sub_domains as $sub_domain) {
822  if (!$this->_validateSubdomain(trim($sub_domain))) {
823  return false;
824  }
825  }
826 
827  // Managed to get here, so return input.
828  return $domain;
829  }
_validateSubdomain(string $subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
Definition: RFC822.php:838
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateLocalPart()

Mail_RFC822::_validateLocalPart ( string  $local_part)
protected

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

private

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

Definition at line 922 of file RFC822.php.

References $parts, _splitCheck(), and _validatePhrase().

Referenced by _validateAddrSpec().

923  {
924  $parts = explode('.', $local_part);
925  $words = [];
926 
927  // Split the local_part into words.
928  while (count($parts) > 0) {
929  $words[] = $this->_splitCheck($parts, '.');
930  for ($i = 0; $i < $this->index + 1; $i++) {
931  array_shift($parts);
932  }
933  }
934 
935  // Validate each word.
936  foreach ($words as $word) {
937  // iszmais patch 19 May 2020 start
938  // word cannot be empty (#17317)
939  //if ($word === '') {
940  // return false;
941  //}
942  // iszmais patch 19 May 2020 end
943  // If this word contains an unquoted space, it is invalid. (6.2.4)
944  if (strpos($word, ' ') && $word[0] !== '"') {
945  // mjansen patch 24 Feb 2016 start
946  // Mantis issue #18018
947  // # http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
948  //return false;
949  // mjansen patch 24 Feb 2016 end
950  }
951 
952  if ($this->_validatePhrase(trim($word)) === false) {
953  return false;
954  }
955  }
956 
957  // Managed to get here, so return the input.
958  return $local_part;
959  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
_validatePhrase(string $phrase)
Function to validate a phrase.
Definition: RFC822.php:549
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 549 of file RFC822.php.

References $parts, _splitCheck(), _validateAtom(), and _validateQuotedString().

Referenced by _validateAddress(), _validateLocalPart(), and validateMailbox().

549  : bool
550  {
551  // Splits on one or more Tab or space.
552  $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
553 
554  $phrase_parts = [];
555  while (count($parts) > 0) {
556  $phrase_parts[] = $this->_splitCheck($parts, ' ');
557  for ($i = 0; $i < $this->index + 1; $i++) {
558  array_shift($parts);
559  }
560  }
561 
562  foreach ($phrase_parts as $part) {
563  // If quoted string:
564  if (strpos($part, '"') === 0) {
565  if (!$this->_validateQuotedString($part)) {
566  return false;
567  }
568  continue;
569  }
570 
571  // Otherwise it's an atom:
572  if (!$this->_validateAtom($part)) {
573  return false;
574  }
575  }
576 
577  return true;
578  }
_validateQuotedString(string $qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> ...
Definition: RFC822.php:627
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_validateAtom(string $atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:592
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 627 of file RFC822.php.

Referenced by _validatePhrase().

627  : bool
628  {
629  // Leading and trailing "
630  $qstring = substr($qstring, 1, -1);
631 
632  // Perform check, removing quoted characters first.
633  return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
634  }
+ Here is the caller graph for this function:

◆ _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 784 of file RFC822.php.

References _validateDomain().

Referenced by _validateRouteAddr().

785  {
786  // Split on comma.
787  $domains = explode(',', trim($route));
788 
789  foreach ($domains as $domain) {
790  $domain = str_replace('@', '', trim($domain));
791  if (!$this->_validateDomain($domain)) {
792  return false;
793  }
794  }
795 
796  return $route;
797  }
_validateDomain(string $domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
Definition: RFC822.php:808
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 735 of file RFC822.php.

References $parts, _splitCheck(), _validateAddrSpec(), and _validateRoute().

Referenced by validateMailbox().

736  {
737  // Check for colon.
738  if (strpos($route_addr, ':') !== false) {
739  $parts = explode(':', $route_addr);
740  $route = $this->_splitCheck($parts, ':');
741  } else {
742  $route = $route_addr;
743  }
744 
745  // If $route is same as $route_addr then the colon was in
746  // quotes or brackets or, of course, non existent.
747  if ($route === $route_addr) {
748  unset($route);
749  $addr_spec = $route_addr;
750  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
751  return false;
752  }
753  } else {
754  // Validate route part.
755  if (($route = $this->_validateRoute($route)) === false) {
756  return false;
757  }
758 
759  $addr_spec = substr($route_addr, strlen($route . ':'));
760 
761  // Validate addr-spec part.
762  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
763  return false;
764  }
765  }
766 
767  if (isset($route)) {
768  $return['adl'] = $route;
769  } else {
770  $return['adl'] = '';
771  }
772 
773  $return = array_merge($return, $addr_spec);
774  return $return;
775  }
_validateAddrSpec(string $addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:873
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_validateRoute(string $route)
Function to validate a route, which is: route = 1#("@" domain) ":".
Definition: RFC822.php:784
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _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 838 of file RFC822.php.

References _validateAtom(), and _validateDliteral().

Referenced by _validateDomain().

838  : bool
839  {
840  if (preg_match('|^\[(.*)]$|', $subdomain, $arr)) {
841  if (!$this->_validateDliteral($arr[1])) {
842  return false;
843  }
844  } elseif (!$this->_validateAtom($subdomain)) {
845  return false;
846  }
847 
848  // Got here, so return successful.
849  return true;
850  }
_validateAtom(string $atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:592
_validateDliteral(string $dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"...
Definition: RFC822.php:859
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 971 of file RFC822.php.

971  : int
972  {
973  return count(preg_split('/(?<!\\\\),/', $data));
974  }

◆ 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 989 of file RFC822.php.

990  {
991  $regex =
992  $strict ?
993  '/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' :
994  '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
995  if (preg_match($regex, trim($data), $matches)) {
996  return [$matches[1], $matches[2]];
997  }
998 
999  return false;
1000  }

◆ 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 189 of file RFC822.php.

References $address, $default_domain, $limit, $structure, $valid, $validate, _splitAddresses(), _validateAddress(), and null.

195  : array {
196  if (!isset($this, $this->mailRFC822)) {
197  $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
198  return $obj->parseAddressList();
199  }
200 
201  if (isset($address)) {
202  $this->address = $address;
203  }
204  if (isset($default_domain)) {
205  $this->default_domain = $default_domain;
206  }
207  if (isset($nest_groups)) {
208  $this->nestGroups = $nest_groups;
209  }
210  if (isset($validate)) {
211  $this->validate = $validate;
212  }
213  if (isset($limit)) {
214  $this->limit = $limit;
215  }
216 
217  $this->structure = [];
218  $this->addresses = [];
219  $this->error = null;
220  $this->index = null;
221 
222  // Unfold any long lines in $this->address.
223  $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
224  $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
225 
226  while ($tmp_address = $this->_splitAddresses($this->address)) {
227  $this->address = $tmp_address;
228  }
229 
230  if ($tmp_address === false || isset($this->error)) {
231  // mjansen patch 14 Ap 2016 start
232  throw new ilMailException($this->error);
233  // mjansen patch 14 Ap 2016 end
234  }
235 
236  // Validate each address individually. If we encounter an invalid
237  // address, stop iterating and return an error immediately.
238  foreach ($this->addresses as $address) {
239  $valid = $this->_validateAddress($address);
240 
241  if ($valid === false || isset($this->error)) {
242  // mjansen patch 14 Ap 2016 start
243  throw new ilMailException($this->error);
244  // mjansen patch 14 Ap 2016 end
245  }
246 
247  if (!$this->nestGroups) {
248  $this->structure = array_merge($this->structure, $valid);
249  } else {
250  $this->structure[] = $valid;
251  }
252  }
253 
254  return $this->structure;
255  }
_validateAddress(array $address)
Function to begin checking the address.
Definition: RFC822.php:467
$valid
string $default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:86
bool $validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:98
array $structure
The final array of parsed address information that we build up.
Definition: RFC822.php:110
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
int $limit
A limit after which processing stops.
Definition: RFC822.php:142
string $address
The address being parsed by the RFC822 object.
Definition: RFC822.php:80
_splitAddresses(string $address)
Splits an address into separate addresses.
Definition: RFC822.php:263
+ Here is the call graph for this function:

◆ 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 644 of file RFC822.php.

References $comment, $comments, $parts, _splitCheck(), _validateAddrSpec(), _validatePhrase(), and _validateRouteAddr().

Referenced by _validateAddress().

644  : bool
645  {
646  // A couple of defaults.
647  $phrase = '';
648  $comment = '';
649  $comments = [];
650 
651  // Catch any RFC822 comments and store them separately.
652  $_mailbox = $mailbox;
653  while (trim($_mailbox) !== '') {
654  $parts = explode('(', $_mailbox);
655  $before_comment = $this->_splitCheck($parts, '(');
656  if ($before_comment !== $_mailbox) {
657  // First char should be a (.
658  $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
659  $parts = explode(')', $comment);
660  $comment = $this->_splitCheck($parts, ')');
661  $comments[] = $comment;
662 
663  // +2 is for the brackets
664  $_mailbox = substr($_mailbox, strpos($_mailbox, '(' . $comment) + strlen($comment) + 2);
665  } else {
666  break;
667  }
668  }
669 
670  foreach ($comments as $comment) {
671  $mailbox = str_replace("($comment)", '', $mailbox);
672  }
673 
674  $mailbox = trim($mailbox);
675 
676  // Check for name + route-addr
677  if (substr($mailbox, -1) === '>' && $mailbox[0] !== '<') {
678  $parts = explode('<', $mailbox);
679  $name = $this->_splitCheck($parts, '<');
680 
681  $phrase = trim($name);
682  $route_addr = trim(substr($mailbox, strlen($name . '<'), -1));
683 
684  if ($this->_validatePhrase($phrase) === false ||
685  ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
686  return false;
687  }
688 
689  // Only got addr-spec
690  } else {
691  // First snip angle brackets if present.
692  if ($mailbox[0] === '<' && substr($mailbox, -1) === '>') {
693  $addr_spec = substr($mailbox, 1, -1);
694  } else {
695  $addr_spec = $mailbox;
696  }
697 
698  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
699  return false;
700  }
701  }
702 
703  // Construct the object that will be returned.
704  $mbox = new stdClass();
705 
706  // Add the phrase (even if empty) and comments
707  $mbox->personal = $phrase;
708  $mbox->comment = $comments ?? [];
709 
710  if (isset($route_addr)) {
711  $mbox->mailbox = $route_addr['local_part'];
712  $mbox->host = $route_addr['domain'];
713  if ($route_addr['adl'] !== '') {
714  $mbox->adl = $route_addr['adl'];
715  }
716  } else {
717  $mbox->mailbox = $addr_spec['local_part'];
718  $mbox->host = $addr_spec['domain'];
719  }
720 
721  $mailbox = $mbox;
722  return true;
723  }
_validateAddrSpec(string $addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:873
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
_validateRouteAddr(string $route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"...
Definition: RFC822.php:735
$comments
$comment
Definition: buildRTE.php:72
_splitCheck(array $parts, string $char)
A common function that will check an exploded string.
Definition: RFC822.php:354
_validatePhrase(string $phrase)
Function to validate a phrase.
Definition: RFC822.php:549
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $address

string Mail_RFC822::$address = ''

The address being parsed by the RFC822 object.

Definition at line 80 of file RFC822.php.

Referenced by __construct(), _splitAddresses(), and parseAddressList().

◆ $addresses

array Mail_RFC822::$addresses = []

The array of raw addresses built up as we parse.

Definition at line 104 of file RFC822.php.

Referenced by _validateAddress().

◆ $default_domain

string Mail_RFC822::$default_domain = 'localhost'

The default domain to use for unqualified addresses.

Definition at line 86 of file RFC822.php.

Referenced by __construct(), _validateAddrSpec(), and parseAddressList().

◆ $error

string Mail_RFC822::$error = null

The current error message, if any.

Definition at line 116 of file RFC822.php.

◆ $index

int Mail_RFC822::$index = null

An internal counter/pointer.

Definition at line 122 of file RFC822.php.

◆ $limit

int Mail_RFC822::$limit = null

A limit after which processing stops.

Definition at line 142 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 136 of file RFC822.php.

◆ $nestGroups

bool Mail_RFC822::$nestGroups = true

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

Definition at line 92 of file RFC822.php.

◆ $num_groups

int Mail_RFC822::$num_groups = 0

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

public

Definition at line 129 of file RFC822.php.

◆ $structure

array Mail_RFC822::$structure = []

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

Definition at line 110 of file RFC822.php.

Referenced by _validateAddress(), and parseAddressList().

◆ $validate

bool Mail_RFC822::$validate = true

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

Definition at line 98 of file RFC822.php.

Referenced by __construct(), _validateAddrSpec(), and parseAddressList().


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