Mail_RFC822 Class Reference

o------------------------------------------------------------------------------o | This package is licensed under the Phpguru license. More...

List of all members.

Public Member Functions

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

Private Attributes

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


Detailed Description

o------------------------------------------------------------------------------o | This package is licensed under the Phpguru license.

A quick summary is | | that for commercial use, there is a small one-time licensing fee to pay. For | | registered charities and educational institutes there is a reduced license | | fee available. You can read more at: | | | | http://www.phpguru.org/static/license.html | o------------------------------------------------------------------------------o

© Copyright 2008,2009 Richard Heyes RFC 822 Email address list validation Utility What is it?

This class will take an address string, and parse it into it's consituent parts, be that either addresses, groups, or combinations. Nested groups are not supported. The structure it returns is pretty straight forward, and is similar to that provided by the imap_rfc822_parse_adrlist(). Use print_r() to view the structure.

How do I use it?

$address_string = 'My Group: "Richard Heyes" <richard> (A comment), ted@example.com (Ted Bloggs), Barney;'; $structure = Mail_RFC822::parseAddressList($address_string, 'example.com', TRUE) print_r($structure);

Definition at line 33 of file RFC822.php.


Constructor & Destructor Documentation

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 $address The address(es) to validate.
string $default_domain Default domain/host etc. If not supplied, will be set to localhost.
boolean $nest_groups Whether to return the structure with groups nested for easier viewing.
boolean $validate Whether 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 116 of file RFC822.php.

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

00117     {
00118         if (isset($address))        $this->address        = $address;
00119         if (isset($default_domain)) $this->default_domain = $default_domain;
00120         if (isset($nest_groups))    $this->nestGroups     = $nest_groups;
00121         if (isset($validate))       $this->validate       = $validate;
00122         if (isset($limit))          $this->limit          = $limit;
00123     }


Member Function Documentation

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 $string The string to check.
string $chars The characters to check for.
Returns:
boolean True if there are unclosed brackets inside the string, false otherwise.

Definition at line 348 of file RFC822.php.

References _hasUnclosedBracketsSub().

Referenced by _splitCheck().

00349     {
00350         $num_angle_start = substr_count($string, $chars[0]);
00351         $num_angle_end   = substr_count($string, $chars[1]);
00352 
00353         $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
00354         $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
00355 
00356         if ($num_angle_start < $num_angle_end) {
00357             $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
00358             return false;
00359         } else {
00360             return ($num_angle_start > $num_angle_end);
00361         }
00362     }

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

Sub function that is used only by hasUnclosedBrackets().

private

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

Definition at line 373 of file RFC822.php.

References _hasUnclosedQuotes().

Referenced by _hasUnclosedBrackets().

00374     {
00375         $parts = explode($char, $string);
00376         for ($i = 0; $i < count($parts); $i++){
00377             if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
00378                 $num--;
00379             if (isset($parts[$i + 1]))
00380                 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
00381         }
00382         
00383         return $num;
00384     }

Mail_RFC822::_hasUnclosedQuotes ( string  ) 

Checks if a string has an unclosed quotes or not.

private

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

Definition at line 327 of file RFC822.php.

Referenced by _hasUnclosedBracketsSub(), and _splitCheck().

00328     {
00329         $string     = explode('"', $string);
00330         $string_cnt = count($string);
00331 
00332         for ($i = 0; $i < (count($string) - 1); $i++)
00333             if (substr($string[$i], -1) == '\\')
00334                 $string_cnt--;
00335 
00336         return ($string_cnt % 2 === 0);
00337     }

Mail_RFC822::_isGroup ( address  ) 

Checks for a group at the start of the string.

private

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

Definition at line 270 of file RFC822.php.

References $address, and _splitCheck().

Referenced by _splitAddresses().

00271     {
00272         // First comma not in quotes, angles or escaped:
00273         $parts  = explode(',', $address);
00274         $string = $this->_splitCheck($parts, ',');
00275 
00276         // Now we have the first address, we can reliably check for a
00277         // group by searching for a colon that's not escaped or in
00278         // quotes or angle brackets.
00279         if (count($parts = explode(':', $string)) > 1) {
00280             $string2 = $this->_splitCheck($parts, ':');
00281             return ($string2 !== $string);
00282         } else {
00283             return false;
00284         }
00285     }

