[BUGFIX] Fix the unit tests to work with PHPUnit 3.6
[Packages/TYPO3.CMS.git] / t3lib / mail / class.t3lib_mail_rfc822addressesparser.php
1 <?php
2 /**
3 * RFC 822 Email address list validation Utility
4 *
5 * PHP versions 4 and 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2001-2010, Richard Heyes
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * o Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * o Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * o The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * @category Mail
38 * @package Mail
39 * @author Richard Heyes <richard@phpguru.org>
40 * @author Chuck Hagenbuch <chuck@horde.org
41 * @copyright 2001-2010 Richard Heyes
42 * @license http://opensource.org/licenses/bsd-license.php New BSD License
43 * @version CVS: $Id: RFC822.php 294749 2010-02-08 08:22:25Z clockwerx $
44 * @link http://pear.php.net/package/Mail/
45 *
46 * Incorporated in TYPO3 by Ernesto Baschny <ernst@cron-it.de>
47 */
48
49 /**
50 * RFC 822 Email address list validation Utility
51 *
52 * What is it?
53 *
54 * This class will take an address string, and parse it into it's consituent
55 * parts, be that either addresses, groups, or combinations. Nested groups
56 * are not supported. The structure it returns is pretty straight forward,
57 * and is similar to that provided by the imap_rfc822_parse_adrlist(). Use
58 * print_r() to view the structure.
59 *
60 * How do I use it?
61 *
62 * $address_string = 'My Group: "Richard" <richard@localhost> (A comment), ted@example.com (Ted Bloggs), Barney;';
63 * $structure = Mail_RFC822::parseAddressList($address_string, 'example.com', true)
64 * print_r($structure);
65 *
66 * @author Richard Heyes <richard@phpguru.org>
67 * @author Chuck Hagenbuch <chuck@horde.org>
68 * @version $Revision: 294749 $
69 * @license BSD
70 * @package Mail
71 */
72 class t3lib_mail_Rfc822AddressesParser {
73
74 /**
75 * The address being parsed by the RFC822 object.
76 * @var string $address
77 */
78 private $address = '';
79
80 /**
81 * The default domain to use for unqualified addresses.
82 * @var string $default_domain
83 */
84 private $default_domain = 'localhost';
85
86 /**
87 /**
88 * Whether or not to validate atoms for non-ascii characters.
89 * @var boolean $validate
90 */
91 private $validate = TRUE;
92
93 /**
94 * The array of raw addresses built up as we parse.
95 * @var array $addresses
96 */
97 private $addresses = array();
98
99 /**
100 * The final array of parsed address information that we build up.
101 * @var array $structure
102 */
103 private $structure = array();
104
105 /**
106 * The current error message, if any.
107 * @var string $error
108 */
109 private $error = null;
110
111 /**
112 * An internal counter/pointer.
113 * @var integer $index
114 */
115 private $index = null;
116
117 /**
118 * The number of groups that have been found in the address list.
119 * @var integer $num_groups
120 * @access public
121 */
122 private $num_groups = 0;
123
124 /**
125 * A limit after which processing stops
126 * @var int $limit
127 */
128 private $limit = null;
129
130 /**
131 * Sets up the object.
132 *
133 * @access public
134 * @param string $address The address(es) to validate.
135 * @param string $default_domain Default domain/host etc. If not supplied, will be set to localhost.
136 * @param boolean $validate Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
137 */
138 public function __construct($address = null, $default_domain = null, $validate = null, $limit = null) {
139 if (isset($address)) $this->address = $address;
140 if (isset($default_domain)) $this->default_domain = $default_domain;
141 if (isset($validate)) $this->validate = $validate;
142 if (isset($limit)) $this->limit = $limit;
143 }
144
145 /**
146 * Starts the whole process. The address must either be set here
147 * or when creating the object. One or the other.
148 *
149 * @access public
150 * @param string $address The address(es) to validate.
151 * @param string $default_domain Default domain/host etc.
152 * @param boolean $nest_groups Whether to return the structure with groups nested for easier viewing.
153 * @param boolean $validate Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
154 *
155 * @return array A structured array of addresses.
156 */
157 public function parseAddressList($address = null, $default_domain = null, $validate = null, $limit = null) {
158 if (isset($address)) $this->address = $address;
159 if (isset($default_domain)) $this->default_domain = $default_domain;
160 if (isset($validate)) $this->validate = $validate;
161 if (isset($limit)) $this->limit = $limit;
162
163 $this->structure = array();
164 $this->addresses = array();
165 $this->error = null;
166 $this->index = null;
167
168 // Unfold any long lines in $this->address.
169 $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
170 $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
171
172 while ($this->address = $this->_splitAddresses($this->address));
173
174 if ($this->address === false || isset($this->error)) {
175 throw new InvalidArgumentException($this->error, 1294681466);
176 }
177
178 // Validate each address individually. If we encounter an invalid
179 // address, stop iterating and return an error immediately.
180 foreach ($this->addresses as $address) {
181 $valid = $this->_validateAddress($address);
182
183 if ($valid === false || isset($this->error)) {
184 throw new InvalidArgumentException($this->error, 1294681467);
185 }
186
187 $this->structure = array_merge($this->structure, $valid);
188 }
189
190 return $this->structure;
191 }
192
193 /**
194 * Splits an address into separate addresses.
195 *
196 * @access private
197 * @param string $address The addresses to split.
198 * @return boolean Success or failure.
199 */
200 protected function _splitAddresses($address) {
201 if (!empty($this->limit) && count($this->addresses) == $this->limit) {
202 return '';
203 }
204
205 if ($this->_isGroup($address) && !isset($this->error)) {
206 $split_char = ';';
207 $is_group = true;
208 } elseif (!isset($this->error)) {
209 $split_char = ',';
210 $is_group = false;
211 } elseif (isset($this->error)) {
212 return false;
213 }
214
215 // Split the string based on the above ten or so lines.
216 $parts = explode($split_char, $address);
217 $string = $this->_splitCheck($parts, $split_char);
218
219 // If a group...
220 if ($is_group) {
221 // If $string does not contain a colon outside of
222 // brackets/quotes etc then something's fubar.
223
224 // First check there's a colon at all:
225 if (strpos($string, ':') === false) {
226 $this->error = 'Invalid address: ' . $string;
227 return false;
228 }
229
230 // Now check it's outside of brackets/quotes:
231 if (!$this->_splitCheck(explode(':', $string), ':')) {
232 return false;
233 }
234
235 // We must have a group at this point, so increase the counter:
236 $this->num_groups++;
237 }
238
239 // $string now contains the first full address/group.
240 // Add to the addresses array.
241 $this->addresses[] = array(
242 'address' => trim($string),
243 'group' => $is_group
244 );
245
246 // Remove the now stored address from the initial line, the +1
247 // is to account for the explode character.
248 $address = trim(substr($address, strlen($string) + 1));
249
250 // If the next char is a comma and this was a group, then
251 // there are more addresses, otherwise, if there are any more
252 // chars, then there is another address.
253 if ($is_group && substr($address, 0, 1) == ',') {
254 $address = trim(substr($address, 1));
255 return $address;
256
257 } elseif (strlen($address) > 0) {
258 return $address;
259
260 } else {
261 return '';
262 }
263 }
264
265 /**
266 * Checks for a group at the start of the string.
267 *
268 * @access private
269 * @param string $address The address to check.
270 * @return boolean Whether or not there is a group at the start of the string.
271 */
272 protected function _isGroup($address) {
273 // First comma not in quotes, angles or escaped:
274 $parts = explode(',', $address);
275 $string = $this->_splitCheck($parts, ',');
276
277 // Now we have the first address, we can reliably check for a
278 // group by searching for a colon that's not escaped or in
279 // quotes or angle brackets.
280 if (count($parts = explode(':', $string)) > 1) {
281 $string2 = $this->_splitCheck($parts, ':');
282 return ($string2 !== $string);
283 } else {
284 return false;
285 }
286 }
287
288 /**
289 * A common function that will check an exploded string.
290 *
291 * @access private
292 * @param array $parts The exloded string.
293 * @param string $char The char that was exploded on.
294 * @return mixed False if the string contains unclosed quotes/brackets, or the string on success.
295 */
296 protected function _splitCheck($parts, $char) {
297 $string = $parts[0];
298
299 for ($i = 0; $i < count($parts); $i++) {
300 if ($this->_hasUnclosedQuotes($string)
301 || $this->_hasUnclosedBrackets($string, '<>')
302 || $this->_hasUnclosedBrackets($string, '[]')
303 || $this->_hasUnclosedBrackets($string, '()')
304 || substr($string, -1) == '\\') {
305 if (isset($parts[$i + 1])) {
306 $string = $string . $char . $parts[$i + 1];
307 } else {
308 $this->error = 'Invalid address spec. Unclosed bracket or quotes';
309 return false;
310 }
311 } else {
312 $this->index = $i;
313 break;
314 }
315 }
316
317 return $string;
318 }
319
320 /**
321 * Checks if a string has unclosed quotes or not.
322 *
323 * @access private
324 * @param string $string The string to check.
325 * @return boolean True if there are unclosed quotes inside the string,
326 * false otherwise.
327 */
328 protected function _hasUnclosedQuotes($string) {
329 $string = trim($string);
330 $iMax = strlen($string);
331 $in_quote = false;
332 $i = $slashes = 0;
333
334 for (; $i < $iMax; ++$i) {
335 switch ($string[$i]) {
336 case '\\':
337 ++$slashes;
338 break;
339
340 case '"':
341 if ($slashes % 2 == 0) {
342 $in_quote = !$in_quote;
343 }
344 // Fall through to default action below.
345
346 default:
347 $slashes = 0;
348 break;
349 }
350 }
351
352 return $in_quote;
353 }
354
355 /**
356 * Checks if a string has an unclosed brackets or not. IMPORTANT:
357 * This function handles both angle brackets and square brackets;
358 *
359 * @access private
360 * @param string $string The string to check.
361 * @param string $chars The characters to check for.
362 * @return boolean True if there are unclosed brackets inside the string, false otherwise.
363 */
364 protected function _hasUnclosedBrackets($string, $chars) {
365 $num_angle_start = substr_count($string, $chars[0]);
366 $num_angle_end = substr_count($string, $chars[1]);
367
368 $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
369 $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
370
371 if ($num_angle_start < $num_angle_end) {
372 $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
373 return false;
374 } else {
375 return ($num_angle_start > $num_angle_end);
376 }
377 }
378
379 /**
380 * Sub function that is used only by hasUnclosedBrackets().
381 *
382 * @access private
383 * @param string $string The string to check.
384 * @param integer &$num The number of occurences.
385 * @param string $char The character to count.
386 * @return integer The number of occurences of $char in $string, adjusted for backslashes.
387 */
388 protected function _hasUnclosedBracketsSub($string, &$num, $char) {
389 $parts = explode($char, $string);
390 for ($i = 0; $i < count($parts); $i++) {
391 if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
392 $num--;
393 if (isset($parts[$i + 1]))
394 $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
395 }
396
397 return $num;
398 }
399
400 /**
401 * Function to begin checking the address.
402 *
403 * @access private
404 * @param string $address The address to validate.
405 * @return mixed False on failure, or a structured array of address information on success.
406 */
407 protected function _validateAddress($address) {
408 $is_group = false;
409 $addresses = array();
410
411 if ($address['group']) {
412 $is_group = true;
413
414 // Get the group part of the name
415 $parts = explode(':', $address['address']);
416 $groupname = $this->_splitCheck($parts, ':');
417 $structure = array();
418
419 // And validate the group part of the name.
420 if (!$this->_validatePhrase($groupname)) {
421 $this->error = 'Group name did not validate.';
422 return false;
423 }
424
425 $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
426 }
427
428 // If a group then split on comma and put into an array.
429 // Otherwise, Just put the whole address in an array.
430 if ($is_group) {
431 while (strlen($address['address']) > 0) {
432 $parts = explode(',', $address['address']);
433 $addresses[] = $this->_splitCheck($parts, ',');
434 $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
435 }
436 } else {
437 $addresses[] = $address['address'];
438 }
439
440 // Check that $addresses is set, if address like this:
441 // Groupname:;
442 // Then errors were appearing.
443 if (!count($addresses)) {
444 $this->error = 'Empty group.';
445 return false;
446 }
447
448 // Trim the whitespace from all of the address strings.
449 array_map('trim', $addresses);
450
451 // Validate each mailbox.
452 // Format could be one of: name <geezer@domain.com>
453 // geezer@domain.com
454 // geezer
455 // ... or any other format valid by RFC 822.
456 for ($i = 0; $i < count($addresses); $i++) {
457 if (!$this->validateMailbox($addresses[$i])) {
458 if (empty($this->error)) {
459 $this->error = 'Validation failed for: ' . $addresses[$i];
460 }
461 return false;
462 }
463 }
464
465 if ($is_group) {
466 $structure = array_merge($structure, $addresses);
467 } else {
468 $structure = $addresses;
469 }
470
471 return $structure;
472 }
473
474 /**
475 * Function to validate a phrase.
476 *
477 * @access private
478 * @param string $phrase The phrase to check.
479 * @return boolean Success or failure.
480 */
481 protected function _validatePhrase($phrase) {
482 // Splits on one or more Tab or space.
483 $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
484
485 $phrase_parts = array();
486 while (count($parts) > 0) {
487 $phrase_parts[] = $this->_splitCheck($parts, ' ');
488 for ($i = 0; $i < $this->index + 1; $i++)
489 array_shift($parts);
490 }
491
492 foreach ($phrase_parts as $part) {
493 // If quoted string:
494 if (substr($part, 0, 1) == '"') {
495 if (!$this->_validateQuotedString($part)) {
496 return false;
497 }
498 continue;
499 }
500
501 // Otherwise it's an atom:
502 if (!$this->_validateAtom($part)) return false;
503 }
504
505 return true;
506 }
507
508 /**
509 * Function to validate an atom which from rfc822 is:
510 * atom = 1*<any CHAR except specials, SPACE and CTLs>
511 *
512 * If validation ($this->validate) has been turned off, then
513 * validateAtom() doesn't actually check anything. This is so that you
514 * can split a list of addresses up before encoding personal names
515 * (umlauts, etc.), for example.
516 *
517 * @access private
518 * @param string $atom The string to check.
519 * @return boolean Success or failure.
520 */
521 protected function _validateAtom($atom) {
522 if (!$this->validate) {
523 // Validation has been turned off; assume the atom is okay.
524 return true;
525 }
526
527 // Check for any char from ASCII 0 - ASCII 127
528 if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) {
529 return false;
530 }
531
532 // Check for specials:
533 if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
534 return false;
535 }
536
537 // Check for control characters (ASCII 0-31):
538 if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
539 return false;
540 }
541
542 return true;
543 }
544
545 /**
546 * Function to validate quoted string, which is:
547 * quoted-string = <"> *(qtext/quoted-pair) <">
548 *
549 * @access private
550 * @param string $qstring The string to check
551 * @return boolean Success or failure.
552 */
553 protected function _validateQuotedString($qstring) {
554 // Leading and trailing "
555 $qstring = substr($qstring, 1, -1);
556
557 // Perform check, removing quoted characters first.
558 return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
559 }
560
561 /**
562 * Function to validate a mailbox, which is:
563 * mailbox = addr-spec ; simple address
564 * / phrase route-addr ; name and route-addr
565 *
566 * @access public
567 * @param string &$mailbox The string to check.
568 * @return boolean Success or failure.
569 */
570 protected function validateMailbox(&$mailbox) {
571 // A couple of defaults.
572 $phrase = '';
573 $comment = '';
574 $comments = array();
575
576 // Catch any RFC822 comments and store them separately.
577 $_mailbox = $mailbox;
578 while (strlen(trim($_mailbox)) > 0) {
579 $parts = explode('(', $_mailbox);
580 $before_comment = $this->_splitCheck($parts, '(');
581 if ($before_comment != $_mailbox) {
582 // First char should be a (.
583 $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
584 $parts = explode(')', $comment);
585 $comment = $this->_splitCheck($parts, ')');
586 $comments[] = $comment;
587
588 // +2 is for the brackets
589 $_mailbox = substr($_mailbox, strpos($_mailbox, '(' . $comment) + strlen($comment) + 2);
590 } else {
591 break;
592 }
593 }
594
595 foreach ($comments as $comment) {
596 $mailbox = str_replace("($comment)", '', $mailbox);
597 }
598
599 $mailbox = trim($mailbox);
600
601 // Check for name + route-addr
602 if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
603 $parts = explode('<', $mailbox);
604 $name = $this->_splitCheck($parts, '<');
605
606 $phrase = trim($name);
607 $route_addr = trim(substr($mailbox, strlen($name . '<'), -1));
608
609 if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
610 return false;
611 }
612
613 // Only got addr-spec
614 } else {
615 // First snip angle brackets if present.
616 if (substr($mailbox, 0, 1) == '<' && substr($mailbox, -1) == '>') {
617 $addr_spec = substr($mailbox, 1, -1);
618 } else {
619 $addr_spec = $mailbox;
620 }
621
622 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
623 return false;
624 }
625 }
626
627 // Construct the object that will be returned.
628 $mbox = new stdClass();
629
630 // Add the phrase (even if empty) and comments
631 $mbox->personal = $phrase;
632 $mbox->comment = isset($comments) ? $comments : array();
633
634 if (isset($route_addr)) {
635 $mbox->mailbox = $route_addr['local_part'];
636 $mbox->host = $route_addr['domain'];
637 $route_addr['adl'] !== '' ? $mbox->adl = $route_addr['adl'] : '';
638 } else {
639 $mbox->mailbox = $addr_spec['local_part'];
640 $mbox->host = $addr_spec['domain'];
641 }
642
643 $mailbox = $mbox;
644 return true;
645 }
646
647 /**
648 * This function validates a route-addr which is:
649 * route-addr = "<" [route] addr-spec ">"
650 *
651 * Angle brackets have already been removed at the point of
652 * getting to this function.
653 *
654 * @access private
655 * @param string $route_addr The string to check.
656 * @return mixed False on failure, or an array containing validated address/route information on success.
657 */
658 protected function _validateRouteAddr($route_addr) {
659 // Check for colon.
660 if (strpos($route_addr, ':') !== false) {
661 $parts = explode(':', $route_addr);
662 $route = $this->_splitCheck($parts, ':');
663 } else {
664 $route = $route_addr;
665 }
666
667 // If $route is same as $route_addr then the colon was in
668 // quotes or brackets or, of course, non existent.
669 if ($route === $route_addr) {
670 unset($route);
671 $addr_spec = $route_addr;
672 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
673 return false;
674 }
675 } else {
676 // Validate route part.
677 if (($route = $this->_validateRoute($route)) === false) {
678 return false;
679 }
680
681 $addr_spec = substr($route_addr, strlen($route . ':'));
682
683 // Validate addr-spec part.
684 if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
685 return false;
686 }
687 }
688
689 if (isset($route)) {
690 $return['adl'] = $route;
691 } else {
692 $return['adl'] = '';
693 }
694
695 $return = array_merge($return, $addr_spec);
696 return $return;
697 }
698
699 /**
700 * Function to validate a route, which is:
701 * route = 1#("@" domain) ":"
702 *
703 * @access private
704 * @param string $route The string to check.
705 * @return mixed False on failure, or the validated $route on success.
706 */
707 protected function _validateRoute($route) {
708 // Split on comma.
709 $domains = explode(',', trim($route));
710
711 foreach ($domains as $domain) {
712 $domain = str_replace('@', '', trim($domain));
713 if (!$this->_validateDomain($domain)) return false;
714 }
715
716 return $route;
717 }
718
719 /**
720 * Function to validate a domain, though this is not quite what
721 * you expect of a strict internet domain.
722 *
723 * domain = sub-domain *("." sub-domain)
724 *
725 * @access private
726 * @param string $domain The string to check.
727 * @return mixed False on failure, or the validated domain on success.
728 */
729 protected function _validateDomain($domain) {
730 // Note the different use of $subdomains and $sub_domains
731 $subdomains = explode('.', $domain);
732
733 while (count($subdomains) > 0) {
734 $sub_domains[] = $this->_splitCheck($subdomains, '.');
735 for ($i = 0; $i < $this->index + 1; $i++)
736 array_shift($subdomains);
737 }
738
739 foreach ($sub_domains as $sub_domain) {
740 if (!$this->_validateSubdomain(trim($sub_domain)))
741 return false;
742 }
743
744 // Managed to get here, so return input.
745 return $domain;
746 }
747
748 /**
749 * Function to validate a subdomain:
750 * subdomain = domain-ref / domain-literal
751 *
752 * @access private
753 * @param string $subdomain The string to check.
754 * @return boolean Success or failure.
755 */
756 protected function _validateSubdomain($subdomain) {
757 if (preg_match('|^\[(.*)]$|', $subdomain, $arr)) {
758 if (!$this->_validateDliteral($arr[1])) return false;
759 } else {
760 if (!$this->_validateAtom($subdomain)) return false;
761 }
762
763 // Got here, so return successful.
764 return true;
765 }
766
767 /**
768 * Function to validate a domain literal:
769 * domain-literal = "[" *(dtext / quoted-pair) "]"
770 *
771 * @access private
772 * @param string $dliteral The string to check.
773 * @return boolean Success or failure.
774 */
775 protected function _validateDliteral($dliteral) {
776 return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
777 }
778
779 /**
780 * Function to validate an addr-spec.
781 *
782 * addr-spec = local-part "@" domain
783 *
784 * @access private
785 * @param string $addr_spec The string to check.
786 * @return mixed False on failure, or the validated addr-spec on success.
787 */
788 protected function _validateAddrSpec($addr_spec) {
789 $addr_spec = trim($addr_spec);
790
791 // Split on @ sign if there is one.
792 if (strpos($addr_spec, '@') !== false) {
793 $parts = explode('@', $addr_spec);
794 $local_part = $this->_splitCheck($parts, '@');
795 $domain = substr($addr_spec, strlen($local_part . '@'));
796
797 // No @ sign so assume the default domain.
798 } else {
799 $local_part = $addr_spec;
800 $domain = $this->default_domain;
801 }
802
803 if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
804 if (($domain = $this->_validateDomain($domain)) === false) return false;
805
806 // Got here so return successful.
807 return array('local_part' => $local_part, 'domain' => $domain);
808 }
809
810 /**
811 * Function to validate the local part of an address:
812 * local-part = word *("." word)
813 *
814 * @access private
815 * @param string $local_part
816 * @return mixed False on failure, or the validated local part on success.
817 */
818 protected function _validateLocalPart($local_part) {
819 $parts = explode('.', $local_part);
820 $words = array();
821
822 // Split the local_part into words.
823 while (count($parts) > 0) {
824 $words[] = $this->_splitCheck($parts, '.');
825 for ($i = 0; $i < $this->index + 1; $i++) {
826 array_shift($parts);
827 }
828 }
829
830 // Validate each word.
831 foreach ($words as $word) {
832 // If this word contains an unquoted space, it is invalid. (6.2.4)
833 if (strpos($word, ' ') && $word[0] !== '"') {
834 return false;
835 }
836
837 if ($this->_validatePhrase(trim($word)) === false) return false;
838 }
839
840 // Managed to get here, so return the input.
841 return $local_part;
842 }
843 }
844
845 ?>