PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log-filter-input.php

class-log-filter-input.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.2, at modules/logs/classes/class-log-filter-input.php

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