regDomain.inc.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?
  2. /*
  3. * Calculate the effective registered domain of a fully qualified domain name.
  4. *
  5. * <@LICENSE>
  6. * Licensed to the Apache Software Foundation (ASF) under one or more
  7. * contributor license agreements. See the NOTICE file distributed with
  8. * this work for additional information regarding copyright ownership.
  9. * The ASF licenses this file to you under the Apache License, Version 2.0
  10. * (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at:
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. * </@LICENSE>
  21. *
  22. * Florian Sager, 25.07.2008, sager@agitos.de
  23. */
  24. /*
  25. * Remove subdomains from a signing domain to get the registered domain.
  26. *
  27. * dkim-reputation.org blocks signing domains on the level of registered domains
  28. * to rate senders who use e.g. a.spamdomain.tld, b.spamdomain.tld, ... under
  29. * the most common identifier - the registered domain - finally.
  30. *
  31. * This function returns NULL if $signingDomain is TLD itself
  32. */
  33. function getRegisteredDomain($signingDomain) {
  34. global $tldTree;
  35. $signingDomainParts = explode('.', $signingDomain);
  36. $result = findRegisteredDomain($signingDomainParts, $tldTree);
  37. if ($result===NULL || $result=="") {
  38. // this is an invalid domain name
  39. return NULL;
  40. }
  41. // assure there is at least 1 TLD in the stripped signing domain
  42. if (!strpos($result, '.')) {
  43. $cnt = count($signingDomainParts);
  44. if ($cnt==1 || $signingDomainParts[$cnt-2]=="") return NULL;
  45. return $signingDomainParts[$cnt-2].'.'.$signingDomainParts[$cnt-1];
  46. }
  47. return $result;
  48. }
  49. // recursive helper method
  50. function findRegisteredDomain($remainingSigningDomainParts, &$treeNode) {
  51. $sub = array_pop($remainingSigningDomainParts);
  52. $result = NULL;
  53. if (isset($treeNode['!'])) {
  54. return '#';
  55. } else if (is_array($treeNode) && array_key_exists($sub, $treeNode)) {
  56. $result = findRegisteredDomain($remainingSigningDomainParts, $treeNode[$sub]);
  57. } else if (is_array($treeNode) && array_key_exists('*', $treeNode)) {
  58. $result = findRegisteredDomain($remainingSigningDomainParts, $treeNode['*']);
  59. } else {
  60. return $sub;
  61. }
  62. // this is a hack 'cause PHP interpretes '' as NULL
  63. if ($result == '#') {
  64. return $sub;
  65. } else if (strlen($result)>0) {
  66. return $result.'.'.$sub;
  67. }
  68. return NULL;
  69. }
  70. ?>