RFC822.php 31 KB

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