RFC822.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. <?php
  2. /**
  3. * RFC 822 Email address list validation Utility
  4. *
  5. * PHP version 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$
  44. * @link http://pear.php.net/package/Mail/
  45. */
  46. /**
  47. * RFC 822 Email address list validation Utility
  48. *
  49. * What is it?
  50. *
  51. * This class will take an address string, and parse it into it's consituent
  52. * parts, be that either addresses, groups, or combinations. Nested groups
  53. * are not supported. The structure it returns is pretty straight forward,
  54. * and is similar to that provided by the imap_rfc822_parse_adrlist(). Use
  55. * print_r() to view the structure.
  56. *
  57. * How do I use it?
  58. *
  59. * $address_string = 'My Group: "Richard" <richard@localhost> (A comment), ted@example.com (Ted Bloggs), Barney;';
  60. * $structure = Mail_RFC822::parseAddressList($address_string, 'example.com', true)
  61. * print_r($structure);
  62. *
  63. * @author Richard Heyes <richard@phpguru.org>
  64. * @author Chuck Hagenbuch <chuck@horde.org>
  65. * @version $Revision$
  66. * @license BSD
  67. * @package Mail
  68. */
  69. class Mail_RFC822 {
  70. /**
  71. * The address being parsed by the RFC822 object.
  72. * @var string $address
  73. */
  74. var $address = '';
  75. /**
  76. * The default domain to use for unqualified addresses.
  77. * @var string $default_domain
  78. */
  79. var $default_domain = 'localhost';
  80. /**
  81. * Should we return a nested array showing groups, or flatten everything?
  82. * @var boolean $nestGroups
  83. */
  84. var $nestGroups = true;
  85. /**
  86. * Whether or not to validate atoms for non-ascii characters.
  87. * @var boolean $validate
  88. */
  89. var $validate = true;
  90. /**
  91. * The array of raw addresses built up as we parse.
  92. * @var array $addresses
  93. */
  94. var $addresses = array();
  95. /**
  96. * The final array of parsed address information that we build up.
  97. * @var array $structure
  98. */
  99. var $structure = array();
  100. /**
  101. * The current error message, if any.
  102. * @var string $error
  103. */
  104. var $error = null;
  105. /**
  106. * An internal counter/pointer.
  107. * @var integer $index
  108. */
  109. var $index = null;
  110. /**
  111. * The number of groups that have been found in the address list.
  112. * @var integer $num_groups
  113. * @access public
  114. */
  115. var $num_groups = 0;
  116. /**
  117. * A variable so that we can tell whether or not we're inside a
  118. * Mail_RFC822 object.
  119. * @var boolean $mailRFC822
  120. */
  121. var $mailRFC822 = true;
  122. /**
  123. * A limit after which processing stops
  124. * @var int $limit
  125. */
  126. var $limit = null;
  127. /**
  128. * Sets up the object. The address must either be set here or when
  129. * calling parseAddressList(). One or the other.
  130. *
  131. * @param string $address The address(es) to validate.
  132. * @param string $default_domain Default domain/host etc. If not supplied, will be set to localhost.
  133. * @param boolean $nest_groups Whether to return the structure with groups nested for easier viewing.
  134. * @param boolean $validate Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
  135. *
  136. * @return object Mail_RFC822 A new Mail_RFC822 object.
  137. */
  138. public function __construct($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
  139. {
  140. if (isset($address)) $this->address = $address;
  141. if (isset($default_domain)) $this->default_domain = $default_domain;
  142. if (isset($nest_groups)) $this->nestGroups = $nest_groups;
  143. if (isset($validate)) $this->validate = $validate;
  144. if (isset($limit)) $this->limit = $limit;
  145. }
  146. /**
  147. * Starts the whole process. The address must either be set here
  148. * or when creating the object. One or the other.
  149. *
  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, $nest_groups = null, $validate = null, $limit = null)
  158. {
  159. if (!isset($this) || !isset($this->mailRFC822)) {
  160. $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
  161. return $obj->parseAddressList();
  162. }
  163. if (isset($address)) $this->address = $address;
  164. if (isset($default_domain)) $this->default_domain = $default_domain;
  165. if (isset($nest_groups)) $this->nestGroups = $nest_groups;
  166. if (isset($validate)) $this->validate = $validate;
  167. if (isset($limit)) $this->limit = $limit;
  168. $this->structure = array();
  169. $this->addresses = array();
  170. $this->error = null;
  171. $this->index = null;
  172. // Unfold any long lines in $this->address.
  173. $this->address = preg_replace('/\r?\n/', "\r\n", $this->address);
  174. $this->address = preg_replace('/\r\n(\t| )+/', ' ', $this->address);
  175. while ($this->address = $this->_splitAddresses($this->address));
  176. if ($this->address === false || isset($this->error)) {
  177. require_once 'PEAR.php';
  178. return PEAR::raiseError($this->error);
  179. }
  180. // Validate each address individually. If we encounter an invalid
  181. // address, stop iterating and return an error immediately.
  182. foreach ($this->addresses as $address) {
  183. $valid = $this->_validateAddress($address);
  184. if ($valid === false || isset($this->error)) {
  185. require_once 'PEAR.php';
  186. return PEAR::raiseError($this->error);
  187. }
  188. if (!$this->nestGroups) {
  189. $this->structure = array_merge($this->structure, $valid);
  190. } else {
  191. $this->structure[] = $valid;
  192. }
  193. }
  194. return $this->structure;
  195. }
  196. /**
  197. * Splits an address into separate addresses.
  198. *
  199. * @param string $address The addresses to split.
  200. * @return boolean Success or failure.
  201. */
  202. protected function _splitAddresses($address)
  203. {
  204. if (!empty($this->limit) && count($this->addresses) == $this->limit) {
  205. return '';
  206. }
  207. if ($this->_isGroup($address) && !isset($this->error)) {
  208. $split_char = ';';
  209. $is_group = true;
  210. } elseif (!isset($this->error)) {
  211. $split_char = ',';
  212. $is_group = false;
  213. } elseif (isset($this->error)) {
  214. return false;
  215. }
  216. // Split the string based on the above ten or so lines.
  217. $parts = explode($split_char, $address);
  218. $string = $this->_splitCheck($parts, $split_char);
  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. // First check there's a colon at all:
  224. if (strpos($string, ':') === false) {
  225. $this->error = 'Invalid address: ' . $string;
  226. return false;
  227. }
  228. // Now check it's outside of brackets/quotes:
  229. if (!$this->_splitCheck(explode(':', $string), ':')) {
  230. return false;
  231. }
  232. // We must have a group at this point, so increase the counter:
  233. $this->num_groups++;
  234. }
  235. // $string now contains the first full address/group.
  236. // Add to the addresses array.
  237. $this->addresses[] = array(
  238. 'address' => trim($string),
  239. 'group' => $is_group
  240. );
  241. // Remove the now stored address from the initial line, the +1
  242. // is to account for the explode character.
  243. $address = trim(substr($address, strlen($string) + 1));
  244. // If the next char is a comma and this was a group, then
  245. // there are more addresses, otherwise, if there are any more
  246. // chars, then there is another address.
  247. if ($is_group && substr($address, 0, 1) == ','){
  248. $address = trim(substr($address, 1));
  249. return $address;
  250. } elseif (strlen($address) > 0) {
  251. return $address;
  252. } else {
  253. return '';
  254. }
  255. // If you got here then something's off
  256. return false;
  257. }
  258. /**
  259. * Checks for a group at the start of the string.
  260. *
  261. * @param string $address The address to check.
  262. * @return boolean Whether or not there is a group at the start of the string.
  263. */
  264. protected function _isGroup($address)
  265. {
  266. // First comma not in quotes, angles or escaped:
  267. $parts = explode(',', $address);
  268. $string = $this->_splitCheck($parts, ',');
  269. // Now we have the first address, we can reliably check for a
  270. // group by searching for a colon that's not escaped or in
  271. // quotes or angle brackets.
  272. if (count($parts = explode(':', $string)) > 1) {
  273. $string2 = $this->_splitCheck($parts, ':');
  274. return ($string2 !== $string);
  275. } else {
  276. return false;
  277. }
  278. }
  279. /**
  280. * A common function that will check an exploded string.
  281. *
  282. * @param array $parts The exloded string.
  283. * @param string $char The char that was exploded on.
  284. * @return mixed False if the string contains unclosed quotes/brackets, or the string on success.
  285. */
  286. protected function _splitCheck($parts, $char)
  287. {
  288. $string = $parts[0];
  289. for ($i = 0; $i < count($parts); $i++) {
  290. if ($this->_hasUnclosedQuotes($string)
  291. || $this->_hasUnclosedBrackets($string, '<>')
  292. || $this->_hasUnclosedBrackets($string, '[]')
  293. || $this->_hasUnclosedBrackets($string, '()')
  294. || substr($string, -1) == '\\') {
  295. if (isset($parts[$i + 1])) {
  296. $string = $string . $char . $parts[$i + 1];
  297. } else {
  298. $this->error = 'Invalid address spec. Unclosed bracket or quotes';
  299. return false;
  300. }
  301. } else {
  302. $this->index = $i;
  303. break;
  304. }
  305. }
  306. return $string;
  307. }
  308. /**
  309. * Checks if a string has unclosed quotes or not.
  310. *
  311. * @param string $string The string to check.
  312. * @return boolean True if there are unclosed quotes inside the string,
  313. * false otherwise.
  314. */
  315. protected function _hasUnclosedQuotes($string)
  316. {
  317. $string = trim($string);
  318. $iMax = strlen($string);
  319. $in_quote = false;
  320. $i = $slashes = 0;
  321. for (; $i < $iMax; ++$i) {
  322. switch ($string[$i]) {
  323. case '\\':
  324. ++$slashes;
  325. break;
  326. case '"':
  327. if ($slashes % 2 == 0) {
  328. $in_quote = !$in_quote;
  329. }
  330. // Fall through to default action below.
  331. default:
  332. $slashes = 0;
  333. break;
  334. }
  335. }
  336. return $in_quote;
  337. }
  338. /**
  339. * Checks if a string has an unclosed brackets or not. IMPORTANT:
  340. * This function handles both angle brackets and square brackets;
  341. *
  342. * @param string $string The string to check.
  343. * @param string $chars The characters to check for.
  344. * @return boolean True if there are unclosed brackets inside the string, false otherwise.
  345. */
  346. protected function _hasUnclosedBrackets($string, $chars)
  347. {
  348. $num_angle_start = substr_count($string, $chars[0]);
  349. $num_angle_end = substr_count($string, $chars[1]);
  350. $this->_hasUnclosedBracketsSub($string, $num_angle_start, $chars[0]);
  351. $this->_hasUnclosedBracketsSub($string, $num_angle_end, $chars[1]);
  352. if ($num_angle_start < $num_angle_end) {
  353. $this->error = 'Invalid address spec. Unmatched quote or bracket (' . $chars . ')';
  354. return false;
  355. } else {
  356. return ($num_angle_start > $num_angle_end);
  357. }
  358. }
  359. /**
  360. * Sub function that is used only by hasUnclosedBrackets().
  361. *
  362. * @param string $string The string to check.
  363. * @param integer &$num The number of occurences.
  364. * @param string $char The character to count.
  365. * @return integer The number of occurences of $char in $string, adjusted for backslashes.
  366. */
  367. protected function _hasUnclosedBracketsSub($string, &$num, $char)
  368. {
  369. $parts = explode($char, $string);
  370. for ($i = 0; $i < count($parts); $i++){
  371. if (substr($parts[$i], -1) == '\\' || $this->_hasUnclosedQuotes($parts[$i]))
  372. $num--;
  373. if (isset($parts[$i + 1]))
  374. $parts[$i + 1] = $parts[$i] . $char . $parts[$i + 1];
  375. }
  376. return $num;
  377. }
  378. /**
  379. * Function to begin checking the address.
  380. *
  381. * @param string $address The address to validate.
  382. * @return mixed False on failure, or a structured array of address information on success.
  383. */
  384. protected function _validateAddress($address)
  385. {
  386. $is_group = false;
  387. $addresses = array();
  388. if ($address['group']) {
  389. $is_group = true;
  390. // Get the group part of the name
  391. $parts = explode(':', $address['address']);
  392. $groupname = $this->_splitCheck($parts, ':');
  393. $structure = array();
  394. // And validate the group part of the name.
  395. if (!$this->_validatePhrase($groupname)){
  396. $this->error = 'Group name did not validate.';
  397. return false;
  398. } else {
  399. // Don't include groups if we are not nesting
  400. // them. This avoids returning invalid addresses.
  401. if ($this->nestGroups) {
  402. $structure = new stdClass;
  403. $structure->groupname = $groupname;
  404. }
  405. }
  406. $address['address'] = ltrim(substr($address['address'], strlen($groupname . ':')));
  407. }
  408. // If a group then split on comma and put into an array.
  409. // Otherwise, Just put the whole address in an array.
  410. if ($is_group) {
  411. while (strlen($address['address']) > 0) {
  412. $parts = explode(',', $address['address']);
  413. $addresses[] = $this->_splitCheck($parts, ',');
  414. $address['address'] = trim(substr($address['address'], strlen(end($addresses) . ',')));
  415. }
  416. } else {
  417. $addresses[] = $address['address'];
  418. }
  419. // Trim the whitespace from all of the address strings.
  420. array_map('trim', $addresses);
  421. // Validate each mailbox.
  422. // Format could be one of: name <geezer@domain.com>
  423. // geezer@domain.com
  424. // geezer
  425. // ... or any other format valid by RFC 822.
  426. for ($i = 0; $i < count($addresses); $i++) {
  427. if (!$this->validateMailbox($addresses[$i])) {
  428. if (empty($this->error)) {
  429. $this->error = 'Validation failed for: ' . $addresses[$i];
  430. }
  431. return false;
  432. }
  433. }
  434. // Nested format
  435. if ($this->nestGroups) {
  436. if ($is_group) {
  437. $structure->addresses = $addresses;
  438. } else {
  439. $structure = $addresses[0];
  440. }
  441. // Flat format
  442. } else {
  443. if ($is_group) {
  444. $structure = array_merge($structure, $addresses);
  445. } else {
  446. $structure = $addresses;
  447. }
  448. }
  449. return $structure;
  450. }
  451. /**
  452. * Function to validate a phrase.
  453. *
  454. * @param string $phrase The phrase to check.
  455. * @return boolean Success or failure.
  456. */
  457. protected function _validatePhrase($phrase)
  458. {
  459. // Splits on one or more Tab or space.
  460. $parts = preg_split('/[ \\x09]+/', $phrase, -1, PREG_SPLIT_NO_EMPTY);
  461. $phrase_parts = array();
  462. while (count($parts) > 0){
  463. $phrase_parts[] = $this->_splitCheck($parts, ' ');
  464. for ($i = 0; $i < $this->index + 1; $i++)
  465. array_shift($parts);
  466. }
  467. foreach ($phrase_parts as $part) {
  468. // If quoted string:
  469. if (substr($part, 0, 1) == '"') {
  470. if (!$this->_validateQuotedString($part)) {
  471. return false;
  472. }
  473. continue;
  474. }
  475. // Otherwise it's an atom:
  476. if (!$this->_validateAtom($part)) return false;
  477. }
  478. return true;
  479. }
  480. /**
  481. * Function to validate an atom which from rfc822 is:
  482. * atom = 1*<any CHAR except specials, SPACE and CTLs>
  483. *
  484. * If validation ($this->validate) has been turned off, then
  485. * validateAtom() doesn't actually check anything. This is so that you
  486. * can split a list of addresses up before encoding personal names
  487. * (umlauts, etc.), for example.
  488. *
  489. * @param string $atom The string to check.
  490. * @return boolean Success or failure.
  491. */
  492. protected function _validateAtom($atom)
  493. {
  494. if (!$this->validate) {
  495. // Validation has been turned off; assume the atom is okay.
  496. return true;
  497. }
  498. // Check for any char from ASCII 0 - ASCII 127
  499. if (!preg_match('/^[\\x00-\\x7E]+$/i', $atom, $matches)) {
  500. return false;
  501. }
  502. // Check for specials:
  503. if (preg_match('/[][()<>@,;\\:". ]/', $atom)) {
  504. return false;
  505. }
  506. // Check for control characters (ASCII 0-31):
  507. if (preg_match('/[\\x00-\\x1F]+/', $atom)) {
  508. return false;
  509. }
  510. return true;
  511. }
  512. /**
  513. * Function to validate quoted string, which is:
  514. * quoted-string = <"> *(qtext/quoted-pair) <">
  515. *
  516. * @param string $qstring The string to check
  517. * @return boolean Success or failure.
  518. */
  519. protected function _validateQuotedString($qstring)
  520. {
  521. // Leading and trailing "
  522. $qstring = substr($qstring, 1, -1);
  523. // Perform check, removing quoted characters first.
  524. return !preg_match('/[\x0D\\\\"]/', preg_replace('/\\\\./', '', $qstring));
  525. }
  526. /**
  527. * Function to validate a mailbox, which is:
  528. * mailbox = addr-spec ; simple address
  529. * / phrase route-addr ; name and route-addr
  530. *
  531. * @param string &$mailbox The string to check.
  532. * @return boolean Success or failure.
  533. */
  534. public function validateMailbox(&$mailbox)
  535. {
  536. // A couple of defaults.
  537. $phrase = '';
  538. $comment = '';
  539. $comments = array();
  540. // Catch any RFC822 comments and store them separately.
  541. $_mailbox = $mailbox;
  542. while (strlen(trim($_mailbox)) > 0) {
  543. $parts = explode('(', $_mailbox);
  544. $before_comment = $this->_splitCheck($parts, '(');
  545. if ($before_comment != $_mailbox) {
  546. // First char should be a (.
  547. $comment = substr(str_replace($before_comment, '', $_mailbox), 1);
  548. $parts = explode(')', $comment);
  549. $comment = $this->_splitCheck($parts, ')');
  550. $comments[] = $comment;
  551. // +2 is for the brackets
  552. $_mailbox = substr($_mailbox, strpos($_mailbox, '('.$comment)+strlen($comment)+2);
  553. } else {
  554. break;
  555. }
  556. }
  557. foreach ($comments as $comment) {
  558. $mailbox = str_replace("($comment)", '', $mailbox);
  559. }
  560. $mailbox = trim($mailbox);
  561. // Check for name + route-addr
  562. if (substr($mailbox, -1) == '>' && substr($mailbox, 0, 1) != '<') {
  563. $parts = explode('<', $mailbox);
  564. $name = $this->_splitCheck($parts, '<');
  565. $phrase = trim($name);
  566. $route_addr = trim(substr($mailbox, strlen($name.'<'), -1));
  567. if ($this->_validatePhrase($phrase) === false || ($route_addr = $this->_validateRouteAddr($route_addr)) === false) {
  568. return false;
  569. }
  570. // Only got addr-spec
  571. } else {
  572. // First snip angle brackets if present.
  573. if (substr($mailbox, 0, 1) == '<' && substr($mailbox, -1) == '>') {
  574. $addr_spec = substr($mailbox, 1, -1);
  575. } else {
  576. $addr_spec = $mailbox;
  577. }
  578. if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
  579. return false;
  580. }
  581. }
  582. // Construct the object that will be returned.
  583. $mbox = new stdClass();
  584. // Add the phrase (even if empty) and comments
  585. $mbox->personal = $phrase;
  586. $mbox->comment = isset($comments) ? $comments : array();
  587. if (isset($route_addr)) {
  588. $mbox->mailbox = $route_addr['local_part'];
  589. $mbox->host = $route_addr['domain'];
  590. $route_addr['adl'] !== '' ? $mbox->adl = $route_addr['adl'] : '';
  591. } else {
  592. $mbox->mailbox = $addr_spec['local_part'];
  593. $mbox->host = $addr_spec['domain'];
  594. }
  595. $mailbox = $mbox;
  596. return true;
  597. }
  598. /**
  599. * This function validates a route-addr which is:
  600. * route-addr = "<" [route] addr-spec ">"
  601. *
  602. * Angle brackets have already been removed at the point of
  603. * getting to this function.
  604. *
  605. * @param string $route_addr The string to check.
  606. * @return mixed False on failure, or an array containing validated address/route information on success.
  607. */
  608. protected function _validateRouteAddr($route_addr)
  609. {
  610. // Check for colon.
  611. if (strpos($route_addr, ':') !== false) {
  612. $parts = explode(':', $route_addr);
  613. $route = $this->_splitCheck($parts, ':');
  614. } else {
  615. $route = $route_addr;
  616. }
  617. // If $route is same as $route_addr then the colon was in
  618. // quotes or brackets or, of course, non existent.
  619. if ($route === $route_addr){
  620. unset($route);
  621. $addr_spec = $route_addr;
  622. if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
  623. return false;
  624. }
  625. } else {
  626. // Validate route part.
  627. if (($route = $this->_validateRoute($route)) === false) {
  628. return false;
  629. }
  630. $addr_spec = substr($route_addr, strlen($route . ':'));
  631. // Validate addr-spec part.
  632. if (($addr_spec = $this->_validateAddrSpec($addr_spec)) === false) {
  633. return false;
  634. }
  635. }
  636. if (isset($route)) {
  637. $return['adl'] = $route;
  638. } else {
  639. $return['adl'] = '';
  640. }
  641. $return = array_merge($return, $addr_spec);
  642. return $return;
  643. }
  644. /**
  645. * Function to validate a route, which is:
  646. * route = 1#("@" domain) ":"
  647. *
  648. * @param string $route The string to check.
  649. * @return mixed False on failure, or the validated $route on success.
  650. */
  651. protected function _validateRoute($route)
  652. {
  653. // Split on comma.
  654. $domains = explode(',', trim($route));
  655. foreach ($domains as $domain) {
  656. $domain = str_replace('@', '', trim($domain));
  657. if (!$this->_validateDomain($domain)) return false;
  658. }
  659. return $route;
  660. }
  661. /**
  662. * Function to validate a domain, though this is not quite what
  663. * you expect of a strict internet domain.
  664. *
  665. * domain = sub-domain *("." sub-domain)
  666. *
  667. * @param string $domain The string to check.
  668. * @return mixed False on failure, or the validated domain on success.
  669. */
  670. protected function _validateDomain($domain)
  671. {
  672. // Note the different use of $subdomains and $sub_domains
  673. $subdomains = explode('.', $domain);
  674. while (count($subdomains) > 0) {
  675. $sub_domains[] = $this->_splitCheck($subdomains, '.');
  676. for ($i = 0; $i < $this->index + 1; $i++)
  677. array_shift($subdomains);
  678. }
  679. foreach ($sub_domains as $sub_domain) {
  680. if (!$this->_validateSubdomain(trim($sub_domain)))
  681. return false;
  682. }
  683. // Managed to get here, so return input.
  684. return $domain;
  685. }
  686. /**
  687. * Function to validate a subdomain:
  688. * subdomain = domain-ref / domain-literal
  689. *
  690. * @param string $subdomain The string to check.
  691. * @return boolean Success or failure.
  692. */
  693. protected function _validateSubdomain($subdomain)
  694. {
  695. if (preg_match('|^\[(.*)]$|', $subdomain, $arr)){
  696. if (!$this->_validateDliteral($arr[1])) return false;
  697. } else {
  698. if (!$this->_validateAtom($subdomain)) return false;
  699. }
  700. // Got here, so return successful.
  701. return true;
  702. }
  703. /**
  704. * Function to validate a domain literal:
  705. * domain-literal = "[" *(dtext / quoted-pair) "]"
  706. *
  707. * @param string $dliteral The string to check.
  708. * @return boolean Success or failure.
  709. */
  710. protected function _validateDliteral($dliteral)
  711. {
  712. return !preg_match('/(.)[][\x0D\\\\]/', $dliteral, $matches) && $matches[1] != '\\';
  713. }
  714. /**
  715. * Function to validate an addr-spec.
  716. *
  717. * addr-spec = local-part "@" domain
  718. *
  719. * @param string $addr_spec The string to check.
  720. * @return mixed False on failure, or the validated addr-spec on success.
  721. */
  722. protected function _validateAddrSpec($addr_spec)
  723. {
  724. $addr_spec = trim($addr_spec);
  725. // Split on @ sign if there is one.
  726. if (strpos($addr_spec, '@') !== false) {
  727. $parts = explode('@', $addr_spec);
  728. $local_part = $this->_splitCheck($parts, '@');
  729. $domain = substr($addr_spec, strlen($local_part . '@'));
  730. // No @ sign so assume the default domain.
  731. } else {
  732. $local_part = $addr_spec;
  733. $domain = $this->default_domain;
  734. }
  735. if (($local_part = $this->_validateLocalPart($local_part)) === false) return false;
  736. if (($domain = $this->_validateDomain($domain)) === false) return false;
  737. // Got here so return successful.
  738. return array('local_part' => $local_part, 'domain' => $domain);
  739. }
  740. /**
  741. * Function to validate the local part of an address:
  742. * local-part = word *("." word)
  743. *
  744. * @param string $local_part
  745. * @return mixed False on failure, or the validated local part on success.
  746. */
  747. protected function _validateLocalPart($local_part)
  748. {
  749. $parts = explode('.', $local_part);
  750. $words = array();
  751. // Split the local_part into words.
  752. while (count($parts) > 0) {
  753. $words[] = $this->_splitCheck($parts, '.');
  754. for ($i = 0; $i < $this->index + 1; $i++) {
  755. array_shift($parts);
  756. }
  757. }
  758. // Validate each word.
  759. foreach ($words as $word) {
  760. // word cannot be empty (#17317)
  761. if ($word === '') {
  762. return false;
  763. }
  764. // If this word contains an unquoted space, it is invalid. (6.2.4)
  765. if (strpos($word, ' ') && $word[0] !== '"')
  766. {
  767. return false;
  768. }
  769. if ($this->_validatePhrase(trim($word)) === false) return false;
  770. }
  771. // Managed to get here, so return the input.
  772. return $local_part;
  773. }
  774. /**
  775. * Returns an approximate count of how many addresses are in the
  776. * given string. This is APPROXIMATE as it only splits based on a
  777. * comma which has no preceding backslash. Could be useful as
  778. * large amounts of addresses will end up producing *large*
  779. * structures when used with parseAddressList().
  780. *
  781. * @param string $data Addresses to count
  782. * @return int Approximate count
  783. */
  784. public function approximateCount($data)
  785. {
  786. return count(preg_split('/(?<!\\\\),/', $data));
  787. }
  788. /**
  789. * This is a email validating function separate to the rest of the
  790. * class. It simply validates whether an email is of the common
  791. * internet form: <user>@<domain>. This can be sufficient for most
  792. * people. Optional stricter mode can be utilised which restricts
  793. * mailbox characters allowed to alphanumeric, full stop, hyphen
  794. * and underscore.
  795. *
  796. * @param string $data Address to check
  797. * @param boolean $strict Optional stricter mode
  798. * @return mixed False if it fails, an indexed array
  799. * username/domain if it matches
  800. */
  801. public function isValidInetAddress($data, $strict = false)
  802. {
  803. $regex = $strict ? '/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i' : '/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i';
  804. if (preg_match($regex, trim($data), $matches)) {
  805. return array($matches[1], $matches[2]);
  806. } else {
  807. return false;
  808. }
  809. }
  810. }