scratch – Blame information for rev 126
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
126 | office | 1 | <?php |
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Uri |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Uri; |
||
12 | |||
13 | /** |
||
14 | * |
||
15 | * Object representation of the Public Suffix List |
||
16 | * |
||
17 | * @package Aura.Uri |
||
18 | * |
||
19 | */ |
||
20 | class PublicSuffixList |
||
21 | { |
||
22 | /** |
||
23 | * |
||
24 | * Public suffix list data. |
||
25 | * |
||
26 | * @var array |
||
27 | * |
||
28 | */ |
||
29 | protected $psl; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param array $list Array representation of the Public Suffix List |
||
36 | * |
||
37 | */ |
||
38 | public function __construct(array $list) |
||
39 | { |
||
40 | $this->psl = $list; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Returns the public suffix portion of provided host |
||
45 | * |
||
46 | * @param string $host host |
||
47 | * @return string public suffix |
||
48 | */ |
||
49 | public function getPublicSuffix($host) |
||
50 | { |
||
51 | if (strpos($host, '.') === 0) { |
||
52 | return null; |
||
53 | } |
||
54 | |||
55 | if (strpos($host, '.') === false) { |
||
56 | return null; |
||
57 | } |
||
58 | |||
59 | $host = strtolower($host); |
||
60 | $parts = array_reverse(explode('.', $host)); |
||
61 | $publicSuffix = array(); |
||
62 | $psl = $this->psl; |
||
63 | |||
64 | foreach ($parts as $part) { |
||
65 | if (array_key_exists($part, $psl) |
||
66 | && array_key_exists('!', $psl[$part])) { |
||
67 | break; |
||
68 | } |
||
69 | |||
70 | if (array_key_exists($part, $psl)) { |
||
71 | array_unshift($publicSuffix, $part); |
||
72 | $psl = $psl[$part]; |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | if (array_key_exists('*', $psl)) { |
||
77 | array_unshift($publicSuffix, $part); |
||
78 | $psl = $psl['*']; |
||
79 | continue; |
||
80 | } |
||
81 | |||
82 | // Avoids improper parsing when $host's subdomain + public suffix === |
||
83 | // a valid public suffix (e.g. host 'us.example.com' and public suffix 'us.com') |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | // Apply algorithm rule #2: If no rules match, the prevailing rule is "*". |
||
88 | if (empty($publicSuffix)) { |
||
89 | $publicSuffix[0] = $parts[0]; |
||
90 | } |
||
91 | |||
92 | return implode('.', array_filter($publicSuffix, 'strlen')); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns registerable domain portion of provided host |
||
97 | * |
||
98 | * Per the test cases provided by Mozilla |
||
99 | * (http://mxr.mozilla.org/mozilla-central/source/netwerk/test/unit/data/test_psl.txt?raw=1), |
||
100 | * this method should return null if the domain provided is a public suffix. |
||
101 | * |
||
102 | * @param string $host host |
||
103 | * @return string registerable domain |
||
104 | */ |
||
105 | public function getRegisterableDomain($host) |
||
106 | { |
||
107 | if (strpos($host, '.') === false) { |
||
108 | return null; |
||
109 | } |
||
110 | |||
111 | $host = strtolower($host); |
||
112 | $publicSuffix = $this->getPublicSuffix($host); |
||
113 | |||
114 | if ($publicSuffix === null || $host == $publicSuffix) { |
||
115 | return null; |
||
116 | } |
||
117 | |||
118 | $publicSuffixParts = array_reverse(explode('.', $publicSuffix)); |
||
119 | $hostParts = array_reverse(explode('.', $host)); |
||
120 | $registerableDomainParts = array_slice($hostParts, 0, count($publicSuffixParts) + 1); |
||
121 | |||
122 | return implode('.', array_reverse($registerableDomainParts)); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns the subdomain portion of provided host |
||
127 | * |
||
128 | * @param string $host host |
||
129 | * @return string subdomain |
||
130 | */ |
||
131 | public function getSubdomain($host) |
||
132 | { |
||
133 | $host = strtolower($host); |
||
134 | $registerableDomain = $this->getRegisterableDomain($host); |
||
135 | |||
136 | if ($registerableDomain === null || $host == $registerableDomain) { |
||
137 | return null; |
||
138 | } |
||
139 | |||
140 | $registerableDomainParts = array_reverse(explode('.', $registerableDomain)); |
||
141 | $hostParts = array_reverse(explode('.', $host)); |
||
142 | $subdomainParts = array_slice($hostParts, count($registerableDomainParts)); |
||
143 | |||
144 | return implode('.', array_reverse($subdomainParts)); |
||
145 | } |
||
146 | } |