Skip to content

Commit 984fd02

Browse files
committed
included public suffix #150
1 parent f02a707 commit 984fd02

File tree

5 files changed

+19647
-11
lines changed

5 files changed

+19647
-11
lines changed

.gitattributes

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/demo export-ignore
2-
/tests export-ignore
3-
.gitattributes export-ignore
4-
.gitignore export-ignore
5-
.travis.yml export-ignore
6-
composer.lock export-ignore
7-
phpunit.xml export-ignore
1+
/demo export-ignore
2+
/tests export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
.travis.yml export-ignore
6+
composer.lock export-ignore
7+
phpunit.xml export-ignore
8+
src/resources/public_suffix_list.dat export-ignore
9+
src/resources/dat2php.php export-ignore

src/Url.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Url
1010
protected $info;
1111

1212
public static $validate = false;
13+
private static $public_suffix_list;
1314

1415
/**
1516
* Constructor. Sets the url.
@@ -181,13 +182,14 @@ public function getDomain($first_level = false)
181182
return $first_level ? ($host[1].'.'.$host[0]) : $host[1];
182183

183184
default:
184-
$subdomains = ['co', 'com', 'org'];
185+
$tld = $host[1].'.'.$host[0];
186+
$suffixes = self::getSuffixes();
185187

186-
if ($first_level) {
187-
return in_array($host[1], $subdomains, true) ? ($host[2].'.'.$host[1].'.'.$host[0]) : ($host[1].'.'.$host[0]);
188+
if (in_array($tld, $suffixes, true)) {
189+
return $first_level ? $host[2].'.'.$tld : $host[2];
188190
}
189191

190-
return in_array($host[1], $subdomains, true) ? $host[2] : $host[1];
192+
return $first_level ? $host[1].'.'.$host[0] : $host[1];
191193
}
192194
}
193195

@@ -561,4 +563,13 @@ private function setPath($path)
561563
}
562564
}
563565
}
566+
567+
private function getSuffixes()
568+
{
569+
if (self::$public_suffix_list === null) {
570+
self::$public_suffix_list = include __DIR__.'/resources/public_suffix_list.php';
571+
}
572+
573+
return self::$public_suffix_list;
574+
}
564575
}

src/resources/dat2php.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$input = __DIR__.'/public_suffix_list.dat';
4+
$output = __DIR__.'/public_suffix_list.php';
5+
6+
$lines = file($input, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
7+
8+
$lines = array_filter($lines, function ($line) {
9+
return $line[0] !== '/';
10+
});
11+
12+
$php = "<?php\n\nreturn ".var_export(array_values($lines), true).";\n";
13+
$php = str_replace('array (', 'array(', $php);
14+
15+
file_put_contents($output, $php);

0 commit comments

Comments
 (0)