PluginProbe
Stream – Activity Log & Audit Trail / 4.0.0
Stream – Activity Log & Audit Trail v4.0.0
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 4.0.0, at classes/class-filter-input.php

165 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Processes form input
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Filter_Input
12 */
13 class Filter_Input {
14
15 /**
16 * Callbacks to be used for input validation/sanitation.
17 *
18 * @var array
19 */
20 public static $filter_callbacks = array(
21 FILTER_DEFAULT => null,
22 // Validate.
23 FILTER_VALIDATE_BOOLEAN => 'is_bool',
24 FILTER_VALIDATE_EMAIL => 'is_email',
25 FILTER_VALIDATE_FLOAT => 'is_float',
26 FILTER_VALIDATE_INT => 'is_int',
27 FILTER_VALIDATE_IP => array( __CLASS__, 'is_ip_address' ),
28 FILTER_VALIDATE_REGEXP => array( __CLASS__, 'is_regex' ),
29 FILTER_VALIDATE_URL => 'wp_http_validate_url',
30 // Sanitize.
31 FILTER_SANITIZE_EMAIL => 'sanitize_email',
32 FILTER_SANITIZE_ENCODED => 'esc_url_raw',
33 FILTER_SANITIZE_NUMBER_FLOAT => 'floatval',
34 FILTER_SANITIZE_NUMBER_INT => 'intval',
35 FILTER_SANITIZE_SPECIAL_CHARS => 'htmlspecialchars',
36 FILTER_SANITIZE_FULL_SPECIAL_CHARS => 'sanitize_text_field',
37 FILTER_SANITIZE_URL => 'esc_url_raw',
38 // Other.
39 FILTER_UNSAFE_RAW => null,
40 );
41
42 /**
43 * Returns input variable
44 *
45 * @param int $type Input type.
46 * @param string $variable_name Variable key.
47 * @param int $filter Filter callback.
48 * @param array $options Filter callback parameters.
49 * @throws \Exception Invalid input type provided.
50 * @return mixed
51 */
52 public static function super( $type, $variable_name, $filter = null, $options = array() ) {
53 $super = null;
54
55 // @codingStandardsIgnoreStart
56 switch ( $type ) {
57 case INPUT_POST :
58 $super = $_POST;
59 break;
60 case INPUT_GET :
61 $super = $_GET;
62 break;
63 case INPUT_COOKIE :
64 $super = $_COOKIE;
65 break;
66 case INPUT_ENV :
67 $super = $_ENV;
68 break;
69 case INPUT_SERVER :
70 $super = $_SERVER;
71 break;
72 }
73 // @codingStandardsIgnoreEnd
74
75 if ( is_null( $super ) ) {
76 throw new \Exception( esc_html__( 'Invalid use, type must be one of INPUT_* family.', 'stream' ) );
77 }
78
79 $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null;
80 $var = self::filter( $var, $filter, $options );
81
82 return $var;
83 }
84
85 /**
86 * Sanitize or validate input.
87 *
88 * @param mixed $var Raw input.
89 * @param int $filter Filter callback.
90 * @param array $options Filter callback parameters.
91 * @throws \Exception Unsupported filter provided.
92 * @return mixed
93 */
94 public static function filter( $var, $filter = null, $options = array() ) {
95 // Default filter is a sanitizer, not validator.
96 $filter_type = 'sanitizer';
97
98 // Only filter value if it is not null.
99 if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) {
100 if ( ! isset( self::$filter_callbacks[ $filter ] ) ) {
101 throw new \Exception( esc_html__( 'Filter not supported.', 'stream' ) );
102 }
103
104 $filter_callback = self::$filter_callbacks[ $filter ];
105 $result = call_user_func( $filter_callback, $var );
106
107 /**
108 * "filter_var / filter_input" treats validation/sanitization filters the same
109 * they both return output and change the var value, this shouldn't be the case here.
110 * We'll do a boolean check on validation function, and let sanitizers change the value
111 */
112 $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer';
113 if ( 'validator' === $filter_type ) { // Validation functions.
114 if ( ! $result ) {
115 $var = false;
116 }
117 } else { // Santization functions.
118 $var = $result;
119 }
120 }
121
122 // Detect FILTER_REQUIRE_ARRAY flag.
123 if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) {
124 if ( ! is_array( $var ) ) {
125 $var = ( 'validator' === $filter_type ) ? false : null;
126 }
127 }
128
129 // Polyfill the `default` attribute only, for now.
130 if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) {
131 if ( 'validator' === $filter_type && false === $var ) {
132 $var = $options['options']['default'];
133 } elseif ( 'sanitizer' === $filter_type && null === $var ) {
134 $var = $options['options']['default'];
135 }
136 }
137
138 return $var;
139 }
140
141 /**
142 * Returns whether the variable is a Regular Expression or not?
143 *
144 * @param string $var Raw input.
145 * @return boolean
146 */
147 public static function is_regex( $var ) {
148 // @codingStandardsIgnoreStart
149 $test = @preg_match( $var, '' );
150 // @codingStandardsIgnoreEnd
151
152 return false !== $test;
153 }
154
155 /**
156 * Returns whether the variable is an IP address or not?
157 *
158 * @param string $var Raw input.
159 * @return boolean
160 */
161 public static function is_ip_address( $var ) {
162 return false !== \WP_Http::is_ip_address( $var );
163 }
164 }
165