ILIAS  release_4-4 Revision
Mail_RFC822 Class Reference
+ Collaboration diagram for Mail_RFC822:

Public Member Functions

 Mail_RFC822 ($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.

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

References _hasUnclosedBracketsSub().

Referenced by _splitCheck().

401  {
402  $num_angle_start = substr_count($string, $chars[0]);
403  $num_angle_end = substr_count($string, $chars[1]);
404 
405  $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
406  $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
407 
408  if ($num_angle_start < $num_angle_end) {
409  $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
410  return false;
411  } else {
412  return ($num_angle_start > $num_angle_end);
413  }
414  }
_hasUnclosedBracketsSub($string, &$num, $char)
Sub function that is used only by hasUnclosedBrackets().
Definition: RFC822.php:425
+ 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 425 of file RFC822.php.

References _hasUnclosedQuotes().

Referenced by _hasUnclosedBrackets().

426  {
427  $parts = explode($char, $string);
428  for ($i = 0; $i < count($parts); $i++){
429  if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
430  $num--;
431  if (isset($parts[$i + 1]))
432  $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
433  }
434 
435  return $num;
436  }
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:363
+ 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 363 of file RFC822.php.

Referenced by _hasUnclosedBracketsSub(), and _splitCheck().

364  {
365  $string = trim($string);
366  $iMax = strlen($string);
367  $in_quote = false;
368  $i = $slashes = 0;
369 
370  for (; $i < $iMax; ++$i) {
371  switch ($string[$i]) {
372  case '\\':
373  ++$slashes;
374  break;
375 
376  case '"':
377  if ($slashes % 2 == 0) {
378  $in_quote = !$in_quote;
379  }
380  // Fall through to default action below.
381 
382  default:
383  $slashes = 0;
384  break;
385  }
386  }
387 
388  return $in_quote;
389  }
+ 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 305 of file RFC822.php.

References _splitCheck().

Referenced by _splitAddresses().

306  {
307  // First comma not in quotes, angles or escaped:
308  $parts = explode(',', $address);
309  $string = $this->_splitCheck($parts, ',');
310 
311  // Now we have the first address, we can reliably check for a
312  // group by searching for a colon that's not escaped or in
313  // quotes or angle brackets.
314  if (count($parts = explode(':', $string)) > 1) {
315  $string2 = $this->_splitCheck($parts, ':');
316  return ($string2 !== $string);
317  } else {
318  return false;
319  }
320  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
$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 229 of file RFC822.php.

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

Referenced by parseAddressList().

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

References _hasUnclosedBrackets(), and _hasUnclosedQuotes().

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

331  {
332  $string = $parts[0];
333 
334  for ($i = 0; $i < count($parts); $i++) {
335  if ($this->_hasUnclosedQuotes($string)
336  || $this->_hasUnclosedBrackets($string, '<>')
337  || $this->_hasUnclosedBrackets($string, '[]')
338  || $this->_hasUnclosedBrackets($string, '()')
339  || substr($string, -1) == '\\') {
340  if (isset($parts[$i + 1])) {
341  $string = $string . $char . $parts[$i + 1];
342  } else {
343  $this->error = 'Invalid address spec. Unclosed bracket or quotes';
344  return false;
345  }
346  } else {
347  $this->index = $i;
348  break;
349  }
350  }
351 
352  return $string;
353  }
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:400
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:363
+ 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 445 of file RFC822.php.

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

Referenced by parseAddressList().

446  {
447  $is_group = false;
448  $addresses = array();
449 
450  if ($address['group']) {
451  $is_group = true;
452 
453  // Get the group part of the name
454  $parts = explode(':', $address['address']);
455  $groupname = $this->_splitCheck($parts, ':');
456  $structure = array();
457 
458  // And validate the group part of the name.
459  if (!$this->_validatePhrase($groupname)){
460  $this->error = 'Group name did not validate.';
461  return false;
462  } else {
463  // Don't include groups if we are not nesting
464  // them. This avoids returning invalid addresses.
465  if ($this->nestGroups) {
466  $structure = new stdClass;
467  $structure->groupname = $groupname;
468  }
469  }
470 
471  $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
472  }
473 
474  // If a group then split on comma and put into an array.
475  // Otherwise, Just put the whole address in an array.
476  if ($is_group) {
477  while (strlen($address['address']) > 0) {
478  $parts = explode(',', $address['address']);
479  $addresses[] = $this->_splitCheck($parts, ',');
480  $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
481  }
482  } else {
483  $addresses[] = $address['address'];
484  }
485 
486  // Check that $addresses is set, if address like this:
487  // Groupname:;
488  // Then errors were appearing.
489  if (!count($addresses)){
490  $this->error = 'Empty group.';
491  return false;
492  }
493 
494  // Trim the whitespace from all of the address strings.
495  array_map('trim', $addresses);
496 
497  // Validate each mailbox.
498  // Format could be one of: name <geezer@domain.com>
499  // geezer@domain.com
500  // geezer
501  // ... or any other format valid by RFC 822.
502  for ($i = 0; $i < count($addresses); $i++) {
503  if (!$this->validateMailbox($addresses[$i])) {
504  if (empty($this->error)) {
505  $this->error = 'Validation failed for: ' . $addresses[$i];
506  }
507  return false;
508  }
509  }
510 
511  // Nested format
512  if ($this->nestGroups) {
513  if ($is_group) {
514  $structure->addresses = $addresses;
515  } else {
516  $structure = $addresses[0];
517  }
518 
519  // Flat format
520  } else {
521  if ($is_group) {
522  $structure = array_merge($structure, $addresses);
523  } else {
525  }
526  }
527 
528  return $structure;
529  }
$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:330
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:538
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:630
$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 854 of file RFC822.php.

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

Referenced by _validateRouteAddr(), and validateMailbox().

855  {
856  $addr_spec = trim($addr_spec);
857 
858  // Split on @ sign if there is one.
859  if (strpos($addr_spec, '@') !== false) {
860  $parts = explode('@', $addr_spec);
861  $local_part = $this->_splitCheck($parts, '@');
862  $domain = substr($addr_spec, strlen($local_part . '@'));
863 
864  // No @ sign so assume the default domain.
865  } else {
866  $local_part = $addr_spec;
867  $domain = $this->default_domain;
868  }
869 
870  if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
871  if (($domain = $this->_validateDomain($domain)) === false) return false;
872 
873  // Got here so return successful.
874  return array('local_part' => $local_part, 'domain' => $domain);
875  }
$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:792
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:885
+ 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 579 of file RFC822.php.

Referenced by _validatePhrase(), and _validateSubdomain().

580  {
581  if (!$this->validate) {
582  // Validation has been turned off; assume the atom is okay.
583  return true;
584  }
585 
586  // Check for any char from ASCII 0 - ASCII 127
587  if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) {
588  return false;
589  }
590 
591  // Check for specials:
592  if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
593  return false;
594  }
595 
596  // Check for control characters (ASCII 0-31):
597  if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
598  return false;
599  }
600 
601  return true;
602  }
+ 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 840 of file RFC822.php.

