PluginProbe
Stream – Activity Log & Audit Trail / 2.0.2
Stream – Activity Log & Audit Trail v2.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-wp-stream-filter-input.php

class-wp-stream-filter-input.php in Stream – Activity Log & Audit Trail 2.0.2, at classes/class-wp-stream-filter-input.php

115 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WP_Stream_Filter_Input {
4
5 public static $filter_callbacks = array(
6 FILTER_DEFAULT => null,
7 // Validate
8 FILTER_VALIDATE_BOOLEAN => 'is_bool',
9 FILTER_VALIDATE_EMAIL => 'is_email',
10 FILTER_VALIDATE_FLOAT => 'is_float',
11 FILTER_VALIDATE_INT => 'is_int',
12 FILTER_VALIDATE_IP => array( 'WP_Stream_Filter_Input', 'is_ip_address' ),
13 FILTER_VALIDATE_REGEXP => array( 'WP_Stream_Filter_Input', 'is_regex' ),
14 FILTER_VALIDATE_URL => 'wp_http_validate_url',
15 // Sanitize
16 FILTER_SANITIZE_EMAIL => 'sanitize_email',
17 FILTER_SANITIZE_ENCODED => 'esc_url_raw',
18 FILTER_SANITIZE_NUMBER_FLOAT => 'floatval',
19 FILTER_SANITIZE_NUMBER_INT => 'intval',
20 FILTER_SANITIZE_SPECIAL_CHARS => 'htmlspecialchars',
21 FILTER_SANITIZE_STRING => 'sanitize_text_field',
22 FILTER_SANITIZE_URL => 'esc_url_raw',
23 // Other
24 FILTER_UNSAFE_RAW => null,
25 );
26
27 public static function super( $type, $variable_name, $filter = null, $options = array() ) {
28 $super = null;
29
30 switch ( $type ) {
31 case INPUT_POST :
32 $super = $_POST;
33 break;
34 case INPUT_GET :
35 $super = $_GET;
36 break;
37 case INPUT_COOKIE :
38 $super = $_COOKIE;
39 break;
40 case INPUT_ENV :
41 $super = $_ENV;
42 break;
43 case INPUT_SERVER :
44 $super = $_SERVER;
45 break;
46 }
47
48 if ( is_null( $super ) ) {
49 throw new Exception( __( 'Invalid use, type must be one of INPUT_* family.', 'stream' ) );
50 }
51
52 $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null;
53 $var = self::filter( $var, $filter, $options );
54
55 return $var;
56 }
57
58 public static function filter( $var, $filter = null, $options = array() ) {
59 // Default filter is a sanitizer, not validator
60 $filter_type = 'sanitizer';
61
62 // Only filter value if it is not null
63 if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) {
64 if ( ! isset( self::$filter_callbacks[ $filter ] ) ) {
65 throw new Exception( __( 'Filter not supported.', 'stream' ) );
66 }
67
68 $filter_callback = self::$filter_callbacks[ $filter ];
69 $result = call_user_func( $filter_callback, $var );
70
71 // filter_var / filter_input treats validation/sanitization filters the same
72 // they both return output and change the var value, this shouldn't be the case here.
73 // We'll do a boolean check on validation function, and let sanitizers change the value
74 $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer';
75 if ( 'validator' === $filter_type ) { // Validation functions
76 if ( ! $result ) {
77 $var = false;
78 }
79 } else { // Santization functions
80 $var = $result;
81 }
82 }
83
84 // Detect FILTER_REQUIRE_ARRAY flag
85 if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) {
86 if ( ! is_array( $var ) ) {
87 $var = ( 'validator' === $filter_type ) ? false : null;
88 }
89 }
90
91 // Polyfill the `default` attribute only, for now.
92 if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) {
93 if ( 'validator' === $filter_type && false === $var ) {
94 $var = $options['options']['default'];
95 } elseif ( 'sanitizer' === $filter_type && null === $var ) {
96 $var = $options['options']['default'];
97 }
98 }
99
100 return $var;
101 }
102
103 public static function is_regex( $var ) {
104 // @codingStandardsIgnoreStart
105 $test = @preg_match( $var, '' );
106 // @codingStandardsIgnoreEnd
107
108 return $test !== false;
109 }
110
111 public static function is_ip_address( $var ) {
112 return false !== WP_Http::is_ip_address( $var );
113 }
114
115 }