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