|
7 | 7 | * @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall)
|
8 | 8 | * @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
|
9 | 9 | */
|
10 |
| - |
11 | 10 | namespace Pdp;
|
12 | 11 |
|
13 | 12 | use Pdp\Uri\Url;
|
|
21 | 20 | class Parser
|
22 | 21 | {
|
23 | 22 | const SCHEME_PATTERN = '#^(http|ftp)s?://#i';
|
| 23 | + const IP_ADDRESS_PATTERN = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; |
24 | 24 |
|
25 | 25 | /**
|
26 | 26 | * @var PublicSuffixList Public Suffix List
|
@@ -104,7 +104,8 @@ public function parseHost($host)
|
104 | 104 |
|
105 | 105 | // Fixes #22: Single label domains are set as Host::$host and all other
|
106 | 106 | // properties are null.
|
107 |
| - if (strpos($host, '.') !== false) { |
| 107 | + // Fixes #43: Ip Addresses should not be parsed |
| 108 | + if ($this->isMutliLabelDomain($host) || !$this->isIpv4Address($host)) { |
108 | 109 | $subdomain = $this->getSubdomain($host);
|
109 | 110 | $registerableDomain = $this->getRegisterableDomain($host);
|
110 | 111 | $publicSuffix = $this->getPublicSuffix($host);
|
@@ -186,7 +187,12 @@ public function getPublicSuffix($host)
|
186 | 187 | // Fixes #22: If a single label domain makes it this far (e.g.,
|
187 | 188 | // localhost, foo, etc.), this stops it from incorrectly being set as
|
188 | 189 | // the public suffix.
|
189 |
| - if (strpos($host, '.') === false) { |
| 190 | + if (!$this->isMutliLabelDomain($host)) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + // Fixes #43 |
| 195 | + if ($this->isIpv4Address($host)) { |
190 | 196 | return;
|
191 | 197 | }
|
192 | 198 |
|
@@ -304,4 +310,30 @@ protected function denormalize($part)
|
304 | 310 |
|
305 | 311 | return $part;
|
306 | 312 | }
|
| 313 | + |
| 314 | + /** |
| 315 | + * Tests host for presence of '.' |
| 316 | + * |
| 317 | + * Related to #22 |
| 318 | + * |
| 319 | + * @param string $host Host part of url |
| 320 | + * @return bool True if multi-label domain, false otherwise |
| 321 | + */ |
| 322 | + protected function isMutliLabelDomain($host) |
| 323 | + { |
| 324 | + return strpos($host, '.') !== false; |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * Tests host to determine if it is an IP address |
| 329 | + * |
| 330 | + * Related to #43 |
| 331 | + * |
| 332 | + * @param string $host Host part of url |
| 333 | + * @return bool True if host is an ip address, false otherwise |
| 334 | + */ |
| 335 | + protected function isIpv4Address($host) |
| 336 | + { |
| 337 | + return preg_match(self::IP_ADDRESS_PATTERN, $host) === 1; |
| 338 | + } |
307 | 339 | }
|
0 commit comments