Referenced by _validateSubdomain().

841  {
842  return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
843  }
+ 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 792 of file RFC822.php.

References _splitCheck(), and _validateSubdomain().

Referenced by _validateAddrSpec(), and _validateRoute().

793  {
794  // Note the different use of $subdomains and $sub_domains
795  $subdomains = explode('.', $domain);
796 
797  while (count($subdomains) > 0) {
798  $sub_domains[] = $this->_splitCheck($subdomains, '.');
799  for ($i = 0; $i < $this->index + 1; $i++)
800  array_shift($subdomains);
801  }
802 
803  foreach ($sub_domains as $sub_domain) {
804  if (!$this->_validateSubdomain(trim($sub_domain)))
805  return false;
806  }
807 
808  // Managed to get here, so return input.
809  return $domain;
810  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
_validateSubdomain($subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
Definition: RFC822.php:820
+ 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 885 of file RFC822.php.

References _splitCheck(), and _validatePhrase().

Referenced by _validateAddrSpec().

886  {
887  $parts = explode('.', $local_part);
888  $words = array();
889 
890  // Split the local_part into words.
891  while (count($parts) > 0){
892  $words[] = $this->_splitCheck($parts, '.');
893  for ($i = 0; $i < $this->index + 1; $i++) {
894  array_shift($parts);
895  }
896  }
897 
898  // Validate each word.
899  foreach ($words as $word) {
900  // If this word contains an unquoted space, it is invalid. (6.2.4)
901  if (strpos($word, ' ') && $word[0] !== '"')
902  {
903  return false;
904  }
905 
906  if ($this->_validatePhrase(trim($word)) === false) return false;
907  }
908 
909  // Managed to get here, so return the input.
910  return $local_part;
911  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:538
+ 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 538 of file RFC822.php.

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

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

539  {
540  // Splits on one or more Tab or space.
541  $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
542 
543  $phrase_parts = array();
544  while (count($parts) > 0){
545  $phrase_parts[] = $this->_splitCheck($parts, ' ');
546  for ($i = 0; $i < $this->index + 1; $i++)
547  array_shift($parts);
548  }
549 
550  foreach ($phrase_parts as $part) {
551  // If quoted string:
552  if (substr($part, 0, 1) == '"') {
553  if (!$this->_validateQuotedString($part)) {
554  return false;
555  }
556  continue;
557  }
558 
559  // Otherwise it's an atom:
560  if (!$this->_validateAtom($part)) return false;
561  }
562 
563  return true;
564  }
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> ...
Definition: RFC822.php:612
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:579
+ 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 612 of file RFC822.php.

Referenced by _validatePhrase().

613  {
614  // Leading and trailing "
615  $qstring = substr($qstring, 1, -1);
616 
617  // Perform check, removing quoted characters first.
618  return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
619  }
+ 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 769 of file RFC822.php.

References _validateDomain().

Referenced by _validateRouteAddr().

770  {
771  // Split on comma.
772  $domains = explode(',', trim($route));
773 
774  foreach ($domains as $domain) {
775  $domain = str_replace('@', '', trim($domain));
776  if (!$this->_validateDomain($domain)) return false;
777  }
778 
779  return $route;
780  }
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
Definition: RFC822.php:792
+ 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 719 of file RFC822.php.

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

Referenced by validateMailbox().

720  {
721  // Check for colon.
722  if (strpos($route_addr, ':') !== false) {
723  $parts = explode(':', $route_addr);
724  $route = $this->_splitCheck($parts, ':');
725  } else {
726  $route = $route_addr;
727  }
728 
729  // If $route is same as $route_addr then the colon was in
730  // quotes or brackets or, of course, non existent.
731  if ($route === $route_addr){
732  unset($route);
733  $addr_spec = $route_addr;
734  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
735  return false;
736  }
737  } else {
738  // Validate route part.
739  if (($route = $this->_validateRoute($route)) === false) {
740  return false;
741  }
742 
743  $addr_spec = substr($route_addr, strlen($route . ':'));
744 
745  // Validate addr-spec part.
746  if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
747  return false;
748  }
749  }
750 
751  if (isset($route)) {
752  $return['adl'] = $route;
753  } else {
754  $return['adl'] = '';
755  }
756 
757  $return = array_merge($return, $addr_spec);
758  return $return;
759  }
_validateRoute($route)
Function to validate a route, which is: route = 1#("@" domain) ":".
Definition: RFC822.php:769
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:854
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
+ 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 820 of file RFC822.php.

References _validateAtom(), and _validateDliteral().

Referenced by _validateDomain().

821  {
822  if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
823  if (!$this->_validateDliteral($arr[1])) return false;
824  } else {
825  if (!$this->_validateAtom($subdomain)) return false;
826  }
827 
828  // Got here, so return successful.
829  return true;
830  }
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"...
Definition: RFC822.php:840
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>
Definition: RFC822.php:579
+ 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 923 of file RFC822.php.

