spf.inc.php 2.9 KB

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