EmailAddress.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @see Zend_Validate_Hostname
  27. */
  28. require_once 'Zend/Validate/Hostname.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Validate_EmailAddress extends Zend_Validate_Abstract
  36. {
  37. const INVALID = 'emailAddressInvalid';
  38. const INVALID_HOSTNAME = 'emailAddressInvalidHostname';
  39. const INVALID_MX_RECORD = 'emailAddressInvalidMxRecord';
  40. const DOT_ATOM = 'emailAddressDotAtom';
  41. const QUOTED_STRING = 'emailAddressQuotedString';
  42. const INVALID_LOCAL_PART = 'emailAddressInvalidLocalPart';
  43. const LENGTH_EXCEEDED = 'emailAddressLengthExceeded';
  44. /**
  45. * @var array
  46. */
  47. protected $_messageTemplates = array(
  48. self::INVALID => "'%value%' is not a valid email address in the basic format local-part@hostname",
  49. self::INVALID_HOSTNAME => "'%hostname%' is not a valid hostname for email address '%value%'",
  50. self::INVALID_MX_RECORD => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'",
  51. self::DOT_ATOM => "'%localPart%' not matched against dot-atom format",
  52. self::QUOTED_STRING => "'%localPart%' not matched against quoted-string format",
  53. self::INVALID_LOCAL_PART => "'%localPart%' is not a valid local part for email address '%value%'",
  54. self::LENGTH_EXCEEDED => "'%value%' exceeds the allowed length"
  55. );
  56. /**
  57. * @var array
  58. */
  59. protected $_messageVariables = array(
  60. 'hostname' => '_hostname',
  61. 'localPart' => '_localPart'
  62. );
  63. /**
  64. * Local object for validating the hostname part of an email address
  65. *
  66. * @var Zend_Validate_Hostname
  67. * @depreciated
  68. */
  69. public $hostnameValidator;
  70. /**
  71. * Whether we check for a valid MX record via DNS
  72. *
  73. * @var boolean
  74. */
  75. protected $_validateMx = false;
  76. /**
  77. * @var string
  78. */
  79. protected $_hostname;
  80. /**
  81. * @var string
  82. */
  83. protected $_localPart;
  84. /**
  85. * Instantiates hostname validator for local use
  86. *
  87. * You can pass a bitfield to determine what types of hostnames are allowed.
  88. * These bitfields are defined by the ALLOW_* constants in Zend_Validate_Hostname
  89. * The default is to allow DNS hostnames only
  90. *
  91. * @param integer $allow OPTIONAL
  92. * @param bool $validateMx OPTIONAL
  93. * @param Zend_Validate_Hostname $hostnameValidator OPTIONAL
  94. * @return void
  95. */
  96. public function __construct($allow = Zend_Validate_Hostname::ALLOW_DNS, $validateMx = false, Zend_Validate_Hostname $hostnameValidator = null)
  97. {
  98. $this->setValidateMx($validateMx);
  99. $this->setHostnameValidator($hostnameValidator, $allow);
  100. }
  101. /**
  102. * Returns the set hostname validator
  103. *
  104. * @return Zend_Validate_Hostname
  105. */
  106. public function getHostnameValidator()
  107. {
  108. return $this->hostnameValidator;
  109. }
  110. /**
  111. * @param Zend_Validate_Hostname $hostnameValidator OPTIONAL
  112. * @param int $allow OPTIONAL
  113. * @return void
  114. */
  115. public function setHostnameValidator(Zend_Validate_Hostname $hostnameValidator = null, $allow = Zend_Validate_Hostname::ALLOW_DNS)
  116. {
  117. if ($hostnameValidator === null) {
  118. $hostnameValidator = new Zend_Validate_Hostname($allow);
  119. }
  120. $this->hostnameValidator = $hostnameValidator;
  121. }
  122. /**
  123. * Whether MX checking via dns_get_mx is supported or not
  124. *
  125. * This currently only works on UNIX systems
  126. *
  127. * @return boolean
  128. */
  129. public function validateMxSupported()
  130. {
  131. return function_exists('dns_get_mx');
  132. }
  133. /**
  134. * Set whether we check for a valid MX record via DNS
  135. *
  136. * This only applies when DNS hostnames are validated
  137. *
  138. * @param boolean $allowed Set allowed to true to validate for MX records, and false to not validate them
  139. */
  140. public function setValidateMx($allowed)
  141. {
  142. $this->_validateMx = (bool) $allowed;
  143. }
  144. /**
  145. * Defined by Zend_Validate_Interface
  146. *
  147. * Returns true if and only if $value is a valid email address
  148. * according to RFC2822
  149. *
  150. * @link http://www.ietf.org/rfc/rfc2822.txt RFC2822
  151. * @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters
  152. * @param string $value
  153. * @return boolean
  154. */
  155. public function isValid($value)
  156. {
  157. $valueString = (string) $value;
  158. $matches = array();
  159. $length = true;
  160. $this->_setValue($valueString);
  161. // Split email address up and disallow '..'
  162. if ((strpos($valueString, '..') !== false) or
  163. (!preg_match('/^(.+)@([^@]+)$/', $valueString, $matches))) {
  164. $this->_error(self::INVALID);
  165. return false;
  166. }
  167. $this->_localPart = $matches[1];
  168. $this->_hostname = $matches[2];
  169. if ((strlen($this->_localPart) > 64) || (strlen($this->_hostname) > 255)) {
  170. $length = false;
  171. $this->_error(self::LENGTH_EXCEEDED);
  172. }
  173. // Match hostname part
  174. $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())
  175. ->isValid($this->_hostname);
  176. if (!$hostnameResult) {
  177. $this->_error(self::INVALID_HOSTNAME);
  178. // Get messages and errors from hostnameValidator
  179. foreach ($this->hostnameValidator->getMessages() as $code => $message) {
  180. $this->_messages[$code] = $message;
  181. }
  182. foreach ($this->hostnameValidator->getErrors() as $error) {
  183. $this->_errors[] = $error;
  184. }
  185. } else if ($this->_validateMx) {
  186. // MX check on hostname via dns_get_record()
  187. if ($this->validateMxSupported()) {
  188. $result = dns_get_mx($this->_hostname, $mxHosts);
  189. if (count($mxHosts) < 1) {
  190. $hostnameResult = false;
  191. $this->_error(self::INVALID_MX_RECORD);
  192. }
  193. } else {
  194. /**
  195. * MX checks are not supported by this system
  196. * @see Zend_Validate_Exception
  197. */
  198. require_once 'Zend/Validate/Exception.php';
  199. throw new Zend_Validate_Exception('Internal error: MX checking not available on this system');
  200. }
  201. }
  202. // First try to match the local part on the common dot-atom format
  203. $localResult = false;
  204. // Dot-atom characters are: 1*atext *("." 1*atext)
  205. // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
  206. // "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
  207. $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
  208. if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
  209. $localResult = true;
  210. } else {
  211. // Try quoted string format
  212. // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE
  213. // qtext: Non white space controls, and the rest of the US-ASCII characters not
  214. // including "\" or the quote character
  215. $noWsCtl = '\x01-\x08\x0b\x0c\x0e-\x1f\x7f';
  216. $qtext = $noWsCtl . '\x21\x23-\x5b\x5d-\x7e';
  217. $ws = '\x20\x09';
  218. if (preg_match('/^\x22([' . $ws . $qtext . '])*[$ws]?\x22$/', $this->_localPart)) {
  219. $localResult = true;
  220. } else {
  221. $this->_error(self::DOT_ATOM);
  222. $this->_error(self::QUOTED_STRING);
  223. $this->_error(self::INVALID_LOCAL_PART);
  224. }
  225. }
  226. // If both parts valid, return true
  227. if ($localResult && $hostnameResult && $length) {
  228. return true;
  229. } else {
  230. return false;
  231. }
  232. }
  233. }