Mail_RFC822::_splitAddresses ( address  ) 

Splits an address into seperate addresses.

private

Parameters:
string $address The addresses to split.
Returns:
boolean Success or failure.

Definition at line 194 of file RFC822.php.

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

Referenced by parseAddressList().

00195     {
00196 
00197         if (!empty($this->limit) AND count($this->addresses) == $this->limit) {
00198             return '';
00199         }
00200 
00201         if ($this->_isGroup($address) && !isset($this->error)) {
00202             $split_char = ';';
00203             $is_group   = true;
00204         } elseif (!isset($this->error)) {
00205             $split_char = ',';
00206             $is_group   = false;
00207         } elseif (isset($this->error)) {
00208             return false;
00209         }
00210 
00211         // Split the string based on the above ten or so lines.
00212         $parts  = explode($split_char, $address);
00213         $string = $this->_splitCheck($parts, $split_char);
00214 
00215         // If a group...
00216         if ($is_group) {
00217             // If $string does not contain a colon outside of
00218             // brackets/quotes etc then something's fubar.
00219 
00220             // First check there's a colon at all:
00221             if (strpos($string, ':') === false) {
00222                 $this->error = 'Invalid address: ' . $string;
00223                 return false;
00224             }
00225 
00226             // Now check it's outside of brackets/quotes:
00227             if (!$this->_splitCheck(explode(':', $string), ':'))
00228                 return false;
00229 
00230             // We must have a group at this point, so increase the counter:
00231             $this->num_groups++;
00232         }
00233 
00234         // $string now contains the first full address/group.
00235         // Add to the addresses array.
00236         $this->addresses[] = array(
00237                                    'address' => trim($string),
00238                                    'group'   => $is_group
00239                                    );
00240 
00241         // Remove the now stored address from the initial line, the +1
00242         // is to account for the explode character.
00243         $address = trim(substr($address, strlen($string) + 1));
00244 
00245         // If the next char is a comma and this was a group, then
00246         // there are more addresses, otherwise, if there are any more
00247         // chars, then there is another address.
00248         if ($is_group && substr($address, 0, 1) == ','){
00249             $address = trim(substr($address, 1));
00250             return $address;
00251 
00252         } elseif (strlen($address) > 0) {
00253             return $address;
00254 
00255         } else {
00256             return '';
00257         }
00258 
00259         // If you got here then something's off
00260         return false;
00261     }

Mail_RFC822::_splitCheck ( parts,
char 
)

A common function that will check an exploded string.

private

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

Definition at line 295 of file RFC822.php.

References _hasUnclosedBrackets(), and _hasUnclosedQuotes().

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

00296     {
00297         $string = $parts[0];
00298 
00299         for ($i = 0; $i < count($parts); $i++) {
00300             if ($this->_hasUnclosedQuotes($string)
00301                 || $this->_hasUnclosedBrackets($string, '<>')
00302                 || $this->_hasUnclosedBrackets($string, '[]')
00303                 || $this->_hasUnclosedBrackets($string, '()')
00304                 || substr($string, -1) == '\\') {
00305                 if (isset($parts[$i + 1])) {
00306                     $string = $string . $char . $parts[$i + 1];
00307                 } else {
00308                     $this->error = 'Invalid address spec. Unclosed bracket or quotes';
00309                     return false;
00310                 }
00311             } else {
00312                 $this->index = $i;
00313                 break;
00314             }
00315         }
00316 
00317         return $string;
00318     }

Mail_RFC822::_validateAddress ( address  ) 

Function to begin checking the address.

private

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

Definition at line 393 of file RFC822.php.

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

Referenced by parseAddressList().

