PluginProbe
Stream – Activity Log & Audit Trail / 3.1
Stream – Activity Log & Audit Trail v3.1
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-filter-input.php

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

117 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WP_Stream;
3
4 class Filter_Input {
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( __CLASS__, 'is_ip_address' ),
13 FILTER_VALIDATE_REGEXP => array( __CLASS__, '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 // @codingStandardsIgnoreStart
31 switch ( $type ) {
32 case INPUT_POST :
33 $super = $_POST;
34 break;
35 case INPUT_GET :
36 $super = $_GET;
37 break;
38 case INPUT_COOKIE :
39 $super = $_COOKIE;
40 break;
41 case INPUT_ENV :
42 $super = $_ENV;
43 break;
44 case INPUT_SERVER :
45 $super = $_SERVER;
46 break;
47 }
48 // @codingStandardsIgnoreEnd
49
50 if ( is_null( $super ) ) {
51 throw new \Exception( esc_html__( 'Invalid use, type must be one of INPUT_* family.', 'stream' ) );
52 }
53
54 $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null;
55 $var = self::filter( $var, $filter, $options );
56
57 return $var;
58 }
59
60 public static function filter( $var, $filter = null, $options = array() ) {
61 // Default filter is a sanitizer, not validator
62 $filter_type = 'sanitizer';
63
64 // Only filter value if it is not null
65 if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) {
66 if ( ! isset( self::$filter_callbacks[ $filter ] ) ) {
67 throw new \Exception( esc_html__( 'Filter not supported.', 'stream' ) );
68 }
69
70 $filter_callback = self::$filter_callbacks[ $filter ];
71 $result = call_user_func( $filter_callback, $var );
72
73 // filter_var / filter_input treats validation/sanitization filters the same
74 // they both return output and change the var value, this shouldn't be the case here.
75 // We'll do a boolean check on validation function, and let sanitizers change the value
76 $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer';
77 if ( 'validator' === $filter_type ) { // Validation functions
78 if ( ! $result ) {
79 $var = false;
80 }
81 } else { // Santization functions
82 $var = $result;
83 }
84 }
85
86 // Detect FILTER_REQUIRE_ARRAY flag
87 if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) {
88 if ( ! is_array( $var ) ) {
89 $var = ( 'validator' === $filter_type ) ? false : null;
90 }
91 }
92
93 // Polyfill the `default` attribute only, for now.
94 if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) {
95 if ( 'validator' === $filter_type && false === $var ) {
96 $var = $options['options']['default'];
97 } elseif ( 'sanitizer' === $filter_type && null === $var ) {
98 $var = $options['options']['default'];
99 }
100 }
101
102 return $var;
103 }
104
105 public static function is_regex( $var ) {
106 // @codingStandardsIgnoreStart
107 $test = @preg_match( $var, '' );
108 // @codingStandardsIgnoreEnd
109
110 return false !== $test;
111 }
112
113 public static function is_ip_address( $var ) {
114 return false !== \WP_Http::is_ip_address( $var );
115 }
116 }
117