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

164 lines 4.8 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 \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 }
72 // @codingStandardsIgnoreEnd
73
74 if ( is_null( $super ) ) {
75 throw new \Exception( esc_html__( 'Invalid use, type must be one of INPUT_* family.', 'mainwp' ) );
76 }
77
78 $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null;
79 $var = self::filter( $var, $filter, $options );
80
81 return $var;
82 }
83
84 /**
85 * Sanitize or validate input.
86 *
87 * @param mixed $var_value Raw input.
88 * @param int $filter Filter callback.
89 * @param array $options Filter callback parameters.
90 * @throws \Exception Unsupported filter provided.
91 * @return mixed
92 */
93 public static function filter( $var_value, $filter = null, $options = array() ) {
94 // Default filter is a sanitizer, not validator.
95 $filter_type = 'sanitizer';
96
97 // Only filter value if it is not null.
98 if ( isset( $var_value ) && $filter && FILTER_DEFAULT !== $filter ) {
99 if ( ! isset( self::$filter_callbacks[ $filter ] ) ) {
100 throw new \Exception( esc_html__( 'Filter not supported.', 'mainwp' ) );
101 }
102
103 $filter_callback = self::$filter_callbacks[ $filter ];
104 $result = call_user_func( $filter_callback, $var_value );
105
106 /**
107 * "filter_var / filter_input" treats validation/sanitization filters the same
108 * they both return output and change the var value, this shouldn't be the case here.
109 * We'll do a boolean check on validation function, and let sanitizers change the value
110 */
111 $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer';
112 if ( 'validator' === $filter_type ) { // Validation functions.
113 if ( ! $result ) {
114 $var_value = false;
115 }
116 } else { // Santization functions.
117 $var_value = $result;
118 }
119 }
120
121 // Detect FILTER_REQUIRE_ARRAY flag.
122 if ( isset( $var_value ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) {
123 if ( ! is_array( $var_value ) ) {
124 $var_value = ( 'validator' === $filter_type ) ? false : null;
125 }
126 }
127
128 // Polyfill the `default` attribute only, for now.
129 if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) {
130 if ( 'validator' === $filter_type && false === $var_value ) {
131 $var_value = $options['options']['default'];
132 } elseif ( 'sanitizer' === $filter_type && null === $var_value ) {
133 $var_value = $options['options']['default'];
134 }
135 }
136
137 return $var_value;
138 }
139
140 /**
141 * Returns whether the variable is a Regular Expression or not?
142 *
143 * @param string $var_value Raw input.
144 * @return boolean
145 */
146 public static function is_regex( $var_value ) {
147 // @codingStandardsIgnoreStart
148 $test = @preg_match( $var_value, '' );
149 // @codingStandardsIgnoreEnd
150
151 return false !== $test;
152 }
153
154 /**
155 * Returns whether the variable is an IP address or not?
156 *
157 * @param string $var_value Raw input.
158 * @return boolean
159 */
160 public static function is_ip_address( $var_value ) {
161 return false !== \WP_Http::is_ip_address( $var_value );
162 }
163 }
164