ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 function Mail_RFC822($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
153 {
154 if (isset($address)) $this->address = $address;
155 if (isset($default_domain)) $this->default_domain = $default_domain;
156 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
157 if (isset($validate)) $this->validate = $validate;
158 if (isset($limit)) $this->limit = $limit;
159 }
160
173 function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
174 {
175 if (!isset($this) || !isset($this->mailRFC822)) {
176 $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
177 return $obj->parseAddressList();
178 }
179
180 if (isset($address)) $this->address = $address;
181 if (isset($default_domain)) $this->default_domain = $default_domain;
182 if (isset($nest_groups)) $this->nestGroups = $nest_groups;
183 if (isset($validate)) $this->validate = $validate;
184 if (isset($limit)) $this->limit = $limit;
185
186 $this->structure = array();
187 $this->addresses = array();
188 $this->error = null;
189 $this->index = null;
190
191 // Unfold any long lines in $this->address.
192 $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
193 $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
194
195 while ($this->address = $this->_splitAddresses($this->address));
196
197 if ($this->address === false || isset($this->error)) {
198 require_once 'PEAR.php';
199 return PEAR::raiseError($this->error);
200 }
201
202 // Validate each address individually. If we encounter an invalid
203 // address, stop iterating and return an error immediately.
204 foreach ($this->addresses as $address) {
205 $valid = $this->_validateAddress($address);
206
207 if ($valid === false || isset($this->error)) {
208 require_once 'PEAR.php';
209 return PEAR::raiseError($this->error);
210 }
211
212 if (!$this->nestGroups) {
213 $this->structure = array_merge($this->structure, $valid);
214 } else {
215 $this->structure[] = $valid;
216 }
217 }
218
219 return $this->structure;
220 }
221
230 {
231 if (!empty($this->limit) && count($this->addresses) == $this->limit) {
232 return '';
233 }
234
235 if ($this->_isGroup($address) && !isset($this->error)) {
236 $split_char = ';';
237 $is_group = true;
238 } elseif (!isset($this->error)) {
239 $split_char = ',';
240 $is_group = false;
241 } elseif (isset($this->error)) {
242 return false;
243 }
244
245 // Split the string based on the above ten or so lines.
246 $parts = explode($split_char, $address);
247 $string = $this->_splitCheck($parts, $split_char);
248
249 // If a group...
250 if ($is_group) {
251 // If $string does not contain a colon outside of
252 // brackets/quotes etc then something's fubar.
253
254 // First check there's a colon at all:
255 if (strpos($string, ':') === false) {
256 $this->error = 'Invalid address: ' . $string;
257 return false;
258 }
259
260 // Now check it's outside of brackets/quotes:
261 if (!$this->_splitCheck(explode(':', $string), ':')) {
262 return false;
263 }
264
265 // We must have a group at this point, so increase the counter:
266 $this->num_groups++;
267 }
268
269 // $string now contains the first full address/group.
270 // Add to the addresses array.
271 $this->addresses[] = array(
272 'address' => trim($string),
273 'group' => $is_group
274 );
275
276 // Remove the now stored address from the initial line, the +1
277 // is to account for the explode character.
278 $address = trim(substr($address, strlen($string) + 1));
279
280 // If the next char is a comma and this was a group, then
281 // there are more addresses, otherwise, if there are any more
282 // chars, then there is another address.
283 if ($is_group && substr($address, 0, 1) == ','){
284 $address = trim(substr($address, 1));
285 return $address;
286
287 } elseif (strlen($address) > 0) {
288 return $address;
289
290 } else {
291 return '';
292 }
293
294 // If you got here then something's off
295 return false;
296 }
297
306 {
307 // First comma not in quotes, angles or escaped:
308 $parts = explode(',', $address);
309 $string = $this->_splitCheck($parts, ',');
310
311 // Now we have the first address, we can reliably check for a
312 // group by searching for a colon that's not escaped or in
313 // quotes or angle brackets.
314 if (count($parts = explode(':', $string)) > 1) {
315 $string2 = $this->_splitCheck($parts, ':');
316 return ($string2 !== $string);
317 } else {
318 return false;
319 }
320 }
321
330 function _splitCheck($parts, $char)
331 {
332 $string = $parts[0];
333
334 for ($i = 0; $i < count($parts); $i++) {
335 if ($this->_hasUnclosedQuotes($string)
336 || $this->_hasUnclosedBrackets($string, '<>')
337 || $this->_hasUnclosedBrackets($string, '[]')
338 || $this->_hasUnclosedBrackets($string, '()')
339 || substr($string, -1) == '\\') {
340 if (isset($parts[$i + 1])) {
341 $string = $string . $char . $parts[$i + 1];
342 } else {
343 $this->error = 'Invalid address spec. Unclosed bracket or quotes';
344 return false;
345 }
346 } else {
347 $this->index = $i;
348 break;
349 }
350 }
351
352 return $string;
353 }
354
363 function _hasUnclosedQuotes($string)
364 {
365 $string = trim($string);
366 $iMax = strlen($string);
367 $in_quote = false;
368 $i = $slashes = 0;
369
370 for (; $i < $iMax; ++$i) {
371 switch ($string[$i]) {
372 case '\\':
373 ++$slashes;
374 break;
375
376 case '"':
377 if ($slashes % 2 == 0) {
378 $in_quote = !$in_quote;
379 }
380 // Fall through to default action below.
381
382 default:
383 $slashes = 0;
384 break;
385 }
386 }
387
388 return $in_quote;
389 }
390
400 function _hasUnclosedBrackets($string, $chars)
401 {
402 $num_angle_start = substr_count($string, $chars[0]);
403 $num_angle_end = substr_count($string, $chars[1]);
404
405 $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
406 $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
407
408 if ($num_angle_start < $num_angle_end) {
409 $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
410 return false;
411 } else {
412 return ($num_angle_start > $num_angle_end);
413 }
414 }
415
425 function _hasUnclosedBracketsSub($string, &$num, $char)
426 {
427 $parts = explode($char, $string);
428 for ($i = 0; $i < count($parts); $i++){
429 if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
430 $num--;
431 if (isset($parts[$i + 1]))
432 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
433 }
434
435 return $num;
436 }
437
446 {
447 $is_group = false;
448 $addresses = array();
449
450 if ($address['group']) {
451 $is_group = true;
452
453 // Get the group part of the name
454 $parts = explode(':', $address['address']);
455 $groupname = $this->_splitCheck($parts, ':');
456 $structure = array();
457
458 // And validate the group part of the name.
459 if (!$this->_validatePhrase($groupname)){
460 $this->error = 'Group name did not validate.';
461 return false;
462 } else {
463 // Don't include groups if we are not nesting
464 // them. This avoids returning invalid addresses.
465 if ($this->nestGroups) {
466 $structure = new stdClass;
467 $structure->groupname = $groupname;
468 }
469 }
470
471 $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
472 }
473
474 // If a group then split on comma and put into an array.
475 // Otherwise, Just put the whole address in an array.
476 if ($is_group) {
477 while (strlen($address['address']) > 0) {
478 $parts = explode(',', $address['address']);
479 $addresses[] = $this->_splitCheck($parts, ',');
480 $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
481 }
482 } else {
483 $addresses[] = $address['address'];
484 }
485
486 // Check that $addresses is set, if address like this:
487 // Groupname:;
488 // Then errors were appearing.
489 if (!count($addresses)){
490 $this->error = 'Empty group.';
491 return false;
492 }
493
494 // Trim the whitespace from all of the address strings.
495 array_map('trim', $addresses);
496
497 // Validate each mailbox.
498 // Format could be one of: name <geezer@domain.com>
499 // geezer@domain.com
500 // geezer
501 // ... or any other format valid by RFC 822.
502 for ($i = 0; $i < count($addresses); $i++) {
503 if (!$this->validateMailbox($addresses[$i])) {
504 if (empty($this->error)) {
505 $this->error = 'Validation failed for: ' . $addresses[$i];
506 }
507 return false;
508 }
509 }
510
511 // Nested format
512 if ($this->nestGroups) {
513 if ($is_group) {
514 $structure->addresses = $addresses;
515 } else {
517 }
518
519 // Flat format
520 } else {
521 if ($is_group) {
522 $structure = array_merge($structure, $addresses);
523 } else {
525 }
526 }
527
528 return $structure;
529 }
530
538 function _validatePhrase($phrase)
539 {
540 // Splits on one or more Tab or space.
541 $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
542
543 $phrase_parts = array();
544 while (count($parts) > 0){
545 $phrase_parts[] = $this->_splitCheck($parts, ' ');
546 for ($i = 0; $i < $this->index + 1; $i++)
547 array_shift($parts);
548 }
549
550 foreach ($phrase_parts as $part) {
551 // If quoted string:
552 if (substr($part, 0, 1) == '"') {
553 if (!$this->_validateQuotedString($part)) {
554 return false;
555 }
556 continue;
557 }
558
559 // Otherwise it's an atom:
560 if (!$this->_validateAtom($part)) return false;
561 }
562
563 return true;
564 }
565
579 function _validateAtom($atom)
580 {
581 if (!$this->validate) {
582 // Validation has been turned off; assume the atom is okay.
583 return true;
584 }
585
586 // Check for any char from ASCII 0 - ASCII 127
587 // mjansen patch 16 Sep 2015 start
588 // Check for specials:
589 if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
590 return false;
591 }
592
593 // Check for control characters (ASCII 0-31):
594 if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
595 return false;
596 }
597 #16291
598 #17618
599 if (!(bool)preg_match('//u', $atom)) {
600 return false;
601 }
602 // mjansen patch 16 Sep 2015 end
603
604 return true;
605 }
606
615 function _validateQuotedString($qstring)
616 {
617 // Leading and trailing "
618 $qstring = substr($qstring, 1, -1);
619
620 // Perform check, removing quoted characters first.
621 return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
622 }
623
633 function validateMailbox(&$mailbox)
634 {
635 // A couple of defaults.
636 $phrase = '';
637 $comment = '';
638 $comments = array();
639
640 // Catch any RFC822 comments and store them separately.
641 $_mailbox = $mailbox;
642 while (strlen(trim($_mailbox)) > 0) {
643 $parts = explode('(', $_mailbox);
644 $before_comment = $this->_splitCheck($parts, '(');
645 if ($before_comment != $_mailbox) {
646 // First char should be a (.
647 $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
648 $parts = explode(')', $comment);
649 $comment = $this->_splitCheck($parts, ')');
650 $comments[] = $comment;
651
652 // +2 is for the brackets
653 $_mailbox = substr($_mailbox, strpos($_mailbox, '('.$comment)+strlen($comment)+2);
654 } else {
655 break;
656 }
657 }
658
659 foreach ($comments as $comment) {
660 $mailbox = str_replace("($comment)", '', $mailbox);
661 }
662
663 $mailbox = trim($mailbox);
664
665 // Check for name + route-addr
666 if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
667 $parts = explode('<', $mailbox);
668 $name = $this->_splitCheck($parts, '<');
669
670 $phrase = trim($name);
671 $route_addr = trim(substr($mailbox, strlen($name.'<'), -1));
672
673 if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
674 return false;
675 }
676
677 // Only got addr-spec
678 } else {
679 // First snip angle brackets if present.
680 if (substr($mailbox, 0, 1) == '<' && substr($mailbox, -1) == '>') {
681 $addr_spec = substr($mailbox, 1, -1);
682 } else {
683 $addr_spec = $mailbox;
684 }
685
686 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
687 return false;
688 }
689 }
690
691 // Construct the object that will be returned.
692 $mbox = new stdClass();
693
694 // Add the phrase (even if empty) and comments
695 $mbox->personal = $phrase;
696 $mbox->comment = isset($comments) ? $comments : array();
697
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'] : '';
702 } else {
703 $mbox->mailbox = $addr_spec['local_part'];
704 $mbox->host = $addr_spec['domain'];
705 }
706
707 $mailbox = $mbox;
708 return true;
709 }
710
722 function _validateRouteAddr($route_addr)
723 {
724 // Check for colon.
725 if (strpos($route_addr, ':') !== false) {
726 $parts = explode(':', $route_addr);
727 $route = $this->_splitCheck($parts, ':');
728 } else {
729 $route = $route_addr;
730 }
731
732 // If $route is same as $route_addr then the colon was in
733 // quotes or brackets or, of course, non existent.
734 if ($route === $route_addr){
735 unset($route);
736 $addr_spec = $route_addr;
737 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
738 return false;
739 }
740 } else {
741 // Validate route part.
742 if (($route = $this->_validateRoute($route)) === false) {
743 return false;
744 }
745
746 $addr_spec = substr($route_addr, strlen($route . ':'));
747
748 // Validate addr-spec part.
749 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
750 return false;
751 }
752 }
753
754 if (isset($route)) {
755 $return['adl'] = $route;
756 } else {
757 $return['adl'] = '';
758 }
759
760 $return = array_merge($return, $addr_spec);
761 return $return;
762 }
763
772 function _validateRoute($route)
773 {
774 // Split on comma.
775 $domains = explode(',', trim($route));
776
777 foreach ($domains as $domain) {
778 $domain = str_replace('@', '', trim($domain));
779 if (!$this->_validateDomain($domain)) return false;
780 }
781
782 return $route;
783 }
784
795 function _validateDomain($domain)
796 {
797 // Note the different use of $subdomains and $sub_domains
798 $subdomains = explode('.', $domain);
799
800 while (count($subdomains) > 0) {
801 $sub_domains[] = $this->_splitCheck($subdomains, '.');
802 for ($i = 0; $i < $this->index + 1; $i++)
803 array_shift($subdomains);
804 }
805
806 foreach ($sub_domains as $sub_domain) {
807 if (!$this->_validateSubdomain(trim($sub_domain)))
808 return false;
809 }
810
811 // Managed to get here, so return input.
812 return $domain;
813 }
814
823 function _validateSubdomain($subdomain)
824 {
825 if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
826 if (!$this->_validateDliteral($arr[1])) return false;
827 } else {
828 if (!$this->_validateAtom($subdomain)) return false;
829 }
830
831 // Got here, so return successful.
832 return true;
833 }
834
843 function _validateDliteral($dliteral)
844 {
845 return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
846 }
847
857 function _validateAddrSpec($addr_spec)
858 {
859 $addr_spec = trim($addr_spec);
860
861 // Split on @ sign if there is one.
862 if (strpos($addr_spec, '@') !== false) {
863 $parts = explode('@', $addr_spec);
864 $local_part = $this->_splitCheck($parts, '@');
865 $domain = substr($addr_spec, strlen($local_part . '@'));
866
867 // No @ sign so assume the default domain.
868 } else {
869 $local_part = $addr_spec;
870 $domain = $this->default_domain;
871 }
872
873 if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
874 if (($domain = $this->_validateDomain($domain)) === false) return false;
875
876 // Got here so return successful.
877 return array('local_part' => $local_part, 'domain' => $domain);
878 }
879
888 function _validateLocalPart($local_part)
889 {
890 $parts = explode('.', $local_part);
891 $words = array();
892
893 // Split the local_part into words.
894 while (count($parts) > 0){
895 $words[] = $this->_splitCheck($parts, '.');
896 for ($i = 0; $i < $this->index + 1; $i++) {
897 array_shift($parts);
898 }
899 }
900
901 // Validate each word.
902 foreach ($words as $word) {
903 // If this word contains an unquoted space, it is invalid. (6.2.4)
904 if (strpos($word, ' ') && $word[0] !== '"')
905 {
906 // mjansen patch 24 Feb 2016 start
907 // Mantis issue #18018
908 // # http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
909 //return false;
910 // mjansen patch 24 Feb 2016 end
911 }
912
913 if ($this->_validatePhrase(trim($word)) === false) return false;
914 }
915
916 // Managed to get here, so return the input.
917 return $local_part;
918 }
919
931 {
932 return count(preg_split('/(?<!\\\\),/', $data));
933 }
934
948 function isValidInetAddress($data, $strict = false)
949 {
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]);
953 } else {
954 return false;
955 }
956 }
957
958}
$comment
Definition: buildRTE.php:83
$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:823
_splitCheck($parts, $char)
A common function that will check an exploded string.
Definition: RFC822.php:330
isValidInetAddress($data, $strict=false)
This is a email validating function separate to the rest of the class.
Definition: RFC822.php:948
_splitAddresses($address)
Splits an address into separate addresses.
Definition: RFC822.php:229
$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:538
_validateQuotedString($qstring)
Function to validate quoted string, which is: quoted-string = <"> *(qtext/quoted-pair) <">
Definition: RFC822.php:615
_validateDliteral($dliteral)
Function to validate a domain literal: domain-literal = "[" *(dtext / quoted-pair) "]".
Definition: RFC822.php:843
_validateLocalPart($local_part)
Function to validate the local part of an address: local-part = word *("." word)
Definition: RFC822.php:888
parseAddressList($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Starts the whole process.
Definition: RFC822.php:173
$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:425
_validateAtom($atom)
Function to validate an atom which from rfc822 is: atom = 1*<any CHAR except specials,...
Definition: RFC822.php:579
_validateDomain($domain)
Function to validate a domain, though this is not quite what you expect of a strict internet domain.
Definition: RFC822.php:795
approximateCount($data)
Returns an approximate count of how many addresses are in the given string.
Definition: RFC822.php:930
validateMailbox(&$mailbox)
Function to validate a mailbox, which is: mailbox = addr-spec ; simple address / phrase route-addr ; ...
Definition: RFC822.php:633
_hasUnclosedQuotes($string)
Checks if a string has unclosed quotes or not.
Definition: RFC822.php:363
Mail_RFC822($address=null, $default_domain=null, $nest_groups=null, $validate=null, $limit=null)
Sets up the object.
Definition: RFC822.php:152
$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:857
_validateAddress($address)
Function to begin checking the address.
Definition: RFC822.php:445
_isGroup($address)
Checks for a group at the start of the string.
Definition: RFC822.php:305
_hasUnclosedBrackets($string, $chars)
Checks if a string has an unclosed brackets or not.
Definition: RFC822.php:400
$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:772
$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:722
& 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...
Definition: PEAR.php:524
error($a_errmsg)
set error message @access public
$valid
$data