00394     {
00395         $is_group = false;
00396 
00397         if ($address['group']) {
00398             $is_group = true;
00399 
00400             // Get the group part of the name
00401             $parts     = explode(':', $address['address']);
00402             $groupname = $this->_splitCheck($parts, ':');
00403             $structure = array();
00404 
00405             // And validate the group part of the name.
00406             if (!$this->_validatePhrase($groupname)){
00407                 $this->error = 'Group name did not validate.';
00408                 return false;
00409             } else {
00410                 // Don't include groups if we are not nesting
00411                 // them. This avoids returning invalid addresses.
00412                 if ($this->nestGroups) {
00413                     $structure = new stdClass;
00414                     $structure->groupname = $groupname;
00415                 }
00416             }
00417 
00418             $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
00419         }
00420 
00421         // If a group then split on comma and put into an array.
00422         // Otherwise, Just put the whole address in an array.
00423         if ($is_group) {
00424             while (strlen($address['address']) > 0) {
00425                 $parts       = explode(',', $address['address']);
00426                 $addresses[] = $this->_splitCheck($parts, ',');
00427                 $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
00428             }
00429         } else {
00430             $addresses[] = $address['address'];
00431         }
00432 
00433         // Check that $addresses is set, if address like this:
00434         // Groupname:;
00435         // Then errors were appearing.
00436         if (!isset($addresses)){
00437             $this->error = 'Empty group.';
00438             return false;
00439         }
00440 
00441         for ($i = 0; $i < count($addresses); $i++) {
00442             $addresses[$i] = trim($addresses[$i]);
00443         }
00444 
00445         // Validate each mailbox.
00446         // Format could be one of: name <geezer@domain.com>
00447         //                         geezer@domain.com
00448         //                         geezer
00449         // ... or any other format valid by RFC 822.
00450         array_walk($addresses, array($this, 'validateMailbox'));
00451 
00452         // Nested format
00453         if ($this->nestGroups) {
00454             if ($is_group) {
00455                 $structure->addresses = $addresses;
00456             } else {
00457                 $structure = $addresses[0];
00458             }
00459 
00460         // Flat format
00461         } else {
00462             if ($is_group) {
00463                 $structure = array_merge($structure, $addresses);
00464             } else {
00465                 $structure = $addresses;
00466             }
00467         }
00468 
00469         return $structure;
00470     }

Mail_RFC822::_validateAddrSpec ( addr_spec  ) 

Function to validate an addr-spec.

addr-spec = local-part "@" domain

private

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

Definition at line 789 of file RFC822.php.

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

Referenced by _validateRouteAddr(), and validateMailbox().

00790     {
00791         $addr_spec = trim($addr_spec);
00792 
00793         // Split on @ sign if there is one.
00794         if (strpos($addr_spec, '@') !== false) {
00795             $parts      = explode('@', $addr_spec);
00796             $local_part = $this->_splitCheck($parts, '@');
00797             $domain     = substr($addr_spec, strlen($local_part . '@'));
00798 
00799         // No @ sign so assume the default domain.
00800         } else {
00801             $local_part = $addr_spec;
00802             $domain     = $this->default_domain;
00803         }
00804 
00805         if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
00806         if (($domain     = $this->_validateDomain($domain)) === false) return false;
00807         
00808         // Got here so return successful.
00809         return array('local_part' => $local_part, 'domain' => $domain);
00810     }

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 $atom The string to check.
Returns:
boolean Success or failure.

Definition at line 519 of file RFC822.php.

Referenced by _validatePhrase(), and _validateSubdomain().

00520     {
00521         if (!$this->validate) {
00522             // Validation has been turned off; assume the atom is okay.
00523             return true;
00524         }
00525 
00526         // Check for any char from ASCII 0 - ASCII 127
00527         if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) {
00528             return false;
00529         }
00530 
00531         // Check for specials:
00532         if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
00533             return false;
00534         }
00535 
00536         // Check for control characters (ASCII 0-31):
00537         if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
00538             return false;
00539         }
00540 
00541         return true;
00542     }

Mail_RFC822::_validateDliteral ( dliteral  ) 

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

private

Parameters:
string $dliteral The string to check.
Returns:
boolean Success or failure.

Definition at line 775 of file RFC822.php.

Referenced by _validateSubdomain().

00776     {
00777         return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
00778     }

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 $domain The string to check.
Returns:
mixed False on failure, or the validated domain on success.

