PluginProbe
FakerPress / 0.1.0
FakerPress v0.1.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / classes / filter.php

filter.php in FakerPress 0.1.0, at classes/filter.php

122 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @see https://github.com/x-team/wp-stream We forked this idea from X-Team's Stream Plugin
4 */
5 namespace FakerPress;
6
7 class Filter {
8
9 public static $filter_callbacks = array(
10 FILTER_DEFAULT => null,
11
12 // Validate
13 FILTER_VALIDATE_BOOLEAN => 'is_bool',
14 FILTER_VALIDATE_EMAIL => 'is_email',
15 FILTER_VALIDATE_FLOAT => 'is_float',
16 FILTER_VALIDATE_INT => 'is_int',
17 FILTER_VALIDATE_IP => array( 'Filter', 'is_ip_address' ),
18 FILTER_VALIDATE_REGEXP => array( 'Filter', 'is_regex' ),
19 FILTER_VALIDATE_URL => 'wp_http_validate_url',
20
21 // Sanitize
22 FILTER_SANITIZE_EMAIL => 'sanitize_email',
23 FILTER_SANITIZE_ENCODED => 'esc_url_raw',
24 FILTER_SANITIZE_NUMBER_FLOAT => 'floatval',
25 FILTER_SANITIZE_NUMBER_INT => 'intval',
26 FILTER_SANITIZE_SPECIAL_CHARS => 'htmlspecialchars',
27 FILTER_SANITIZE_STRING => 'sanitize_text_field',
28 FILTER_SANITIZE_URL => 'esc_url_raw',
29 'file' => 'sanitize_file_name',
30
31 // Other
32 FILTER_UNSAFE_RAW => null,
33 );
34
35 public static function super( $type, $variable_name, $filter = null, $options = array() ) {
36 $super = null;
37
38 switch ( $type ) {
39 case INPUT_POST :
40 $super = $_POST;
41 break;
42 case INPUT_GET :
43 $super = $_GET;
44 break;
45 case INPUT_COOKIE :
46 $super = $_COOKIE;
47 break;
48 case INPUT_ENV :
49 $super = $_ENV;
50 break;
51 case INPUT_SERVER :
52 $super = $_SERVER;
53 break;
54 }
55
56 if ( is_null( $super ) ) {
57 throw new Exception( __( 'Invalid use, type must be one of INPUT_* family.', 'fakerpress' ) );
58 }
59
60 $var = isset( $super[ $variable_name ] ) ? $super[ $variable_name ] : null;
61 $var = self::filter( $var, $filter, $options );
62
63 return $var;
64 }
65
66 public static function filter( $var, $filter = null, $options = array() ) {
67 // Default filter is a sanitizer, not validator
68 $filter_type = 'sanitizer';
69
70 // Default value when there is none
71 $default = ( array_key_exists( 'default', (array) $options ) ? $options['default'] : $options );
72 if ( 'validator' === $filter_type && false === $var ) {
73 $var = empty( $options ) ? false : $default;
74 } elseif ( 'sanitizer' === $filter_type && null === $var ) {
75 $var = empty( $options ) ? null : $default;
76 }
77
78 // Only filter value if it is not null
79 if ( isset( $var ) && $filter && FILTER_DEFAULT !== $filter ) {
80 if ( ! isset( self::$filter_callbacks[ $filter ] ) ) {
81 throw new \Exception( __( 'Filter not supported.', 'fakerpress' ) );
82 }
83
84 $filter_callback = self::$filter_callbacks[ $filter ];
85 $result = call_user_func( $filter_callback, $var );
86
87 // filter_var / filter_input treats validation/sanitization filters the same
88 // they both return output and change the var value, this shouldn't be the case here.
89 // We'll do a boolean check on validation function, and let sanitizers change the value
90 $filter_type = ( $filter < 500 ) ? 'validator' : 'sanitizer';
91 if ( 'validator' === $filter_type ) { // Validation functions
92 if ( ! $result ) {
93 $var = false;
94 }
95 } else { // Santization functions
96 $var = $result;
97 }
98 }
99
100 // Detect FILTER_REQUIRE_ARRAY flag
101 if ( isset( $var ) && is_int( $options ) && FILTER_REQUIRE_ARRAY === $options ) {
102 if ( ! is_array( $var ) ) {
103 $var = ( 'validator' === $filter_type ) ? false : null;
104 }
105 }
106
107 return $var;
108 }
109
110 public static function is_regex( $var ) {
111 // @codingStandardsIgnoreStart
112 $test = @preg_match( $var, '' );
113 // @codingStandardsIgnoreEnd
114
115 return $test !== false;
116 }
117
118 public static function is_ip_address( $var ) {
119 return false !== WP_Http::is_ip_address( $var );
120 }
121
122 }