-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
class.vartype-php5.php
333 lines (297 loc) · 9.03 KB
/
class.vartype-php5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* PHP 5+ tests.
*
* @package PHPCheatsheets
*
* Select PHPCS exclusions: this file is only included when on PHP 5+.
* @phpcs:disable PHPCompatibility.Classes.NewClasses.exceptionFound
*
* Select PHPCS exclusions: the calls to these functions are wrapped in a function_exists() check.
* @phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.filter_varFound
* @phpcs:disable PHPCompatibility.FunctionUse.NewFunctions.filter_var_arrayFound
*/
// Prevent direct calls to this file.
if ( ! defined( 'APP_DIR' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
/**
* Overload some tests when using PHP5.
*
* These tests are added in the relevant child class of the Vartype class.
*/
class VartypePHP5 {
/**
* The PHP5 specific tests which will overrule the PHP4 compatible tests.
*
* @var array $tests Multi-dimensional array.
*/
public static $tests = array(
/*
* String comparison functions.
*
* @see class.vartype-compare.php
*/
'strcmp' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "strcmp" );',
),
'strcasecmp' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "strcasecmp" );',
),
'strnatcmp' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "strnatcmp" );',
),
'strnatcasecmp' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "strnatcasecmp" );',
),
'strcoll' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "strcoll" );',
),
'similar_text' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "similar_text" );',
),
'levenshtein' => array(
'function' => 'VartypePHP5::compare_strings( $a, $b, "levenshtein" );',
),
/*
* Loose type juggling.
*
* @see class.vartype-test.php
*/
'juggle_int' => array(
'function' => '
try {
if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) {
$x = $x + 0;
if ( is_int( $x ) ) {
pr_int( $x );
}
else if ( is_float( $x ) ) {
pr_flt( $x );
}
else {
pr_var( $x, \'\', true, true );
}
}
else {
trigger_error( \'Unsupported operand types\', E_USER_ERROR );
}
}
catch ( Exception $e ) {
$message = $e->getMessage();
$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
if ( $key === false ) {
$GLOBALS[\'encountered_errors\'][] = $message;
$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
}
echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\';
}
',
),
'juggle_flt' => array(
'function' => '
try {
if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) {
$r = $x + 0.0;
if ( is_float( $r ) ) {
pr_flt( $r );
}
else if ( is_int( $r ) ) {
pr_int( $r );
}
else {
pr_var( $r, \'\', true, true );
}
}
else {
trigger_error( \'Unsupported operand types\', E_USER_ERROR );
}
}
catch ( Exception $e ) {
$message = $e->getMessage();
$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
if ( $key === false ) {
$GLOBALS[\'encountered_errors\'][] = $message;
$key = array_search( $message, $GLOBALS[\'encountered_errors\'] );
}
echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\';
}
',
),
/*
* Some object related functions.
*
* @see class.vartype-test.php
*/
'instanceof' => array(
'function' => '$c = \'TestObject\'; $r = ( $x instanceof $c ); if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); }',
),
);
/**
* Helper method to retrieve the static variable.
* Needed to prevent parse error in PHP4.. *sigh*.
*
* @return array
*/
public static function get_tests() {
return self::$tests;
}
/**
* Ensure we clone an object before using it to avoid contamination by results of previous actions.
*
* @param mixed $value
*
* @return mixed
*/
public static function generate_value( $value ) {
if ( is_object( $value ) ) {
$value = clone $value;
}
if ( is_array( $value ) || is_object( $value ) ) {
reset( $value );
}
return $value;
}
/**
* Smarter way to compare strings in PHP5.
*
* @param mixed $var1
* @param mixed $var2
* @param string $function
*/
public static function compare_strings( $var1, $var2, $function ) {
if ( ( PHP_VERSION_ID >= 50000 && $function === 'levenshtein' ) && ( ( gettype( $var1 ) === 'array' || gettype( $var1 ) === 'resource' ) || ( gettype( $var2 ) === 'array' || gettype( $var2 ) === 'resource' ) ) ) {
try {
pc_compare_strings( $var1, $var2, $function );
}
catch ( Exception $e ) {
self::handle_exception( $e->getMessage() );
}
}
else if ( PHP_VERSION_ID >= 50200 && ( gettype( $var1 ) === 'object' || gettype( $var2 ) === 'object' ) ) {
try {
pc_compare_strings( $var1, $var2, $function );
}
catch ( Exception $e ) {
self::handle_exception( $e->getMessage() );
}
}
else {
pc_compare_strings( $var1, $var2, $function );
}
}
/**
* Helper function to handle exceptions from the string compare function.
*
* @param string $message The error message.
*/
public static function handle_exception( $message ) {
$key = get_error_key( $message );
echo '<span class="error">(Catchable) Fatal error <a href="#', $GLOBALS['test'], '-errors">#', ( $key + 1 ), '</a></span>';
}
/**
* Run tests using the filter extension.
*
* @param mixed $value Value to test.
* @param string|null $expected Expected variable type of the output of the test.
* @param int $filter The Filter to apply.
* @param mixed|null $flags Which filter flags to apply.
* @param mixed|null $options Which options to apply.
*/
public static function filter_combined( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
if ( function_exists( 'filter_var' ) && function_exists( 'filter_var_array' ) ) {
if ( ! is_array( $value ) ) {
self::do_filter_var( $value, $expected, $filter, $flags, $options );
}
else {
self::do_filter_var_array( $value, $filter, $flags, $options );
}
}
else {
echo 'E: not available (PHP 5.2.0+)';
}
}
/**
* Helper function: Run tests using the `filter_var()` function.
*
* @param mixed $value Value to test.
* @param string|null $expected Expected variable type of the output of the test.
* @param int $filter The Filter to apply.
* @param mixed|null $flags Which filter flags to apply.
* @param mixed|null $options Which options to apply.
*/
public static function do_filter_var( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
$opt = array();
if ( isset( $flags ) ) {
$opt['flags'] = $flags;
}
if ( isset( $options ) && ( is_array( $options ) && $options !== array() ) ) {
$opt['options'] = $options;
}
if ( $opt !== array() ) {
$result = filter_var( $value, $filter, $opt );
}
else {
$result = filter_var( $value, $filter );
}
self::print_typed_result( $result, $expected );
}
/**
* Helper function: Run tests using the `filter_var_array()` function.
*
* @param mixed $value Value to test.
* @param int $filter The Filter to apply.
* @param mixed|null $flags Which filter flags to apply.
* @param mixed|null $options Which options to apply.
*/
public static function do_filter_var_array( $value, $filter = FILTER_DEFAULT, $flags = null, $options = null ) {
if ( ! isset( $flags ) && ! isset( $options ) ) {
pr_var( filter_var_array( $value, $filter ), '', true, true );
}
else {
$input = array(
'x' => $value,
);
$filter_def = array(
'x' => array(
'filter' => $filter,
),
);
if ( isset( $flags ) ) {
$filter_def['x']['flags'] = ( FILTER_REQUIRE_ARRAY | $flags );
}
if ( isset( $options ) && ( is_array( $options ) && $options !== array() ) ) {
$filter_def['x']['options'] = $options;
}
$output = filter_var_array( $input, $filter_def );
pr_var( $output['x'], '', true, true );
}
}
/**
* Helper function to print the filter result.
*
* @param mixed $result
* @param string|null $expected Expected variable type of the output of the test.
*/
public static function print_typed_result( $result, $expected = null ) {
switch ( true ) {
case ( $expected === 'bool' && is_bool( $result ) === true ):
pr_bool( $result );
break;
case ( $expected === 'int' && is_int( $result ) === true ):
pr_int( $result );
break;
case ( $expected === 'float' && is_float( $result ) === true ):
pr_flt( $result );
break;
case ( $expected === 'string' && is_string( $result ) === true ):
pr_str( $result );
break;
default:
pr_var( $result, '', true, true );
break;
}
}
}