152 function Mail_RFC822($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
154 if (isset($address)) $this->address =
$address;
156 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
157 if (isset($validate)) $this->validate =
$validate;
158 if (isset($limit)) $this->limit =
$limit;
173 function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
175 if (!isset($this) || !isset($this->mailRFC822)) {
176 $obj =
new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
177 return $obj->parseAddressList();
180 if (isset($address)) $this->address =
$address;
182 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
183 if (isset($validate)) $this->validate =
$validate;
184 if (isset($limit)) $this->limit =
$limit;
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) {
207 if (
$valid ===
false || isset($this->error)) {
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) {
235 if ($this->
_isGroup($address) && !isset($this->error)) {
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),
278 $address = trim(substr($address, strlen($string) + 1));
283 if ($is_group && substr($address, 0, 1) ==
','){
284 $address = trim(substr($address, 1));
287 } elseif (strlen($address) > 0) {
308 $parts = explode(
',', $address);
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];
448 $addresses = array();
450 if ($address[
'group']) {
454 $parts = explode(
':', $address[
'address']);
456 $structure = array();
460 $this->error =
'Group name did not validate.';
465 if ($this->nestGroups) {
466 $structure =
new stdClass;
467 $structure->groupname = $groupname;
471 $address[
'address'] = ltrim(substr($address[
'address'], strlen($groupname .
':')));
477 while (strlen($address[
'address']) > 0) {
478 $parts = explode(
',', $address[
'address']);
480 $address[
'address'] = trim(substr($address[
'address'], strlen(end($addresses) .
',')));
483 $addresses[] = $address[
'address'];
489 if (!count($addresses)){
490 $this->error =
'Empty group.';
495 array_map(
'trim', $addresses);
502 for ($i = 0; $i < count($addresses); $i++) {
504 if (empty($this->error)) {
505 $this->error =
'Validation failed for: ' . $addresses[$i];
512 if ($this->nestGroups) {
516 $structure = $addresses[0];
522 $structure = array_merge($structure, $addresses);
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) {
587 if (!preg_match(
'/^[\\x00-\\x7E]+$/i', $atom, $matches)) {
592 if (preg_match(
'/[][()<>@,;\\:". ]/', $atom)) {
597 if (preg_match(
'/[\\x00-\\x1F]+/', $atom)) {
615 $qstring = substr($qstring, 1, -1);
618 return !preg_match(
'/[\x0D\\\\"]/', preg_replace(
'/\\\\./',
'', $qstring));
638 $_mailbox = $mailbox;
639 while (strlen(trim($_mailbox)) > 0) {
640 $parts = explode(
'(', $_mailbox);
642 if ($before_comment != $_mailbox) {
644 $comment = substr(str_replace($before_comment,
'', $_mailbox), 1);
650 $_mailbox = substr($_mailbox, strpos($_mailbox,
'('.
$comment)+strlen(
$comment)+2);
657 $mailbox = str_replace(
"($comment)",
'', $mailbox);
660 $mailbox = trim($mailbox);
663 if (substr($mailbox, -1) ==
'>' && substr($mailbox, 0, 1) !=
'<') {
664 $parts = explode(
'<', $mailbox);
667 $phrase = trim($name);
668 $route_addr = trim(substr($mailbox, strlen($name.
'<'), -1));
677 if (substr($mailbox, 0, 1) ==
'<' && substr($mailbox, -1) ==
'>') {
678 $addr_spec = substr($mailbox, 1, -1);
680 $addr_spec = $mailbox;
689 $mbox =
new stdClass();
692 $mbox->personal = $phrase;
693 $mbox->comment = isset($comments) ? $comments : array();
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'] :
'';
700 $mbox->mailbox = $addr_spec[
'local_part'];
701 $mbox->host = $addr_spec[
'domain'];
722 if (strpos($route_addr,
':') !==
false) {
723 $parts = explode(
':', $route_addr);
726 $route = $route_addr;
731 if ($route === $route_addr){
733 $addr_spec = $route_addr;
743 $addr_spec = substr($route_addr, strlen($route .
':'));
752 $return[
'adl'] = $route;
757 $return = array_merge($return, $addr_spec);
772 $domains = explode(
',', trim($route));
774 foreach ($domains as $domain) {
775 $domain = str_replace(
'@',
'', trim($domain));
795 $subdomains = explode(
'.', $domain);
797 while (count($subdomains) > 0) {
798 $sub_domains[] = $this->
_splitCheck($subdomains,
'.');
799 for ($i = 0; $i < $this->index + 1; $i++)
800 array_shift($subdomains);
803 foreach ($sub_domains as $sub_domain) {
822 if (preg_match(
'|^\[(.*)]$|', $subdomain, $arr)){
842 return !preg_match(
'/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] !=
'\\';
856 $addr_spec = trim($addr_spec);
859 if (strpos($addr_spec,
'@') !==
false) {
860 $parts = explode(
'@', $addr_spec);
862 $domain = substr($addr_spec, strlen($local_part .
'@'));
866 $local_part = $addr_spec;
871 if (($domain = $this->
_validateDomain($domain)) ===
false)
return false;
874 return array(
'local_part' => $local_part,
'domain' => $domain);
887 $parts = explode(
'.', $local_part);
891 while (count($parts) > 0){
893 for ($i = 0; $i < $this->index + 1; $i++) {
899 foreach ($words as $word) {
901 if (strpos($word,
' ') && $word[0] !==
'"')
925 return count(preg_split(
'/(?<!\\\\),/',
$data));
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]);
$index
An internal counter/pointer.
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]"...
$validate
Whether or not to validate atoms for non-ascii characters.
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
_validateRoute($route)
Function to validate a route, which is: route = 1#("@" domain) ":".
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
$default_domain
The default domain to use for unqualified addresses.
_hasUnclosedBracketsSub($string, &$num, $char)
Sub function that is used only by hasUnclosedBrackets().
parseAddressList($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Starts the whole process.
_validateAddress($address)
Function to begin checking the address.
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain...
$structure
The final array of parsed address information that we build up.
_splitCheck($parts, $char)
A common function that will check an exploded string.
_validatePhrase($phrase)
Function to validate a phrase.
$limit
A limit after which processing stops.
$error
The current error message, if any.
_splitAddresses($address)
Splits an address into separate addresses.
_validateRouteAddr($route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">"...
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <"> ...
$num_groups
The number of groups that have been found in the address list.
approximateCount($data)
Returns an approximate count of how many addresses are in the given string.
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
$mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
& 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...
$address
The address being parsed by the RFC822 object.
isValidInetAddress($data, $strict=false)
This is a email validating function separate to the rest of the class.
$nestGroups
Should we return a nested array showing groups, or flatten everything?
_isGroup($address)
Checks for a group at the start of the string.
Mail_RFC822($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Sets up the object.
_validateSubdomain($subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
$addresses
The array of raw addresses built up as we parse.
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials, SPACE and CTLs>