vendor
10 years ago
admin.php
9 years ago
class-wp-stream-author.php
10 years ago
connector.php
9 years ago
connectors.php
9 years ago
context-query.php
10 years ago
dashboard.php
10 years ago
date-interval.php
10 years ago
db.php
9 years ago
filter-input.php
10 years ago
functions.php
10 years ago
install.php
9 years ago
list-table.php
9 years ago
live-update.php
9 years ago
log.php
10 years ago
network.php
10 years ago
query.php
9 years ago
settings.php
9 years ago
filter-input.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | class MainWP_WP_Stream_Filter_Input { |
| 4 | |
| 5 | public static $filter_callbacks = array( |
| 6 | FILTER_DEFAULT => null, |
| 7 | // Validate |
| 8 | FILTER_VALIDATE_BOOLEAN => 'is_bool', |
| 9 | FILTER_VALIDATE_EMAIL => 'is_email', |
| 10 | FILTER_VALIDATE_FLOAT => 'is_float', |
| 11 | FILTER_VALIDATE_INT => 'is_int', |
| 12 | FILTER_VALIDATE_IP => array( 'MainWP_WP_Stream_Filter_Input', 'is_ip_address' ), |
| 13 | FILTER_VALIDATE_REGEXP => array( 'MainWP_WP_Stream_Filter_Input', 'is_regex' ), |
| 14 | FILTER_VALIDATE_URL => 'wp_http_validate_url', |
| 15 | // Sanitize |
| 16 | FILTER_SANITIZE_EMAIL => 'sanitize_email', |
| 17 | FILTER_SANITIZE_ENCODED => 'esc_url_raw', |
| 18 | FILTER_SANITIZE_NUMBER_FLOAT => 'floatval', |
| 19 | FILTER_SANITIZE_NUMBER_INT => 'intval', |
| 20 | FILTER_SANITIZE_SPECIAL_CHARS => 'htmlspecialchars', |
| 21 | FILTER_SANITIZE_STRING => 'sanitize_text_field', |
| 22 | FILTER_SANITIZE_URL => 'esc_url_raw', |
| 23 | // Other |
| 24 | FILTER_UNSAFE_RAW => null, |
| 25 | ); |
| 26 | |
| 27 | public static function super( $type, $variable_name, $filter = null, $options = array() ) { |
| 28 | $super = null; |
| 29 | |
| 30 | switch ( $type ) { |
| 31 | case INPUT_POST : |
| 32 | $super = $_POST; |
| 33 | break; |
| 34 | case INPUT_GET : |
| 35 | $super = $_GET; |
| 36 | break; |
| 37 | case INPUT_COOKIE : |
| 38 | $super = $_COOKIE; |
| 39 | break; |
| 40 | case INPUT_ENV : |
| 41 | $super = $_ENV; |
| 42 | break; |
| 43 | case INPUT_SERVER : |
| 44 | $super = $_SERVER; |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | if ( is_null( $super ) ) { |
| 49 | throw new Exception( __( 'Invalid use, type must be one of INPUT_* family.', 'mainwp-child-reports' ) ); |
| 50 | } |
| 51 | |
| 52 | $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null; |
| 53 | $var = self::filter( $var, $filter, $options ); |
| 54 | |
| 55 | return $var; |
| 56 | } |
| 57 | |
| 58 | public static function filter( $var, $filter = null, $options = array() ) { |
| 59 | // Default filter is a sanitizer, not validator |
| 60 | $filter_type = 'sanitizer'; |
| 61 | |
| 62 | // Only filter value if it is not null |
| 63 | if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) { |
| 64 | if ( ! isset( self::$filter_callbacks[ $filter ] ) ) { |
| 65 | throw new Exception( __( 'Filter not supported.', 'mainwp-child-reports' ) ); |
| 66 | } |
| 67 | |
| 68 | $filter_callback = self::$filter_callbacks[ $filter ]; |
| 69 | $result = call_user_func( $filter_callback, $var ); |
| 70 | |
| 71 | $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer'; |
| 72 | if ( 'validator' === $filter_type ) { // Validation functions |
| 73 | if ( ! $result ) { |
| 74 | $var = false; |
| 75 | } |
| 76 | } else { // Santization functions |
| 77 | $var = $result; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Detect FILTER_REQUIRE_ARRAY flag |
| 82 | if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) { |
| 83 | if ( ! is_array( $var ) ) { |
| 84 | $var = ( 'validator' === $filter_type ) ? false : null; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Polyfill the `default` attribute only, for now. |
| 89 | if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) { |
| 90 | if ( 'validator' === $filter_type && false === $var ) { |
| 91 | $var = $options['options']['default']; |
| 92 | } elseif ( 'sanitizer' === $filter_type && null === $var ) { |
| 93 | $var = $options['options']['default']; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $var; |
| 98 | } |
| 99 | |
| 100 | public static function is_regex( $var ) { |
| 101 | // @codingStandardsIgnoreStart |
| 102 | $test = @preg_match( $var, '' ); |
| 103 | // @codingStandardsIgnoreEnd |
| 104 | |
| 105 | return $test !== false; |
| 106 | } |
| 107 | |
| 108 | public static function is_ip_address( $var ) { |
| 109 | return false !== WP_Http::is_ip_address( $var ); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | function mainwp_wp_stream_filter_input( $type, $variable_name, $filter = null, $options = array() ) { |
| 115 | return call_user_func_array( array( 'MainWP_WP_Stream_Filter_Input', 'super' ), func_get_args() ); |
| 116 | } |
| 117 | |
| 118 | function mainwp_wp_stream_filter_var( $var, $filter = null, $options = array() ) { |
| 119 | return call_user_func_array( array( 'MainWP_WP_Stream_Filter_Input', 'filter' ), func_get_args() ); |
| 120 | } |
| 121 |