spf.inc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. try {
  77. $mx_records = dns_get_record($domain, DNS_MX);
  78. foreach ($mx_records as $mx_record)
  79. {
  80. $new_hosts = get_a_hosts($mx_record['target']);
  81. $hosts = array_unique(array_merge($hosts,$new_hosts), SORT_REGULAR);
  82. }
  83. }
  84. catch (Exception $e) {
  85. if ($e->getMessage() !== 'dns_get_record(): A temporary server error occurred.') {
  86. throw $e;
  87. }
  88. $mx_records = false;
  89. }
  90. return $hosts;
  91. }
  92. function get_a_hosts($domain)
  93. {
  94. $hosts = array();
  95. $a_records = dns_get_record($domain, DNS_A);
  96. foreach ($a_records as $a_record)
  97. {
  98. $hosts[] = $a_record['ip'];
  99. }
  100. $a_records = dns_get_record($domain, DNS_AAAA);
  101. foreach ($a_records as $a_record)
  102. {
  103. $hosts[] = $a_record['ipv6'];
  104. }
  105. return $hosts;
  106. }
  107. function get_outgoing_hosts_best_guess($domain)
  108. {
  109. // try the SPF record to get hosts that are allowed to send outgoing mails for this domain
  110. $hosts = get_spf_allowed_hosts($domain);
  111. if ($hosts) return $hosts;
  112. // try the MX record to get mail servers for this domain
  113. $hosts = get_mx_hosts($domain);
  114. if ($hosts) return $hosts;
  115. // fall back to the A record to get the host name for this domain
  116. return get_a_hosts($domain);
  117. }
  118. ?>