PluginProbe
Redirection / 5.8.1
Redirection v5.8.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / request.php

request.php in Redirection 5.8.1, at models/request.php

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