Definition at line 727 of file RFC822.php.

References _splitCheck(), and _validateSubdomain().

Referenced by _validateAddrSpec(), and _validateRoute().

00728     {
00729         // Note the different use of $subdomains and $sub_domains                        
00730         $subdomains = explode('.', $domain);
00731 
00732         while (count($subdomains) > 0) {
00733             $sub_domains[] = $this->_splitCheck($subdomains, '.');
00734             for ($i = 0; $i < $this->index + 1; $i++)
00735                 array_shift($subdomains);
00736         }
00737 
00738         for ($i = 0; $i < count($sub_domains); $i++) {
00739             if (!$this->_validateSubdomain(trim($sub_domains[$i])))
00740                 return false;
00741         }
00742 
00743         // Managed to get here, so return input.
00744         return $domain;
00745     }

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

References _splitCheck(), and _validatePhrase().

Referenced by _validateAddrSpec().

00821     {
00822         $parts = explode('.', $local_part);
00823 
00824         // Split the local_part into words.
00825         while (count($parts) > 0){
00826             $words[] = $this->_splitCheck($parts, '.');
00827             for ($i = 0; $i < $this->index + 1; $i++) {
00828                 array_shift($parts);
00829             }
00830         }
00831 
00832         // Validate each word.
00833         for ($i = 0; $i < count($words); $i++) {
00834             if ($this->_validatePhrase(trim($words[$i])) === false) return false;
00835         }
00836 
00837         // Managed to get here, so return the input.
00838         return $local_part;
00839     }

Mail_RFC822::_validatePhrase ( phrase  ) 

Function to validate a phrase.

private

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

Definition at line 479 of file RFC822.php.

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

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

00480     {
00481         // Splits on one or more Tab or space.
00482         $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
00483 
00484         $phrase_parts = array();
00485         while (count($parts) > 0){
00486             $phrase_parts[] = $this->_splitCheck($parts, ' ');
00487             for ($i = 0; $i < $this->index + 1; $i++)
00488                 array_shift($parts);
00489         }
00490 
00491         for ($i = 0; $i < count($phrase_parts); $i++) {
00492             // If quoted string:
00493             if (substr($phrase_parts[$i], 0, 1) == '"') {
00494                 if (!$this->_validateQuotedString($phrase_parts[$i]))
00495                     return false;
00496                 continue;
00497             }
00498 
00499             // Otherwise it's an atom:
00500             if (!$this->_validateAtom($phrase_parts[$i])) return false;
00501         }
00502 
00503         return true;
00504     }

Mail_RFC822::_validateQuotedString ( qstring  ) 

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

private

Parameters:
string $qstring The string to check
Returns:
boolean Success or failure.

Definition at line 552 of file RFC822.php.

Referenced by _validatePhrase().

00553     {
00554         // Leading and trailing "
00555         $qstring = substr($qstring, 1, -1);
00556 
00557         // Perform check.
00558         return !(preg_match('/(.)[\x0D\\\\"]/', $qstring, $matches) && $matches[1] != '\\');
00559     }

Mail_RFC822::_validateRoute ( route  ) 

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

private

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

Definition at line 704 of file RFC822.php.

References _validateDomain().

Referenced by _validateRouteAddr().

00705     {
00706         // Split on comma.
00707         $domains = explode(',', trim($route));
00708 
00709         for ($i = 0; $i < count($domains); $i++) {
00710             $domains[$i] = str_replace('@', '', trim($domains[$i]));
00711             if (!$this->_validateDomain($domains[$i])) return false;
00712         }
00713 
00714         return $route;
00715     }

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_addr The string to check.
Returns:
mixed False on failure, or an array containing validated address/route information on success.

Definition at line 654 of file RFC822.php.

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

Referenced by validateMailbox().

