PluginProbe
Redirection / 5.3.7
Redirection v5.3.7
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.3.7, at models/request.php

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