@@ -27,157 +27,157 @@ |
||
| 27 | 27 | |
| 28 | 28 | class DomainUtils { |
| 29 | 29 | |
| 30 | - public static function getTLD ($url) { |
|
| 31 | - $domain_tld = ""; |
|
| 30 | + public static function getTLD ($url) { |
|
| 31 | + $domain_tld = ""; |
|
| 32 | 32 | |
| 33 | - //http://news.mullerdigital.com/2013/10/30/how-to-get-the-domain-and-tld-from-a-url-using-php-and-regular-expression/ |
|
| 33 | + //http://news.mullerdigital.com/2013/10/30/how-to-get-the-domain-and-tld-from-a-url-using-php-and-regular-expression/ |
|
| 34 | 34 | |
| 35 | - preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), $domain_tld); |
|
| 35 | + preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), $domain_tld); |
|
| 36 | 36 | |
| 37 | - return $domain_tld[0]; |
|
| 38 | - } |
|
| 37 | + return $domain_tld[0]; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public static function isHTTPS () { |
|
| 41 | - return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off"; |
|
| 42 | - } |
|
| 40 | + public static function isHTTPS () { |
|
| 41 | + return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off"; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public static function getPort () { |
|
| 45 | - return (int) $_SERVER['SERVER_PORT']; |
|
| 46 | - } |
|
| 44 | + public static function getPort () { |
|
| 45 | + return (int) $_SERVER['SERVER_PORT']; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public static function isProxyUsed () { |
|
| 49 | - return isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST']); |
|
| 50 | - } |
|
| 48 | + public static function isProxyUsed () { |
|
| 49 | + return isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST']); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public static function getHost () { |
|
| 53 | - $host = ""; |
|
| 52 | + public static function getHost () { |
|
| 53 | + $host = ""; |
|
| 54 | 54 | |
| 55 | - if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 56 | - $host = $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 55 | + if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 56 | + $host = $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 57 | 57 | |
| 58 | - //because HTTP_X_FORWARDED_HOST can contains more than 1 host, we only want to get the last host name |
|
| 59 | - $elements = explode(',', $host); |
|
| 60 | - $host = end($elements); |
|
| 61 | - } else if (isset($_SERVER['SERVER_NAME']) && !empty($_SERVER['SERVER_NAME'])) { |
|
| 62 | - $host = $_SERVER['SERVER_NAME']; |
|
| 63 | - } else if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { |
|
| 64 | - $host = $_SERVER['HTTP_HOST']; |
|
| 65 | - } else { |
|
| 66 | - //unknown host |
|
| 58 | + //because HTTP_X_FORWARDED_HOST can contains more than 1 host, we only want to get the last host name |
|
| 59 | + $elements = explode(',', $host); |
|
| 60 | + $host = end($elements); |
|
| 61 | + } else if (isset($_SERVER['SERVER_NAME']) && !empty($_SERVER['SERVER_NAME'])) { |
|
| 62 | + $host = $_SERVER['SERVER_NAME']; |
|
| 63 | + } else if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) { |
|
| 64 | + $host = $_SERVER['HTTP_HOST']; |
|
| 65 | + } else { |
|
| 66 | + //unknown host |
|
| 67 | 67 | |
| 68 | - //use server ip |
|
| 69 | - return htmlentities($_SERVER['SERVER_ADDR']); |
|
| 70 | - } |
|
| 68 | + //use server ip |
|
| 69 | + return htmlentities($_SERVER['SERVER_ADDR']); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - //TODO: remove this code |
|
| 73 | - /*if ($host == "cms.chipbyte.de") { |
|
| 72 | + //TODO: remove this code |
|
| 73 | + /*if ($host == "cms.chipbyte.de") { |
|
| 74 | 74 | echo "Debugging: Invalide host!"; |
| 75 | 75 | var_dump($_SERVER); |
| 76 | 76 | }*/ |
| 77 | 77 | |
| 78 | - // Remove port number from host |
|
| 79 | - $host = preg_replace("%:\d+$%", "", $host); |
|
| 78 | + // Remove port number from host |
|
| 79 | + $host = preg_replace("%:\d+$%", "", $host); |
|
| 80 | 80 | |
| 81 | - return trim($host); |
|
| 82 | - } |
|
| 81 | + return trim($host); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * get domain |
|
| 86 | - * |
|
| 87 | - * alias to getHost() |
|
| 88 | - */ |
|
| 89 | - public static function getDomain () { |
|
| 90 | - return self::getHost(); |
|
| 91 | - } |
|
| 84 | + /** |
|
| 85 | + * get domain |
|
| 86 | + * |
|
| 87 | + * alias to getHost() |
|
| 88 | + */ |
|
| 89 | + public static function getDomain () { |
|
| 90 | + return self::getHost(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - public static function getReferer () { |
|
| 94 | - return htmlentities($_SERVER['HTTP_REFERER']); |
|
| 95 | - } |
|
| 93 | + public static function getReferer () { |
|
| 94 | + return htmlentities($_SERVER['HTTP_REFERER']); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - public static function getRequestMethod () { |
|
| 98 | - return htmlspecialchars($_SERVER['REQUEST_METHOD']); |
|
| 99 | - } |
|
| 97 | + public static function getRequestMethod () { |
|
| 98 | + return htmlspecialchars($_SERVER['REQUEST_METHOD']); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - public static function getRequestURI () { |
|
| 102 | - return htmlentities($_SERVER['REQUEST_URI']); |
|
| 103 | - } |
|
| 101 | + public static function getRequestURI () { |
|
| 102 | + return htmlentities($_SERVER['REQUEST_URI']); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - public static function getBaseURL (bool $without_protocol = false) { |
|
| 106 | - $url = ""; |
|
| 105 | + public static function getBaseURL (bool $without_protocol = false) { |
|
| 106 | + $url = ""; |
|
| 107 | 107 | |
| 108 | - if (!$without_protocol) {//add protocol |
|
| 109 | - if (self::isHTTPS()) { |
|
| 110 | - $url .= "https://"; |
|
| 111 | - } else { |
|
| 112 | - $url .= "http://"; |
|
| 113 | - } |
|
| 114 | - } |
|
| 108 | + if (!$without_protocol) {//add protocol |
|
| 109 | + if (self::isHTTPS()) { |
|
| 110 | + $url .= "https://"; |
|
| 111 | + } else { |
|
| 112 | + $url .= "http://"; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - //add domain |
|
| 117 | - $url .= self::getDomain(); |
|
| 116 | + //add domain |
|
| 117 | + $url .= self::getDomain(); |
|
| 118 | 118 | |
| 119 | - //check, if an specific server port is used |
|
| 120 | - if (self::getPort() != 80 && self::getPort() != 433) { |
|
| 121 | - $url .= ":" . self::getPort(); |
|
| 122 | - } |
|
| 119 | + //check, if an specific server port is used |
|
| 120 | + if (self::getPort() != 80 && self::getPort() != 433) { |
|
| 121 | + $url .= ":" . self::getPort(); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - return $url; |
|
| 125 | - } |
|
| 124 | + return $url; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * generate an url for a page in this form: http(s)://<Domain><Base URL><Page> |
|
| 129 | - */ |
|
| 130 | - public static function generateURL (string $page, array $params = array()) : string { |
|
| 131 | - $params_str = ""; |
|
| 127 | + /** |
|
| 128 | + * generate an url for a page in this form: http(s)://<Domain><Base URL><Page> |
|
| 129 | + */ |
|
| 130 | + public static function generateURL (string $page, array $params = array()) : string { |
|
| 131 | + $params_str = ""; |
|
| 132 | 132 | |
| 133 | - if (count($params) > 0) { |
|
| 134 | - $params_str = "?"; |
|
| 133 | + if (count($params) > 0) { |
|
| 134 | + $params_str = "?"; |
|
| 135 | 135 | |
| 136 | - $array = ""; |
|
| 136 | + $array = ""; |
|
| 137 | 137 | |
| 138 | - foreach ($params as $key=>$value) { |
|
| 139 | - $array[] = $key . "=" . $value; |
|
| 140 | - } |
|
| 138 | + foreach ($params as $key=>$value) { |
|
| 139 | + $array[] = $key . "=" . $value; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - $params_str .= implode("&", $array); |
|
| 143 | - } |
|
| 142 | + $params_str .= implode("&", $array); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - return self::getBaseURL() . "/" . $page . $params_str; |
|
| 146 | - } |
|
| 145 | + return self::getBaseURL() . "/" . $page . $params_str; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - public static function getURL () { |
|
| 149 | - return self::getBaseURL() . self::getRequestURI(); |
|
| 150 | - } |
|
| 148 | + public static function getURL () { |
|
| 149 | + return self::getBaseURL() . self::getRequestURI(); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * faster implementation of getTld() |
|
| 154 | - */ |
|
| 155 | - public static function getCurrentDomain () { |
|
| 156 | - $host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
| 157 | - $domain = explode("?", $host); |
|
| 158 | - $host = $domain[0]; |
|
| 159 | - $array = explode("/", $host); |
|
| 160 | - $host = $array[0]; |
|
| 152 | + /** |
|
| 153 | + * faster implementation of getTld() |
|
| 154 | + */ |
|
| 155 | + public static function getCurrentDomain () { |
|
| 156 | + $host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
| 157 | + $domain = explode("?", $host); |
|
| 158 | + $host = $domain[0]; |
|
| 159 | + $array = explode("/", $host); |
|
| 160 | + $host = $array[0]; |
|
| 161 | 161 | |
| 162 | - /*$domain = ""; |
|
| 162 | + /*$domain = ""; |
|
| 163 | 163 | |
| 164 | 164 | for ($i = 0; $i < count($array) - 1; $i++) { |
| 165 | 165 | $domain .= $array[$i]; |
| 166 | 166 | }*/ |
| 167 | 167 | |
| 168 | - $array1 = explode(":", $host); |
|
| 169 | - $host = $array1[0]; |
|
| 168 | + $array1 = explode(":", $host); |
|
| 169 | + $host = $array1[0]; |
|
| 170 | 170 | |
| 171 | - return /*$domain*/$host; |
|
| 172 | - } |
|
| 171 | + return /*$domain*/$host; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - public static function getProtocol () : string { |
|
| 175 | - if (self::isHTTPS()) { |
|
| 176 | - return "https://"; |
|
| 177 | - } else { |
|
| 178 | - return "http://"; |
|
| 179 | - } |
|
| 180 | - } |
|
| 174 | + public static function getProtocol () : string { |
|
| 175 | + if (self::isHTTPS()) { |
|
| 176 | + return "https://"; |
|
| 177 | + } else { |
|
| 178 | + return "http://"; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | } |
| 183 | 183 | |