spf.inc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. error_reporting(0);
  3. function expand_ipv6($ip) {
  4. $hex = unpack("H*hex", inet_pton($ip));
  5. $ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
  6. return $ip;
  7. }
  8. function get_spf_allowed_hosts($check_domain)
  9. {
  10. $hosts = array();
  11. $records = dns_get_record($check_domain, DNS_TXT);
  12. foreach ($records as $record)
  13. {
  14. $txt = explode(' ', $record['entries'][0]);
  15. if (array_shift($txt) != 'v=spf1') // only handle SPF records
  16. continue;
  17. foreach ($txt as $mech)
  18. {
  19. $qual = substr($mech, 0, 1);
  20. if ($qual == '-' || $qual == '~') // only handle pass or neutral records
  21. continue(2);
  22. if ($qual == '+' || $qual == '?')
  23. $mech = substr($mech, 1); // remove the qualifier
  24. if (strpos($mech, '=') !== FALSE) // handle a modifier
  25. {
  26. $mod = explode('=', $mech);
  27. if ($mod[0] == 'redirect') // handle a redirect
  28. {
  29. $hosts = get_spf_allowed_hosts($mod[1]);
  30. return $hosts;
  31. }
  32. }
  33. else
  34. {
  35. unset($cidr);
  36. // reset domain to check_domain
  37. $domain = $check_domain;
  38. if (strpos($mech, ':') !== FALSE) // handle a domain specification
  39. {
  40. $split = explode(':', $mech);
  41. $mech = array_shift($split);
  42. $domain = implode(':', $split);
  43. if (strpos($domain, '/') !== FALSE) // remove CIDR specification
  44. {
  45. $split = explode('/', $domain);
  46. $domain = $split[0];
  47. $cidr = $split[1];
  48. }
  49. }
  50. $new_hosts = array();
  51. if ($mech == 'include' && $check_domain != $domain) // handle an inclusion
  52. {
  53. $new_hosts = get_spf_allowed_hosts($domain);
  54. }
  55. elseif ($mech == 'a') // handle a mechanism
  56. {
  57. $new_hosts = get_a_hosts($domain);
  58. }
  59. elseif ($mech == 'mx') // handle mx mechanism
  60. {
  61. $new_hosts = get_mx_hosts($domain);
  62. }
  63. elseif ($mech == 'ip4' || $mech == 'ip6') // handle ip mechanism
  64. {
  65. $new_hosts = array($domain);
  66. }
  67. if (isset($cidr)) // add CIDR specification if present
  68. {
  69. foreach ($new_hosts as &$host)
  70. {
  71. $host .= '/' . $cidr;
  72. }
  73. unset($host);
  74. }
  75. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  76. }
  77. }
  78. }
  79. foreach ($hosts as &$host) {
  80. if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  81. $host = expand_ipv6($host);
  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. {
  117. $hosts[] = $a_record['ipv6'];
  118. }
  119. return $hosts;
  120. }
  121. function get_outgoing_hosts_best_guess($domain)
  122. {
  123. // try the SPF record to get hosts that are allowed to send outgoing mails for this domain
  124. $hosts = get_spf_allowed_hosts($domain);
  125. if ($hosts) return $hosts;
  126. // try the MX record to get mail servers for this domain
  127. $hosts = get_mx_hosts($domain);
  128. if ($hosts) return $hosts;
  129. // fall back to the A record to get the host name for this domain
  130. return get_a_hosts($domain);
  131. }
  132. ?>