References $data.

924  {
925  return count(preg_split('/(?<!\\\\),/', $data));
926  }
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data

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

References $data.

942  {
943  $regex = $strict ? '/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' : '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
944  if (preg_match($regex, trim($data), $matches)) {
945  return array($matches[1], $matches[2]);
946  } else {
947  return false;
948  }
949  }
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data

◆ Mail_RFC822()

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

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

Referenced by parseAddressList().

153  {
154  if (isset($address)) $this->address = $address;
155  if (isset($default_domain)) $this->default_domain = $default_domain;
156  if (isset($nest_groups)) $this->nestGroups = $nest_groups;
157  if (isset($validate)) $this->validate = $validate;
158  if (isset($limit)) $this->limit = $limit;
159  }
$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
+ Here is the caller graph for this function:

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

References $address, $default_domain, $limit, $structure, $valid, $validate, _splitAddresses(), _validateAddress(), Mail_RFC822(), and PEAR\raiseError().

Referenced by Mail\parseRecipients().

174  {
175  if (!isset($this) || !isset($this->mailRFC822)) {
176  $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
177  return $obj->parseAddressList();
178  }
179 
180  if (isset($address)) $this->address = $address;
181  if (isset($default_domain)) $this->default_domain = $default_domain;
182  if (isset($nest_groups)) $this->nestGroups = $nest_groups;
183  if (isset($validate)) $this->validate = $validate;
184  if (isset($limit)) $this->limit = $limit;
185 
186  $this->structure = array();
187  $this->addresses = array();
188  $this->error = null;
189  $this->index = null;
190 
191  // Unfold any long lines in $this->address.
192  $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
193  $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
194 
195  while ($this->address = $this->_splitAddresses($this->address));
196 
197  if ($this->address === false || isset($this->error)) {
198  require_once 'PEAR.php';
199  return PEAR::raiseError($this->error);
200  }
201 
202  // Validate each address individually. If we encounter an invalid
203  // address, stop iterating and return an error immediately.
204  foreach ($this->addresses as $address) {
205  $valid = $this->_validateAddress($address);
206 
207  if ($valid === false || isset($this->error)) {
208  require_once 'PEAR.php';
209  return PEAR::raiseError($this->error);
210  }
211 
212  if (!$this->nestGroups) {
213  $this->structure = array_merge($this->structure, $valid);
214  } else {
215  $this->structure[] = $valid;
216  }
217  }
218 
219  return $this->structure;
220  }
$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
_validateAddress($address)
Function to begin checking the address.
Definition: RFC822.php:445
$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:229
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
Mail_RFC822($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Sets up the object.
Definition: RFC822.php:152
+ Here is the call graph for this function:
+ Here is the caller 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 630 of file RFC822.php.

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

Referenced by _validateAddress().

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


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