File indexing completed on 2024-12-22 05:36:24
0001 <?php 0002 0003 /** 0004 * ocs-webserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-webserver. 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 $domain 0030 * @param bool $debug 0031 * 0032 * @return string 0033 */ 0034 public static function get_domain($domain, $debug = false) 0035 { 0036 if (false == isset($domain)) { 0037 return null; 0038 } 0039 0040 $original = $domain = strtolower($domain); 0041 if (filter_var($domain, FILTER_VALIDATE_IP)) { 0042 return $domain; 0043 } 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 if (count($_sub) === 2) // two level TLD 0051 { 0052 $removed = array_shift($arr); 0053 if ($count === 4) // got a subdomain acting as a domain 0054 { 0055 $removed = array_shift($arr); 0056 } 0057 } else if (count($_sub) === 1) // one level TLD 0058 { 0059 $removed = array_shift($arr); //remove the subdomain 0060 if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters 0061 { 0062 array_unshift($arr, $removed); 0063 } else { 0064 // non country TLD according to IANA 0065 $tlds = array( 0066 'aero', 0067 'arpa', 0068 'asia', 0069 'biz', 0070 'cat', 0071 'com', 0072 'coop', 0073 'edu', 0074 'gov', 0075 'info', 0076 'jobs', 0077 'mil', 0078 'mobi', 0079 'museum', 0080 'name', 0081 'net', 0082 'org', 0083 'post', 0084 'pro', 0085 'tel', 0086 'travel', 0087 'xxx', 0088 ); 0089 if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country 0090 { 0091 array_shift($arr); 0092 } 0093 } 0094 } else // more than 3 levels, something is wrong 0095 { 0096 for ($i = count($_sub); $i > 1; $i--) { 0097 $removed = array_shift($arr); 0098 } 0099 } 0100 } else if (count($arr) === 2) { 0101 $arr0 = array_shift($arr); 0102 if (strpos(join('.', $arr), '.') === false 0103 && in_array($arr[0], array('localhost', 'test', 'invalid')) === false) // not a reserved domain 0104 { 0105 // seems invalid domain, restore it 0106 array_unshift($arr, $arr0); 0107 } 0108 } 0109 0110 return join('.', $arr); 0111 } 0112 0113 }