PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
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.1, at modules/logs/classes/class-log-filter-input.php

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