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