File indexing completed on 2025-03-02 05:29:09
0001 <?php 0002 0003 /** 0004 * ocs-apiserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-apiserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 * 0023 * Created: 18.11.2016 0024 **/ 0025 class Local_Tools_ParseDomain 0026 { 0027 0028 /** 0029 * @param bool $debug 0030 * @return string 0031 */ 0032 public static function get_domain($debug = false) 0033 { 0034 //Note the HTTP Host header may includes the port. 0035 //$host = strtok($_SERVER['HTTP_HOST'], ':'); 0036 //$host = parse_url($_SERVER['HTTP_HOST'],PHP_URL_HOST); 0037 list($host) = explode(':', $_SERVER['HTTP_HOST']); 0038 0039 $original = $domain = strtolower($host); 0040 if (filter_var($domain, FILTER_VALIDATE_IP)) { 0041 return $domain; 0042 } 0043 $debug ? print('<strong style="color:green">»</strong> Parsing: ' . $original) : false; 0044 $arr = array_slice(array_filter(explode('.', $domain, 4), function ($value) { 0045 return $value !== 'www'; 0046 }), 0); //rebuild array indexes 0047 if (count($arr) > 2) { 0048 $count = count($arr); 0049 $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]); 0050 $debug ? print(" (parts count: {$count})") : false; 0051 if (count($_sub) === 2) // two level TLD 0052 { 0053 $removed = array_shift($arr); 0054 if ($count === 4) // got a subdomain acting as a domain 0055 { 0056 $removed = array_shift($arr); 0057 } 0058 $debug ? print("<br>\n" . '[*] Two level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false; 0059 } elseif (count($_sub) === 1) // one level TLD 0060 { 0061 $removed = array_shift($arr); //remove the subdomain 0062 if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters 0063 { 0064 array_unshift($arr, $removed); 0065 } else { 0066 // non country TLD according to IANA 0067 $tlds = array( 0068 'aero', 0069 'arpa', 0070 'asia', 0071 'biz', 0072 'cat', 0073 'com', 0074 'coop', 0075 'edu', 0076 'gov', 0077 'info', 0078 'jobs', 0079 'mil', 0080 'mobi', 0081 'museum', 0082 'name', 0083 'net', 0084 'org', 0085 'post', 0086 'pro', 0087 'tel', 0088 'travel', 0089 'xxx', 0090 ); 0091 if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country 0092 { 0093 array_shift($arr); 0094 } 0095 } 0096 $debug ? print("<br>\n" . '[*] One level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false; 0097 } else // more than 3 levels, something is wrong 0098 { 0099 for ($i = count($_sub); $i > 1; $i--) { 0100 $removed = array_shift($arr); 0101 } 0102 $debug ? print("<br>\n" . '[*] Three level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false; 0103 } 0104 } elseif (count($arr) === 2) { 0105 $arr0 = array_shift($arr); 0106 if (strpos(join('.', $arr), '.') === false 0107 && in_array($arr[0], array('localhost', 'test', 'invalid')) === false 0108 ) // not a reserved domain 0109 { 0110 $debug ? print("<br>\n" . 'Seems invalid domain: <strong>' . join('.', 0111 $arr) . '</strong> re-adding: <strong>' . $arr0 . '</strong> ') : false; 0112 // seems invalid domain, restore it 0113 array_unshift($arr, $arr0); 0114 } 0115 } 0116 $debug ? print("<br>\n" . '<strong style="color:gray">«</strong> Done parsing: <span style="color:red">' . $original . '</span> as <span style="color:blue">' . join('.', 0117 $arr) . "</span><br>\n") : false; 0118 0119 return join('.', $arr); 0120 } 0121 0122 }