RFC822.php 32 KB

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