DNSCheckValidation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Egulias\EmailValidator\Validation;
  3. use Egulias\EmailValidator\EmailLexer;
  4. use Egulias\EmailValidator\Exception\InvalidEmail;
  5. use Egulias\EmailValidator\Exception\LocalOrReservedDomain;
  6. use Egulias\EmailValidator\Exception\DomainAcceptsNoMail;
  7. use Egulias\EmailValidator\Warning\NoDNSMXRecord;
  8. use Egulias\EmailValidator\Exception\NoDNSRecord;
  9. class DNSCheckValidation implements EmailValidation
  10. {
  11. /**
  12. * @var array
  13. */
  14. private $warnings = [];
  15. /**
  16. * @var InvalidEmail|null
  17. */
  18. private $error;
  19. /**
  20. * @var array
  21. */
  22. private $mxRecords = [];
  23. public function __construct()
  24. {
  25. if (!function_exists('idn_to_ascii')) {
  26. throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
  27. }
  28. }
  29. public function isValid($email, EmailLexer $emailLexer)
  30. {
  31. // use the input to check DNS if we cannot extract something similar to a domain
  32. $host = $email;
  33. // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
  34. if (false !== $lastAtPos = strrpos($email, '@')) {
  35. $host = substr($email, $lastAtPos + 1);
  36. }
  37. // Get the domain parts
  38. $hostParts = explode('.', $host);
  39. // Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2),
  40. // mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G)
  41. $reservedTopLevelDnsNames = [
  42. // Reserved Top Level DNS Names
  43. 'test',
  44. 'example',
  45. 'invalid',
  46. 'localhost',
  47. // mDNS
  48. 'local',
  49. // Private DNS Namespaces
  50. 'intranet',
  51. 'internal',
  52. 'private',
  53. 'corp',
  54. 'home',
  55. 'lan',
  56. ];
  57. $isLocalDomain = count($hostParts) <= 1;
  58. $isReservedTopLevel = in_array($hostParts[(count($hostParts) - 1)], $reservedTopLevelDnsNames, true);
  59. // Exclude reserved top level DNS names
  60. if ($isLocalDomain || $isReservedTopLevel) {
  61. $this->error = new LocalOrReservedDomain();
  62. return false;
  63. }
  64. return $this->checkDns($host);
  65. }
  66. public function getError()
  67. {
  68. return $this->error;
  69. }
  70. public function getWarnings()
  71. {
  72. return $this->warnings;
  73. }
  74. /**
  75. * @param string $host
  76. *
  77. * @return bool
  78. */
  79. protected function checkDns($host)
  80. {
  81. $variant = INTL_IDNA_VARIANT_UTS46;
  82. $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
  83. return $this->validateDnsRecords($host);
  84. }
  85. /**
  86. * Validate the DNS records for given host.
  87. *
  88. * @param string $host A set of DNS records in the format returned by dns_get_record.
  89. *
  90. * @return bool True on success.
  91. */
  92. private function validateDnsRecords($host)
  93. {
  94. // Get all MX, A and AAAA DNS records for host
  95. // Using @ as workaround to fix https://bugs.php.net/bug.php?id=73149
  96. $dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA);
  97. // No MX, A or AAAA DNS records
  98. if (empty($dnsRecords)) {
  99. $this->error = new NoDNSRecord();
  100. return false;
  101. }
  102. // For each DNS record
  103. foreach ($dnsRecords as $dnsRecord) {
  104. if (!$this->validateMXRecord($dnsRecord)) {
  105. return false;
  106. }
  107. }
  108. // No MX records (fallback to A or AAAA records)
  109. if (empty($this->mxRecords)) {
  110. $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
  111. }
  112. return true;
  113. }
  114. /**
  115. * Validate an MX record
  116. *
  117. * @param array $dnsRecord Given DNS record.
  118. *
  119. * @return bool True if valid.
  120. */
  121. private function validateMxRecord($dnsRecord)
  122. {
  123. if ($dnsRecord['type'] !== 'MX') {
  124. return true;
  125. }
  126. // "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505)
  127. if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') {
  128. $this->error = new DomainAcceptsNoMail();
  129. return false;
  130. }
  131. $this->mxRecords[] = $dnsRecord;
  132. return true;
  133. }
  134. }