-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator.php
96 lines (79 loc) · 2.38 KB
/
validator.php
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
<?php
/**
* Debug Bar Rewrite Rules. Regexp Validator.
*
* @package WordPress\Plugins\Debug Bar Rewrite Rules\Regexp Rules validator.
* @author Oleg Butuzov
* @link https://github.com/butuzov/Debug-Bar-Rewrite-Rules
* @version 0.6.5
* @license http://creativecommons.org/licenses/GPL/2.0/ GNU General Public License, version 2 or higher
*
* @wordpress-plugin
*/
if ( function_exists( 'filter_input_array' ) ) {
$input = filter_input_array(
INPUT_POST,
array(
'rules' => array(
'filter' => FILTER_CALLBACK,
'options' => function ( $var ) {
return filter_var( $var, FILTER_SANITIZE_STRING );
},
),
'search' => FILTER_SANITIZE_STRING,
)
);
} else {
$input = array_map( 'sanitize', $_POST ); // input var, CSRF.
}
if ( ! empty( $input['rules'] ) && is_array( $input['rules'] ) && ! empty( $input['search'] ) ) {
$search = trim( $input['search'] );
$search_u = urldecode( $search );
foreach ( $input['rules'] as $k => $rule ) {
$regexp = sprintf( '#^%s#', $rule['rule'] );
if ( preg_match( $regexp, $search, $matches )
|| preg_match( $regexp, $search_u, $matches ) ) {
// Trim the query of everything up to the '?'.
$query = preg_replace( '!^.+\?!', '', $rule['match'] );
foreach ( $matches as $_k => $_i ) {
if ( false !== strpos( $query, '$matches[' . $_k . ']' ) ) {
$query = str_replace( '$matches[' . $_k . ']', $_i, $query );
}
}
parse_str( $query, $data );
if ( is_array( $data ) && ! empty( $data ) > 0 ) {
foreach ( $data as $key => $value ) {
if ( false === strpos( $value, '$matches' ) ) {
$input['rules'][ $k ]['vars'][ $key ] = $value;
}
}
}
$input['rules'][ $k ]['result'] = true;
} else {
$input['rules'][ $k ]['result'] = false;
}
}
}
if ( ! function_exists( 'sanitize' ) ) {
/**
* Simple deep Stripslashes function.
*
* @param array $value Incoming filter values.
* @return array Filtred values.
*/
function sanitize( $value ) {
if ( is_array( $value ) ) {
$value = array_map( 'sanitize', $value );
} elseif ( is_object( $value ) ) {
$vars = get_object_vars( $value );
foreach ( $vars as $key => $data ) {
$value->{$key} = sanitize( $data );
}
} elseif ( is_string( $value ) ) {
$value = stripslashes( $value );
}
return $value;
}
}
header( 'Content-type: application/json' );
die( json_encode( $input ) );