153 function __construct($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
155 if (isset($address)) $this->address =
$address;
157 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
158 if (isset($validate)) $this->validate =
$validate;
159 if (isset($limit)) $this->limit =
$limit;
174 function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
176 if (!isset($this) || !isset($this->mailRFC822)) {
177 $obj =
new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
178 return $obj->parseAddressList();
181 if (isset($address)) $this->address =
$address;
183 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
184 if (isset($validate)) $this->validate =
$validate;
185 if (isset($limit)) $this->limit =
$limit;
187 $this->structure =
array();
188 $this->addresses =
array();
193 $this->address = preg_replace(
'/\r?\n/',
"\r\n", $this->address);
194 $this->address = preg_replace(
'/\r\n(\t| )+/',
' ', $this->address);
198 if ($this->address ===
false || isset($this->error)) {
200 require_once
'Services/Mail/exceptions/class.ilMailException.php';
207 foreach ($this->addresses as $address) {
210 if (
$valid ===
false || isset($this->error)) {
212 require_once
'Services/Mail/exceptions/class.ilMailException.php';
217 if (!$this->nestGroups) {
218 $this->structure = array_merge($this->structure,
$valid);
220 $this->structure[] =
$valid;
236 if (!empty($this->limit) && count($this->addresses) == $this->limit) {
240 if ($this->
_isGroup($address) && !isset($this->error)) {
243 } elseif (!isset($this->error)) {
246 } elseif (isset($this->error)) {
251 $parts = explode($split_char, $address);
260 if (strpos($string,
':') ===
false) {
261 $this->error =
'Invalid address: ' . $string;
266 if (!$this->
_splitCheck(explode(
':', $string),
':')) {
276 $this->addresses[] =
array(
277 'address' => trim($string),
283 $address = trim(substr($address, strlen($string) + 1));
288 if ($is_group && substr($address, 0, 1) ==
','){
289 $address = trim(substr($address, 1));
292 } elseif (strlen($address) > 0) {
313 $parts = explode(
',', $address);
319 if (count($parts = explode(
':', $string)) > 1) {
321 return ($string2 !== $string);
339 for ($i = 0; $i < count($parts); $i++) {
344 || substr($string, -1) ==
'\\') {
345 if (isset($parts[$i + 1])) {
346 $string = $string . $char . $parts[$i + 1];
348 $this->error =
'Invalid address spec. Unclosed bracket or quotes';
370 $string = trim($string);
371 $iMax = strlen($string);
375 for (; $i < $iMax; ++$i) {
376 switch ($string[$i]) {
382 if ($slashes % 2 == 0) {
383 $in_quote = !$in_quote;
407 $num_angle_start = substr_count($string, $chars[0]);
408 $num_angle_end = substr_count($string, $chars[1]);
413 if ($num_angle_start < $num_angle_end) {
414 $this->error =
'Invalid address spec. Unmatched quote or bracket (' . $chars .
')';
417 return ($num_angle_start > $num_angle_end);
432 $parts = explode($char, $string);
433 for ($i = 0; $i < count($parts); $i++){
436 if (isset($parts[$i + 1]))
437 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
453 $addresses =
array();
455 if ($address[
'group']) {
459 $parts = explode(
':', $address[
'address']);
461 $structure =
array();
465 $this->error =
'Group name did not validate.';
470 if ($this->nestGroups) {
471 $structure =
new stdClass;
472 $structure->groupname = $groupname;
476 $address[
'address'] = ltrim(substr($address[
'address'], strlen($groupname .
':')));
482 while (strlen($address[
'address']) > 0) {
483 $parts = explode(
',', $address[
'address']);
485 $address[
'address'] = trim(substr($address[
'address'], strlen(end($addresses) .
',')));
488 $addresses[] = $address[
'address'];
494 if (!count($addresses)){
495 $this->error =
'Empty group.';
500 array_map(
'trim', $addresses);
507 for ($i = 0; $i < count($addresses); $i++) {
509 if (empty($this->error)) {
510 $this->error =
'Validation failed for: ' . $addresses[$i];
517 if ($this->nestGroups) {
521 $structure = $addresses[0];
527 $structure = array_merge($structure, $addresses);
546 $parts = preg_split(
'/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
548 $phrase_parts =
array();
549 while (count($parts) > 0){
551 for ($i = 0; $i < $this->index + 1; $i++)
555 foreach ($phrase_parts as $part) {
557 if (substr($part, 0, 1) ==
'"') {
586 if (!$this->validate) {
594 if (preg_match(
'/[][()<>@,;\\:". ]/', $atom)) {
599 if (preg_match(
'/[\\x00-\\x1F]+/', $atom)) {
604 if (!(
bool)preg_match(
'//u', $atom)) {
623 $qstring = substr($qstring, 1, -1);
626 return !preg_match(
'/[\x0D\\\\"]/', preg_replace(
'/\\\\./',
'', $qstring));
646 $_mailbox = $mailbox;
647 while (strlen(trim($_mailbox)) > 0) {
648 $parts = explode(
'(', $_mailbox);
650 if ($before_comment != $_mailbox) {
652 $comment = substr(str_replace($before_comment,
'', $_mailbox), 1);
658 $_mailbox = substr($_mailbox, strpos($_mailbox,
'('.
$comment)+strlen(
$comment)+2);
665 $mailbox = str_replace(
"($comment)",
'', $mailbox);
668 $mailbox = trim($mailbox);
671 if (substr($mailbox, -1) ==
'>' && substr($mailbox, 0, 1) !=
'<') {
672 $parts = explode(
'<', $mailbox);
675 $phrase = trim($name);
676 $route_addr = trim(substr($mailbox, strlen($name.
'<'), -1));
685 if (substr($mailbox, 0, 1) ==
'<' && substr($mailbox, -1) ==
'>') {
686 $addr_spec = substr($mailbox, 1, -1);
688 $addr_spec = $mailbox;
697 $mbox =
new stdClass();
700 $mbox->personal = $phrase;
701 $mbox->comment = isset($comments) ? $comments :
array();
703 if (isset($route_addr)) {
704 $mbox->mailbox = $route_addr[
'local_part'];
705 $mbox->host = $route_addr[
'domain'];
706 $route_addr[
'adl'] !==
'' ? $mbox->adl = $route_addr[
'adl'] :
'';
708 $mbox->mailbox = $addr_spec[
'local_part'];
709 $mbox->host = $addr_spec[
'domain'];
730 if (strpos($route_addr,
':') !==
false) {
731 $parts = explode(
':', $route_addr);
734 $route = $route_addr;
739 if ($route === $route_addr){
741 $addr_spec = $route_addr;
751 $addr_spec = substr($route_addr, strlen($route .
':'));
760 $return[
'adl'] = $route;
765 $return = array_merge($return, $addr_spec);
780 $domains = explode(
',', trim($route));
782 foreach ($domains as $domain) {
783 $domain = str_replace(
'@',
'', trim($domain));
803 $subdomains = explode(
'.', $domain);
805 while (count($subdomains) > 0) {
806 $sub_domains[] = $this->
_splitCheck($subdomains,
'.');
807 for ($i = 0; $i < $this->index + 1; $i++)
808 array_shift($subdomains);
811 foreach ($sub_domains as $sub_domain) {
830 if (preg_match(
'|^\[(.*)]$|', $subdomain, $arr)){
850 return !preg_match(
'/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] !=
'\\';
864 $addr_spec = trim($addr_spec);
870 if (strpos($addr_spec,
'@') !==
false) {
871 $parts = explode(
'@', $addr_spec);
873 $domain = substr($addr_spec, strlen($local_part .
'@'));
875 if (substr_count($addr_spec,
'@') != 1 && $local_part ==
'') {
876 $this->validate =
false;
877 $local_part = $addr_spec;
883 $local_part = $addr_spec;
889 if ($validateState != $this->validate) {
890 $this->validate = $validateState;
893 if (($domain = $this->
_validateDomain($domain)) ===
false)
return false;
896 return array(
'local_part' => $local_part,
'domain' => $domain);
909 $parts = explode(
'.', $local_part);
913 while (count($parts) > 0){
915 for ($i = 0; $i < $this->index + 1; $i++) {
921 foreach ($words as $word) {
923 if (strpos($word,
' ') && $word[0] !==
'"')
951 return count(preg_split(
'/(?<!\\\\),/',
$data));
969 $regex = $strict ?
'/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' :
'/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
970 if (preg_match($regex, trim(
$data), $matches)) {
971 return array($matches[1], $matches[2]);
$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 ; ...
Create styles array
The data for the language used.
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
$mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
$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.
__construct($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>