diff options
author | Pierre Joye <pajoye@php.net> | 2006-05-08 14:39:57 +0000 |
---|---|---|
committer | Pierre Joye <pajoye@php.net> | 2006-05-08 14:39:57 +0000 |
commit | 826a577675ae8a081b10c0383079364f2620c3b0 (patch) | |
tree | e4480a90953b06f596cdda4df8e6918ecb57d1e3 | |
parent | 889f66837c14c02325aa1675eec456860a6a952c (diff) | |
download | php-git-826a577675ae8a081b10c0383079364f2620c3b0.tar.gz |
- add input_get_args (input_get is kept untouched for now)
- add FILTER_FLAG_SCALAR and FILTER_FLAG_ARRAY
- update ReST doc and add an example
-rw-r--r-- | ext/filter/docs/filter.txt | 6 | ||||
-rw-r--r-- | ext/filter/docs/input_get_args.php | 41 | ||||
-rw-r--r-- | ext/filter/filter.c | 229 | ||||
-rw-r--r-- | ext/filter/filter_private.h | 3 | ||||
-rw-r--r-- | ext/filter/package.xml | 3 | ||||
-rw-r--r-- | ext/filter/package2.xml | 27 | ||||
-rw-r--r-- | ext/filter/php_filter.h | 3 |
7 files changed, 280 insertions, 32 deletions
diff --git a/ext/filter/docs/filter.txt b/ext/filter/docs/filter.txt index a82d08876d..180391fd42 100644 --- a/ext/filter/docs/filter.txt +++ b/ext/filter/docs/filter.txt @@ -58,6 +58,12 @@ mixed *input_get* (int *source*, string *name*, [, int *filter* [, mixed *filter filter as specified in *$filter* with a constant, and additional options to the filter through *$filter_options*. +mixed *input_get_args* (array *definitions*, int *source*, [, array *data*]); + Returns an array with all filtered variables defined in 'definition'. + The keys are used as the name of the argument. The value can be either + an integer (flags) or an array of options. This array can contain + the 'filter' type, the 'flags', the 'otptions' or the 'charset' + bool *input_has_variable (int *source*, string *name*); Returns *true* if the variable with the name *name* exists in *source*, or *false* otherwise. diff --git a/ext/filter/docs/input_get_args.php b/ext/filter/docs/input_get_args.php new file mode 100644 index 0000000000..b580524489 --- /dev/null +++ b/ext/filter/docs/input_get_args.php @@ -0,0 +1,41 @@ +<?php +error_reporting(E_ALL|E_STRICT); +$data = array( + 'product_id' => 'product id<script>', + 'component' => '10', + 'versions' => '1.2.33', + 'testscalar' => array('2','23','10','12'), + 'testarray' => '2', +); + +$args = array( + 'product_id' => FILTER_SANITIZE_ENCODED, + 'component' => array('filter' => FILTER_VALIDATE_INT, + 'flags' => FILTER_FLAG_ARRAY, + 'options' => array("min_range"=>1, "max_range"=>10) + ), + + /* equivalent of => FILTER_SANITIZE_ENCODED as SCALAR is + * the default mode + */ + 'versions' => array( + 'filter' => FILTER_SANITIZE_ENCODED, + 'flags' => FILTER_FLAG_SCALAR, + ), + 'doesnotexist' => FILTER_VALIDATE_INT, + 'testscalar' => FILTER_VALIDATE_INT, + 'testarray' => array( + 'filter' => FILTER_VALIDATE_INT, + 'flags' => FILTER_FLAG_ARRAY, + ) + +); + +/* +The other INPUT_* can be used as well. +$myinputs = input_get_args($args, INPUT_POST); +*/ +$myinputs = input_get_args($args, INPUT_DATA, $data); + +var_dump($myinputs); + diff --git a/ext/filter/filter.c b/ext/filter/filter.c index 07561163a1..a9fb1c6f8d 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -14,6 +14,7 @@ +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf <rasmus@php.net> | | Derick Rethans <derick@php.net> | + | Pierre-A. Joye <pierre@php.net> | +----------------------------------------------------------------------+ */ @@ -58,7 +59,7 @@ filter_list_entry filter_list[] = { { "callback", FILTER_CALLBACK, php_filter_callback }, }; - + #ifndef PARSE_ENV #define PARSE_ENV 4 #endif @@ -71,12 +72,17 @@ filter_list_entry filter_list[] = { #define PARSE_SESSION 6 #endif +#ifndef PARSE_DATA +#define PARSE_DATA 7 +#endif + static unsigned int php_sapi_filter(int arg, char *var, char **val, unsigned int val_len, unsigned int *new_val_len TSRMLS_DC); /* {{{ filter_functions[] */ zend_function_entry filter_functions[] = { PHP_FE(input_get, NULL) + PHP_FE(input_get_args, NULL) PHP_FE(input_filters_list, NULL) PHP_FE(input_has_variable, NULL) PHP_FE(input_name_to_filter, NULL) @@ -167,15 +173,19 @@ PHP_MINIT_FUNCTION(filter) REGISTER_INI_ENTRIES(); - REGISTER_LONG_CONSTANT("INPUT_POST", PARSE_POST, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("INPUT_GET", PARSE_GET, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("INPUT_COOKIE", PARSE_COOKIE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("INPUT_ENV", PARSE_ENV, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("INPUT_SERVER", PARSE_SERVER, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("INPUT_SESSION", PARSE_SESSION, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_POST", PARSE_POST, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_GET", PARSE_GET, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_COOKIE", PARSE_COOKIE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_ENV", PARSE_ENV, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_SERVER", PARSE_SERVER, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_SESSION", PARSE_SESSION, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("INPUT_DATA", PARSE_DATA, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_FLAG_NONE", FILTER_FLAG_NONE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FILTER_FLAG_SCALAR", FILTER_FLAG_SCALAR, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FILTER_FLAG_ARRAY", FILTER_FLAG_ARRAY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FILTER_VALIDATE_INT", FILTER_VALIDATE_INT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_BOOLEAN", FILTER_VALIDATE_BOOLEAN, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_FLOAT", FILTER_VALIDATE_FLOAT, CONST_CS | CONST_PERSISTENT); @@ -294,7 +304,7 @@ static filter_list_entry php_find_filter(long id) static void php_zval_filter(zval *value, long filter, long flags, zval *options, char* charset TSRMLS_DC) { filter_list_entry filter_func; - + filter_func = php_find_filter(filter); if (!filter_func.id) { @@ -415,9 +425,29 @@ static void php_zval_filter_recursive(zval *value, long filter, long flags, zval } /* }}} */ -#define FIND_SOURCE(a,t) \ - array_ptr = IF_G(a); \ - break; +static zval * php_filter_get_storage(long arg TSRMLS_DC) +{ + zval * array_ptr = NULL; + switch (arg) { + case PARSE_GET: + array_ptr = IF_G(get_array); + break; + case PARSE_POST: + array_ptr = IF_G(post_array); + break; + case PARSE_COOKIE: + array_ptr = IF_G(cookie_array); + break; + case PARSE_SERVER: + array_ptr = IF_G(server_array); + break; + case PARSE_ENV: + array_ptr = IF_G(env_array); + break; + } + + return array_ptr; +} /* {{{ proto mixed input_has_variable(constant type, string variable_name) */ @@ -435,13 +465,7 @@ PHP_FUNCTION(input_has_variable) return; } - switch (arg) { - case PARSE_GET: FIND_SOURCE(get_array, TRACK_VARS_GET) - case PARSE_POST: FIND_SOURCE(post_array, TRACK_VARS_POST) - case PARSE_COOKIE: FIND_SOURCE(cookie_array, TRACK_VARS_COOKIE) - case PARSE_SERVER: FIND_SOURCE(server_array, TRACK_VARS_SERVER) - case PARSE_ENV: FIND_SOURCE(env_array, TRACK_VARS_ENV) - } + array_ptr = php_filter_get_storage(arg TSRMLS_CC); if (!array_ptr) { RETURN_FALSE; @@ -495,17 +519,23 @@ PHP_FUNCTION(input_get) } switch(arg) { - case PARSE_GET: FIND_SOURCE(get_array, TRACK_VARS_GET) - case PARSE_POST: FIND_SOURCE(post_array, TRACK_VARS_POST) - case PARSE_COOKIE: FIND_SOURCE(cookie_array, TRACK_VARS_COOKIE) - case PARSE_SERVER: FIND_SOURCE(server_array, TRACK_VARS_SERVER) - case PARSE_ENV: FIND_SOURCE(env_array, TRACK_VARS_ENV) + case PARSE_GET: + case PARSE_POST: + case PARSE_COOKIE: + case PARSE_SERVER: + case PARSE_ENV: + array_ptr = php_filter_get_storage(arg TSRMLS_CC); + break; case PARSE_SESSION: /* FIXME: Implement session source */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "INPUT_SESSION not implemented"); break; case PARSE_REQUEST: + /* FIXME: Implement session source */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "INPUT_SESSION not implemented"); + return; if (PG(variables_order)) { zval **a_ptr = &array_ptr; char *p, *variables_order = PG(variables_order); @@ -541,14 +571,13 @@ PHP_FUNCTION(input_get) a_ptr = &array_ptr2; continue; } - if (array_ptr2 && !array_ptr3) { + if (array_ptr2 && !array_ptr3) { a_ptr = &array_ptr3; } } } else { - FIND_SOURCE(get_array, TRACK_VARS_GET) + array_ptr = php_filter_get_storage(PARSE_GET); } - } if (!array_ptr) { @@ -560,7 +589,7 @@ PHP_FUNCTION(input_get) if (hash_ptr && zend_hash_find(hash_ptr, var, var_len + 1, (void **)&tmp) == SUCCESS) { *return_value = **tmp; found = 1; - } + } } if (array_ptr2 && !found) { @@ -589,6 +618,152 @@ PHP_FUNCTION(input_get) } /* }}} */ +/* {{{ proto mixed input_get_args(array definition, constant type [, array data]) + * returns an array with all arguments defined in 'definition'. The key is used + * as the name of the argument. The value can be either an integer (flags) or + * an of options. This array can contain the 'filter' type, the 'flags', + * the 'otptions' or the 'charset' + */ +PHP_FUNCTION(input_get_args) +{ + long arg, filter = FILTER_DEFAULT; + char *charset = NULL; + zval **tmp, ** option; + int filter_flags = FILTER_FLAG_SCALAR; + zval *options = NULL, *temparray = NULL; + + zval *args_array, *values; + HashTable *args_hash; + HashPosition pos; + + long args_from = 0; + long elm_count; + char *key; + unsigned int key_len; + unsigned long index; + + /* pointers to the zval array GET, POST,... */ + zval *array_ptr = NULL; + zval **element; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al|a", &args_array, &args_from, &values) == FAILURE) { + RETURN_FALSE; + } + + args_hash = HASH_OF(args_array); + elm_count = zend_hash_num_elements(args_hash); + + if (elm_count < 1) { + RETURN_NULL(); + } + + switch (args_from) { + case PARSE_GET: + case PARSE_POST: + case PARSE_COOKIE: + case PARSE_SERVER: + case PARSE_ENV: + array_ptr = php_filter_get_storage(arg TSRMLS_CC); + break; + + case PARSE_DATA: + array_ptr = values; + break; + + case PARSE_SESSION: + /* FIXME: Implement session source */ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "INPUT_SESSION not implemented"); + break; + } + + if (!array_ptr) { + RETURN_FALSE; + } + + HashTable * g_hash = HASH_OF(array_ptr); + zend_hash_internal_pointer_reset_ex(g_hash, &pos); + array_init(return_value); + + for (zend_hash_internal_pointer_reset_ex(args_hash, &pos); + zend_hash_get_current_data_ex(args_hash, (void **) &element, &pos) == SUCCESS; + zend_hash_move_forward_ex(args_hash, &pos)) { + + if (zend_hash_get_current_key_ex(args_hash, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING) { + zval_dtor(return_value); + RETURN_FALSE; + } + + if (g_hash && zend_hash_find(g_hash, key, key_len, (void **)&tmp) == SUCCESS) { + if (Z_TYPE_PP(element) != IS_ARRAY) { + convert_to_long(*element); + filter = Z_LVAL_PP(element); + } else { + if (zend_hash_find(HASH_OF(*element), "filter", sizeof("filter"), (void **)&option) == SUCCESS) { + convert_to_long(*option); + filter = Z_LVAL_PP(option); + } + + if (zend_hash_find(HASH_OF(*element), "options", sizeof("options"), (void **)&option) == SUCCESS) { + if (Z_TYPE_PP(option) == IS_ARRAY) { + options = *option; + } + } else { + options = NULL; + } + + if (zend_hash_find(HASH_OF(*element), "flags", sizeof("flags"), (void **)&option) == SUCCESS) { + + switch (Z_TYPE_PP(option)) { + case IS_ARRAY: + break; + default: + convert_to_long(*option); + filter_flags = Z_LVAL_PP(option); + break; + } + } else { + filter_flags = FILTER_FLAG_SCALAR; + } + + if (filter_flags & FILTER_FLAG_SCALAR && Z_TYPE_PP(tmp) == IS_ARRAY) { + /* asked for scalar and found an array do not test further */ + add_assoc_bool(return_value, key, 0); + continue; + } + + if (zend_hash_find(HASH_OF(*element), "charset", sizeof("charset"), (void **)&option) == SUCCESS) { + convert_to_string(*option); + charset = Z_STRVAL_PP(option); + } + } + + if (filter_flags & FILTER_FLAG_SCALAR) { + php_zval_filter(*tmp, filter, filter_flags, options, charset TSRMLS_CC); + } else { + php_zval_filter_recursive(*tmp, filter, filter_flags, options, charset TSRMLS_CC); + + /* ARRAY always returns an array */ + if (Z_TYPE_PP(tmp) != IS_ARRAY) { + ALLOC_INIT_ZVAL(temparray); + array_init(temparray); + add_next_index_zval(temparray, *tmp); + *tmp = temparray; + } + } + + if (Z_TYPE_PP(tmp) == IS_NULL) { + add_assoc_bool(return_value, key, 0); + } else { + add_assoc_zval(return_value, key, *tmp); + } + } else { + add_assoc_null(return_value, key); + } + filter_flags = 0; + } +} +/* }}} */ + /* {{{ proto input_filters_list() * Returns a list of all supported filters */ PHP_FUNCTION(input_filters_list) diff --git a/ext/filter/filter_private.h b/ext/filter/filter_private.h index a1c0d42635..a64ac40900 100644 --- a/ext/filter/filter_private.h +++ b/ext/filter/filter_private.h @@ -23,6 +23,9 @@ #define FILTER_FLAG_NONE 0x0000 +#define FILTER_FLAG_ARRAY 0x1000000 +#define FILTER_FLAG_SCALAR 0x2000000 + #define FILTER_FLAG_ALLOW_OCTAL 0x0001 #define FILTER_FLAG_ALLOW_HEX 0x0002 diff --git a/ext/filter/package.xml b/ext/filter/package.xml index aab16aa1c8..5f87d6cf08 100644 --- a/ext/filter/package.xml +++ b/ext/filter/package.xml @@ -29,7 +29,10 @@ of filters and mechanisms that users can use to safely access their input data. <notes> - Fixed PECL bug #6639: uppercase hexadecimal digits are not supported. - Implemented PECL req #6641: negative values for hexadecimal and octal numbers are not supported. +- Added support for php pcre expressions (Pierre) - Fixed Possible leak in internal sapi_filter (Pierre) +- Added input_get_args, fetches all input in one call (Pierre) +- Added FILTER_FLAG_SCALAR and FILTER_FLAG_ARRAY , allows or not array values (Pierre) </notes> <filelist> <file role="src" name="config.m4"/> diff --git a/ext/filter/package2.xml b/ext/filter/package2.xml index afa150a8dd..8222bd3c1c 100644 --- a/ext/filter/package2.xml +++ b/ext/filter/package2.xml @@ -33,10 +33,12 @@ of filters and mechanisms that users can use to safely access their input data. <api>beta</api> </stability> <license uri="http://www.php.net/license">PHP</license> - <notes>- Reimplement php_filter_callback() as exception-safe and without memleaks -- Renamed all constants. -- Fixed PECL bug #6124: Crash on HTML tags when using FS_STRING - + <notes>- Fixed PECL bug #6639: uppercase hexadecimal digits are not supported +- Implemented PECL req #6641: negative values for hexadecimal and octal numbers are not supported. +- Added support for php pcre expressions (Pierre) +- Fixed Possible leak in internal sapi_filter (Pierre) +- Added input_get_args, fetches all input in one call (Pierre) +- Added FILTER_FLAG_SCALAR and FILTER_FLAG_ARRAY , allows or not array values (Pierre) </notes> <contents> <dir name="/"> @@ -63,4 +65,21 @@ of filters and mechanisms that users can use to safely access their input data. </dependencies> <providesextension>filter</providesextension> <extsrcrelease /> + <changelog> + <release> + <date>2006-01-23</date> + <time>10:04:15</time> + <version> + <release>0.9.4</release> + <api>0.9.4</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <notes>- Reimplement php_filter_callback() as exception-safe and without memleaks +- Renamed all constants. +- Fixed PECL bug #6124: Crash on HTML tags when using FS_STRING</notes> + </release> + </changelog> </package> diff --git a/ext/filter/php_filter.h b/ext/filter/php_filter.h index fd22b5a06c..4c2b9381bb 100644 --- a/ext/filter/php_filter.h +++ b/ext/filter/php_filter.h @@ -50,6 +50,7 @@ PHP_RSHUTDOWN_FUNCTION(filter); PHP_MINFO_FUNCTION(filter); PHP_FUNCTION(input_get); +PHP_FUNCTION(input_get_args); PHP_FUNCTION(input_filters_list); PHP_FUNCTION(input_has_variable); PHP_FUNCTION(input_name_to_filter); @@ -93,7 +94,7 @@ void php_filter_number_float(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_magic_quotes(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_callback(PHP_INPUT_FILTER_PARAM_DECL); - + #endif /* FILTER_H */ /* |