2
0

EmailAddress.php 9.0 KB

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