PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/filter - filter.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 336 369 91.1 %
Date: 2016-09-27 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2016 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
      16             :   |          Derick Rethans <derick@php.net>                             |
      17             :   |          Pierre-A. Joye <pierre@php.net>                             |
      18             :   |          Ilia Alshanetsky <iliaa@php.net>                            |
      19             :   +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : /* $Id: 9cc4bdc40f4013d538d685f912be2e9698c649cc $ */
      23             : 
      24             : #ifdef HAVE_CONFIG_H
      25             : #include "config.h"
      26             : #endif
      27             : 
      28             : #include "php_filter.h"
      29             : 
      30             : ZEND_DECLARE_MODULE_GLOBALS(filter)
      31             : 
      32             : #include "filter_private.h"
      33             : 
      34             : typedef struct filter_list_entry {
      35             :         const char *name;
      36             :         int    id;
      37             :         void (*function)(PHP_INPUT_FILTER_PARAM_DECL);
      38             : } filter_list_entry;
      39             : 
      40             : /* {{{ filter_list */
      41             : static const filter_list_entry filter_list[] = {
      42             :         { "int",             FILTER_VALIDATE_INT,           php_filter_int             },
      43             :         { "boolean",         FILTER_VALIDATE_BOOLEAN,       php_filter_boolean         },
      44             :         { "float",           FILTER_VALIDATE_FLOAT,         php_filter_float           },
      45             : 
      46             :         { "validate_regexp", FILTER_VALIDATE_REGEXP,        php_filter_validate_regexp },
      47             :         { "validate_domain", FILTER_VALIDATE_DOMAIN,        php_filter_validate_domain },
      48             :         { "validate_url",    FILTER_VALIDATE_URL,           php_filter_validate_url    },
      49             :         { "validate_email",  FILTER_VALIDATE_EMAIL,         php_filter_validate_email  },
      50             :         { "validate_ip",     FILTER_VALIDATE_IP,            php_filter_validate_ip     },
      51             :         { "validate_mac",    FILTER_VALIDATE_MAC,           php_filter_validate_mac    },
      52             : 
      53             :         { "string",          FILTER_SANITIZE_STRING,        php_filter_string          },
      54             :         { "stripped",        FILTER_SANITIZE_STRING,        php_filter_string          },
      55             :         { "encoded",         FILTER_SANITIZE_ENCODED,       php_filter_encoded         },
      56             :         { "special_chars",   FILTER_SANITIZE_SPECIAL_CHARS, php_filter_special_chars   },
      57             :         { "full_special_chars",   FILTER_SANITIZE_FULL_SPECIAL_CHARS, php_filter_full_special_chars   },
      58             :         { "unsafe_raw",      FILTER_UNSAFE_RAW,             php_filter_unsafe_raw      },
      59             :         { "email",           FILTER_SANITIZE_EMAIL,         php_filter_email           },
      60             :         { "url",             FILTER_SANITIZE_URL,           php_filter_url             },
      61             :         { "number_int",      FILTER_SANITIZE_NUMBER_INT,    php_filter_number_int      },
      62             :         { "number_float",    FILTER_SANITIZE_NUMBER_FLOAT,  php_filter_number_float    },
      63             :         { "magic_quotes",    FILTER_SANITIZE_MAGIC_QUOTES,  php_filter_magic_quotes    },
      64             : 
      65             :         { "callback",        FILTER_CALLBACK,               php_filter_callback        },
      66             : };
      67             : /* }}} */
      68             : 
      69             : #ifndef PARSE_ENV
      70             : #define PARSE_ENV 4
      71             : #endif
      72             : 
      73             : #ifndef PARSE_SERVER
      74             : #define PARSE_SERVER 5
      75             : #endif
      76             : 
      77             : #ifndef PARSE_SESSION
      78             : #define PARSE_SESSION 6
      79             : #endif
      80             : 
      81             : static unsigned int php_sapi_filter(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
      82             : static unsigned int php_sapi_filter_init(void);
      83             : 
      84             : /* {{{ arginfo */
      85             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input, 0, 0, 2)
      86             :         ZEND_ARG_INFO(0, type)
      87             :         ZEND_ARG_INFO(0, variable_name)
      88             :         ZEND_ARG_INFO(0, filter)
      89             :         ZEND_ARG_INFO(0, options)
      90             : ZEND_END_ARG_INFO()
      91             : 
      92             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var, 0, 0, 1)
      93             :         ZEND_ARG_INFO(0, variable)
      94             :         ZEND_ARG_INFO(0, filter)
      95             :         ZEND_ARG_INFO(0, options)
      96             : ZEND_END_ARG_INFO()
      97             : 
      98             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input_array, 0, 0, 1)
      99             :         ZEND_ARG_INFO(0, type)
     100             :         ZEND_ARG_INFO(0, definition)
     101             :         ZEND_ARG_INFO(0, add_empty)
     102             : ZEND_END_ARG_INFO()
     103             : 
     104             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var_array, 0, 0, 1)
     105             :         ZEND_ARG_INFO(0, data)
     106             :         ZEND_ARG_INFO(0, definition)
     107             :         ZEND_ARG_INFO(0, add_empty)
     108             : ZEND_END_ARG_INFO()
     109             : 
     110             : ZEND_BEGIN_ARG_INFO(arginfo_filter_list, 0)
     111             : ZEND_END_ARG_INFO()
     112             : 
     113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_has_var, 0, 0, 2)
     114             :         ZEND_ARG_INFO(0, type)
     115             :         ZEND_ARG_INFO(0, variable_name)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_id, 0, 0, 1)
     119             :         ZEND_ARG_INFO(0, filtername)
     120             : ZEND_END_ARG_INFO()
     121             : /* }}} */
     122             : 
     123             : /* {{{ filter_functions[]
     124             :  */
     125             : static const zend_function_entry filter_functions[] = {
     126             :         PHP_FE(filter_input,            arginfo_filter_input)
     127             :         PHP_FE(filter_var,              arginfo_filter_var)
     128             :         PHP_FE(filter_input_array,      arginfo_filter_input_array)
     129             :         PHP_FE(filter_var_array,        arginfo_filter_var_array)
     130             :         PHP_FE(filter_list,             arginfo_filter_list)
     131             :         PHP_FE(filter_has_var,          arginfo_filter_has_var)
     132             :         PHP_FE(filter_id,               arginfo_filter_id)
     133             :         PHP_FE_END
     134             : };
     135             : /* }}} */
     136             : 
     137             : /* {{{ filter_module_entry
     138             :  */
     139             : zend_module_entry filter_module_entry = {
     140             :         STANDARD_MODULE_HEADER,
     141             :         "filter",
     142             :         filter_functions,
     143             :         PHP_MINIT(filter),
     144             :         PHP_MSHUTDOWN(filter),
     145             :         NULL,
     146             :         PHP_RSHUTDOWN(filter),
     147             :         PHP_MINFO(filter),
     148             :         PHP_FILTER_VERSION,
     149             :         STANDARD_MODULE_PROPERTIES
     150             : };
     151             : /* }}} */
     152             : 
     153             : #ifdef COMPILE_DL_FILTER
     154             : #ifdef ZTS
     155             : ZEND_TSRMLS_CACHE_DEFINE()
     156             : #endif
     157             : ZEND_GET_MODULE(filter)
     158             : #endif
     159             : 
     160       23950 : static PHP_INI_MH(UpdateDefaultFilter) /* {{{ */
     161             : {
     162       23950 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     163             : 
     164      359230 :         for (i = 0; i < size; ++i) {
     165      359230 :                 if ((strcasecmp(ZSTR_VAL(new_value), filter_list[i].name) == 0)) {
     166       23950 :                         IF_G(default_filter) = filter_list[i].id;
     167       23950 :                         return SUCCESS;
     168             :                 }
     169             :         }
     170             :         /* Fallback to the default filter */
     171           0 :         IF_G(default_filter) = FILTER_DEFAULT;
     172           0 :         return SUCCESS;
     173             : }
     174             : /* }}} */
     175             : 
     176             : /* {{{ PHP_INI
     177             :  */
     178       23950 : static PHP_INI_MH(OnUpdateFlags)
     179             : {
     180       23950 :         if (!new_value) {
     181       23944 :                 IF_G(default_filter_flags) = FILTER_FLAG_NO_ENCODE_QUOTES;
     182             :         } else {
     183           6 :                 IF_G(default_filter_flags) = atoi(ZSTR_VAL(new_value));
     184             :         }
     185       23950 :         return SUCCESS;
     186             : }
     187             : 
     188             : PHP_INI_BEGIN()
     189             :         STD_PHP_INI_ENTRY("filter.default",   "unsafe_raw", PHP_INI_SYSTEM|PHP_INI_PERDIR, UpdateDefaultFilter, default_filter, zend_filter_globals, filter_globals)
     190             :         PHP_INI_ENTRY("filter.default_flags", NULL,     PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateFlags)
     191             : PHP_INI_END()
     192             : /* }}} */
     193             : 
     194       23950 : static void php_filter_init_globals(zend_filter_globals *filter_globals) /* {{{ */
     195             : {
     196             : #if defined(COMPILE_DL_FILTER) && defined(ZTS)
     197             : ZEND_TSRMLS_CACHE_UPDATE();
     198             : #endif
     199       23950 :         ZVAL_UNDEF(&filter_globals->post_array);
     200       23950 :         ZVAL_UNDEF(&filter_globals->get_array);
     201       23950 :         ZVAL_UNDEF(&filter_globals->cookie_array);
     202       23950 :         ZVAL_UNDEF(&filter_globals->env_array);
     203       23950 :         ZVAL_UNDEF(&filter_globals->server_array);
     204       23950 :         ZVAL_UNDEF(&filter_globals->session_array);
     205       23950 :         filter_globals->default_filter = FILTER_DEFAULT;
     206       23950 : }
     207             : /* }}} */
     208             : 
     209             : #define PARSE_REQUEST 99
     210             : 
     211             : /* {{{ PHP_MINIT_FUNCTION
     212             :  */
     213       23950 : PHP_MINIT_FUNCTION(filter)
     214             : {
     215       23950 :         ZEND_INIT_MODULE_GLOBALS(filter, php_filter_init_globals, NULL);
     216             : 
     217       23950 :         REGISTER_INI_ENTRIES();
     218             : 
     219       23950 :         REGISTER_LONG_CONSTANT("INPUT_POST",  PARSE_POST,     CONST_CS | CONST_PERSISTENT);
     220       23950 :         REGISTER_LONG_CONSTANT("INPUT_GET",           PARSE_GET,              CONST_CS | CONST_PERSISTENT);
     221       23950 :         REGISTER_LONG_CONSTANT("INPUT_COOKIE",        PARSE_COOKIE,   CONST_CS | CONST_PERSISTENT);
     222       23950 :         REGISTER_LONG_CONSTANT("INPUT_ENV",           PARSE_ENV,              CONST_CS | CONST_PERSISTENT);
     223       23950 :         REGISTER_LONG_CONSTANT("INPUT_SERVER",        PARSE_SERVER,   CONST_CS | CONST_PERSISTENT);
     224       23950 :         REGISTER_LONG_CONSTANT("INPUT_SESSION", PARSE_SESSION,        CONST_CS | CONST_PERSISTENT);
     225       23950 :         REGISTER_LONG_CONSTANT("INPUT_REQUEST", PARSE_REQUEST,        CONST_CS | CONST_PERSISTENT);
     226             : 
     227       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NONE", FILTER_FLAG_NONE, CONST_CS | CONST_PERSISTENT);
     228             : 
     229       23950 :         REGISTER_LONG_CONSTANT("FILTER_REQUIRE_SCALAR", FILTER_REQUIRE_SCALAR, CONST_CS | CONST_PERSISTENT);
     230       23950 :         REGISTER_LONG_CONSTANT("FILTER_REQUIRE_ARRAY", FILTER_REQUIRE_ARRAY, CONST_CS | CONST_PERSISTENT);
     231       23950 :         REGISTER_LONG_CONSTANT("FILTER_FORCE_ARRAY", FILTER_FORCE_ARRAY, CONST_CS | CONST_PERSISTENT);
     232       23950 :         REGISTER_LONG_CONSTANT("FILTER_NULL_ON_FAILURE", FILTER_NULL_ON_FAILURE, CONST_CS | CONST_PERSISTENT);
     233             : 
     234       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_INT", FILTER_VALIDATE_INT, CONST_CS | CONST_PERSISTENT);
     235       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_BOOLEAN", FILTER_VALIDATE_BOOLEAN, CONST_CS | CONST_PERSISTENT);
     236       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_FLOAT", FILTER_VALIDATE_FLOAT, CONST_CS | CONST_PERSISTENT);
     237             : 
     238       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_REGEXP", FILTER_VALIDATE_REGEXP, CONST_CS | CONST_PERSISTENT);
     239       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_DOMAIN", FILTER_VALIDATE_DOMAIN, CONST_CS | CONST_PERSISTENT);
     240       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_URL", FILTER_VALIDATE_URL, CONST_CS | CONST_PERSISTENT);
     241       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_EMAIL", FILTER_VALIDATE_EMAIL, CONST_CS | CONST_PERSISTENT);
     242       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_IP", FILTER_VALIDATE_IP, CONST_CS | CONST_PERSISTENT);
     243       23950 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_MAC", FILTER_VALIDATE_MAC, CONST_CS | CONST_PERSISTENT);
     244             : 
     245       23950 :         REGISTER_LONG_CONSTANT("FILTER_DEFAULT", FILTER_DEFAULT, CONST_CS | CONST_PERSISTENT);
     246       23950 :         REGISTER_LONG_CONSTANT("FILTER_UNSAFE_RAW", FILTER_UNSAFE_RAW, CONST_CS | CONST_PERSISTENT);
     247             : 
     248       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRING", FILTER_SANITIZE_STRING, CONST_CS | CONST_PERSISTENT);
     249       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRIPPED", FILTER_SANITIZE_STRING, CONST_CS | CONST_PERSISTENT);
     250       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_ENCODED", FILTER_SANITIZE_ENCODED, CONST_CS | CONST_PERSISTENT);
     251       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_SPECIAL_CHARS", FILTER_SANITIZE_SPECIAL_CHARS, CONST_CS | CONST_PERSISTENT);
     252       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_FULL_SPECIAL_CHARS", FILTER_SANITIZE_FULL_SPECIAL_CHARS, CONST_CS | CONST_PERSISTENT);
     253       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_EMAIL", FILTER_SANITIZE_EMAIL, CONST_CS | CONST_PERSISTENT);
     254       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_URL", FILTER_SANITIZE_URL, CONST_CS | CONST_PERSISTENT);
     255       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_NUMBER_INT", FILTER_SANITIZE_NUMBER_INT, CONST_CS | CONST_PERSISTENT);
     256       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_NUMBER_FLOAT", FILTER_SANITIZE_NUMBER_FLOAT, CONST_CS | CONST_PERSISTENT);
     257       23950 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_MAGIC_QUOTES", FILTER_SANITIZE_MAGIC_QUOTES, CONST_CS | CONST_PERSISTENT);
     258             : 
     259       23950 :         REGISTER_LONG_CONSTANT("FILTER_CALLBACK", FILTER_CALLBACK, CONST_CS | CONST_PERSISTENT);
     260             : 
     261       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_OCTAL", FILTER_FLAG_ALLOW_OCTAL, CONST_CS | CONST_PERSISTENT);
     262       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_HEX", FILTER_FLAG_ALLOW_HEX, CONST_CS | CONST_PERSISTENT);
     263             : 
     264       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_LOW", FILTER_FLAG_STRIP_LOW, CONST_CS | CONST_PERSISTENT);
     265       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_HIGH", FILTER_FLAG_STRIP_HIGH, CONST_CS | CONST_PERSISTENT);
     266       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_BACKTICK", FILTER_FLAG_STRIP_BACKTICK, CONST_CS | CONST_PERSISTENT);
     267       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_LOW", FILTER_FLAG_ENCODE_LOW, CONST_CS | CONST_PERSISTENT);
     268       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_HIGH", FILTER_FLAG_ENCODE_HIGH, CONST_CS | CONST_PERSISTENT);
     269       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_AMP", FILTER_FLAG_ENCODE_AMP, CONST_CS | CONST_PERSISTENT);
     270       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_ENCODE_QUOTES", FILTER_FLAG_NO_ENCODE_QUOTES, CONST_CS | CONST_PERSISTENT);
     271       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_EMPTY_STRING_NULL", FILTER_FLAG_EMPTY_STRING_NULL, CONST_CS | CONST_PERSISTENT);
     272             : 
     273       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_FRACTION", FILTER_FLAG_ALLOW_FRACTION, CONST_CS | CONST_PERSISTENT);
     274       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_THOUSAND", FILTER_FLAG_ALLOW_THOUSAND, CONST_CS | CONST_PERSISTENT);
     275       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_SCIENTIFIC", FILTER_FLAG_ALLOW_SCIENTIFIC, CONST_CS | CONST_PERSISTENT);
     276             : 
     277       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_SCHEME_REQUIRED", FILTER_FLAG_SCHEME_REQUIRED, CONST_CS | CONST_PERSISTENT);
     278       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_HOST_REQUIRED", FILTER_FLAG_HOST_REQUIRED, CONST_CS | CONST_PERSISTENT);
     279       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_PATH_REQUIRED", FILTER_FLAG_PATH_REQUIRED, CONST_CS | CONST_PERSISTENT);
     280       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_QUERY_REQUIRED", FILTER_FLAG_QUERY_REQUIRED, CONST_CS | CONST_PERSISTENT);
     281             : 
     282       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_IPV4", FILTER_FLAG_IPV4, CONST_CS | CONST_PERSISTENT);
     283       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_IPV6", FILTER_FLAG_IPV6, CONST_CS | CONST_PERSISTENT);
     284       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_RES_RANGE", FILTER_FLAG_NO_RES_RANGE, CONST_CS | CONST_PERSISTENT);
     285       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_PRIV_RANGE", FILTER_FLAG_NO_PRIV_RANGE, CONST_CS | CONST_PERSISTENT);
     286             : 
     287       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_HOSTNAME", FILTER_FLAG_HOSTNAME, CONST_CS | CONST_PERSISTENT);
     288             : 
     289       23950 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_EMAIL_UNICODE", FILTER_FLAG_EMAIL_UNICODE, CONST_CS | CONST_PERSISTENT);
     290             : 
     291       23950 :         sapi_register_input_filter(php_sapi_filter, php_sapi_filter_init);
     292             : 
     293       23950 :         return SUCCESS;
     294             : }
     295             : /* }}} */
     296             : 
     297             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     298             :  */
     299       23969 : PHP_MSHUTDOWN_FUNCTION(filter)
     300             : {
     301       23969 :         UNREGISTER_INI_ENTRIES();
     302             : 
     303       23969 :         return SUCCESS;
     304             : }
     305             : /* }}} */
     306             : 
     307             : /* {{{ PHP_RSHUTDOWN_FUNCTION
     308             :  */
     309             : #define VAR_ARRAY_COPY_DTOR(a)   \
     310             :         if (!Z_ISUNDEF(IF_G(a))) {   \
     311             :                 zval_ptr_dtor(&IF_G(a)); \
     312             :                 ZVAL_UNDEF(&IF_G(a));    \
     313             :         }
     314             : 
     315       23925 : PHP_RSHUTDOWN_FUNCTION(filter)
     316             : {
     317       23925 :         VAR_ARRAY_COPY_DTOR(get_array)
     318       23925 :         VAR_ARRAY_COPY_DTOR(post_array)
     319       23925 :         VAR_ARRAY_COPY_DTOR(cookie_array)
     320       23925 :         VAR_ARRAY_COPY_DTOR(server_array)
     321       23925 :         VAR_ARRAY_COPY_DTOR(env_array)
     322       23925 :         VAR_ARRAY_COPY_DTOR(session_array)
     323       23925 :         return SUCCESS;
     324             : }
     325             : /* }}} */
     326             : 
     327             : /* {{{ PHP_MINFO_FUNCTION
     328             :  */
     329         143 : PHP_MINFO_FUNCTION(filter)
     330             : {
     331         143 :         php_info_print_table_start();
     332         143 :         php_info_print_table_row( 2, "Input Validation and Filtering", "enabled" );
     333         143 :         php_info_print_table_row( 2, "Revision", "$Id: 9cc4bdc40f4013d538d685f912be2e9698c649cc $");
     334         143 :         php_info_print_table_end();
     335             : 
     336         143 :         DISPLAY_INI_ENTRIES();
     337         143 : }
     338             : /* }}} */
     339             : 
     340        1105 : static filter_list_entry php_find_filter(zend_long id) /* {{{ */
     341             : {
     342        1105 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     343             : 
     344        9556 :         for (i = 0; i < size; ++i) {
     345        9551 :                 if (filter_list[i].id == id) {
     346        1100 :                         return filter_list[i];
     347             :                 }
     348             :         }
     349             :         /* Fallback to "string" filter */
     350          75 :         for (i = 0; i < size; ++i) {
     351          75 :                 if (filter_list[i].id == FILTER_DEFAULT) {
     352           5 :                         return filter_list[i];
     353             :                 }
     354             :         }
     355             :         /* To shut up GCC */
     356           0 :         return filter_list[0];
     357             : }
     358             : /* }}} */
     359             : 
     360       23904 : static unsigned int php_sapi_filter_init(void)
     361             : {
     362       23904 :         ZVAL_UNDEF(&IF_G(get_array));
     363       23904 :         ZVAL_UNDEF(&IF_G(post_array));
     364       23904 :         ZVAL_UNDEF(&IF_G(cookie_array));
     365       23904 :         ZVAL_UNDEF(&IF_G(server_array));
     366       23904 :         ZVAL_UNDEF(&IF_G(env_array));
     367       23904 :         ZVAL_UNDEF(&IF_G(session_array));
     368       23904 :         return SUCCESS;
     369             : }
     370             : 
     371        1105 : static void php_zval_filter(zval *value, zend_long filter, zend_long flags, zval *options, char* charset, zend_bool copy) /* {{{ */
     372             : {
     373             :         filter_list_entry  filter_func;
     374             : 
     375        1105 :         filter_func = php_find_filter(filter);
     376             : 
     377        1105 :         if (!filter_func.id) {
     378             :                 /* Find default filter */
     379           0 :                 filter_func = php_find_filter(FILTER_DEFAULT);
     380             :         }
     381             : 
     382        1105 :         if (copy) {
     383        1830 :                 SEPARATE_ZVAL(value);
     384             :         }
     385             : 
     386             :         /* #49274, fatal error with object without a toString method
     387             :           Fails nicely instead of getting a recovarable fatal error. */
     388        1105 :         if (Z_TYPE_P(value) == IS_OBJECT) {
     389             :                 zend_class_entry *ce;
     390             : 
     391           5 :                 ce = Z_OBJCE_P(value);
     392           5 :                 if (!ce->__tostring) {
     393           4 :                         zval_ptr_dtor(value);
     394             :                         /* #67167: doesn't return null on failure for objects */
     395           4 :                         if (flags & FILTER_NULL_ON_FAILURE) {
     396           2 :                                 ZVAL_NULL(value);
     397             :                         } else {
     398           2 :                                 ZVAL_FALSE(value);
     399             :                         }
     400           4 :                         goto handle_default;
     401             :                 }
     402             :         }
     403             : 
     404             :         /* Here be strings */
     405        1101 :         convert_to_string(value);
     406             : 
     407        1101 :         filter_func.function(value, flags, options, charset);
     408             : 
     409             : handle_default:
     410        1375 :         if (options && (Z_TYPE_P(options) == IS_ARRAY || Z_TYPE_P(options) == IS_OBJECT) &&
     411          43 :                 ((flags & FILTER_NULL_ON_FAILURE && Z_TYPE_P(value) == IS_NULL) ||
     412          42 :                 (!(flags & FILTER_NULL_ON_FAILURE) && Z_TYPE_P(value) == IS_FALSE)) &&
     413          28 :                 zend_hash_str_exists(HASH_OF(options), "default", sizeof("default") - 1)) {
     414             :                 zval *tmp;
     415           3 :                 if ((tmp = zend_hash_str_find(HASH_OF(options), "default", sizeof("default") - 1)) != NULL) {
     416           3 :                         ZVAL_COPY(value, tmp);
     417             :                 }
     418             :         }
     419        1105 : }
     420             : /* }}} */
     421             : 
     422      117770 : static unsigned int php_sapi_filter(int arg, char *var, char **val, size_t val_len, size_t *new_val_len) /* {{{ */
     423             : {
     424             :         zval  new_var, raw_var;
     425      117770 :         zval *array_ptr = NULL, *orig_array_ptr = NULL;
     426      117770 :         int retval = 0;
     427             : 
     428             :         assert(*val != NULL);
     429             : 
     430             : #define PARSE_CASE(s,a,t)                               \
     431             :                 case s:                                         \
     432             :                         if (Z_ISUNDEF(IF_G(a))) {                       \
     433             :                                 array_init(&IF_G(a));                               \
     434             :                         }                                                                               \
     435             :                         array_ptr = &IF_G(a);                               \
     436             :                         orig_array_ptr = &PG(http_globals)[t];      \
     437             :                         break;
     438             : 
     439      117770 :         switch (arg) {
     440         124 :                 PARSE_CASE(PARSE_POST,    post_array,    TRACK_VARS_POST)
     441         242 :                 PARSE_CASE(PARSE_GET,     get_array,     TRACK_VARS_GET)
     442          41 :                 PARSE_CASE(PARSE_COOKIE,  cookie_array,  TRACK_VARS_COOKIE)
     443      117241 :                 PARSE_CASE(PARSE_SERVER,  server_array,  TRACK_VARS_SERVER)
     444           0 :                 PARSE_CASE(PARSE_ENV,     env_array,     TRACK_VARS_ENV)
     445             : 
     446             :                 case PARSE_STRING: /* PARSE_STRING is used by parse_str() function */
     447         122 :                         retval = 1;
     448             :                         break;
     449             :         }
     450             : 
     451             :         /*
     452             :          * According to rfc2965, more specific paths are listed above the less specific ones.
     453             :          * If we encounter a duplicate cookie name, we should skip it, since it is not possible
     454             :          * to have the same (plain text) cookie name for the same path and we should not overwrite
     455             :          * more specific cookies with the less specific ones.
     456             :         */
     457      117811 :         if (arg == PARSE_COOKIE && orig_array_ptr &&
     458          41 :                         zend_symtable_str_exists(Z_ARRVAL_P(orig_array_ptr), var, strlen(var))) {
     459           3 :                 return 0;
     460             :         }
     461             : 
     462      117767 :         if (array_ptr) {
     463             :                 /* Store the RAW variable internally */
     464      235290 :                 ZVAL_STRINGL(&raw_var, *val, val_len);
     465      117645 :                 php_register_variable_ex(var, &raw_var, array_ptr);
     466             :         }
     467             : 
     468      117767 :         if (val_len) {
     469             :                 /* Register mangled variable */
     470       94023 :                 if (IF_G(default_filter) != FILTER_UNSAFE_RAW) {
     471          18 :                         ZVAL_STRINGL(&new_var, *val, val_len);
     472           9 :                         php_zval_filter(&new_var, IF_G(default_filter), IF_G(default_filter_flags), NULL, NULL, 0);
     473             :                 } else {
     474      188028 :                         ZVAL_STRINGL(&new_var, *val, val_len);
     475             :                 }
     476             :         } else { /* empty string */
     477       23744 :                 ZVAL_EMPTY_STRING(&new_var);
     478             :         }
     479             : 
     480      117767 :         if (orig_array_ptr) {
     481      117645 :                 php_register_variable_ex(var, &new_var, orig_array_ptr);
     482             :         }
     483             : 
     484      117767 :         if (retval) {
     485         122 :                 if (new_val_len) {
     486         114 :                         *new_val_len = Z_STRLEN(new_var);
     487             :                 }
     488         122 :                 efree(*val);
     489         122 :                 if (Z_STRLEN(new_var)) {
     490         115 :                         *val = estrndup(Z_STRVAL(new_var), Z_STRLEN(new_var));
     491             :                 } else {
     492           7 :                         *val = estrdup("");
     493             :                 }
     494         122 :                 zval_ptr_dtor(&new_var);
     495             :         }
     496             : 
     497      117767 :         return retval;
     498             : }
     499             : /* }}} */
     500             : 
     501          21 : static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long flags, zval *options, char *charset, zend_bool copy) /* {{{ */
     502             : {
     503          21 :         if (Z_TYPE_P(value) == IS_ARRAY) {
     504             :                 zval *element;
     505             : 
     506          21 :                 if (Z_ARRVAL_P(value)->u.v.nApplyCount > 1) {
     507           0 :                         return;
     508             :                 }
     509             : 
     510         111 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), element) {
     511          45 :                         ZVAL_DEREF(element);
     512         115 :                         SEPARATE_ZVAL_NOREF(element);
     513          45 :                         if (Z_TYPE_P(element) == IS_ARRAY) {
     514           6 :                                 Z_ARRVAL_P(element)->u.v.nApplyCount++;
     515           6 :                                 php_zval_filter_recursive(element, filter, flags, options, charset, copy);
     516           6 :                                 Z_ARRVAL_P(element)->u.v.nApplyCount--;
     517             :                         } else {
     518          39 :                                 php_zval_filter(element, filter, flags, options, charset, copy);
     519             :                         }
     520             :                 } ZEND_HASH_FOREACH_END();
     521             :         } else {
     522           0 :                 php_zval_filter(value, filter, flags, options, charset, copy);
     523             :         }
     524             : }
     525             : /* }}} */
     526             : 
     527          48 : static zval *php_filter_get_storage(zend_long arg)/* {{{ */
     528             : 
     529             : {
     530          48 :         zval *array_ptr = NULL;
     531             : 
     532          48 :         switch (arg) {
     533             :                 case PARSE_GET:
     534          23 :                         array_ptr = &IF_G(get_array);
     535          23 :                         break;
     536             :                 case PARSE_POST:
     537          14 :                         array_ptr = &IF_G(post_array);
     538          14 :                         break;
     539             :                 case PARSE_COOKIE:
     540           7 :                         array_ptr = &IF_G(cookie_array);
     541           7 :                         break;
     542             :                 case PARSE_SERVER:
     543           2 :                         if (PG(auto_globals_jit)) {
     544           2 :                                 zend_is_auto_global_str(ZEND_STRL("_SERVER"));
     545             :                         }
     546           2 :                         array_ptr = &IF_G(server_array);
     547           2 :                         break;
     548             :                 case PARSE_ENV:
     549           0 :                         if (PG(auto_globals_jit)) {
     550           0 :                                 zend_is_auto_global_str(ZEND_STRL("_ENV"));
     551             :                         }
     552           0 :                         array_ptr = !Z_ISUNDEF(IF_G(env_array)) ? &IF_G(env_array) : &PG(http_globals)[TRACK_VARS_ENV];
     553           0 :                         break;
     554             :                 case PARSE_SESSION:
     555             :                         /* FIXME: Implement session source */
     556           0 :                         php_error_docref(NULL, E_WARNING, "INPUT_SESSION is not yet implemented");
     557           0 :                         break;
     558             :                 case PARSE_REQUEST:
     559             :                         /* FIXME: Implement request source */
     560           0 :                         php_error_docref(NULL, E_WARNING, "INPUT_REQUEST is not yet implemented");
     561             :                         break;
     562             :         }
     563             : 
     564          48 :         return array_ptr;
     565             : }
     566             : /* }}} */
     567             : 
     568             : /* {{{ proto mixed filter_has_var(constant type, string variable_name)
     569             :  * Returns true if the variable with the name 'name' exists in source.
     570             :  */
     571          31 : PHP_FUNCTION(filter_has_var)
     572             : {
     573             :         zend_long         arg;
     574             :         zend_string *var;
     575          31 :         zval        *array_ptr = NULL;
     576             : 
     577          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lS", &arg, &var) == FAILURE) {
     578           9 :                 RETURN_FALSE;
     579             :         }
     580             : 
     581          22 :         array_ptr = php_filter_get_storage(arg);
     582             : 
     583          62 :         if (array_ptr && HASH_OF(array_ptr) && zend_hash_exists(HASH_OF(array_ptr), var)) {
     584           9 :                 RETURN_TRUE;
     585             :         }
     586             : 
     587          13 :         RETURN_FALSE;
     588             : }
     589             : /* }}} */
     590             : 
     591        1078 : static void php_filter_call(zval *filtered, zend_long filter, zval *filter_args, const int copy, zend_long filter_flags) /* {{{ */
     592             : {
     593        1078 :         zval *options = NULL;
     594             :         zval *option;
     595        1078 :         char *charset = NULL;
     596             : 
     597        2161 :         if (filter_args && Z_TYPE_P(filter_args) != IS_ARRAY) {
     598         252 :                 zend_long lval = zval_get_long(filter_args);
     599             : 
     600         252 :                 if (filter != -1) { /* handler for array apply */
     601             :                         /* filter_args is the filter_flags */
     602         241 :                         filter_flags = lval;
     603             : 
     604         241 :                         if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
     605         236 :                                 filter_flags |= FILTER_REQUIRE_SCALAR;
     606             :                         }
     607             :                 } else {
     608          11 :                         filter = lval;
     609             :                 }
     610         826 :         } else if (filter_args) {
     611         579 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "filter", sizeof("filter") - 1)) != NULL) {
     612           9 :                         filter = zval_get_long(option);
     613             :                 }
     614             : 
     615         579 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "flags", sizeof("flags") - 1)) != NULL) {
     616         312 :                         filter_flags = zval_get_long(option);
     617             : 
     618         312 :                         if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
     619         301 :                                 filter_flags |= FILTER_REQUIRE_SCALAR;
     620             :                         }
     621             :                 }
     622             : 
     623         579 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "options", sizeof("options") - 1)) != NULL) {
     624         266 :                         if (filter != FILTER_CALLBACK) {
     625         229 :                                 if (Z_TYPE_P(option) == IS_ARRAY) {
     626          39 :                                         options = option;
     627             :                                 }
     628             :                         } else {
     629          37 :                                 options = option;
     630          37 :                                 filter_flags = 0;
     631             :                         }
     632             :                 }
     633             :         }
     634             : 
     635        1078 :         if (Z_TYPE_P(filtered) == IS_ARRAY) {
     636          20 :                 if (filter_flags & FILTER_REQUIRE_SCALAR) {
     637           5 :                         if (copy) {
     638           6 :                                 SEPARATE_ZVAL(filtered);
     639             :                         }
     640           5 :                         zval_ptr_dtor(filtered);
     641           5 :                         if (filter_flags & FILTER_NULL_ON_FAILURE) {
     642           0 :                                 ZVAL_NULL(filtered);
     643             :                         } else {
     644           5 :                                 ZVAL_FALSE(filtered);
     645             :                         }
     646           5 :                         return;
     647             :                 }
     648          15 :                 php_zval_filter_recursive(filtered, filter, filter_flags, options, charset, copy);
     649          15 :                 return;
     650             :         }
     651        1058 :         if (filter_flags & FILTER_REQUIRE_ARRAY) {
     652           1 :                 if (copy) {
     653           1 :                         SEPARATE_ZVAL(filtered);
     654             :                 }
     655           1 :                 zval_ptr_dtor(filtered);
     656           1 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     657           0 :                         ZVAL_NULL(filtered);
     658             :                 } else {
     659           1 :                         ZVAL_FALSE(filtered);
     660             :                 }
     661           1 :                 return;
     662             :         }
     663             : 
     664        1057 :         php_zval_filter(filtered, filter, filter_flags, options, charset, copy);
     665        1057 :         if (filter_flags & FILTER_FORCE_ARRAY) {
     666             :                 zval tmp;
     667           5 :                 ZVAL_COPY_VALUE(&tmp, filtered);
     668           5 :                 array_init(filtered);
     669           5 :                 add_next_index_zval(filtered, &tmp);
     670             :         }
     671             : }
     672             : /* }}} */
     673             : 
     674          18 : static void php_filter_array_handler(zval *input, zval *op, zval *return_value, zend_bool add_empty) /* {{{ */
     675             : {
     676             :         zend_string *arg_key;
     677             :         zval *tmp, *arg_elm;
     678             : 
     679          18 :         if (!op) {
     680           2 :                 zval_ptr_dtor(return_value);
     681           2 :                 ZVAL_DUP(return_value, input);
     682           2 :                 php_filter_call(return_value, FILTER_DEFAULT, NULL, 0, FILTER_REQUIRE_ARRAY);
     683          16 :         } else if (Z_TYPE_P(op) == IS_LONG) {
     684           1 :                 zval_ptr_dtor(return_value);
     685           1 :                 ZVAL_DUP(return_value, input);
     686           1 :                 php_filter_call(return_value, Z_LVAL_P(op), NULL, 0, FILTER_REQUIRE_ARRAY);
     687          15 :         } else if (Z_TYPE_P(op) == IS_ARRAY) {
     688          15 :                 array_init(return_value);
     689             : 
     690          81 :                 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(op), arg_key, arg_elm) {
     691          34 :                         if (arg_key == NULL) {
     692           0 :                                 php_error_docref(NULL, E_WARNING, "Numeric keys are not allowed in the definition array");
     693           0 :                                 zval_ptr_dtor(return_value);
     694           0 :                                 RETURN_FALSE;
     695             :                         }
     696          34 :                         if (ZSTR_LEN(arg_key) == 0) {
     697           2 :                                 php_error_docref(NULL, E_WARNING, "Empty keys are not allowed in the definition array");
     698           2 :                                 zval_ptr_dtor(return_value);
     699           2 :                                 RETURN_FALSE;
     700             :                         }
     701          32 :                         if ((tmp = zend_hash_find(Z_ARRVAL_P(input), arg_key)) == NULL) {
     702          10 :                                 if (add_empty) {
     703           9 :                                         add_assoc_null_ex(return_value, ZSTR_VAL(arg_key), ZSTR_LEN(arg_key));
     704             :                                 }
     705             :                         } else {
     706             :                                 zval nval;
     707          22 :                                 ZVAL_DEREF(tmp);
     708          22 :                                 ZVAL_DUP(&nval, tmp);
     709          22 :                                 php_filter_call(&nval, -1, arg_elm, 0, FILTER_REQUIRE_SCALAR);
     710          22 :                                 zend_hash_update(Z_ARRVAL_P(return_value), arg_key, &nval);
     711             :                         }
     712             :                 } ZEND_HASH_FOREACH_END();
     713             :         } else {
     714           0 :                 RETURN_FALSE;
     715             :         }
     716             : }
     717             : /* }}} */
     718             : 
     719             : /* {{{ proto mixed filter_input(constant type, string variable_name [, long filter [, mixed options]])
     720             :  * Returns the filtered variable 'name'* from source `type`.
     721             :  */
     722          26 : PHP_FUNCTION(filter_input)
     723             : {
     724          26 :         zend_long fetch_from, filter = FILTER_DEFAULT;
     725          26 :         zval *filter_args = NULL, *tmp;
     726          26 :         zval *input = NULL;
     727             :         zend_string *var;
     728             : 
     729          26 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lS|lz", &fetch_from, &var, &filter, &filter_args) == FAILURE) {
     730           1 :                 return;
     731             :         }
     732             : 
     733          25 :         if (!PHP_FILTER_ID_EXISTS(filter)) {
     734           0 :                 RETURN_FALSE;
     735             :         }
     736             : 
     737          25 :         input = php_filter_get_storage(fetch_from);
     738             : 
     739          75 :         if (!input || !HASH_OF(input) || (tmp = zend_hash_find(HASH_OF(input), var)) == NULL) {
     740           5 :                 zend_long filter_flags = 0;
     741             :                 zval *option, *opt, *def;
     742           5 :                 if (filter_args) {
     743           2 :                         if (Z_TYPE_P(filter_args) == IS_LONG) {
     744           0 :                                 filter_flags = Z_LVAL_P(filter_args);
     745           3 :                         } else if (Z_TYPE_P(filter_args) == IS_ARRAY && (option = zend_hash_str_find(HASH_OF(filter_args), "flags", sizeof("flags") - 1)) != NULL) {
     746           1 :                                 filter_flags = zval_get_long(option);
     747             :                         }
     748           5 :                         if (Z_TYPE_P(filter_args) == IS_ARRAY &&
     749           2 :                                 (opt = zend_hash_str_find(HASH_OF(filter_args), "options", sizeof("options") - 1)) != NULL &&
     750             :                                 Z_TYPE_P(opt) == IS_ARRAY &&
     751           1 :                                 (def = zend_hash_str_find(HASH_OF(opt), "default", sizeof("default") - 1)) != NULL) {
     752           1 :                                 ZVAL_COPY(return_value, def);
     753           1 :                                 return;
     754             :                         }
     755             :                 }
     756             : 
     757             :                 /* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
     758             :                  * the function: normally when validation fails false is returned, and
     759             :                  * when the input value doesn't exist NULL is returned. With the flag
     760             :                  * set, NULL and false should be returned, respectively. Ergo, although
     761             :                  * the code below looks incorrect, it's actually right. */
     762           4 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     763           0 :                         RETURN_FALSE;
     764             :                 } else {
     765           4 :                         RETURN_NULL();
     766             :                 }
     767             :         }
     768             : 
     769          20 :         ZVAL_DUP(return_value, tmp);
     770             : 
     771          20 :         php_filter_call(return_value, filter, filter_args, 1, FILTER_REQUIRE_SCALAR);
     772             : }
     773             : /* }}} */
     774             : 
     775             : /* {{{ proto mixed filter_var(mixed variable [, long filter [, mixed options]])
     776             :  * Returns the filtered version of the variable.
     777             :  */
     778        1039 : PHP_FUNCTION(filter_var)
     779             : {
     780        1039 :         zend_long filter = FILTER_DEFAULT;
     781        1039 :         zval *filter_args = NULL, *data;
     782             : 
     783        1039 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z/|lz", &data, &filter, &filter_args) == FAILURE) {
     784           4 :                 return;
     785             :         }
     786             : 
     787        1035 :         if (!PHP_FILTER_ID_EXISTS(filter)) {
     788           2 :                 RETURN_FALSE;
     789             :         }
     790             : 
     791        1033 :         ZVAL_DUP(return_value, data);
     792             : 
     793        1033 :         php_filter_call(return_value, filter, filter_args, 1, FILTER_REQUIRE_SCALAR);
     794             : }
     795             : /* }}} */
     796             : 
     797             : /* {{{ proto mixed filter_input_array(constant type, [, mixed options [, bool add_empty]]])
     798             :  * Returns an array with all arguments defined in 'definition'.
     799             :  */
     800           7 : PHP_FUNCTION(filter_input_array)
     801             : {
     802             :         zend_long    fetch_from;
     803           7 :         zval   *array_input = NULL, *op = NULL;
     804           7 :         zend_bool add_empty = 1;
     805             : 
     806           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|zb",  &fetch_from, &op, &add_empty) == FAILURE) {
     807           0 :                 return;
     808             :         }
     809             : 
     810          20 :         if (op && (Z_TYPE_P(op) != IS_ARRAY) && !(Z_TYPE_P(op) == IS_LONG && PHP_FILTER_ID_EXISTS(Z_LVAL_P(op)))) {
     811           6 :                 RETURN_FALSE;
     812             :         }
     813             : 
     814           1 :         array_input = php_filter_get_storage(fetch_from);
     815             : 
     816           2 :         if (!array_input || !HASH_OF(array_input)) {
     817           0 :                 zend_long filter_flags = 0;
     818             :                 zval *option;
     819           0 :                 if (op) {
     820           0 :                         if (Z_TYPE_P(op) == IS_LONG) {
     821           0 :                                 filter_flags = Z_LVAL_P(op);
     822           0 :                         } else if (Z_TYPE_P(op) == IS_ARRAY && (option = zend_hash_str_find(HASH_OF(op), "flags", sizeof("flags") - 1)) != NULL) {
     823           0 :                                 filter_flags = zval_get_long(option);
     824             :                         }
     825             :                 }
     826             : 
     827             :                 /* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
     828             :                  * the function: normally when validation fails false is returned, and
     829             :                  * when the input value doesn't exist NULL is returned. With the flag
     830             :                  * set, NULL and false should be returned, respectively. Ergo, although
     831             :                  * the code below looks incorrect, it's actually right. */
     832           0 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     833           0 :                         RETURN_FALSE;
     834             :                 } else {
     835           0 :                         RETURN_NULL();
     836             :                 }
     837             :         }
     838             : 
     839           1 :         php_filter_array_handler(array_input, op, return_value, add_empty);
     840             : }
     841             : /* }}} */
     842             : 
     843             : /* {{{ proto mixed filter_var_array(array data, [, mixed options [, bool add_empty]]])
     844             :  * Returns an array with all arguments defined in 'definition'.
     845             :  */
     846          37 : PHP_FUNCTION(filter_var_array)
     847             : {
     848          37 :         zval *array_input = NULL, *op = NULL;
     849          37 :         zend_bool add_empty = 1;
     850             : 
     851          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|zb",  &array_input, &op, &add_empty) == FAILURE) {
     852           1 :                 return;
     853             :         }
     854             : 
     855          90 :         if (op && (Z_TYPE_P(op) != IS_ARRAY) && !(Z_TYPE_P(op) == IS_LONG && PHP_FILTER_ID_EXISTS(Z_LVAL_P(op)))) {
     856          19 :                 RETURN_FALSE;
     857             :         }
     858             : 
     859          17 :         php_filter_array_handler(array_input, op, return_value, add_empty);
     860             : }
     861             : /* }}} */
     862             : 
     863             : /* {{{ proto filter_list()
     864             :  * Returns a list of all supported filters */
     865           3 : PHP_FUNCTION(filter_list)
     866             : {
     867           3 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     868             : 
     869           3 :         if (zend_parse_parameters_none() == FAILURE) {
     870           1 :                 return;
     871             :         }
     872             : 
     873           2 :         array_init(return_value);
     874          44 :         for (i = 0; i < size; ++i) {
     875          42 :                 add_next_index_string(return_value, (char *)filter_list[i].name);
     876             :         }
     877             : }
     878             : /* }}} */
     879             : 
     880             : /* {{{ proto filter_id(string filtername)
     881             :  * Returns the filter ID belonging to a named filter */
     882         218 : PHP_FUNCTION(filter_id)
     883             : {
     884             :         int i;
     885             :         size_t filter_len;
     886         218 :         int size = sizeof(filter_list) / sizeof(filter_list_entry);
     887             :         char *filter;
     888             : 
     889         218 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &filter, &filter_len) == FAILURE) {
     890           2 :                 return;
     891             :         }
     892             : 
     893        2393 :         for (i = 0; i < size; ++i) {
     894        2391 :                 if (strcmp(filter_list[i].name, filter) == 0) {
     895         214 :                         RETURN_LONG(filter_list[i].id);
     896             :                 }
     897             :         }
     898             : 
     899           2 :         RETURN_FALSE;
     900             : }
     901             : /* }}} */
     902             : 
     903             : /*
     904             :  * Local variables:
     905             :  * tab-width: 4
     906             :  * c-basic-offset: 4
     907             :  * End:
     908             :  * vim600: noet sw=4 ts=4 fdm=marker
     909             :  * vim<600: noet sw=4 ts=4
     910             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:25:56 +0000 (1 hour ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.