00655     {
00656         // Check for colon.
00657         if (strpos($route_addr, ':') !== false) {
00658             $parts = explode(':', $route_addr);
00659             $route = $this->_splitCheck($parts, ':');
00660         } else {
00661             $route = $route_addr;
00662         }
00663 
00664         // If $route is same as $route_addr then the colon was in
00665         // quotes or brackets or, of course, non existent.
00666         if ($route === $route_addr){
00667             unset($route);
00668             $addr_spec = $route_addr;
00669             if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
00670                 return false;
00671             }
00672         } else {
00673             // Validate route part.
00674             if (($route = $this->_validateRoute($route)) === false) {
00675                 return false;
00676             }
00677 
00678             $addr_spec = substr($route_addr, strlen($route . ':'));
00679 
00680             // Validate addr-spec part.
00681             if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
00682                 return false;
00683             }
00684         }
00685 
00686         if (isset($route)) {
00687             $return['adl'] = $route;
00688         } else {
00689             $return['adl'] = '';
00690         }
00691 
00692         $return = array_merge($return, $addr_spec);
00693         return $return;
00694     }

Mail_RFC822::_validateSubdomain ( subdomain  ) 

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

private

Parameters:
string $subdomain The string to check.
Returns:
boolean Success or failure.

Definition at line 755 of file RFC822.php.

References _validateAtom(), and _validateDliteral().

Referenced by _validateDomain().

00756     {
00757         if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
00758             if (!$this->_validateDliteral($arr[1])) return false;
00759         } else {
00760             if (!$this->_validateAtom($subdomain)) return false;
00761         }
00762 
00763         // Got here, so return successful.
00764         return true;
00765     }

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 $data Addresses to count
Returns:
int Approximate count

Definition at line 851 of file RFC822.php.

00852     {
00853         return count(preg_split('/(?<!\\\\),/', $data));
00854     }

Mail_RFC822::isValidInetAddress ( data,
strict = false 
)

This is a email validating function seperate 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 $data Address to check
boolean $strict Optional stricter mode
Returns:
mixed False if it fails, an indexed array username/domain if it matches

Definition at line 869 of file RFC822.php.

00870     {
00871         $regex = $strict ? '/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
00872         if (preg_match($regex, trim($data), $matches)) {
00873             return array($matches[1], $matches[2]);
00874         } else {
00875             return false;
00876         }
00877     }

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 $address The address(es) to validate.
string $default_domain Default domain/host etc.
boolean $nest_groups Whether to return the structure with groups nested for easier viewing.
boolean $validate Whether 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 138 of file RFC822.php.

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

Referenced by Rmail::send().

00139     {
00140 
00141         if (!isset($this->mailRFC822)) {
00142             $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
00143             return $obj->parseAddressList();
00144         }
00145 
00146         if (isset($address))        $this->address        = $address;
00147         if (isset($default_domain)) $this->default_domain = $default_domain;
00148         if (isset($nest_groups))    $this->nestGroups     = $nest_groups;
00149         if (isset($validate))       $this->validate       = $validate;
00150         if (isset($limit))          $this->limit          = $limit;
00151 
00152         $this->structure  = array();
00153         $this->addresses  = array();
00154         $this->error      = null;
00155         $this->index      = null;
00156 
00157         while ($this->address = $this->_splitAddresses($this->address)) {
00158             continue;
00159         }
00160         
00161         if ($this->address === false || isset($this->error)) {
00162             return false;
00163         }
00164 
00165         // Reset timer since large amounts of addresses can take a long time to
00166         // get here
00167         set_time_limit(30);
00168 
00169         // Loop through all the addresses
00170         for ($i = 0; $i < count($this->addresses); $i++){
00171 
00172             if (($return = $this->_validateAddress($this->addresses[$i])) === false
00173                 || isset($this->error)) {
00174                 return false;
00175             }
00176             
00177             if (!$this->nestGroups) {
00178                 $this->structure = array_merge($this->structure, $return);
00179             } else {
00180                 $this->structure[] = $return;
00181             }
00182         }
00183 
00184         return $this->structure;
00185     }

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 &$mailbox The string to check.
Returns:
boolean Success or failure.

Definition at line 570 of file RFC822.php.

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

