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: 332 366 90.7 %
Date: 2014-12-13 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-2014 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: dec6bfb4ec675d1090b133b69f614650a03fe730 $ */
      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 TSRMLS_DC);
      82             : static unsigned int php_sapi_filter_init(TSRMLS_D);
      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             : #if ZEND_MODULE_API_NO >= 20010901
     141             :         STANDARD_MODULE_HEADER,
     142             : #endif
     143             :         "filter",
     144             :         filter_functions,
     145             :         PHP_MINIT(filter),
     146             :         PHP_MSHUTDOWN(filter),
     147             :         NULL,
     148             :         PHP_RSHUTDOWN(filter),
     149             :         PHP_MINFO(filter),
     150             :         "0.11.0",
     151             :         STANDARD_MODULE_PROPERTIES
     152             : };
     153             : /* }}} */
     154             : 
     155             : #ifdef COMPILE_DL_FILTER
     156             : ZEND_GET_MODULE(filter)
     157             : #endif
     158             : 
     159       20622 : static PHP_INI_MH(UpdateDefaultFilter) /* {{{ */
     160             : {
     161       20622 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     162             : 
     163      309310 :         for (i = 0; i < size; ++i) {
     164      309310 :                 if ((strcasecmp(new_value->val, filter_list[i].name) == 0)) {
     165       20622 :                         IF_G(default_filter) = filter_list[i].id;
     166       20622 :                         return SUCCESS;
     167             :                 }
     168             :         }
     169             :         /* Fallback to the default filter */
     170           0 :         IF_G(default_filter) = FILTER_DEFAULT;
     171           0 :         return SUCCESS;
     172             : }
     173             : /* }}} */
     174             : 
     175             : /* {{{ PHP_INI
     176             :  */
     177       20622 : static PHP_INI_MH(OnUpdateFlags)
     178             : {
     179       20622 :         if (!new_value) {
     180       20616 :                 IF_G(default_filter_flags) = FILTER_FLAG_NO_ENCODE_QUOTES;
     181             :         } else {
     182           6 :                 IF_G(default_filter_flags) = atoi(new_value->val);
     183             :         }
     184       20622 :         return SUCCESS;
     185             : }
     186             : 
     187             : PHP_INI_BEGIN()
     188             :         STD_PHP_INI_ENTRY("filter.default",   "unsafe_raw", PHP_INI_SYSTEM|PHP_INI_PERDIR, UpdateDefaultFilter, default_filter, zend_filter_globals, filter_globals)
     189             :         PHP_INI_ENTRY("filter.default_flags", NULL,     PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateFlags)
     190             : PHP_INI_END()
     191             : /* }}} */
     192             : 
     193       20622 : static void php_filter_init_globals(zend_filter_globals *filter_globals) /* {{{ */
     194             : {
     195       20622 :         ZVAL_UNDEF(&filter_globals->post_array);
     196       20622 :         ZVAL_UNDEF(&filter_globals->get_array);
     197       20622 :         ZVAL_UNDEF(&filter_globals->cookie_array);
     198       20622 :         ZVAL_UNDEF(&filter_globals->env_array);
     199       20622 :         ZVAL_UNDEF(&filter_globals->server_array);
     200       20622 :         ZVAL_UNDEF(&filter_globals->session_array);
     201       20622 :         filter_globals->default_filter = FILTER_DEFAULT;
     202       20622 : }
     203             : /* }}} */
     204             : 
     205             : #define PARSE_REQUEST 99
     206             : 
     207             : /* {{{ PHP_MINIT_FUNCTION
     208             :  */
     209       20622 : PHP_MINIT_FUNCTION(filter)
     210             : {
     211       20622 :         ZEND_INIT_MODULE_GLOBALS(filter, php_filter_init_globals, NULL);
     212             : 
     213       20622 :         REGISTER_INI_ENTRIES();
     214             : 
     215       20622 :         REGISTER_LONG_CONSTANT("INPUT_POST",  PARSE_POST,     CONST_CS | CONST_PERSISTENT);
     216       20622 :         REGISTER_LONG_CONSTANT("INPUT_GET",           PARSE_GET,              CONST_CS | CONST_PERSISTENT);
     217       20622 :         REGISTER_LONG_CONSTANT("INPUT_COOKIE",        PARSE_COOKIE,   CONST_CS | CONST_PERSISTENT);
     218       20622 :         REGISTER_LONG_CONSTANT("INPUT_ENV",           PARSE_ENV,              CONST_CS | CONST_PERSISTENT);
     219       20622 :         REGISTER_LONG_CONSTANT("INPUT_SERVER",        PARSE_SERVER,   CONST_CS | CONST_PERSISTENT);
     220       20622 :         REGISTER_LONG_CONSTANT("INPUT_SESSION", PARSE_SESSION,        CONST_CS | CONST_PERSISTENT);
     221       20622 :         REGISTER_LONG_CONSTANT("INPUT_REQUEST", PARSE_REQUEST,        CONST_CS | CONST_PERSISTENT);
     222             : 
     223       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NONE", FILTER_FLAG_NONE, CONST_CS | CONST_PERSISTENT);
     224             : 
     225       20622 :         REGISTER_LONG_CONSTANT("FILTER_REQUIRE_SCALAR", FILTER_REQUIRE_SCALAR, CONST_CS | CONST_PERSISTENT);
     226       20622 :         REGISTER_LONG_CONSTANT("FILTER_REQUIRE_ARRAY", FILTER_REQUIRE_ARRAY, CONST_CS | CONST_PERSISTENT);
     227       20622 :         REGISTER_LONG_CONSTANT("FILTER_FORCE_ARRAY", FILTER_FORCE_ARRAY, CONST_CS | CONST_PERSISTENT);
     228       20622 :         REGISTER_LONG_CONSTANT("FILTER_NULL_ON_FAILURE", FILTER_NULL_ON_FAILURE, CONST_CS | CONST_PERSISTENT);
     229             : 
     230       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_INT", FILTER_VALIDATE_INT, CONST_CS | CONST_PERSISTENT);
     231       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_BOOLEAN", FILTER_VALIDATE_BOOLEAN, CONST_CS | CONST_PERSISTENT);
     232       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_FLOAT", FILTER_VALIDATE_FLOAT, CONST_CS | CONST_PERSISTENT);
     233             : 
     234       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_REGEXP", FILTER_VALIDATE_REGEXP, CONST_CS | CONST_PERSISTENT);
     235       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_DOMAIN", FILTER_VALIDATE_DOMAIN, CONST_CS | CONST_PERSISTENT);
     236       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_URL", FILTER_VALIDATE_URL, CONST_CS | CONST_PERSISTENT);
     237       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_EMAIL", FILTER_VALIDATE_EMAIL, CONST_CS | CONST_PERSISTENT);
     238       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_IP", FILTER_VALIDATE_IP, CONST_CS | CONST_PERSISTENT);
     239       20622 :         REGISTER_LONG_CONSTANT("FILTER_VALIDATE_MAC", FILTER_VALIDATE_MAC, CONST_CS | CONST_PERSISTENT);
     240             : 
     241       20622 :         REGISTER_LONG_CONSTANT("FILTER_DEFAULT", FILTER_DEFAULT, CONST_CS | CONST_PERSISTENT);
     242       20622 :         REGISTER_LONG_CONSTANT("FILTER_UNSAFE_RAW", FILTER_UNSAFE_RAW, CONST_CS | CONST_PERSISTENT);
     243             : 
     244       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRING", FILTER_SANITIZE_STRING, CONST_CS | CONST_PERSISTENT);
     245       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRIPPED", FILTER_SANITIZE_STRING, CONST_CS | CONST_PERSISTENT);
     246       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_ENCODED", FILTER_SANITIZE_ENCODED, CONST_CS | CONST_PERSISTENT);
     247       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_SPECIAL_CHARS", FILTER_SANITIZE_SPECIAL_CHARS, CONST_CS | CONST_PERSISTENT);
     248       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_FULL_SPECIAL_CHARS", FILTER_SANITIZE_FULL_SPECIAL_CHARS, CONST_CS | CONST_PERSISTENT);
     249       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_EMAIL", FILTER_SANITIZE_EMAIL, CONST_CS | CONST_PERSISTENT);
     250       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_URL", FILTER_SANITIZE_URL, CONST_CS | CONST_PERSISTENT);
     251       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_NUMBER_INT", FILTER_SANITIZE_NUMBER_INT, CONST_CS | CONST_PERSISTENT);
     252       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_NUMBER_FLOAT", FILTER_SANITIZE_NUMBER_FLOAT, CONST_CS | CONST_PERSISTENT);
     253       20622 :         REGISTER_LONG_CONSTANT("FILTER_SANITIZE_MAGIC_QUOTES", FILTER_SANITIZE_MAGIC_QUOTES, CONST_CS | CONST_PERSISTENT);
     254             : 
     255       20622 :         REGISTER_LONG_CONSTANT("FILTER_CALLBACK", FILTER_CALLBACK, CONST_CS | CONST_PERSISTENT);
     256             : 
     257       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_OCTAL", FILTER_FLAG_ALLOW_OCTAL, CONST_CS | CONST_PERSISTENT);
     258       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_HEX", FILTER_FLAG_ALLOW_HEX, CONST_CS | CONST_PERSISTENT);
     259             : 
     260       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_LOW", FILTER_FLAG_STRIP_LOW, CONST_CS | CONST_PERSISTENT);
     261       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_HIGH", FILTER_FLAG_STRIP_HIGH, CONST_CS | CONST_PERSISTENT);
     262       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_STRIP_BACKTICK", FILTER_FLAG_STRIP_BACKTICK, CONST_CS | CONST_PERSISTENT);
     263       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_LOW", FILTER_FLAG_ENCODE_LOW, CONST_CS | CONST_PERSISTENT);
     264       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_HIGH", FILTER_FLAG_ENCODE_HIGH, CONST_CS | CONST_PERSISTENT);
     265       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ENCODE_AMP", FILTER_FLAG_ENCODE_AMP, CONST_CS | CONST_PERSISTENT);
     266       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_ENCODE_QUOTES", FILTER_FLAG_NO_ENCODE_QUOTES, CONST_CS | CONST_PERSISTENT);
     267       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_EMPTY_STRING_NULL", FILTER_FLAG_EMPTY_STRING_NULL, CONST_CS | CONST_PERSISTENT);
     268             : 
     269       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_FRACTION", FILTER_FLAG_ALLOW_FRACTION, CONST_CS | CONST_PERSISTENT);
     270       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_THOUSAND", FILTER_FLAG_ALLOW_THOUSAND, CONST_CS | CONST_PERSISTENT);
     271       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_ALLOW_SCIENTIFIC", FILTER_FLAG_ALLOW_SCIENTIFIC, CONST_CS | CONST_PERSISTENT);
     272             : 
     273       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_SCHEME_REQUIRED", FILTER_FLAG_SCHEME_REQUIRED, CONST_CS | CONST_PERSISTENT);
     274       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_HOST_REQUIRED", FILTER_FLAG_HOST_REQUIRED, CONST_CS | CONST_PERSISTENT);
     275       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_PATH_REQUIRED", FILTER_FLAG_PATH_REQUIRED, CONST_CS | CONST_PERSISTENT);
     276       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_QUERY_REQUIRED", FILTER_FLAG_QUERY_REQUIRED, CONST_CS | CONST_PERSISTENT);
     277             : 
     278       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_IPV4", FILTER_FLAG_IPV4, CONST_CS | CONST_PERSISTENT);
     279       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_IPV6", FILTER_FLAG_IPV6, CONST_CS | CONST_PERSISTENT);
     280       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_RES_RANGE", FILTER_FLAG_NO_RES_RANGE, CONST_CS | CONST_PERSISTENT);
     281       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_NO_PRIV_RANGE", FILTER_FLAG_NO_PRIV_RANGE, CONST_CS | CONST_PERSISTENT);
     282             : 
     283       20622 :         REGISTER_LONG_CONSTANT("FILTER_FLAG_HOSTNAME", FILTER_FLAG_HOSTNAME, CONST_CS | CONST_PERSISTENT);
     284             : 
     285       20622 :         sapi_register_input_filter(php_sapi_filter, php_sapi_filter_init TSRMLS_CC);
     286             : 
     287       20622 :         return SUCCESS;
     288             : }
     289             : /* }}} */
     290             : 
     291             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     292             :  */
     293       20656 : PHP_MSHUTDOWN_FUNCTION(filter)
     294             : {
     295       20656 :         UNREGISTER_INI_ENTRIES();
     296             : 
     297       20656 :         return SUCCESS;
     298             : }
     299             : /* }}} */
     300             : 
     301             : /* {{{ PHP_RSHUTDOWN_FUNCTION
     302             :  */
     303             : #define VAR_ARRAY_COPY_DTOR(a)   \
     304             :         if (!Z_ISUNDEF(IF_G(a))) {   \
     305             :                 zval_ptr_dtor(&IF_G(a)); \
     306             :                 ZVAL_UNDEF(&IF_G(a));    \
     307             :         }
     308             : 
     309       20615 : PHP_RSHUTDOWN_FUNCTION(filter)
     310             : {
     311       20615 :         VAR_ARRAY_COPY_DTOR(get_array)
     312       20615 :         VAR_ARRAY_COPY_DTOR(post_array)
     313       20615 :         VAR_ARRAY_COPY_DTOR(cookie_array)
     314       20615 :         VAR_ARRAY_COPY_DTOR(server_array)
     315       20615 :         VAR_ARRAY_COPY_DTOR(env_array)
     316       20615 :         VAR_ARRAY_COPY_DTOR(session_array)
     317       20615 :         return SUCCESS;
     318             : }
     319             : /* }}} */
     320             : 
     321             : /* {{{ PHP_MINFO_FUNCTION
     322             :  */
     323         144 : PHP_MINFO_FUNCTION(filter)
     324             : {
     325         144 :         php_info_print_table_start();
     326         144 :         php_info_print_table_row( 2, "Input Validation and Filtering", "enabled" );
     327         144 :         php_info_print_table_row( 2, "Revision", "$Id: dec6bfb4ec675d1090b133b69f614650a03fe730 $");
     328         144 :         php_info_print_table_end();
     329             : 
     330         144 :         DISPLAY_INI_ENTRIES();
     331         144 : }
     332             : /* }}} */
     333             : 
     334        1034 : static filter_list_entry php_find_filter(zend_long id) /* {{{ */
     335             : {
     336        1034 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     337             : 
     338        9009 :         for (i = 0; i < size; ++i) {
     339        9004 :                 if (filter_list[i].id == id) {
     340        1029 :                         return filter_list[i];
     341             :                 }
     342             :         }
     343             :         /* Fallback to "string" filter */
     344          75 :         for (i = 0; i < size; ++i) {
     345          75 :                 if (filter_list[i].id == FILTER_DEFAULT) {
     346           5 :                         return filter_list[i];
     347             :                 }
     348             :         }
     349             :         /* To shut up GCC */
     350           0 :         return filter_list[0];
     351             : }
     352             : /* }}} */
     353             : 
     354       20579 : static unsigned int php_sapi_filter_init(TSRMLS_D)
     355             : {
     356       20579 :         ZVAL_UNDEF(&IF_G(get_array));
     357       20579 :         ZVAL_UNDEF(&IF_G(post_array));
     358       20579 :         ZVAL_UNDEF(&IF_G(cookie_array));
     359       20579 :         ZVAL_UNDEF(&IF_G(server_array));
     360       20579 :         ZVAL_UNDEF(&IF_G(env_array));
     361       20579 :         ZVAL_UNDEF(&IF_G(session_array));
     362       20579 :         return SUCCESS;
     363             : }
     364             : 
     365        1034 : static void php_zval_filter(zval *value, zend_long filter, zend_long flags, zval *options, char* charset, zend_bool copy TSRMLS_DC) /* {{{ */
     366             : {
     367             :         filter_list_entry  filter_func;
     368             : 
     369        1034 :         filter_func = php_find_filter(filter);
     370             : 
     371        1034 :         if (!filter_func.id) {
     372             :                 /* Find default filter */
     373           0 :                 filter_func = php_find_filter(FILTER_DEFAULT);
     374             :         }
     375             : 
     376        1034 :         if (copy) {
     377        1696 :                 SEPARATE_ZVAL(value);
     378             :         }
     379             : 
     380             :         /* #49274, fatal error with object without a toString method
     381             :           Fails nicely instead of getting a recovarable fatal error. */
     382        1034 :         if (Z_TYPE_P(value) == IS_OBJECT) {
     383             :                 zend_class_entry *ce;
     384             : 
     385           2 :                 ce = Z_OBJCE_P(value);
     386           2 :                 if (!ce->__tostring) {
     387           1 :                         ZVAL_FALSE(value);
     388           1 :                         return;
     389             :                 }
     390             :         }
     391             : 
     392             :         /* Here be strings */
     393        1033 :         convert_to_string(value);
     394             : 
     395        1033 :         filter_func.function(value, flags, options, charset TSRMLS_CC);
     396             : 
     397        1294 :         if (options && (Z_TYPE_P(options) == IS_ARRAY || Z_TYPE_P(options) == IS_OBJECT) &&
     398          41 :                 ((flags & FILTER_NULL_ON_FAILURE && Z_TYPE_P(value) == IS_NULL) || 
     399          41 :                 (!(flags & FILTER_NULL_ON_FAILURE) && Z_TYPE_P(value) == IS_FALSE)) &&
     400          26 :                 zend_hash_str_exists(HASH_OF(options), "default", sizeof("default") - 1)) {
     401             :                 zval *tmp; 
     402           1 :                 if ((tmp = zend_hash_str_find(HASH_OF(options), "default", sizeof("default") - 1)) != NULL) {
     403           1 :                         ZVAL_COPY(value, tmp);
     404             :                 }
     405             :         }
     406             : }
     407             : /* }}} */
     408             : 
     409      101481 : static unsigned int php_sapi_filter(int arg, char *var, char **val, size_t val_len, size_t *new_val_len TSRMLS_DC) /* {{{ */
     410             : {
     411             :         zval  new_var, raw_var;
     412      101481 :         zval *array_ptr = NULL, *orig_array_ptr = NULL;
     413      101481 :         int retval = 0;
     414             : 
     415             :         assert(*val != NULL);
     416             : 
     417             : #define PARSE_CASE(s,a,t)                               \
     418             :                 case s:                                         \
     419             :                         if (Z_ISUNDEF(IF_G(a))) {                       \
     420             :                                 array_init(&IF_G(a));                               \
     421             :                         }                                                                               \
     422             :                         array_ptr = &IF_G(a);                               \
     423             :                         orig_array_ptr = &PG(http_globals)[t];      \
     424             :                         break;
     425             : 
     426      101481 :         switch (arg) {
     427         117 :                 PARSE_CASE(PARSE_POST,    post_array,    TRACK_VARS_POST)
     428         235 :                 PARSE_CASE(PARSE_GET,     get_array,     TRACK_VARS_GET)
     429          39 :                 PARSE_CASE(PARSE_COOKIE,  cookie_array,  TRACK_VARS_COOKIE)
     430      100971 :                 PARSE_CASE(PARSE_SERVER,  server_array,  TRACK_VARS_SERVER)
     431           0 :                 PARSE_CASE(PARSE_ENV,     env_array,     TRACK_VARS_ENV)
     432             : 
     433             :                 case PARSE_STRING: /* PARSE_STRING is used by parse_str() function */
     434         119 :                         retval = 1;
     435             :                         break;
     436             :         }
     437             : 
     438             :         /* 
     439             :          * According to rfc2965, more specific paths are listed above the less specific ones.
     440             :          * If we encounter a duplicate cookie name, we should skip it, since it is not possible
     441             :          * to have the same (plain text) cookie name for the same path and we should not overwrite
     442             :          * more specific cookies with the less specific ones.
     443             :         */
     444      101520 :         if (arg == PARSE_COOKIE && orig_array_ptr &&
     445          39 :                         zend_symtable_str_exists(Z_ARRVAL_P(orig_array_ptr), var, strlen(var))) {
     446           3 :                 return 0;
     447             :         }
     448             : 
     449      101478 :         if (array_ptr) {
     450             :                 /* Store the RAW variable internally */
     451      202718 :                 ZVAL_STRINGL(&raw_var, *val, val_len);
     452      101359 :                 php_register_variable_ex(var, &raw_var, array_ptr TSRMLS_CC);
     453             :         }
     454             : 
     455      101478 :         if (val_len) {
     456             :                 /* Register mangled variable */
     457       80994 :                 if (IF_G(default_filter) != FILTER_UNSAFE_RAW) {
     458          18 :                         ZVAL_STRINGL(&new_var, *val, val_len);
     459           9 :                         php_zval_filter(&new_var, IF_G(default_filter), IF_G(default_filter_flags), NULL, NULL, 0 TSRMLS_CC);
     460             :                 } else {
     461      161970 :                         ZVAL_STRINGL(&new_var, *val, val_len);
     462             :                 }
     463             :         } else { /* empty string */
     464       20484 :                 ZVAL_EMPTY_STRING(&new_var);
     465             :         }
     466             : 
     467      101478 :         if (orig_array_ptr) {
     468      101359 :                 php_register_variable_ex(var, &new_var, orig_array_ptr TSRMLS_CC);
     469             :         }
     470             : 
     471      101478 :         if (retval) {
     472         119 :                 if (new_val_len) {
     473         111 :                         *new_val_len = Z_STRLEN(new_var);
     474             :                 }
     475         119 :                 efree(*val);
     476         119 :                 if (Z_STRLEN(new_var)) {
     477         112 :                         *val = estrndup(Z_STRVAL(new_var), Z_STRLEN(new_var));
     478             :                 } else {
     479           7 :                         *val = estrdup("");
     480             :                 }
     481         119 :                 zval_ptr_dtor(&new_var);
     482             :         }
     483             : 
     484      101478 :         return retval;
     485             : }
     486             : /* }}} */
     487             : 
     488          21 : static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long flags, zval *options, char *charset, zend_bool copy TSRMLS_DC) /* {{{ */
     489             : {
     490          21 :         if (Z_TYPE_P(value) == IS_ARRAY) {
     491             :                 zval *element;
     492             : 
     493          21 :                 if (Z_ARRVAL_P(value)->u.v.nApplyCount > 1) {
     494           0 :                         return;
     495             :                 }
     496             : 
     497         111 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), element) {
     498          45 :                         ZVAL_DEREF(element);
     499         114 :                         SEPARATE_ZVAL_NOREF(element);
     500          45 :                         if (Z_TYPE_P(element) == IS_ARRAY) {
     501           6 :                                 Z_ARRVAL_P(element)->u.v.nApplyCount++;
     502           6 :                                 php_zval_filter_recursive(element, filter, flags, options, charset, copy TSRMLS_CC);
     503           6 :                                 Z_ARRVAL_P(element)->u.v.nApplyCount--;
     504             :                         } else {
     505          39 :                                 php_zval_filter(element, filter, flags, options, charset, copy TSRMLS_CC);
     506             :                         }
     507             :                 } ZEND_HASH_FOREACH_END();
     508             :         } else {
     509           0 :                 php_zval_filter(value, filter, flags, options, charset, copy TSRMLS_CC);
     510             :         }
     511             : }
     512             : /* }}} */
     513             : 
     514          48 : static zval *php_filter_get_storage(zend_long arg TSRMLS_DC)/* {{{ */
     515             : 
     516             : {
     517          48 :         zval *array_ptr = NULL;
     518             : 
     519          48 :         switch (arg) {
     520             :                 case PARSE_GET:
     521          23 :                         array_ptr = &IF_G(get_array);
     522          23 :                         break;
     523             :                 case PARSE_POST:
     524          14 :                         array_ptr = &IF_G(post_array);
     525          14 :                         break;
     526             :                 case PARSE_COOKIE:
     527           7 :                         array_ptr = &IF_G(cookie_array);
     528           7 :                         break;
     529             :                 case PARSE_SERVER:
     530           2 :                         if (PG(auto_globals_jit)) {
     531           2 :                                 zend_string *name = zend_string_init("_SERVER", sizeof("_SERVER") - 1, 0);
     532           2 :                                 zend_is_auto_global(name TSRMLS_CC);
     533             :                                 zend_string_release(name);
     534             :                         }
     535           2 :                         array_ptr = &IF_G(server_array);
     536           2 :                         break;
     537             :                 case PARSE_ENV:
     538           0 :                         if (PG(auto_globals_jit)) {
     539           0 :                                 zend_string *name = zend_string_init("_ENV", sizeof("_ENV") - 1, 0);
     540           0 :                                 zend_is_auto_global(name TSRMLS_CC);
     541             :                                 zend_string_release(name);
     542             :                         }
     543           0 :                         array_ptr = &IF_G(env_array) ? &IF_G(env_array) : &PG(http_globals)[TRACK_VARS_ENV];
     544           0 :                         break;
     545             :                 case PARSE_SESSION:
     546             :                         /* FIXME: Implement session source */
     547           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "INPUT_SESSION is not yet implemented");
     548           0 :                         break;
     549             :                 case PARSE_REQUEST:
     550             :                         /* FIXME: Implement request source */
     551           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "INPUT_REQUEST is not yet implemented");
     552             :                         break;
     553             :         }
     554             : 
     555          48 :         return array_ptr;
     556             : }
     557             : /* }}} */
     558             : 
     559             : /* {{{ proto mixed filter_has_var(constant type, string variable_name)
     560             :  * Returns true if the variable with the name 'name' exists in source.
     561             :  */
     562          31 : PHP_FUNCTION(filter_has_var)
     563             : {
     564             :         zend_long         arg;
     565             :         zend_string *var;
     566          31 :         zval        *array_ptr = NULL;
     567             : 
     568          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lS", &arg, &var) == FAILURE) {
     569           9 :                 RETURN_FALSE;
     570             :         }
     571             : 
     572          22 :         array_ptr = php_filter_get_storage(arg TSRMLS_CC);
     573             : 
     574          62 :         if (array_ptr && HASH_OF(array_ptr) && zend_hash_exists(HASH_OF(array_ptr), var)) {
     575           9 :                 RETURN_TRUE;
     576             :         }
     577             : 
     578          13 :         RETURN_FALSE;
     579             : }
     580             : /* }}} */
     581             : 
     582        1007 : static void php_filter_call(zval *filtered, zend_long filter, zval *filter_args, const int copy, zend_long filter_flags TSRMLS_DC) /* {{{ */
     583             : {
     584        1007 :         zval *options = NULL;
     585             :         zval *option;
     586        1007 :         char *charset = NULL;
     587             : 
     588        1950 :         if (filter_args && Z_TYPE_P(filter_args) != IS_ARRAY) {
     589         183 :                 zend_long lval = zval_get_long(filter_args);
     590             : 
     591         183 :                 if (filter != -1) { /* handler for array apply */
     592             :                         /* filter_args is the filter_flags */
     593         172 :                         filter_flags = lval;
     594             : 
     595         172 :                         if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
     596         167 :                                 filter_flags |= FILTER_REQUIRE_SCALAR;
     597             :                         }
     598             :                 } else {
     599          11 :                         filter = lval;
     600             :                 }
     601         824 :         } else if (filter_args) {
     602         577 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "filter", sizeof("filter") - 1)) != NULL) {
     603           9 :                         filter = zval_get_long(option);
     604             :                 }
     605             : 
     606         577 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "flags", sizeof("flags") - 1)) != NULL) {
     607         311 :                         filter_flags = zval_get_long(option);
     608             : 
     609         311 :                         if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
     610         300 :                                 filter_flags |= FILTER_REQUIRE_SCALAR;
     611             :                         }
     612             :                 }
     613             : 
     614         577 :                 if ((option = zend_hash_str_find(HASH_OF(filter_args), "options", sizeof("options") - 1)) != NULL) {
     615         264 :                         if (filter != FILTER_CALLBACK) {
     616         227 :                                 if (Z_TYPE_P(option) == IS_ARRAY) {
     617          37 :                                         options = option;
     618             :                                 }
     619             :                         } else {
     620          37 :                                 options = option;
     621          37 :                                 filter_flags = 0;
     622             :                         }
     623             :                 }
     624             :         }
     625             : 
     626        1007 :         if (Z_TYPE_P(filtered) == IS_ARRAY) {
     627          20 :                 if (filter_flags & FILTER_REQUIRE_SCALAR) {
     628           5 :                         if (copy) {
     629           6 :                                 SEPARATE_ZVAL(filtered);
     630             :                         }
     631           5 :                         zval_ptr_dtor(filtered);
     632           5 :                         if (filter_flags & FILTER_NULL_ON_FAILURE) {
     633           0 :                                 ZVAL_NULL(filtered);
     634             :                         } else {
     635           5 :                                 ZVAL_FALSE(filtered);
     636             :                         }
     637           5 :                         return;
     638             :                 }
     639          15 :                 php_zval_filter_recursive(filtered, filter, filter_flags, options, charset, copy TSRMLS_CC);
     640          15 :                 return;
     641             :         }
     642         987 :         if (filter_flags & FILTER_REQUIRE_ARRAY) {
     643           1 :                 if (copy) {
     644           1 :                         SEPARATE_ZVAL(filtered);
     645             :                 }
     646           1 :                 zval_ptr_dtor(filtered);
     647           1 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     648           0 :                         ZVAL_NULL(filtered);
     649             :                 } else {
     650           1 :                         ZVAL_FALSE(filtered);
     651             :                 }
     652           1 :                 return;
     653             :         }
     654             : 
     655         986 :         php_zval_filter(filtered, filter, filter_flags, options, charset, copy TSRMLS_CC);
     656         986 :         if (filter_flags & FILTER_FORCE_ARRAY) {
     657             :                 zval tmp;
     658           5 :                 ZVAL_COPY_VALUE(&tmp, filtered);
     659           5 :                 array_init(filtered);
     660           5 :                 add_next_index_zval(filtered, &tmp);
     661             :         }
     662             : }
     663             : /* }}} */
     664             : 
     665          23 : static void php_filter_array_handler(zval *input, zval *op, zval *return_value, zend_bool add_empty TSRMLS_DC) /* {{{ */
     666             : {
     667             :         zend_string *arg_key;
     668             :         zval *tmp, *arg_elm;
     669             : 
     670          23 :         if (!op) {
     671           2 :                 zval_ptr_dtor(return_value);
     672           2 :                 ZVAL_DUP(return_value, input);
     673           2 :                 php_filter_call(return_value, FILTER_DEFAULT, NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
     674          21 :         } else if (Z_TYPE_P(op) == IS_LONG) {
     675           1 :                 zval_ptr_dtor(return_value);
     676           1 :                 ZVAL_DUP(return_value, input);
     677           1 :                 php_filter_call(return_value, Z_LVAL_P(op), NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
     678          20 :         } else if (Z_TYPE_P(op) == IS_ARRAY) {
     679          15 :                 array_init(return_value);
     680             : 
     681          81 :                 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(op), arg_key, arg_elm) {
     682          34 :                         if (arg_key == NULL) {
     683           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Numeric keys are not allowed in the definition array");
     684           0 :                                 zval_ptr_dtor(return_value);
     685           0 :                                 RETURN_FALSE;
     686             :                         }
     687          34 :                         if (arg_key->len == 0) {
     688           2 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty keys are not allowed in the definition array");
     689           2 :                                 zval_ptr_dtor(return_value);
     690           2 :                                 RETURN_FALSE;
     691             :                         }
     692          32 :                         if ((tmp = zend_hash_find(Z_ARRVAL_P(input), arg_key)) == NULL) {
     693          10 :                                 if (add_empty) {
     694           9 :                                         add_assoc_null_ex(return_value, arg_key->val, arg_key->len);
     695             :                                 }
     696             :                         } else {
     697             :                                 zval nval;
     698          22 :                                 ZVAL_DEREF(tmp);
     699          22 :                                 ZVAL_DUP(&nval, tmp);
     700          22 :                                 php_filter_call(&nval, -1, arg_elm, 0, FILTER_REQUIRE_SCALAR TSRMLS_CC);
     701          22 :                                 zend_hash_update(Z_ARRVAL_P(return_value), arg_key, &nval);
     702             :                         }
     703             :                 } ZEND_HASH_FOREACH_END();
     704             :         } else {
     705           5 :                 RETURN_FALSE;
     706             :         }
     707             : }
     708             : /* }}} */
     709             : 
     710             : /* {{{ proto mixed filter_input(constant type, string variable_name [, long filter [, mixed options]])
     711             :  * Returns the filtered variable 'name'* from source `type`.
     712             :  */
     713          26 : PHP_FUNCTION(filter_input)
     714             : {
     715          26 :         zend_long fetch_from, filter = FILTER_DEFAULT;
     716          26 :         zval *filter_args = NULL, *tmp;
     717          26 :         zval *input = NULL;
     718             :         zend_string *var;
     719             : 
     720          26 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lS|lz", &fetch_from, &var, &filter, &filter_args) == FAILURE) {
     721           1 :                 return;
     722             :         }
     723             : 
     724          25 :         if (!PHP_FILTER_ID_EXISTS(filter)) {
     725           0 :                 RETURN_FALSE;
     726             :         }
     727             : 
     728          25 :         input = php_filter_get_storage(fetch_from TSRMLS_CC);
     729             : 
     730          75 :         if (!input || !HASH_OF(input) || (tmp = zend_hash_find(HASH_OF(input), var)) == NULL) {
     731           5 :                 zend_long filter_flags = 0;
     732             :                 zval *option, *opt, *def;
     733           5 :                 if (filter_args) {
     734           2 :                         if (Z_TYPE_P(filter_args) == IS_LONG) {
     735           0 :                                 filter_flags = Z_LVAL_P(filter_args);
     736           3 :                         } else if (Z_TYPE_P(filter_args) == IS_ARRAY && (option = zend_hash_str_find(HASH_OF(filter_args), "flags", sizeof("flags") - 1)) != NULL) {
     737           1 :                                 filter_flags = zval_get_long(option);
     738             :                         }
     739           5 :                         if (Z_TYPE_P(filter_args) == IS_ARRAY && 
     740           2 :                                 (opt = zend_hash_str_find(HASH_OF(filter_args), "options", sizeof("options") - 1)) != NULL &&
     741             :                                 Z_TYPE_P(opt) == IS_ARRAY &&
     742           1 :                                 (def = zend_hash_str_find(HASH_OF(opt), "default", sizeof("default") - 1)) != NULL) {
     743           1 :                                 ZVAL_COPY(return_value, def);
     744           1 :                                 return;
     745             :                         }
     746             :                 }
     747             : 
     748             :                 /* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
     749             :                  * the function: normally when validation fails false is returned, and
     750             :                  * when the input value doesn't exist NULL is returned. With the flag
     751             :                  * set, NULL and false should be returned, respectively. Ergo, although
     752             :                  * the code below looks incorrect, it's actually right. */
     753           4 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     754           0 :                         RETURN_FALSE;
     755             :                 } else {
     756           4 :                         RETURN_NULL();
     757             :                 }
     758             :         }
     759             : 
     760          20 :         ZVAL_DUP(return_value, tmp);
     761             : 
     762          20 :         php_filter_call(return_value, filter, filter_args, 1, FILTER_REQUIRE_SCALAR TSRMLS_CC);
     763             : }
     764             : /* }}} */
     765             : 
     766             : /* {{{ proto mixed filter_var(mixed variable [, long filter [, mixed options]])
     767             :  * Returns the filtered version of the variable.
     768             :  */
     769         968 : PHP_FUNCTION(filter_var)
     770             : {
     771         968 :         zend_long filter = FILTER_DEFAULT;
     772         968 :         zval *filter_args = NULL, *data;
     773             : 
     774         968 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|lz", &data, &filter, &filter_args) == FAILURE) {
     775           4 :                 return;
     776             :         }
     777             : 
     778         964 :         if (!PHP_FILTER_ID_EXISTS(filter)) {
     779           2 :                 RETURN_FALSE;
     780             :         }
     781             : 
     782         962 :         ZVAL_DUP(return_value, data);
     783             : 
     784         962 :         php_filter_call(return_value, filter, filter_args, 1, FILTER_REQUIRE_SCALAR TSRMLS_CC);
     785             : }
     786             : /* }}} */
     787             : 
     788             : /* {{{ proto mixed filter_input_array(constant type, [, mixed options [, bool add_empty]]])
     789             :  * Returns an array with all arguments defined in 'definition'.
     790             :  */
     791           1 : PHP_FUNCTION(filter_input_array)
     792             : {
     793             :         zend_long    fetch_from;
     794           1 :         zval   *array_input = NULL, *op = NULL;
     795           1 :         zend_bool add_empty = 1;
     796             : 
     797           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|zb",  &fetch_from, &op, &add_empty) == FAILURE) {
     798           0 :                 return;
     799             :         }
     800             : 
     801           2 :         if (op && (Z_TYPE_P(op) != IS_ARRAY) && (Z_TYPE_P(op) == IS_LONG && !PHP_FILTER_ID_EXISTS(Z_LVAL_P(op)))) {
     802           0 :                 RETURN_FALSE;
     803             :         }
     804             : 
     805           1 :         array_input = php_filter_get_storage(fetch_from TSRMLS_CC);
     806             : 
     807           2 :         if (!array_input || !HASH_OF(array_input)) {
     808           0 :                 zend_long filter_flags = 0;
     809             :                 zval *option;
     810           0 :                 if (op) {
     811           0 :                         if (Z_TYPE_P(op) == IS_LONG) {
     812           0 :                                 filter_flags = Z_LVAL_P(op);
     813           0 :                         } else if (Z_TYPE_P(op) == IS_ARRAY && (option = zend_hash_str_find(HASH_OF(op), "flags", sizeof("flags") - 1)) != NULL) {
     814           0 :                                 filter_flags = zval_get_long(option);
     815             :                         }
     816             :                 }
     817             : 
     818             :                 /* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
     819             :                  * the function: normally when validation fails false is returned, and
     820             :                  * when the input value doesn't exist NULL is returned. With the flag
     821             :                  * set, NULL and false should be returned, respectively. Ergo, although
     822             :                  * the code below looks incorrect, it's actually right. */
     823           0 :                 if (filter_flags & FILTER_NULL_ON_FAILURE) {
     824           0 :                         RETURN_FALSE;
     825             :                 } else {
     826           0 :                         RETURN_NULL();
     827             :                 }
     828             :         }
     829             : 
     830           1 :         php_filter_array_handler(array_input, op, return_value, add_empty TSRMLS_CC);
     831             : }
     832             : /* }}} */
     833             : 
     834             : /* {{{ proto mixed filter_var_array(array data, [, mixed options [, bool add_empty]]])
     835             :  * Returns an array with all arguments defined in 'definition'.
     836             :  */
     837          31 : PHP_FUNCTION(filter_var_array)
     838             : {
     839          31 :         zval *array_input = NULL, *op = NULL;
     840          31 :         zend_bool add_empty = 1;
     841             : 
     842          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|zb",  &array_input, &op, &add_empty) == FAILURE) {
     843           1 :                 return;
     844             :         }
     845             : 
     846          72 :         if (op && (Z_TYPE_P(op) != IS_ARRAY) && (Z_TYPE_P(op) == IS_LONG && !PHP_FILTER_ID_EXISTS(Z_LVAL_P(op)))) {
     847           8 :                 RETURN_FALSE;
     848             :         }
     849             : 
     850          22 :         php_filter_array_handler(array_input, op, return_value, add_empty TSRMLS_CC);
     851             : }
     852             : /* }}} */
     853             : 
     854             : /* {{{ proto filter_list()
     855             :  * Returns a list of all supported filters */
     856           3 : PHP_FUNCTION(filter_list)
     857             : {
     858           3 :         int i, size = sizeof(filter_list) / sizeof(filter_list_entry);
     859             : 
     860           3 :         if (zend_parse_parameters_none() == FAILURE) {
     861           1 :                 return;
     862             :         }
     863             : 
     864           2 :         array_init(return_value);
     865          44 :         for (i = 0; i < size; ++i) {
     866          42 :                 add_next_index_string(return_value, (char *)filter_list[i].name);
     867             :         }
     868             : }
     869             : /* }}} */
     870             : 
     871             : /* {{{ proto filter_id(string filtername)
     872             :  * Returns the filter ID belonging to a named filter */
     873         218 : PHP_FUNCTION(filter_id)
     874             : {
     875             :         int i;
     876             :         size_t filter_len;
     877         218 :         int size = sizeof(filter_list) / sizeof(filter_list_entry);
     878             :         char *filter;
     879             : 
     880         218 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filter, &filter_len) == FAILURE) {
     881           2 :                 return;
     882             :         }
     883             : 
     884        2393 :         for (i = 0; i < size; ++i) {
     885        2391 :                 if (strcmp(filter_list[i].name, filter) == 0) {
     886         214 :                         RETURN_LONG(filter_list[i].id);
     887             :                 }
     888             :         }
     889             : 
     890           2 :         RETURN_FALSE;
     891             : }
     892             : /* }}} */
     893             : 
     894             : /*
     895             :  * Local variables:
     896             :  * tab-width: 4
     897             :  * c-basic-offset: 4
     898             :  * End:
     899             :  * vim600: noet sw=4 ts=4 fdm=marker
     900             :  * vim<600: noet sw=4 ts=4
     901             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:12 +0000 (9 days ago)

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