Test Failed
Push — master ( 890a1b...d82040 )
by Justin
43:15 queued 39:27
created

DomainUtils   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 30
c 0
b 0
f 0
dl 0
loc 145
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isProxyUsed() 0 2 2
A isHTTPS() 0 2 3
A getReferer() 0 2 1
A getDomain() 0 2 1
A getRequestURI() 0 2 1
A getTLD() 0 8 1
A getPort() 0 2 1
C getHost() 0 24 7
A getRequestMethod() 0 2 1
A generateURL() 0 16 3
A getURL() 0 2 1
A getProtocol() 0 5 2
B getBaseURL() 0 20 5
A getCurrentDomain() 0 17 1
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 05.03.2018
25
 * Time: 01:12
26
 */
27
28
class DomainUtils {
29
30
	public static function getTLD ($url) {
31
		$domain_tld = "";
32
33
		//http://news.mullerdigital.com/2013/10/30/how-to-get-the-domain-and-tld-from-a-url-using-php-and-regular-expression/
34
35
		preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), $domain_tld);
0 ignored issues
show
Bug introduced by
$domain_tld of type string is incompatible with the type null|array expected by parameter $matches of preg_match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
		preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), /** @scrutinizer ignore-type */ $domain_tld);
Loading history...
36
37
		return $domain_tld[0];
38
	}
39
40
	public static function isHTTPS () {
41
		return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off";
42
	}
43
44
	public static function getPort () {
45
		return (int) $_SERVER['SERVER_PORT'];
46
	}
47
48
	public static function isProxyUsed () {
49
		return isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST']);
50
	}
51
52
	public static function getHost () {
53
		$host = "";
54
55
		if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
56
			$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
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
67
68
			//use server ip
69
			return htmlentities($_SERVER['SERVER_ADDR']);
70
		}
71
72
		// Remove port number from host
73
		$host = preg_replace("%:\d+$%", "", $host);
74
75
		return trim($host);
76
	}
77
78
	/**
79
	 * get domain
80
	 *
81
	 * alias to getHost()
82
	 */
83
	public static function getDomain () {
84
		return self::getHost();
85
	}
86
87
	public static function getReferer () {
88
		return htmlentities($_SERVER['HTTP_REFERER']);
89
	}
90
91
	public static function getRequestMethod () {
92
		return htmlspecialchars($_SERVER['REQUEST_METHOD']);
93
	}
94
95
	public static function getRequestURI () {
96
		return htmlentities($_SERVER['REQUEST_URI']);
97
	}
98
99
	public static function getBaseURL (bool $without_protocol = false) {
100
		$url = "";
101
102
		if (!$without_protocol) {//add protocol
103
			if (self::isHTTPS()) {
104
				$url .= "https://";
105
			} else {
106
				$url .= "http://";
107
			}
108
		}
109
110
		//add domain
111
		$url .= self::getDomain();
112
113
		//check, if an specific server port is used
114
		if (self::getPort() != 80 && self::getPort() != 433) {
115
			$url .= ":" . self::getPort();
116
		}
117
118
		return $url;
119
	}
120
121
	/**
122
	 * generate an url for a page in this form: http(s)://<Domain><Base URL><Page>
123
	 */
124
	public static function generateURL (string $page, array $params = array()) : string {
125
		$params_str = "";
126
127
		if (count($params) > 0) {
128
			$params_str = "?";
129
130
			$array = "";
131
132
			foreach ($params as $key=>$value) {
133
				$array[] = $key . "=" . $value;
134
			}
135
136
			$params_str .= implode("&amp;", $array);
0 ignored issues
show
Bug introduced by
$array of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
			$params_str .= implode("&amp;", /** @scrutinizer ignore-type */ $array);
Loading history...
137
		}
138
139
		return self::getBaseURL() . "/" . $page . $params_str;
140
	}
141
142
	public static function getURL () {
143
		return self::getBaseURL() . self::getRequestURI();
144
	}
145
146
	/**
147
	 * faster implementation of getTld()
148
	 */
149
	public static function getCurrentDomain () {
150
		$host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
151
		$domain = explode("?", $host);
152
		$host = $domain[0];
153
		$array = explode("/", $host);
154
		$host = $array[0];
155
156
		/*$domain = "";
157
158
		for ($i = 0; $i < count($array) - 1; $i++) {
159
			$domain .= $array[$i];
160
		}*/
161
162
		$array1 = explode(":", $host);
163
		$host = $array1[0];
164
165
		return /*$domain*/$host;
166
	}
167
168
	public static function getProtocol () : string {
169
		if (self::isHTTPS()) {
170
			return "https://";
171
		} else {
172
			return "http://";
173
		}
174
	}
175
176
}
177
178
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
179