| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/ip.php'; |
| 4 |
|
| 5 |
class Redirection_Request { |
| 6 |
/** |
| 7 |
* URL friendly sanitize_text_fields which lets encoded characters through and doesn't trim |
| 8 |
* |
| 9 |
* @param string $value Value. |
| 10 |
* @return string |
| 11 |
*/ |
| 12 |
public static function sanitize_url( $value ) { |
| 13 |
// Remove invalid UTF |
| 14 |
$url = wp_check_invalid_utf8( $value, true ); |
| 15 |
|
| 16 |
// No new lines |
| 17 |
$url = preg_replace( "/[\r\n\t].*?$/s", '', $url ); |
| 18 |
|
| 19 |
// Clean control codes |
| 20 |
$url = preg_replace( '/[^\PC\s]/u', '', $url ); |
| 21 |
|
| 22 |
return $url; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get HTTP headers |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function get_request_headers() { |
| 31 |
$ignore = apply_filters( 'redirection_request_headers_ignore', [ |
| 32 |
'cookie', |
| 33 |
'host', |
| 34 |
] ); |
| 35 |
$headers = []; |
| 36 |
|
| 37 |
foreach ( $_SERVER as $name => $value ) { |
| 38 |
$value = sanitize_text_field( $value ); |
| 39 |
$name = sanitize_text_field( $name ); |
| 40 |
|
| 41 |
if ( substr( $name, 0, 5 ) === 'HTTP_' ) { |
| 42 |
$name = strtolower( substr( $name, 5 ) ); |
| 43 |
$name = str_replace( '_', ' ', $name ); |
| 44 |
$name = ucwords( $name ); |
| 45 |
$name = str_replace( ' ', '-', $name ); |
| 46 |
|
| 47 |
if ( ! in_array( strtolower( $name ), $ignore, true ) ) { |
| 48 |
$headers[ $name ] = $value; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return apply_filters( 'redirection_request_headers', $headers ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get request method |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public static function get_request_method() { |
| 62 |
$method = ''; |
| 63 |
|
| 64 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ) { |
| 65 |
$method = sanitize_text_field( $_SERVER['REQUEST_METHOD'] ); |
| 66 |
} |
| 67 |
|
| 68 |
return apply_filters( 'redirection_request_method', $method ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the server name (from $_SERVER['SERVER_NAME]), or use the request name ($_SERVER['HTTP_HOST']) if not present |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public static function get_server_name() { |
| 77 |
$host = self::get_request_server_name(); |
| 78 |
|
| 79 |
if ( isset( $_SERVER['SERVER_NAME'] ) && is_string( $_SERVER['SERVER_NAME'] ) ) { |
| 80 |
$host = sanitize_text_field( $_SERVER['SERVER_NAME'] ); |
| 81 |
} |
| 82 |
|
| 83 |
return apply_filters( 'redirection_request_server', $host ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the request server name (from $_SERVER['HTTP_HOST]) |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
public static function get_request_server_name() { |
| 92 |
$host = ''; |
| 93 |
|
| 94 |
if ( isset( $_SERVER['HTTP_HOST'] ) && is_string( $_SERVER['HTTP_HOST'] ) ) { |
| 95 |
$host = sanitize_text_field( $_SERVER['HTTP_HOST'] ); |
| 96 |
} |
| 97 |
|
| 98 |
$parts = explode( ':', $host ); |
| 99 |
|
| 100 |
return apply_filters( 'redirection_request_server_host', $parts[0] ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get server name + protocol |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public static function get_server() { |
| 109 |
return self::get_protocol() . '://' . self::get_server_name(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get protocol |
| 114 |
* |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public static function get_protocol() { |
| 118 |
return is_ssl() ? 'https' : 'http'; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get request protocol |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public static function get_request_url() { |
| 127 |
$url = ''; |
| 128 |
|
| 129 |
if ( isset( $_SERVER['REQUEST_URI'] ) && is_string( $_SERVER['REQUEST_URI'] ) ) { |
| 130 |
$url = self::sanitize_url( $_SERVER['REQUEST_URI'] ); |
| 131 |
} |
| 132 |
|
| 133 |
return apply_filters( 'redirection_request_url', stripslashes( $url ) ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get user agent |
| 138 |
* |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
public static function get_user_agent() { |
| 142 |
$agent = ''; |
| 143 |
|
| 144 |
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && is_string( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 145 |
$agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
| 146 |
} |
| 147 |
|
| 148 |
return apply_filters( 'redirection_request_agent', $agent ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get referrer |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
public static function get_referrer() { |
| 157 |
$referrer = ''; |
| 158 |
|
| 159 |
if ( isset( $_SERVER['HTTP_REFERER'] ) && is_string( $_SERVER['HTTP_REFERER'] ) ) { |
| 160 |
$referrer = self::sanitize_url( $_SERVER['HTTP_REFERER'] ); |
| 161 |
} |
| 162 |
|
| 163 |
return apply_filters( 'redirection_request_referrer', $referrer ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get standard IP header names |
| 168 |
* |
| 169 |
* @return string[] |
| 170 |
*/ |
| 171 |
public static function get_ip_headers() { |
| 172 |
return [ |
| 173 |
'HTTP_CF_CONNECTING_IP', |
| 174 |
'HTTP_CLIENT_IP', |
| 175 |
'HTTP_X_FORWARDED_FOR', |
| 176 |
'HTTP_X_FORWARDED', |
| 177 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 178 |
'HTTP_FORWARDED_FOR', |
| 179 |
'HTTP_FORWARDED', |
| 180 |
'HTTP_VIA', |
| 181 |
'REMOTE_ADDR', |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get browser IP |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public static function get_ip() { |
| 191 |
$options = red_get_options(); |
| 192 |
$ip = new Redirection_IP(); |
| 193 |
|
| 194 |
// This is set by the server, but may not be the actual IP |
| 195 |
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 196 |
$ip = new Redirection_IP( $_SERVER['REMOTE_ADDR'] ); |
| 197 |
} |
| 198 |
|
| 199 |
if ( in_array( $ip->get(), $options['ip_proxy'], true ) || empty( $options['ip_proxy'] ) ) { |
| 200 |
foreach ( $options['ip_headers'] as $header ) { |
| 201 |
if ( isset( $_SERVER[ $header ] ) ) { |
| 202 |
$ip = new Redirection_IP( $_SERVER[ $header ] ); |
| 203 |
error_log( $header . ' = ' . $ip->get() ); |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return apply_filters( 'redirection_request_ip', $ip->get() ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get a cookie |
| 214 |
* |
| 215 |
* @param string $cookie Name. |
| 216 |
* @return string|false |
| 217 |
*/ |
| 218 |
public static function get_cookie( $cookie ) { |
| 219 |
if ( isset( $_COOKIE[ $cookie ] ) && is_string( $_COOKIE[ $cookie ] ) ) { |
| 220 |
return apply_filters( 'redirection_request_cookie', sanitize_text_field( $_COOKIE[ $cookie ] ), $cookie ); |
| 221 |
} |
| 222 |
|
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get a HTTP header |
| 228 |
* |
| 229 |
* @param string $name Header name. |
| 230 |
* @return string|false |
| 231 |
*/ |
| 232 |
public static function get_header( $name ) { |
| 233 |
$name = 'HTTP_' . strtoupper( $name ); |
| 234 |
$name = str_replace( '-', '_', $name ); |
| 235 |
|
| 236 |
if ( isset( $_SERVER[ $name ] ) && is_string( $_SERVER[ $name ] ) ) { |
| 237 |
return apply_filters( 'redirection_request_header', sanitize_text_field( $_SERVER[ $name ] ), $name ); |
| 238 |
} |
| 239 |
|
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get browser accept language |
| 245 |
* |
| 246 |
* @return string[] |
| 247 |
*/ |
| 248 |
public static function get_accept_language() { |
| 249 |
if ( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) && is_string( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { |
| 250 |
$languages = preg_replace( '/;.*$/', '', sanitize_text_field( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ); |
| 251 |
$languages = str_replace( ' ', '', $languages ); |
| 252 |
|
| 253 |
return apply_filters( 'redirection_request_accept_language', explode( ',', $languages ) ); |
| 254 |
} |
| 255 |
|
| 256 |
return []; |
| 257 |
} |
| 258 |
} |
| 259 |
|