class-admin.php
3 months ago
class-author.php
3 months ago
class-cli.php
3 months ago
class-connector.php
3 months ago
class-connectors.php
3 months ago
class-date-interval.php
3 months ago
class-db-driver-wpdb.php
2 months ago
class-db-driver.php
3 months ago
class-db.php
3 months ago
class-export.php
3 months ago
class-exporter.php
3 months ago
class-filter-input.php
3 months ago
class-form-generator.php
3 months ago
class-install.php
3 months ago
class-list-table.php
3 months ago
class-live-update.php
3 months ago
class-log.php
2 months ago
class-mainwp-child-report-helper.php
3 months ago
class-network.php
3 months ago
class-plugin.php
3 months ago
class-preview-list-table.php
3 months ago
class-query.php
3 months ago
class-record.php
3 months ago
class-settings.php
3 months ago
class-uninstall.php
3 months ago
class-filter-input.php
166 lines
| 1 | <?php |
| 2 | /** MainWP Child Reports filter input. */ |
| 3 | |
| 4 | namespace WP_MainWP_Stream; |
| 5 | |
| 6 | // Exit if accessed directly. |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Class Filter_Input. |
| 13 | * |
| 14 | * @package WP_MainWP_Stream |
| 15 | */ |
| 16 | class Filter_Input { |
| 17 | |
| 18 | /** @var array $filter_callbacks Array of filter callbacks. */ |
| 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_FULL_SPECIAL_CHARS => 'sanitize_text_field', |
| 36 | FILTER_SANITIZE_URL => 'esc_url_raw', |
| 37 | // Other. |
| 38 | FILTER_UNSAFE_RAW => null, |
| 39 | ); |
| 40 | |
| 41 | /** |
| 42 | * Input type checker. |
| 43 | * |
| 44 | * @param $type |
| 45 | * @param $variable_name |
| 46 | * @param null $filter |
| 47 | * @param array $options |
| 48 | * @return array|bool|mixed|null |
| 49 | * @throws \Exception |
| 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-child-reports' ) ); |
| 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 | * Filter & sanitiser. |
| 86 | * |
| 87 | * @param $var |
| 88 | * @param null $filter |
| 89 | * @param array $options |
| 90 | * @return array|bool|mixed|null |
| 91 | * @throws \Exception |
| 92 | */ |
| 93 | public static function filter( $var, $filter = null, $options = array() ) { |
| 94 | |
| 95 | // Default filter is a sanitizer, not validator |
| 96 | $filter_type = 'sanitizer'; |
| 97 | |
| 98 | // Only filter value if it is not null |
| 99 | if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) { |
| 100 | if ( ! isset( self::$filter_callbacks[ $filter ] ) ) { |
| 101 | throw new \Exception( esc_html__( 'Filter not supported.', 'mainwp-child-reports' ) ); |
| 102 | } |
| 103 | |
| 104 | $filter_callback = self::$filter_callbacks[ $filter ]; |
| 105 | $result = call_user_func( $filter_callback, $var ); |
| 106 | |
| 107 | /** |
| 108 | * filter_var / filter_input treats validation/sanitization filters the same. |
| 109 | * They both return output and change the var value, this shouldn't be the case here. |
| 110 | * We'll do a boolean check on validation function, and let sanitizers change the value. |
| 111 | */ |
| 112 | $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer'; |
| 113 | if ( 'validator' === $filter_type ) { // Validation functions |
| 114 | if ( ! $result ) { |
| 115 | $var = false; |
| 116 | } |
| 117 | } else { // Santization functions |
| 118 | $var = $result; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Detect FILTER_REQUIRE_ARRAY flag. |
| 123 | if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) { |
| 124 | if ( ! is_array( $var ) ) { |
| 125 | $var = ( 'validator' === $filter_type ) ? false : null; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Polyfill the `default` attribute only, for now. |
| 130 | if ( is_array( $options ) && ! empty( $options['options']['default'] ) ) { |
| 131 | if ( 'validator' === $filter_type && false === $var ) { |
| 132 | $var = $options['options']['default']; |
| 133 | } elseif ( 'sanitizer' === $filter_type && null === $var ) { |
| 134 | $var = $options['options']['default']; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return $var; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Check if subject is equal to $var. |
| 143 | * |
| 144 | * @param string $var Pregmatch pattern. |
| 145 | * @return bool Return FALSE if $test equals false. |
| 146 | */ |
| 147 | public static function is_regex( $var ) { |
| 148 | // @codingStandardsIgnoreStart |
| 149 | $test = @preg_match( $var, '' ); |
| 150 | // @codingStandardsIgnoreEnd |
| 151 | |
| 152 | return false !== $test; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Check for IP address. |
| 157 | * |
| 158 | * @param string $var String to check for IP address. |
| 159 | * |
| 160 | * @return bool Return FASLE if not an IP address. |
| 161 | */ |
| 162 | public static function is_ip_address( $var ) { |
| 163 | return false !== \WP_Http::is_ip_address( $var ); |
| 164 | } |
| 165 | } |
| 166 |