| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Support; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles server functions |
| 7 |
*/ |
| 8 |
class Server { |
| 9 |
/** |
| 10 |
* The url of the site. |
| 11 |
* |
| 12 |
* @return string |
| 13 |
*/ |
| 14 |
protected $url = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* Get the url of the site |
| 18 |
* |
| 19 |
* @param string $url The url for the site. |
| 20 |
*/ |
| 21 |
public function __construct( $url ) { |
| 22 |
$this->url = $url; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Are we on the localhost? |
| 27 |
* |
| 28 |
* @return boolean |
| 29 |
*/ |
| 30 |
public function isLocalHost() { |
| 31 |
// check the ip address. |
| 32 |
if ( self::isLocalIP() ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
// check the domain for .local or .test. |
| 37 |
if ( self::isLocalDomain() ) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Check if the site is a local domain (.local or .test.) |
| 46 |
* |
| 47 |
* @return boolean |
| 48 |
*/ |
| 49 |
public function isLocalDomain() { |
| 50 |
$parsed = explode( '.', wp_parse_url( $this->url, PHP_URL_HOST ) ?? '' ); |
| 51 |
return in_array( end( $parsed ), array( 'local', 'test' ), true ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Is this a local IP address? |
| 56 |
* |
| 57 |
* @return boolean |
| 58 |
*/ |
| 59 |
public function isLocalIP() { |
| 60 |
$ip_address = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? ''; |
| 61 |
return in_array( $ip_address, array( '127.0.0.1', '::1' ), true ); |
| 62 |
} |
| 63 |
} |
| 64 |
|