156 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
175 if (!isset($this) || !isset($this->mailRFC822)) {
177 return $obj->parseAddressList();
182 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
186 $this->structure = array();
187 $this->addresses = array();
192 $this->address = preg_replace(
'/\r?\n/',
"\r\n", $this->address);
193 $this->address = preg_replace(
'/\r\n(\t| )+/',
' ', $this->address);
197 if ($this->address ===
false || isset($this->
error)) {
198 require_once
'PEAR.php';
204 foreach ($this->addresses as
$address) {
208 require_once
'PEAR.php';
212 if (!$this->nestGroups) {
213 $this->structure = array_merge($this->structure,
$valid);
215 $this->structure[] =
$valid;
231 if (!empty($this->limit) && count($this->addresses) == $this->limit) {
238 } elseif (!isset($this->
error)) {
241 } elseif (isset($this->
error)) {
246 $parts = explode($split_char,
$address);
255 if (strpos($string,
':') ===
false) {
256 $this->
error =
'Invalid address: ' . $string;
261 if (!$this->
_splitCheck(explode(
':', $string),
':')) {
271 $this->addresses[] = array(
272 'address' => trim($string),
283 if ($is_group && substr(
$address, 0, 1) ==
','){
314 if (count($parts = explode(
':', $string)) > 1) {
316 return ($string2 !== $string);
334 for ($i = 0; $i < count($parts); $i++) {
339 || substr($string, -1) ==
'\\') {
340 if (isset($parts[$i + 1])) {
341 $string = $string . $char . $parts[$i + 1];
343 $this->
error =
'Invalid address spec. Unclosed bracket or quotes';
365 $string = trim($string);
366 $iMax = strlen($string);
370 for (; $i < $iMax; ++$i) {
371 switch ($string[$i]) {
377 if ($slashes % 2 == 0) {
378 $in_quote = !$in_quote;
402 $num_angle_start = substr_count($string, $chars[0]);
403 $num_angle_end = substr_count($string, $chars[1]);
408 if ($num_angle_start < $num_angle_end) {
409 $this->
error =
'Invalid address spec. Unmatched quote or bracket (' . $chars .
')';
412 return ($num_angle_start > $num_angle_end);
427 $parts = explode($char, $string);
428 for ($i = 0; $i < count($parts); $i++){
431 if (isset($parts[$i + 1]))
432 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
454 $parts = explode(
':',
$address[
'address']);
460 $this->
error =
'Group name did not validate.';
465 if ($this->nestGroups) {
471 $address[
'address'] = ltrim(substr(
$address[
'address'], strlen($groupname .
':')));
477 while (strlen(
$address[
'address']) > 0) {
478 $parts = explode(
',',
$address[
'address']);
490 $this->
error =
'Empty group.';
504 if (empty($this->
error)) {
505 $this->
error =
'Validation failed for: ' . $addresses[$i];
512 if ($this->nestGroups) {
541 $parts = preg_split(
'/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
543 $phrase_parts = array();
544 while (count($parts) > 0){
546 for ($i = 0; $i < $this->index + 1; $i++)
550 foreach ($phrase_parts as $part) {
552 if (substr($part, 0, 1) ==
'"') {
581 if (!$this->validate) {
589 if (preg_match(
'/[][()<>@,;\\:". ]/', $atom)) {
594 if (preg_match(
'/[\\x00-\\x1F]+/', $atom)) {
599 if (!(
bool)preg_match(
'//u', $atom)) {
618 $qstring = substr($qstring, 1, -1);
621 return !preg_match(
'/[\x0D\\\\"]/', preg_replace(
'/\\\\./',
'', $qstring));
641 $_mailbox = $mailbox;
642 while (strlen(trim($_mailbox)) > 0) {
643 $parts = explode(
'(', $_mailbox);
645 if ($before_comment != $_mailbox) {
647 $comment = substr(str_replace($before_comment,
'', $_mailbox), 1);
653 $_mailbox = substr($_mailbox, strpos($_mailbox,
'('.
$comment)+strlen(
$comment)+2);
660 $mailbox = str_replace(
"($comment)",
'', $mailbox);
663 $mailbox = trim($mailbox);
666 if (substr($mailbox, -1) ==
'>' && substr($mailbox, 0, 1) !=
'<') {
667 $parts = explode(
'<', $mailbox);
670 $phrase = trim($name);
671 $route_addr = trim(substr($mailbox, strlen($name.
'<'), -1));
680 if (substr($mailbox, 0, 1) ==
'<' && substr($mailbox, -1) ==
'>') {
681 $addr_spec = substr($mailbox, 1, -1);
683 $addr_spec = $mailbox;
692 $mbox =
new stdClass();
695 $mbox->personal = $phrase;
696 $mbox->comment = isset($comments) ? $comments : array();
698 if (isset($route_addr)) {
699 $mbox->mailbox = $route_addr[
'local_part'];
700 $mbox->host = $route_addr[
'domain'];
701 $route_addr[
'adl'] !==
'' ? $mbox->adl = $route_addr[
'adl'] :
'';
703 $mbox->mailbox = $addr_spec[
'local_part'];
704 $mbox->host = $addr_spec[
'domain'];
725 if (strpos($route_addr,
':') !==
false) {
726 $parts = explode(
':', $route_addr);
729 $route = $route_addr;
734 if ($route === $route_addr){
736 $addr_spec = $route_addr;
746 $addr_spec = substr($route_addr, strlen($route .
':'));
755 $return[
'adl'] = $route;
760 $return = array_merge($return, $addr_spec);
775 $domains = explode(
',', trim($route));
777 foreach ($domains as $domain) {
778 $domain = str_replace(
'@',
'', trim($domain));
798 $subdomains = explode(
'.', $domain);
800 while (count($subdomains) > 0) {
801 $sub_domains[] = $this->
_splitCheck($subdomains,
'.');
802 for ($i = 0; $i < $this->index + 1; $i++)
803 array_shift($subdomains);
806 foreach ($sub_domains as $sub_domain) {
825 if (preg_match(
'|^\[(.*)]$|', $subdomain, $arr)){
845 return !preg_match(
'/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] !=
'\\';
859 $addr_spec = trim($addr_spec);
862 if (strpos($addr_spec,
'@') !==
false) {
863 $parts = explode(
'@', $addr_spec);
865 $domain = substr($addr_spec, strlen($local_part .
'@'));
869 $local_part = $addr_spec;
874 if (($domain = $this->
_validateDomain($domain)) ===
false)
return false;
877 return array(
'local_part' => $local_part,
'domain' => $domain);
890 $parts = explode(
'.', $local_part);
894 while (count($parts) > 0){
896 for ($i = 0; $i < $this->index + 1; $i++) {
902 foreach ($words as $word) {
904 if (strpos($word,
' ') && $word[0] !==
'"')
932 return count(preg_split(
'/(?<!\\\\),/',
$data));
950 $regex = $strict ?
'/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' :
'/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
951 if (preg_match($regex, trim(
$data), $matches)) {
952 return array($matches[1], $matches[2]);
$error
The current error message, if any.
$nestGroups
Should we return a nested array showing groups, or flatten everything?
_validateSubdomain($subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
_splitCheck($parts, $char)
A common function that will check an exploded string.
isValidInetAddress($data, $strict=false)
This is a email validating function separate to the rest of the class.
_splitAddresses($address)
Splits an address into separate addresses.
$mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
$structure
The final array of parsed address information that we build up.
$default_domain
The default domain to use for unqualified addresses.
_validatePhrase($phrase)
Function to validate a phrase.
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
parseAddressList($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Starts the whole process.
$validate
Whether or not to validate atoms for non-ascii characters.
_hasUnclosedBracketsSub($string, &$num, $char)
Sub function that is used only by hasUnclosedBrackets().
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials,...
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
approximateCount($data)
Returns an approximate count of how many addresses are in the given string.
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Mail_RFC822($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Sets up the object.
$addresses
The array of raw addresses built up as we parse.
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
_validateAddress($address)
Function to begin checking the address.
_isGroup($address)
Checks for a group at the start of the string.
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
$address
The address being parsed by the RFC822 object.
$index
An internal counter/pointer.
$num_groups
The number of groups that have been found in the address list.
_validateRoute($route)
Function to validate a route, which is: route = 1#("@" domain) ":".
$limit
A limit after which processing stops.
_validateRouteAddr($route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">".
& 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's de...
error($a_errmsg)
set error message @access public