ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Mail_RFC822 Class Reference
+ Collaboration diagram for Mail_RFC822:

Public Member Functions

 __construct ($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
 Sets up the object. More...
 
 parseAddressList ($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
 Starts the whole process. More...
 
 _splitAddresses ($address)
 Splits an address into separate addresses. More...
 
 _isGroup ($address)
 Checks for a group at the start of the string. More...
 
 _splitCheck ($parts, $char)
 A common function that will check an exploded string. More...
 
 _hasUnclosedQuotes ($string)
 Checks if a string has unclosed quotes or not. More...
 
 _hasUnclosedBrackets ($string, $chars)
 Checks if a string has an unclosed brackets or not. More...
 
 _hasUnclosedBracketsSub ($string, &$num, $char)
 Sub function that is used only by hasUnclosedBrackets(). More...
 
 _validateAddress ($address)
 Function to begin checking the address. More...
 
 _validatePhrase ($phrase)
 Function to validate a phrase. More...
 
 _validateAtom ($atom)
 Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs> More...
 
 _validateQuotedString ($qstring)
 Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> More...
 
 validateMailbox (&$mailbox)
 Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; name and route-addr. More...
 
 _validateRouteAddr ($route_addr)
 This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">". More...
 
 _validateRoute ($route)
 Function to validate a route, which is: route = 1#("@" domain) ":". More...
 
 _validateDomain ($domain)
 Function to validate a domain, though this is not quite what you expect of a strict internet domain. More...
 
 _validateSubdomain ($subdomain)
 Function to validate a subdomain: subdomain = domain-ref / domain-literal. More...
 
 _validateDliteral ($dliteral)
 Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]". More...
 
 _validateAddrSpec ($addr_spec)
 Function to validate an addr-spec. More...
 
 _validateLocalPart ($local_part)
 Function to validate the local part of an address: local-part = word *("." word) More...
 
 approximateCount ($data)
 Returns an approximate count of how many addresses are in the given string. More...
 
 isValidInetAddress ($data, $strict=false)
 This is a email validating function separate to the rest of the class. More...
 

Data Fields

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

Detailed Description

Definition at line 70 of file RFC822.php.

Constructor & Destructor Documentation

◆ __construct()

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

Sets up the object.

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

public

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

Definition at line 153 of file RFC822.php.

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

154  {
155  if (isset($address)) $this->address = $address;
156  if (isset($default_domain)) $this->default_domain = $default_domain;
157  if (isset($nest_groups)) $this->nestGroups = $nest_groups;
158  if (isset($validate)) $this->validate = $validate;
159  if (isset($limit)) $this->limit = $limit;
160  }
$validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:94
$default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:82
$limit
A limit after which processing stops.
Definition: RFC822.php:138
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76

Member Function Documentation

◆ _hasUnclosedBrackets()

Mail_RFC822::_hasUnclosedBrackets (   $string,
  $chars 
)

Checks if a string has an unclosed brackets or not.

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

private

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

Definition at line 405 of file RFC822.php.

References _hasUnclosedBracketsSub().

Referenced by _splitCheck().

406  {
407  $num_angle_start = substr_count($string, $chars[0]);
408  $num_angle_end = substr_count($string, $chars[1]);
409 
410  $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
411  $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
412 
413  if ($num_angle_start < $num_angle_end) {
414  $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
415  return false;
416  } else {
417  return ($num_angle_start > $num_angle_end);
418  }
419  }
_hasUnclosedBracketsSub($string, &$num, $char)
Sub function that is used only by hasUnclosedBrackets().
Definition: RFC822.php:430
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _hasUnclosedBracketsSub()

Mail_RFC822::_hasUnclosedBracketsSub (   $string,
$num,
  $char 
)

Sub function that is used only by hasUnclosedBrackets().

private

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

Definition at line 430 of file RFC822.php.

References _hasUnclosedQuotes().

Referenced by _hasUnclosedBrackets().

431  {
432  $parts = explode($char, $string);
433  for ($i = 0; $i < count($parts); $i++){
434  if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
435  $num--;
436  if (isset($parts[$i + 1]))
437  $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
438  }
439 
440  return $num;
441  }
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:368
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _hasUnclosedQuotes()

Mail_RFC822::_hasUnclosedQuotes (   $string)

Checks if a string has unclosed quotes or not.

private

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

Definition at line 368 of file RFC822.php.

Referenced by _hasUnclosedBracketsSub(), and _splitCheck().

369  {
370  $string = trim($string);
371  $iMax = strlen($string);
372  $in_quote = false;
373  $i = $slashes = 0;
374 
375  for (; $i < $iMax; ++$i) {
376  switch ($string[$i]) {
377  case '\\':
378  ++$slashes;
379  break;
380 
381  case '"':
382  if ($slashes % 2 == 0) {
383  $in_quote = !$in_quote;
384  }
385  // Fall through to default action below.
386 
387  default:
388  $slashes = 0;
389  break;
390  }
391  }
392 
393  return $in_quote;
394  }
+ Here is the caller graph for this function:

◆ _isGroup()

Mail_RFC822::_isGroup (   $address)

Checks for a group at the start of the string.

private

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

Definition at line 310 of file RFC822.php.

References _splitCheck().

Referenced by _splitAddresses().

311  {
312  // First comma not in quotes, angles or escaped:
313  $parts = explode(',', $address);
314  $string = $this->_splitCheck($parts, ',');
315 
316  // Now we have the first address, we can reliably check for a
317  // group by searching for a colon that's not escaped or in
318  // quotes or angle brackets.
319  if (count($parts = explode(':', $string)) > 1) {
320  $string2 = $this->_splitCheck($parts, ':');
321  return ($string2 !== $string);
322  } else {
323  return false;
324  }
325  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _splitAddresses()

Mail_RFC822::_splitAddresses (   $address)

Splits an address into separate addresses.

private

Parameters
string$addressThe addresses to split.
Returns
boolean Success or failure.

Definition at line 234 of file RFC822.php.

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

Referenced by parseAddressList().

235  {
236  if (!empty($this->limit) && count($this->addresses) == $this->limit) {
237  return '';
238  }
239 
240  if ($this->_isGroup($address) && !isset($this->error)) {
241  $split_char = ';';
242  $is_group = true;
243  } elseif (!isset($this->error)) {
244  $split_char = ',';
245  $is_group = false;
246  } elseif (isset($this->error)) {
247  return false;
248  }
249 
250  // Split the string based on the above ten or so lines.
251  $parts = explode($split_char, $address);
252  $string = $this->_splitCheck($parts, $split_char);
253 
254  // If a group...
255  if ($is_group) {
256  // If $string does not contain a colon outside of
257  // brackets/quotes etc then something's fubar.
258 
259  // First check there's a colon at all:
260  if (strpos($string, ':') === false) {
261  $this->error = 'Invalid address: ' . $string;
262  return false;
263  }
264 
265  // Now check it's outside of brackets/quotes:
266  if (!$this->_splitCheck(explode(':', $string), ':')) {
267  return false;
268  }
269 
270  // We must have a group at this point, so increase the counter:
271  $this->num_groups++;
272  }
273 
274  // $string now contains the first full address/group.
275  // Add to the addresses array.
276  $this->addresses[] = array(
277  'address' => trim($string),
278  'group' => $is_group
279  );
280 
281  // Remove the now stored address from the initial line, the +1
282  // is to account for the explode character.
283  $address = trim(substr($address, strlen($string) + 1));
284 
285  // If the next char is a comma and this was a group, then
286  // there are more addresses, otherwise, if there are any more
287  // chars, then there is another address.
288  if ($is_group && substr($address, 0, 1) == ','){
289  $address = trim(substr($address, 1));
290  return $address;
291 
292  } elseif (strlen($address) > 0) {
293  return $address;
294 
295  } else {
296  return '';
297  }
298 
299  // If you got here then something's off
300  return false;
301  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
Create styles array
The data for the language used.
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
_isGroup($address)
Checks for a group at the start of the string.
Definition: RFC822.php:310
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _splitCheck()

Mail_RFC822::_splitCheck (   $parts,
  $char 
)

A common function that will check an exploded string.

private

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

Definition at line 335 of file RFC822.php.

References _hasUnclosedBrackets(), and _hasUnclosedQuotes().

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

336  {
337  $string = $parts[0];
338 
339  for ($i = 0; $i < count($parts); $i++) {
340  if ($this->_hasUnclosedQuotes($string)
341  || $this->_hasUnclosedBrackets($string, '<>')
342  || $this->_hasUnclosedBrackets($string, '[]')
343  || $this->_hasUnclosedBrackets($string, '()')
344  || substr($string, -1) == '\\') {
345  if (isset($parts[$i + 1])) {
346  $string = $string . $char . $parts[$i + 1];
347  } else {
348  $this->error = 'Invalid address spec. Unclosed bracket or quotes';
349  return false;
350  }
351  } else {
352  $this->index = $i;
353  break;
354  }
355  }
356 
357  return $string;
358  }
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:405
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:368
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateAddress()

Mail_RFC822::_validateAddress (   $address)

Function to begin checking the address.

private

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

Definition at line 450 of file RFC822.php.

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

Referenced by parseAddressList().

451  {
452  $is_group = false;
453  $addresses = array();
454 
455  if ($address['group']) {
456  $is_group = true;
457 
458  // Get the group part of the name
459  $parts = explode(':', $address['address']);
460  $groupname = $this->_splitCheck($parts, ':');
461  $structure = array();
462 
463  // And validate the group part of the name.
464  if (!$this->_validatePhrase($groupname)){
465  $this->error = 'Group name did not validate.';
466  return false;
467  } else {
468  // Don't include groups if we are not nesting
469  // them. This avoids returning invalid addresses.
470  if ($this->nestGroups) {
471  $structure = new stdClass;
472  $structure->groupname = $groupname;
473  }
474  }
475 
476  $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
477  }
478 
479  // If a group then split on comma and put into an array.
480  // Otherwise, Just put the whole address in an array.
481  if ($is_group) {
482  while (strlen($address['address']) > 0) {
483  $parts = explode(',', $address['address']);
484  $addresses[] = $this->_splitCheck($parts, ',');
485  $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
486  }
487  } else {
488  $addresses[] = $address['address'];
489  }
490 
491  // Check that $addresses is set, if address like this:
492  // Groupname:;
493  // Then errors were appearing.
494  if (!count($addresses)){
495  $this->error = 'Empty group.';
496  return false;
497  }
498 
499  // Trim the whitespace from all of the address strings.
500  array_map('trim', $addresses);
501 
502  // Validate each mailbox.
503  // Format could be one of: name <geezer@domain.com>
504  // geezer@domain.com
505  // geezer
506  // ... or any other format valid by RFC 822.
507  for ($i = 0; $i < count($addresses); $i++) {
508  if (!$this->validateMailbox($addresses[$i])) {
509  if (empty($this->error)) {
510  $this->error = 'Validation failed for: ' . $addresses[$i];
511  }
512  return false;
513  }
514  }
515 
516  // Nested format
517  if ($this->nestGroups) {
518  if ($is_group) {
519  $structure->addresses = $addresses;
520  } else {
521  $structure = $addresses[0];
522  }
523 
524  // Flat format
525  } else {
526  if ($is_group) {
527  $structure = array_merge($structure, $addresses);
528  } else {
530  }
531  }
532 
533  return $structure;
534  }
$structure
The final array of parsed address information that we build up.
Definition: RFC822.php:106
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:543
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:638
Create styles array
The data for the language used.
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
$addresses
The array of raw addresses built up as we parse.
Definition: RFC822.php:100
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateAddrSpec()

Mail_RFC822::_validateAddrSpec (   $addr_spec)

Function to validate an addr-spec.

addr-spec = local-part "@" domain

private

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

Definition at line 862 of file RFC822.php.

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

Referenced by _validateRouteAddr(), and validateMailbox().

863  {
864  $addr_spec = trim($addr_spec);
865 
866  // mjansen patch 16 Sep 2016 start
867  $validateState = $this->validate;
868  // mjansen patch 16 Sep 2016 end
869  // Split on @ sign if there is one.
870  if (strpos($addr_spec, '@') !== false) {
871  $parts = explode('@', $addr_spec);
872  $local_part = $this->_splitCheck($parts, '@');
873  $domain = substr($addr_spec, strlen($local_part . '@'));
874  // mjansen patch 16 Sep 2016 start
875  if (substr_count($addr_spec, '@') != 1 && $local_part == '') {
876  $this->validate = false;
877  $local_part = $addr_spec;
878  $domain = $this->default_domain;
879  }
880  // mjansen patch 16 Sep 2016 end
881  // No @ sign so assume the default domain.
882  } else {
883  $local_part = $addr_spec;
884  $domain = $this->default_domain;
885  }
886 
887  if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
888  // mjansen patch 16 Sep 2016 start
889  if ($validateState != $this->validate) {
890  $this->validate = $validateState;
891  }
892  // mjansen patch 16 Sep 2016 end
893  if (($domain = $this->_validateDomain($domain)) === false) return false;
894 
895  // Got here so return successful.
896  return array('local_part' => $local_part, 'domain' => $domain);
897  }
$validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:94
$default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:82
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
Definition: RFC822.php:800
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
Create styles array
The data for the language used.
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:907
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateAtom()

Mail_RFC822::_validateAtom (   $atom)

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.

private

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

Definition at line 584 of file RFC822.php.

Referenced by _validatePhrase(), and _validateSubdomain().

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

◆ _validateDliteral()

Mail_RFC822::_validateDliteral (   $dliteral)

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

private

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

Definition at line 848 of file RFC822.php.

Referenced by _validateSubdomain().

849  {
850  return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
851  }
+ Here is the caller graph for this function:

◆ _validateDomain()

Mail_RFC822::_validateDomain (   $domain)

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

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

private

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

Definition at line 800 of file RFC822.php.

References _splitCheck(), and _validateSubdomain().

Referenced by _validateAddrSpec(), and _validateRoute().

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

◆ _validateLocalPart()

Mail_RFC822::_validateLocalPart (   $local_part)

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

References _splitCheck(), _validatePhrase(), and array.

Referenced by _validateAddrSpec().

908  {
909  $parts = explode('.', $local_part);
910  $words = array();
911 
912  // Split the local_part into words.
913  while (count($parts) > 0){
914  $words[] = $this->_splitCheck($parts, '.');
915  for ($i = 0; $i < $this->index + 1; $i++) {
916  array_shift($parts);
917  }
918  }
919 
920  // Validate each word.
921  foreach ($words as $word) {
922  // If this word contains an unquoted space, it is invalid. (6.2.4)
923  if (strpos($word, ' ') && $word[0] !== '"')
924  {
925  // mjansen patch 24 Feb 2016 start
926  // Mantis issue #18018
927  // # http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
928  //return false;
929  // mjansen patch 24 Feb 2016 end
930  }
931 
932  if ($this->_validatePhrase(trim($word)) === false) return false;
933  }
934 
935  // Managed to get here, so return the input.
936  return $local_part;
937  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:543
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validatePhrase()

Mail_RFC822::_validatePhrase (   $phrase)

Function to validate a phrase.

private

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

Definition at line 543 of file RFC822.php.

References _splitCheck(), _validateAtom(), _validateQuotedString(), and array.

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

544  {
545  // Splits on one or more Tab or space.
546  $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
547 
548  $phrase_parts = array();
549  while (count($parts) > 0){
550  $phrase_parts[] = $this->_splitCheck($parts, ' ');
551  for ($i = 0; $i < $this->index + 1; $i++)
552  array_shift($parts);
553  }
554 
555  foreach ($phrase_parts as $part) {
556  // If quoted string:
557  if (substr($part, 0, 1) == '"') {
558  if (!$this->_validateQuotedString($part)) {
559  return false;
560  }
561  continue;
562  }
563 
564  // Otherwise it's an atom:
565  if (!$this->_validateAtom($part)) return false;
566  }
567 
568  return true;
569  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> ...
Definition: RFC822.php:620
Create styles array
The data for the language used.
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:584
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateQuotedString()

Mail_RFC822::_validateQuotedString (   $qstring)

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

private

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

Definition at line 620 of file RFC822.php.

Referenced by _validatePhrase().

621  {
622  // Leading and trailing "
623  $qstring = substr($qstring, 1, -1);
624 
625  // Perform check, removing quoted characters first.
626  return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
627  }
+ Here is the caller graph for this function:

◆ _validateRoute()

Mail_RFC822::_validateRoute (   $route)

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

private

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

Definition at line 777 of file RFC822.php.

References _validateDomain().

Referenced by _validateRouteAddr().

778  {
779  // Split on comma.
780  $domains = explode(',', trim($route));
781 
782  foreach ($domains as $domain) {
783  $domain = str_replace('@', '', trim($domain));
784  if (!$this->_validateDomain($domain)) return false;
785  }
786 
787  return $route;
788  }
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
Definition: RFC822.php:800
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateRouteAddr()

Mail_RFC822::_validateRouteAddr (   $route_addr)

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.

private

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

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

Referenced by validateMailbox().

728  {
729  // Check for colon.
730  if (strpos($route_addr, ':') !== false) {
731  $parts = explode(':', $route_addr);
732  $route = $this->_splitCheck($parts, ':');
733  } else {
734  $route = $route_addr;
735  }
736 
737  // If $route is same as $route_addr then the colon was in
738  // quotes or brackets or, of course, non existent.
739  if ($route === $route_addr){
740  unset($route);
741  $addr_spec = $route_addr;
742  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
743  return false;
744  }
745  } else {
746  // Validate route part.
747  if (($route = $this->_validateRoute($route)) === false) {
748  return false;
749  }
750 
751  $addr_spec = substr($route_addr, strlen($route . ':'));
752 
753  // Validate addr-spec part.
754  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
755  return false;
756  }
757  }
758 
759  if (isset($route)) {
760  $return['adl'] = $route;
761  } else {
762  $return['adl'] = '';
763  }
764 
765  $return = array_merge($return, $addr_spec);
766  return $return;
767  }
_validateRoute($route)
Function to validate a route, which is: route = 1#("@" domain) ":".
Definition: RFC822.php:777
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:862
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _validateSubdomain()

Mail_RFC822::_validateSubdomain (   $subdomain)

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

private

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

Definition at line 828 of file RFC822.php.

References _validateAtom(), and _validateDliteral().

Referenced by _validateDomain().

829  {
830  if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
831  if (!$this->_validateDliteral($arr[1])) return false;
832  } else {
833  if (!$this->_validateAtom($subdomain)) return false;
834  }
835 
836  // Got here, so return successful.
837  return true;
838  }
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"...
Definition: RFC822.php:848
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:584
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ approximateCount()

Mail_RFC822::approximateCount (   $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 949 of file RFC822.php.

References $data.

950  {
951  return count(preg_split('/(?<!\\\\),/', $data));
952  }

◆ isValidInetAddress()

Mail_RFC822::isValidInetAddress (   $data,
  $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
boolean$strictOptional stricter mode
Returns
mixed False if it fails, an indexed array username/domain if it matches

Definition at line 967 of file RFC822.php.

References $data, and array.

968  {
969  $regex = $strict ? '/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' : '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
970  if (preg_match($regex, trim($data), $matches)) {
971  return array($matches[1], $matches[2]);
972  } else {
973  return false;
974  }
975  }
Create styles array
The data for the language used.

◆ parseAddressList()

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

Starts the whole process.

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

public

Parameters
string$addressThe address(es) to validate.
string$default_domainDefault domain/host etc.
boolean$nest_groupsWhether to return the structure with groups nested for easier viewing.
boolean$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 174 of file RFC822.php.

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

175  {
176  if (!isset($this) || !isset($this->mailRFC822)) {
177  $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
178  return $obj->parseAddressList();
179  }
180 
181  if (isset($address)) $this->address = $address;
182  if (isset($default_domain)) $this->default_domain = $default_domain;
183  if (isset($nest_groups)) $this->nestGroups = $nest_groups;
184  if (isset($validate)) $this->validate = $validate;
185  if (isset($limit)) $this->limit = $limit;
186 
187  $this->structure = array();
188  $this->addresses = array();
189  $this->error = null;
190  $this->index = null;
191 
192  // Unfold any long lines in $this->address.
193  $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
194  $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
195 
196  while ($this->address = $this->_splitAddresses($this->address));
197 
198  if ($this->address === false || isset($this->error)) {
199  // mjansen patch 14 Ap 2016 start
200  require_once 'Services/Mail/exceptions/class.ilMailException.php';
201  throw new ilMailException($this->error);
202  // mjansen patch 14 Ap 2016 end
203  }
204 
205  // Validate each address individually. If we encounter an invalid
206  // address, stop iterating and return an error immediately.
207  foreach ($this->addresses as $address) {
208  $valid = $this->_validateAddress($address);
209 
210  if ($valid === false || isset($this->error)) {
211  // mjansen patch 14 Ap 2016 start
212  require_once 'Services/Mail/exceptions/class.ilMailException.php';
213  throw new ilMailException($this->error);
214  // mjansen patch 14 Ap 2016 end
215  }
216 
217  if (!$this->nestGroups) {
218  $this->structure = array_merge($this->structure, $valid);
219  } else {
220  $this->structure[] = $valid;
221  }
222  }
223 
224  return $this->structure;
225  }
$validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:94
$valid
$default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:82
Class ilMailException.
_validateAddress($address)
Function to begin checking the address.
Definition: RFC822.php:450
$structure
The final array of parsed address information that we build up.
Definition: RFC822.php:106
$limit
A limit after which processing stops.
Definition: RFC822.php:138
_splitAddresses($address)
Splits an address into separate addresses.
Definition: RFC822.php:234
Create styles array
The data for the language used.
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
+ Here is the call graph for this function:

◆ validateMailbox()

Mail_RFC822::validateMailbox ( $mailbox)

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

public

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

Definition at line 638 of file RFC822.php.

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

Referenced by _validateAddress().

639  {
640  // A couple of defaults.
641  $phrase = '';
642  $comment = '';
643  $comments = array();
644 
645  // Catch any RFC822 comments and store them separately.
646  $_mailbox = $mailbox;
647  while (strlen(trim($_mailbox)) > 0) {
648  $parts = explode('(', $_mailbox);
649  $before_comment = $this->_splitCheck($parts, '(');
650  if ($before_comment != $_mailbox) {
651  // First char should be a (.
652  $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
653  $parts = explode(')', $comment);
654  $comment = $this->_splitCheck($parts, ')');
655  $comments[] = $comment;
656 
657  // +2 is for the brackets
658  $_mailbox = substr($_mailbox, strpos($_mailbox, '('.$comment)+strlen($comment)+2);
659  } else {
660  break;
661  }
662  }
663 
664  foreach ($comments as $comment) {
665  $mailbox = str_replace("($comment)", '', $mailbox);
666  }
667 
668  $mailbox = trim($mailbox);
669 
670  // Check for name + route-addr
671  if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
672  $parts = explode('<', $mailbox);
673  $name = $this->_splitCheck($parts, '<');
674 
675  $phrase = trim($name);
676  $route_addr = trim(substr($mailbox, strlen($name.'<'), -1));
677 
678  if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
679  return false;
680  }
681 
682  // Only got addr-spec
683  } else {
684  // First snip angle brackets if present.
685  if (substr($mailbox, 0, 1) == '<' && substr($mailbox, -1) == '>') {
686  $addr_spec = substr($mailbox, 1, -1);
687  } else {
688  $addr_spec = $mailbox;
689  }
690 
691  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
692  return false;
693  }
694  }
695 
696  // Construct the object that will be returned.
697  $mbox = new stdClass();
698 
699  // Add the phrase (even if empty) and comments
700  $mbox->personal = $phrase;
701  $mbox->comment = isset($comments) ? $comments : array();
702 
703  if (isset($route_addr)) {
704  $mbox->mailbox = $route_addr['local_part'];
705  $mbox->host = $route_addr['domain'];
706  $route_addr['adl'] !== '' ? $mbox->adl = $route_addr['adl'] : '';
707  } else {
708  $mbox->mailbox = $addr_spec['local_part'];
709  $mbox->host = $addr_spec['domain'];
710  }
711 
712  $mailbox = $mbox;
713  return true;
714  }
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:862
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:543
_validateRouteAddr($route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"...
Definition: RFC822.php:727
$comment
Definition: buildRTE.php:83
Create styles array
The data for the language used.
+ 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 76 of file RFC822.php.

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

◆ $addresses

array Mail_RFC822::$addresses = array()

The array of raw addresses built up as we parse.

Definition at line 100 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 82 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 112 of file RFC822.php.

◆ $index

integer Mail_RFC822::$index = null

An internal counter/pointer.

Definition at line 118 of file RFC822.php.

◆ $limit

int Mail_RFC822::$limit = null

A limit after which processing stops.

Definition at line 138 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

◆ $mailRFC822

boolean Mail_RFC822::$mailRFC822 = true

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

Definition at line 132 of file RFC822.php.

◆ $nestGroups

boolean Mail_RFC822::$nestGroups = true

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

Definition at line 88 of file RFC822.php.

◆ $num_groups

integer Mail_RFC822::$num_groups = 0

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

public

Definition at line 125 of file RFC822.php.

◆ $structure

array Mail_RFC822::$structure = array()

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

Definition at line 106 of file RFC822.php.

Referenced by _validateAddress(), and parseAddressList().

◆ $validate

boolean Mail_RFC822::$validate = true

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

Definition at line 94 of file RFC822.php.

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


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