diff --git a/metager/app/Models/Result.php b/metager/app/Models/Result.php index f142e7273028a7022f9d16d968b2c6345a65ba7c..e6c8c787c9dadf2defc45c8d0d26c560a844b5b1 100644 --- a/metager/app/Models/Result.php +++ b/metager/app/Models/Result.php @@ -406,12 +406,8 @@ class Result $re["web"] = "www."; $re["host"] = substr($re["host"], strpos($re["host"], ".") + 1); } - $hostParts = explode(".", $re["host"]); - if (sizeof($hostParts) >= 3) { - $re["domain"] = $hostParts[sizeof($hostParts) - 2] . "." . $hostParts[sizeof($hostParts) - 1]; - } else { - $re["domain"] = $re["host"]; - } + $re["domain"] = $this->get_domain($re["host"]); + $re["port"] = empty($parts["port"]) ? ($re["schema"] === "http" ? 80 : ($re["schema"] === "https" ? 443 : 80)) : $parts["port"]; $re["path"] = empty($parts["path"]) ? "" : $parts["path"]; $re["query"] = empty($parts["query"]) ? "" : $parts["query"]; @@ -420,6 +416,63 @@ class Result return $re; } + /** + * @param string $domain Pass $_SERVER['SERVER_NAME'] here + * @param bool $debug + * + * @debug bool $debug + * @return string + */ + private function get_domain($domain) + { + $domain = strtolower($domain); + if (filter_var($domain, FILTER_VALIDATE_IP)) { + return $domain; + } + + $arr = array_slice(array_filter(explode('.', $domain, 4), function ($value) { + return $value !== 'www'; + }), 0); //rebuild array indexes + + if (count($arr) > 2) { + $count = count($arr); + $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]); + + + if (count($_sub) === 2) { // two level TLD + $removed = array_shift($arr); + if ($count === 4) // got a subdomain acting as a domain + $removed = array_shift($arr); + } elseif (count($_sub) === 1) { // one level TLD + $removed = array_shift($arr); //remove the subdomain + if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters + array_unshift($arr, $removed); + else { + // non country TLD according to IANA + $tlds = array('aero', 'arpa', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'post', 'pro', 'tel', 'travel', 'xxx',); + if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) { //special TLD don't have a country + array_shift($arr); + } + } + } else { // more than 3 levels, something is wrong + for ($i = count($_sub); $i > 1; $i--) + $removed = array_shift($arr); + } + } elseif (count($arr) === 2) { + $arr0 = array_shift($arr); + if ( + strpos(join('.', $arr), '.') === false + && in_array($arr[0], array('localhost', 'test', 'invalid')) === false + ) // not a reserved domain + { + // seems invalid domain, restore it + array_unshift($arr, $arr0); + } + } + + return join('.', $arr); + } + # Getter public function getRank()