spf.inc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. error_reporting(0);
  3. function get_spf_allowed_hosts($check_domain)
  4. {
  5. $hosts = array();
  6. $records = dns_get_record($check_domain, DNS_TXT);
  7. foreach ($records as $record)
  8. {
  9. $txt = explode(' ', $record['entries'][0]);
  10. if (array_shift($txt) != 'v=spf1') // only handle SPF records
  11. continue;
  12. foreach ($txt as $mech)
  13. {
  14. $qual = substr($mech, 0, 1);
  15. if ($qual == '-' || $qual == '~') // only handle pass or neutral records
  16. continue(2);
  17. if ($qual == '+' || $qual == '?')
  18. $mech = substr($mech, 1); // remove the qualifier
  19. if (strpos($mech, '=') !== FALSE) // handle a modifier
  20. {
  21. $mod = explode('=', $mech);
  22. if ($mod[0] == 'redirect') // handle a redirect
  23. {
  24. $hosts = get_spf_allowed_hosts($mod[1]);
  25. return $hosts;
  26. }
  27. }
  28. else
  29. {
  30. unset($cidr);
  31. // reset domain to check_domain
  32. $domain = $check_domain;
  33. if (strpos($mech, ':') !== FALSE) // handle a domain specification
  34. {
  35. $split = explode(':', $mech);
  36. $mech = array_shift($split);
  37. $domain = implode(':', $split);
  38. if (strpos($domain, '/') !== FALSE) // remove CIDR specification
  39. {
  40. $split = explode('/', $domain);
  41. $domain = $split[0];
  42. $cidr = $split[1];
  43. }
  44. }
  45. $new_hosts = array();
  46. if ($mech == 'include' && $check_domain != $domain) // handle an inclusion
  47. {
  48. $new_hosts = get_spf_allowed_hosts($domain);
  49. }
  50. elseif ($mech == 'a') // handle a mechanism
  51. {
  52. $new_hosts = get_a_hosts($domain);
  53. }
  54. elseif ($mech == 'mx') // handle mx mechanism
  55. {
  56. $new_hosts = get_mx_hosts($domain);
  57. }
  58. elseif ($mech == 'ip4' || $mech == 'ip6') // handle ip mechanism
  59. {
  60. $new_hosts = array($domain);
  61. }
  62. if (isset($cidr)) // add CIDR specification if present
  63. {
  64. foreach ($new_hosts as &$host)
  65. {
  66. $host .= '/' . $cidr;
  67. }
  68. unset($host);
  69. }
  70. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  71. }
  72. }
  73. }
  74. foreach ($hosts as &$host) {
  75. if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  76. $host = $host;
  77. }
  78. }
  79. return $hosts;
  80. }
  81. function get_mx_hosts($domain)
  82. {
  83. $hosts = array();
  84. try {
  85. $mx_records = dns_get_record($domain, DNS_MX);
  86. if ($mx_records) {
  87. foreach ($mx_records as $mx_record) {
  88. $new_hosts = get_a_hosts($mx_record['target']);
  89. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  90. }
  91. }
  92. }
  93. catch (Exception $e) {
  94. if ($e->getMessage() !== 'dns_get_record(): A temporary server error occurred.') {
  95. throw $e;
  96. }
  97. $mx_records = false;
  98. }
  99. return $hosts;
  100. }
  101. function get_a_hosts($domain)
  102. {
  103. $hosts = array();
  104. $a_records = dns_get_record($domain, DNS_A);
  105. foreach ($a_records as $a_record)
  106. {
  107. $hosts[] = $a_record['ip'];
  108. }
  109. $a_records = dns_get_record($domain, DNS_AAAA);
  110. foreach ($a_records as $a_record)
  111. {
  112. $hosts[] = $a_record['ipv6'];
  113. }
  114. return $hosts;
  115. }
  116. function get_outgoing_hosts_best_guess($domain)
  117. {
  118. // try the SPF record to get hosts that are allowed to send outgoing mails for this domain
  119. $hosts = get_spf_allowed_hosts($domain);
  120. if ($hosts) return $hosts;
  121. // try the MX record to get mail servers for this domain
  122. $hosts = get_mx_hosts($domain);
  123. if ($hosts) return $hosts;
  124. // fall back to the A record to get the host name for this domain
  125. return get_a_hosts($domain);
  126. }
  127. ?>