22
33class Servery
44{
5+ /**
6+ * Localhost names and IPs.
7+ */
8+ const LOCAL_HOSTS = ['localhost ' , '127.0.0.1 ' , '::1 ' ];
9+
10+ /**
11+ * Checks if the current environment is running locally.
12+ *
13+ * @return bool
14+ */
15+ public static function isLocalHost (): bool
16+ {
17+ $ remote_addr = $ _SERVER ['REMOTE_ADDR ' ] ?? null ;
18+
19+ if (empty ($ remote_addr ) || in_array ($ remote_addr , static ::LOCAL_HOSTS , true )) {
20+ return true ;
21+ }
22+
23+ $ server_name = $ _SERVER ['SERVER_NAME ' ] ?? '' ;
24+ $ http_host = $ _SERVER ['HTTP_HOST ' ] ?? '' ;
25+
26+ if (in_array (strtolower ($ server_name ), static ::LOCAL_HOSTS , true ) || in_array (strtolower ($ http_host ), static ::LOCAL_HOSTS , true )) {
27+ return true ;
28+ }
29+
30+ if (filter_var ($ remote_addr , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
31+ $ parts = explode ('. ' , $ remote_addr );
32+
33+ if ($ parts [0 ] === '10 ' ) {
34+ return true ;
35+ }
36+
37+ if ($ parts [0 ] === '172 ' && $ parts [1 ] >= 16 && $ parts [1 ] <= 31 ) {
38+ return true ;
39+ }
40+
41+ if ($ parts [0 ] === '192 ' && $ parts [1 ] === '168 ' ) {
42+ return true ;
43+ }
44+ }
45+
46+ if (strpos ($ remote_addr , '::1 ' ) === 0 ) {
47+ return true ;
48+ }
49+
50+ return false ;
51+ }
52+
53+ /**
54+ * @return string
55+ */
556 public static function getWebServer (): string
657 {
7- return $ _SERVER ['SERVER_NAME ' ] ?? '' ;
58+ if (!empty ($ _SERVER ['SERVER_NAME ' ]) && is_string ($ _SERVER ['SERVER_NAME ' ])) {
59+ return $ _SERVER ['SERVER_NAME ' ];
60+ }
61+
62+ return 'localhost ' ;
863 }
964
1065 /**
@@ -149,8 +204,7 @@ public static function getDomain(): string
149204 */
150205 public static function isHttps (): bool
151206 {
152- return (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off '
153- || $ _SERVER ['SERVER_PORT ' ] == 443 );
207+ return (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' || $ _SERVER ['SERVER_PORT ' ] == 443 );
154208 }
155209
156210 /**
@@ -163,6 +217,7 @@ public static function getUrlProtocol(): string
163217 if (static ::isHttps ()) {
164218 return 'https ' ;
165219 }
220+
166221 return 'http ' ;
167222 }
168223
@@ -174,9 +229,11 @@ public static function getUrlProtocol(): string
174229 public static function getServerProtocol (): string
175230 {
176231 $ protocol = $ _SERVER ['SERVER_PROTOCOL ' ] ?? '' ;
232+
177233 if (!in_array ($ protocol , ['HTTP/1.1 ' , 'HTTP/2 ' , 'HTTP/2.0 ' , 'HTTP/3 ' ], true )) {
178234 $ protocol = 'HTTP/1.0 ' ;
179235 }
236+
180237 return $ protocol ;
181238 }
182239
@@ -233,8 +290,10 @@ public static function getRequrestedPath(): ?string
233290 $ path = $ _SERVER ['SCRIPT_FILENAME ' ];
234291 $ path = dirname ($ path );
235292 $ path = rtrim ($ path , '/ ' );
293+
236294 return $ path ;
237295 }
296+
238297 return null ;
239298 }
240299
@@ -246,9 +305,11 @@ public static function getRequrestedPath(): ?string
246305 public static function getHomePath (): string
247306 {
248307 $ path = static ::getRequrestedPath ();
308+
249309 if ($ path === null ) {
250310 $ path = __DIR__ .'/../../../.. ' ; // from vendor/nabeghe/servery/src
251311 }
312+
252313 return $ path ;
253314 }
254315
@@ -260,18 +321,31 @@ public static function getHomePath(): string
260321 public static function getHomeUrl (): ?string
261322 {
262323 $ path = static ::getRequrestedPath ();
324+
263325 if ($ path === null ) {
264326 return null ;
265327 }
328+
266329 $ home_url = static ::getRootUrl ().($ path ? "/ $ path " : '' );
330+
267331 return $ home_url ;
268332 }
269333
334+ /**
335+ * An alias for {@see UserAgent::$instance()}.
336+ *
337+ * @return UserAgent
338+ */
270339 public static function getUserAgent (): UserAgent
271340 {
272341 return UserAgent::instance ();
273342 }
274343
344+ /**
345+ * An alias for {@see UserAgent::getCurrentReal()}.
346+ *
347+ * @return string
348+ */
275349 public static function getUserAgentValue (): string
276350 {
277351 return UserAgent::getCurrentReal ();
0 commit comments