spf.inc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. error_reporting(0);
  3. function get_spf_allowed_hosts($domain)
  4. {
  5. $hosts = array();
  6. $records = dns_get_record($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. if (strpos($mech, ':') !== FALSE) // handle a domain specification
  32. {
  33. $split = explode(':', $mech);
  34. $mech = array_shift($split);
  35. $domain = implode(':', $split);
  36. if (strpos($domain, '/') !== FALSE) // remove CIDR specification
  37. {
  38. $split = explode('/', $domain);
  39. $domain = $split[0];
  40. $cidr = $split[1];
  41. }
  42. }
  43. $new_hosts = array();
  44. if ($mech == 'include') // handle an inclusion
  45. {
  46. $new_hosts = get_spf_allowed_hosts($domain);
  47. }
  48. elseif ($mech == 'a') // handle a mechanism
  49. {
  50. $new_hosts = get_a_hosts($domain);
  51. }
  52. elseif ($mech == 'mx') // handle mx mechanism
  53. {
  54. $new_hosts = get_mx_hosts($domain);
  55. }
  56. elseif ($mech == 'ip4' || $mech == 'ip6') // handle ip mechanism
  57. {
  58. $new_hosts = array($domain);
  59. }
  60. if (isset($cidr)) // add CIDR specification if present
  61. {
  62. foreach ($new_hosts as &$host)
  63. {
  64. $host .= '/' . $cidr;
  65. }
  66. unset($host);
  67. }
  68. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  69. }
  70. }
  71. }
  72. return $hosts;
  73. }
  74. function get_mx_hosts($domain)
  75. {
  76. $hosts = array();
  77. try {
  78. $mx_records = dns_get_record($domain, DNS_MX);
  79. if ($mx_records) {
  80. foreach ($mx_records as $mx_record) {
  81. $new_hosts = get_a_hosts($mx_record['target']);
  82. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  83. }
  84. }
  85. }
  86. catch (Exception $e) {
  87. if ($e->getMessage() !== 'dns_get_record(): A temporary server error occurred.') {
  88. throw $e;
  89. }
  90. $mx_records = false;
  91. }
  92. return $hosts;
  93. }
  94. function get_a_hosts($domain)
  95. {
  96. $hosts = array();
  97. $a_records = dns_get_record($domain, DNS_A);
  98. foreach ($a_records as $a_record)
  99. {
  100. $hosts[] = $a_record['ip'];
  101. }
  102. $a_records = dns_get_record($domain, DNS_AAAA);
  103. foreach ($a_records as $a_record)
  104. {
  105. $hosts[] = $a_record['ipv6'];
  106. }
  107. return $hosts;
  108. }
  109. function get_outgoing_hosts_best_guess($domain)
  110. {
  111. // try the SPF record to get hosts that are allowed to send outgoing mails for this domain
  112. $hosts = get_spf_allowed_hosts($domain);
  113. if ($hosts) return $hosts;
  114. // try the MX record to get mail servers for this domain
  115. $hosts = get_mx_hosts($domain);
  116. if ($hosts) return $hosts;
  117. // fall back to the A record to get the host name for this domain
  118. return get_a_hosts($domain);
  119. }
  120. ?>