ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
RFC822.php
Go to the documentation of this file.
1<?php
71
76 var $address = '';
77
82 var $default_domain = 'localhost';
83
88 var $nestGroups = true;
89
94 var $validate = true;
95
100 var $addresses = array();
101
106 var $structure = array();
107
112 var $error = null;
113
118 var $index = null;
119
125 var $num_groups = 0;
126
132 var $mailRFC822 = true;
133
138 var $limit = null;
139
152 // php7-workaround alex: constructor
153 function __construct($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
154 {
155 if (isset($address)) $this->address = $address;
156 if (isset($default_domain)) $this->default_domain = $default_domain;
157 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
158 if (isset($validate)) $this->validate = $validate;
159 if (isset($limit)) $this->limit = $limit;
160 }
161
174 function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
175 {
176 if (!isset($this) || !isset($this->mailRFC822)) {
177 $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
178 return $obj->parseAddressList();
179 }
180
181 if (isset($address)) $this->address = $address;
182 if (isset($default_domain)) $this->default_domain = $default_domain;
183 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
184 if (isset($validate)) $this->validate = $validate;
185 if (isset($limit)) $this->limit = $limit;
186
187 $this->structure = array();
188 $this->addresses = array();
189 $this->error = null;
190 $this->index = null;
191
192 // Unfold any long lines in $this->address.
193 $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
194 $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
195
196 while ($this->address = $this->_splitAddresses($this->address));
197
198 if ($this->address === false || isset($this->error)) {
199 // mjansen patch 14 Ap 2016 start
200 require_once 'Services/Mail/exceptions/class.ilMailException.php';
201 throw new ilMailException($this->error);
202 // mjansen patch 14 Ap 2016 end
203 }
204
205 // Validate each address individually. If we encounter an invalid
206 // address, stop iterating and return an error immediately.
207 foreach ($this->addresses as $address) {
208 $valid = $this->_validateAddress($address);
209
210 if ($valid === false || isset($this->error)) {
211 // mjansen patch 14 Ap 2016 start
212 require_once 'Services/Mail/exceptions/class.ilMailException.php';
213 throw new ilMailException($this->error);
214 // mjansen patch 14 Ap 2016 end
215 }
216
217 if (!$this->nestGroups) {
218 $this->structure = array_merge($this->structure, $valid);
219 } else {
220 $this->structure[] = $valid;
221 }
222 }
223
224 return $this->structure;
225 }
226
235 {
236 if (!empty($this->limit) && count($this->addresses) == $this->limit) {
237 return '';
238 }
239
240 if ($this->_isGroup($address) && !isset($this->error)) {
241 $split_char = ';';
242 $is_group = true;
243 } elseif (!isset($this->error)) {
244 $split_char = ',';
245 $is_group = false;
246 } elseif (isset($this->error)) {
247 return false;
248 }
249
250 // Split the string based on the above ten or so lines.
251 $parts = explode($split_char, $address);
252 $string = $this->_splitCheck($parts, $split_char);
253
254 // If a group...
255 if ($is_group) {
256 // If $string does not contain a colon outside of
257 // brackets/quotes etc then something's fubar.
258
259 // First check there's a colon at all:
260 if (strpos($string, ':') === false) {
261 $this->error = 'Invalid address: ' . $string;
262 return false;
263 }
264
265 // Now check it's outside of brackets/quotes:
266 if (!$this->_splitCheck(explode(':', $string), ':')) {
267 return false;
268 }
269
270 // We must have a group at this point, so increase the counter:
271 $this->num_groups++;
272 }
273
274 // $string now contains the first full address/group.
275 // Add to the addresses array.
276 $this->addresses[] = array(
277 'address' => trim($string),
278 'group' => $is_group
279 );
280
281 // Remove the now stored address from the initial line, the +1
282 // is to account for the explode character.
283 $address = trim(substr($address, strlen($string) + 1));
284
285 // If the next char is a comma and this was a group, then
286 // there are more addresses, otherwise, if there are any more
287 // chars, then there is another address.
288 if ($is_group && substr($address, 0, 1) == ','){
289 $address = trim(substr($address, 1));
290 return $address;
291
292 } elseif (strlen($address) > 0) {
293 return $address;
294
295 } else {
296 return '';
297 }
298
299 // If you got here then something's off
300 return false;
301 }
302
311 {
312 // First comma not in quotes, angles or escaped:
313 $parts = explode(',', $address);
314 $string = $this->_splitCheck($parts, ',');
315
316 // Now we have the first address, we can reliably check for a
317 // group by searching for a colon that's not escaped or in
318 // quotes or angle brackets.
319 if (count($parts = explode(':', $string)) > 1) {
320 $string2 = $this->_splitCheck($parts, ':');
321 return ($string2 !== $string);
322 } else {
323 return false;
324 }
325 }
326
335 function _splitCheck($parts, $char)
336 {
337 $string = $parts[0];
338
339 for ($i = 0; $i < count($parts); $i++) {
340 if ($this->_hasUnclosedQuotes($string)
341 || $this->_hasUnclosedBrackets($string, '<>')
342 || $this->_hasUnclosedBrackets($string, '[]')
343 || $this->_hasUnclosedBrackets($string, '()')
344 || substr($string, -1) == '\\') {
345 if (isset($parts[$i + 1])) {
346 $string = $string . $char . $parts[$i + 1];
347 } else {
348 $this->error = 'Invalid address spec. Unclosed bracket or quotes';
349 return false;
350 }
351 } else {
352 $this->index = $i;
353 break;
354 }
355 }
356
357 return $string;
358 }
359
368 function _hasUnclosedQuotes($string)
369 {
370 $string = trim($string);
371 $iMax = strlen($string);
372 $in_quote = false;
373 $i = $slashes = 0;
374
375 for (; $i < $iMax; ++$i) {
376 switch ($string[$i]) {
377 case '\\':
378 ++$slashes;
379 break;
380
381 case '"':
382 if ($slashes % 2 == 0) {
383 $in_quote = !$in_quote;
384 }
385 // Fall through to default action below.
386
387 default:
388 $slashes = 0;
389 break;
390 }
391 }
392
393 return $in_quote;
394 }
395
405 function _hasUnclosedBrackets($string, $chars)
406 {
407 $num_angle_start = substr_count($string, $chars[0]);
408 $num_angle_end = substr_count($string, $chars[1]);
409
410 $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
411 $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
412
413 if ($num_angle_start < $num_angle_end) {
414 $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
415 return false;
416 } else {
417 return ($num_angle_start > $num_angle_end);
418 }
419 }
420
430 function _hasUnclosedBracketsSub($string, &$num, $char)
431 {
432 $parts = explode($char, $string);
433 for ($i = 0; $i < count($parts); $i++){
434 if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
435 $num--;
436 if (isset($parts[$i + 1]))
437 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
438 }
439
440 return $num;
441 }
442
451 {
452 $is_group = false;
453 $addresses = array();
454
455 if ($address['group']) {
456 $is_group = true;
457
458 // Get the group part of the name
459 $parts = explode(':', $address['address']);
460 $groupname = $this->_splitCheck($parts, ':');
461 $structure = array();
462
463 // And validate the group part of the name.
464 if (!$this->_validatePhrase($groupname)){
465 $this->error = 'Group name did not validate.';
466 return false;
467 } else {
468 // Don't include groups if we are not nesting
469 // them. This avoids returning invalid addresses.
470 if ($this->nestGroups) {
471 $structure = new stdClass;
472 $structure->groupname = $groupname;
473 }
474 }
475
476 $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
477 }
478
479 // If a group then split on comma and put into an array.
480 // Otherwise, Just put the whole address in an array.
481 if ($is_group) {
482 while (strlen($address['address']) > 0) {
483 $parts = explode(',', $address['address']);
484 $addresses[] = $this->_splitCheck($parts, ',');
485 $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
486 }
487 } else {
488 $addresses[] = $address['address'];
489 }
490
491 // Check that $addresses is set, if address like this:
492 // Groupname:;
493 // Then errors were appearing.
494 if (!count($addresses)){
495 $this->error = 'Empty group.';
496 return false;
497 }
498
499 // Trim the whitespace from all of the address strings.
500 array_map('trim', $addresses);
501
502 // Validate each mailbox.
503 // Format could be one of: name <geezer@domain.com>
504 // geezer@domain.com
505 // geezer
506 // ... or any other format valid by RFC 822.
507 for ($i = 0; $i < count($addresses); $i++) {
508 if (!$this->validateMailbox($addresses[$i])) {
509 if (empty($this->error)) {
510 $this->error = 'Validation failed for: ' . $addresses[$i];
511 }
512 return false;
513 }
514 }
515
516 // Nested format
517 if ($this->nestGroups) {
518 if ($is_group) {
519 $structure->addresses = $addresses;
520 } else {
522 }
523
524 // Flat format
525 } else {
526 if ($is_group) {
527 $structure = array_merge($structure, $addresses);
528 } else {
530 }
531 }
532
533 return $structure;
534 }
535
543 function _validatePhrase($phrase)
544 {
545 // Splits on one or more Tab or space.
546 $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
547
548 $phrase_parts = array();
549 while (count($parts) > 0){
550 $phrase_parts[] = $this->_splitCheck($parts, ' ');
551 for ($i = 0; $i < $this->index + 1; $i++)
552 array_shift($parts);
553 }
554
555 foreach ($phrase_parts as $part) {
556 // If quoted string:
557 if (substr($part, 0, 1) == '"') {
558 if (!$this->_validateQuotedString($part)) {
559 return false;
560 }
561 continue;
562 }
563
564 // Otherwise it's an atom:
565 if (!$this->_validateAtom($part)) return false;
566 }
567
568 return true;
569 }
570
584 function _validateAtom($atom)
585 {
586 if (!$this->validate) {
587 // Validation has been turned off; assume the atom is okay.
588 return true;
589 }
590
591 // Check for any char from ASCII 0 - ASCII 127
592 // mjansen patch 16 Sep 2015 start
593 // Check for specials:
594 if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
595 return false;
596 }
597
598 // Check for control characters (ASCII 0-31):
599 if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
600 return false;
601 }
602 #16291
603 #17618
604 if (!(bool)preg_match('//u', $atom)) {
605 return false;
606 }
607 // mjansen patch 16 Sep 2015 end
608
609 return true;
610 }
611
620 function _validateQuotedString($qstring)
621 {
622 // Leading and trailing "
623 $qstring = substr($qstring, 1, -1);
624
625 // Perform check, removing quoted characters first.
626 return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
627 }
628
638 function validateMailbox(&$mailbox)
639 {
640 // A couple of defaults.
641 $phrase = '';
642 $comment = '';
643 $comments = array();
644
645 // Catch any RFC822 comments and store them separately.
646 $_mailbox = $mailbox;
647 while (strlen(trim($_mailbox)) > 0) {
648 $parts = explode('(', $_mailbox);
649 $before_comment = $this->_splitCheck($parts, '(');
650 if ($before_comment != $_mailbox) {
651 // First char should be a (.
652 $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
653 $parts = explode(')', $comment);
654 $comment = $this->_splitCheck($parts, ')');
655 $comments[] = $comment;
656
657 // +2 is for the brackets
658 $_mailbox = substr($_mailbox, strpos($_mailbox, '('.$comment)+strlen($comment)+2);
659 } else {
660 break;
661 }
662 }
663
664 foreach ($comments as $comment) {
665 $mailbox = str_replace("($comment)", '', $mailbox);
666 }
667
668 $mailbox = trim($mailbox);
669
670 // Check for name + route-addr
671 if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
672 $parts = explode('<', $mailbox);
673 $name = $this->_splitCheck($parts, '<');
674
675 $phrase = trim($name);
676 $route_addr = trim(substr($mailbox, strlen($name.'<'), -1));
677
678 if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
679 return false;
680 }
681
682 // Only got addr-spec
683 } else {
684 // First snip angle brackets if present.
685 if (substr($mailbox, 0, 1) == '<' && substr($mailbox, -1) == '>') {
686 $addr_spec = substr($mailbox, 1, -1);
687 } else {
688 $addr_spec = $mailbox;
689 }
690
691 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
692 return false;
693 }
694 }
695
696 // Construct the object that will be returned.
697 $mbox = new stdClass();
698
699 // Add the phrase (even if empty) and comments
700 $mbox->personal = $phrase;
701 $mbox->comment = isset($comments) ? $comments : array();
702
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'] : '';
707 } else {
708 $mbox->mailbox = $addr_spec['local_part'];
709 $mbox->host = $addr_spec['domain'];
710 }
711
712 $mailbox = $mbox;
713 return true;
714 }
715
727 function _validateRouteAddr($route_addr)
728 {
729 // Check for colon.
730 if (strpos($route_addr, ':') !== false) {
731 $parts = explode(':', $route_addr);
732 $route = $this->_splitCheck($parts, ':');
733 } else {
734 $route = $route_addr;
735 }
736
737 // If $route is same as $route_addr then the colon was in
738 // quotes or brackets or, of course, non existent.
739 if ($route === $route_addr){
740 unset($route);
741 $addr_spec = $route_addr;
742 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
743 return false;
744 }
745 } else {
746 // Validate route part.
747 if (($route = $this->_validateRoute($route)) === false) {
748 return false;
749 }
750
751 $addr_spec = substr($route_addr, strlen($route . ':'));
752
753 // Validate addr-spec part.
754 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
755 return false;
756 }
757 }
758
759 if (isset($route)) {
760 $return['adl'] = $route;
761 } else {
762 $return['adl'] = '';
763 }
764
765 $return = array_merge($return, $addr_spec);
766 return $return;
767 }
768
777 function _validateRoute($route)
778 {
779 // Split on comma.
780 $domains = explode(',', trim($route));
781
782 foreach ($domains as $domain) {
783 $domain = str_replace('@', '', trim($domain));
784 if (!$this->_validateDomain($domain)) return false;
785 }
786
787 return $route;
788 }
789
800 function _validateDomain($domain)
801 {
802 // Note the different use of $subdomains and $sub_domains
803 $subdomains = explode('.', $domain);
804
805 while (count($subdomains) > 0) {
806 $sub_domains[] = $this->_splitCheck($subdomains, '.');
807 for ($i = 0; $i < $this->index + 1; $i++)
808 array_shift($subdomains);
809 }
810
811 foreach ($sub_domains as $sub_domain) {
812 if (!$this->_validateSubdomain(trim($sub_domain)))
813 return false;
814 }
815
816 // Managed to get here, so return input.
817 return $domain;
818 }
819
828 function _validateSubdomain($subdomain)
829 {
830 if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
831 if (!$this->_validateDliteral($arr[1])) return false;
832 } else {
833 if (!$this->_validateAtom($subdomain)) return false;
834 }
835
836 // Got here, so return successful.
837 return true;
838 }
839
848 function _validateDliteral($dliteral)
849 {
850 return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
851 }
852
862 function _validateAddrSpec($addr_spec)
863 {
864 $addr_spec = trim($addr_spec);
865
866 // mjansen patch 16 Sep 2016 start
867 $validateState = $this->validate;
868 // mjansen patch 16 Sep 2016 end
869 // Split on @ sign if there is one.
870 if (strpos($addr_spec, '@') !== false) {
871 $parts = explode('@', $addr_spec);
872 $local_part = $this->_splitCheck($parts, '@');
873 $domain = substr($addr_spec, strlen($local_part . '@'));
874 // mjansen patch 16 Sep 2016 start
875 if (substr_count($addr_spec, '@') != 1 && $local_part == '') {
876 $this->validate = false;
877 $local_part = $addr_spec;
878 $domain = $this->default_domain;
879 }
880 // mjansen patch 16 Sep 2016 end
881 // No @ sign so assume the default domain.
882 } else {
883 $local_part = $addr_spec;
884 $domain = $this->default_domain;
885 }
886
887 if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
888 // mjansen patch 16 Sep 2016 start
889 if ($validateState != $this->validate) {
890 $this->validate = $validateState;
891 }
892 // mjansen patch 16 Sep 2016 end
893 if (($domain = $this->_validateDomain($domain)) === false) return false;
894
895 // Got here so return successful.
896 return array('local_part' => $local_part, 'domain' => $domain);
897 }
898
907 function _validateLocalPart($local_part)
908 {
909 $parts = explode('.', $local_part);
910 $words = array();
911
912 // Split the local_part into words.
913 while (count($parts) > 0){
914 $words[] = $this->_splitCheck($parts, '.');
915 for ($i = 0; $i < $this->index + 1; $i++) {
916 array_shift($parts);
917 }
918 }
919
920 // Validate each word.
921 foreach ($words as $word) {
922 // If this word contains an unquoted space, it is invalid. (6.2.4)
923 if (strpos($word, ' ') && $word[0] !== '"')
924 {
925 // mjansen patch 24 Feb 2016 start
926 // Mantis issue #18018
927 // # http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
928 //return false;
929 // mjansen patch 24 Feb 2016 end
930 }
931
932 if ($this->_validatePhrase(trim($word)) === false) return false;
933 }
934
935 // Managed to get here, so return the input.
936 return $local_part;
937 }
938
950 {
951 return count(preg_split('/(?<!\\\\),/', $data));
952 }
953
967 function isValidInetAddress($data, $strict = false)
968 {
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]);
972 } else {
973 return false;
974 }
975 }
976
977}
$comment
Definition: buildRTE.php:83
An exception for terminatinating execution or to throw for unit testing.
$error
The current error message, if any.
Definition: RFC822.php:112
$nestGroups
Should we return a nested array showing groups, or flatten everything?
Definition: RFC822.php:88
_validateSubdomain($subdomain)
Function to validate a subdomain: subdomain = domain-ref / domain-literal.
Definition: RFC822.php:828
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:335
isValidInetAddress($data, $strict=false)
This is a email validating function separate to the rest of the class.
Definition: RFC822.php:967
_splitAddresses($address)
Splits an address into separate addresses.
Definition: RFC822.php:234
$mailRFC822
A variable so that we can tell whether or not we're inside a Mail_RFC822 object.
Definition: RFC822.php:132
$structure
The final array of parsed address information that we build up.
Definition: RFC822.php:106
$default_domain
The default domain to use for unqualified addresses.
Definition: RFC822.php:82
_validatePhrase($phrase)
Function to validate a phrase.
Definition: RFC822.php:543
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
Definition: RFC822.php:620
__construct($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Sets up the object.
Definition: RFC822.php:153
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".
Definition: RFC822.php:848
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:907
parseAddressList($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Starts the whole process.
Definition: RFC822.php:174
$validate
Whether or not to validate atoms for non-ascii characters.
Definition: RFC822.php:94
_hasUnclosedBracketsSub($string, &$num, $char)
Sub function that is used only by hasUnclosedBrackets().
Definition: RFC822.php:430
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials,...
Definition: RFC822.php:584
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
Definition: RFC822.php:800
approximateCount($data)
Returns an approximate count of how many addresses are in the given string.
Definition: RFC822.php:949
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:638
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:368
$addresses
The array of raw addresses built up as we parse.
Definition: RFC822.php:100
_validateAddrSpec($addr_spec)
Function to validate an addr-spec.
Definition: RFC822.php:862
_validateAddress($address)
Function to begin checking the address.
Definition: RFC822.php:450
_isGroup($address)
Checks for a group at the start of the string.
Definition: RFC822.php:310
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:405
$address
The address being parsed by the RFC822 object.
Definition: RFC822.php:76
$index
An internal counter/pointer.
Definition: RFC822.php:118
$num_groups
The number of groups that have been found in the address list.
Definition: RFC822.php:125
_validateRoute($route)
Function to validate a route, which is: route = 1#("@" domain) ":".
Definition: RFC822.php:777
$limit
A limit after which processing stops.
Definition: RFC822.php:138
_validateRouteAddr($route_addr)
This function validates a route-addr which is: route-addr = "<" [route] addr-spec ">".
Definition: RFC822.php:727
error($a_errmsg)
set error message @access public
Class ilMailException.
$valid