1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace Egulias\EmailValidator\Validation;
- use Egulias\EmailValidator\EmailLexer;
- use Egulias\EmailValidator\Exception\InvalidEmail;
- use Egulias\EmailValidator\Warning\NoDNSMXRecord;
- use Egulias\EmailValidator\Exception\NoDNSRecord;
- class DNSCheckValidation implements EmailValidation
- {
- /**
- * @var array
- */
- private $warnings = [];
- /**
- * @var InvalidEmail|null
- */
- private $error;
- public function __construct()
- {
- if (!function_exists('idn_to_ascii')) {
- throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
- }
- }
- public function isValid($email, EmailLexer $emailLexer)
- {
- // use the input to check DNS if we cannot extract something similar to a domain
- $host = $email;
- // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
- if (false !== $lastAtPos = strrpos($email, '@')) {
- $host = substr($email, $lastAtPos + 1);
- }
- return $this->checkDNS($host);
- }
- public function getError()
- {
- return $this->error;
- }
- public function getWarnings()
- {
- return $this->warnings;
- }
- /**
- * @param string $host
- *
- * @return bool
- */
- protected function checkDNS($host)
- {
- $variant = INTL_IDNA_VARIANT_2003;
- if ( defined('INTL_IDNA_VARIANT_UTS46') ) {
- $variant = INTL_IDNA_VARIANT_UTS46;
- }
- $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
- $Aresult = true;
- $MXresult = checkdnsrr($host, 'MX');
- if (!$MXresult) {
- $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
- $Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
- if (!$Aresult) {
- $this->error = new NoDNSRecord();
- }
- }
- return $MXresult || $Aresult;
- }
- }
|