PluginProbe
Redirection / 5.5.2
Redirection v5.5.2
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 / redirection-settings.php

redirection-settings.php in Redirection 5.5.2, at redirection-settings.php

427 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 define( 'REDIRECTION_OPTION', 'redirection_options' );
4 define( 'REDIRECTION_API_JSON', 0 );
5 define( 'REDIRECTION_API_JSON_INDEX', 1 );
6 define( 'REDIRECTION_API_JSON_RELATIVE', 3 );
7
8 function red_get_plugin_data( $plugin ) {
9 if ( ! function_exists( 'get_plugin_data' ) ) {
10 /** @psalm-suppress MissingFile */
11 include_once ABSPATH . '/wp-admin/includes/plugin.php';
12 }
13
14 return get_plugin_data( $plugin );
15 }
16
17 function red_get_post_types( $full = true ) {
18 $types = get_post_types( [ 'public' => true ], 'objects' );
19 $types[] = (object) array(
20 'name' => 'trash',
21 'label' => __( 'Trash', 'default' ),
22 );
23
24 $post_types = [];
25 foreach ( $types as $type ) {
26 if ( $type->name === 'attachment' ) {
27 continue;
28 }
29
30 if ( $full && strlen( $type->label ) > 0 ) {
31 $post_types[ $type->name ] = $type->label;
32 } else {
33 $post_types[] = $type->name;
34 }
35 }
36
37 return apply_filters( 'redirection_post_types', $post_types );
38 }
39
40 /**
41 * Get default options. Contains all valid options
42 *
43 * @return array
44 */
45 function red_get_default_options() {
46 $flags = new Red_Source_Flags();
47 $defaults = [
48 'support' => false,
49 'token' => md5( uniqid() ),
50 'monitor_post' => 0, // Dont monitor posts by default
51 'monitor_types' => [],
52 'associated_redirect' => '',
53 'auto_target' => '',
54 'expire_redirect' => 7, // Expire in 7 days
55 'expire_404' => 7, // Expire in 7 days
56 'log_external' => false,
57 'log_header' => false,
58 'track_hits' => true,
59 'modules' => [],
60 'redirect_cache' => 1, // 1 hour
61 'ip_logging' => 0, // No IP logging
62 'ip_headers' => [],
63 'ip_proxy' => [],
64 'last_group_id' => 0,
65 'rest_api' => REDIRECTION_API_JSON,
66 'https' => false,
67 'headers' => [],
68 'database' => '',
69 'relocate' => '',
70 'preferred_domain' => '',
71 'aliases' => [],
72 'permalinks' => [],
73 'cache_key' => 0,
74 'plugin_update' => 'prompt',
75 'update_notice' => 0,
76 ];
77 $defaults = array_merge( $defaults, $flags->get_json() );
78
79 return apply_filters( 'red_default_options', $defaults );
80 }
81
82 /**
83 * Set options
84 *
85 * @param array $settings Partial settings.
86 * @return array
87 */
88 function red_set_options( array $settings = [] ) {
89 $options = red_get_options();
90 $monitor_types = [];
91
92 if ( isset( $settings['database'] ) ) {
93 $options['database'] = sanitize_text_field( $settings['database'] );
94 }
95
96 if ( array_key_exists( 'database_stage', $settings ) ) {
97 if ( $settings['database_stage'] === false ) {
98 unset( $options['database_stage'] );
99 } else {
100 $options['database_stage'] = $settings['database_stage'];
101 }
102 }
103
104 if ( isset( $settings['ip_proxy'] ) && is_array( $settings['ip_proxy'] ) ) {
105 $options['ip_proxy'] = array_map( function( $ip ) {
106 $ip = new Redirection_IP( $ip );
107 return $ip->get();
108 }, $settings['ip_proxy'] );
109
110 $options['ip_proxy'] = array_values( array_filter( $options['ip_proxy'] ) );
111 }
112
113 if ( isset( $settings['ip_headers'] ) && is_array( $settings['ip_headers'] ) ) {
114 $available = Redirection_Request::get_ip_headers();
115 $options['ip_headers'] = array_filter( $settings['ip_headers'], function( $header ) use ( $available ) {
116 return in_array( $header, $available, true );
117 } );
118 $options['ip_headers'] = array_values( $options['ip_headers'] );
119 }
120
121 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
122 $options['rest_api'] = intval( $settings['rest_api'], 10 );
123 }
124
125 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
126 $allowed = red_get_post_types( false );
127
128 foreach ( $settings['monitor_types'] as $type ) {
129 if ( in_array( $type, $allowed, true ) ) {
130 $monitor_types[] = $type;
131 }
132 }
133
134 $options['monitor_types'] = $monitor_types;
135 }
136
137 if ( isset( $settings['associated_redirect'] ) && is_string( $settings['associated_redirect'] ) ) {
138 $options['associated_redirect'] = '';
139
140 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
141 $sanitizer = new Red_Item_Sanitize();
142 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
143 }
144 }
145
146 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
147 $options['monitor_post'] = 0;
148 $options['associated_redirect'] = '';
149 } elseif ( isset( $settings['monitor_post'] ) ) {
150 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
151
152 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
153 $groups = Red_Group::get_all();
154
155 if ( count( $groups ) > 0 ) {
156 $options['monitor_post'] = $groups[0]['id'];
157 }
158 }
159 }
160
161 if ( isset( $settings['auto_target'] ) && is_string( $settings['auto_target'] ) ) {
162 $options['auto_target'] = sanitize_text_field( $settings['auto_target'] );
163 }
164
165 if ( isset( $settings['last_group_id'] ) ) {
166 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
167
168 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
169 $groups = Red_Group::get_all();
170 $options['last_group_id'] = $groups[0]['id'];
171 }
172 }
173
174 if ( isset( $settings['token'] ) && is_string( $settings['token'] ) ) {
175 $options['token'] = sanitize_text_field( $settings['token'] );
176 }
177
178 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
179 $options['token'] = md5( uniqid() );
180 }
181
182 // Boolean settings
183 foreach ( [ 'support', 'https', 'log_external', 'log_header', 'track_hits' ] as $name ) {
184 if ( isset( $settings[ $name ] ) ) {
185 $options[ $name ] = $settings[ $name ] ? true : false;
186 }
187 }
188
189 if ( isset( $settings['expire_redirect'] ) ) {
190 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
191 }
192
193 if ( isset( $settings['expire_404'] ) ) {
194 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
195 }
196
197 if ( isset( $settings['ip_logging'] ) ) {
198 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
199 }
200
201 if ( isset( $settings['redirect_cache'] ) ) {
202 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
203
204 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
205 $options['redirect_cache'] = 1;
206 }
207 }
208
209 if ( isset( $settings['location'] ) && ( ! isset( $options['location'] ) || $options['location'] !== $settings['location'] ) ) {
210 $module = Red_Module::get( 2 );
211 if ( $module ) {
212 $options['modules'][2] = $module->update( $settings );
213 }
214 }
215
216 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
217 // If we have a monitor_post set, but no types, then blank everything
218 $options['monitor_post'] = 0;
219 $options['associated_redirect'] = '';
220 }
221
222 if ( isset( $settings['plugin_update'] ) && in_array( $settings['plugin_update'], [ 'prompt', 'admin' ], true ) ) {
223 $options['plugin_update'] = sanitize_text_field( $settings['plugin_update'] );
224 }
225
226 $flags = new Red_Source_Flags();
227 $flags_present = [];
228
229 foreach ( array_keys( $flags->get_json() ) as $flag ) {
230 if ( isset( $settings[ $flag ] ) ) {
231 $flags_present[ $flag ] = $settings[ $flag ];
232 }
233 }
234
235 if ( count( $flags_present ) > 0 ) {
236 $flags->set_flags( $flags_present );
237 $options = array_merge( $options, $flags->get_json() );
238 }
239
240 if ( isset( $settings['headers'] ) ) {
241 $headers = new Red_Http_Headers( $settings['headers'] );
242 $options['headers'] = $headers->get_json();
243 }
244
245 if ( isset( $settings['aliases'] ) && is_array( $settings['aliases'] ) ) {
246 $options['aliases'] = array_map( 'sanitize_text_field', $settings['aliases'] );
247 $options['aliases'] = array_values( array_filter( array_map( 'red_parse_domain_only', $settings['aliases'] ) ) );
248 $options['aliases'] = array_slice( $options['aliases'], 0, 20 ); // Max 20
249 }
250
251 if ( isset( $settings['permalinks'] ) && is_array( $settings['permalinks'] ) ) {
252 $options['permalinks'] = array_map(
253 function ( $permalink ) {
254 return sanitize_option( 'permalink_structure', $permalink );
255 },
256 $settings['permalinks']
257 );
258 $options['permalinks'] = array_values( array_filter( array_map( 'trim', $options['permalinks'] ) ) );
259 $options['permalinks'] = array_slice( $options['permalinks'], 0, 10 ); // Max 10
260 }
261
262 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
263 $options['preferred_domain'] = sanitize_text_field( $settings['preferred_domain'] );
264 }
265
266 if ( isset( $settings['relocate'] ) && is_string( $settings['relocate'] ) ) {
267 $options['relocate'] = red_parse_domain_path( sanitize_text_field( $settings['relocate'] ) );
268
269 if ( strlen( $options['relocate'] ) > 0 ) {
270 $options['preferred_domain'] = '';
271 $options['aliases'] = [];
272 $options['https'] = false;
273 }
274 }
275
276 if ( isset( $settings['cache_key'] ) ) {
277 $key = intval( $settings['cache_key'], 10 );
278
279 if ( $settings['cache_key'] === true ) {
280 $key = time();
281 } elseif ( $settings['cache_key'] === false ) {
282 $key = 0;
283 }
284
285 $options['cache_key'] = $key;
286 }
287
288 if ( isset( $settings['update_notice'] ) ) {
289 $major_version = explode( '-', REDIRECTION_VERSION )[0]; // Remove any beta suffix
290 $major_version = implode( '.', array_slice( explode( '.', $major_version ), 0, 2 ) );
291 $options['update_notice'] = $major_version;
292 }
293
294 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
295 return $options;
296 }
297
298 function red_parse_url( $url ) {
299 $domain = filter_var( $url, FILTER_SANITIZE_URL );
300 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
301 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
302 }
303
304 return wp_parse_url( $domain );
305 }
306
307 function red_parse_domain_only( $domain ) {
308 $parsed = red_parse_url( $domain );
309
310 if ( $parsed && isset( $parsed['host'] ) ) {
311 return $parsed['host'];
312 }
313
314 return '';
315 }
316
317 function red_parse_domain_path( $domain ) {
318 $parsed = red_parse_url( $domain );
319
320 if ( $parsed && isset( $parsed['host'] ) ) {
321 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
322 }
323
324 return '';
325 }
326
327 /**
328 * Have redirects been disabled?
329 *
330 * @return boolean
331 */
332 function red_is_disabled() {
333 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
334 }
335
336 /**
337 * Get Redirection options
338 *
339 * @return array
340 */
341 function red_get_options() {
342 $options = get_option( REDIRECTION_OPTION );
343 $fresh_install = false;
344
345 if ( $options === false ) {
346 $fresh_install = true;
347 }
348
349 if ( ! is_array( $options ) ) {
350 $options = [];
351 }
352
353 if ( red_is_disabled() ) {
354 $options['https'] = false;
355 }
356
357 $defaults = red_get_default_options();
358
359 foreach ( $defaults as $key => $value ) {
360 if ( ! isset( $options[ $key ] ) ) {
361 $options[ $key ] = $value;
362 }
363 }
364
365 if ( $fresh_install ) {
366 // Default flags for new installs - ignore case and trailing slashes
367 $options['flag_case'] = true;
368 $options['flag_trailing'] = true;
369 }
370
371 // Back-compat. If monitor_post is set without types then it's from an older Redirection
372 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
373 $options['monitor_types'] = [ 'post' ];
374 }
375
376 // Remove old options not in red_get_default_options()
377 foreach ( array_keys( $options ) as $key ) {
378 if ( ! isset( $defaults[ $key ] ) && $key !== 'database_stage' ) {
379 unset( $options[ $key ] );
380 }
381 }
382
383 // Back-compat fix
384 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
385 $options['rest_api'] = REDIRECTION_API_JSON;
386 }
387
388 if ( isset( $options['modules'] ) && isset( $options['modules']['2'] ) && isset( $options['modules']['2']['location'] ) ) {
389 $options['location'] = $options['modules']['2']['location'];
390 }
391
392 return $options;
393 }
394
395 /**
396 * Get the current REST API
397 *
398 * @param boolean $type Override with a specific API type.
399 * @return string
400 */
401 function red_get_rest_api( $type = false ) {
402 if ( $type === false ) {
403 $options = red_get_options();
404 $type = $options['rest_api'];
405 }
406
407 $url = get_rest_url(); // REDIRECTION_API_JSON
408
409 if ( $type === REDIRECTION_API_JSON_INDEX ) {
410 $url = home_url( '/?rest_route=/' );
411 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
412 /** @psalm-suppress TooManyArguments, InvalidCast */
413 $relative = (string) wp_parse_url( $url, PHP_URL_PATH );
414
415 if ( $relative ) {
416 $url = $relative;
417 }
418
419 if ( $url === '/index.php' ) {
420 // No permalinks. Default to normal REST API
421 $url = home_url( '/?rest_route=/' );
422 }
423 }
424
425 return $url;
426 }
427