Subversion Repositories oidplus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
226 daniel-mar 1
<?php
2
 
3
# if (!interface_exists('VolcanoSearchProvider')) throw new Exception('Required interface "VolcanoSearchProvider" not found.');
4
# if (!class_exists('VolcanoDB')) throw new Exception('Required class "VolcanoDB" not found.');
5
 
6
require_once __DIR__ . '/../../core/VolcanoSearchProvider.class.php';
7
 
8
class VolcanoSearchDomain implements VolcanoSearchProvider {
9
        public static function checkId($id) {
10
                return self::strEqual($id, 'domain', true); // is always case insensitive
11
        }
12
 
13
        protected static function tryNormalize($term) {
14
                // TODO: also validate syntax
15
                $ary = explode(':', $term);
16
                $term = $ary[0];
17
                if (substr($term, -1, 1) != '.') $term .= '.';
18
                return $term;
19
        }
20
 
21
        public static function calcDistance($candidate, $searchterm) {
22
                // TODO: punycode?
23
 
24
                $candidate  = self::tryNormalize($candidate);
25
                $searchterm = self::tryNormalize($searchterm);
26
 
27
                if (strlen($candidate) <= strlen($searchterm)) {
28
                        $cmp = substr($searchterm, strlen($searchterm)-strlen($candidate));
29
                        if (!self::strEqual($cmp, $candidate, true)) return false;
30
 
31
                        $subdoms = substr($searchterm, 0, strlen($searchterm)-strlen($candidate));
32
                        return substr_count($subdoms, '.');
33
                } else {
34
                        $cmp = substr($candidate, strlen($candidate)-strlen($searchterm));
35
                        if (!self::strEqual($cmp, $searchterm, true)) return false;
36
 
37
                        $too_specific = substr($candidate, 0, strlen($candidate)-strlen($searchterm));
38
                        return -substr_count($too_specific, '.');
39
                }
40
        }
41
 
42
        protected static function strEqual($str1, $str2, $caseInsensitive = false) {
43
                if ($caseInsensitive) {
44
                        return strtolower($str1) == strtolower($str2);
45
                } else {
46
                        return $str1 == $str2;
47
                }
48
        }
49
}
50
 
51
require_once __DIR__ . '/../../core/1_VolcanoDB.class.php';
52
VolcanoDB::registerSearchProvider(new VolcanoSearchDomain());
53
 
54
# --- TEST
55
 
56
/*
57
$x = new VolcanoSearchDomain();
58
assert($x->calcDistance('de.',                  'viathinksoft.DE.') ==  1);
59
assert($x->calcDistance('de.',                  'viathinksoft.de.') ==  1);
60
assert($x->calcDistance('viathinksoft.de.',     'viathinksoft.de.') ==  0);
61
assert($x->calcDistance('viathinksoft.DE.',     'viathinksoft.de.') ==  0);
62
assert($x->calcDistance('www.viathinksoft.de.', 'viathinksoft.de.') == -1);
63
 
64
assert($x->calcDistance('de.',                  'viathinksoft.xx.') === false);
65
assert($x->calcDistance('viathinksoft.de.',     'viathinksoft.xx.') === false);
66
assert($x->calcDistance('www.viathinksoft.de.', 'viathinksoft.xx.') === false);
67
*/