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