00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026
00030 require_once 'Zend/Validate/Hostname.php';
00031
00038 class Zend_Validate_EmailAddress extends Zend_Validate_Abstract
00039 {
00040 const INVALID = 'emailAddressInvalid';
00041 const INVALID_FORMAT = 'emailAddressInvalidFormat';
00042 const INVALID_HOSTNAME = 'emailAddressInvalidHostname';
00043 const INVALID_MX_RECORD = 'emailAddressInvalidMxRecord';
00044 const DOT_ATOM = 'emailAddressDotAtom';
00045 const QUOTED_STRING = 'emailAddressQuotedString';
00046 const INVALID_LOCAL_PART = 'emailAddressInvalidLocalPart';
00047 const LENGTH_EXCEEDED = 'emailAddressLengthExceeded';
00048
00052 protected $_messageTemplates = array(
00053 self::INVALID => "Invalid type given, value should be a string",
00054 self::INVALID_FORMAT => "'%value%' is not a valid email address in the basic format local-part@hostname",
00055 self::INVALID_HOSTNAME => "'%hostname%' is not a valid hostname for email address '%value%'",
00056 self::INVALID_MX_RECORD => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'",
00057 self::DOT_ATOM => "'%localPart%' not matched against dot-atom format",
00058 self::QUOTED_STRING => "'%localPart%' not matched against quoted-string format",
00059 self::INVALID_LOCAL_PART => "'%localPart%' is not a valid local part for email address '%value%'",
00060 self::LENGTH_EXCEEDED => "'%value%' exceeds the allowed length"
00061 );
00062
00066 protected $_messageVariables = array(
00067 'hostname' => '_hostname',
00068 'localPart' => '_localPart'
00069 );
00070
00077 public $hostnameValidator;
00078
00084 protected $_validateMx = false;
00085
00089 protected $_hostname;
00090
00094 protected $_localPart;
00095
00108 public function __construct($allow = Zend_Validate_Hostname::ALLOW_DNS, $validateMx = false, Zend_Validate_Hostname $hostnameValidator = null)
00109 {
00110 $this->setValidateMx($validateMx);
00111 $this->setHostnameValidator($hostnameValidator, $allow);
00112 }
00113
00119 public function getHostnameValidator()
00120 {
00121 return $this->hostnameValidator;
00122 }
00123
00129 public function setHostnameValidator(Zend_Validate_Hostname $hostnameValidator = null, $allow = Zend_Validate_Hostname::ALLOW_DNS)
00130 {
00131 if ($hostnameValidator === null) {
00132 $hostnameValidator = new Zend_Validate_Hostname($allow);
00133 }
00134 $this->hostnameValidator = $hostnameValidator;
00135 }
00136
00144 public function validateMxSupported()
00145 {
00146 return function_exists('dns_get_mx');
00147 }
00148
00156 public function setValidateMx($allowed)
00157 {
00158 $this->_validateMx = (bool) $allowed;
00159 }
00160
00172 public function isValid($value)
00173 {
00174 if (!is_string($value)) {
00175 $this->_error(self::INVALID);
00176 return false;
00177 }
00178
00179 $matches = array();
00180 $length = true;
00181
00182 $this->_setValue($value);
00183
00184
00185 if ((strpos($value, '..') !== false) or
00186 (!preg_match('/^(.+)@([^@]+)$/', $value, $matches))) {
00187 $this->_error(self::INVALID_FORMAT);
00188 return false;
00189 }
00190
00191 $this->_localPart = $matches[1];
00192 $this->_hostname = $matches[2];
00193
00194 if ((strlen($this->_localPart) > 64) || (strlen($this->_hostname) > 255)) {
00195 $length = false;
00196 $this->_error(self::LENGTH_EXCEEDED);
00197 }
00198
00199
00200 $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())
00201 ->isValid($this->_hostname);
00202 if (!$hostnameResult) {
00203 $this->_error(self::INVALID_HOSTNAME);
00204
00205
00206 foreach ($this->hostnameValidator->getMessages() as $code => $message) {
00207 $this->_messages[$code] = $message;
00208 }
00209 foreach ($this->hostnameValidator->getErrors() as $error) {
00210 $this->_errors[] = $error;
00211 }
00212 } else if ($this->_validateMx) {
00213
00214 if ($this->validateMxSupported()) {
00215 $result = dns_get_mx($this->_hostname, $mxHosts);
00216 if (count($mxHosts) < 1) {
00217 $hostnameResult = false;
00218 $this->_error(self::INVALID_MX_RECORD);
00219 }
00220 } else {
00225 require_once 'Zend/Validate/Exception.php';
00226 throw new Zend_Validate_Exception('Internal error: MX checking not available on this system');
00227 }
00228 }
00229
00230
00231 $localResult = false;
00232
00233
00234
00235
00236 $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
00237 if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->_localPart)) {
00238 $localResult = true;
00239 } else {
00240
00241
00242
00243
00244
00245 $noWsCtl = '\x01-\x08\x0b\x0c\x0e-\x1f\x7f';
00246 $qtext = $noWsCtl . '\x21\x23-\x5b\x5d-\x7e';
00247 $ws = '\x20\x09';
00248 if (preg_match('/^\x22([' . $ws . $qtext . '])*[$ws]?\x22$/', $this->_localPart)) {
00249 $localResult = true;
00250 } else {
00251 $this->_error(self::DOT_ATOM);
00252 $this->_error(self::QUOTED_STRING);
00253 $this->_error(self::INVALID_LOCAL_PART);
00254 }
00255 }
00256
00257
00258 if ($localResult && $hostnameResult && $length) {
00259 return true;
00260 } else {
00261 return false;
00262 }
00263 }
00264 }