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

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