| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Validate an email address |
| 9 |
* |
| 10 |
* Email Address syntax: (<local part>@<domain-literal part>) |
| 11 |
* |
| 12 |
* We currently do not support dot-atom syntax (refer RFC 2822 [https://www.ietf.org/rfc/rfc2822.txt] |
| 13 |
* documentation for more details) for domain-literal part of an email address |
| 14 |
* |
| 15 |
*/ |
| 16 |
class EmailAddressValidator extends BaseValidator |
| 17 |
{ |
| 18 |
/** @var bool If MX check should be enabled */ |
| 19 |
protected bool $mx = false; |
| 20 |
|
| 21 |
/** @var bool If a deep MX check should be enabled */ |
| 22 |
protected bool $deep = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Create a new EmailAddressValidator |
| 26 |
* |
| 27 |
* Optional options: |
| 28 |
* |
| 29 |
* - mx: (bool) Whether to validate the hostname via MX DNS record, default false |
| 30 |
* - deep: (bool) Whether to perform a deep MX check (requires mx), default false |
| 31 |
* |
| 32 |
* @param array{mx?: bool, deep?: bool} $options |
| 33 |
* |
| 34 |
* @throws InvalidArgumentException If deep MX check is enabled without enabling the MX check first |
| 35 |
*/ |
| 36 |
public function __construct(array $options = []) |
| 37 |
{ |
| 38 |
if (array_key_exists('mx', $options)) { |
| 39 |
$this->setEnableMxCheck($options['mx']); |
| 40 |
} |
| 41 |
|
| 42 |
if (array_key_exists('deep', $options)) { |
| 43 |
$this->setEnableDeepMxCheck($options['deep']); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Set MX check |
| 49 |
* |
| 50 |
* To validate if the hostname is a DNS mail exchange (MX) record set it to true |
| 51 |
* |
| 52 |
* @param bool $mx If MX check should be enabled |
| 53 |
* |
| 54 |
* @return $this |
| 55 |
*/ |
| 56 |
public function setEnableMxCheck(bool $mx = true): static |
| 57 |
{ |
| 58 |
$this->mx = $mx; |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Set Deep MX check |
| 65 |
* |
| 66 |
* To validate if the hostname is a DNS mail exchange (MX) record, and it points to an A record (for IPv4) or |
| 67 |
* an AAAA / A6 record (for IPv6) set it to true |
| 68 |
* |
| 69 |
* @param bool $deep If deep MX check should be enabled |
| 70 |
* |
| 71 |
* @return $this |
| 72 |
* |
| 73 |
* @throws InvalidArgumentException If MX check is not enabled |
| 74 |
*/ |
| 75 |
public function setEnableDeepMxCheck(bool $deep = true): static |
| 76 |
{ |
| 77 |
if (! $this->mx) { |
| 78 |
throw new InvalidArgumentException("MX record check has to be enabled to enable deep MX record check"); |
| 79 |
} |
| 80 |
|
| 81 |
$this->deep = $deep; |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Validate the local part (username / the part before '@') of the email address |
| 88 |
* |
| 89 |
* @param string $localPart |
| 90 |
* @param string $email |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
private function validateLocalPart(string $localPart, string $email): bool |
| 95 |
{ |
| 96 |
// Try to match the local part against the dot-atom format. |
| 97 |
$result = false; |
| 98 |
|
| 99 |
// Dot-atom characters are: 1*atext *("." 1*atext) |
| 100 |
// atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*", |
| 101 |
// "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~" |
| 102 |
$atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e'; |
| 103 |
if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $localPart)) { |
| 104 |
$result = true; |
| 105 |
} else { |
| 106 |
// Try quoted string format (RFC 5321 Chapter 4.1.2) |
| 107 |
|
| 108 |
// Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE |
| 109 |
$qtext = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126 |
| 110 |
$quotedPair = '\x20-\x7e'; // %d92 %d32-126 |
| 111 |
if (preg_match('/^"([' . $qtext . ']|\x5c[' . $quotedPair . '])*"$/', $localPart)) { |
| 112 |
$result = true; |
| 113 |
} else { |
| 114 |
$this->addMessage(sprintf( |
| 115 |
$this->translate( |
| 116 |
"'%s' can not be matched against dot-atom format or quoted-string format" |
| 117 |
), |
| 118 |
$localPart |
| 119 |
)); |
| 120 |
$this->addMessage(sprintf( |
| 121 |
$this->translate("Hence '%s' is not a valid local part for email address '%s'"), |
| 122 |
$localPart, |
| 123 |
$email |
| 124 |
)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $result; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Validate the hostname part of the email address |
| 133 |
* |
| 134 |
* @param string $hostname |
| 135 |
* @param string $email |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
private function validateHostnamePart(string $hostname, string $email): bool |
| 140 |
{ |
| 141 |
$hostValidator = new HostnameValidator(); |
| 142 |
|
| 143 |
if ($this->validateIp($hostname)) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
if (preg_match('/^\[([^\]]*)\]$/i', $hostname, $matches)) { |
| 148 |
$validHostname = $matches[1]; |
| 149 |
if (! $this->validateIp($validHostname)) { |
| 150 |
$this->addMessage(sprintf( |
| 151 |
$this->translate("host name %s is a domain literal and is invalid"), |
| 152 |
$hostname |
| 153 |
)); |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
if (! $hostValidator->isValid($hostname)) { |
| 162 |
$this->addMessage(sprintf( |
| 163 |
$this->translate('%s is not a valid domain name for email address %s.'), |
| 164 |
$hostname, |
| 165 |
$email |
| 166 |
)); |
| 167 |
|
| 168 |
return false; |
| 169 |
} elseif ($this->mx) { |
| 170 |
// Check MX records for the hostname. |
| 171 |
return $this->validateMXRecords($hostname, $email); |
| 172 |
} |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check if the given IP address is valid |
| 179 |
* |
| 180 |
* @param string $value |
| 181 |
* |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
private function validateIp(string $value): bool |
| 185 |
{ |
| 186 |
if (! filter_var($value, FILTER_VALIDATE_IP)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Check whether the value is a valid email address per RFC 2822 |
| 195 |
* |
| 196 |
* @param string $value |
| 197 |
* |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
public function isValid($value): bool |
| 201 |
{ |
| 202 |
$this->clearMessages(); |
| 203 |
|
| 204 |
$matches = []; |
| 205 |
$length = true; |
| 206 |
|
| 207 |
// Split the address and disallow '..'. |
| 208 |
if ( |
| 209 |
(strpos($value, '..') !== false) |
| 210 |
|| (! preg_match('/^(.+)@([^@]+)$/', $value, $matches)) |
| 211 |
) { |
| 212 |
$this->addMessage(sprintf( |
| 213 |
$this->translate("'%s' is not a valid email address in the basic format local-part@hostname"), |
| 214 |
$value |
| 215 |
)); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
$localPart = $matches[1]; |
| 220 |
$hostname = $matches[2]; |
| 221 |
|
| 222 |
if ((strlen($localPart) > 64) || (strlen($hostname) > 255)) { |
| 223 |
$length = false; |
| 224 |
$this->addMessage(sprintf( |
| 225 |
$this->translate("'%s' exceeds the allowed length"), |
| 226 |
$value |
| 227 |
)); |
| 228 |
} |
| 229 |
|
| 230 |
$local = $this->validateLocalPart($localPart, $value); |
| 231 |
|
| 232 |
// Both parts and length must be valid. |
| 233 |
if (($local && $this->validateHostnamePart($hostname, $value)) && $length) { |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Perform deep MX record validation |
| 242 |
* |
| 243 |
* Check if the hostname is a valid DNS mail exchange (MX) record in case deep MX record check is enabled, |
| 244 |
* also checks if the corresponding MX record points to an A record (for IPv4) or an AAAA / A6 record (for IPv6) |
| 245 |
* |
| 246 |
* @param string $hostname |
| 247 |
* @param string $email |
| 248 |
* |
| 249 |
* @return bool |
| 250 |
*/ |
| 251 |
private function validateMXRecords(string $hostname, string $email): bool |
| 252 |
{ |
| 253 |
$mxHosts = []; |
| 254 |
// Decode IDN domain name. |
| 255 |
$decodedHostname = idn_to_ascii($hostname, 0, INTL_IDNA_VARIANT_UTS46); |
| 256 |
|
| 257 |
$result = $decodedHostname && getmxrr($decodedHostname, $mxHosts); |
| 258 |
|
| 259 |
if (! $result) { |
| 260 |
$this->addMessage(sprintf( |
| 261 |
$this->translate("'%s' does not appear to have a valid MX record for the email address '%s'"), |
| 262 |
$hostname, |
| 263 |
$email |
| 264 |
)); |
| 265 |
} elseif ($this->deep) { |
| 266 |
$validAddress = false; |
| 267 |
$reserved = true; |
| 268 |
foreach ($mxHosts as $decodedHostname) { |
| 269 |
$res = $this->isReserved($decodedHostname); |
| 270 |
if (! $res) { |
| 271 |
$reserved = false; |
| 272 |
} |
| 273 |
|
| 274 |
if ( |
| 275 |
! $res |
| 276 |
&& ( |
| 277 |
checkdnsrr($decodedHostname, "A") |
| 278 |
|| checkdnsrr($decodedHostname, "AAAA") |
| 279 |
|| checkdnsrr($decodedHostname, "A6") |
| 280 |
) |
| 281 |
) { |
| 282 |
$validAddress = true; |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (! $validAddress) { |
| 288 |
$result = false; |
| 289 |
if ($reserved) { |
| 290 |
$this->addMessage(sprintf( |
| 291 |
$this->translate( |
| 292 |
"'%s' is not in a routable network segment." . |
| 293 |
" The email address '%s' should not be resolved from public network" |
| 294 |
), |
| 295 |
$hostname, |
| 296 |
$email |
| 297 |
)); |
| 298 |
} else { |
| 299 |
$this->addMessage(sprintf( |
| 300 |
$this->translate("'%s' does not appear to have a valid MX record for the email address '%s'"), |
| 301 |
$hostname, |
| 302 |
$email |
| 303 |
)); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return $result; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Validate whether the given host is reserved |
| 313 |
* |
| 314 |
* @param string $host Hostname or IP address to check |
| 315 |
* |
| 316 |
* @return bool |
| 317 |
*/ |
| 318 |
private function isReserved(string $host): bool |
| 319 |
{ |
| 320 |
if (! preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $host)) { |
| 321 |
$host = gethostbyname($host); |
| 322 |
} |
| 323 |
|
| 324 |
if (! filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)) { |
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
return false; |
| 329 |
} |
| 330 |
} |