spf.inc.php 3.5 KB

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