]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/extlib/regDomain.inc.php
merge 0.9.x into 1.0.x
[quix0rs-gnu-social.git] / plugins / DomainStatusNetwork / extlib / regDomain.inc.php
1 <?
2
3 /*
4  * Calculate the effective registered domain of a fully qualified domain name.
5  *
6  * <@LICENSE>
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:
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  * </@LICENSE>
22  *
23  * Florian Sager, 25.07.2008, sager@agitos.de
24  */
25
26 /*
27  * Remove subdomains from a signing domain to get the registered domain.
28  *
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.
32  *
33  * This function returns NULL if $signingDomain is TLD itself
34  */
35
36 function getRegisteredDomain($signingDomain) {
37
38         global $tldTree;
39
40         $signingDomainParts = split('\.', $signingDomain);
41
42         $result = findRegisteredDomain($signingDomainParts, $tldTree);
43
44         if ($result===NULL || $result=="") {
45                 // this is an invalid domain name
46                 return NULL;
47         }
48
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];
54         }
55         return $result;
56 }
57
58 // recursive helper method
59 function findRegisteredDomain($remainingSigningDomainParts, &$treeNode) {
60
61         $sub = array_pop($remainingSigningDomainParts);
62
63         $result = NULL;
64         if (isset($treeNode['!'])) {
65                 return '#';
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['*']);
70         } else {
71                 return $sub;
72         }
73
74         // this is a hack 'cause PHP interpretes '' as NULL
75         if ($result == '#') {
76                 return $sub;
77         } else if (strlen($result)>0) {
78                 return $result.'.'.$sub;
79         }
80         return NULL;
81 }
82
83 ?>