| 1 |
<?php |
| 2 |
|
| 3 |
class Redirection_Request { |
| 4 |
public static function get_request_headers() { |
| 5 |
$ignore = apply_filters( 'redirection_request_headers_ignore', [ |
| 6 |
'cookie', |
| 7 |
'host', |
| 8 |
] ); |
| 9 |
$headers = []; |
| 10 |
|
| 11 |
foreach ( $_SERVER as $name => $value ) { |
| 12 |
if ( substr( $name, 0, 5 ) === 'HTTP_' ) { |
| 13 |
$name = strtolower( substr( $name, 5 ) ); |
| 14 |
$name = str_replace( '_', ' ', $name ); |
| 15 |
$name = ucwords( $name ); |
| 16 |
$name = str_replace( ' ', '-', $name ); |
| 17 |
|
| 18 |
if ( ! in_array( strtolower( $name ), $ignore, true ) ) { |
| 19 |
$headers[ $name ] = $value; |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return apply_filters( 'redirection_request_headers', $headers ); |
| 25 |
} |
| 26 |
|
| 27 |
public static function get_request_method() { |
| 28 |
$method = ''; |
| 29 |
|
| 30 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) ) { |
| 31 |
$method = $_SERVER['REQUEST_METHOD']; |
| 32 |
} |
| 33 |
|
| 34 |
return apply_filters( 'redirection_request_method', $method ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the server name (from $_SERVER['SERVER_NAME]), or use the request name ($_SERVER['HTTP_HOST']) if not present |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
public static function get_server_name() { |
| 43 |
$host = self::get_request_server_name(); |
| 44 |
|
| 45 |
if ( isset( $_SERVER['SERVER_NAME'] ) ) { |
| 46 |
$host = $_SERVER['SERVER_NAME']; |
| 47 |
} |
| 48 |
|
| 49 |
return apply_filters( 'redirection_request_server', $host ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the request server name (from $_SERVER['HTTP_HOST]) |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public static function get_request_server_name() { |
| 58 |
$host = ''; |
| 59 |
|
| 60 |
if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
| 61 |
$host = $_SERVER['HTTP_HOST']; |
| 62 |
} |
| 63 |
|
| 64 |
return apply_filters( 'redirection_request_server_host', $host ); |
| 65 |
} |
| 66 |
|
| 67 |
public static function get_server() { |
| 68 |
return self::get_protocol() . '://' . self::get_server_name(); |
| 69 |
} |
| 70 |
|
| 71 |
public static function get_protocol() { |
| 72 |
return is_ssl() ? 'https' : 'http'; |
| 73 |
} |
| 74 |
|
| 75 |
public static function get_request_url() { |
| 76 |
$url = ''; |
| 77 |
|
| 78 |
if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 79 |
$url = $_SERVER['REQUEST_URI']; |
| 80 |
} |
| 81 |
|
| 82 |
return apply_filters( 'redirection_request_url', stripslashes( $url ) ); |
| 83 |
} |
| 84 |
|
| 85 |
public static function get_user_agent() { |
| 86 |
$agent = ''; |
| 87 |
|
| 88 |
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 89 |
$agent = $_SERVER['HTTP_USER_AGENT']; |
| 90 |
} |
| 91 |
|
| 92 |
return apply_filters( 'redirection_request_agent', $agent ); |
| 93 |
} |
| 94 |
|
| 95 |
public static function get_referrer() { |
| 96 |
$referrer = ''; |
| 97 |
|
| 98 |
if ( isset( $_SERVER['HTTP_REFERER'] ) ) { |
| 99 |
$referrer = $_SERVER['HTTP_REFERER']; |
| 100 |
} |
| 101 |
|
| 102 |
return apply_filters( 'redirection_request_referrer', $referrer ); |
| 103 |
} |
| 104 |
|
| 105 |
public static function get_ip_headers() { |
| 106 |
return [ |
| 107 |
'HTTP_CF_CONNECTING_IP', |
| 108 |
'HTTP_CLIENT_IP', |
| 109 |
'HTTP_X_FORWARDED_FOR', |
| 110 |
'HTTP_X_FORWARDED', |
| 111 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 112 |
'HTTP_FORWARDED_FOR', |
| 113 |
'HTTP_FORWARDED', |
| 114 |
'HTTP_VIA', |
| 115 |
'REMOTE_ADDR', |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
public static function get_ip() { |
| 120 |
$ip = ''; |
| 121 |
|
| 122 |
foreach ( self::get_ip_headers() as $var ) { |
| 123 |
if ( ! empty( $_SERVER[ $var ] ) ) { |
| 124 |
$ip = $_SERVER[ $var ]; |
| 125 |
$ip = explode( ',', $ip ); |
| 126 |
$ip = array_shift( $ip ); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Convert to binary |
| 132 |
// phpcs:ignore |
| 133 |
$ip = @inet_pton( trim( $ip ) ); |
| 134 |
if ( $ip !== false ) { |
| 135 |
// phpcs:ignore |
| 136 |
$ip = @inet_ntop( $ip ); // Convert back to string |
| 137 |
} |
| 138 |
|
| 139 |
return apply_filters( 'redirection_request_ip', $ip ? $ip : '' ); |
| 140 |
} |
| 141 |
|
| 142 |
public static function get_cookie( $cookie ) { |
| 143 |
if ( isset( $_COOKIE[ $cookie ] ) ) { |
| 144 |
return apply_filters( 'redirection_request_cookie', $_COOKIE[ $cookie ], $cookie ); |
| 145 |
} |
| 146 |
|
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
public static function get_header( $name ) { |
| 151 |
$name = 'HTTP_' . strtoupper( $name ); |
| 152 |
$name = str_replace( '-', '_', $name ); |
| 153 |
|
| 154 |
if ( isset( $_SERVER[ $name ] ) ) { |
| 155 |
return apply_filters( 'redirection_request_header', $_SERVER[ $name ], $name ); |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
public static function get_accept_language() { |
| 162 |
if ( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { |
| 163 |
$languages = preg_replace( '/;.*$/', '', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); |
| 164 |
$languages = str_replace( ' ', '', $languages ); |
| 165 |
return apply_filters( 'redirection_request_accept_language', explode( ',', $languages ) ); |
| 166 |
} |
| 167 |
|
| 168 |
return []; |
| 169 |
} |
| 170 |
} |
| 171 |
|