00571     {
00572         // A couple of defaults.
00573         $phrase  = '';
00574         $comment = '';
00575 
00576         // Catch any RFC822 comments and store them separately
00577         $_mailbox = $mailbox;
00578         while (strlen(trim($_mailbox)) > 0) {
00579             $parts = explode('(', $_mailbox);
00580             $before_comment = $this->_splitCheck($parts, '(');
00581             if ($before_comment != $_mailbox) {
00582                 // First char should be a (
00583                 $comment    = substr(str_replace($before_comment, '', $_mailbox), 1);
00584                 $parts      = explode(')', $comment);
00585                 $comment    = $this->_splitCheck($parts, ')');
00586                 $comments[] = $comment;
00587 
00588                 // +1 is for the trailing )
00589                 $_mailbox   = substr($_mailbox, strpos($_mailbox, $comment)+strlen($comment)+1);
00590             } else {
00591                 break;
00592             }
00593         }
00594 
00595         for($i=0; $i<count(@$comments); $i++){
00596             $mailbox = str_replace('('.$comments[$i].')', '', $mailbox);
00597         }
00598         $mailbox = trim($mailbox);
00599 
00600         // Check for name + route-addr
00601         if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
00602             $parts  = explode('<', $mailbox);
00603             $name   = $this->_splitCheck($parts, '<');
00604 
00605             $phrase     = trim($name);
00606             $route_addr = trim(substr($mailbox, strlen($name.'<'), -1));
00607 
00608             if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false)
00609                 return false;
00610 
00611         // Only got addr-spec
00612         } else {
00613             // First snip angle brackets if present.
00614             if (substr($mailbox,0,1) == '<' && substr($mailbox,-1) == '>')
00615                 $addr_spec = substr($mailbox,1,-1);
00616             else
00617                 $addr_spec = $mailbox;
00618 
00619             if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false)
00620                 return false;
00621         }
00622 
00623         // Construct the object that will be returned.
00624         $mbox = new stdClass();
00625 
00626         // Add the phrase (even if empty) and comments
00627         $mbox->personal = $phrase;
00628         $mbox->comment  = isset($comments) ? $comments : array();
00629 
00630         if (isset($route_addr)) {
00631             $mbox->mailbox = $route_addr['local_part'];
00632             $mbox->host    = $route_addr['domain'];
00633             $route_addr['adl'] !== '' ? $mbox->adl = $route_addr['adl'] : '';
00634         } else {
00635             $mbox->mailbox = $addr_spec['local_part'];
00636             $mbox->host    = $addr_spec['domain'];
00637         }
00638 
00639         $mailbox = $mbox;
00640         return true;
00641     }


Member Data Documentation

Mail_RFC822::$address = '' [private]

The address being parsed by the RFC822 object.

string $address

Definition at line 39 of file RFC822.php.

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

Mail_RFC822::$addresses = array() [private]

The array of raw addresses built up as we parse.

array $addresses

Definition at line 63 of file RFC822.php.

Referenced by _validateAddress().

Mail_RFC822::$default_domain = 'localhost' [private]

The default domain to use for unqualified addresses.

string $default_domain

Definition at line 45 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

Mail_RFC822::$error = null [private]

The current error message, if any.

string $error

Definition at line 75 of file RFC822.php.

Mail_RFC822::$index = null [private]

An internal counter/pointer.

integer $index

Definition at line 81 of file RFC822.php.

Mail_RFC822::$limit = null [private]

A limit after which processing stops int $limit.

Definition at line 101 of file RFC822.php.

Referenced by __construct(), and parseAddressList().

Mail_RFC822::$mailRFC822 = true [private]

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

boolean $mailRFC822

Definition at line 95 of file RFC822.php.

Mail_RFC822::$nestGroups = true [private]

Should we return a nested array showing groups, or flatten everything? boolean $nestGroups.

Definition at line 51 of file RFC822.php.

Mail_RFC822::$num_groups = 0 [private]

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

integer $num_groups public

Definition at line 88 of file RFC822.php.

Mail_RFC822::$structure = array() [private]

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

array $structure

Definition at line 69 of file RFC822.php.

Referenced by _validateAddress().

Mail_RFC822::$validate = true [private]

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

boolean $validate

Definition at line 57 of file RFC822.php.

Referenced by __construct(), and parseAddressList().


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

Generated on Wed May 6 23:10:50 2009 for fareofficelib by  doxygen 1.5.8