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: 353 393 89.8 %
Date: 2018-09-24 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 24 Sep 2018 07:30:09 +0000 (77 days ago)

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